Making WordPress.org

Changeset 10769


Ignore:
Timestamp:
03/05/2021 12:21:47 AM (4 years ago)
Author:
coffee2code
Message:

Handbooks, Init & Handbook: Improve multi-handbook support and configuration handling

Init:

  • Introduce 'handbooks_config' as filter for defining and configuring handbooks
  • Deprecate 'handbook_post_types' as filter for defining handbooks
  • Store handbook objects as an associative array with post types as keys
  • Discontinue explicitly memoizing handbook post types
  • Introduce get_handbooks_config() to retrieve config for a particular handbook or all handbooks

Handbook:

  • Change constructor to accept config array as optional arg
  • Introduce get_config() as a getter to return handbook's config
  • Start with 'label' and 'slug' as a config options
Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook
Files:
3 edited

Legend:

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

    r10768 r10769  
    1414     */
    1515    public $post_type = '';
     16
     17    /**
     18     * The handbook's settings.
     19     *
     20     * @var array
     21     */
     22    public $settings = [];
    1623
    1724    /**
     
    6067
    6168    /**
     69     * Returns handbook default config.
     70     *
     71     * @return array
     72     */
     73    public static function get_default_handbook_config() {
     74        /**
     75         * Filters default handbook configuration array.
     76         *
     77         * @param string $slug The slug for the post type. Default is post type.
     78         */
     79        return (array) apply_filters( 'handbook_default_handbook_config', [
     80            'label'         => '',
     81            'slug'          => '',
     82        ] );
     83    }
     84
     85    /**
    6286     * Returns the handbook name.
    6387     *
     
    95119     *
    96120     * @param string $type   The post type for the handbook.
    97      */
    98     public function __construct( $type ) {
     121     * @param array  $config The config array for the handbook.
     122     */
     123    public function __construct( $type, $config = [] ) {
    99124        $this->post_type = sanitize_title( $type );
    100125
    101         $this->label = ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) );
    102         $this->label = apply_filters( 'handbook_label', $this->label, $this->post_type );
     126        $config = $this->config = wp_parse_args( $config, self::get_default_handbook_config() );
     127
     128        $this->label = apply_filters(
     129            'handbook_label',
     130            $config['label'] ?: ucwords( str_replace( [ '-', '_' ], ' ', $this->post_type ) ),
     131            $this->post_type
     132        );
    103133
    104134        $this->setting_name = $this->post_type . '_name';
     
    127157
    128158    /**
     159     * Returns the configuration array for handbooks.
     160     *
     161     * @return array
     162     */
     163    public function get_config() {
     164        return $this->config;
     165    }
     166
     167    /**
    129168     * Adds 'Handbook Front Page' post state indicator for handbook landing pages.
    130169     *
     
    235274     */
    236275    public function register_post_type() {
    237         if ( 'handbook' != $this->post_type ) {
    238             $slug = substr( $this->post_type, 0, -9 );
     276        $config = $this->get_config();
     277
     278        if ( ! empty( $config['slug'] ) ) {
     279            $slug = $config['slug'];
     280        } elseif ( 'handbook' !== $this->post_type ) {
     281            $slug = str_replace( '-handbook', '', $this->post_type );
    239282        } else {
    240283            $slug = 'handbook';
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/inc/init.php

    r10768 r10769  
    99
    1010    /**
    11      * Array of WPorg_Handbook objects.
     11     * Asociative array of WPorg_Handbook objects.
    1212     *
    1313     * @var array
     
    4242     */
    4343    public static function get_post_types() {
    44         /**
    45          * Filters the handbook post types for creating handbooks.
    46          *
    47          * @param array $handbooks Array of handbook post types. Default 'handbook'.
    48          */
    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         );
     44        return array_keys( self::$handbooks );
     45    }
     46
     47    /**
     48     * Returns the handbooks configurations.
     49     *
     50     * @param string $handbook Optional. A specific handbook to return config for.
     51     *                         If none specified, then all are returned. Default ''.
     52     * @return array|false If $handbook defined, then the config for that handbook
     53     *                     if handbook exists, else false. If no $handbook specified,
     54     *                     then an associative array of config for all handbooks,
     55     *                     keyed by post type.
     56     */
     57    public static function get_handbooks_config( $handbook = '' ) {
     58        $return = false;
     59        $handbooks = self::get_handbook_objects();
     60
     61        // If no handbook specified, return configs for all handbooks.
     62        if ( ! $handbook ) {
     63            $return = [];
     64            foreach ( $handbooks as $type => $handbook_obj ) {
     65                $return[ $type ] = $handbook_obj->get_config();
     66            }
     67        }
     68
     69        return ( $handbook && ! empty( $handbooks[ $handbook ] ) && is_a( $handbooks[ $handbook ], 'WPorg_Handbook' ) )
     70            ? $handbooks[ $handbook ]->get_config()
     71            : $return;
    5772    }
    5873
     
    7792     */
    7893    public static function init() {
     94        $config = [];
     95        $handbooks_config = [];
     96
    7997        /**
    8098         * Fires before handbooks have been initialized.
    8199         */
    82100        do_action( 'before_handbooks_init' );
     101
     102        /**
     103         * Filters the handbook post types for creating handbooks.
     104         *
     105         * @deprecated Use {@see 'handbooks_config'} instead.
     106         *
     107         * @param array $handbooks Array of handbook post types. Default empty
     108         *                         array, which later will be interpreted to
     109         *                         be 'handbook'.
     110         */
     111        $post_types = (array) apply_filters( 'handbook_post_types', [] );
     112
     113        foreach ( $post_types as $post_type ) {
     114            $config[ $post_type ] = [];
     115        }
     116
     117        /**
     118         * Defines and configures all handbooks.
     119         *
     120         * @param array $config {
     121         *     Associative array of post types and their configuration options. The keys should actually be
     122         *     the post type base, hypenated and without appending '-handbook' (which will automatically get
     123         *     appended when the handbook post type is created, if the key isn't "handbook"). Default is 'handbook'.
     124         *
     125         *     @type string $label The label for the handbook. Default is the
     126         *                         post type slug converted to titlecase (e.g.
     127         *                         plugin-handbok => "Plugin Handbook").
     128         *     @type string $slug  The slug for the post type. Default is the
     129         *                         post type.
     130         * }
     131         */
     132        $config = (array) apply_filters( 'handbooks_config', $config );
     133
     134        // If no handbooks were configured, default to a basic handbook.
     135        if ( ! $config ) {
     136            $config = [ 'handbook' => [] ];
     137        }
     138
     139        // Get default settings for a handbook.
     140        $defaults = WPorg_Handbook::get_default_handbook_config();
     141
     142        // Determine each handbook's config.
     143        foreach ( $config as $key => $value ) {
     144            $key = sanitize_title( $key );
     145            $post_type = ( 'handbook' === $key || false !== strpos( $key, '-handbook' ) ) ? $key : $key . '-handbook';
     146
     147            $handbooks_config[ $post_type ] = wp_parse_args( $value, $defaults );
     148
     149            // Set slug if not explicitly set.
     150            if ( empty( $handbooks_config[ $post_type ]['slug'] ) ) {
     151                $handbooks_config[ $post_type ]['slug'] = $key;
     152            }
     153        }
    83154
    84155        $post_types = self::get_post_types();
     
    89160        // Instantiate each of the handbooks.
    90161        self::$handbooks = [];
    91         foreach ( $post_types as $type ) {
    92             self::$handbooks[] = new WPorg_Handbook( $type );
     162        foreach ( $handbooks_config as $type => $conf ) {
     163            self::$handbooks[ $type ] = new WPorg_Handbook( $type, $conf );
    93164        }
    94165
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/handbook/phpunit/tests/init.php

    r10768 r10769  
    1515
    1616        WPorg_Handbook_Init::reset( true );
     17    }
     18
     19
     20    //
     21    //
     22    // DATA PROVIDERS
     23    //
     24    //
     25
     26
     27    public static function get_default_config() {
     28        return [
     29            [ 'label', '' ],
     30            [ 'manifest', '' ],
     31            [ 'slug', '' ],
     32            [ 'with_front', '' ],
     33        ];
    1734    }
    1835
     
    143160    }
    144161
     162    /*
     163     * get_handbooks_config()
     164     */
     165
     166    /**
     167     * @dataProvider get_default_config
     168     */
     169    public function test_get_handbooks_config_default( $key, $default ) {
     170        $configs = WPorg_Handbook_Init::get_handbooks_config();
     171        $this->assertArrayHasKey( 'handbook', $configs );
     172        $this->assertEquals( 1, count( $configs ) );
     173
     174        $config = $configs['handbook'];
     175
     176        $this->assertArrayHasKey( $key, $config );
     177        $value = $config[ $key ];
     178
     179        if ( 'slug' === $key ) {
     180            $this->assertEquals( 'handbook', $value );
     181        } elseif ( '' === $default ) {
     182            $this->assertEmpty( $value );
     183        } else {
     184            $this->assertEquals( $default, $value );
     185        }
     186    }
     187
     188    public function test_get_handbooks_config_non_handbook() {
     189        $this->assertEmpty( WPorg_Handbook_Init::get_handbooks_config( 'nonexistent-handbook' ) );
     190    }
     191
     192    /**
     193     * @dataProvider get_default_config
     194     */
     195    public function test_get_handbooks_config_specific_handbook_default( $key, $default ) {
     196        $config = WPorg_Handbook_Init::get_handbooks_config( 'handbook' );
     197
     198        $this->assertArrayHasKey( $key, $config );
     199        $value = $config[ $key ];
     200
     201        if ( 'slug' === $key ) {
     202            $this->assertEquals( 'handbook', $value );
     203        } elseif ( '' === $default ) {
     204            $this->assertEmpty( $value );
     205        } else {
     206            $this->assertEquals( $default, $value );
     207        }
     208    }
     209
     210    /**
     211     * @dataProvider get_default_config
     212     */
     213    public function test_get_handbooks_config_specific_custom_handbook( $key, $default ) {
     214        reinit_handbooks( [ 'plugins' => [] ] );
     215
     216        $config = WPorg_Handbook_Init::get_handbooks_config( 'plugins-handbook' );
     217
     218        $this->assertArrayHasKey( $key, $config );
     219        $value = $config[ $key ];
     220
     221        if ( 'slug' === $key ) {
     222            $this->assertEquals( 'plugins', $value );
     223        } elseif ( '' === $default ) {
     224            $this->assertEmpty( $value );
     225        } else {
     226            $this->assertEquals( $default, $value );
     227        }
     228    }
     229
    145230}
Note: See TracChangeset for help on using the changeset viewer.