X-Git-Url: https://scripts.mit.edu/gitweb/autoinstallsdev/wordpress.git/blobdiff_plain/9e77185fafaf4e60e2b73821e0e4b9b1a11fb85f..af50974463450c98503e763a7836a50e260461a9:/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..3c8a8bd9 100644 --- a/wp-includes/class-wp-customize-control.php +++ b/wp-includes/class-wp-customize-control.php @@ -7,6 +7,27 @@ * @since 3.4.0 */ class WP_Customize_Control { + + /** + * Incremented with each new class instantiation, then stored in $instance_number. + * + * Used when sorting two instances whose priorities are equal. + * + * @since 4.1.0 + * @access protected + * @var int + */ + protected static $instance_count = 0; + + /** + * Order in which this instance was created in relation to other instances. + * + * @since 4.1.0 + * @access public + * @var int + */ + public $instance_number; + /** * @access public * @var WP_Customize_Manager @@ -74,6 +95,7 @@ class WP_Customize_Control { public $input_attrs = array(); /** + * @deprecated It is better to just call the json() method * @access public * @var array */ @@ -126,6 +148,8 @@ class WP_Customize_Control { if ( empty( $this->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,7 +176,7 @@ 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 @@ -218,7 +242,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; } /** @@ -241,6 +283,21 @@ 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. + */ + public final function get_content() { + ob_start(); + $this->maybe_render(); + $template = trim( ob_get_contents() ); + ob_end_clean(); + return $template; + } + /** * Check capabilities and render the control. * @@ -263,12 +320,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 +393,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,6 +502,38 @@ 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. + } #> 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' ), + ); + } /** * Enqueue control related scripts/styles. @@ -551,7 +673,7 @@ class WP_Customize_Upload_Control extends WP_Customize_Control { * @since 3.4.0 */ public function enqueue() { - wp_enqueue_script( 'wp-plupload' ); + wp_enqueue_media(); } /** @@ -562,35 +684,120 @@ class WP_Customize_Upload_Control extends WP_Customize_Control { */ public function to_json() { parent::to_json(); + $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. + $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 ), + ); - $this->json['removed'] = $this->removed; + if ( 'image' === $type ) { + $default_attachment['sizes'] = array( + 'full' => array( 'url' => $this->setting->default ), + ); + } - if ( $this->context ) - $this->json['context'] = $this->context; + $this->json['defaultAttachment'] = $default_attachment; + } - if ( $this->extensions ) - $this->json['extensions'] = implode( ',', $this->extensions ); + if ( $value && $this->setting->default && $value === $this->setting->default ) { + // Set the default as the attachment. + $this->json['attachment'] = $this->json['defaultAttachment']; + } elseif ( $value ) { + // Get the attachment model for the existing file. + $attachment_id = attachment_url_to_postid( $value ); + if ( $attachment_id ) { + $this->json['attachment'] = wp_prepare_attachment_for_js( $attachment_id ); + } + } + } } /** - * Render the control's content. + * Don't render any content for this control from PHP. * + * @see WP_Customize_Upload_Control::content_template() * @since 3.4.0 */ - public function render_content() { + public function render_content() {} + + /** + * Render a JS template for the content of the upload control. + * + * @since 4.1.0 + */ + public function content_template() { ?> -