Making WordPress.org


Ignore:
Timestamp:
06/19/2019 05:04:11 AM (6 years ago)
Author:
coffee2code
Message:

Developer: Create Common APIs handbook.

Handbook is currently only accessible to logged-in users.

See #4376.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-developer/inc/handbooks.php

    r8869 r8961  
    2020
    2121    /**
     22     * Hidden handbook post types.
     23     *
     24     * Note: Hidden only from users who aren't logged in.
     25     *
     26     * @var array
     27     * @access public
     28     */
     29    public static $hidden_handbooks = [ 'apis-handbook' ];
     30
     31    /**
    2232     * Initializer
    2333     *
     
    2535     */
    2636    public static function init() {
     37        add_filter( 'handbook_label', array( __CLASS__, 'change_handbook_label' ), 10, 2 );
    2738        add_filter( 'handbook_post_type_defaults', array( __CLASS__, 'filter_handbook_post_type_defaults' ), 10, 2 );
    2839        add_filter( 'handbook_post_types', array( __CLASS__, 'filter_handbook_post_types' ) );
     
    3748    public static function do_init() {
    3849        add_filter( 'query_vars',  array( __CLASS__, 'add_query_vars' ) );
     50
     51        add_action( 'template_redirect', array( __CLASS__, 'redirect_hidden_handbooks' ), 1 );
    3952
    4053        add_action( 'pre_get_posts',  array( __CLASS__, 'pre_get_posts' ), 9 );
     
    7083
    7184    /**
     85     * Redirects handbooks that should be inaccessible to visitors who aren't logged in.
     86     */
     87    public static function redirect_hidden_handbooks() {
     88        if ( ! self::$hidden_handbooks || get_current_user_id() || ! function_exists( 'wporg_is_handbook' ) || ! wporg_is_handbook() ) {
     89            return;
     90        }
     91
     92        if ( in_array( wporg_get_current_handbook(), self::$hidden_handbooks ) ) {
     93            wp_safe_redirect( home_url() );
     94            exit();
     95        }   
     96    }
     97
     98    /**
    7299     * Add handbook query vars to the current query.
    73100     *
     
    88115
    89116    /**
    90      * Filter handbook post types to create handbooks for: plugins, themes.
     117     * Filter handbook post types to create handbooks for: apis, plugins, themes.
    91118     *
    92119     * @access public
     
    97124    public static function filter_handbook_post_types( $types ) {
    98125        if ( ! self::$post_types ) {
    99             self::$post_types = apply_filters( 'devhub_handbook_post_types', [ 'plugin', 'theme' ] );
     126            self::$post_types = apply_filters( 'devhub_handbook_post_types', [ 'apis', 'plugin', 'theme' ] );
    100127        }
    101128
     
    241268    }
    242269
     270    /**
     271     * Overrides the default handbook label when post type name does not directly
     272     * translate to post type label.
     273     *
     274     * @param string $label     The default label, which is merely a sanitized
     275     *                          version of the handbook name.
     276     * @param string $post_type The handbook post type.
     277     * @return string
     278     */
     279    public static function change_handbook_label( $label, $post_type ) {
     280        if ( 'apis-handbook' === $post_type ) {
     281            $label = __( 'Common APIs Handbook', 'wporg' );
     282        }
     283
     284        return $label;
     285    }
     286
    243287} // Devhub_Handbooks
    244288
Note: See TracChangeset for help on using the changeset viewer.