Making WordPress.org

Changeset 800


Ignore:
Timestamp:
08/19/2014 05:34:26 PM (10 years ago)
Author:
coffee2code
Message:

Handbook plugin: add support for defining and retrieving custom handbook name

  • Add WPorg::get_name() to retrieve handbook name. Derives default if one isn't explicitly set.
  • Add input for custom name via Settings -> General

See #439

File:
1 edited

Legend:

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

    r776 r800  
    3838
    3939    public $post_type = '';
     40    public $setting_name = '';
    4041
    4142    protected $label = '';
     
    5758    }
    5859
     60    /**
     61     * Returns the handbook name.
     62     *
     63     * If one isn't set via settings, one is generated.
     64     *
     65     * @param  string $post_type Optional. Handbook post type.
     66     * @param  bool   $raw       Optional. Return only explicitly set name without attempting to generate default name?
     67     * @return string
     68     */
     69    static function get_name( $post_type = 'handbook', $raw = false ) {
     70        // Prefer explicitly configured handbook name.
     71        $name = get_option( $post_type . '_name' );
     72
     73        // If handbook name isn't set, try root relative site path.
     74        if ( ! $raw && empty( $name ) ) {
     75            if ( is_multisite() ) {
     76                $name = trim( get_blog_details()->path, '/' );
     77            } else {
     78                $name = trim( parse_url( get_option( 'home' ), PHP_URL_PATH ), '/' );
     79            }
     80
     81            // If no name defined yet, try handbook post type if not standard.
     82            if ( empty( $name ) && ( 'handbook' != $post_type ) ) {
     83                $name = ucfirst( substr( $post_type, 0, -9 ) );
     84            }
     85
     86            $name .= ' Handbook';
     87        }
     88
     89        return trim( $name );
     90    }
     91
    5992    function __construct( $type ) {
    6093        if ( 'handbook' != $type ) {
     
    6598
    6699        $this->label = ucwords( str_replace( array( '-', '_' ), ' ', $this->post_type ) );
     100
     101        $this->setting_name = $this->post_type . '_name';
    67102
    68103        add_filter( 'user_has_cap',                       array( $this, 'grant_handbook_caps' ) );
     
    74109        add_action( 'wporg_email_changes_for_post_types', array( $this, 'wporg_email_changes_for_post_types' ) );
    75110        add_action( 'p2_action_links',                    array( $this, 'disable_p2_resolved_posts_action_links' ) );
     111        add_action( 'admin_init',                         array( $this, 'add_name_setting' ) );
     112    }
     113
     114    function add_name_setting() {
     115        register_setting( 'general', $this->setting_name, 'esc_attr' );
     116
     117        $label = ( 'handbook' == $this->post_type ) ?
     118            __( 'Handbook name', 'wporg' ) :
     119            sprintf( __( 'Handbook name (%s)', 'wporg' ), substr( $this->post_type, 0, -9 ) );
     120
     121        add_settings_field(
     122            $this->setting_name,
     123            '<label for="' . esc_attr( $this->setting_name ) . '">' . $label . '</label>',
     124            array( $this, 'name_setting_html' ),
     125            'general'
     126        );
     127    }
     128
     129    function name_setting_html() {
     130        $value = get_option( $this->setting_name, '' );
     131        echo '<input type="text" id="' . esc_attr( $this->setting_name ) . '" name="' . esc_attr( $this->setting_name ) . '" value="' . esc_attr( $value ) . '" class="regular-text ltr" />';
    76132    }
    77133
Note: See TracChangeset for help on using the changeset viewer.