Making WordPress.org


Ignore:
Timestamp:
03/05/2021 12:21:47 AM (6 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
File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.