Making WordPress.org

Changeset 10751


Ignore:
Timestamp:
03/04/2021 10:43:04 PM (4 years ago)
Author:
coffee2code
Message:

Handbooks, Handbook: Improve code formatting for WPorg_Handbook.

  • Explicitly declare methods as public
  • Use short array syntax
  • Minor code reformatting
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/handbook.php

    r10356 r10751  
    1313    protected $label = '';
    1414
    15     static function caps() {
    16         return array(
    17             'edit_handbook_pages', 'edit_others_handbook_pages',
     15    public static function caps() {
     16        return [
     17            'edit_handbook_pages',
     18            'edit_others_handbook_pages',
    1819            'edit_published_handbook_pages',
    19         );
    20     }
    21 
    22     static function editor_caps() {
    23         return array(
     20        ];
     21    }
     22
     23    public static function editor_caps() {
     24        return [
    2425            'publish_handbook_pages',
    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         );
     26            'delete_handbook_pages',
     27            'delete_others_handbook_pages',
     28            'delete_published_handbook_pages',
     29            'delete_private_handbook_pages',
     30            'edit_private_handbook_pages',
     31            'read_private_handbook_pages',
     32        ];
    2933    }
    3034
     
    3842     * @return string
    3943     */
    40     static function get_name( $post_type = 'handbook', $raw = false ) {
     44    public static function get_name( $post_type = 'handbook', $raw = false ) {
    4145        // Prefer explicitly configured handbook name.
    4246        $name = get_option( $post_type . '_name' );
    4347
    4448        // If handbook name isn't set, try root relative site path.
    45         if ( ! $raw && empty( $name ) ) {
     49        if ( ! $raw && ! $name ) {
    4650            if ( is_multisite() ) {
    4751                $name = trim( get_blog_details()->path, '/' );
     
    5155
    5256            // If no name defined yet, try handbook post type if not standard.
    53             if ( empty( $name ) && ( 'handbook' != $post_type ) ) {
     57            if ( ! $name && ( 'handbook' !== $post_type ) ) {
    5458                $name = ucfirst( substr( $post_type, 0, -9 ) );
    5559            }
     
    6165    }
    6266
    63     function __construct( $type ) {
    64         if ( 'handbook' != $type ) {
     67    public function __construct( $type ) {
     68        if ( 'handbook' !== $type ) {
    6569            $this->post_type = $type . '-handbook';
    6670        } else {
     
    6872        }
    6973
    70         $this->label = ucwords( str_replace( array( '-', '_' ), ' ', $this->post_type ) );
     74        $this->label = ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) );
    7175        $this->label = apply_filters( 'handbook_label', $this->label, $this->post_type );
    7276
    7377        $this->setting_name = $this->post_type . '_name';
    7478
    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 );
     79        add_filter( 'user_has_cap',                       [ $this, 'grant_handbook_caps' ] );
     80        add_action( 'widgets_init',                       [ $this, 'register_post_type' ] );
     81        add_filter( 'post_type_link',                     [ $this, 'post_type_link' ], 10, 2 );
     82        add_action( 'template_redirect',                  [ $this, 'redirect_handbook_root_page' ] );
     83        add_filter( 'template_include',                   [ $this, 'template_include' ] );
     84        add_filter( 'pre_get_posts',                      [ $this, 'pre_get_posts' ] );
     85        add_action( 'widgets_init',                       [ $this, 'handbook_sidebar' ], 11 ); // After P2
     86        add_action( 'wporg_email_changes_for_post_types', [ $this, 'wporg_email_changes_for_post_types' ] );
     87        add_action( 'p2_action_links',                    [ $this, 'disable_p2_resolved_posts_action_links' ] );
     88        add_action( 'admin_init',                         [ $this, 'add_name_setting' ] );
     89        add_filter( 'body_class',                         [ $this, 'add_body_class' ] );
     90        add_filter( 'post_class',                         [ $this, 'add_post_class' ] );
     91        add_filter( 'o2_process_the_content',             [ $this, 'disable_o2_processing' ] );
     92        add_filter( 'o2_application_container',           [ $this, 'o2_application_container' ] );
     93        add_filter( 'o2_view_type',                       [ $this, 'o2_view_type' ] );
     94        add_filter( 'o2_post_fragment',                   [ $this, 'o2_post_fragment' ] );
     95        add_filter( 'comments_open',                      [ $this, 'comments_open' ], 10, 2 );
     96        add_filter( 'wp_nav_menu_objects',                [ $this, 'highlight_menu_handbook_link' ] );
     97        add_filter( 'display_post_states',                [ $this, 'display_post_states' ], 10, 2 );
    9498    }
    9599
     
    101105     * @return string[]
    102106     */
    103     function display_post_states( $post_states, $post ) {
     107    public function display_post_states( $post_states, $post ) {
    104108        if ( $this->post_is_landing_page( $post ) ) {
    105109            $post_states[] = __( 'Handbook Front Page', 'wporg' );
     
    119123     * @return array
    120124     */
    121     function add_body_class( $classes ) {
     125    public function add_body_class( $classes ) {
    122126        if ( is_singular() && wporg_is_handbook( $this->post_type ) ) {
    123127            $classes[] = 'single-handbook';
     
    142146     * @return array
    143147     */
    144     function add_post_class( $classes ) {
     148    public function add_post_class( $classes ) {
    145149        if ( $this->post_type === get_post_type() ) {
    146150            $classes[] = 'type-handbook';
     
    150154    }
    151155
    152     function add_name_setting() {
     156    public function add_name_setting() {
    153157        register_setting( 'general', $this->setting_name, 'esc_attr' );
    154158
    155         $label = ( 'handbook' == $this->post_type ) ?
     159        $label = ( 'handbook' === $this->post_type ) ?
    156160            __( 'Handbook name', 'wporg' ) :
    157161            sprintf( __( 'Handbook name (%s)', 'wporg' ), substr( $this->post_type, 0, -9 ) );
     
    160164            $this->setting_name,
    161165            '<label for="' . esc_attr( $this->setting_name ) . '">' . $label . '</label>',
    162             array( $this, 'name_setting_html' ),
     166            [ $this, 'name_setting_html' ],
    163167            'general'
    164168        );
    165169    }
    166170
    167     function name_setting_html() {
     171    public function name_setting_html() {
    168172        $value = get_option( $this->setting_name, '' );
    169173        echo '<input type="text" id="' . esc_attr( $this->setting_name ) . '" name="' . esc_attr( $this->setting_name ) . '" value="' . esc_attr( $value ) . '" class="regular-text ltr" />';
    170174    }
    171175
    172     function grant_handbook_caps( $caps ) {
     176    public function grant_handbook_caps( $caps ) {
    173177        if ( ! is_user_member_of_blog() ) {
    174178            return $caps;
     
    188192    }
    189193
    190     function register_post_type() {
     194    public function register_post_type() {
    191195        if ( 'handbook' != $this->post_type ) {
    192196            $slug = substr( $this->post_type, 0, -9 );
     
    195199        }
    196200
    197         $default_config = array(
    198             'labels' => array(
     201        $default_config = [
     202            'labels' => [
    199203                'name'          => $this->label,
    200204                'singular_name' => sprintf( __( '%s Page', 'wporg' ), $this->label ),
    201205                'menu_name'     => $this->label,
    202206                'all_items'     => sprintf( __( '%s Pages', 'wporg' ), $this->label ),
    203             ),
     207            ],
    204208            'public'            => true,
    205209            'show_ui'           => true,
     
    211215            'menu_icon'         => 'dashicons-book',
    212216            'menu_position'     => 11,
    213             'rewrite' => array(
     217            'rewrite' => [
    214218                'feeds'         => false,
    215219                'slug'          => $slug,
    216220                'with_front'    => false,
    217             ),
     221            ],
    218222            'delete_with_user'  => false,
    219             'supports'          => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'revisions', 'wpcom-markdown' ),
    220         );
     223            'supports'          => [ 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'revisions' , 'wpcom-markdown' ],
     224        ];
    221225        // Allow customization of the default post type configuration via filter.
    222         $config = apply_filters( 'handbook_post_type_defaults', $default_config, $slug );
    223 
     226        $config = (array) apply_filters( 'handbook_post_type_defaults', $default_config, $slug );
     227
     228        // Override the presumed label with a potentially customized value.
    224229        $this->label = $config['labels']['name'];
    225230
     
    235240     *              handbook's landing page.
    236241     */
    237     protected function post_is_landing_page( $post = null ) {
     242    public function post_is_landing_page( $post = null ) {
    238243        $is_landing_page = false;
    239244
     
    269274     * @param WP_Post $post      The post in question.
    270275     */
    271     function post_type_link( $post_link, $post ) {
     276    public function post_type_link( $post_link, $post ) {
    272277        $post_type = get_post_type( $post );
    273278
     
    284289     * post type archive link for the handbook.
    285290     */
    286     function redirect_handbook_root_page() {
     291    public function redirect_handbook_root_page() {
    287292        global $wp_query;
    288293
     
    309314     * @return string
    310315     */
    311     function template_include( $template ) {
     316    public function template_include( $template ) {
    312317        global $wp_query;
    313318
     
    317322        }
    318323
    319         $handbook_templates = array();
     324        $handbook_templates = [];
    320325
    321326        // For singular handbook pages not of the 'handbook' post type.
    322327        if ( is_singular( $this->post_type ) && 'handbook' !== $this->post_type ) {
    323             $handbook_templates = array( "single-{$this->post_type}.php", 'single-handbook.php' );
     328            $handbook_templates = [ "single-{$this->post_type}.php", 'single-handbook.php' ];
    324329        }
    325330        // For handbook landing page.
     
    340345    }
    341346
    342     function pre_get_posts( $query ) {
     347    public function pre_get_posts( $query ) {
    343348        // Bail early if query is not for this handbook's post type.
    344349        if ( get_query_var( 'post_type' ) !== $this->post_type ) {
     
    379384    }
    380385
    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 ),
     386    public function handbook_sidebar() {
     387        $sidebar_args = [
     388            'id'            => $this->post_type,
     389            'name'          => sprintf( __( '%s Sidebar', 'wporg' ), $this->label ),
     390            'description'   => sprintf( __( 'Used on %s pages', 'wporg' ), $this->label ),
    386391            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    387392            'after_widget'  => '</aside>',
    388393            'before_title'  => '<h2 class="widget-title">',
    389394            'after_title'   => '</h2>',
    390         );
     395        ];
    391396
    392397        $sidebar_args = apply_filters( 'wporg_handbook_sidebar_args', $sidebar_args, $this );
     
    398403    }
    399404
    400     function wporg_email_changes_for_post_types( $post_types ) {
     405    public function wporg_email_changes_for_post_types( $post_types ) {
    401406        if ( ! in_array( $this->post_type, $post_types ) ) {
    402407            $post_types[] = $this->post_type;
     
    410415     * if that plugin is active.
    411416     */
    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 );
     417    public function disable_p2_resolved_posts_action_links() {
     418        if ( ( $this->post_type === get_post_type() ) && class_exists( 'P2_Resolved_Posts' ) && isset( $GLOBALS['p2_resolved_posts'] ) && is_object( $GLOBALS['p2_resolved_posts'] ) ) {
     419            remove_filter( 'p2_action_links', [ P2_Resolved_Posts::instance(), 'p2_action_links' ], 100 );
    415420        }
    416421    }
     
    422427     * @return bool
    423428     */
    424     function disable_o2_processing( $process_with_o2 ) {
     429    public function disable_o2_processing( $process_with_o2 ) {
    425430        return ( is_singular() && $this->post_type === get_post_type() ) ? false : $process_with_o2;
    426431    }
     
    432437     * @return string
    433438     */
    434     function o2_application_container( $container ) {
     439    public function o2_application_container( $container ) {
    435440        return ( is_singular() && $this->post_type === get_post_type() ) ? '#primary' : $container;
    436441    }
     
    443448     * @return string
    444449     */
    445     function o2_view_type( $view_type ) {
     450    public function o2_view_type( $view_type ) {
    446451        return ( is_singular() && $this->post_type === get_post_type() ) ? 'single' : $view_type;
    447452    }
     
    453458     * @return array
    454459     */
    455     function o2_post_fragment( $post_fragment ) {
     460    public function o2_post_fragment( $post_fragment ) {
    456461        $post = get_post( $post_fragment['id'] );
    457462        if ( ! $post ) {
     
    473478     * @return bool
    474479     */
    475     function comments_open( $open, $post_id ) {
     480    public function comments_open( $open, $post_id ) {
    476481        $post = get_post( $post_id );
    477482        if ( ! $post ) {
     
    499504     * @return array
    500505     */
    501     function highlight_menu_handbook_link( $menu_items ) {
     506    public function highlight_menu_handbook_link( $menu_items ) {
    502507        // Must be on a handbook page that isn't the handbook landing page (which will already be handled).
    503         if ( ! is_page( array( 'handbook', 'handbooks' ) ) && ( ! wporg_is_handbook() || wporg_is_handbook_landing_page() ) ) {
     508        if ( ! is_page( [ 'handbook', 'handbooks' ] ) && ( ! wporg_is_handbook() || wporg_is_handbook_landing_page() ) ) {
    504509            return $menu_items;
    505510        }
    506511
    507512        // Menu must not have an item that is already noted as being current.
    508         $current_menu_item = wp_filter_object_list( $menu_items, array( 'current' => true ) );
     513        $current_menu_item = wp_filter_object_list( $menu_items, [ 'current' => true ] );
    509514        if ( $current_menu_item ) {
    510515            return $menu_items;
     
    512517
    513518        // Menu must have an item that links to handbook home page.
    514         $root_handbook_menu_item = wp_filter_object_list( $menu_items, array( 'url' => wporg_get_current_handbook_home_url() ) );
     519        $root_handbook_menu_item = wp_filter_object_list( $menu_items, [ 'url' => wporg_get_current_handbook_home_url() ] );
    515520        if ( ! $root_handbook_menu_item ) {
    516521            // Or it must have an item that links to a 'handbook' or 'handbooks' page.
     
    518523            $page = get_page_by_path( $page_slug );
    519524            if ( $page ) {
    520                 $root_handbook_menu_item = wp_filter_object_list( $menu_items, array( 'object_id' => $page->ID ) );
     525                $root_handbook_menu_item = wp_filter_object_list( $menu_items, [ 'object_id' => $page->ID ] );
    521526            }
    522527        }
Note: See TracChangeset for help on using the changeset viewer.