From 79f8b89fbc8d30b9c44dcd344310afa7888d54b1 Mon Sep 17 00:00:00 2001 From: koopersmith Date: Thu, 29 Mar 2012 06:35:54 +0000 Subject: [PATCH] Theme Customizer: Move upload and image controls to subclasses. see #19910. Instead of grouping together every control parameter into a single array and passing them all to the JS, use subclasses with custom parameters to implement additional PHP functionality and the parameter to send only the necessary variables to the JavaScript control. Replaces WP_Customize_Control->control_params with WP_Customize_Control->json and WP_Customize_Control->to_json(). The to_json() method refreshes the json array passed to the JavaScript control (set to control.param by default). Creates WP_Customize_Upload_Control and WP_Customize_Image_Control. git-svn-id: http://svn.automattic.com/wordpress/trunk@20319 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/ajax-actions.php | 2 +- wp-includes/class-wp-customize-control.php | 170 +++++++++++++-------- wp-includes/class-wp-customize.php | 27 ++-- wp-includes/customize-controls.php | 18 +-- wp-includes/js/customize-controls.dev.js | 2 +- 5 files changed, 119 insertions(+), 100 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 9873ca0b83..ecd4d2f1ef 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1615,7 +1615,7 @@ function wp_ajax_upload_attachment() { $post_id = null; } - $post_data = is_array( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array(); + $post_data = isset( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array(); $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data ); diff --git a/wp-includes/class-wp-customize-control.php b/wp-includes/class-wp-customize-control.php index a5bd9daa5a..81f8ae78a1 100644 --- a/wp-includes/class-wp-customize-control.php +++ b/wp-includes/class-wp-customize-control.php @@ -17,11 +17,11 @@ class WP_Customize_Control { public $priority = 10; public $section = ''; public $label = ''; - // @todo: remove control_params - public $control_params = array(); // @todo: remove choices public $choices = array(); + public $json = array(); + public $visibility; public $type = 'text'; @@ -35,7 +35,7 @@ class WP_Customize_Control { * @since 3.4.0 */ function __construct( $manager, $id, $args = array() ) { - $keys = array_keys( get_class_vars( __CLASS__ ) ); + $keys = array_keys( get_object_vars( $this ) ); foreach ( $keys as $key ) { if ( isset( $args[ $key ] ) ) $this->$key = $args[ $key ]; @@ -90,18 +90,33 @@ class WP_Customize_Control { return $this->settings[ $setting_key ]->value(); } - public function json( $args = array() ) { - $settings = array(); + /** + * Refresh the parameters passed to the JavaScript via JSON. + * + * @since 3.4.0 + */ + public function to_json() { + $this->json['settings'] = array(); foreach ( $this->settings as $key => $setting ) { - $settings[ $key ] = $setting->id; + $this->json['settings'][ $key ] = $setting->id; } - return array( - 'type' => $this->type, - 'params' => wp_parse_args( wp_parse_args( $args, array( - 'settings' => $settings, - ) ), $this->control_params ), - ); + $this->json['type'] = $this->type; + + if ( $this->visibility ) { + if ( is_string( $this->visibility ) ) { + $this->json['visibility'] = array( + 'id' => $this->visibility, + 'value' => true, + ); + } else { + $this->json['visibility'] = array( + 'id' => $this->visibility[0], + 'value' => $this->visibility[1], + ); + } + + } } /** @@ -258,60 +273,6 @@ class WP_Customize_Control { - - value(); - - $image = $value; - if ( isset( $this->control_params['get_url'] ) ) - $image = call_user_func( $this->control_params['get_url'], $image ); - - ?> - - json['removed'] = $this->removed; + + if ( $this->context ) + $this->json['context'] = $this->context; + } + + public function render_content() { + ?> + + value(); + if ( isset( $this->get_url ) ) + $src = call_user_func( $this->get_url, $src ); + + ?> + + 'custom-header', ) ); - $this->add_control( 'header_image', array( + $this->add_control( new WP_Customize_Image_Control( $this, 'header_image', array( 'label' => 'Header Image', 'section' => 'header', - 'type' => 'image', - 'control_params' => array( - 'context' => 'custom-header', - 'removed' => 'remove-header', - 'get_url' => 'get_header_image', - 'tabs' => array( - array( 'uploaded', __('Uploaded'), 'wp_customize_print_uploaded_headers' ), - array( 'included', __('Included'), 'wp_customize_print_included_headers' ), - ), + 'context' => 'custom-header', + 'removed' => 'remove-header', + 'get_url' => 'get_header_image', + 'tabs' => array( + array( 'uploaded', __('Uploaded'), 'wp_customize_print_uploaded_headers' ), + array( 'included', __('Included'), 'wp_customize_print_included_headers' ), ), - ) ); + ) ) ); /* Custom Background */ @@ -627,14 +624,12 @@ final class WP_Customize { 'theme_supports' => 'custom-background', ) ); - $this->add_control( 'background_image', array( + $this->add_control( new WP_Customize_Upload_Control( $this, 'background_image', array( 'label' => __( 'Background Image' ), 'section' => 'background', 'type' => 'upload', - 'control_params' => array( - 'context' => 'custom-background', - ), - ) ); + 'context' => 'custom-background', + ) ) ); $this->add_setting( 'background_repeat', array( 'default' => 'repeat', diff --git a/wp-includes/customize-controls.php b/wp-includes/customize-controls.php index 97fb0c48b5..1290c1fd41 100644 --- a/wp-includes/customize-controls.php +++ b/wp-includes/customize-controls.php @@ -107,22 +107,8 @@ do_action( 'customize_controls_print_scripts' ); } foreach ( $this->controls as $id => $control ) { - $settings['controls'][ $id ] = $control->json(); - - if ( $control->visibility ) { - if ( is_string( $control->visibility ) ) { - $settings['controls'][ $id ]['visibility'] = array( - 'id' => $control->visibility, - 'value' => true, - ); - } else { - $settings['controls'][ $id ]['visibility'] = array( - 'id' => $control->visibility[0], - 'value' => $control->visibility[1], - ); - } - - } + $control->to_json(); + $settings['controls'][ $id ] = $control->json; } ?> diff --git a/wp-includes/js/customize-controls.dev.js b/wp-includes/js/customize-controls.dev.js index 359bf35d95..55949cb533 100644 --- a/wp-includes/js/customize-controls.dev.js +++ b/wp-includes/js/customize-controls.dev.js @@ -361,7 +361,7 @@ control; control = api.control.add( id, new constructor( id, { - params: data.params, + params: data, previewer: previewer } ) );