mirror of
https://github.com/janunger/rheinwerk-video-training.git
synced 2026-02-06 15:15:15 +01:00
Initiale Version
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Background_Image_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Background Image Control class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Image_Control
|
||||
*/
|
||||
class WP_Customize_Background_Image_Control extends WP_Customize_Image_Control {
|
||||
public $type = 'background';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @uses WP_Customize_Image_Control::__construct()
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
*/
|
||||
public function __construct( $manager ) {
|
||||
parent::__construct( $manager, 'background_image', array(
|
||||
'label' => __( 'Background Image' ),
|
||||
'section' => 'background_image',
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue control related scripts/styles.
|
||||
*
|
||||
* @since 4.1.0
|
||||
*/
|
||||
public function enqueue() {
|
||||
parent::enqueue();
|
||||
|
||||
wp_localize_script( 'customize-controls', '_wpCustomizeBackground', array(
|
||||
'nonces' => array(
|
||||
'add' => wp_create_nonce( 'background-add' ),
|
||||
),
|
||||
) );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Background_Image_Setting class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customizer Background Image Setting class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Setting
|
||||
*/
|
||||
final class WP_Customize_Background_Image_Setting extends WP_Customize_Setting {
|
||||
public $id = 'background_image_thumb';
|
||||
|
||||
/**
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @param $value
|
||||
*/
|
||||
public function update( $value ) {
|
||||
remove_theme_mod( 'background_image_thumb' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Color_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Color Control class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_Color_Control extends WP_Customize_Control {
|
||||
/**
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'color';
|
||||
|
||||
/**
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $statuses;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @uses WP_Customize_Control::__construct()
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
* @param string $id Control ID.
|
||||
* @param array $args Optional. Arguments to override class property defaults.
|
||||
*/
|
||||
public function __construct( $manager, $id, $args = array() ) {
|
||||
$this->statuses = array( '' => __('Default') );
|
||||
parent::__construct( $manager, $id, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue scripts/styles for the color picker.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*/
|
||||
public function enqueue() {
|
||||
wp_enqueue_script( 'wp-color-picker' );
|
||||
wp_enqueue_style( 'wp-color-picker' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the parameters passed to the JavaScript via JSON.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @uses WP_Customize_Control::to_json()
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
$this->json['statuses'] = $this->statuses;
|
||||
$this->json['defaultValue'] = $this->setting->default;
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't render the control content from PHP, as it's rendered via JS on load.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*/
|
||||
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.
|
||||
} #>
|
||||
<label>
|
||||
<# if ( data.label ) { #>
|
||||
<span class="customize-control-title">{{{ data.label }}}</span>
|
||||
<# } #>
|
||||
<# if ( data.description ) { #>
|
||||
<span class="description customize-control-description">{{{ data.description }}}</span>
|
||||
<# } #>
|
||||
<div class="customize-control-content">
|
||||
<input class="color-picker-hex" type="text" maxlength="7" placeholder="<?php esc_attr_e( 'Hex Value' ); ?>" {{ defaultValue }} />
|
||||
</div>
|
||||
</label>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Cropped_Image_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Cropped Image Control class.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Image_Control
|
||||
*/
|
||||
class WP_Customize_Cropped_Image_Control extends WP_Customize_Image_Control {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'cropped_image';
|
||||
|
||||
/**
|
||||
* Suggested width for cropped image.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $width = 150;
|
||||
|
||||
/**
|
||||
* Suggested height for cropped image.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $height = 150;
|
||||
|
||||
/**
|
||||
* Whether the width is flexible.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var bool
|
||||
*/
|
||||
public $flex_width = false;
|
||||
|
||||
/**
|
||||
* Whether the height is flexible.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var bool
|
||||
*/
|
||||
public $flex_height = false;
|
||||
|
||||
/**
|
||||
* Enqueue control related scripts/styles.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function enqueue() {
|
||||
wp_enqueue_script( 'customize-views' );
|
||||
|
||||
parent::enqueue();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the parameters passed to the JavaScript via JSON.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Control::to_json()
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$this->json['width'] = absint( $this->width );
|
||||
$this->json['height'] = absint( $this->height );
|
||||
$this->json['flex_width'] = absint( $this->flex_width );
|
||||
$this->json['flex_height'] = absint( $this->flex_height );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Filter_Setting class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* A setting that is used to filter a value, but will not save the results.
|
||||
*
|
||||
* Results should be properly handled using another setting or callback.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Setting
|
||||
*/
|
||||
class WP_Customize_Filter_Setting extends WP_Customize_Setting {
|
||||
|
||||
/**
|
||||
* Saves the value of the setting, using the related API.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @access public
|
||||
*
|
||||
* @param mixed $value The value to update.
|
||||
*/
|
||||
public function update( $value ) {}
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Header_Image_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Header Image Control class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Image_Control
|
||||
*/
|
||||
class WP_Customize_Header_Image_Control extends WP_Customize_Image_Control {
|
||||
public $type = 'header';
|
||||
public $uploaded_headers;
|
||||
public $default_headers;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
*/
|
||||
public function __construct( $manager ) {
|
||||
parent::__construct( $manager, 'header_image', array(
|
||||
'label' => __( 'Header Image' ),
|
||||
'settings' => array(
|
||||
'default' => 'header_image',
|
||||
'data' => 'header_image_data',
|
||||
),
|
||||
'section' => 'header_image',
|
||||
'removed' => 'remove-header',
|
||||
'get_url' => 'get_header_image',
|
||||
) );
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @access public
|
||||
*/
|
||||
public function enqueue() {
|
||||
wp_enqueue_media();
|
||||
wp_enqueue_script( 'customize-views' );
|
||||
|
||||
$this->prepare_control();
|
||||
|
||||
wp_localize_script( 'customize-views', '_wpCustomizeHeader', array(
|
||||
'data' => array(
|
||||
'width' => absint( get_theme_support( 'custom-header', 'width' ) ),
|
||||
'height' => absint( get_theme_support( 'custom-header', 'height' ) ),
|
||||
'flex-width' => absint( get_theme_support( 'custom-header', 'flex-width' ) ),
|
||||
'flex-height' => absint( get_theme_support( 'custom-header', 'flex-height' ) ),
|
||||
'currentImgSrc' => $this->get_current_image_src(),
|
||||
),
|
||||
'nonces' => array(
|
||||
'add' => wp_create_nonce( 'header-add' ),
|
||||
'remove' => wp_create_nonce( 'header-remove' ),
|
||||
),
|
||||
'uploads' => $this->uploaded_headers,
|
||||
'defaults' => $this->default_headers
|
||||
) );
|
||||
|
||||
parent::enqueue();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @global Custom_Image_Header $custom_image_header
|
||||
*/
|
||||
public function prepare_control() {
|
||||
global $custom_image_header;
|
||||
if ( empty( $custom_image_header ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Process default headers and uploaded headers.
|
||||
$custom_image_header->process_default_headers();
|
||||
$this->default_headers = $custom_image_header->get_default_header_images();
|
||||
$this->uploaded_headers = $custom_image_header->get_uploaded_header_images();
|
||||
}
|
||||
|
||||
/**
|
||||
* @access public
|
||||
*/
|
||||
public function print_header_image_template() {
|
||||
?>
|
||||
<script type="text/template" id="tmpl-header-choice">
|
||||
<# if (data.random) { #>
|
||||
<button type="button" class="button display-options random">
|
||||
<span class="dashicons dashicons-randomize dice"></span>
|
||||
<# if ( data.type === 'uploaded' ) { #>
|
||||
<?php _e( 'Randomize uploaded headers' ); ?>
|
||||
<# } else if ( data.type === 'default' ) { #>
|
||||
<?php _e( 'Randomize suggested headers' ); ?>
|
||||
<# } #>
|
||||
</button>
|
||||
|
||||
<# } else { #>
|
||||
|
||||
<# if (data.type === 'uploaded') { #>
|
||||
<button type="button" class="dashicons dashicons-no close"><span class="screen-reader-text"><?php _e( 'Remove image' ); ?></span></button>
|
||||
<# } #>
|
||||
|
||||
<button type="button" class="choice thumbnail"
|
||||
data-customize-image-value="{{{data.header.url}}}"
|
||||
data-customize-header-image-data="{{JSON.stringify(data.header)}}">
|
||||
<span class="screen-reader-text"><?php _e( 'Set image' ); ?></span>
|
||||
<img src="{{{data.header.thumbnail_url}}}" alt="{{{data.header.alt_text || data.header.description}}}">
|
||||
</button>
|
||||
|
||||
<# } #>
|
||||
</script>
|
||||
|
||||
<script type="text/template" id="tmpl-header-current">
|
||||
<# if (data.choice) { #>
|
||||
<# if (data.random) { #>
|
||||
|
||||
<div class="placeholder">
|
||||
<div class="inner">
|
||||
<span><span class="dashicons dashicons-randomize dice"></span>
|
||||
<# if ( data.type === 'uploaded' ) { #>
|
||||
<?php _e( 'Randomizing uploaded headers' ); ?>
|
||||
<# } else if ( data.type === 'default' ) { #>
|
||||
<?php _e( 'Randomizing suggested headers' ); ?>
|
||||
<# } #>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<# } else { #>
|
||||
|
||||
<img src="{{{data.header.thumbnail_url}}}" alt="{{{data.header.alt_text || data.header.description}}}" tabindex="0"/>
|
||||
|
||||
<# } #>
|
||||
<# } else { #>
|
||||
|
||||
<div class="placeholder">
|
||||
<div class="inner">
|
||||
<span>
|
||||
<?php _e( 'No image set' ); ?>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<# } #>
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|void
|
||||
*/
|
||||
public function get_current_image_src() {
|
||||
$src = $this->value();
|
||||
if ( isset( $this->get_url ) ) {
|
||||
$src = call_user_func( $this->get_url, $src );
|
||||
return $src;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {
|
||||
$this->print_header_image_template();
|
||||
$visibility = $this->get_current_image_src() ? '' : ' style="display:none" ';
|
||||
$width = absint( get_theme_support( 'custom-header', 'width' ) );
|
||||
$height = absint( get_theme_support( 'custom-header', 'height' ) );
|
||||
?>
|
||||
<div class="customize-control-content">
|
||||
<p class="customizer-section-intro">
|
||||
<?php
|
||||
if ( $width && $height ) {
|
||||
/* translators: %s: header size in pixels */
|
||||
printf( __( 'While you can crop images to your liking after clicking <strong>Add new image</strong>, your theme recommends a header size of %s pixels.' ),
|
||||
sprintf( '<strong>%s × %s</strong>', $width, $height )
|
||||
);
|
||||
} elseif ( $width ) {
|
||||
/* translators: %s: header width in pixels */
|
||||
printf( __( 'While you can crop images to your liking after clicking <strong>Add new image</strong>, your theme recommends a header width of %s pixels.' ),
|
||||
sprintf( '<strong>%s</strong>', $width )
|
||||
);
|
||||
} else {
|
||||
/* translators: %s: header height in pixels */
|
||||
printf( __( 'While you can crop images to your liking after clicking <strong>Add new image</strong>, your theme recommends a header height of %s pixels.' ),
|
||||
sprintf( '<strong>%s</strong>', $height )
|
||||
);
|
||||
}
|
||||
?>
|
||||
</p>
|
||||
<div class="current">
|
||||
<label for="header_image-button">
|
||||
<span class="customize-control-title">
|
||||
<?php _e( 'Current header' ); ?>
|
||||
</span>
|
||||
</label>
|
||||
<div class="container">
|
||||
</div>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<?php if ( current_user_can( 'upload_files' ) ): ?>
|
||||
<button type="button"<?php echo $visibility; ?> class="button remove" aria-label="<?php esc_attr_e( 'Hide header image' ); ?>"><?php _e( 'Hide image' ); ?></button>
|
||||
<button type="button" class="button new" id="header_image-button" aria-label="<?php esc_attr_e( 'Add new header image' ); ?>"><?php _e( 'Add new image' ); ?></button>
|
||||
<div style="clear:both"></div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<div class="choices">
|
||||
<span class="customize-control-title header-previously-uploaded">
|
||||
<?php _ex( 'Previously uploaded', 'custom headers' ); ?>
|
||||
</span>
|
||||
<div class="uploaded">
|
||||
<div class="list">
|
||||
</div>
|
||||
</div>
|
||||
<span class="customize-control-title header-default">
|
||||
<?php _ex( 'Suggested', 'custom headers' ); ?>
|
||||
</span>
|
||||
<div class="default">
|
||||
<div class="list">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Header_Image_Setting class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* A setting that is used to filter a value, but will not save the results.
|
||||
*
|
||||
* Results should be properly handled using another setting or callback.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Setting
|
||||
*/
|
||||
final class WP_Customize_Header_Image_Setting extends WP_Customize_Setting {
|
||||
public $id = 'header_image_data';
|
||||
|
||||
/**
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @global Custom_Image_Header $custom_image_header
|
||||
*
|
||||
* @param $value
|
||||
*/
|
||||
public function update( $value ) {
|
||||
global $custom_image_header;
|
||||
|
||||
// If the value doesn't exist (removed or random),
|
||||
// use the header_image value.
|
||||
if ( ! $value )
|
||||
$value = $this->manager->get_setting('header_image')->post_value();
|
||||
|
||||
if ( is_array( $value ) && isset( $value['choice'] ) )
|
||||
$custom_image_header->set_header_image( $value['choice'] );
|
||||
else
|
||||
$custom_image_header->set_header_image( $value );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Image_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Image Control class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Upload_Control
|
||||
*/
|
||||
class WP_Customize_Image_Control extends WP_Customize_Upload_Control {
|
||||
public $type = 'image';
|
||||
public $mime_type = 'image';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @uses WP_Customize_Upload_Control::__construct()
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
* @param string $id Control ID.
|
||||
* @param array $args Optional. Arguments to override class property defaults.
|
||||
*/
|
||||
public function __construct( $manager, $id, $args = array() ) {
|
||||
parent::__construct( $manager, $id, $args );
|
||||
|
||||
$this->button_labels = wp_parse_args( $this->button_labels, array(
|
||||
'select' => __( 'Select Image' ),
|
||||
'change' => __( 'Change Image' ),
|
||||
'remove' => __( 'Remove' ),
|
||||
'default' => __( 'Default' ),
|
||||
'placeholder' => __( 'No image selected' ),
|
||||
'frame_title' => __( 'Select Image' ),
|
||||
'frame_button' => __( 'Choose Image' ),
|
||||
) );
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 3.4.2
|
||||
* @deprecated 4.1.0
|
||||
*/
|
||||
public function prepare_control() {}
|
||||
|
||||
/**
|
||||
* @since 3.4.0
|
||||
* @deprecated 4.1.0
|
||||
*
|
||||
* @param string $id
|
||||
* @param string $label
|
||||
* @param mixed $callback
|
||||
*/
|
||||
public function add_tab( $id, $label, $callback ) {}
|
||||
|
||||
/**
|
||||
* @since 3.4.0
|
||||
* @deprecated 4.1.0
|
||||
*
|
||||
* @param string $id
|
||||
*/
|
||||
public function remove_tab( $id ) {}
|
||||
|
||||
/**
|
||||
* @since 3.4.0
|
||||
* @deprecated 4.1.0
|
||||
*
|
||||
* @param string $url
|
||||
* @param string $thumbnail_url
|
||||
*/
|
||||
public function print_tab_image( $url, $thumbnail_url = null ) {}
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Media_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Media Control class.
|
||||
*
|
||||
* @since 4.2.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_Media_Control extends WP_Customize_Control {
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'media';
|
||||
|
||||
/**
|
||||
* Media control mime type.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $mime_type = '';
|
||||
|
||||
/**
|
||||
* Button labels.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
* @var array
|
||||
*/
|
||||
public $button_labels = array();
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
* @param string $id Control ID.
|
||||
* @param array $args Optional. Arguments to override class property defaults.
|
||||
*/
|
||||
public function __construct( $manager, $id, $args = array() ) {
|
||||
parent::__construct( $manager, $id, $args );
|
||||
|
||||
if ( ! ( $this instanceof WP_Customize_Image_Control ) ) {
|
||||
$this->button_labels = wp_parse_args( $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' ),
|
||||
) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueue control related scripts/styles.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @since 4.2.0 Moved from WP_Customize_Upload_Control.
|
||||
*/
|
||||
public function enqueue() {
|
||||
wp_enqueue_media();
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh the parameters passed to the JavaScript via JSON.
|
||||
*
|
||||
* @since 3.4.0
|
||||
* @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['label'] = html_entity_decode( $this->label, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
||||
$this->json['mime_type'] = $this->mime_type;
|
||||
$this->json['button_labels'] = $this->button_labels;
|
||||
$this->json['canUpload'] = current_user_can( 'upload_files' );
|
||||
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {}
|
||||
|
||||
/**
|
||||
* 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 for="{{ data.settings['default'] }}-button">
|
||||
<# if ( data.label ) { #>
|
||||
<span class="customize-control-title">{{ data.label }}</span>
|
||||
<# } #>
|
||||
<# if ( data.description ) { #>
|
||||
<span class="description customize-control-description">{{{ data.description }}}</span>
|
||||
<# } #>
|
||||
</label>
|
||||
|
||||
<# if ( data.attachment && data.attachment.id ) { #>
|
||||
<div class="current">
|
||||
<div class="container">
|
||||
<div class="attachment-media-view attachment-media-view-{{ data.attachment.type }} {{ data.attachment.orientation }}">
|
||||
<div class="thumbnail thumbnail-{{ data.attachment.type }}">
|
||||
<# if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.medium ) { #>
|
||||
<img class="attachment-thumb" src="{{ data.attachment.sizes.medium.url }}" draggable="false" alt="" />
|
||||
<# } else if ( 'image' === data.attachment.type && data.attachment.sizes && data.attachment.sizes.full ) { #>
|
||||
<img class="attachment-thumb" src="{{ data.attachment.sizes.full.url }}" draggable="false" alt="" />
|
||||
<# } else if ( 'audio' === data.attachment.type ) { #>
|
||||
<# if ( data.attachment.image && data.attachment.image.src && data.attachment.image.src !== data.attachment.icon ) { #>
|
||||
<img src="{{ data.attachment.image.src }}" class="thumbnail" draggable="false" alt="" />
|
||||
<# } else { #>
|
||||
<img src="{{ data.attachment.icon }}" class="attachment-thumb type-icon" draggable="false" alt="" />
|
||||
<# } #>
|
||||
<p class="attachment-meta attachment-meta-title">“{{ data.attachment.title }}”</p>
|
||||
<# if ( data.attachment.album || data.attachment.meta.album ) { #>
|
||||
<p class="attachment-meta"><em>{{ data.attachment.album || data.attachment.meta.album }}</em></p>
|
||||
<# } #>
|
||||
<# if ( data.attachment.artist || data.attachment.meta.artist ) { #>
|
||||
<p class="attachment-meta">{{ data.attachment.artist || data.attachment.meta.artist }}</p>
|
||||
<# } #>
|
||||
<audio style="visibility: hidden" controls class="wp-audio-shortcode" width="100%" preload="none">
|
||||
<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
|
||||
</audio>
|
||||
<# } else if ( 'video' === data.attachment.type ) { #>
|
||||
<div class="wp-media-wrapper wp-video">
|
||||
<video controls="controls" class="wp-video-shortcode" preload="metadata"
|
||||
<# if ( data.attachment.image && data.attachment.image.src !== data.attachment.icon ) { #>poster="{{ data.attachment.image.src }}"<# } #>>
|
||||
<source type="{{ data.attachment.mime }}" src="{{ data.attachment.url }}"/>
|
||||
</video>
|
||||
</div>
|
||||
<# } else { #>
|
||||
<img class="attachment-thumb type-icon icon" src="{{ data.attachment.icon }}" draggable="false" alt="" />
|
||||
<p class="attachment-title">{{ data.attachment.title }}</p>
|
||||
<# } #>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<# if ( data.canUpload ) { #>
|
||||
<button type="button" class="button remove-button">{{ data.button_labels.remove }}</button>
|
||||
<button type="button" class="button upload-button control-focus" id="{{ data.settings['default'] }}-button">{{ data.button_labels.change }}</button>
|
||||
<div style="clear:both"></div>
|
||||
<# } #>
|
||||
</div>
|
||||
<# } else { #>
|
||||
<div class="current">
|
||||
<div class="container">
|
||||
<div class="placeholder">
|
||||
<div class="inner">
|
||||
<span>
|
||||
{{ data.button_labels.placeholder }}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<# if ( data.defaultAttachment ) { #>
|
||||
<button type="button" class="button default-button">{{ data.button_labels['default'] }}</button>
|
||||
<# } #>
|
||||
<# if ( data.canUpload ) { #>
|
||||
<button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button">{{ data.button_labels.select }}</button>
|
||||
<# } #>
|
||||
<div style="clear:both"></div>
|
||||
</div>
|
||||
<# } #>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Auto_Add_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize control to represent the auto_add field for a given menu.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Auto_Add_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Type of control, used by JS.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menu_auto_add';
|
||||
|
||||
/**
|
||||
* No-op since we're using JS template.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function render_content() {}
|
||||
|
||||
/**
|
||||
* Render the Underscore template for this control.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function content_template() {
|
||||
?>
|
||||
<span class="customize-control-title"><?php _e( 'Menu options' ); ?></span>
|
||||
<label>
|
||||
<input type="checkbox" class="auto_add" />
|
||||
<?php _e( 'Automatically add new top-level pages to this menu' ); ?>
|
||||
</label>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Nav Menu Control Class.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menu';
|
||||
|
||||
/**
|
||||
* The nav menu setting.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var WP_Customize_Nav_Menu_Setting
|
||||
*/
|
||||
public $setting;
|
||||
|
||||
/**
|
||||
* Don't render the control's content - it uses a JS template instead.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {}
|
||||
|
||||
/**
|
||||
* JS/Underscore template for the control UI.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function content_template() {
|
||||
?>
|
||||
<button type="button" class="button-secondary add-new-menu-item" aria-label="<?php esc_attr_e( 'Add or remove menu items' ); ?>" aria-expanded="false" aria-controls="available-menu-items">
|
||||
<?php _e( 'Add Items' ); ?>
|
||||
</button>
|
||||
<button type="button" class="button-link reorder-toggle" aria-label="<?php esc_attr_e( 'Reorder menu items' ); ?>" aria-describedby="reorder-items-desc-{{ data.menu_id }}">
|
||||
<span class="reorder"><?php _ex( 'Reorder', 'Reorder menu items in Customizer' ); ?></span>
|
||||
<span class="reorder-done"><?php _ex( 'Done', 'Cancel reordering menu items in Customizer' ); ?></span>
|
||||
</button>
|
||||
<p class="screen-reader-text" id="reorder-items-desc-{{ data.menu_id }}"><?php _e( 'When in reorder mode, additional controls to reorder menu items will be available in the items list above.' ); ?></p>
|
||||
<span class="menu-delete-item">
|
||||
<button type="button" class="button-link menu-delete">
|
||||
<?php _e( 'Delete Menu' ); ?>
|
||||
</button>
|
||||
</span>
|
||||
<?php if ( current_theme_supports( 'menus' ) ) : ?>
|
||||
<ul class="menu-settings">
|
||||
<li class="customize-control">
|
||||
<span class="customize-control-title"><?php _e( 'Menu locations' ); ?></span>
|
||||
</li>
|
||||
|
||||
<?php foreach ( get_registered_nav_menus() as $location => $description ) : ?>
|
||||
<li class="customize-control customize-control-checkbox assigned-menu-location">
|
||||
<label>
|
||||
<input type="checkbox" data-menu-id="{{ data.menu_id }}" data-location-id="<?php echo esc_attr( $location ); ?>" class="menu-location" /> <?php echo $description; ?>
|
||||
<span class="theme-location-set"><?php
|
||||
/* translators: %s: menu name */
|
||||
printf( _x( '(Current: %s)', 'menu location' ),
|
||||
'<span class="current-menu-location-name-' . esc_attr( $location ) . '"></span>'
|
||||
);
|
||||
?></span>
|
||||
</label>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
|
||||
</ul>
|
||||
<?php endif;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return parameters for this control.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Exported parameters.
|
||||
*/
|
||||
public function json() {
|
||||
$exported = parent::json();
|
||||
$exported['menu_id'] = $this->setting->term_id;
|
||||
|
||||
return $exported;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Item_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize control to represent the name field for a given menu.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Item_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menu_item';
|
||||
|
||||
/**
|
||||
* The nav menu item setting.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var WP_Customize_Nav_Menu_Item_Setting
|
||||
*/
|
||||
public $setting;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Control::__construct()
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
* @param string $id The control ID.
|
||||
* @param array $args Optional. Overrides class property defaults.
|
||||
*/
|
||||
public function __construct( $manager, $id, $args = array() ) {
|
||||
parent::__construct( $manager, $id, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't render the control's content - it's rendered with a JS template.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {}
|
||||
|
||||
/**
|
||||
* JS/Underscore template for the control UI.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function content_template() {
|
||||
?>
|
||||
<div class="menu-item-bar">
|
||||
<div class="menu-item-handle">
|
||||
<span class="item-type" aria-hidden="true">{{ data.item_type_label }}</span>
|
||||
<span class="item-title" aria-hidden="true">
|
||||
<span class="spinner"></span>
|
||||
<span class="menu-item-title<# if ( ! data.title ) { #> no-title<# } #>">{{ data.title || wp.customize.Menus.data.l10n.untitled }}</span>
|
||||
</span>
|
||||
<span class="item-controls">
|
||||
<button type="button" class="button-link item-edit" aria-expanded="false"><span class="screen-reader-text"><?php
|
||||
/* translators: 1: Title of a menu item, 2: Type of a menu item */
|
||||
printf( __( 'Edit menu item: %1$s (%2$s)' ), '{{ data.title || wp.customize.Menus.data.l10n.untitled }}', '{{ data.item_type_label }}' );
|
||||
?></span><span class="toggle-indicator" aria-hidden="true"></span></button>
|
||||
<button type="button" class="button-link item-delete submitdelete deletion"><span class="screen-reader-text"><?php
|
||||
/* translators: 1: Title of a menu item, 2: Type of a menu item */
|
||||
printf( __( 'Remove Menu Item: %1$s (%2$s)' ), '{{ data.title || wp.customize.Menus.data.l10n.untitled }}', '{{ data.item_type_label }}' );
|
||||
?></span></button>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="menu-item-settings" id="menu-item-settings-{{ data.menu_item_id }}">
|
||||
<# if ( 'custom' === data.item_type ) { #>
|
||||
<p class="field-url description description-thin">
|
||||
<label for="edit-menu-item-url-{{ data.menu_item_id }}">
|
||||
<?php _e( 'URL' ); ?><br />
|
||||
<input class="widefat code edit-menu-item-url" type="text" id="edit-menu-item-url-{{ data.menu_item_id }}" name="menu-item-url" />
|
||||
</label>
|
||||
</p>
|
||||
<# } #>
|
||||
<p class="description description-thin">
|
||||
<label for="edit-menu-item-title-{{ data.menu_item_id }}">
|
||||
<?php _e( 'Navigation Label' ); ?><br />
|
||||
<input type="text" id="edit-menu-item-title-{{ data.menu_item_id }}" class="widefat edit-menu-item-title" name="menu-item-title" />
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-link-target description description-thin">
|
||||
<label for="edit-menu-item-target-{{ data.menu_item_id }}">
|
||||
<input type="checkbox" id="edit-menu-item-target-{{ data.menu_item_id }}" class="edit-menu-item-target" value="_blank" name="menu-item-target" />
|
||||
<?php _e( 'Open link in a new tab' ); ?>
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-title-attribute field-attr-title description description-thin">
|
||||
<label for="edit-menu-item-attr-title-{{ data.menu_item_id }}">
|
||||
<?php _e( 'Title Attribute' ); ?><br />
|
||||
<input type="text" id="edit-menu-item-attr-title-{{ data.menu_item_id }}" class="widefat edit-menu-item-attr-title" name="menu-item-attr-title" />
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-css-classes description description-thin">
|
||||
<label for="edit-menu-item-classes-{{ data.menu_item_id }}">
|
||||
<?php _e( 'CSS Classes' ); ?><br />
|
||||
<input type="text" id="edit-menu-item-classes-{{ data.menu_item_id }}" class="widefat code edit-menu-item-classes" name="menu-item-classes" />
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-xfn description description-thin">
|
||||
<label for="edit-menu-item-xfn-{{ data.menu_item_id }}">
|
||||
<?php _e( 'Link Relationship (XFN)' ); ?><br />
|
||||
<input type="text" id="edit-menu-item-xfn-{{ data.menu_item_id }}" class="widefat code edit-menu-item-xfn" name="menu-item-xfn" />
|
||||
</label>
|
||||
</p>
|
||||
<p class="field-description description description-thin">
|
||||
<label for="edit-menu-item-description-{{ data.menu_item_id }}">
|
||||
<?php _e( 'Description' ); ?><br />
|
||||
<textarea id="edit-menu-item-description-{{ data.menu_item_id }}" class="widefat edit-menu-item-description" rows="3" cols="20" name="menu-item-description">{{ data.description }}</textarea>
|
||||
<span class="description"><?php _e( 'The description will be displayed in the menu if the current theme supports it.' ); ?></span>
|
||||
</label>
|
||||
</p>
|
||||
|
||||
<div class="menu-item-actions description-thin submitbox">
|
||||
<# if ( ( 'post_type' === data.item_type || 'taxonomy' === data.item_type ) && '' !== data.original_title ) { #>
|
||||
<p class="link-to-original">
|
||||
<?php printf( __( 'Original: %s' ), '<a class="original-link" href="{{ data.url }}">{{ data.original_title }}</a>' ); ?>
|
||||
</p>
|
||||
<# } #>
|
||||
|
||||
<button type="button" class="button-link item-delete submitdelete deletion"><?php _e( 'Remove' ); ?></button>
|
||||
<span class="spinner"></span>
|
||||
</div>
|
||||
<input type="hidden" name="menu-item-db-id[{{ data.menu_item_id }}]" class="menu-item-data-db-id" value="{{ data.menu_item_id }}" />
|
||||
<input type="hidden" name="menu-item-parent-id[{{ data.menu_item_id }}]" class="menu-item-data-parent-id" value="{{ data.parent }}" />
|
||||
</div><!-- .menu-item-settings-->
|
||||
<ul class="menu-item-transport"></ul>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Return parameters for this control.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Exported parameters.
|
||||
*/
|
||||
public function json() {
|
||||
$exported = parent::json();
|
||||
$exported['menu_item_id'] = $this->setting->post_id;
|
||||
|
||||
return $exported;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,822 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Item_Setting class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Setting to represent a nav_menu.
|
||||
*
|
||||
* Subclass of WP_Customize_Setting to represent a nav_menu taxonomy term, and
|
||||
* the IDs for the nav_menu_items associated with the nav menu.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Setting
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Item_Setting extends WP_Customize_Setting {
|
||||
|
||||
const ID_PATTERN = '/^nav_menu_item\[(?P<id>-?\d+)\]$/';
|
||||
|
||||
const POST_TYPE = 'nav_menu_item';
|
||||
|
||||
const TYPE = 'nav_menu_item';
|
||||
|
||||
/**
|
||||
* Setting type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = self::TYPE;
|
||||
|
||||
/**
|
||||
* Default setting value.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var array
|
||||
*
|
||||
* @see wp_setup_nav_menu_item()
|
||||
*/
|
||||
public $default = array(
|
||||
// The $menu_item_data for wp_update_nav_menu_item().
|
||||
'object_id' => 0,
|
||||
'object' => '', // Taxonomy name.
|
||||
'menu_item_parent' => 0, // A.K.A. menu-item-parent-id; note that post_parent is different, and not included.
|
||||
'position' => 0, // A.K.A. menu_order.
|
||||
'type' => 'custom', // Note that type_label is not included here.
|
||||
'title' => '',
|
||||
'url' => '',
|
||||
'target' => '',
|
||||
'attr_title' => '',
|
||||
'description' => '',
|
||||
'classes' => '',
|
||||
'xfn' => '',
|
||||
'status' => 'publish',
|
||||
'original_title' => '',
|
||||
'nav_menu_term_id' => 0, // This will be supplied as the $menu_id arg for wp_update_nav_menu_item().
|
||||
'_invalid' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* Default transport.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @since 4.5.0 Default changed to 'refresh'
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $transport = 'refresh';
|
||||
|
||||
/**
|
||||
* The post ID represented by this setting instance. This is the db_id.
|
||||
*
|
||||
* A negative value represents a placeholder ID for a new menu not yet saved.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $post_id;
|
||||
|
||||
/**
|
||||
* Storage of pre-setup menu item to prevent wasted calls to wp_setup_nav_menu_item().
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
* @var array
|
||||
*/
|
||||
protected $value;
|
||||
|
||||
/**
|
||||
* Previous (placeholder) post ID used before creating a new menu item.
|
||||
*
|
||||
* This value will be exported to JS via the customize_save_response filter
|
||||
* so that JavaScript can update the settings to refer to the newly-assigned
|
||||
* post ID. This value is always negative to indicate it does not refer to
|
||||
* a real post.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::update()
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::amend_customize_save_response()
|
||||
*/
|
||||
public $previous_post_id;
|
||||
|
||||
/**
|
||||
* When previewing or updating a menu item, this stores the previous nav_menu_term_id
|
||||
* which ensures that we can apply the proper filters.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $original_nav_menu_term_id;
|
||||
|
||||
/**
|
||||
* Whether or not update() was called.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
* @var bool
|
||||
*/
|
||||
protected $is_updated = false;
|
||||
|
||||
/**
|
||||
* Status for calling the update method, used in customize_save_response filter.
|
||||
*
|
||||
* When status is inserted, the placeholder post ID is stored in $previous_post_id.
|
||||
* When status is error, the error is stored in $update_error.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string updated|inserted|deleted|error
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::update()
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::amend_customize_save_response()
|
||||
*/
|
||||
public $update_status;
|
||||
|
||||
/**
|
||||
* Any error object returned by wp_update_nav_menu_item() when setting is updated.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var WP_Error
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::update()
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::amend_customize_save_response()
|
||||
*/
|
||||
public $update_error;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Any supplied $args override class property defaults.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Bootstrap Customizer instance.
|
||||
* @param string $id An specific ID of the setting. Can be a
|
||||
* theme mod or option name.
|
||||
* @param array $args Optional. Setting arguments.
|
||||
*
|
||||
* @throws Exception If $id is not valid for this setting type.
|
||||
*/
|
||||
public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
|
||||
if ( empty( $manager->nav_menus ) ) {
|
||||
throw new Exception( 'Expected WP_Customize_Manager::$nav_menus to be set.' );
|
||||
}
|
||||
|
||||
if ( ! preg_match( self::ID_PATTERN, $id, $matches ) ) {
|
||||
throw new Exception( "Illegal widget setting ID: $id" );
|
||||
}
|
||||
|
||||
$this->post_id = intval( $matches['id'] );
|
||||
add_action( 'wp_update_nav_menu_item', array( $this, 'flush_cached_value' ), 10, 2 );
|
||||
|
||||
parent::__construct( $manager, $id, $args );
|
||||
|
||||
// Ensure that an initially-supplied value is valid.
|
||||
if ( isset( $this->value ) ) {
|
||||
$this->populate_value();
|
||||
foreach ( array_diff( array_keys( $this->default ), array_keys( $this->value ) ) as $missing ) {
|
||||
throw new Exception( "Supplied nav_menu_item value missing property: $missing" );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear the cached value when this nav menu item is updated.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param int $menu_id The term ID for the menu.
|
||||
* @param int $menu_item_id The post ID for the menu item.
|
||||
*/
|
||||
public function flush_cached_value( $menu_id, $menu_item_id ) {
|
||||
unset( $menu_id );
|
||||
if ( $menu_item_id === $this->post_id ) {
|
||||
$this->value = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance data for a given nav_menu_item setting.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see wp_setup_nav_menu_item()
|
||||
*
|
||||
* @return array|false Instance data array, or false if the item is marked for deletion.
|
||||
*/
|
||||
public function value() {
|
||||
if ( $this->is_previewed && $this->_previewed_blog_id === get_current_blog_id() ) {
|
||||
$undefined = new stdClass(); // Symbol.
|
||||
$post_value = $this->post_value( $undefined );
|
||||
|
||||
if ( $undefined === $post_value ) {
|
||||
$value = $this->_original_value;
|
||||
} else {
|
||||
$value = $post_value;
|
||||
}
|
||||
} else if ( isset( $this->value ) ) {
|
||||
$value = $this->value;
|
||||
} else {
|
||||
$value = false;
|
||||
|
||||
// Note that a ID of less than one indicates a nav_menu not yet inserted.
|
||||
if ( $this->post_id > 0 ) {
|
||||
$post = get_post( $this->post_id );
|
||||
if ( $post && self::POST_TYPE === $post->post_type ) {
|
||||
$value = (array) wp_setup_nav_menu_item( $post );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_array( $value ) ) {
|
||||
$value = $this->default;
|
||||
}
|
||||
|
||||
// Cache the value for future calls to avoid having to re-call wp_setup_nav_menu_item().
|
||||
$this->value = $value;
|
||||
$this->populate_value();
|
||||
$value = $this->value;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure that the value is fully populated with the necessary properties.
|
||||
*
|
||||
* Translates some properties added by wp_setup_nav_menu_item() and removes others.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::value()
|
||||
*/
|
||||
protected function populate_value() {
|
||||
if ( ! is_array( $this->value ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( isset( $this->value['menu_order'] ) ) {
|
||||
$this->value['position'] = $this->value['menu_order'];
|
||||
unset( $this->value['menu_order'] );
|
||||
}
|
||||
if ( isset( $this->value['post_status'] ) ) {
|
||||
$this->value['status'] = $this->value['post_status'];
|
||||
unset( $this->value['post_status'] );
|
||||
}
|
||||
|
||||
if ( ! isset( $this->value['original_title'] ) ) {
|
||||
$original_title = '';
|
||||
if ( 'post_type' === $this->value['type'] ) {
|
||||
$original_title = get_the_title( $this->value['object_id'] );
|
||||
} elseif ( 'taxonomy' === $this->value['type'] ) {
|
||||
$original_title = get_term_field( 'name', $this->value['object_id'], $this->value['object'], 'raw' );
|
||||
if ( is_wp_error( $original_title ) ) {
|
||||
$original_title = '';
|
||||
}
|
||||
}
|
||||
$this->value['original_title'] = html_entity_decode( $original_title, ENT_QUOTES, get_bloginfo( 'charset' ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $this->value['nav_menu_term_id'] ) && $this->post_id > 0 ) {
|
||||
$menus = wp_get_post_terms( $this->post_id, WP_Customize_Nav_Menu_Setting::TAXONOMY, array(
|
||||
'fields' => 'ids',
|
||||
) );
|
||||
if ( ! empty( $menus ) ) {
|
||||
$this->value['nav_menu_term_id'] = array_shift( $menus );
|
||||
} else {
|
||||
$this->value['nav_menu_term_id'] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ( array( 'object_id', 'menu_item_parent', 'nav_menu_term_id' ) as $key ) {
|
||||
if ( ! is_int( $this->value[ $key ] ) ) {
|
||||
$this->value[ $key ] = intval( $this->value[ $key ] );
|
||||
}
|
||||
}
|
||||
foreach ( array( 'classes', 'xfn' ) as $key ) {
|
||||
if ( is_array( $this->value[ $key ] ) ) {
|
||||
$this->value[ $key ] = implode( ' ', $this->value[ $key ] );
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! isset( $this->value['title'] ) ) {
|
||||
$this->value['title'] = '';
|
||||
}
|
||||
|
||||
if ( ! isset( $this->value['_invalid'] ) ) {
|
||||
$this->value['_invalid'] = false;
|
||||
$is_known_invalid = (
|
||||
( ( 'post_type' === $this->value['type'] || 'post_type_archive' === $this->value['type'] ) && ! post_type_exists( $this->value['object'] ) )
|
||||
||
|
||||
( 'taxonomy' === $this->value['type'] && ! taxonomy_exists( $this->value['object'] ) )
|
||||
);
|
||||
if ( $is_known_invalid ) {
|
||||
$this->value['_invalid'] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove remaining properties available on a setup nav_menu_item post object which aren't relevant to the setting value.
|
||||
$irrelevant_properties = array(
|
||||
'ID',
|
||||
'comment_count',
|
||||
'comment_status',
|
||||
'db_id',
|
||||
'filter',
|
||||
'guid',
|
||||
'ping_status',
|
||||
'pinged',
|
||||
'post_author',
|
||||
'post_content',
|
||||
'post_content_filtered',
|
||||
'post_date',
|
||||
'post_date_gmt',
|
||||
'post_excerpt',
|
||||
'post_mime_type',
|
||||
'post_modified',
|
||||
'post_modified_gmt',
|
||||
'post_name',
|
||||
'post_parent',
|
||||
'post_password',
|
||||
'post_title',
|
||||
'post_type',
|
||||
'to_ping',
|
||||
);
|
||||
foreach ( $irrelevant_properties as $property ) {
|
||||
unset( $this->value[ $property ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle previewing the setting.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @since 4.4.0 Added boolean return value.
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Manager::post_value()
|
||||
*
|
||||
* @return bool False if method short-circuited due to no-op.
|
||||
*/
|
||||
public function preview() {
|
||||
if ( $this->is_previewed ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$undefined = new stdClass();
|
||||
$is_placeholder = ( $this->post_id < 0 );
|
||||
$is_dirty = ( $undefined !== $this->post_value( $undefined ) );
|
||||
if ( ! $is_placeholder && ! $is_dirty ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->is_previewed = true;
|
||||
$this->_original_value = $this->value();
|
||||
$this->original_nav_menu_term_id = $this->_original_value['nav_menu_term_id'];
|
||||
$this->_previewed_blog_id = get_current_blog_id();
|
||||
|
||||
add_filter( 'wp_get_nav_menu_items', array( $this, 'filter_wp_get_nav_menu_items' ), 10, 3 );
|
||||
|
||||
$sort_callback = array( __CLASS__, 'sort_wp_get_nav_menu_items' );
|
||||
if ( ! has_filter( 'wp_get_nav_menu_items', $sort_callback ) ) {
|
||||
add_filter( 'wp_get_nav_menu_items', array( __CLASS__, 'sort_wp_get_nav_menu_items' ), 1000, 3 );
|
||||
}
|
||||
|
||||
// @todo Add get_post_metadata filters for plugins to add their data.
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the wp_get_nav_menu_items() result to supply the previewed menu items.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see wp_get_nav_menu_items()
|
||||
*
|
||||
* @param array $items An array of menu item post objects.
|
||||
* @param object $menu The menu object.
|
||||
* @param array $args An array of arguments used to retrieve menu item objects.
|
||||
* @return array Array of menu items,
|
||||
*/
|
||||
public function filter_wp_get_nav_menu_items( $items, $menu, $args ) {
|
||||
$this_item = $this->value();
|
||||
$current_nav_menu_term_id = $this_item['nav_menu_term_id'];
|
||||
unset( $this_item['nav_menu_term_id'] );
|
||||
|
||||
$should_filter = (
|
||||
$menu->term_id === $this->original_nav_menu_term_id
|
||||
||
|
||||
$menu->term_id === $current_nav_menu_term_id
|
||||
);
|
||||
if ( ! $should_filter ) {
|
||||
return $items;
|
||||
}
|
||||
|
||||
// Handle deleted menu item, or menu item moved to another menu.
|
||||
$should_remove = (
|
||||
false === $this_item
|
||||
||
|
||||
true === $this_item['_invalid']
|
||||
||
|
||||
(
|
||||
$this->original_nav_menu_term_id === $menu->term_id
|
||||
&&
|
||||
$current_nav_menu_term_id !== $this->original_nav_menu_term_id
|
||||
)
|
||||
);
|
||||
if ( $should_remove ) {
|
||||
$filtered_items = array();
|
||||
foreach ( $items as $item ) {
|
||||
if ( $item->db_id !== $this->post_id ) {
|
||||
$filtered_items[] = $item;
|
||||
}
|
||||
}
|
||||
return $filtered_items;
|
||||
}
|
||||
|
||||
$mutated = false;
|
||||
$should_update = (
|
||||
is_array( $this_item )
|
||||
&&
|
||||
$current_nav_menu_term_id === $menu->term_id
|
||||
);
|
||||
if ( $should_update ) {
|
||||
foreach ( $items as $item ) {
|
||||
if ( $item->db_id === $this->post_id ) {
|
||||
foreach ( get_object_vars( $this->value_as_wp_post_nav_menu_item() ) as $key => $value ) {
|
||||
$item->$key = $value;
|
||||
}
|
||||
$mutated = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Not found so we have to append it..
|
||||
if ( ! $mutated ) {
|
||||
$items[] = $this->value_as_wp_post_nav_menu_item();
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-apply the tail logic also applied on $items by wp_get_nav_menu_items().
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @static
|
||||
*
|
||||
* @see wp_get_nav_menu_items()
|
||||
*
|
||||
* @param array $items An array of menu item post objects.
|
||||
* @param object $menu The menu object.
|
||||
* @param array $args An array of arguments used to retrieve menu item objects.
|
||||
* @return array Array of menu items,
|
||||
*/
|
||||
public static function sort_wp_get_nav_menu_items( $items, $menu, $args ) {
|
||||
// @todo We should probably re-apply some constraints imposed by $args.
|
||||
unset( $args['include'] );
|
||||
|
||||
// Remove invalid items only in front end.
|
||||
if ( ! is_admin() ) {
|
||||
$items = array_filter( $items, '_is_valid_nav_menu_item' );
|
||||
}
|
||||
|
||||
if ( ARRAY_A === $args['output'] ) {
|
||||
$GLOBALS['_menu_item_sort_prop'] = $args['output_key'];
|
||||
usort( $items, '_sort_nav_menu_items' );
|
||||
$i = 1;
|
||||
|
||||
foreach ( $items as $k => $item ) {
|
||||
$items[ $k ]->{$args['output_key']} = $i++;
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value emulated into a WP_Post and set up as a nav_menu_item.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @return WP_Post With wp_setup_nav_menu_item() applied.
|
||||
*/
|
||||
public function value_as_wp_post_nav_menu_item() {
|
||||
$item = (object) $this->value();
|
||||
unset( $item->nav_menu_term_id );
|
||||
|
||||
$item->post_status = $item->status;
|
||||
unset( $item->status );
|
||||
|
||||
$item->post_type = 'nav_menu_item';
|
||||
$item->menu_order = $item->position;
|
||||
unset( $item->position );
|
||||
|
||||
if ( $item->title ) {
|
||||
$item->post_title = $item->title;
|
||||
}
|
||||
|
||||
$item->ID = $this->post_id;
|
||||
$item->db_id = $this->post_id;
|
||||
$post = new WP_Post( (object) $item );
|
||||
|
||||
if ( empty( $post->post_author ) ) {
|
||||
$post->post_author = get_current_user_id();
|
||||
}
|
||||
|
||||
if ( ! isset( $post->type_label ) ) {
|
||||
if ( 'post_type' === $post->type ) {
|
||||
$object = get_post_type_object( $post->object );
|
||||
if ( $object ) {
|
||||
$post->type_label = $object->labels->singular_name;
|
||||
} else {
|
||||
$post->type_label = $post->object;
|
||||
}
|
||||
} elseif ( 'taxonomy' == $post->type ) {
|
||||
$object = get_taxonomy( $post->object );
|
||||
if ( $object ) {
|
||||
$post->type_label = $object->labels->singular_name;
|
||||
} else {
|
||||
$post->type_label = $post->object;
|
||||
}
|
||||
} else {
|
||||
$post->type_label = __( 'Custom Link' );
|
||||
}
|
||||
}
|
||||
|
||||
/** This filter is documented in wp-includes/nav-menu.php */
|
||||
$post->attr_title = apply_filters( 'nav_menu_attr_title', $post->attr_title );
|
||||
|
||||
/** This filter is documented in wp-includes/nav-menu.php */
|
||||
$post->description = apply_filters( 'nav_menu_description', wp_trim_words( $post->description, 200 ) );
|
||||
|
||||
return $post;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize an input.
|
||||
*
|
||||
* Note that parent::sanitize() erroneously does wp_unslash() on $value, but
|
||||
* we remove that in this override.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $menu_item_value The value to sanitize.
|
||||
* @return array|false|null Null if an input isn't valid. False if it is marked for deletion.
|
||||
* Otherwise the sanitized value.
|
||||
*/
|
||||
public function sanitize( $menu_item_value ) {
|
||||
// Menu is marked for deletion.
|
||||
if ( false === $menu_item_value ) {
|
||||
return $menu_item_value;
|
||||
}
|
||||
|
||||
// Invalid.
|
||||
if ( ! is_array( $menu_item_value ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$default = array(
|
||||
'object_id' => 0,
|
||||
'object' => '',
|
||||
'menu_item_parent' => 0,
|
||||
'position' => 0,
|
||||
'type' => 'custom',
|
||||
'title' => '',
|
||||
'url' => '',
|
||||
'target' => '',
|
||||
'attr_title' => '',
|
||||
'description' => '',
|
||||
'classes' => '',
|
||||
'xfn' => '',
|
||||
'status' => 'publish',
|
||||
'original_title' => '',
|
||||
'nav_menu_term_id' => 0,
|
||||
'_invalid' => false,
|
||||
);
|
||||
$menu_item_value = array_merge( $default, $menu_item_value );
|
||||
$menu_item_value = wp_array_slice_assoc( $menu_item_value, array_keys( $default ) );
|
||||
$menu_item_value['position'] = intval( $menu_item_value['position'] );
|
||||
|
||||
foreach ( array( 'object_id', 'menu_item_parent', 'nav_menu_term_id' ) as $key ) {
|
||||
// Note we need to allow negative-integer IDs for previewed objects not inserted yet.
|
||||
$menu_item_value[ $key ] = intval( $menu_item_value[ $key ] );
|
||||
}
|
||||
|
||||
foreach ( array( 'type', 'object', 'target' ) as $key ) {
|
||||
$menu_item_value[ $key ] = sanitize_key( $menu_item_value[ $key ] );
|
||||
}
|
||||
|
||||
foreach ( array( 'xfn', 'classes' ) as $key ) {
|
||||
$value = $menu_item_value[ $key ];
|
||||
if ( ! is_array( $value ) ) {
|
||||
$value = explode( ' ', $value );
|
||||
}
|
||||
$menu_item_value[ $key ] = implode( ' ', array_map( 'sanitize_html_class', $value ) );
|
||||
}
|
||||
|
||||
$menu_item_value['original_title'] = sanitize_text_field( $menu_item_value['original_title'] );
|
||||
|
||||
// Apply the same filters as when calling wp_insert_post().
|
||||
$menu_item_value['title'] = wp_unslash( apply_filters( 'title_save_pre', wp_slash( $menu_item_value['title'] ) ) );
|
||||
$menu_item_value['attr_title'] = wp_unslash( apply_filters( 'excerpt_save_pre', wp_slash( $menu_item_value['attr_title'] ) ) );
|
||||
$menu_item_value['description'] = wp_unslash( apply_filters( 'content_save_pre', wp_slash( $menu_item_value['description'] ) ) );
|
||||
|
||||
$menu_item_value['url'] = esc_url_raw( $menu_item_value['url'] );
|
||||
if ( 'publish' !== $menu_item_value['status'] ) {
|
||||
$menu_item_value['status'] = 'draft';
|
||||
}
|
||||
|
||||
$menu_item_value['_invalid'] = (bool) $menu_item_value['_invalid'];
|
||||
|
||||
/** This filter is documented in wp-includes/class-wp-customize-setting.php */
|
||||
return apply_filters( "customize_sanitize_{$this->id}", $menu_item_value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Create/update the nav_menu_item post for this setting.
|
||||
*
|
||||
* Any created menu items will have their assigned post IDs exported to the client
|
||||
* via the customize_save_response filter. Likewise, any errors will be exported
|
||||
* to the client via the customize_save_response() filter.
|
||||
*
|
||||
* To delete a menu, the client can send false as the value.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*
|
||||
* @see wp_update_nav_menu_item()
|
||||
*
|
||||
* @param array|false $value The menu item array to update. If false, then the menu item will be deleted
|
||||
* entirely. See WP_Customize_Nav_Menu_Item_Setting::$default for what the value
|
||||
* should consist of.
|
||||
* @return null|void
|
||||
*/
|
||||
protected function update( $value ) {
|
||||
if ( $this->is_updated ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->is_updated = true;
|
||||
$is_placeholder = ( $this->post_id < 0 );
|
||||
$is_delete = ( false === $value );
|
||||
|
||||
// Update the cached value.
|
||||
$this->value = $value;
|
||||
|
||||
add_filter( 'customize_save_response', array( $this, 'amend_customize_save_response' ) );
|
||||
|
||||
if ( $is_delete ) {
|
||||
// If the current setting post is a placeholder, a delete request is a no-op.
|
||||
if ( $is_placeholder ) {
|
||||
$this->update_status = 'deleted';
|
||||
} else {
|
||||
$r = wp_delete_post( $this->post_id, true );
|
||||
|
||||
if ( false === $r ) {
|
||||
$this->update_error = new WP_Error( 'delete_failure' );
|
||||
$this->update_status = 'error';
|
||||
} else {
|
||||
$this->update_status = 'deleted';
|
||||
}
|
||||
// @todo send back the IDs for all associated nav menu items deleted, so these settings (and controls) can be removed from Customizer?
|
||||
}
|
||||
} else {
|
||||
|
||||
// Handle saving menu items for menus that are being newly-created.
|
||||
if ( $value['nav_menu_term_id'] < 0 ) {
|
||||
$nav_menu_setting_id = sprintf( 'nav_menu[%s]', $value['nav_menu_term_id'] );
|
||||
$nav_menu_setting = $this->manager->get_setting( $nav_menu_setting_id );
|
||||
|
||||
if ( ! $nav_menu_setting || ! ( $nav_menu_setting instanceof WP_Customize_Nav_Menu_Setting ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = new WP_Error( 'unexpected_nav_menu_setting' );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( false === $nav_menu_setting->save() ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = new WP_Error( 'nav_menu_setting_failure' );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $nav_menu_setting->previous_term_id !== intval( $value['nav_menu_term_id'] ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = new WP_Error( 'unexpected_previous_term_id' );
|
||||
return;
|
||||
}
|
||||
|
||||
$value['nav_menu_term_id'] = $nav_menu_setting->term_id;
|
||||
}
|
||||
|
||||
// Handle saving a nav menu item that is a child of a nav menu item being newly-created.
|
||||
if ( $value['menu_item_parent'] < 0 ) {
|
||||
$parent_nav_menu_item_setting_id = sprintf( 'nav_menu_item[%s]', $value['menu_item_parent'] );
|
||||
$parent_nav_menu_item_setting = $this->manager->get_setting( $parent_nav_menu_item_setting_id );
|
||||
|
||||
if ( ! $parent_nav_menu_item_setting || ! ( $parent_nav_menu_item_setting instanceof WP_Customize_Nav_Menu_Item_Setting ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = new WP_Error( 'unexpected_nav_menu_item_setting' );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( false === $parent_nav_menu_item_setting->save() ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = new WP_Error( 'nav_menu_item_setting_failure' );
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $parent_nav_menu_item_setting->previous_post_id !== intval( $value['menu_item_parent'] ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = new WP_Error( 'unexpected_previous_post_id' );
|
||||
return;
|
||||
}
|
||||
|
||||
$value['menu_item_parent'] = $parent_nav_menu_item_setting->post_id;
|
||||
}
|
||||
|
||||
// Insert or update menu.
|
||||
$menu_item_data = array(
|
||||
'menu-item-object-id' => $value['object_id'],
|
||||
'menu-item-object' => $value['object'],
|
||||
'menu-item-parent-id' => $value['menu_item_parent'],
|
||||
'menu-item-position' => $value['position'],
|
||||
'menu-item-type' => $value['type'],
|
||||
'menu-item-title' => $value['title'],
|
||||
'menu-item-url' => $value['url'],
|
||||
'menu-item-description' => $value['description'],
|
||||
'menu-item-attr-title' => $value['attr_title'],
|
||||
'menu-item-target' => $value['target'],
|
||||
'menu-item-classes' => $value['classes'],
|
||||
'menu-item-xfn' => $value['xfn'],
|
||||
'menu-item-status' => $value['status'],
|
||||
);
|
||||
|
||||
$r = wp_update_nav_menu_item(
|
||||
$value['nav_menu_term_id'],
|
||||
$is_placeholder ? 0 : $this->post_id,
|
||||
wp_slash( $menu_item_data )
|
||||
);
|
||||
|
||||
if ( is_wp_error( $r ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = $r;
|
||||
} else {
|
||||
if ( $is_placeholder ) {
|
||||
$this->previous_post_id = $this->post_id;
|
||||
$this->post_id = $r;
|
||||
$this->update_status = 'inserted';
|
||||
} else {
|
||||
$this->update_status = 'updated';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Export data for the JS client.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Item_Setting::update()
|
||||
*
|
||||
* @param array $data Additional information passed back to the 'saved' event on `wp.customize`.
|
||||
* @return array Save response data.
|
||||
*/
|
||||
public function amend_customize_save_response( $data ) {
|
||||
if ( ! isset( $data['nav_menu_item_updates'] ) ) {
|
||||
$data['nav_menu_item_updates'] = array();
|
||||
}
|
||||
|
||||
$data['nav_menu_item_updates'][] = array(
|
||||
'post_id' => $this->post_id,
|
||||
'previous_post_id' => $this->previous_post_id,
|
||||
'error' => $this->update_error ? $this->update_error->get_error_code() : null,
|
||||
'status' => $this->update_status,
|
||||
);
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Location_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Menu Location Control Class.
|
||||
*
|
||||
* This custom control is only needed for JS.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Location_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menu_location';
|
||||
|
||||
/**
|
||||
* Location ID.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $location_id = '';
|
||||
|
||||
/**
|
||||
* Refresh the parameters passed to JavaScript via JSON.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Control::to_json()
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
$this->json['locationId'] = $this->location_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render content just like a normal select control.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {
|
||||
if ( empty( $this->choices ) ) {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<label>
|
||||
<?php if ( ! empty( $this->label ) ) : ?>
|
||||
<span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if ( ! empty( $this->description ) ) : ?>
|
||||
<span class="description customize-control-description"><?php echo $this->description; ?></span>
|
||||
<?php endif; ?>
|
||||
|
||||
<select <?php $this->link(); ?>>
|
||||
<?php
|
||||
foreach ( $this->choices as $value => $label ) :
|
||||
echo '<option value="' . esc_attr( $value ) . '"' . selected( $this->value(), $value, false ) . '>' . $label . '</option>';
|
||||
endforeach;
|
||||
?>
|
||||
</select>
|
||||
</label>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Name_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize control to represent the name field for a given menu.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Name_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Type of control, used by JS.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menu_name';
|
||||
|
||||
/**
|
||||
* No-op since we're using JS template.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function render_content() {}
|
||||
|
||||
/**
|
||||
* Render the Underscore template for this control.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function content_template() {
|
||||
?>
|
||||
<label>
|
||||
<# if ( data.label ) { #>
|
||||
<span class="customize-control-title screen-reader-text">{{ data.label }}</span>
|
||||
<# } #>
|
||||
<input type="text" class="menu-name-field live-update-section-title" />
|
||||
</label>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Section class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Menu Section Class
|
||||
*
|
||||
* Custom section only needed in JS.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Section
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Section extends WP_Customize_Section {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menu';
|
||||
|
||||
/**
|
||||
* Get section parameters for JS.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @return array Exported parameters.
|
||||
*/
|
||||
public function json() {
|
||||
$exported = parent::json();
|
||||
$exported['menu_id'] = intval( preg_replace( '/^nav_menu\[(\d+)\]/', '$1', $this->id ) );
|
||||
|
||||
return $exported;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,652 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menu_Setting class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Setting to represent a nav_menu.
|
||||
*
|
||||
* Subclass of WP_Customize_Setting to represent a nav_menu taxonomy term, and
|
||||
* the IDs for the nav_menu_items associated with the nav menu.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see wp_get_nav_menu_object()
|
||||
* @see WP_Customize_Setting
|
||||
*/
|
||||
class WP_Customize_Nav_Menu_Setting extends WP_Customize_Setting {
|
||||
|
||||
const ID_PATTERN = '/^nav_menu\[(?P<id>-?\d+)\]$/';
|
||||
|
||||
const TAXONOMY = 'nav_menu';
|
||||
|
||||
const TYPE = 'nav_menu';
|
||||
|
||||
/**
|
||||
* Setting type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = self::TYPE;
|
||||
|
||||
/**
|
||||
* Default setting value.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var array
|
||||
*
|
||||
* @see wp_get_nav_menu_object()
|
||||
*/
|
||||
public $default = array(
|
||||
'name' => '',
|
||||
'description' => '',
|
||||
'parent' => 0,
|
||||
'auto_add' => false,
|
||||
);
|
||||
|
||||
/**
|
||||
* Default transport.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $transport = 'postMessage';
|
||||
|
||||
/**
|
||||
* The term ID represented by this setting instance.
|
||||
*
|
||||
* A negative value represents a placeholder ID for a new menu not yet saved.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*/
|
||||
public $term_id;
|
||||
|
||||
/**
|
||||
* Previous (placeholder) term ID used before creating a new menu.
|
||||
*
|
||||
* This value will be exported to JS via the customize_save_response filter
|
||||
* so that JavaScript can update the settings to refer to the newly-assigned
|
||||
* term ID. This value is always negative to indicate it does not refer to
|
||||
* a real term.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var int
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::update()
|
||||
* @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
|
||||
*/
|
||||
public $previous_term_id;
|
||||
|
||||
/**
|
||||
* Whether or not update() was called.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
* @var bool
|
||||
*/
|
||||
protected $is_updated = false;
|
||||
|
||||
/**
|
||||
* Status for calling the update method, used in customize_save_response filter.
|
||||
*
|
||||
* When status is inserted, the placeholder term ID is stored in $previous_term_id.
|
||||
* When status is error, the error is stored in $update_error.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string updated|inserted|deleted|error
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::update()
|
||||
* @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
|
||||
*/
|
||||
public $update_status;
|
||||
|
||||
/**
|
||||
* Any error object returned by wp_update_nav_menu_object() when setting is updated.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var WP_Error
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::update()
|
||||
* @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
|
||||
*/
|
||||
public $update_error;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Any supplied $args override class property defaults.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Bootstrap Customizer instance.
|
||||
* @param string $id An specific ID of the setting. Can be a
|
||||
* theme mod or option name.
|
||||
* @param array $args Optional. Setting arguments.
|
||||
*
|
||||
* @throws Exception If $id is not valid for this setting type.
|
||||
*/
|
||||
public function __construct( WP_Customize_Manager $manager, $id, array $args = array() ) {
|
||||
if ( empty( $manager->nav_menus ) ) {
|
||||
throw new Exception( 'Expected WP_Customize_Manager::$nav_menus to be set.' );
|
||||
}
|
||||
|
||||
if ( ! preg_match( self::ID_PATTERN, $id, $matches ) ) {
|
||||
throw new Exception( "Illegal widget setting ID: $id" );
|
||||
}
|
||||
|
||||
$this->term_id = intval( $matches['id'] );
|
||||
|
||||
parent::__construct( $manager, $id, $args );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance data for a given widget setting.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see wp_get_nav_menu_object()
|
||||
*
|
||||
* @return array Instance data.
|
||||
*/
|
||||
public function value() {
|
||||
if ( $this->is_previewed && $this->_previewed_blog_id === get_current_blog_id() ) {
|
||||
$undefined = new stdClass(); // Symbol.
|
||||
$post_value = $this->post_value( $undefined );
|
||||
|
||||
if ( $undefined === $post_value ) {
|
||||
$value = $this->_original_value;
|
||||
} else {
|
||||
$value = $post_value;
|
||||
}
|
||||
} else {
|
||||
$value = false;
|
||||
|
||||
// Note that a term_id of less than one indicates a nav_menu not yet inserted.
|
||||
if ( $this->term_id > 0 ) {
|
||||
$term = wp_get_nav_menu_object( $this->term_id );
|
||||
|
||||
if ( $term ) {
|
||||
$value = wp_array_slice_assoc( (array) $term, array_keys( $this->default ) );
|
||||
|
||||
$nav_menu_options = (array) get_option( 'nav_menu_options', array() );
|
||||
$value['auto_add'] = false;
|
||||
|
||||
if ( isset( $nav_menu_options['auto_add'] ) && is_array( $nav_menu_options['auto_add'] ) ) {
|
||||
$value['auto_add'] = in_array( $term->term_id, $nav_menu_options['auto_add'] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! is_array( $value ) ) {
|
||||
$value = $this->default;
|
||||
}
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle previewing the setting.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @since 4.4.0 Added boolean return value
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Manager::post_value()
|
||||
*
|
||||
* @return bool False if method short-circuited due to no-op.
|
||||
*/
|
||||
public function preview() {
|
||||
if ( $this->is_previewed ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$undefined = new stdClass();
|
||||
$is_placeholder = ( $this->term_id < 0 );
|
||||
$is_dirty = ( $undefined !== $this->post_value( $undefined ) );
|
||||
if ( ! $is_placeholder && ! $is_dirty ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$this->is_previewed = true;
|
||||
$this->_original_value = $this->value();
|
||||
$this->_previewed_blog_id = get_current_blog_id();
|
||||
|
||||
add_filter( 'wp_get_nav_menus', array( $this, 'filter_wp_get_nav_menus' ), 10, 2 );
|
||||
add_filter( 'wp_get_nav_menu_object', array( $this, 'filter_wp_get_nav_menu_object' ), 10, 2 );
|
||||
add_filter( 'default_option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
|
||||
add_filter( 'option_nav_menu_options', array( $this, 'filter_nav_menu_options' ) );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the wp_get_nav_menus() result to ensure the inserted menu object is included, and the deleted one is removed.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see wp_get_nav_menus()
|
||||
*
|
||||
* @param array $menus An array of menu objects.
|
||||
* @param array $args An array of arguments used to retrieve menu objects.
|
||||
* @return array
|
||||
*/
|
||||
public function filter_wp_get_nav_menus( $menus, $args ) {
|
||||
if ( get_current_blog_id() !== $this->_previewed_blog_id ) {
|
||||
return $menus;
|
||||
}
|
||||
|
||||
$setting_value = $this->value();
|
||||
$is_delete = ( false === $setting_value );
|
||||
$index = -1;
|
||||
|
||||
// Find the existing menu item's position in the list.
|
||||
foreach ( $menus as $i => $menu ) {
|
||||
if ( (int) $this->term_id === (int) $menu->term_id || (int) $this->previous_term_id === (int) $menu->term_id ) {
|
||||
$index = $i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $is_delete ) {
|
||||
// Handle deleted menu by removing it from the list.
|
||||
if ( -1 !== $index ) {
|
||||
array_splice( $menus, $index, 1 );
|
||||
}
|
||||
} else {
|
||||
// Handle menus being updated or inserted.
|
||||
$menu_obj = (object) array_merge( array(
|
||||
'term_id' => $this->term_id,
|
||||
'term_taxonomy_id' => $this->term_id,
|
||||
'slug' => sanitize_title( $setting_value['name'] ),
|
||||
'count' => 0,
|
||||
'term_group' => 0,
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'filter' => 'raw',
|
||||
), $setting_value );
|
||||
|
||||
array_splice( $menus, $index, ( -1 === $index ? 0 : 1 ), array( $menu_obj ) );
|
||||
}
|
||||
|
||||
// Make sure the menu objects get re-sorted after an update/insert.
|
||||
if ( ! $is_delete && ! empty( $args['orderby'] ) ) {
|
||||
$this->_current_menus_sort_orderby = $args['orderby'];
|
||||
usort( $menus, array( $this, '_sort_menus_by_orderby' ) );
|
||||
}
|
||||
// @todo add support for $args['hide_empty'] === true
|
||||
|
||||
return $menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Temporary non-closure passing of orderby value to function.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
* @var string
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
|
||||
* @see WP_Customize_Nav_Menu_Setting::_sort_menus_by_orderby()
|
||||
*/
|
||||
protected $_current_menus_sort_orderby;
|
||||
|
||||
/**
|
||||
* Sort menu objects by the class-supplied orderby property.
|
||||
*
|
||||
* This is a workaround for a lack of closures.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
* @param object $menu1
|
||||
* @param object $menu2
|
||||
* @return int
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::filter_wp_get_nav_menus()
|
||||
*/
|
||||
protected function _sort_menus_by_orderby( $menu1, $menu2 ) {
|
||||
$key = $this->_current_menus_sort_orderby;
|
||||
return strcmp( $menu1->$key, $menu2->$key );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the wp_get_nav_menu_object() result to supply the previewed menu object.
|
||||
*
|
||||
* Requesting a nav_menu object by anything but ID is not supported.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see wp_get_nav_menu_object()
|
||||
*
|
||||
* @param object|null $menu_obj Object returned by wp_get_nav_menu_object().
|
||||
* @param string $menu_id ID of the nav_menu term. Requests by slug or name will be ignored.
|
||||
* @return object|null
|
||||
*/
|
||||
public function filter_wp_get_nav_menu_object( $menu_obj, $menu_id ) {
|
||||
$ok = (
|
||||
get_current_blog_id() === $this->_previewed_blog_id
|
||||
&&
|
||||
is_int( $menu_id )
|
||||
&&
|
||||
$menu_id === $this->term_id
|
||||
);
|
||||
if ( ! $ok ) {
|
||||
return $menu_obj;
|
||||
}
|
||||
|
||||
$setting_value = $this->value();
|
||||
|
||||
// Handle deleted menus.
|
||||
if ( false === $setting_value ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Handle sanitization failure by preventing short-circuiting.
|
||||
if ( null === $setting_value ) {
|
||||
return $menu_obj;
|
||||
}
|
||||
|
||||
$menu_obj = (object) array_merge( array(
|
||||
'term_id' => $this->term_id,
|
||||
'term_taxonomy_id' => $this->term_id,
|
||||
'slug' => sanitize_title( $setting_value['name'] ),
|
||||
'count' => 0,
|
||||
'term_group' => 0,
|
||||
'taxonomy' => self::TAXONOMY,
|
||||
'filter' => 'raw',
|
||||
), $setting_value );
|
||||
|
||||
return $menu_obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the nav_menu_options option to include this menu's auto_add preference.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $nav_menu_options Nav menu options including auto_add.
|
||||
* @return array (Kaybe) modified nav menu options.
|
||||
*/
|
||||
public function filter_nav_menu_options( $nav_menu_options ) {
|
||||
if ( $this->_previewed_blog_id !== get_current_blog_id() ) {
|
||||
return $nav_menu_options;
|
||||
}
|
||||
|
||||
$menu = $this->value();
|
||||
$nav_menu_options = $this->filter_nav_menu_options_value(
|
||||
$nav_menu_options,
|
||||
$this->term_id,
|
||||
false === $menu ? false : $menu['auto_add']
|
||||
);
|
||||
|
||||
return $nav_menu_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize an input.
|
||||
*
|
||||
* Note that parent::sanitize() erroneously does wp_unslash() on $value, but
|
||||
* we remove that in this override.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $value The value to sanitize.
|
||||
* @return array|false|null Null if an input isn't valid. False if it is marked for deletion.
|
||||
* Otherwise the sanitized value.
|
||||
*/
|
||||
public function sanitize( $value ) {
|
||||
// Menu is marked for deletion.
|
||||
if ( false === $value ) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
// Invalid.
|
||||
if ( ! is_array( $value ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$default = array(
|
||||
'name' => '',
|
||||
'description' => '',
|
||||
'parent' => 0,
|
||||
'auto_add' => false,
|
||||
);
|
||||
$value = array_merge( $default, $value );
|
||||
$value = wp_array_slice_assoc( $value, array_keys( $default ) );
|
||||
|
||||
$value['name'] = trim( esc_html( $value['name'] ) ); // This sanitization code is used in wp-admin/nav-menus.php.
|
||||
$value['description'] = sanitize_text_field( $value['description'] );
|
||||
$value['parent'] = max( 0, intval( $value['parent'] ) );
|
||||
$value['auto_add'] = ! empty( $value['auto_add'] );
|
||||
|
||||
if ( '' === $value['name'] ) {
|
||||
$value['name'] = _x( '(unnamed)', 'Missing menu name.' );
|
||||
}
|
||||
|
||||
/** This filter is documented in wp-includes/class-wp-customize-setting.php */
|
||||
return apply_filters( "customize_sanitize_{$this->id}", $value, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Storage for data to be sent back to client in customize_save_response filter.
|
||||
*
|
||||
* @access protected
|
||||
* @since 4.3.0
|
||||
* @var array
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::amend_customize_save_response()
|
||||
*/
|
||||
protected $_widget_nav_menu_updates = array();
|
||||
|
||||
/**
|
||||
* Create/update the nav_menu term for this setting.
|
||||
*
|
||||
* Any created menus will have their assigned term IDs exported to the client
|
||||
* via the customize_save_response filter. Likewise, any errors will be exported
|
||||
* to the client via the customize_save_response() filter.
|
||||
*
|
||||
* To delete a menu, the client can send false as the value.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*
|
||||
* @see wp_update_nav_menu_object()
|
||||
*
|
||||
* @param array|false $value {
|
||||
* The value to update. Note that slug cannot be updated via wp_update_nav_menu_object().
|
||||
* If false, then the menu will be deleted entirely.
|
||||
*
|
||||
* @type string $name The name of the menu to save.
|
||||
* @type string $description The term description. Default empty string.
|
||||
* @type int $parent The id of the parent term. Default 0.
|
||||
* @type bool $auto_add Whether pages will auto_add to this menu. Default false.
|
||||
* }
|
||||
* @return null|void
|
||||
*/
|
||||
protected function update( $value ) {
|
||||
if ( $this->is_updated ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->is_updated = true;
|
||||
$is_placeholder = ( $this->term_id < 0 );
|
||||
$is_delete = ( false === $value );
|
||||
|
||||
add_filter( 'customize_save_response', array( $this, 'amend_customize_save_response' ) );
|
||||
|
||||
$auto_add = null;
|
||||
if ( $is_delete ) {
|
||||
// If the current setting term is a placeholder, a delete request is a no-op.
|
||||
if ( $is_placeholder ) {
|
||||
$this->update_status = 'deleted';
|
||||
} else {
|
||||
$r = wp_delete_nav_menu( $this->term_id );
|
||||
|
||||
if ( is_wp_error( $r ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = $r;
|
||||
} else {
|
||||
$this->update_status = 'deleted';
|
||||
$auto_add = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Insert or update menu.
|
||||
$menu_data = wp_array_slice_assoc( $value, array( 'description', 'parent' ) );
|
||||
$menu_data['menu-name'] = $value['name'];
|
||||
|
||||
$menu_id = $is_placeholder ? 0 : $this->term_id;
|
||||
$r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) );
|
||||
$original_name = $menu_data['menu-name'];
|
||||
$name_conflict_suffix = 1;
|
||||
while ( is_wp_error( $r ) && 'menu_exists' === $r->get_error_code() ) {
|
||||
$name_conflict_suffix += 1;
|
||||
/* translators: 1: original menu name, 2: duplicate count */
|
||||
$menu_data['menu-name'] = sprintf( __( '%1$s (%2$d)' ), $original_name, $name_conflict_suffix );
|
||||
$r = wp_update_nav_menu_object( $menu_id, wp_slash( $menu_data ) );
|
||||
}
|
||||
|
||||
if ( is_wp_error( $r ) ) {
|
||||
$this->update_status = 'error';
|
||||
$this->update_error = $r;
|
||||
} else {
|
||||
if ( $is_placeholder ) {
|
||||
$this->previous_term_id = $this->term_id;
|
||||
$this->term_id = $r;
|
||||
$this->update_status = 'inserted';
|
||||
} else {
|
||||
$this->update_status = 'updated';
|
||||
}
|
||||
|
||||
$auto_add = $value['auto_add'];
|
||||
}
|
||||
}
|
||||
|
||||
if ( null !== $auto_add ) {
|
||||
$nav_menu_options = $this->filter_nav_menu_options_value(
|
||||
(array) get_option( 'nav_menu_options', array() ),
|
||||
$this->term_id,
|
||||
$auto_add
|
||||
);
|
||||
update_option( 'nav_menu_options', $nav_menu_options );
|
||||
}
|
||||
|
||||
if ( 'inserted' === $this->update_status ) {
|
||||
// Make sure that new menus assigned to nav menu locations use their new IDs.
|
||||
foreach ( $this->manager->settings() as $setting ) {
|
||||
if ( ! preg_match( '/^nav_menu_locations\[/', $setting->id ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$post_value = $setting->post_value( null );
|
||||
if ( ! is_null( $post_value ) && $this->previous_term_id === intval( $post_value ) ) {
|
||||
$this->manager->set_post_value( $setting->id, $this->term_id );
|
||||
$setting->save();
|
||||
}
|
||||
}
|
||||
|
||||
// Make sure that any nav_menu widgets referencing the placeholder nav menu get updated and sent back to client.
|
||||
foreach ( array_keys( $this->manager->unsanitized_post_values() ) as $setting_id ) {
|
||||
$nav_menu_widget_setting = $this->manager->get_setting( $setting_id );
|
||||
if ( ! $nav_menu_widget_setting || ! preg_match( '/^widget_nav_menu\[/', $nav_menu_widget_setting->id ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$widget_instance = $nav_menu_widget_setting->post_value(); // Note that this calls WP_Customize_Widgets::sanitize_widget_instance().
|
||||
if ( empty( $widget_instance['nav_menu'] ) || intval( $widget_instance['nav_menu'] ) !== $this->previous_term_id ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$widget_instance['nav_menu'] = $this->term_id;
|
||||
$updated_widget_instance = $this->manager->widgets->sanitize_widget_js_instance( $widget_instance );
|
||||
$this->manager->set_post_value( $nav_menu_widget_setting->id, $updated_widget_instance );
|
||||
$nav_menu_widget_setting->save();
|
||||
|
||||
$this->_widget_nav_menu_updates[ $nav_menu_widget_setting->id ] = $updated_widget_instance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a nav_menu_options array.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::filter_nav_menu_options()
|
||||
* @see WP_Customize_Nav_Menu_Setting::update()
|
||||
*
|
||||
* @param array $nav_menu_options Array as returned by get_option( 'nav_menu_options' ).
|
||||
* @param int $menu_id The term ID for the given menu.
|
||||
* @param bool $auto_add Whether to auto-add or not.
|
||||
* @return array (Maybe) modified nav_menu_otions array.
|
||||
*/
|
||||
protected function filter_nav_menu_options_value( $nav_menu_options, $menu_id, $auto_add ) {
|
||||
$nav_menu_options = (array) $nav_menu_options;
|
||||
if ( ! isset( $nav_menu_options['auto_add'] ) ) {
|
||||
$nav_menu_options['auto_add'] = array();
|
||||
}
|
||||
|
||||
$i = array_search( $menu_id, $nav_menu_options['auto_add'] );
|
||||
if ( $auto_add && false === $i ) {
|
||||
array_push( $nav_menu_options['auto_add'], $this->term_id );
|
||||
} elseif ( ! $auto_add && false !== $i ) {
|
||||
array_splice( $nav_menu_options['auto_add'], $i, 1 );
|
||||
}
|
||||
|
||||
return $nav_menu_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Export data for the JS client.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Nav_Menu_Setting::update()
|
||||
*
|
||||
* @param array $data Additional information passed back to the 'saved' event on `wp.customize`.
|
||||
* @return array Export data.
|
||||
*/
|
||||
public function amend_customize_save_response( $data ) {
|
||||
if ( ! isset( $data['nav_menu_updates'] ) ) {
|
||||
$data['nav_menu_updates'] = array();
|
||||
}
|
||||
if ( ! isset( $data['widget_nav_menu_updates'] ) ) {
|
||||
$data['widget_nav_menu_updates'] = array();
|
||||
}
|
||||
|
||||
$data['nav_menu_updates'][] = array(
|
||||
'term_id' => $this->term_id,
|
||||
'previous_term_id' => $this->previous_term_id,
|
||||
'error' => $this->update_error ? $this->update_error->get_error_code() : null,
|
||||
'status' => $this->update_status,
|
||||
'saved_value' => 'deleted' === $this->update_status ? null : $this->value(),
|
||||
);
|
||||
|
||||
$data['widget_nav_menu_updates'] = array_merge(
|
||||
$data['widget_nav_menu_updates'],
|
||||
$this->_widget_nav_menu_updates
|
||||
);
|
||||
$this->_widget_nav_menu_updates = array();
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Nav_Menus_Panel class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Nav Menus Panel Class
|
||||
*
|
||||
* Needed to add screen options.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Panel
|
||||
*/
|
||||
class WP_Customize_Nav_Menus_Panel extends WP_Customize_Panel {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'nav_menus';
|
||||
|
||||
/**
|
||||
* Render screen options for Menus.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_screen_options() {
|
||||
// Adds the screen options.
|
||||
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
|
||||
add_filter( 'manage_nav-menus_columns', 'wp_nav_menu_manage_columns' );
|
||||
|
||||
// Display screen options.
|
||||
$screen = WP_Screen::get( 'nav-menus.php' );
|
||||
$screen->render_screen_options( array( 'wrap' => false ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the advanced options for the nav menus page.
|
||||
*
|
||||
* Link title attribute added as it's a relatively advanced concept for new users.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @deprecated 4.5.0 Deprecated in favor of wp_nav_menu_manage_columns().
|
||||
*/
|
||||
public function wp_nav_menu_manage_columns() {
|
||||
_deprecated_function( __METHOD__, '4.5.0', 'wp_nav_menu_manage_columns' );
|
||||
require_once ABSPATH . 'wp-admin/includes/nav-menu.php';
|
||||
return wp_nav_menu_manage_columns();
|
||||
}
|
||||
|
||||
/**
|
||||
* An Underscore (JS) template for this panel's content (but not its container).
|
||||
*
|
||||
* Class variables for this panel class are available in the `data` JS object;
|
||||
* export custom variables by overriding WP_Customize_Panel::json().
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*
|
||||
* @see WP_Customize_Panel::print_template()
|
||||
*/
|
||||
protected function content_template() {
|
||||
?>
|
||||
<li class="panel-meta customize-info accordion-section <# if ( ! data.description ) { #> cannot-expand<# } #>">
|
||||
<button type="button" class="customize-panel-back" tabindex="-1">
|
||||
<span class="screen-reader-text"><?php _e( 'Back' ); ?></span>
|
||||
</button>
|
||||
<div class="accordion-section-title">
|
||||
<span class="preview-notice">
|
||||
<?php
|
||||
/* translators: %s: the site/panel title in the Customizer */
|
||||
printf( __( 'You are customizing %s' ), '<strong class="panel-title">{{ data.title }}</strong>' );
|
||||
?>
|
||||
</span>
|
||||
<button type="button" class="customize-help-toggle dashicons dashicons-editor-help" aria-expanded="false">
|
||||
<span class="screen-reader-text"><?php _e( 'Help' ); ?></span>
|
||||
</button>
|
||||
<button type="button" class="customize-screen-options-toggle" aria-expanded="false">
|
||||
<span class="screen-reader-text"><?php _e( 'Menu Options' ); ?></span>
|
||||
</button>
|
||||
</div>
|
||||
<# if ( data.description ) { #>
|
||||
<div class="description customize-panel-description">{{{ data.description }}}</div>
|
||||
<# } #>
|
||||
<div id="screen-options-wrap">
|
||||
<?php $this->render_screen_options(); ?>
|
||||
</div>
|
||||
</li>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_New_Menu_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize control class for new menus.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_New_Menu_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'new_menu';
|
||||
|
||||
/**
|
||||
* Render the control's content.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {
|
||||
?>
|
||||
<button type="button" class="button button-primary" id="create-new-menu-submit"><?php _e( 'Create Menu' ); ?></button>
|
||||
<span class="spinner"></span>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_New_Menu_Section class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Menu Section Class
|
||||
*
|
||||
* Implements the new-menu-ui toggle button instead of a regular section.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Section
|
||||
*/
|
||||
class WP_Customize_New_Menu_Section extends WP_Customize_Section {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'new_menu';
|
||||
|
||||
/**
|
||||
* Render the section, and the controls that have been added to it.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function render() {
|
||||
?>
|
||||
<li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="accordion-section-new-menu">
|
||||
<button type="button" class="button-secondary add-new-menu-item add-menu-toggle" aria-expanded="false">
|
||||
<?php echo esc_html( $this->title ); ?>
|
||||
</button>
|
||||
<ul class="new-menu-section-content"></ul>
|
||||
</li>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Partial class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core Customizer class for implementing selective refresh partials.
|
||||
*
|
||||
* Representation of a rendered region in the previewed page that gets
|
||||
* selectively refreshed when an associated setting is changed.
|
||||
* This class is analogous of WP_Customize_Control.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*/
|
||||
class WP_Customize_Partial {
|
||||
|
||||
/**
|
||||
* Component.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var WP_Customize_Selective_Refresh
|
||||
*/
|
||||
public $component;
|
||||
|
||||
/**
|
||||
* Unique identifier for the partial.
|
||||
*
|
||||
* If the partial is used to display a single setting, this would generally
|
||||
* be the same as the associated setting's ID.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* Parsed ID.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access private
|
||||
* @var array {
|
||||
* @type string $base ID base.
|
||||
* @type array $keys Keys for multidimensional.
|
||||
* }
|
||||
*/
|
||||
protected $id_data = array();
|
||||
|
||||
/**
|
||||
* Type of this partial.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'default';
|
||||
|
||||
/**
|
||||
* The jQuery selector to find the container element for the partial.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $selector;
|
||||
|
||||
/**
|
||||
* IDs for settings tied to the partial.
|
||||
*
|
||||
* @access public
|
||||
* @since 4.5.0
|
||||
* @var array
|
||||
*/
|
||||
public $settings;
|
||||
|
||||
/**
|
||||
* The ID for the setting that this partial is primarily responsible for rendering.
|
||||
*
|
||||
* If not supplied, it will default to the ID of the first setting.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $primary_setting;
|
||||
|
||||
/**
|
||||
* Capability required to edit this partial.
|
||||
*
|
||||
* Normally this is empty and the capability is derived from the capabilities
|
||||
* of the associated `$settings`.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $capability;
|
||||
|
||||
/**
|
||||
* Render callback.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @see WP_Customize_Partial::render()
|
||||
* @var callable Callback is called with one argument, the instance of
|
||||
* WP_Customize_Partial. The callback can either echo the
|
||||
* partial or return the partial as a string, or return false if error.
|
||||
*/
|
||||
public $render_callback;
|
||||
|
||||
/**
|
||||
* Whether the container element is included in the partial, or if only the contents are rendered.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var bool
|
||||
*/
|
||||
public $container_inclusive = false;
|
||||
|
||||
/**
|
||||
* Whether to refresh the entire preview in case a partial cannot be refreshed.
|
||||
*
|
||||
* A partial render is considered a failure if the render_callback returns false.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var bool
|
||||
*/
|
||||
public $fallback_refresh = true;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* Supplied `$args` override class property defaults.
|
||||
*
|
||||
* If `$args['settings']` is not defined, use the $id as the setting ID.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Selective_Refresh $component Customize Partial Refresh plugin instance.
|
||||
* @param string $id Control ID.
|
||||
* @param array $args {
|
||||
* Optional. Arguments to override class property defaults.
|
||||
*
|
||||
* @type array|string $settings All settings IDs tied to the partial. If undefined, `$id` will be used.
|
||||
* }
|
||||
*/
|
||||
public function __construct( WP_Customize_Selective_Refresh $component, $id, $args = array() ) {
|
||||
$keys = array_keys( get_object_vars( $this ) );
|
||||
foreach ( $keys as $key ) {
|
||||
if ( isset( $args[ $key ] ) ) {
|
||||
$this->$key = $args[ $key ];
|
||||
}
|
||||
}
|
||||
|
||||
$this->component = $component;
|
||||
$this->id = $id;
|
||||
$this->id_data['keys'] = preg_split( '/\[/', str_replace( ']', '', $this->id ) );
|
||||
$this->id_data['base'] = array_shift( $this->id_data['keys'] );
|
||||
|
||||
if ( empty( $this->render_callback ) ) {
|
||||
$this->render_callback = array( $this, 'render_callback' );
|
||||
}
|
||||
|
||||
// Process settings.
|
||||
if ( ! isset( $this->settings ) ) {
|
||||
$this->settings = array( $id );
|
||||
} else if ( is_string( $this->settings ) ) {
|
||||
$this->settings = array( $this->settings );
|
||||
}
|
||||
|
||||
if ( empty( $this->primary_setting ) ) {
|
||||
$this->primary_setting = current( $this->settings );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves parsed ID data for multidimensional setting.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @return array {
|
||||
* ID data for multidimensional partial.
|
||||
*
|
||||
* @type string $base ID base.
|
||||
* @type array $keys Keys for multidimensional array.
|
||||
* }
|
||||
*/
|
||||
final public function id_data() {
|
||||
return $this->id_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the template partial involving the associated settings.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param array $container_context Optional. Array of context data associated with the target container (placement).
|
||||
* Default empty array.
|
||||
* @return string|array|false The rendered partial as a string, raw data array (for client-side JS template),
|
||||
* or false if no render applied.
|
||||
*/
|
||||
final public function render( $container_context = array() ) {
|
||||
$partial = $this;
|
||||
$rendered = false;
|
||||
|
||||
if ( ! empty( $this->render_callback ) ) {
|
||||
ob_start();
|
||||
$return_render = call_user_func( $this->render_callback, $this, $container_context );
|
||||
$ob_render = ob_get_clean();
|
||||
|
||||
if ( null !== $return_render && '' !== $ob_render ) {
|
||||
_doing_it_wrong( __FUNCTION__, __( 'Partial render must echo the content or return the content string (or array), but not both.' ), '4.5.0' );
|
||||
}
|
||||
|
||||
/*
|
||||
* Note that the string return takes precedence because the $ob_render may just\
|
||||
* include PHP warnings or notices.
|
||||
*/
|
||||
$rendered = null !== $return_render ? $return_render : $ob_render;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters partial rendering.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param string|array|false $rendered The partial value. Default false.
|
||||
* @param WP_Customize_Partial $partial WP_Customize_Setting instance.
|
||||
* @param array $container_context Optional array of context data associated with
|
||||
* the target container.
|
||||
*/
|
||||
$rendered = apply_filters( 'customize_partial_render', $rendered, $partial, $container_context );
|
||||
|
||||
/**
|
||||
* Filters partial rendering for a specific partial.
|
||||
*
|
||||
* The dynamic portion of the hook name, `$partial->ID` refers to the partial ID.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param string|array|false $rendered The partial value. Default false.
|
||||
* @param WP_Customize_Partial $partial WP_Customize_Setting instance.
|
||||
* @param array $container_context Optional array of context data associated with
|
||||
* the target container.
|
||||
*/
|
||||
$rendered = apply_filters( "customize_partial_render_{$partial->id}", $rendered, $partial, $container_context );
|
||||
|
||||
return $rendered;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default callback used when invoking WP_Customize_Control::render().
|
||||
*
|
||||
* Note that this method may echo the partial *or* return the partial as
|
||||
* a string or array, but not both. Output buffering is performed when this
|
||||
* is called. Subclasses can override this with their specific logic, or they
|
||||
* may provide an 'render_callback' argument to the constructor.
|
||||
*
|
||||
* This method may return an HTML string for straight DOM injection, or it
|
||||
* may return an array for supporting Partial JS subclasses to render by
|
||||
* applying to client-side templating.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Partial $partial Partial.
|
||||
* @param array $context Context.
|
||||
* @return string|array|false
|
||||
*/
|
||||
public function render_callback( WP_Customize_Partial $partial, $context = array() ) {
|
||||
unset( $partial, $context );
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the data to export to the client via JSON.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Array of parameters passed to the JavaScript.
|
||||
*/
|
||||
public function json() {
|
||||
$exports = array(
|
||||
'settings' => $this->settings,
|
||||
'primarySetting' => $this->primary_setting,
|
||||
'selector' => $this->selector,
|
||||
'type' => $this->type,
|
||||
'fallbackRefresh' => $this->fallback_refresh,
|
||||
'containerInclusive' => $this->container_inclusive,
|
||||
);
|
||||
return $exports;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the user can refresh this partial.
|
||||
*
|
||||
* Returns false if the user cannot manipulate one of the associated settings,
|
||||
* or if one of the associated settings does not exist.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @return bool False if user can't edit one one of the related settings,
|
||||
* or if one of the associated settings does not exist.
|
||||
*/
|
||||
final public function check_capabilities() {
|
||||
if ( ! empty( $this->capability ) && ! current_user_can( $this->capability ) ) {
|
||||
return false;
|
||||
}
|
||||
foreach ( $this->settings as $setting_id ) {
|
||||
$setting = $this->component->manager->get_setting( $setting_id );
|
||||
if ( ! $setting || ! $setting->check_capabilities() ) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,439 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Selective_Refresh class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.5.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core Customizer class for implementing selective refresh.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*/
|
||||
final class WP_Customize_Selective_Refresh {
|
||||
|
||||
/**
|
||||
* Query var used in requests to render partials.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*/
|
||||
const RENDER_QUERY_VAR = 'wp_customize_render_partials';
|
||||
|
||||
/**
|
||||
* Customize manager.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
* @var WP_Customize_Manager
|
||||
*/
|
||||
public $manager;
|
||||
|
||||
/**
|
||||
* Registered instances of WP_Customize_Partial.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access protected
|
||||
* @var WP_Customize_Partial[]
|
||||
*/
|
||||
protected $partials = array();
|
||||
|
||||
/**
|
||||
* Log of errors triggered when partials are rendered.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
protected $triggered_errors = array();
|
||||
|
||||
/**
|
||||
* Keep track of the current partial being rendered.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access private
|
||||
* @var string
|
||||
*/
|
||||
protected $current_partial_id;
|
||||
|
||||
/**
|
||||
* Plugin bootstrap for Partial Refresh functionality.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Manager instance.
|
||||
*/
|
||||
public function __construct( WP_Customize_Manager $manager ) {
|
||||
$this->manager = $manager;
|
||||
require_once( ABSPATH . WPINC . '/customize/class-wp-customize-partial.php' );
|
||||
|
||||
add_action( 'customize_preview_init', array( $this, 'init_preview' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the registered partials.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @return array Partials.
|
||||
*/
|
||||
public function partials() {
|
||||
return $this->partials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a partial.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Partial|string $id Customize Partial object, or Panel ID.
|
||||
* @param array $args Optional. Partial arguments. Default empty array.
|
||||
* @return WP_Customize_Partial The instance of the panel that was added.
|
||||
*/
|
||||
public function add_partial( $id, $args = array() ) {
|
||||
if ( $id instanceof WP_Customize_Partial ) {
|
||||
$partial = $id;
|
||||
} else {
|
||||
$class = 'WP_Customize_Partial';
|
||||
|
||||
/** This filter (will be) documented in wp-includes/class-wp-customize-manager.php */
|
||||
$args = apply_filters( 'customize_dynamic_partial_args', $args, $id );
|
||||
|
||||
/** This filter (will be) documented in wp-includes/class-wp-customize-manager.php */
|
||||
$class = apply_filters( 'customize_dynamic_partial_class', $class, $id, $args );
|
||||
|
||||
$partial = new $class( $this, $id, $args );
|
||||
}
|
||||
|
||||
$this->partials[ $partial->id ] = $partial;
|
||||
return $partial;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a partial.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $id Customize Partial ID.
|
||||
* @return WP_Customize_Partial|null The partial, if set. Otherwise null.
|
||||
*/
|
||||
public function get_partial( $id ) {
|
||||
if ( isset( $this->partials[ $id ] ) ) {
|
||||
return $this->partials[ $id ];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a partial.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @param string $id Customize Partial ID.
|
||||
*/
|
||||
public function remove_partial( $id ) {
|
||||
unset( $this->partials[ $id ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the Customizer preview.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*/
|
||||
public function init_preview() {
|
||||
add_action( 'template_redirect', array( $this, 'handle_render_partials_request' ) );
|
||||
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_preview_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues preview scripts.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*/
|
||||
public function enqueue_preview_scripts() {
|
||||
wp_enqueue_script( 'customize-selective-refresh' );
|
||||
add_action( 'wp_footer', array( $this, 'export_preview_data' ), 1000 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Exports data in preview after it has finished rendering so that partials can be added at runtime.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*/
|
||||
public function export_preview_data() {
|
||||
$partials = array();
|
||||
|
||||
foreach ( $this->partials() as $partial ) {
|
||||
if ( $partial->check_capabilities() ) {
|
||||
$partials[ $partial->id ] = $partial->json();
|
||||
}
|
||||
}
|
||||
|
||||
$exports = array(
|
||||
'partials' => $partials,
|
||||
'renderQueryVar' => self::RENDER_QUERY_VAR,
|
||||
'l10n' => array(
|
||||
'shiftClickToEdit' => __( 'Shift-click to edit this element.' ),
|
||||
/* translators: %s: document.write() */
|
||||
'badDocumentWrite' => sprintf( __( '%s is forbidden' ), 'document.write()' ),
|
||||
),
|
||||
);
|
||||
|
||||
// Export data to JS.
|
||||
echo sprintf( '<script>var _customizePartialRefreshExports = %s;</script>', wp_json_encode( $exports ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers dynamically-created partials.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Manager::add_dynamic_settings()
|
||||
*
|
||||
* @param array $partial_ids The partial ID to add.
|
||||
* @return array Added WP_Customize_Partial instances.
|
||||
*/
|
||||
public function add_dynamic_partials( $partial_ids ) {
|
||||
$new_partials = array();
|
||||
|
||||
foreach ( $partial_ids as $partial_id ) {
|
||||
|
||||
// Skip partials already created.
|
||||
$partial = $this->get_partial( $partial_id );
|
||||
if ( $partial ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$partial_args = false;
|
||||
$partial_class = 'WP_Customize_Partial';
|
||||
|
||||
/**
|
||||
* Filters a dynamic partial's constructor arguments.
|
||||
*
|
||||
* For a dynamic partial to be registered, this filter must be employed
|
||||
* to override the default false value with an array of args to pass to
|
||||
* the WP_Customize_Partial constructor.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param false|array $partial_args The arguments to the WP_Customize_Partial constructor.
|
||||
* @param string $partial_id ID for dynamic partial.
|
||||
*/
|
||||
$partial_args = apply_filters( 'customize_dynamic_partial_args', $partial_args, $partial_id );
|
||||
if ( false === $partial_args ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the class used to construct partials.
|
||||
*
|
||||
* Allow non-statically created partials to be constructed with custom WP_Customize_Partial subclass.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param string $partial_class WP_Customize_Partial or a subclass.
|
||||
* @param string $partial_id ID for dynamic partial.
|
||||
* @param array $partial_args The arguments to the WP_Customize_Partial constructor.
|
||||
*/
|
||||
$partial_class = apply_filters( 'customize_dynamic_partial_class', $partial_class, $partial_id, $partial_args );
|
||||
|
||||
$partial = new $partial_class( $this, $partial_id, $partial_args );
|
||||
|
||||
$this->add_partial( $partial );
|
||||
$new_partials[] = $partial;
|
||||
}
|
||||
return $new_partials;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the request is for rendering partials.
|
||||
*
|
||||
* Note that this will not consider whether the request is authorized or valid,
|
||||
* just that essentially the route is a match.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*
|
||||
* @return bool Whether the request is for rendering partials.
|
||||
*/
|
||||
public function is_render_partials_request() {
|
||||
return ! empty( $_POST[ self::RENDER_QUERY_VAR ] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles PHP errors triggered during rendering the partials.
|
||||
*
|
||||
* These errors will be relayed back to the client in the Ajax response.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access private
|
||||
*
|
||||
* @param int $errno Error number.
|
||||
* @param string $errstr Error string.
|
||||
* @param string $errfile Error file.
|
||||
* @param string $errline Error line.
|
||||
* @return true Always true.
|
||||
*/
|
||||
public function handle_error( $errno, $errstr, $errfile = null, $errline = null ) {
|
||||
$this->triggered_errors[] = array(
|
||||
'partial' => $this->current_partial_id,
|
||||
'error_number' => $errno,
|
||||
'error_string' => $errstr,
|
||||
'error_file' => $errfile,
|
||||
'error_line' => $errline,
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the Ajax request to return the rendered partials for the requested placements.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*/
|
||||
public function handle_render_partials_request() {
|
||||
if ( ! $this->is_render_partials_request() ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->manager->remove_preview_signature();
|
||||
|
||||
/*
|
||||
* Note that is_customize_preview() returning true will entail that the
|
||||
* user passed the 'customize' capability check and the nonce check, since
|
||||
* WP_Customize_Manager::setup_theme() is where the previewing flag is set.
|
||||
*/
|
||||
if ( ! is_customize_preview() ) {
|
||||
status_header( 403 );
|
||||
wp_send_json_error( 'expected_customize_preview' );
|
||||
} else if ( ! isset( $_POST['partials'] ) ) {
|
||||
status_header( 400 );
|
||||
wp_send_json_error( 'missing_partials' );
|
||||
}
|
||||
|
||||
$partials = json_decode( wp_unslash( $_POST['partials'] ), true );
|
||||
|
||||
if ( ! is_array( $partials ) ) {
|
||||
wp_send_json_error( 'malformed_partials' );
|
||||
}
|
||||
|
||||
$this->add_dynamic_partials( array_keys( $partials ) );
|
||||
|
||||
/**
|
||||
* Fires immediately before partials are rendered.
|
||||
*
|
||||
* Plugins may do things like call wp_enqueue_scripts() and gather a list of the scripts
|
||||
* and styles which may get enqueued in the response.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param WP_Customize_Selective_Refresh $this Selective refresh component.
|
||||
* @param array $partials Placements' context data for the partials rendered in the request.
|
||||
* The array is keyed by partial ID, with each item being an array of
|
||||
* the placements' context data.
|
||||
*/
|
||||
do_action( 'customize_render_partials_before', $this, $partials );
|
||||
|
||||
set_error_handler( array( $this, 'handle_error' ), error_reporting() );
|
||||
|
||||
$contents = array();
|
||||
|
||||
foreach ( $partials as $partial_id => $container_contexts ) {
|
||||
$this->current_partial_id = $partial_id;
|
||||
|
||||
if ( ! is_array( $container_contexts ) ) {
|
||||
wp_send_json_error( 'malformed_container_contexts' );
|
||||
}
|
||||
|
||||
$partial = $this->get_partial( $partial_id );
|
||||
|
||||
if ( ! $partial || ! $partial->check_capabilities() ) {
|
||||
$contents[ $partial_id ] = null;
|
||||
continue;
|
||||
}
|
||||
|
||||
$contents[ $partial_id ] = array();
|
||||
|
||||
// @todo The array should include not only the contents, but also whether the container is included?
|
||||
if ( empty( $container_contexts ) ) {
|
||||
// Since there are no container contexts, render just once.
|
||||
$contents[ $partial_id ][] = $partial->render( null );
|
||||
} else {
|
||||
foreach ( $container_contexts as $container_context ) {
|
||||
$contents[ $partial_id ][] = $partial->render( $container_context );
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->current_partial_id = null;
|
||||
|
||||
restore_error_handler();
|
||||
|
||||
/**
|
||||
* Fires immediately after partials are rendered.
|
||||
*
|
||||
* Plugins may do things like call wp_footer() to scrape scripts output and return them
|
||||
* via the {@see 'customize_render_partials_response'} filter.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param WP_Customize_Selective_Refresh $this Selective refresh component.
|
||||
* @param array $partials Placements' context data for the partials rendered in the request.
|
||||
* The array is keyed by partial ID, with each item being an array of
|
||||
* the placements' context data.
|
||||
*/
|
||||
do_action( 'customize_render_partials_after', $this, $partials );
|
||||
|
||||
$response = array(
|
||||
'contents' => $contents,
|
||||
);
|
||||
|
||||
if ( defined( 'WP_DEBUG_DISPLAY' ) && WP_DEBUG_DISPLAY ) {
|
||||
$response['errors'] = $this->triggered_errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the response from rendering the partials.
|
||||
*
|
||||
* Plugins may use this filter to inject `$scripts` and `$styles`, which are dependencies
|
||||
* for the partials being rendered. The response data will be available to the client via
|
||||
* the `render-partials-response` JS event, so the client can then inject the scripts and
|
||||
* styles into the DOM if they have not already been enqueued there.
|
||||
*
|
||||
* If plugins do this, they'll need to take care for any scripts that do `document.write()`
|
||||
* and make sure that these are not injected, or else to override the function to no-op,
|
||||
* or else the page will be destroyed.
|
||||
*
|
||||
* Plugins should be aware that `$scripts` and `$styles` may eventually be included by
|
||||
* default in the response.
|
||||
*
|
||||
* @since 4.5.0
|
||||
*
|
||||
* @param array $response {
|
||||
* Response.
|
||||
*
|
||||
* @type array $contents Associative array mapping a partial ID its corresponding array of contents
|
||||
* for the containers requested.
|
||||
* @type array $errors List of errors triggered during rendering of partials, if `WP_DEBUG_DISPLAY`
|
||||
* is enabled.
|
||||
* }
|
||||
* @param WP_Customize_Selective_Refresh $this Selective refresh component.
|
||||
* @param array $partials Placements' context data for the partials rendered in the request.
|
||||
* The array is keyed by partial ID, with each item being an array of
|
||||
* the placements' context data.
|
||||
*/
|
||||
$response = apply_filters( 'customize_render_partials_response', $response, $this, $partials );
|
||||
|
||||
wp_send_json_success( $response );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Sidebar_Section class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customizer section representing widget area (sidebar).
|
||||
*
|
||||
* @since 4.1.0
|
||||
*
|
||||
* @see WP_Customize_Section
|
||||
*/
|
||||
class WP_Customize_Sidebar_Section extends WP_Customize_Section {
|
||||
|
||||
/**
|
||||
* Type of this section.
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'sidebar';
|
||||
|
||||
/**
|
||||
* Unique identifier.
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $sidebar_id;
|
||||
|
||||
/**
|
||||
* Gather the parameters passed to client JavaScript via JSON.
|
||||
*
|
||||
* @since 4.1.0
|
||||
*
|
||||
* @return array The array to be exported to the client as JSON.
|
||||
*/
|
||||
public function json() {
|
||||
$json = parent::json();
|
||||
$json['sidebarId'] = $this->sidebar_id;
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the current sidebar is rendered on the page.
|
||||
*
|
||||
* @since 4.1.0
|
||||
* @access public
|
||||
*
|
||||
* @return bool Whether sidebar is rendered.
|
||||
*/
|
||||
public function active_callback() {
|
||||
return $this->manager->widgets->is_sidebar_rendered( $this->sidebar_id );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Site_Icon_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Site Icon control class.
|
||||
*
|
||||
* Used only for custom functionality in JavaScript.
|
||||
*
|
||||
* @since 4.3.0
|
||||
*
|
||||
* @see WP_Customize_Cropped_Image_Control
|
||||
*/
|
||||
class WP_Customize_Site_Icon_Control extends WP_Customize_Cropped_Image_Control {
|
||||
|
||||
/**
|
||||
* Control type.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'site_icon';
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @since 4.3.0
|
||||
* @access public
|
||||
*
|
||||
* @param WP_Customize_Manager $manager Customizer bootstrap instance.
|
||||
* @param string $id Control ID.
|
||||
* @param array $args Optional. Arguments to override class property defaults.
|
||||
*/
|
||||
public function __construct( $manager, $id, $args = array() ) {
|
||||
parent::__construct( $manager, $id, $args );
|
||||
add_action( 'customize_controls_print_styles', 'wp_site_icon', 99 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders a JS template for the content of the site icon control.
|
||||
*
|
||||
* @since 4.5.0
|
||||
* @access public
|
||||
*/
|
||||
public function content_template() {
|
||||
?>
|
||||
<label for="{{ data.settings['default'] }}-button">
|
||||
<# if ( data.label ) { #>
|
||||
<span class="customize-control-title">{{ data.label }}</span>
|
||||
<# } #>
|
||||
<# if ( data.description ) { #>
|
||||
<span class="description customize-control-description">{{{ data.description }}}</span>
|
||||
<# } #>
|
||||
</label>
|
||||
|
||||
<# if ( data.attachment && data.attachment.id ) { #>
|
||||
<# if ( data.attachment.sizes ) { #>
|
||||
<div class="current">
|
||||
<div class="container">
|
||||
<div class="site-icon-preview">
|
||||
<div class="favicon-preview">
|
||||
<img src="<?php echo esc_url( admin_url( 'images/browser.png' ) ); ?>" class="browser-preview" width="182" alt="<?php esc_attr_e( 'Browser interface preview' ); ?>" />
|
||||
|
||||
<div class="favicon">
|
||||
<img id="preview-favicon" src="{{ data.attachment.sizes.full.url }}" alt="<?php esc_attr_e( 'Preview as a browser icon' ); ?>"/>
|
||||
</div>
|
||||
<span class="browser-title"><?php bloginfo( 'name' ); ?></span>
|
||||
</div>
|
||||
<img class="app-icon-preview" src="{{ data.attachment.sizes.full.url }}" alt="<?php esc_attr_e( 'Preview as an app icon' ); ?>"/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<# } #>
|
||||
<div class="actions">
|
||||
<# if ( data.canUpload ) { #>
|
||||
<button type="button" class="button remove-button"><?php echo $this->button_labels['remove']; ?></button>
|
||||
<button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button"><?php echo $this->button_labels['change']; ?></button>
|
||||
<div style="clear:both"></div>
|
||||
<# } #>
|
||||
</div>
|
||||
<# } else { #>
|
||||
<div class="current">
|
||||
<div class="container">
|
||||
<div class="placeholder">
|
||||
<div class="inner">
|
||||
<span><?php echo $this->button_labels['placeholder']; ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="actions">
|
||||
<# if ( data.defaultAttachment ) { #>
|
||||
<button type="button" class="button default-button"><?php echo $this->button_labels['default']; ?></button>
|
||||
<# } #>
|
||||
<# if ( data.canUpload ) { #>
|
||||
<button type="button" class="button upload-button" id="{{ data.settings['default'] }}-button"><?php echo $this->button_labels['select']; ?></button>
|
||||
<# } #>
|
||||
<div style="clear:both"></div>
|
||||
</div>
|
||||
<# } #>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Theme_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Theme Control class.
|
||||
*
|
||||
* @since 4.2.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Customize_Theme_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Customize control type.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'theme';
|
||||
|
||||
/**
|
||||
* Theme object.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
* @var WP_Theme
|
||||
*/
|
||||
public $theme;
|
||||
|
||||
/**
|
||||
* Refresh the parameters passed to the JavaScript via JSON.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
*
|
||||
* @see WP_Customize_Control::to_json()
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
$this->json['theme'] = $this->theme;
|
||||
}
|
||||
|
||||
/**
|
||||
* Don't render the control content from PHP, as it's rendered via JS on load.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {}
|
||||
|
||||
/**
|
||||
* Render a JS template for theme display.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
*/
|
||||
public function content_template() {
|
||||
$current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] );
|
||||
$active_url = esc_url( remove_query_arg( 'theme', $current_url ) );
|
||||
$preview_url = esc_url( add_query_arg( 'theme', '__THEME__', $current_url ) ); // Token because esc_url() strips curly braces.
|
||||
$preview_url = str_replace( '__THEME__', '{{ data.theme.id }}', $preview_url );
|
||||
?>
|
||||
<# if ( data.theme.isActiveTheme ) { #>
|
||||
<div class="theme active" tabindex="0" data-preview-url="<?php echo esc_attr( $active_url ); ?>" aria-describedby="{{ data.theme.id }}-action {{ data.theme.id }}-name">
|
||||
<# } else { #>
|
||||
<div class="theme" tabindex="0" data-preview-url="<?php echo esc_attr( $preview_url ); ?>" aria-describedby="{{ data.theme.id }}-action {{ data.theme.id }}-name">
|
||||
<# } #>
|
||||
|
||||
<# if ( data.theme.screenshot[0] ) { #>
|
||||
<div class="theme-screenshot">
|
||||
<img data-src="{{ data.theme.screenshot[0] }}" alt="" />
|
||||
</div>
|
||||
<# } else { #>
|
||||
<div class="theme-screenshot blank"></div>
|
||||
<# } #>
|
||||
|
||||
<# if ( data.theme.isActiveTheme ) { #>
|
||||
<span class="more-details" id="{{ data.theme.id }}-action"><?php _e( 'Customize' ); ?></span>
|
||||
<# } else { #>
|
||||
<span class="more-details" id="{{ data.theme.id }}-action"><?php _e( 'Live Preview' ); ?></span>
|
||||
<# } #>
|
||||
|
||||
<div class="theme-author"><?php printf( __( 'By %s' ), '{{ data.theme.author }}' ); ?></div>
|
||||
|
||||
<# if ( data.theme.isActiveTheme ) { #>
|
||||
<h3 class="theme-name" id="{{ data.theme.id }}-name">
|
||||
<?php
|
||||
/* translators: %s: theme name */
|
||||
printf( __( '<span>Active:</span> %s' ), '{{{ data.theme.name }}}' );
|
||||
?>
|
||||
</h3>
|
||||
<# } else { #>
|
||||
<h3 class="theme-name" id="{{ data.theme.id }}-name">{{{ data.theme.name }}}</h3>
|
||||
<div class="theme-actions">
|
||||
<button type="button" class="button theme-details"><?php _e( 'Theme Details' ); ?></button>
|
||||
</div>
|
||||
<# } #>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Themes_Section class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Themes Section class.
|
||||
*
|
||||
* A UI container for theme controls, which behaves like a backwards Panel.
|
||||
*
|
||||
* @since 4.2.0
|
||||
*
|
||||
* @see WP_Customize_Section
|
||||
*/
|
||||
class WP_Customize_Themes_Section extends WP_Customize_Section {
|
||||
|
||||
/**
|
||||
* Customize section type.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'themes';
|
||||
|
||||
/**
|
||||
* Render the themes section, which behaves like a panel.
|
||||
*
|
||||
* @since 4.2.0
|
||||
* @access protected
|
||||
*/
|
||||
protected function render() {
|
||||
$classes = 'accordion-section control-section control-section-' . $this->type;
|
||||
?>
|
||||
<li id="accordion-section-<?php echo esc_attr( $this->id ); ?>" class="<?php echo esc_attr( $classes ); ?>">
|
||||
<h3 class="accordion-section-title">
|
||||
<?php
|
||||
if ( $this->manager->is_theme_active() ) {
|
||||
echo '<span class="customize-action">' . __( 'Active theme' ) . '</span> ' . $this->title;
|
||||
} else {
|
||||
echo '<span class="customize-action">' . __( 'Previewing theme' ) . '</span> ' . $this->title;
|
||||
}
|
||||
?>
|
||||
|
||||
<?php if ( count( $this->controls ) > 0 ) : ?>
|
||||
<button type="button" class="button change-theme" tabindex="0"><?php _ex( 'Change', 'theme' ); ?></button>
|
||||
<?php endif; ?>
|
||||
</h3>
|
||||
<div class="customize-themes-panel control-panel-content themes-php">
|
||||
<h3 class="accordion-section-title customize-section-title">
|
||||
<span class="customize-action"><?php _e( 'Customizing' ); ?></span>
|
||||
<?php _e( 'Themes' ); ?>
|
||||
<span class="title-count theme-count"><?php echo count( $this->controls ) + 1 /* Active theme */; ?></span>
|
||||
</h3>
|
||||
<h3 class="accordion-section-title customize-section-title">
|
||||
<?php
|
||||
if ( $this->manager->is_theme_active() ) {
|
||||
echo '<span class="customize-action">' . __( 'Active theme' ) . '</span> ' . $this->title;
|
||||
} else {
|
||||
echo '<span class="customize-action">' . __( 'Previewing theme' ) . '</span> ' . $this->title;
|
||||
}
|
||||
?>
|
||||
<button type="button" class="button customize-theme"><?php _e( 'Customize' ); ?></button>
|
||||
</h3>
|
||||
|
||||
<div class="theme-overlay" tabindex="0" role="dialog" aria-label="<?php esc_attr_e( 'Theme Details' ); ?>"></div>
|
||||
|
||||
<div id="customize-container"></div>
|
||||
<?php if ( count( $this->controls ) > 4 ) : ?>
|
||||
<p><label for="themes-filter">
|
||||
<span class="screen-reader-text"><?php _e( 'Search installed themes…' ); ?></span>
|
||||
<input type="text" id="themes-filter" placeholder="<?php esc_attr_e( 'Search installed themes…' ); ?>" />
|
||||
</label></p>
|
||||
<?php endif; ?>
|
||||
<div class="theme-browser rendered">
|
||||
<ul class="themes accordion-section-content">
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
<?php }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Customize_Upload_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Customize Upload Control Class.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @see WP_Customize_Media_Control
|
||||
*/
|
||||
class WP_Customize_Upload_Control extends WP_Customize_Media_Control {
|
||||
public $type = 'upload';
|
||||
public $mime_type = '';
|
||||
public $button_labels = array();
|
||||
public $removed = ''; // unused
|
||||
public $context; // unused
|
||||
public $extensions = array(); // unused
|
||||
|
||||
/**
|
||||
* Refresh the parameters passed to the JavaScript via JSON.
|
||||
*
|
||||
* @since 3.4.0
|
||||
*
|
||||
* @uses WP_Customize_Media_Control::to_json()
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
|
||||
$value = $this->value();
|
||||
if ( $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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Widget_Area_Customize_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 3.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Widget Area Customize Control class.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Widget_Area_Customize_Control extends WP_Customize_Control {
|
||||
|
||||
/**
|
||||
* Customize control type.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @access public
|
||||
* @var string
|
||||
*/
|
||||
public $type = 'sidebar_widgets';
|
||||
|
||||
/**
|
||||
* Sidebar ID.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @access public
|
||||
* @var int|string
|
||||
*/
|
||||
public $sidebar_id;
|
||||
|
||||
/**
|
||||
* Refreshes the parameters passed to the JavaScript via JSON.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @access public
|
||||
*/
|
||||
public function to_json() {
|
||||
parent::to_json();
|
||||
$exported_properties = array( 'sidebar_id' );
|
||||
foreach ( $exported_properties as $key ) {
|
||||
$this->json[ $key ] = $this->$key;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the control's content.
|
||||
*
|
||||
* @since 3.9.0
|
||||
* @access public
|
||||
*/
|
||||
public function render_content() {
|
||||
$id = 'reorder-widgets-desc-' . str_replace( array( '[', ']' ), array( '-', '' ), $this->id );
|
||||
?>
|
||||
<button type="button" class="button-secondary add-new-widget" aria-expanded="false" aria-controls="available-widgets">
|
||||
<?php _e( 'Add a Widget' ); ?>
|
||||
</button>
|
||||
<button type="button" class="button-link reorder-toggle" aria-label="<?php esc_attr_e( 'Reorder widgets' ); ?>" aria-describedby="<?php echo esc_attr( $id ); ?>">
|
||||
<span class="reorder"><?php _ex( 'Reorder', 'Reorder widgets in Customizer' ); ?></span>
|
||||
<span class="reorder-done"><?php _ex( 'Done', 'Cancel reordering widgets in Customizer' ); ?></span>
|
||||
</button>
|
||||
<p class="screen-reader-text" id="<?php echo esc_attr( $id ); ?>"><?php _e( 'When in reorder mode, additional controls to reorder widgets will be available in the widgets list above.' ); ?></p>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Customize API: WP_Widget_Form_Customize_Control class
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Customize
|
||||
* @since 4.4.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Widget Form Customize Control class.
|
||||
*
|
||||
* @since 3.9.0
|
||||
*
|
||||
* @see WP_Customize_Control
|
||||
*/
|
||||
class WP_Widget_Form_Customize_Control extends WP_Customize_Control {
|
||||
public $type = 'widget_form';
|
||||
public $widget_id;
|
||||
public $widget_id_base;
|
||||
public $sidebar_id;
|
||||
public $is_new = false;
|
||||
public $width;
|
||||
public $height;
|
||||
public $is_wide = false;
|
||||
|
||||
/**
|
||||
* Gather control params for exporting to JavaScript.
|
||||
*
|
||||
* @global array $wp_registered_widgets
|
||||
*/
|
||||
public function to_json() {
|
||||
global $wp_registered_widgets;
|
||||
|
||||
parent::to_json();
|
||||
$exported_properties = array( 'widget_id', 'widget_id_base', 'sidebar_id', 'width', 'height', 'is_wide' );
|
||||
foreach ( $exported_properties as $key ) {
|
||||
$this->json[ $key ] = $this->$key;
|
||||
}
|
||||
|
||||
// Get the widget_control and widget_content.
|
||||
require_once ABSPATH . '/wp-admin/includes/widgets.php';
|
||||
|
||||
$widget = $wp_registered_widgets[ $this->widget_id ];
|
||||
if ( ! isset( $widget['params'][0] ) ) {
|
||||
$widget['params'][0] = array();
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'widget_id' => $widget['id'],
|
||||
'widget_name' => $widget['name'],
|
||||
);
|
||||
|
||||
$args = wp_list_widget_controls_dynamic_sidebar( array( 0 => $args, 1 => $widget['params'][0] ) );
|
||||
$widget_control_parts = $this->manager->widgets->get_widget_control_parts( $args );
|
||||
|
||||
$this->json['widget_control'] = $widget_control_parts['control'];
|
||||
$this->json['widget_content'] = $widget_control_parts['content'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Override render_content to be no-op since content is exported via to_json for deferred embedding.
|
||||
*/
|
||||
public function render_content() {}
|
||||
|
||||
/**
|
||||
* Whether the current widget is rendered on the page.
|
||||
*
|
||||
* @since 4.0.0
|
||||
* @access public
|
||||
*
|
||||
* @return bool Whether the widget is rendered.
|
||||
*/
|
||||
public function active_callback() {
|
||||
return $this->manager->widgets->is_widget_rendered( $this->widget_id );
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user