web/wp-includes/class-wp-customize-control.php
changeset 194 32102edaa81b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 /**
       
     3  * Customize Control Class
       
     4  *
       
     5  * @package WordPress
       
     6  * @subpackage Customize
       
     7  * @since 3.4.0
       
     8  */
       
     9 
       
    10 class WP_Customize_Control {
       
    11 	public $manager;
       
    12 	public $id;
       
    13 
       
    14 	// All settings tied to the control.
       
    15 	public $settings;
       
    16 
       
    17 	// The primary setting for the control (if there is one).
       
    18 	public $setting = 'default';
       
    19 
       
    20 	public $priority          = 10;
       
    21 	public $section           = '';
       
    22 	public $label             = '';
       
    23 	// @todo: remove choices
       
    24 	public $choices           = array();
       
    25 
       
    26 	public $json = array();
       
    27 
       
    28 	public $type = 'text';
       
    29 
       
    30 
       
    31 	/**
       
    32 	 * Constructor.
       
    33 	 *
       
    34 	 * If $args['settings'] is not defined, use the $id as the setting ID.
       
    35 	 *
       
    36 	 * @since 3.4.0
       
    37 	 */
       
    38 	function __construct( $manager, $id, $args = array() ) {
       
    39 		$keys = array_keys( get_object_vars( $this ) );
       
    40 		foreach ( $keys as $key ) {
       
    41 			if ( isset( $args[ $key ] ) )
       
    42 				$this->$key = $args[ $key ];
       
    43 		}
       
    44 
       
    45 		$this->manager = $manager;
       
    46 		$this->id = $id;
       
    47 
       
    48 
       
    49 		// Process settings.
       
    50 		if ( empty( $this->settings ) )
       
    51 			$this->settings = $id;
       
    52 
       
    53 		$settings = array();
       
    54 		if ( is_array( $this->settings ) ) {
       
    55 			foreach ( $this->settings as $key => $setting ) {
       
    56 				$settings[ $key ] = $this->manager->get_setting( $setting );
       
    57 			}
       
    58 		} else {
       
    59 			$this->setting = $this->manager->get_setting( $this->settings );
       
    60 			$settings['default'] = $this->setting;
       
    61 		}
       
    62 		$this->settings = $settings;
       
    63 	}
       
    64 
       
    65 	/**
       
    66 	 * Enqueue control related scripts/styles.
       
    67 	 *
       
    68 	 * @since 3.4.0
       
    69 	 */
       
    70 	public function enqueue() {}
       
    71 
       
    72 
       
    73 	/**
       
    74 	 * Fetch a setting's value.
       
    75 	 * Grabs the main setting by default.
       
    76 	 *
       
    77 	 * @since 3.4.0
       
    78 	 */
       
    79 	public final function value( $setting_key = 'default' ) {
       
    80 		if ( isset( $this->settings[ $setting_key ] ) )
       
    81 			return $this->settings[ $setting_key ]->value();
       
    82 	}
       
    83 
       
    84 	/**
       
    85 	 * Refresh the parameters passed to the JavaScript via JSON.
       
    86 	 *
       
    87 	 * @since 3.4.0
       
    88 	 */
       
    89 	public function to_json() {
       
    90 		$this->json['settings'] = array();
       
    91 		foreach ( $this->settings as $key => $setting ) {
       
    92 			$this->json['settings'][ $key ] = $setting->id;
       
    93 		}
       
    94 
       
    95 		$this->json['type'] = $this->type;
       
    96 	}
       
    97 
       
    98 	/**
       
    99 	 * Check if the theme supports the control and check user capabilities.
       
   100 	 *
       
   101 	 * @since 3.4.0
       
   102 	 *
       
   103 	 * @return bool False if theme doesn't support the control or user doesn't have the required permissions, otherwise true.
       
   104 	 */
       
   105 	public final function check_capabilities() {
       
   106 		foreach ( $this->settings as $setting ) {
       
   107 			if ( ! $setting->check_capabilities() )
       
   108 				return false;
       
   109 		}
       
   110 
       
   111 		$section = $this->manager->get_section( $this->section );
       
   112 		if ( isset( $section ) && ! $section->check_capabilities() )
       
   113 			return false;
       
   114 
       
   115 		return true;
       
   116 	}
       
   117 
       
   118 	/**
       
   119 	 * Check capabilities and render the control.
       
   120 	 *
       
   121 	 * @since 3.4.0
       
   122 	 */
       
   123 	public final function maybe_render() {
       
   124 		if ( ! $this->check_capabilities() )
       
   125 			return;
       
   126 
       
   127 		do_action( 'customize_render_control', $this );
       
   128 		do_action( 'customize_render_control_' . $this->id, $this );
       
   129 
       
   130 		$this->render();
       
   131 	}
       
   132 
       
   133 	/**
       
   134 	 * Render the control. Renders the control wrapper, then calls $this->render_content().
       
   135 	 *
       
   136 	 * @since 3.4.0
       
   137 	 */
       
   138 	protected function render() {
       
   139 		$id    = 'customize-control-' . str_replace( '[', '-', str_replace( ']', '', $this->id ) );
       
   140 		$class = 'customize-control customize-control-' . $this->type;
       
   141 
       
   142 		?><li id="<?php echo esc_attr( $id ); ?>" class="<?php echo esc_attr( $class ); ?>">
       
   143 			<?php $this->render_content(); ?>
       
   144 		</li><?php
       
   145 	}
       
   146 
       
   147 	public function get_link( $setting_key = 'default' ) {
       
   148 		if ( ! isset( $this->settings[ $setting_key ] ) )
       
   149 			return '';
       
   150 
       
   151 		return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
       
   152 	}
       
   153 
       
   154 	public function link( $setting_key = 'default' ) {
       
   155 		echo $this->get_link( $setting_key );
       
   156 	}
       
   157 
       
   158 	/**
       
   159 	 * Render the control's content.
       
   160 	 *
       
   161 	 * Allows the content to be overriden without having to rewrite the wrapper.
       
   162 	 *
       
   163 	 * @since 3.4.0
       
   164 	 */
       
   165 	protected function render_content() {
       
   166 		switch( $this->type ) {
       
   167 			case 'text':
       
   168 				?>
       
   169 				<label>
       
   170 					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
       
   171 					<input type="text" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); ?> />
       
   172 				</label>
       
   173 				<?php
       
   174 				break;
       
   175 			case 'checkbox':
       
   176 				?>
       
   177 				<label>
       
   178 					<input type="checkbox" value="<?php echo esc_attr( $this->value() ); ?>" <?php $this->link(); checked( $this->value() ); ?> />
       
   179 					<?php echo esc_html( $this->label ); ?>
       
   180 				</label>
       
   181 				<?php
       
   182 				break;
       
   183 			case 'radio':
       
   184 				if ( empty( $this->choices ) )
       
   185 					return;
       
   186 
       
   187 				$name = '_customize-radio-' . $this->id;
       
   188 
       
   189 				?>
       
   190 				<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
       
   191 				<?php
       
   192 				foreach ( $this->choices as $value => $label ) :
       
   193 					?>
       
   194 					<label>
       
   195 						<input type="radio" value="<?php echo esc_attr( $value ); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?> />
       
   196 						<?php echo esc_html( $label ); ?><br/>
       
   197 					</label>
       
   198 					<?php
       
   199 				endforeach;
       
   200 				break;
       
   201 			case 'select':
       
   202 				if ( empty( $this->choices ) )
       
   203 					return;
       
   204 
       
   205 				?>
       
   206 				<label>
       
   207 					<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
       
   208 					<select <?php $this->link(); ?>>
       
   209 						<?php
       
   210 						foreach ( $this->choices as $value => $label )
       
   211 							echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
       
   212 						?>
       
   213 					</select>
       
   214 				</label>
       
   215 				<?php
       
   216 				break;
       
   217 			case 'dropdown-pages':
       
   218 				$dropdown = wp_dropdown_pages(
       
   219 					array(
       
   220 						'name'              => '_customize-dropdown-pages-' . $this->id,
       
   221 						'echo'              => 0,
       
   222 						'show_option_none'  => __( '&mdash; Select &mdash;' ),
       
   223 						'option_none_value' => '0',
       
   224 						'selected'          => $this->value(),
       
   225 					)
       
   226 				);
       
   227 
       
   228 				// Hackily add in the data link parameter.
       
   229 				$dropdown = str_replace( '<select', '<select ' . $this->get_link(), $dropdown );
       
   230 
       
   231 				printf(
       
   232 					'<label class="customize-control-select"><span class="customize-control-title">%s</span> %s</label>',
       
   233 					$this->label,
       
   234 					$dropdown
       
   235 				);
       
   236 				break;
       
   237 		}
       
   238 	}
       
   239 }
       
   240 
       
   241 class WP_Customize_Color_Control extends WP_Customize_Control {
       
   242 	public $type = 'color';
       
   243 	public $statuses;
       
   244 
       
   245 	public function __construct( $manager, $id, $args = array() ) {
       
   246 		$this->statuses = array( '' => __('Default') );
       
   247 		parent::__construct( $manager, $id, $args );
       
   248 	}
       
   249 
       
   250 	public function enqueue() {
       
   251 		wp_enqueue_script( 'farbtastic' );
       
   252 		wp_enqueue_style( 'farbtastic' );
       
   253 	}
       
   254 
       
   255 	public function to_json() {
       
   256 		parent::to_json();
       
   257 		$this->json['statuses'] = $this->statuses;
       
   258 	}
       
   259 
       
   260 	public function render_content() {
       
   261 		?>
       
   262 		<label>
       
   263 			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
       
   264 			<div class="customize-control-content">
       
   265 				<div class="dropdown">
       
   266 					<div class="dropdown-content">
       
   267 						<div class="dropdown-status"></div>
       
   268 					</div>
       
   269 					<div class="dropdown-arrow"></div>
       
   270 				</div>
       
   271 				<input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e('Hex Value'); ?>" />
       
   272 			</div>
       
   273 			<div class="farbtastic-placeholder"></div>
       
   274 		</label>
       
   275 		<?php
       
   276 	}
       
   277 }
       
   278 
       
   279 class WP_Customize_Upload_Control extends WP_Customize_Control {
       
   280 	public $type    = 'upload';
       
   281 	public $removed = '';
       
   282 	public $context;
       
   283 
       
   284 	public function enqueue() {
       
   285 		wp_enqueue_script( 'wp-plupload' );
       
   286 	}
       
   287 
       
   288 	public function to_json() {
       
   289 		parent::to_json();
       
   290 
       
   291 		$this->json['removed'] = $this->removed;
       
   292 
       
   293 		if ( $this->context )
       
   294 			$this->json['context'] = $this->context;
       
   295 	}
       
   296 
       
   297 	public function render_content() {
       
   298 		?>
       
   299 		<label>
       
   300 			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
       
   301 			<div>
       
   302 				<a href="#" class="button-secondary upload"><?php _e( 'Upload' ); ?></a>
       
   303 				<a href="#" class="remove"><?php _e( 'Remove' ); ?></a>
       
   304 			</div>
       
   305 		</label>
       
   306 		<?php
       
   307 	}
       
   308 }
       
   309 
       
   310 class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
       
   311 	public $type = 'image';
       
   312 	public $get_url;
       
   313 	public $statuses;
       
   314 
       
   315 	protected $tabs = array();
       
   316 
       
   317 	public function __construct( $manager, $id, $args ) {
       
   318 		$this->statuses = array( '' => __('No Image') );
       
   319 
       
   320 		parent::__construct( $manager, $id, $args );
       
   321 
       
   322 		$this->add_tab( 'upload-new', __('Upload New'), array( $this, 'tab_upload_new' ) );
       
   323 		$this->add_tab( 'uploaded',   __('Uploaded'),   array( $this, 'tab_uploaded' ) );
       
   324 
       
   325 		// Early priority to occur before $this->manager->prepare_controls();
       
   326 		add_action( 'customize_controls_init', array( $this, 'prepare_control' ), 5 );
       
   327 	}
       
   328 
       
   329 	/**
       
   330 	 * Prepares the control.
       
   331 	 *
       
   332 	 * If no tabs exist, removes the control from the manager.
       
   333 	 *
       
   334 	 * @since 3.4.2
       
   335 	 */
       
   336 	public function prepare_control() {
       
   337 		if ( ! $this->tabs )
       
   338 			$this->manager->remove_control( $this->id );
       
   339 	}
       
   340 
       
   341 	public function to_json() {
       
   342 		parent::to_json();
       
   343 		$this->json['statuses'] = $this->statuses;
       
   344 	}
       
   345 
       
   346 	public function render_content() {
       
   347 		$src = $this->value();
       
   348 		if ( isset( $this->get_url ) )
       
   349 			$src = call_user_func( $this->get_url, $src );
       
   350 
       
   351 		?>
       
   352 		<div class="customize-image-picker">
       
   353 			<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
       
   354 
       
   355 			<div class="customize-control-content">
       
   356 				<div class="dropdown preview-thumbnail">
       
   357 					<div class="dropdown-content">
       
   358 						<?php if ( empty( $src ) ): ?>
       
   359 							<img style="display:none;" />
       
   360 						<?php else: ?>
       
   361 							<img src="<?php echo esc_url( set_url_scheme( $src ) ); ?>" />
       
   362 						<?php endif; ?>
       
   363 						<div class="dropdown-status"></div>
       
   364 					</div>
       
   365 					<div class="dropdown-arrow"></div>
       
   366 				</div>
       
   367 			</div>
       
   368 
       
   369 			<div class="library">
       
   370 				<ul>
       
   371 					<?php foreach ( $this->tabs as $id => $tab ): ?>
       
   372 						<li data-customize-tab='<?php echo esc_attr( $id ); ?>'>
       
   373 							<?php echo esc_html( $tab['label'] ); ?>
       
   374 						</li>
       
   375 					<?php endforeach; ?>
       
   376 				</ul>
       
   377 				<?php foreach ( $this->tabs as $id => $tab ): ?>
       
   378 					<div class="library-content" data-customize-tab='<?php echo esc_attr( $id ); ?>'>
       
   379 						<?php call_user_func( $tab['callback'] ); ?>
       
   380 					</div>
       
   381 				<?php endforeach; ?>
       
   382 			</div>
       
   383 
       
   384 			<div class="actions">
       
   385 				<a href="#" class="remove"><?php _e( 'Remove Image' ); ?></a>
       
   386 			</div>
       
   387 		</div>
       
   388 		<?php
       
   389 	}
       
   390 
       
   391 	public function add_tab( $id, $label, $callback ) {
       
   392 		$this->tabs[ $id ] = array(
       
   393 			'label'    => $label,
       
   394 			'callback' => $callback,
       
   395 		);
       
   396 	}
       
   397 
       
   398 	public function remove_tab( $id ) {
       
   399 		unset( $this->tabs[ $id ] );
       
   400 	}
       
   401 
       
   402 	public function tab_upload_new() {
       
   403 		if ( ! _device_can_upload() ) {
       
   404 			?>
       
   405 			<p><?php _e('The web browser on your device cannot be used to upload files. You may be able to use the <a href="http://wordpress.org/extend/mobile/">native app for your device</a> instead.'); ?></p>
       
   406 			<?php
       
   407 		} else {
       
   408 			?>
       
   409 			<div class="upload-dropzone">
       
   410 				<?php _e('Drop a file here or <a href="#" class="upload">select a file</a>.'); ?>
       
   411 			</div>
       
   412 			<div class="upload-fallback">
       
   413 				<span class="button-secondary"><?php _e('Select File'); ?></span>
       
   414 			</div>
       
   415 			<?php
       
   416 		}
       
   417 	}
       
   418 
       
   419 	public function tab_uploaded() {
       
   420 		?>
       
   421 		<div class="uploaded-target"></div>
       
   422 		<?php
       
   423 	}
       
   424 
       
   425 	public function print_tab_image( $url, $thumbnail_url = null ) {
       
   426 		$url = set_url_scheme( $url );
       
   427 		$thumbnail_url = ( $thumbnail_url ) ? set_url_scheme( $thumbnail_url ) : $url;
       
   428 		?>
       
   429 		<a href="#" class="thumbnail" data-customize-image-value="<?php echo esc_url( $url ); ?>">
       
   430 			<img src="<?php echo esc_url( $thumbnail_url ); ?>" />
       
   431 		</a>
       
   432 		<?php
       
   433 	}
       
   434 }
       
   435 
       
   436 class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
       
   437 	public function __construct( $manager ) {
       
   438 		parent::__construct( $manager, 'background_image', array(
       
   439 			'label'    => __( 'Background Image' ),
       
   440 			'section'  => 'background_image',
       
   441 			'context'  => 'custom-background',
       
   442 			'get_url'  => 'get_background_image',
       
   443 		) );
       
   444 
       
   445 		if ( $this->setting->default )
       
   446 			$this->add_tab( 'default',  __('Default'),  array( $this, 'tab_default_background' ) );
       
   447 	}
       
   448 
       
   449 	public function tab_uploaded() {
       
   450 		$backgrounds = get_posts( array(
       
   451 			'post_type'  => 'attachment',
       
   452 			'meta_key'   => '_wp_attachment_is_custom_background',
       
   453 			'meta_value' => $this->manager->get_stylesheet(),
       
   454 			'orderby'    => 'none',
       
   455 			'nopaging'   => true,
       
   456 		) );
       
   457 
       
   458 		?><div class="uploaded-target"></div><?php
       
   459 
       
   460 		if ( empty( $backgrounds ) )
       
   461 			return;
       
   462 
       
   463 		foreach ( (array) $backgrounds as $background )
       
   464 			$this->print_tab_image( esc_url_raw( $background->guid ) );
       
   465 	}
       
   466 
       
   467 	public function tab_default_background() {
       
   468 		$this->print_tab_image( $this->setting->default );
       
   469 	}
       
   470 }
       
   471 
       
   472 class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
       
   473 	/**
       
   474 	 * The processed default headers.
       
   475 	 * @since 3.4.2
       
   476 	 * @var array
       
   477 	 */
       
   478 	protected $default_headers;
       
   479 
       
   480 	/**
       
   481 	 * The uploaded headers.
       
   482 	 * @since 3.4.2
       
   483 	 * @var array
       
   484 	 */
       
   485 	protected $uploaded_headers;
       
   486 
       
   487 	public function __construct( $manager ) {
       
   488 		parent::__construct( $manager, 'header_image', array(
       
   489 			'label'    => __( 'Header Image' ),
       
   490 			'settings' => array(
       
   491 				'default' => 'header_image',
       
   492 				'data'    => 'header_image_data',
       
   493 			),
       
   494 			'section'  => 'header_image',
       
   495 			'context'  => 'custom-header',
       
   496 			'removed'  => 'remove-header',
       
   497 			'get_url'  => 'get_header_image',
       
   498 			'statuses' => array(
       
   499 				''                      => __('Default'),
       
   500 				'remove-header'         => __('No Image'),
       
   501 				'random-default-image'  => __('Random Default Image'),
       
   502 				'random-uploaded-image' => __('Random Uploaded Image'),
       
   503 			)
       
   504 		) );
       
   505 
       
   506 		// Remove the upload tab.
       
   507 		$this->remove_tab( 'upload-new' );
       
   508 	}
       
   509 
       
   510 	/**
       
   511 	 * Prepares the control.
       
   512 	 *
       
   513 	 * If no tabs exist, removes the control from the manager.
       
   514 	 *
       
   515 	 * @since 3.4.2
       
   516 	 */
       
   517 	public function prepare_control() {
       
   518 		global $custom_image_header;
       
   519 		if ( empty( $custom_image_header ) )
       
   520 			return parent::prepare_control();
       
   521 
       
   522 		// Process default headers and uploaded headers.
       
   523 		$custom_image_header->process_default_headers();
       
   524 		$this->default_headers = $custom_image_header->default_headers;
       
   525 		$this->uploaded_headers = get_uploaded_header_images();
       
   526 
       
   527 		if ( $this->default_headers )
       
   528 			$this->add_tab( 'default',  __('Default'),  array( $this, 'tab_default_headers' ) );
       
   529 
       
   530 		if ( ! $this->uploaded_headers )
       
   531 			$this->remove_tab( 'uploaded' );
       
   532 
       
   533 		return parent::prepare_control();
       
   534 	}
       
   535 
       
   536 	public function print_header_image( $choice, $header ) {
       
   537 		$header['url']           = set_url_scheme( $header['url'] );
       
   538 		$header['thumbnail_url'] = set_url_scheme( $header['thumbnail_url'] );
       
   539 
       
   540 		$header_image_data = array( 'choice' => $choice );
       
   541 		foreach ( array( 'attachment_id', 'width', 'height', 'url', 'thumbnail_url' ) as $key ) {
       
   542 			if ( isset( $header[ $key ] ) )
       
   543 				$header_image_data[ $key ] = $header[ $key ];
       
   544 		}
       
   545 
       
   546 
       
   547 		?>
       
   548 		<a href="#" class="thumbnail"
       
   549 			data-customize-image-value="<?php echo esc_url( $header['url'] ); ?>"
       
   550 			data-customize-header-image-data="<?php echo esc_attr( json_encode( $header_image_data ) ); ?>">
       
   551 			<img src="<?php echo esc_url( $header['thumbnail_url'] ); ?>" />
       
   552 		</a>
       
   553 		<?php
       
   554 	}
       
   555 
       
   556 	public function tab_uploaded() {
       
   557 		?><div class="uploaded-target"></div><?php
       
   558 
       
   559 		foreach ( $this->uploaded_headers as $choice => $header )
       
   560 			$this->print_header_image( $choice, $header );
       
   561 	}
       
   562 
       
   563 	public function tab_default_headers() {
       
   564 		foreach ( $this->default_headers as $choice => $header )
       
   565 			$this->print_header_image( $choice, $header );
       
   566 	}
       
   567 }