Making WordPress.org


Ignore:
File:
1 edited

Legend:

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

    r10041 r10792  
    88class WPorg_Handbook_Init {
    99
    10     public static function get_post_types() {
    11         return (array) apply_filters( 'handbook_post_types', array( 'handbook' ) );
     10    /**
     11     * Asociative array of WPorg_Handbook objects.
     12     *
     13     * @var array
     14     */
     15    protected static $handbooks = [];
     16
     17    /**
     18     * Returns the handbook objects.
     19     *
     20     * @return WPorg_Handbook[]
     21     */
     22    public static function get_handbook_objects() {
     23        return self::$handbooks;
    1224    }
    1325
    14     static function init() {
     26    /**
     27     * Returns a handbook of the given post type.
     28     *
     29     * @param string $post_type The handbook post type.
     30     * @return WPorg_Handbook|false The handbook object, or false if no such
     31     *                              handbook.
     32     */
     33    public static function get_handbook( $post_type ) {
     34        $handbooks = self::get_handbook_objects();
     35        return $handbooks[ $post_type ] ?? false;
     36    }
    1537
    16         $post_types = self::get_post_types();
     38    /**
     39     * Returns the post types of all handbooks.
     40     *
     41     * @return array
     42     */
     43    public static function get_post_types() {
     44        return array_keys( self::$handbooks );
     45    }
    1746
     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;
     72    }
     73
     74    /**
     75     * Resets memoized and cached variables.
     76     *
     77     * @param bool $delete_handbook_objects Optional. Delete associated handbook
     78     *                                      objects? Default false.
     79     */
     80    public static function reset( $delete_handbook_objects = false ) {
     81        if ( $delete_handbook_objects ) {
     82            foreach ( self::get_handbook_objects() as $obj ) {
     83                unset( $obj );
     84            }
     85        }
     86
     87        self::$handbooks  = [];
     88    }
     89
     90    /**
     91     * Initializes handbooks.
     92     */
     93    public static function init() {
     94        $config = [];
     95        $handbooks_config = [];
     96
     97        /**
     98         * Fires before handbooks have been initialized.
     99         */
     100        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         * @see WPorg_Handbook::get_default_handbook_config()
     121         *
     122         * @param array $config Associative array of handbooks and their
     123         *                      configuration options. Keys should be the handbook
     124         *                      post type (which will get appended '-handbook' if
     125         *                      the post type isn't 'handbook' and doesn't already
     126         *                      contain '-handbook'. See
     127         *                      {@see WPorg_Handbook::get_default_handbook_config()}
     128         *                      for list of per-handbook configuration options.
     129         */
     130        $config = (array) apply_filters( 'handbooks_config', $config );
     131
     132        // If no handbooks were configured, default to a basic handbook.
     133        if ( ! $config ) {
     134            $config = [ 'handbook' => [] ];
     135        }
     136
     137        // Get default settings for a handbook.
     138        $defaults = WPorg_Handbook::get_default_handbook_config();
     139
     140        // Determine each handbook's config.
     141        foreach ( $config as $key => $value ) {
     142            $key = sanitize_title( $key );
     143            $post_type = ( 'handbook' === $key || false !== strpos( $key, '-handbook' ) ) ? $key : $key . '-handbook';
     144
     145            $handbooks_config[ $post_type ] = wp_parse_args( $value, $defaults );
     146
     147            // Set slug if not explicitly set.
     148            if ( empty( $handbooks_config[ $post_type ]['slug'] ) ) {
     149                $handbooks_config[ $post_type ]['slug'] = $key;
     150            }
     151        }
     152
     153        $post_types = array_keys( $handbooks_config );
     154
     155        // Enable table of contents.
    18156        new WPorg_Handbook_TOC( $post_types );
    19157
    20         foreach ( $post_types as $type ) {
    21             new WPorg_Handbook( $type );
     158        // Instantiate each of the handbooks.
     159        self::$handbooks = [];
     160        foreach ( $handbooks_config as $type => $conf ) {
     161            self::$handbooks[ $type ] = new WPorg_Handbook( $type, $conf );
    22162        }
    23163
     164        // Enable glossary.
    24165        WPorg_Handbook_Glossary::init();
    25166
    26         add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_styles' ) );
    27         add_action( 'wp_enqueue_scripts', array( __CLASS__, 'enqueue_scripts' ) );
     167        // Enqueue styles and scripts.
     168        add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_styles' ] );
     169        add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] );
     170
     171        /**
     172         * Fires after handbooks have been initialized.
     173         */
     174        do_action( 'after_handbooks_init' );
    28175    }
    29176
    30     static public function enqueue_styles() {
    31         wp_enqueue_style( 'wporg-handbook-css', plugins_url( 'stylesheets/callout-boxes.css', WPORG_HANDBOOK_PLUGIN_FILE ), array(), '20200121' );
     177    /**
     178     * Enqueues styles.
     179     */
     180    public static function enqueue_styles() {
     181        wp_enqueue_style( 'wporg-handbook-css', plugins_url( 'stylesheets/callout-boxes.css', WPORG_HANDBOOK_PLUGIN_FILE ), [], '20200121' );
    32182    }
    33183
    34     static public function enqueue_scripts() {
    35         wp_enqueue_script( 'wporg-handbook', plugins_url( 'scripts/handbook.js', WPORG_HANDBOOK_PLUGIN_FILE ), array( 'jquery' ), '20150930' );
     184    /**
     185     * Enqueues scripts.
     186     */
     187    public static function enqueue_scripts() {
     188        wp_enqueue_script( 'wporg-handbook', plugins_url( 'scripts/handbook.js', WPORG_HANDBOOK_PLUGIN_FILE ), [ 'jquery' ], '20150930' );
    36189    }
    37190
    38191}
    39192
    40 add_action( 'after_setup_theme', array( 'WPorg_Handbook_Init', 'init' ) );
    41 
     193add_action( 'after_setup_theme', [ 'WPorg_Handbook_Init', 'init' ] );
Note: See TracChangeset for help on using the changeset viewer.