X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/48ab98cb1779cf2088c1351ac3dd3d0da6fb31d3..46588ee871246a923d972538dbc93b26f4fda932:/wp-includes/class-wp-customize-control.php diff --git a/wp-includes/class-wp-customize-control.php b/wp-includes/class-wp-customize-control.php index 216ceafb..bf17fb20 100644 --- a/wp-includes/class-wp-customize-control.php +++ b/wp-includes/class-wp-customize-control.php @@ -1,12 +1,39 @@ active_callback ) ) { $this->active_callback = array( $this, 'active_callback' ); } + self::$instance_count += 1; + $this->instance_number = self::$instance_count; // Process settings. if ( empty( $this->settings ) ) { @@ -152,14 +182,14 @@ class WP_Customize_Control { public function enqueue() {} /** - * Check whether control is active to current customizer preview. + * Check whether control is active to current Customizer preview. * * @since 4.0.0 * @access public * * @return bool Whether the control is active to the current preview. */ - public final function active() { + final public function active() { $control = $this; $active = call_user_func( $this->active_callback, $this ); @@ -200,7 +230,7 @@ class WP_Customize_Control { * @param string $setting_key * @return mixed The requested setting's value, if the setting exists. */ - public final function value( $setting_key = 'default' ) { + final public function value( $setting_key = 'default' ) { if ( isset( $this->settings[ $setting_key ] ) ) { return $this->settings[ $setting_key ]->value(); } @@ -218,7 +248,25 @@ class WP_Customize_Control { } $this->json['type'] = $this->type; + $this->json['priority'] = $this->priority; $this->json['active'] = $this->active(); + $this->json['section'] = $this->section; + $this->json['content'] = $this->get_content(); + $this->json['label'] = $this->label; + $this->json['description'] = $this->description; + $this->json['instanceNumber'] = $this->instance_number; + } + + /** + * Get the data to export to the client via JSON. + * + * @since 4.1.0 + * + * @return array Array of parameters passed to the JavaScript. + */ + public function json() { + $this->to_json(); + return $this->json; } /** @@ -228,7 +276,7 @@ class WP_Customize_Control { * * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true. */ - public final function check_capabilities() { + final public function check_capabilities() { foreach ( $this->settings as $setting ) { if ( ! $setting->check_capabilities() ) return false; @@ -241,13 +289,28 @@ class WP_Customize_Control { return true; } + /** + * Get the control's content for insertion into the Customizer pane. + * + * @since 4.1.0 + * + * @return string Contents of the control. + */ + final public function get_content() { + ob_start(); + $this->maybe_render(); + $template = trim( ob_get_contents() ); + ob_end_clean(); + return $template; + } + /** * Check capabilities and render the control. * * @since 3.4.0 * @uses WP_Customize_Control::render() */ - public final function maybe_render() { + final public function maybe_render() { if ( ! $this->check_capabilities() ) return; @@ -263,12 +326,12 @@ class WP_Customize_Control { /** * Fires just before a specific Customizer control is rendered. * - * The dynamic portion of the hook name, $this->id, refers to + * The dynamic portion of the hook name, `$this->id`, refers to * the control ID. * * @since 3.4.0 * - * @param WP_Customize_Control $this WP_Customize_Control instance. + * @param WP_Customize_Control $this {@see WP_Customize_Control} instance. */ do_action( 'customize_render_control_' . $this->id, $this ); @@ -336,6 +399,8 @@ class WP_Customize_Control { * 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()}. + * * @since 3.4.0 */ protected function render_content() { @@ -443,14 +508,46 @@ class WP_Customize_Control { break; } } + + /** + * 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()}. + * + * In the future, this will also print the template for the control's container + * element and be override-able. + * + * @since 4.1.0 + */ + final public function print_template() { + ?> + + json['statuses'] = $this->statuses; + $this->json['defaultValue'] = $this->setting->default; } /** - * Render the control's content. + * Don't render the control content from PHP, as it's rendered via JS on load. * * @since 3.4.0 */ - public function render_content() { - $this_default = $this->setting->default; - $default_attr = ''; - if ( $this_default ) { - if ( false === strpos( $this_default, '#' ) ) - $this_default = '#' . $this_default; - $default_attr = ' data-default-color="' . esc_attr( $this_default ) . '"'; - } - // The input's value gets set by JS. Don't fill it. + public function render_content() {} + + /** + * Render a JS template for the content of the color picker control. + * + * @since 4.1.0 + */ + public function content_template() { ?> + <# var defaultValue = ''; + if ( data.defaultValue ) { + if ( '#' !== data.defaultValue.substring( 0, 1 ) ) { + defaultValue = '#' + data.defaultValue; + } else { + defaultValue = data.defaultValue; + } + defaultValue = ' data-default-color=' + defaultValue; // Quotes added automatically. + } #> json['removed'] = $this->removed; - - if ( $this->context ) - $this->json['context'] = $this->context; - - if ( $this->extensions ) - $this->json['extensions'] = implode( ',', $this->extensions ); - } + public $mime_type = ''; /** - * Render the control's content. + * Button labels. * - * @since 3.4.0 + * @since 4.2.0 + * @access public + * @var array */ - public function render_content() { - ?> - - statuses = array( '' => __('No Image') ); - + public function __construct( $manager, $id, $args = array() ) { parent::__construct( $manager, $id, $args ); - $this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) ); - $this->add_tab( 'uploaded', __('Uploaded'), array( $this, 'tab_uploaded' ) ); - - // Early priority to occur before $this->manager->prepare_controls(); - add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 ); + $this->button_labels = array( + 'select' => __( 'Select File' ), + 'change' => __( 'Change File' ), + 'default' => __( 'Default' ), + 'remove' => __( 'Remove' ), + 'placeholder' => __( 'No file selected' ), + 'frame_title' => __( 'Select File' ), + 'frame_button' => __( 'Choose File' ), + ); } /** - * Prepares the control. - * - * If no tabs exist, removes the control from the manager. + * Enqueue control related scripts/styles. * - * @since 3.4.2 + * @since 3.4.0 + * @since 4.2.0 Moved from WP_Customize_Upload_Control. */ - public function prepare_control() { - if ( ! $this->tabs ) - $this->manager->remove_control( $this->id ); + public function enqueue() { + wp_enqueue_media(); } /** * Refresh the parameters passed to the JavaScript via JSON. * * @since 3.4.0 - * @uses WP_Customize_Upload_Control::to_json() + * @since 4.2.0 Moved from WP_Customize_Upload_Control. + * + * @see WP_Customize_Control::to_json() */ public function to_json() { parent::to_json(); - $this->json['statuses'] = $this->statuses; + $this->json['mime_type'] = $this->mime_type; + $this->json['button_labels'] = $this->button_labels; + + $value = $this->value(); + + if ( is_object( $this->setting ) ) { + if ( $this->setting->default ) { + // Fake an attachment model - needs all fields used by template. + // Note that the default value must be a URL, NOT an attachment ID. + $type = in_array( substr( $this->setting->default, -3 ), array( 'jpg', 'png', 'gif', 'bmp' ) ) ? 'image' : 'document'; + $default_attachment = array( + 'id' => 1, + 'url' => $this->setting->default, + 'type' => $type, + 'icon' => wp_mime_type_icon( $type ), + 'title' => basename( $this->setting->default ), + ); + + if ( 'image' === $type ) { + $default_attachment['sizes'] = array( + 'full' => array( 'url' => $this->setting->default ), + ); + } + + $this->json['defaultAttachment'] = $default_attachment; + } + + if ( $value && $this->setting->default && $value === $this->setting->default ) { + // Set the default as the attachment. + $this->json['attachment'] = $this->json['defaultAttachment']; + } elseif ( $value ) { + $this->json['attachment'] = wp_prepare_attachment_for_js( $value ); + } + } } /** - * Render the control's content. + * Don't render any content for this control from PHP. * * @since 3.4.0 + * @since 4.2.0 Moved from WP_Customize_Upload_Control. + * + * @see WP_Customize_Media_Control::content_template() */ - public function render_content() { - $src = $this->value(); - if ( isset( $this->get_url ) ) - $src = call_user_func( $this->get_url, $src ); + public function render_content() {} + /** + * Render a JS template for the content of the media control. + * + * @since 4.1.0 + * @since 4.2.0 Moved from WP_Customize_Upload_Control. + */ + public function content_template() { ?> -
- label ) ) : ?> - label ); ?> - description ) ) : ?> - description; ?> - + -
-