PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-help/tags/0.1/wp-help.php

https://github.com/brandonburke/WordPress-Plugin-Baseline
PHP | 200 lines | 170 code | 14 blank | 16 comment | 31 complexity | aa6c05d81b66e4d86f83446e40efecdc MD5 | raw file
  1. <?php
  2. /*
  3. Plugin Name: WP Help
  4. Description: Administrators can create detailed, hierarchical documentation for the site's authors and editors, viewable in the WordPress admin.
  5. Version: 0.1
  6. Author: Mark Jaquith
  7. Author URI: http://coveredwebservices.com/
  8. */
  9. class CWS_WP_Help_Plugin {
  10. public static $instance;
  11. const default_doc = 'cws_wp_help_default_doc';
  12. public function __construct() {
  13. self::$instance = $this;
  14. add_action( 'init', array( $this, 'init' ) );
  15. }
  16. public function init() {
  17. add_action( 'admin_menu', array( $this, 'admin_menu' ) );
  18. add_action( 'do_meta_boxes', array( $this, 'do_meta_boxes' ), 20, 2 );
  19. add_action( 'save_post', array( $this, 'save_post' ) );
  20. add_filter( 'post_type_link', array( $this, 'page_link' ), 10, 2 );
  21. add_filter( 'post_updated_messages', array( $this, 'post_updated_messages' ) );
  22. add_action( 'admin_init', array( $this, 'ajax_listener' ) );
  23. register_post_type( 'wp-help',
  24. array(
  25. 'label' => __( 'Publishing Help' ),
  26. // 'description' => '',
  27. 'public' => false,
  28. 'show_ui' => true,
  29. 'show_in_menu' => false,
  30. 'hierarchical' => true,
  31. 'supports' => array( 'title', 'editor', 'revisions', 'page-attributes' ),
  32. 'capabilities' => array(
  33. 'publish_posts' => 'manage_options',
  34. 'edit_posts' => 'manage_options',
  35. 'edit_others_posts' => 'manage_options',
  36. 'delete_posts' => 'manage_options',
  37. 'read_private_posts' => 'manage_options',
  38. 'edit_post' => 'manage_options',
  39. 'delete_post' => 'manage_options',
  40. 'read_post' => 'read'
  41. ),
  42. 'labels' => array (
  43. 'name' => __( 'Help Documents' ),
  44. 'singular_name' => __( 'Help Document' ),
  45. 'add_new' => __( 'Add New' ),
  46. 'add_new_item' => __( 'Add New Help Document' ),
  47. 'edit' => __( 'Edit' ),
  48. 'edit_item' => __( 'Edit Help Document' ),
  49. 'new_item' => __( 'New Help Document' ),
  50. 'view' => __( 'View' ),
  51. 'view_item' => __( 'View Help Document' ),
  52. 'search_items' => __( 'Search Documents' ),
  53. 'not_found' => __( 'No Help Documents Found' ),
  54. 'not_found_in_trash' => __( 'No Help Documents found in Trash' ),
  55. 'parent' => __( 'Parent Help Document' )
  56. )
  57. )
  58. );
  59. }
  60. public function ajax_listener() {
  61. if ( !defined( 'DOING_AJAX' ) || !DOING_AJAX || !isset( $_POST['action'] ) || 'wp-link-ajax' != $_POST['action'] )
  62. return;
  63. // It's the right kind of request
  64. // Now to see if it originated from our post type
  65. $qs = parse_url( $_SERVER['HTTP_REFERER'], PHP_URL_QUERY );
  66. wp_parse_str( $qs, $vars );
  67. if ( isset( $vars['post_type'] ) ) {
  68. $post_type = $vars['post_type'];
  69. } elseif ( isset( $vars['post'] ) ) {
  70. $post = get_post( $vars['post'] );
  71. $post_type = $post->post_type;
  72. } else {
  73. // Can't determine post type. Bail.
  74. return;
  75. }
  76. if ( 'wp-help' == $post_type ) {
  77. // Nice! This originated from our post type
  78. // Now we make our post type public, and all others non-public
  79. // There really should be a better way to do this. :-\
  80. global $wp_post_types;
  81. foreach ( $wp_post_types as $name => $type ) {
  82. $wp_post_types[$name]->publicly_queryable = false;
  83. }
  84. $wp_post_types['wp-help']->publicly_queryable = true;
  85. }
  86. }
  87. public function admin_menu() {
  88. $hook = add_dashboard_page( __( 'Publishing Help' ), __( 'Publishing Help' ), 'publish_posts', 'wp-help-documents', array( $this, 'render_listing_page' ) );
  89. add_action( "load-{$hook}", array( $this, 'enqueue' ) );
  90. }
  91. public function do_meta_boxes( $page, $context ) {
  92. if ( 'wp-help' == $page && 'side' == $context )
  93. add_meta_box( 'cws-wp-help-meta', __( 'WP Help Options' ), array( $this, 'meta_box' ), $page, 'side' );
  94. }
  95. public function meta_box() {
  96. global $post;
  97. wp_nonce_field( 'cws-wp-help-save', '_cws_wp_help_nonce', false, true ); ?>
  98. <p><input type="checkbox" name="cws_wp_help_make_default_doc" id="cws_wp_help_make_default_doc" <?php checked( $post->ID == get_option( self::default_doc ) ); ?> /> <label for="cws_wp_help_make_default_doc"><?php _e( 'Make this the default help document' ); ?></label></p>
  99. <?php
  100. }
  101. public function save_post( $post_id ) {
  102. if ( wp_verify_nonce( $_POST['_cws_wp_help_nonce'], 'cws-wp-help-save' ) ) {
  103. if ( isset( $_POST['cws_wp_help_make_default_doc'] ) ) {
  104. // Make it the default_doc
  105. update_option( self::default_doc, absint( $post_id ) );
  106. } elseif ( $post_id == get_option( self::default_doc ) ) {
  107. // Unset
  108. update_option( self::default_doc, 0 );
  109. }
  110. }
  111. return $post_id;
  112. }
  113. public function post_updated_messages( $messages ) {
  114. global $post_ID, $post;
  115. $messages['wp-help'] = array(
  116. 0 => '', // Unused. Messages start at index 1.
  117. 1 => sprintf( __('Document updated. <a href="%s">View document</a>'), esc_url( get_permalink($post_ID) ) ),
  118. 2 => __('Custom field updated.'),
  119. 3 => __('Custom field deleted.'),
  120. 4 => __('Document updated.'),
  121. 5 => isset($_GET['revision']) ? sprintf( __('Document restored to revision from %s'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
  122. 6 => sprintf( __('Document published. <a href="%s">View document</a>'), esc_url( get_permalink($post_ID) ) ),
  123. 7 => __('Document saved.'),
  124. 8 => sprintf( __('Document submitted. <a target="_blank" href="%s">Preview document</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  125. 9 => sprintf( __('Document scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview document</a>'), date_i18n( __( 'M j, Y @ G:i' ), strtotime( $post->post_date ) ), esc_url( get_permalink($post_ID) ) ),
  126. 10 => sprintf( __('Document draft updated. <a target="_blank" href="%s">Preview document</a>'), esc_url( add_query_arg( 'preview', 'true', get_permalink($post_ID) ) ) ),
  127. );
  128. return $messages;
  129. }
  130. public function enqueue() {
  131. $suffix = defined ('SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : '';
  132. wp_enqueue_style( 'cws-wp-help', plugins_url( "wp-help$suffix.css", __FILE__ ), array(), '20110518b' );
  133. }
  134. public function page_link( $link, $post ) {
  135. $post = get_post( $post );
  136. if ( 'wp-help' == $post->post_type )
  137. return admin_url( 'index.php?page=wp-help-documents&document=' . absint( $post->ID ) );
  138. else
  139. return $link;
  140. }
  141. private function get_help_topics_html() {
  142. return wp_list_pages( array( 'post_type' => 'wp-help', 'hierarchical' => true, 'echo' => false, 'title_li' => '' ) );
  143. }
  144. public function render_listing_page() {
  145. if ( !isset( $_GET['document'] ) && get_option( self::default_doc ) )
  146. $_GET['document'] = get_option( self::default_doc );
  147. if ( isset( $_GET['document'] ) ) : ?>
  148. <style>
  149. div#cws-wp-help-listing .page-item-<?php echo absint( $_GET['document'] ); ?> > a {
  150. font-weight: bold;
  151. }
  152. </style>
  153. <?php endif; ?>
  154. <div class="wrap">
  155. <?php screen_icon(); ?><h2><?php _e( 'Publishing Help' ); ?></h2>
  156. <?php $pages = $this->get_help_topics_html(); ?>
  157. <?php if ( trim( $pages ) ) : ?>
  158. <div id="cws-wp-help-listing">
  159. <h3><?php _e( 'Help Topics' ); ?><?php if ( current_user_can( 'publish_pages' ) ) : ?><span><a href="<?php echo admin_url( 'edit.php?post_type=wp-help' ); ?>">Manage</a></span><?php endif; ?></h3>
  160. <ul>
  161. <?php echo $pages; ?>
  162. </ul>
  163. </div>
  164. <div id="cws-wp-help-document">
  165. <?php if ( $_GET['document'] ) : ?>
  166. <?php $document = new WP_Query( array( 'post_type' => 'wp-help', 'p' => absint( $_GET['document'] ) ) ); ?>
  167. <?php if ( $document->have_posts() ) : $document->the_post(); ?>
  168. <h2><?php the_title(); ?></h2>
  169. <?php the_content(); ?>
  170. <?php else : ?>
  171. <p><?php _e( 'The requested help document could not be found' ); ?>
  172. <?php endif; ?>
  173. <?php endif; ?>
  174. </div>
  175. <?php else : ?>
  176. <?php if ( current_user_can( 'manage_options' ) ) : ?>
  177. <p><?php printf( __( 'No published help documents found. <a href="%s">Manage Help Documents</a>.' ), admin_url( 'edit.php?post_type=wp-help' ) ); ?></p>
  178. <?php else : ?>
  179. <p><?php _e( 'No help documents found. Contact the site administrator.' ); ?></p>
  180. <?php endif; ?>
  181. <?php endif; ?>
  182. </div>
  183. <?php
  184. }
  185. }
  186. new CWS_WP_Help_Plugin;