]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-includes/class-wp-customize-control.php
WordPress 4.7
[autoinstalls/wordpress.git] / wp-includes / class-wp-customize-control.php
index 3027a7b55c65a8493a55be62f1313e7687db5085..07c6dcfce1808196d9b7b1d84242c481b8e6e82e 100644 (file)
@@ -64,6 +64,18 @@ class WP_Customize_Control {
         */
        public $setting = 'default';
 
+       /**
+        * Capability required to use this control.
+        *
+        * Normally this is empty and the capability is derived from the capabilities
+        * of the associated `$settings`.
+        *
+        * @since 4.5.0
+        * @access public
+        * @var string
+        */
+       public $capability;
+
        /**
         * @access public
         * @var int
@@ -102,6 +114,15 @@ class WP_Customize_Control {
         */
        public $input_attrs = array();
 
+       /**
+        * Show UI for adding new content, currently only used for the dropdown-pages control.
+        *
+        * @since 4.7.0
+        * @access public
+        * @var bool
+        */
+       public $allow_addition = false;
+
        /**
         * @deprecated It is better to just call the json() method
         * @access public
@@ -133,15 +154,42 @@ class WP_Customize_Control {
        /**
         * Constructor.
         *
-        * Supplied $args override class property defaults.
+        * Supplied `$args` override class property defaults.
         *
-        * If $args['settings'] is not defined, use the $id as the setting ID.
+        * If `$args['settings']` is not defined, use the $id as the setting ID.
         *
         * @since 3.4.0
         *
         * @param WP_Customize_Manager $manager Customizer bootstrap instance.
         * @param string               $id      Control ID.
-        * @param array                $args    Optional. Arguments to override class property defaults.
+        * @param array                $args    {
+        *     Optional. Arguments to override class property defaults.
+        *
+        *     @type int                  $instance_number Order in which this instance was created in relation
+        *                                                 to other instances.
+        *     @type WP_Customize_Manager $manager         Customizer bootstrap instance.
+        *     @type string               $id              Control ID.
+        *     @type array                $settings        All settings tied to the control. If undefined, `$id` will
+        *                                                 be used.
+        *     @type string               $setting         The primary setting for the control (if there is one).
+        *                                                 Default 'default'.
+        *     @type int                  $priority        Order priority to load the control. Default 10.
+        *     @type string               $section         Section the control belongs to. Default empty.
+        *     @type string               $label           Label for the control. Default empty.
+        *     @type string               $description     Description for the control. Default empty.
+        *     @type array                $choices         List of choices for 'radio' or 'select' type controls, where
+        *                                                 values are the keys, and labels are the values.
+        *                                                 Default empty array.
+        *     @type array                $input_attrs     List of custom input attributes for control output, where
+        *                                                 attribute names are the keys and values are the values. Not
+        *                                                 used for 'checkbox', 'radio', 'select', 'textarea', or
+        *                                                 'dropdown-pages' control types. Default empty array.
+        *     @type array                $json            Deprecated. Use WP_Customize_Control::json() instead.
+        *     @type string               $type            Control type. Core controls include 'text', 'checkbox',
+        *                                                 'textarea', 'radio', 'select', and 'dropdown-pages'. Additional
+        *                                                 input types such as 'email', 'url', 'number', 'hidden', and
+        *                                                 'date' are supported implicitly. Default 'text'.
+        * }
         */
        public function __construct( $manager, $id, $args = array() ) {
                $keys = array_keys( get_object_vars( $this ) );
@@ -160,7 +208,7 @@ class WP_Customize_Control {
                $this->instance_number = self::$instance_count;
 
                // Process settings.
-               if ( empty( $this->settings ) ) {
+               if ( ! isset( $this->settings ) ) {
                        $this->settings = $id;
                }
 
@@ -169,7 +217,7 @@ class WP_Customize_Control {
                        foreach ( $this->settings as $key => $setting ) {
                                $settings[ $key ] = $this->manager->get_setting( $setting );
                        }
-               } else {
+               } else if ( is_string( $this->settings ) ) {
                        $this->setting = $this->manager->get_setting( $this->settings );
                        $settings['default'] = $this->setting;
                }
@@ -196,7 +244,7 @@ class WP_Customize_Control {
                $active = call_user_func( $this->active_callback, $this );
 
                /**
-                * Filter response of WP_Customize_Control::active().
+                * Filters response of WP_Customize_Control::active().
                 *
                 * @since 4.0.0
                 *
@@ -257,6 +305,10 @@ class WP_Customize_Control {
                $this->json['label'] = $this->label;
                $this->json['description'] = $this->description;
                $this->json['instanceNumber'] = $this->instance_number;
+
+               if ( 'dropdown-pages' === $this->type ) {
+                       $this->json['allow_addition'] = $this->allow_addition;
+               }
        }
 
        /**
@@ -272,21 +324,32 @@ class WP_Customize_Control {
        }
 
        /**
-        * Check if the theme supports the control and check user capabilities.
+        * Checks if the user can use this control.
+        *
+        * Returns false if the user cannot manipulate one of the associated settings,
+        * or if one of the associated settings does not exist. Also returns false if
+        * the associated section does not exist or if its capability check returns
+        * false.
         *
         * @since 3.4.0
         *
         * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
         */
        final public function check_capabilities() {
+               if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
+                       return false;
+               }
+
                foreach ( $this->settings as $setting ) {
-                       if ( ! $setting->check_capabilities() )
+                       if ( ! $setting || ! $setting->check_capabilities() ) {
                                return false;
+                       }
                }
 
                $section = $this->manager->get_section( $this->section );
-               if ( isset( $section ) && ! $section->check_capabilities() )
+               if ( isset( $section ) && ! $section->check_capabilities() ) {
                        return false;
+               }
 
                return true;
        }
@@ -331,9 +394,9 @@ class WP_Customize_Control {
                 *
                 * @since 3.4.0
                 *
-                * @param WP_Customize_Control $this {@see WP_Customize_Control} instance.
+                * @param WP_Customize_Control $this WP_Customize_Control instance.
                 */
-               do_action( 'customize_render_control_' . $this->id, $this );
+               do_action( "customize_render_control_{$this->id}", $this );
 
                $this->render();
        }
@@ -394,12 +457,12 @@ class WP_Customize_Control {
        /**
         * Render the control's content.
         *
-        * Allows the content to be overriden without having to rewrite the wrapper in $this->render().
+        * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
         *
         * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
         * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
         *
-        * Control content can alternately be rendered in JS. See {@see WP_Customize_Control::print_template()}.
+        * Control content can alternately be rendered in JS. See WP_Customize_Control::print_template().
         *
         * @since 3.4.0
         */
@@ -469,7 +532,7 @@ class WP_Customize_Control {
                                        if ( ! empty( $this->description ) ) : ?>
                                                <span class="description customize-control-description"><?php echo $this->description; ?></span>
                                        <?php endif; ?>
-                                       <textarea rows="5" <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
+                                       <textarea rows="5" <?php $this->input_attrs(); ?> <?php $this->link(); ?>><?php echo esc_textarea( $this->value() ); ?></textarea>
                                </label>
                                <?php
                                break;
@@ -483,22 +546,58 @@ class WP_Customize_Control {
                                        <span class="description customize-control-description"><?php echo $this->description; ?></span>
                                <?php endif; ?>
 
-                               <?php $dropdown = wp_dropdown_pages(
+                               <?php
+                               $dropdown_name = '_customize-dropdown-pages-' . $this->id;
+                               $show_option_none = __( '&mdash; Select &mdash;' );
+                               $option_none_value = '0';
+                               $dropdown = wp_dropdown_pages(
                                        array(
-                                               'name'              => '_customize-dropdown-pages-' . $this->id,
+                                               'name'              => $dropdown_name,
                                                'echo'              => 0,
-                                               'show_option_none'  => __( '&mdash; Select &mdash;' ),
-                                               'option_none_value' => '0',
+                                               'show_option_none'  => $show_option_none,
+                                               'option_none_value' => $option_none_value,
                                                'selected'          => $this->value(),
                                        )
                                );
+                               if ( empty( $dropdown ) ) {
+                                       $dropdown = sprintf( '<select id="%1$s" name="%1$s">', esc_attr( $dropdown_name ) );
+                                       $dropdown .= sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $option_none_value ), esc_html( $show_option_none ) );
+                                       $dropdown .= '</select>';
+                               }
 
                                // Hackily add in the data link parameter.
                                $dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
+
+                               // Even more hacikly add auto-draft page stubs.
+                               // @todo Eventually this should be removed in favor of the pages being injected into the underlying get_pages() call. See <https://github.com/xwp/wp-customize-posts/pull/250>.
+                               $nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
+                               if ( $nav_menus_created_posts_setting && current_user_can( 'publish_pages' ) ) {
+                                       $auto_draft_page_options = '';
+                                       foreach ( $nav_menus_created_posts_setting->value() as $auto_draft_page_id ) {
+                                               $post = get_post( $auto_draft_page_id );
+                                               if ( $post && 'page' === $post->post_type ) {
+                                                       $auto_draft_page_options .= sprintf( '<option value="%1$s">%2$s</option>', esc_attr( $post->ID ), esc_html( $post->post_title ) );
+                                               }
+                                       }
+                                       if ( $auto_draft_page_options ) {
+                                               $dropdown = str_replace( '</select>', $auto_draft_page_options . '</select>', $dropdown );
+                                       }
+                               }
+
                                echo $dropdown;
                                ?>
                                </label>
-                               <?php
+                               <?php if ( $this->allow_addition && current_user_can( 'publish_pages' ) && current_user_can( 'edit_theme_options' ) ) : // Currently tied to menus functionality. ?>
+                                       <button type="button" class="button-link add-new-toggle"><?php
+                                               /* translators: %s: add new page label */
+                                               printf( __( '+ %s' ), get_post_type_object( 'page' )->labels->add_new_item );
+                                       ?></button>
+                                       <div class="new-content-item">
+                                               <label for="create-input-<?php echo $this->id; ?>"><span class="screen-reader-text"><?php _e( 'New page title' ); ?></span></label>
+                                               <input type="text" id="create-input-<?php echo $this->id; ?>" class="create-item-input" placeholder="<?php esc_attr_e( 'New page title&hellip;' ); ?>">
+                                               <button type="button" class="button add-content"><?php _e( 'Add' ); ?></button>
+                                       </div>
+                               <?php endif;
                                break;
                        default:
                                ?>
@@ -520,7 +619,7 @@ class WP_Customize_Control {
         * Render the control's JS template.
         *
         * This function is only run for control types that have been registered with
-        * {@see WP_Customize_Manager::register_control_type()}.
+        * WP_Customize_Manager::register_control_type().
         *
         * In the future, this will also print the template for the control's container
         * element and be override-able.
@@ -539,7 +638,7 @@ class WP_Customize_Control {
         * An Underscore (JS) template for this control's content (but not its container).
         *
         * Class variables for this control class are available in the `data` JS object;
-        * export custom variables by overriding {@see WP_Customize_Control::to_json()}.
+        * export custom variables by overriding WP_Customize_Control::to_json().
         *
         * @see WP_Customize_Control::print_template()
         *
@@ -564,6 +663,9 @@ require_once( ABSPATH . WPINC . '/customize/class-wp-customize-image-control.php
 /** WP_Customize_Background_Image_Control class */
 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-image-control.php' );
 
+/** WP_Customize_Background_Position_Control class */
+require_once( ABSPATH . WPINC . '/customize/class-wp-customize-background-position-control.php' );
+
 /** WP_Customize_Cropped_Image_Control class */
 require_once( ABSPATH . WPINC . '/customize/class-wp-customize-cropped-image-control.php' );