wp/wp-includes/class-wp-customize-control.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @subpackage Customize
     6  * @subpackage Customize
     7  * @since 3.4.0
     7  * @since 3.4.0
     8  */
     8  */
       
     9 
       
    10 // Don't load directly.
       
    11 if ( ! defined( 'ABSPATH' ) ) {
       
    12 	die( '-1' );
       
    13 }
     9 
    14 
    10 /**
    15 /**
    11  * Customize Control class.
    16  * Customize Control class.
    12  *
    17  *
    13  * @since 3.4.0
    18  * @since 3.4.0
   240 		}
   245 		}
   241 		$this->settings = $settings;
   246 		$this->settings = $settings;
   242 	}
   247 	}
   243 
   248 
   244 	/**
   249 	/**
   245 	 * Enqueue control related scripts/styles.
   250 	 * Enqueues control related scripts/styles.
   246 	 *
   251 	 *
   247 	 * @since 3.4.0
   252 	 * @since 3.4.0
   248 	 */
   253 	 */
   249 	public function enqueue() {}
   254 	public function enqueue() {}
   250 
   255 
   251 	/**
   256 	/**
   252 	 * Check whether control is active to current Customizer preview.
   257 	 * Checks whether control is active to current Customizer preview.
   253 	 *
   258 	 *
   254 	 * @since 4.0.0
   259 	 * @since 4.0.0
   255 	 *
   260 	 *
   256 	 * @return bool Whether the control is active to the current preview.
   261 	 * @return bool Whether the control is active to the current preview.
   257 	 */
   262 	 */
   285 	public function active_callback() {
   290 	public function active_callback() {
   286 		return true;
   291 		return true;
   287 	}
   292 	}
   288 
   293 
   289 	/**
   294 	/**
   290 	 * Fetch a setting's value.
   295 	 * Fetches a setting's value.
   291 	 * Grabs the main setting by default.
   296 	 * Grabs the main setting by default.
   292 	 *
   297 	 *
   293 	 * @since 3.4.0
   298 	 * @since 3.4.0
   294 	 *
   299 	 *
   295 	 * @param string $setting_key
   300 	 * @param string $setting_key
   300 			return $this->settings[ $setting_key ]->value();
   305 			return $this->settings[ $setting_key ]->value();
   301 		}
   306 		}
   302 	}
   307 	}
   303 
   308 
   304 	/**
   309 	/**
   305 	 * Refresh the parameters passed to the JavaScript via JSON.
   310 	 * Refreshes the parameters passed to the JavaScript via JSON.
   306 	 *
   311 	 *
   307 	 * @since 3.4.0
   312 	 * @since 3.4.0
   308 	 */
   313 	 */
   309 	public function to_json() {
   314 	public function to_json() {
   310 		$this->json['settings'] = array();
   315 		$this->json['settings'] = array();
   325 			$this->json['allow_addition'] = $this->allow_addition;
   330 			$this->json['allow_addition'] = $this->allow_addition;
   326 		}
   331 		}
   327 	}
   332 	}
   328 
   333 
   329 	/**
   334 	/**
   330 	 * Get the data to export to the client via JSON.
   335 	 * Gets the data to export to the client via JSON.
   331 	 *
   336 	 *
   332 	 * @since 4.1.0
   337 	 * @since 4.1.0
   333 	 *
   338 	 *
   334 	 * @return array Array of parameters passed to the JavaScript.
   339 	 * @return array Array of parameters passed to the JavaScript.
   335 	 */
   340 	 */
   368 
   373 
   369 		return true;
   374 		return true;
   370 	}
   375 	}
   371 
   376 
   372 	/**
   377 	/**
   373 	 * Get the control's content for insertion into the Customizer pane.
   378 	 * Gets the control's content for insertion into the Customizer pane.
   374 	 *
   379 	 *
   375 	 * @since 4.1.0
   380 	 * @since 4.1.0
   376 	 *
   381 	 *
   377 	 * @return string Contents of the control.
   382 	 * @return string Contents of the control.
   378 	 */
   383 	 */
   381 		$this->maybe_render();
   386 		$this->maybe_render();
   382 		return trim( ob_get_clean() );
   387 		return trim( ob_get_clean() );
   383 	}
   388 	}
   384 
   389 
   385 	/**
   390 	/**
   386 	 * Check capabilities and render the control.
   391 	 * Checks capabilities and render the control.
   387 	 *
   392 	 *
   388 	 * @since 3.4.0
   393 	 * @since 3.4.0
   389 	 * @uses WP_Customize_Control::render()
   394 	 * @uses WP_Customize_Control::render()
   390 	 */
   395 	 */
   391 	final public function maybe_render() {
   396 	final public function maybe_render() {
   430 		$this->render_content();
   435 		$this->render_content();
   431 		echo '</li>';
   436 		echo '</li>';
   432 	}
   437 	}
   433 
   438 
   434 	/**
   439 	/**
   435 	 * Get the data link attribute for a setting.
   440 	 * Gets the data link attribute for a setting.
   436 	 *
   441 	 *
   437 	 * @since 3.4.0
   442 	 * @since 3.4.0
   438 	 * @since 4.9.0 Return a `data-customize-setting-key-link` attribute if a setting is not registered for the supplied setting key.
   443 	 * @since 4.9.0 Return a `data-customize-setting-key-link` attribute if a setting is not registered for the supplied setting key.
   439 	 *
   444 	 *
   440 	 * @param string $setting_key
   445 	 * @param string $setting_key
   441 	 * @return string Data link parameter, a `data-customize-setting-link` attribute if the `$setting_key` refers to a pre-registered setting,
   446 	 * @return string Data link parameter, a `data-customize-setting-link` attribute if the `$setting_key` refers
   442 	 *                and a `data-customize-setting-key-link` attribute if the setting is not yet registered.
   447 	 *                to a pre-registered setting, and a `data-customize-setting-key-link` attribute if the setting
       
   448 	 *                is not yet registered.
   443 	 */
   449 	 */
   444 	public function get_link( $setting_key = 'default' ) {
   450 	public function get_link( $setting_key = 'default' ) {
   445 		if ( isset( $this->settings[ $setting_key ] ) && $this->settings[ $setting_key ] instanceof WP_Customize_Setting ) {
   451 		if ( isset( $this->settings[ $setting_key ] ) && $this->settings[ $setting_key ] instanceof WP_Customize_Setting ) {
   446 			return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
   452 			return 'data-customize-setting-link="' . esc_attr( $this->settings[ $setting_key ]->id ) . '"';
   447 		} else {
   453 		} else {
   448 			return 'data-customize-setting-key-link="' . esc_attr( $setting_key ) . '"';
   454 			return 'data-customize-setting-key-link="' . esc_attr( $setting_key ) . '"';
   449 		}
   455 		}
   450 	}
   456 	}
   451 
   457 
   452 	/**
   458 	/**
   453 	 * Render the data link attribute for the control's input element.
   459 	 * Renders the data link attribute for the control's input element.
   454 	 *
   460 	 *
   455 	 * @since 3.4.0
   461 	 * @since 3.4.0
   456 	 * @uses WP_Customize_Control::get_link()
   462 	 * @uses WP_Customize_Control::get_link()
   457 	 *
   463 	 *
   458 	 * @param string $setting_key
   464 	 * @param string $setting_key Default 'default'.
   459 	 */
   465 	 */
   460 	public function link( $setting_key = 'default' ) {
   466 	public function link( $setting_key = 'default' ) {
   461 		echo $this->get_link( $setting_key );
   467 		echo $this->get_link( $setting_key );
   462 	}
   468 	}
   463 
   469 
   464 	/**
   470 	/**
   465 	 * Render the custom attributes for the control's input element.
   471 	 * Renders the custom attributes for the control's input element.
   466 	 *
   472 	 *
   467 	 * @since 4.0.0
   473 	 * @since 4.0.0
   468 	 */
   474 	 */
   469 	public function input_attrs() {
   475 	public function input_attrs() {
   470 		foreach ( $this->input_attrs as $attr => $value ) {
   476 		foreach ( $this->input_attrs as $attr => $value ) {
   471 			echo $attr . '="' . esc_attr( $value ) . '" ';
   477 			echo $attr . '="' . esc_attr( $value ) . '" ';
   472 		}
   478 		}
   473 	}
   479 	}
   474 
   480 
   475 	/**
   481 	/**
   476 	 * Render the control's content.
   482 	 * Renders the control's content.
   477 	 *
   483 	 *
   478 	 * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
   484 	 * Allows the content to be overridden without having to rewrite the wrapper in `$this::render()`.
   479 	 *
   485 	 *
   480 	 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
   486 	 * Supports basic input types `text`, `checkbox`, `textarea`, `radio`, `select` and `dropdown-pages`.
   481 	 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
   487 	 * Additional input types such as `email`, `url`, `number`, `hidden` and `date` are supported implicitly.
   606 
   612 
   607 				// Hackily add in the data link parameter.
   613 				// Hackily add in the data link parameter.
   608 				$dropdown = str_replace( '<select', '<select ' . $this->get_link() . ' id="' . esc_attr( $input_id ) . '" ' . $describedby_attr, $dropdown );
   614 				$dropdown = str_replace( '<select', '<select ' . $this->get_link() . ' id="' . esc_attr( $input_id ) . '" ' . $describedby_attr, $dropdown );
   609 
   615 
   610 				/*
   616 				/*
   611 				 * Even more hacikly add auto-draft page stubs.
   617 				 * Even more hackily add auto-draft page stubs.
   612 				 * @todo Eventually this should be removed in favor of the pages being injected into the underlying get_pages() call.
   618 				 * @todo Eventually this should be removed in favor of the pages being injected into the underlying get_pages() call.
   613 				 * See <https://github.com/xwp/wp-customize-posts/pull/250>.
   619 				 * See <https://github.com/xwp/wp-customize-posts/pull/250>.
   614 				 */
   620 				 */
   615 				$nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
   621 				$nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
   616 				if ( $nav_menus_created_posts_setting && current_user_can( 'publish_pages' ) ) {
   622 				if ( $nav_menus_created_posts_setting && current_user_can( 'publish_pages' ) ) {
   629 				echo $dropdown;
   635 				echo $dropdown;
   630 				?>
   636 				?>
   631 				<?php if ( $this->allow_addition && current_user_can( 'publish_pages' ) && current_user_can( 'edit_theme_options' ) ) : // Currently tied to menus functionality. ?>
   637 				<?php if ( $this->allow_addition && current_user_can( 'publish_pages' ) && current_user_can( 'edit_theme_options' ) ) : // Currently tied to menus functionality. ?>
   632 					<button type="button" class="button-link add-new-toggle">
   638 					<button type="button" class="button-link add-new-toggle">
   633 						<?php
   639 						<?php
   634 						/* translators: %s: Add New Page label. */
   640 						/* translators: %s: Add Page label. */
   635 						printf( __( '+ %s' ), get_post_type_object( 'page' )->labels->add_new_item );
   641 						printf( __( '+ %s' ), get_post_type_object( 'page' )->labels->add_new_item );
   636 						?>
   642 						?>
   637 					</button>
   643 					</button>
   638 					<div class="new-content-item-wrapper">
   644 					<div class="new-content-item-wrapper">
   639 						<label for="create-input-<?php echo esc_attr( $this->id ); ?>"><?php _e( 'New page title' ); ?></label>
   645 						<label for="create-input-<?php echo esc_attr( $this->id ); ?>"><?php _e( 'New page title' ); ?></label>
   667 				break;
   673 				break;
   668 		}
   674 		}
   669 	}
   675 	}
   670 
   676 
   671 	/**
   677 	/**
   672 	 * Render the control's JS template.
   678 	 * Renders the control's JS template.
   673 	 *
   679 	 *
   674 	 * This function is only run for control types that have been registered with
   680 	 * This function is only run for control types that have been registered with
   675 	 * WP_Customize_Manager::register_control_type().
   681 	 * WP_Customize_Manager::register_control_type().
   676 	 *
   682 	 *
   677 	 * In the future, this will also print the template for the control's container
   683 	 * In the future, this will also print the template for the control's container