Making WordPress.org

Changeset 10768


Ignore:
Timestamp:
03/05/2021 12:05:23 AM (3 years ago)
Author:
coffee2code
Message:

Handbooks: Use WPorg_Handbook_Init::get_post_types() as the canonical source for obtaining all handbook post types.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook
Files:
7 edited

Legend:

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

    r10764 r10768  
    9393    /**
    9494     * Constructor
     95     *
     96     * @param string $type   The post type for the handbook.
    9597     */
    9698    public function __construct( $type ) {
    97         if ( 'handbook' !== $type ) {
    98             $this->post_type = $type . '-handbook';
    99         } else {
    100             $this->post_type = $type;
    101         }
     99        $this->post_type = sanitize_title( $type );
    102100
    103101        $this->label = ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/init.php

    r10767 r10768  
    4747         * @param array $handbooks Array of handbook post types. Default 'handbook'.
    4848         */
    49         return (array) apply_filters( 'handbook_post_types', array( 'handbook' ) );
     49        $post_types = (array) apply_filters( 'handbook_post_types', [ 'handbook' ] );
     50        return array_map(
     51            function( $pt ) {
     52                $pt = sanitize_title( $pt );
     53                return ( in_array( $pt, [ 'handbook', 'page' ] ) || false !== strpos( $pt, '-handbook' ) ) ? $pt : $pt . '-handbook';
     54            },
     55            $post_types
     56        );
    5057    }
    5158
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/table-of-contents.php

    r8899 r10768  
    4040
    4141    function load_filters() {
    42         $this->post_types = array_map( array( $this, 'append_suffix' ), $this->post_types );
    43 
    4442        if ( is_singular( $this->post_types ) && ! is_embed() ) {
    4543            add_filter( 'the_content', array( $this, 'add_toc' ) );
    4644        }
    47     }
    48 
    49     function append_suffix( $t ) {
    50         if ( in_array( $t, array( 'handbook', 'page' ) ) ) {
    51             return $t;
    52         }
    53 
    54         return $t . '-handbook';
    5545    }
    5646
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/template-tags.php

    r10763 r10768  
    66 * Wrapper function for WPorg_Handbook_Init::get_post_types().
    77 *
    8  * @return array Array with full handbook post type names {post-type}-handbook.
     8 * @return array Array of handbook post types.
    99 */
    1010function wporg_get_handbook_post_types() {
     
    1313    }
    1414
    15     $post_types = WPorg_Handbook_Init::get_post_types();
    16 
    17     foreach ( $post_types as $key => $post_type ) {
    18         if ( 'handbook' !== $post_type ) {
    19             $post_types[ $key ] = $post_type . '-handbook';
    20         }
    21     }
    22 
    23     return $post_types;
     15    return WPorg_Handbook_Init::get_post_types();
    2416}
    2517
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/watchlist.php

    r10187 r10768  
    33class WPorg_Handbook_Watchlist {
    44
    5     private static $post_types = array( 'handbook' );
     5    private static $post_types;
    66
    77    public static function init() {
     
    1010
    1111    public static function on_init() {
    12         self::$post_types = (array) apply_filters( 'handbook_post_types', self::$post_types );
    13         self::$post_types = array_map( array( __CLASS__, 'append_suffix' ), self::$post_types );
     12        self::$post_types = WPorg_Handbook_Init::get_post_types();
    1413
    1514        add_action( 'p2_action_links', array(__CLASS__, 'display_action_link'), 100 );
    1615        add_filter( 'o2_filter_post_actions', array( __CLASS__, 'add_o2_action_link' ) );
    1716        add_filter( 'o2_filter_post_action_html', array( __CLASS__, 'get_o2_action_link' ), 10, 2 );
    18     }
    19 
    20     /**
    21      * Appends '-handbook' to the dynamic post type, if not already 'handbook'.
    22      *
    23      * @param  string $t Hanbook post type name.
    24      * @return string
    25      */
    26     private static function append_suffix( $t ) {
    27         if ( in_array( $t, array( 'handbook', 'page' ) ) ) {
    28             return $t;
    29         }
    30 
    31         return $t . '-handbook';
    3217    }
    3318
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/widgets.php

    r8942 r10768  
    1111    protected static $widget_id_base = 'handbook_pages';
    1212
    13     protected $post_types = array( 'handbook' );
     13    protected $post_types;
    1414
    1515    /**
     
    7272        }
    7373
    74         $this->post_types = (array) apply_filters( 'handbook_post_types', $this->post_types );
    75         $this->post_types = array_map( array( $this, 'append_suffix' ), $this->post_types );
     74        $this->post_types = WPorg_Handbook_Init::get_post_types();
    7675
    7776        $post_type = '';
     
    9493
    9594        // Exclude root handbook page from the table of contents.
    96         $page = get_page_by_path( $this->append_suffix( $post_type ), OBJECT, $post_type );
     95        $page = get_page_by_path( $post_type, OBJECT, $post_type );
    9796        if ( ! $page ) {
    9897            $slug = substr( $post_type, 0, -9 );
     
    108107
    109108        return $args;
    110     }
    111 
    112     public function append_suffix( $t ) {
    113         if ( in_array( $t, array( 'handbook', 'page' ) ) ) {
    114             return $t;
    115         }
    116 
    117         return $t . '-handbook';
    118109    }
    119110
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/tests/init.php

    r10765 r10768  
    101101    }
    102102
     103    public function test_get_post_types_custom() {
     104        reinit_handbooks( [ 'plugins-handbook', 'themes' ], 'post_types' );
     105
     106        // Note: The automatic appending of '-handbook' is for back-compat.
     107        $this->assertEquals( ['plugins-handbook', 'themes-handbook'], WPorg_Handbook_Init::get_post_types() );
     108    }
     109
    103110    public function test_get_post_types_filtered() {
     111        add_filter( 'handbook_post_types', function ( $post_types ) {
     112            $post_types[] = 'example';
     113            return $post_types;
     114        }, 11 );
    104115        reinit_handbooks( [ 'plugins', 'themes' ], 'post_types' );
    105116
    106117        // Note: The automatic appending of '-handbook' is for back-compat.
    107         $this->assertEquals( ['plugins-handbook', 'themes-handbook'], WPorg_Handbook_Init::get_post_types() );
     118        $this->assertEquals( [ 'plugins-handbook', 'themes-handbook', 'example-handbook' ], WPorg_Handbook_Init::get_post_types() );
    108119    }
    109120
Note: See TracChangeset for help on using the changeset viewer.