Changes in sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php [10837:10356]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php
r10837 r10356 8 8 class WPorg_Handbook { 9 9 10 /**11 * The handbook post type.12 *13 * @var string14 */15 10 public $post_type = ''; 16 17 /**18 * The handbook's settings.19 *20 * @var array21 */22 public $settings = [];23 24 /**25 * The name of the setting for the handbook's name.26 *27 * @var string28 */29 11 public $setting_name = ''; 30 12 31 /**32 * The memoized and filtered label text for the handbook.33 *34 * @var string35 */36 13 protected $label = ''; 37 14 38 /** 39 * The associated importer object, if warranted. 40 */ 41 protected $importer; 42 43 /** 44 * Returns the custom handbook-related capabilities granted to site users. 45 * 46 * @return array 47 */ 48 public static function caps() { 49 return [ 50 'edit_handbook_pages', 51 'edit_others_handbook_pages', 15 static function caps() { 16 return array( 17 'edit_handbook_pages', 'edit_others_handbook_pages', 52 18 'edit_published_handbook_pages', 53 ]; 54 } 55 56 /** 57 * Returns the custom capabilities granted to handbook editors. 58 * 59 * @return array 60 */ 61 public static function editor_caps() { 62 return [ 19 ); 20 } 21 22 static function editor_caps() { 23 return array( 63 24 'publish_handbook_pages', 64 'delete_handbook_pages', 65 'delete_others_handbook_pages', 66 'delete_published_handbook_pages', 67 'delete_private_handbook_pages', 68 'edit_private_handbook_pages', 69 'read_private_handbook_pages', 70 ]; 71 } 72 73 /** 74 * Returns handbook default config. 75 * 76 * @return array 77 */ 78 public static function get_default_handbook_config() { 79 /** 80 * Filters default handbook configuration array. 81 * 82 * @param array $config { 83 * Associative array of handbook configuration. 84 * 85 * @type string $cron_interval The cron interval for which an imported handbook gets 86 * imported, e.g. 'hourly', 'daily'. If defined as an 87 * unrecognized interval, 'hourly' will be used. 88 * Default '15_minutes'. 89 * @type string $label The label for the handbook. Default is the 90 * post type slug converted to titlecase (e.g. 91 * plugin-handbok => "Plugin Handbook"). 92 * @type string manifest The URL to the manifest JSON file for an imported 93 * handbook. 94 * @type string $slug The slug for the post type. Default is the 95 * post type. 96 * } 97 */ 98 return (array) apply_filters( 'handbook_default_handbook_config', [ 99 'cron_interval' => '15_minutes', 100 'label' => '', 101 'manifest' => '', 102 'slug' => '', 103 ] ); 25 'delete_handbook_pages', 'delete_others_handbook_pages', 26 'delete_published_handbook_pages', 'delete_private_handbook_pages', 27 'edit_private_handbook_pages', 'read_private_handbook_pages', 28 ); 104 29 } 105 30 … … 113 38 * @return string 114 39 */ 115 publicstatic function get_name( $post_type = 'handbook', $raw = false ) {40 static function get_name( $post_type = 'handbook', $raw = false ) { 116 41 // Prefer explicitly configured handbook name. 117 42 $name = get_option( $post_type . '_name' ); 118 43 119 // If handbook name isn't set, try configured label.120 if ( ! $raw && ! $name ) {121 $config = WPorg_Handbook_Init::get_handbooks_config( $post_type );122 if ( ! empty( $config['label'] ) ) {123 $name = $config['label'];124 }125 }126 127 44 // If handbook name isn't set, try root relative site path. 128 if ( ! $raw && ! $name) {45 if ( ! $raw && empty( $name ) ) { 129 46 if ( is_multisite() ) { 130 47 $name = trim( get_blog_details()->path, '/' ); … … 134 51 135 52 // If no name defined yet, try handbook post type if not standard. 136 if ( ! $name && ( 'handbook' !== $post_type ) ) {137 $name = str_replace( '-handbook', '', $post_type);53 if ( empty( $name ) && ( 'handbook' != $post_type ) ) { 54 $name = ucfirst( substr( $post_type, 0, -9 ) ); 138 55 } 139 56 140 57 $name .= ' Handbook'; 141 $name = ucfirst( $name );142 58 } 143 59 … … 145 61 } 146 62 147 /** 148 * Constructor 149 * 150 * @param string $type The post type for the handbook. 151 * @param array $config The config array for the handbook. 152 */ 153 public function __construct( $type, $config = [] ) { 154 $this->post_type = sanitize_title( $type ); 155 156 $config = $this->config = wp_parse_args( $config, self::get_default_handbook_config() ); 157 158 $this->label = apply_filters( 159 'handbook_label', 160 $config['label'] ?: ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) ), 161 $this->post_type 162 ); 63 function __construct( $type ) { 64 if ( 'handbook' != $type ) { 65 $this->post_type = $type . '-handbook'; 66 } else { 67 $this->post_type = $type; 68 } 69 70 $this->label = ucwords( str_replace( array( '-', '_' ), ' ', $this->post_type ) ); 71 $this->label = apply_filters( 'handbook_label', $this->label, $this->post_type ); 163 72 164 73 $this->setting_name = $this->post_type . '_name'; 165 74 166 add_action( 'after_handbooks_init', [ $this, 'init_importer' ] ); 167 add_filter( 'user_has_cap', [ $this, 'grant_handbook_caps' ] ); 168 add_action( 'widgets_init', [ $this, 'register_post_type' ] ); 169 add_filter( 'post_type_link', [ $this, 'post_type_link' ], 10, 2 ); 170 add_action( 'template_redirect', [ $this, 'redirect_handbook_root_page' ] ); 171 add_filter( 'template_include', [ $this, 'template_include' ] ); 172 add_filter( 'pre_get_posts', [ $this, 'pre_get_posts' ] ); 173 add_filter( 'posts_pre_query', [ $this, 'posts_pre_query' ], 10, 2 ); 174 add_action( 'widgets_init', [ $this, 'handbook_sidebar' ], 11 ); // After P2 175 add_action( 'wporg_email_changes_for_post_types', [ $this, 'wporg_email_changes_for_post_types' ] ); 176 add_action( 'p2_action_links', [ $this, 'disable_p2_resolved_posts_action_links' ] ); 177 add_action( 'admin_init', [ $this, 'add_name_setting' ] ); 178 add_filter( 'body_class', [ $this, 'add_body_class' ] ); 179 add_filter( 'post_class', [ $this, 'add_post_class' ] ); 180 add_filter( 'o2_process_the_content', [ $this, 'disable_o2_processing' ] ); 181 add_filter( 'o2_application_container', [ $this, 'o2_application_container' ] ); 182 add_filter( 'o2_view_type', [ $this, 'o2_view_type' ] ); 183 add_filter( 'o2_post_fragment', [ $this, 'o2_post_fragment' ] ); 184 add_filter( 'comments_open', [ $this, 'comments_open' ], 10, 2 ); 185 add_filter( 'wp_nav_menu_objects', [ $this, 'highlight_menu_handbook_link' ] ); 186 add_filter( 'display_post_states', [ $this, 'display_post_states' ], 10, 2 ); 187 } 188 189 /** 190 * Returns the configuration array for handbooks. 191 * 192 * @return array 193 */ 194 public function get_config() { 195 return $this->config; 196 } 197 198 /** 199 * Returns the handbook's importer object, if applicable. 200 * 201 * @return WPorg_Handbook_Importer|null 202 */ 203 public function get_importer() { 204 return $this->importer; 205 } 206 207 /** 208 * Initializes the importer, if applicable. 209 */ 210 public function init_importer() { 211 $config = $this->get_config(); 212 213 if ( class_exists( 'WPorg_Handbook_Importer' ) ) { 214 if ( WPorg_Handbook_Importer::is_handbook_imported( $this->post_type ) ) { 215 $this->importer = new WPorg_Handbook_Importer( $this ); 216 } 217 } elseif ( is_admin() && ( $config['manifest'] ?: false ) ) { 218 add_action( 'admin_notices', function () { 219 echo '<div class="notice notice-error"><p>' . __( 'Error: The <strong>WPORG Markdown Importer</strong> plugin needs to be activated in order to allow importing of handbooks.', 'wporg' ) . '</p></div>'; 220 } ); 221 } 75 add_filter( 'user_has_cap', array( $this, 'grant_handbook_caps' ) ); 76 add_action( 'widgets_init', array( $this, 'register_post_type' ) ); 77 add_filter( 'post_type_link', array( $this, 'post_type_link' ), 10, 2 ); 78 add_action( 'template_redirect', array( $this, 'redirect_handbook_root_page' ) ); 79 add_filter( 'template_include', array( $this, 'template_include' ) ); 80 add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); 81 add_action( 'widgets_init', array( $this, 'handbook_sidebar' ), 11 ); // After P2 82 add_action( 'wporg_email_changes_for_post_types', array( $this, 'wporg_email_changes_for_post_types' ) ); 83 add_action( 'p2_action_links', array( $this, 'disable_p2_resolved_posts_action_links' ) ); 84 add_action( 'admin_init', array( $this, 'add_name_setting' ) ); 85 add_filter( 'body_class', array( $this, 'add_body_class' ) ); 86 add_filter( 'post_class', array( $this, 'add_post_class' ) ); 87 add_filter( 'o2_process_the_content', array( $this, 'disable_o2_processing' ) ); 88 add_filter( 'o2_application_container', array( $this, 'o2_application_container' ) ); 89 add_filter( 'o2_view_type', array( $this, 'o2_view_type' ) ); 90 add_filter( 'o2_post_fragment', array( $this, 'o2_post_fragment' ) ); 91 add_filter( 'comments_open', array( $this, 'comments_open' ), 10, 2 ); 92 add_filter( 'wp_nav_menu_objects', array( $this, 'highlight_menu_handbook_link' ) ); 93 add_filter( 'display_post_states', array( $this, 'display_post_states' ), 10, 2 ); 222 94 } 223 95 … … 229 101 * @return string[] 230 102 */ 231 publicfunction display_post_states( $post_states, $post ) {103 function display_post_states( $post_states, $post ) { 232 104 if ( $this->post_is_landing_page( $post ) ) { 233 105 $post_states[] = __( 'Handbook Front Page', 'wporg' ); … … 247 119 * @return array 248 120 */ 249 publicfunction add_body_class( $classes ) {121 function add_body_class( $classes ) { 250 122 if ( is_singular() && wporg_is_handbook( $this->post_type ) ) { 251 123 $classes[] = 'single-handbook'; … … 270 142 * @return array 271 143 */ 272 publicfunction add_post_class( $classes ) {144 function add_post_class( $classes ) { 273 145 if ( $this->post_type === get_post_type() ) { 274 146 $classes[] = 'type-handbook'; … … 278 150 } 279 151 280 /** 281 * Adds the setting for the handbook's name. 282 */ 283 public function add_name_setting() { 152 function add_name_setting() { 284 153 register_setting( 'general', $this->setting_name, 'esc_attr' ); 285 154 286 $label = ( 'handbook' == =$this->post_type ) ?155 $label = ( 'handbook' == $this->post_type ) ? 287 156 __( 'Handbook name', 'wporg' ) : 288 sprintf( __( 'Handbook name (%s)', 'wporg' ), s tr_replace( '-handbook', '', $this->post_type) );157 sprintf( __( 'Handbook name (%s)', 'wporg' ), substr( $this->post_type, 0, -9 ) ); 289 158 290 159 add_settings_field( 291 160 $this->setting_name, 292 161 '<label for="' . esc_attr( $this->setting_name ) . '">' . $label . '</label>', 293 [ $this, 'name_setting_html' ],162 array( $this, 'name_setting_html' ), 294 163 'general' 295 164 ); 296 165 } 297 166 298 /** 299 * Outputs the HTML for the input field for the handbook's name. 300 */ 301 public function name_setting_html() { 167 function name_setting_html() { 302 168 $value = get_option( $this->setting_name, '' ); 303 169 echo '<input type="text" id="' . esc_attr( $this->setting_name ) . '" name="' . esc_attr( $this->setting_name ) . '" value="' . esc_attr( $value ) . '" class="regular-text ltr" />'; 304 170 } 305 171 306 /** 307 * Grants handbook caps to the current user. 308 * 309 * @return array 310 */ 311 public function grant_handbook_caps( $caps ) { 172 function grant_handbook_caps( $caps ) { 312 173 if ( ! is_user_member_of_blog() ) { 313 174 return $caps; … … 327 188 } 328 189 329 /** 330 * Registers handbook post types. 331 */ 332 public function register_post_type() { 333 $config = $this->get_config(); 334 335 if ( ! empty( $config['slug'] ) ) { 336 $slug = $config['slug']; 337 } elseif ( 'handbook' !== $this->post_type ) { 338 $slug = str_replace( '-handbook', '', $this->post_type ); 190 function register_post_type() { 191 if ( 'handbook' != $this->post_type ) { 192 $slug = substr( $this->post_type, 0, -9 ); 339 193 } else { 340 194 $slug = 'handbook'; 341 195 } 342 196 343 $default_config = [344 'labels' => [197 $default_config = array( 198 'labels' => array( 345 199 'name' => $this->label, 346 200 'singular_name' => sprintf( __( '%s Page', 'wporg' ), $this->label ), 347 201 'menu_name' => $this->label, 348 202 'all_items' => sprintf( __( '%s Pages', 'wporg' ), $this->label ), 349 ],203 ), 350 204 'public' => true, 351 205 'show_ui' => true, … … 357 211 'menu_icon' => 'dashicons-book', 358 212 'menu_position' => 11, 359 'rewrite' => [213 'rewrite' => array( 360 214 'feeds' => false, 361 215 'slug' => $slug, 362 216 'with_front' => false, 363 ],217 ), 364 218 'delete_with_user' => false, 365 'supports' => [ 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'revisions', 'wpcom-markdown' ],366 ];219 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'revisions', 'wpcom-markdown' ), 220 ); 367 221 // Allow customization of the default post type configuration via filter. 368 $config = (array) apply_filters( 'handbook_post_type_defaults', $default_config, $slug ); 369 370 // Override the presumed label with a potentially customized value. 371 if ( ! empty( $config['labels']['name'] ) ) { 372 $this->label = $config['labels']['name']; 373 } 222 $config = apply_filters( 'handbook_post_type_defaults', $default_config, $slug ); 223 224 $this->label = $config['labels']['name']; 374 225 375 226 register_post_type( $this->post_type, $config ); … … 384 235 * handbook's landing page. 385 236 */ 386 p ublicfunction post_is_landing_page( $post = null ) {237 protected function post_is_landing_page( $post = null ) { 387 238 $is_landing_page = false; 388 239 … … 418 269 * @param WP_Post $post The post in question. 419 270 */ 420 publicfunction post_type_link( $post_link, $post ) {271 function post_type_link( $post_link, $post ) { 421 272 $post_type = get_post_type( $post ); 422 273 … … 433 284 * post type archive link for the handbook. 434 285 */ 435 publicfunction redirect_handbook_root_page() {286 function redirect_handbook_root_page() { 436 287 global $wp_query; 437 288 … … 458 309 * @return string 459 310 */ 460 publicfunction template_include( $template ) {311 function template_include( $template ) { 461 312 global $wp_query; 462 313 … … 466 317 } 467 318 468 $handbook_templates = [];319 $handbook_templates = array(); 469 320 470 321 // For singular handbook pages not of the 'handbook' post type. 471 322 if ( is_singular( $this->post_type ) && 'handbook' !== $this->post_type ) { 472 $handbook_templates = [ "single-{$this->post_type}.php", 'single-handbook.php' ];323 $handbook_templates = array( "single-{$this->post_type}.php", 'single-handbook.php' ); 473 324 } 474 325 // For handbook landing page. … … 489 340 } 490 341 491 /** 492 * Pre-emptively sets the query posts to the handbook landing page when 493 * appropriate. 494 * 495 * @param array $posts Posts. 496 * @param WP_Query $query Query object. 497 * @return array 498 */ 499 public function posts_pre_query( $posts, $query ) { 500 if ( $query->is_main_query() && ! $query->is_admin && ! $query->is_search && $query->is_handbook_root ) { 501 $posts = [ $query->is_handbook_root ]; 502 } 503 504 return $posts; 505 } 506 507 /** 508 * Performs query object adjustments for handbook requests prior to querying 509 * for posts. 510 */ 511 public function pre_get_posts( $query ) { 342 function pre_get_posts( $query ) { 512 343 // Bail early if query is not for this handbook's post type. 513 344 if ( get_query_var( 'post_type' ) !== $this->post_type ) { … … 526 357 $page = get_page_by_path( $this->post_type, OBJECT, $this->post_type ); 527 358 if ( ! $page ) { 528 $slug = str_replace( '-handbook', '', $this->post_type ); 529 if ( $slug !== $this->post_type ) { 530 $page = get_page_by_path( $slug, OBJECT, $this->post_type ); 531 } 532 } 533 if ( ! $page && 'handbook' !== $this->post_type ) { 359 $slug = substr( $this->post_type, 0, -9 ); 360 $page = get_page_by_path( $slug, OBJECT, $this->post_type ); 361 } 362 if ( ! $page ) { 534 363 $page = get_page_by_path( 'handbook', OBJECT, $this->post_type ); 535 364 } 536 if ( ! $page && 'welcome' !== $this->post_type) {365 if ( ! $page ) { 537 366 $page = get_page_by_path( 'welcome', OBJECT, $this->post_type ); 538 367 } 539 368 if ( $page ) { 540 369 $query->set( 'page_id', $page->ID ); 541 $query->is_handbook_root = $page;370 $query->is_handbook_root = true; 542 371 543 372 $query->is_archive = false; … … 550 379 } 551 380 552 /** 553 * Registers sidebar and widgets for handbook pages. 554 */ 555 public function handbook_sidebar() { 556 $sidebar_args = [ 557 'id' => $this->post_type, 558 'name' => sprintf( __( '%s Sidebar', 'wporg' ), $this->label ), 559 'description' => sprintf( __( 'Used on %s pages', 'wporg' ), $this->label ), 381 function handbook_sidebar() { 382 $sidebar_args = array( 383 'id' => $this->post_type, 384 'name' => sprintf( __( '%s Sidebar', 'wporg' ), $this->label ), 385 'description' => sprintf( __( 'Used on %s pages', 'wporg' ), $this->label ), 560 386 'before_widget' => '<aside id="%1$s" class="widget %2$s">', 561 387 'after_widget' => '</aside>', 562 388 'before_title' => '<h2 class="widget-title">', 563 389 'after_title' => '</h2>', 564 ];390 ); 565 391 566 392 $sidebar_args = apply_filters( 'wporg_handbook_sidebar_args', $sidebar_args, $this ); … … 572 398 } 573 399 574 /** 575 * Amends list of post types for which users can opt into receiving emails 576 * about changes. 577 * 578 * @param array $post_types Post types. 579 * @return array 580 */ 581 public function wporg_email_changes_for_post_types( $post_types ) { 400 function wporg_email_changes_for_post_types( $post_types ) { 582 401 if ( ! in_array( $this->post_type, $post_types ) ) { 583 402 $post_types[] = $this->post_type; … … 591 410 * if that plugin is active. 592 411 */ 593 publicfunction disable_p2_resolved_posts_action_links() {594 if ( ( $this->post_type == =get_post_type() ) && class_exists( 'P2_Resolved_Posts' ) && isset( $GLOBALS['p2_resolved_posts'] ) && is_object( $GLOBALS['p2_resolved_posts'] ) ) {595 remove_filter( 'p2_action_links', [ P2_Resolved_Posts::instance(), 'p2_action_links' ], 100 );412 function disable_p2_resolved_posts_action_links() { 413 if ( ( $this->post_type == get_post_type() ) && class_exists( 'P2_Resolved_Posts' ) && isset( $GLOBALS['p2_resolved_posts'] ) && is_object( $GLOBALS['p2_resolved_posts'] ) ) { 414 remove_filter( 'p2_action_links', array( P2_Resolved_Posts::instance(), 'p2_action_links' ), 100 ); 596 415 } 597 416 } … … 603 422 * @return bool 604 423 */ 605 publicfunction disable_o2_processing( $process_with_o2 ) {424 function disable_o2_processing( $process_with_o2 ) { 606 425 return ( is_singular() && $this->post_type === get_post_type() ) ? false : $process_with_o2; 607 426 } … … 613 432 * @return string 614 433 */ 615 publicfunction o2_application_container( $container ) {434 function o2_application_container( $container ) { 616 435 return ( is_singular() && $this->post_type === get_post_type() ) ? '#primary' : $container; 617 436 } … … 624 443 * @return string 625 444 */ 626 publicfunction o2_view_type( $view_type ) {445 function o2_view_type( $view_type ) { 627 446 return ( is_singular() && $this->post_type === get_post_type() ) ? 'single' : $view_type; 628 447 } … … 634 453 * @return array 635 454 */ 636 public function o2_post_fragment( $post_fragment ) { 637 if ( empty( $post_fragment['id'] ) ) { 638 return $post_fragment; 639 } 640 455 function o2_post_fragment( $post_fragment ) { 641 456 $post = get_post( $post_fragment['id'] ); 642 457 if ( ! $post ) { … … 658 473 * @return bool 659 474 */ 660 publicfunction comments_open( $open, $post_id ) {475 function comments_open( $open, $post_id ) { 661 476 $post = get_post( $post_id ); 662 477 if ( ! $post ) { … … 684 499 * @return array 685 500 */ 686 publicfunction highlight_menu_handbook_link( $menu_items ) {501 function highlight_menu_handbook_link( $menu_items ) { 687 502 // Must be on a handbook page that isn't the handbook landing page (which will already be handled). 688 if ( ! is_page( [ 'handbook', 'handbooks' ]) && ( ! wporg_is_handbook() || wporg_is_handbook_landing_page() ) ) {503 if ( ! is_page( array( 'handbook', 'handbooks' ) ) && ( ! wporg_is_handbook() || wporg_is_handbook_landing_page() ) ) { 689 504 return $menu_items; 690 505 } 691 506 692 507 // Menu must not have an item that is already noted as being current. 693 $current_menu_item = wp_filter_object_list( $menu_items, [ 'current' => true ]);508 $current_menu_item = wp_filter_object_list( $menu_items, array( 'current' => true ) ); 694 509 if ( $current_menu_item ) { 695 510 return $menu_items; … … 697 512 698 513 // Menu must have an item that links to handbook home page. 699 $root_handbook_menu_item = wp_filter_object_list( $menu_items, [ 'url' => wporg_get_current_handbook_home_url() ]);514 $root_handbook_menu_item = wp_filter_object_list( $menu_items, array( 'url' => wporg_get_current_handbook_home_url() ) ); 700 515 if ( ! $root_handbook_menu_item ) { 701 516 // Or it must have an item that links to a 'handbook' or 'handbooks' page. … … 703 518 $page = get_page_by_path( $page_slug ); 704 519 if ( $page ) { 705 $root_handbook_menu_item = wp_filter_object_list( $menu_items, [ 'object_id' => $page->ID ]);520 $root_handbook_menu_item = wp_filter_object_list( $menu_items, array( 'object_id' => $page->ID ) ); 706 521 } 707 522 }
Note: See TracChangeset
for help on using the changeset viewer.