Making WordPress.org


Ignore:
Timestamp:
03/05/2021 12:21:47 AM (5 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/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';
Note: See TracChangeset for help on using the changeset viewer.