Code is Poetry.

WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.


Built from https://develop.svn.wordpress.org/trunk@42343


git-svn-id: http://core.svn.wordpress.org/trunk@42172 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast
2017-11-30 23:11:00 +00:00
parent 62be7ad218
commit aaf99e6913
610 changed files with 55731 additions and 43711 deletions

View File

@@ -42,7 +42,7 @@ final class WP_Customize_Nav_Menus {
* @param object $manager An instance of the WP_Customize_Manager class.
*/
public function __construct( $manager ) {
$this->manager = $manager;
$this->manager = $manager;
$this->original_nav_menu_locations = get_nav_menu_locations();
// See https://github.com/xwp/wp-customize-snapshots/blob/962586659688a5b1fd9ae93618b7ce2d4e7a421c/php/class-customize-snapshot-manager.php#L469-L499
@@ -95,15 +95,15 @@ final class WP_Customize_Nav_Menus {
wp_die( -1 );
}
$all_items = array();
$all_items = array();
$item_types = array();
if ( isset( $_POST['item_types'] ) && is_array( $_POST['item_types'] ) ) {
$item_types = wp_unslash( $_POST['item_types'] );
} elseif ( isset( $_POST['type'] ) && isset( $_POST['object'] ) ) { // Back compat.
$item_types[] = array(
'type' => wp_unslash( $_POST['type'] ),
'type' => wp_unslash( $_POST['type'] ),
'object' => wp_unslash( $_POST['object'] ),
'page' => empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ),
'page' => empty( $_POST['page'] ) ? 0 : absint( $_POST['page'] ),
);
} else {
wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
@@ -113,10 +113,10 @@ final class WP_Customize_Nav_Menus {
if ( empty( $item_type['type'] ) || empty( $item_type['object'] ) ) {
wp_send_json_error( 'nav_menus_missing_type_or_object_parameter' );
}
$type = sanitize_key( $item_type['type'] );
$type = sanitize_key( $item_type['type'] );
$object = sanitize_key( $item_type['object'] );
$page = empty( $item_type['page'] ) ? 0 : absint( $item_type['page'] );
$items = $this->load_available_items_query( $type, $object, $page );
$page = empty( $item_type['page'] ) ? 0 : absint( $item_type['page'] );
$items = $this->load_available_items_query( $type, $object, $page );
if ( is_wp_error( $items ) ) {
wp_send_json_error( $items->get_error_code() );
}
@@ -179,13 +179,17 @@ final class WP_Customize_Nav_Menus {
}
}
$posts = array_merge( $posts, get_posts( array(
'numberposts' => 10,
'offset' => 10 * $page,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => $object,
) ) );
$posts = array_merge(
$posts, get_posts(
array(
'numberposts' => 10,
'offset' => 10 * $page,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => $object,
)
)
);
foreach ( $posts as $post ) {
$post_title = $post->post_title;
@@ -204,18 +208,20 @@ final class WP_Customize_Nav_Menus {
);
}
} elseif ( 'taxonomy' === $type ) {
$terms = get_terms( $object, array(
'child_of' => 0,
'exclude' => '',
'hide_empty' => false,
'hierarchical' => 1,
'include' => '',
'number' => 10,
'offset' => 10 * $page,
'order' => 'DESC',
'orderby' => 'count',
'pad_counts' => false,
) );
$terms = get_terms(
$object, array(
'child_of' => 0,
'exclude' => '',
'hide_empty' => false,
'hierarchical' => 1,
'include' => '',
'number' => 10,
'offset' => 10 * $page,
'order' => 'DESC',
'orderby' => 'count',
'pad_counts' => false,
)
);
if ( is_wp_error( $terms ) ) {
return $terms;
}
@@ -269,8 +275,13 @@ final class WP_Customize_Nav_Menus {
$p = 1;
}
$s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
$items = $this->search_available_items_query( array( 'pagenum' => $p, 's' => $s ) );
$s = sanitize_text_field( wp_unslash( $_POST['search'] ) );
$items = $this->search_available_items_query(
array(
'pagenum' => $p,
's' => $s,
)
);
if ( empty( $items ) ) {
wp_send_json_error( array( 'message' => __( 'No results found.' ) ) );
@@ -293,7 +304,7 @@ final class WP_Customize_Nav_Menus {
$items = array();
$post_type_objects = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
$query = array(
$query = array(
'post_type' => array_keys( $post_type_objects ),
'suppress_filters' => true,
'update_post_term_cache' => false,
@@ -314,20 +325,22 @@ final class WP_Customize_Nav_Menus {
// Prepend list of posts with nav_menus_created_posts search results on first page.
$nav_menus_created_posts_setting = $this->manager->get_setting( 'nav_menus_created_posts' );
if ( 1 === $args['pagenum'] && $nav_menus_created_posts_setting && count( $nav_menus_created_posts_setting ) > 0 ) {
$stub_post_query = new WP_Query( array_merge(
$query,
array(
'post_status' => 'auto-draft',
'post__in' => $nav_menus_created_posts_setting->value(),
'posts_per_page' => -1,
$stub_post_query = new WP_Query(
array_merge(
$query,
array(
'post_status' => 'auto-draft',
'post__in' => $nav_menus_created_posts_setting->value(),
'posts_per_page' => -1,
)
)
) );
$posts = array_merge( $posts, $stub_post_query->posts );
);
$posts = array_merge( $posts, $stub_post_query->posts );
}
// Query posts.
$get_posts = new WP_Query( $query );
$posts = array_merge( $posts, $get_posts->posts );
$posts = array_merge( $posts, $get_posts->posts );
// Create items for posts.
foreach ( $posts as $post ) {
@@ -349,11 +362,13 @@ final class WP_Customize_Nav_Menus {
// Query taxonomy terms.
$taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'names' );
$terms = get_terms( $taxonomies, array(
'name__like' => $args['s'],
'number' => 20,
'offset' => 20 * ($args['pagenum'] - 1),
) );
$terms = get_terms(
$taxonomies, array(
'name__like' => $args['s'],
'number' => 20,
'offset' => 20 * ( $args['pagenum'] - 1 ),
)
);
// Check if any taxonomies were found.
if ( ! empty( $terms ) ) {
@@ -405,9 +420,9 @@ final class WP_Customize_Nav_Menus {
// Pass data to JS.
$settings = array(
'allMenus' => wp_get_nav_menus(),
'itemTypes' => $this->available_item_types(),
'l10n' => array(
'allMenus' => wp_get_nav_menus(),
'itemTypes' => $this->available_item_types(),
'l10n' => array(
'untitled' => _x( '(no label)', 'missing menu item navigation label' ),
'unnamed' => _x( '(unnamed)', 'Missing menu name.' ),
'custom_label' => __( 'Custom Link' ),
@@ -440,9 +455,9 @@ final class WP_Customize_Nav_Menus {
'reorderLabelOn' => esc_attr__( 'Reorder menu items' ),
'reorderLabelOff' => esc_attr__( 'Close reorder mode' ),
),
'settingTransport' => 'postMessage',
'phpIntMax' => PHP_INT_MAX,
'defaultSettingValues' => array(
'settingTransport' => 'postMessage',
'phpIntMax' => PHP_INT_MAX,
'defaultSettingValues' => array(
'nav_menu' => $temp_nav_menu_setting->default,
'nav_menu_item' => $temp_nav_menu_item_setting->default,
),
@@ -455,21 +470,21 @@ final class WP_Customize_Nav_Menus {
// This is copied from nav-menus.php, and it has an unfortunate object name of `menus`.
$nav_menus_l10n = array(
'oneThemeLocationNoMenus' => null,
'moveUp' => __( 'Move up one' ),
'moveDown' => __( 'Move down one' ),
'moveToTop' => __( 'Move to the top' ),
'moveUp' => __( 'Move up one' ),
'moveDown' => __( 'Move down one' ),
'moveToTop' => __( 'Move to the top' ),
/* translators: %s: previous item name */
'moveUnder' => __( 'Move under %s' ),
'moveUnder' => __( 'Move under %s' ),
/* translators: %s: previous item name */
'moveOutFrom' => __( 'Move out from under %s' ),
'moveOutFrom' => __( 'Move out from under %s' ),
/* translators: %s: previous item name */
'under' => __( 'Under %s' ),
'under' => __( 'Under %s' ),
/* translators: %s: previous item name */
'outFrom' => __( 'Out from under %s' ),
'outFrom' => __( 'Out from under %s' ),
/* translators: 1: item name, 2: item position, 3: total number of items */
'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),
'menuFocus' => __( '%1$s. Menu item %2$d of %3$d.' ),
/* translators: 1: item name, 2: item position, 3: parent item name */
'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
'subMenuFocus' => __( '%1$s. Sub item number %2$d under %3$s.' ),
);
wp_localize_script( 'nav-menu', 'menus', $nav_menus_l10n );
}
@@ -561,12 +576,16 @@ final class WP_Customize_Nav_Menus {
} else {
$description .= '<p>' . __( 'Menus can be displayed in locations defined by your theme.' ) . '</p>';
}
$this->manager->add_panel( new WP_Customize_Nav_Menus_Panel( $this->manager, 'nav_menus', array(
'title' => __( 'Menus' ),
'description' => $description,
'priority' => 100,
// 'theme_supports' => 'menus|widgets', @todo allow multiple theme supports
) ) );
$this->manager->add_panel(
new WP_Customize_Nav_Menus_Panel(
$this->manager, 'nav_menus', array(
'title' => __( 'Menus' ),
'description' => $description,
'priority' => 100,
// 'theme_supports' => 'menus|widgets', @todo allow multiple theme supports
)
)
);
$menus = wp_get_nav_menus();
// Menu locations.
@@ -584,12 +603,14 @@ final class WP_Customize_Nav_Menus {
$description .= '<p>' . sprintf( __( 'If your theme has widget areas, you can also add menus there. Visit the <a href="%s">Widgets panel</a> and add a &#8220;Navigation Menu widget&#8221; to display a menu in a sidebar or footer.' ), "javascript:wp.customize.panel( 'widgets' ).focus();" ) . '</p>';
}
$this->manager->add_section( 'menu_locations', array(
'title' => 1 === $num_locations ? _x( 'View Location', 'menu locations' ) : _x( 'View All Locations', 'menu locations' ),
'panel' => 'nav_menus',
'priority' => 30,
'description' => $description,
) );
$this->manager->add_section(
'menu_locations', array(
'title' => 1 === $num_locations ? _x( 'View Location', 'menu locations' ) : _x( 'View All Locations', 'menu locations' ),
'panel' => 'nav_menus',
'priority' => 30,
'description' => $description,
)
);
$choices = array( '0' => __( '&mdash; Select &mdash;' ) );
foreach ( $menus as $menu ) {
@@ -618,13 +639,15 @@ final class WP_Customize_Nav_Menus {
remove_filter( "customize_sanitize_{$setting_id}", 'absint' );
add_filter( "customize_sanitize_{$setting_id}", array( $this, 'intval_base10' ) );
} else {
$this->manager->add_setting( $setting_id, array(
'sanitize_callback' => array( $this, 'intval_base10' ),
'theme_supports' => 'menus',
'type' => 'theme_mod',
'transport' => 'postMessage',
'default' => 0,
) );
$this->manager->add_setting(
$setting_id, array(
'sanitize_callback' => array( $this, 'intval_base10' ),
'theme_supports' => 'menus',
'type' => 'theme_mod',
'transport' => 'postMessage',
'default' => 0,
)
);
}
// Override the assigned nav menu location if mapped during previewed theme switch.
@@ -632,12 +655,16 @@ final class WP_Customize_Nav_Menus {
$this->manager->set_post_value( $setting_id, $mapped_nav_menu_locations[ $location ] );
}
$this->manager->add_control( new WP_Customize_Nav_Menu_Location_Control( $this->manager, $setting_id, array(
'label' => $description,
'location_id' => $location,
'section' => 'menu_locations',
'choices' => $choices,
) ) );
$this->manager->add_control(
new WP_Customize_Nav_Menu_Location_Control(
$this->manager, $setting_id, array(
'label' => $description,
'location_id' => $location,
'section' => 'menu_locations',
'choices' => $choices,
)
)
);
}
// Register each menu as a Customizer section, and add each menu item to each menu.
@@ -646,16 +673,24 @@ final class WP_Customize_Nav_Menus {
// Create a section for each menu.
$section_id = 'nav_menu[' . $menu_id . ']';
$this->manager->add_section( new WP_Customize_Nav_Menu_Section( $this->manager, $section_id, array(
'title' => html_entity_decode( $menu->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
'priority' => 10,
'panel' => 'nav_menus',
) ) );
$this->manager->add_section(
new WP_Customize_Nav_Menu_Section(
$this->manager, $section_id, array(
'title' => html_entity_decode( $menu->name, ENT_QUOTES, get_bloginfo( 'charset' ) ),
'priority' => 10,
'panel' => 'nav_menus',
)
)
);
$nav_menu_setting_id = 'nav_menu[' . $menu_id . ']';
$this->manager->add_setting( new WP_Customize_Nav_Menu_Setting( $this->manager, $nav_menu_setting_id, array(
'transport' => 'postMessage',
) ) );
$this->manager->add_setting(
new WP_Customize_Nav_Menu_Setting(
$this->manager, $nav_menu_setting_id, array(
'transport' => 'postMessage',
)
)
);
// Add the menu contents.
$menu_items = (array) wp_get_nav_menu_items( $menu_id );
@@ -671,36 +706,50 @@ final class WP_Customize_Nav_Menus {
}
$value['nav_menu_term_id'] = $menu_id;
$this->manager->add_setting( new WP_Customize_Nav_Menu_Item_Setting( $this->manager, $menu_item_setting_id, array(
'value' => $value,
'transport' => 'postMessage',
) ) );
$this->manager->add_setting(
new WP_Customize_Nav_Menu_Item_Setting(
$this->manager, $menu_item_setting_id, array(
'value' => $value,
'transport' => 'postMessage',
)
)
);
// Create a control for each menu item.
$this->manager->add_control( new WP_Customize_Nav_Menu_Item_Control( $this->manager, $menu_item_setting_id, array(
'label' => $item->title,
'section' => $section_id,
'priority' => 10 + $i,
) ) );
$this->manager->add_control(
new WP_Customize_Nav_Menu_Item_Control(
$this->manager, $menu_item_setting_id, array(
'label' => $item->title,
'section' => $section_id,
'priority' => 10 + $i,
)
)
);
}
// Note: other controls inside of this section get added dynamically in JS via the MenuSection.ready() function.
}
// Add the add-new-menu section and controls.
$this->manager->add_section( 'add_menu', array(
'type' => 'new_menu',
'title' => __( 'New Menu' ),
'panel' => 'nav_menus',
'priority' => 20,
) );
$this->manager->add_section(
'add_menu', array(
'type' => 'new_menu',
'title' => __( 'New Menu' ),
'panel' => 'nav_menus',
'priority' => 20,
)
);
$this->manager->add_setting( new WP_Customize_Filter_Setting( $this->manager, 'nav_menus_created_posts', array(
'transport' => 'postMessage',
'type' => 'option', // To prevent theme prefix in changeset.
'default' => array(),
'sanitize_callback' => array( $this, 'sanitize_nav_menus_created_posts' ),
) ) );
$this->manager->add_setting(
new WP_Customize_Filter_Setting(
$this->manager, 'nav_menus_created_posts', array(
'transport' => 'postMessage',
'type' => 'option', // To prevent theme prefix in changeset.
'default' => array(),
'sanitize_callback' => array( $this, 'sanitize_nav_menus_created_posts' ),
)
)
);
}
/**
@@ -733,10 +782,10 @@ final class WP_Customize_Nav_Menus {
if ( $post_types ) {
foreach ( $post_types as $slug => $post_type ) {
$item_types[] = array(
'title' => $post_type->labels->name,
'title' => $post_type->labels->name,
'type_label' => $post_type->labels->singular_name,
'type' => 'post_type',
'object' => $post_type->name,
'type' => 'post_type',
'object' => $post_type->name,
);
}
}
@@ -748,10 +797,10 @@ final class WP_Customize_Nav_Menus {
continue;
}
$item_types[] = array(
'title' => $taxonomy->labels->name,
'title' => $taxonomy->labels->name,
'type_label' => $taxonomy->labels->singular_name,
'type' => 'taxonomy',
'object' => $taxonomy->name,
'type' => 'taxonomy',
'object' => $taxonomy->name,
);
}
}
@@ -777,10 +826,10 @@ final class WP_Customize_Nav_Menus {
* @param array $postarr {
* Post array. Note that post_status is overridden to be `auto-draft`.
*
* @var string $post_title Post title. Required.
* @var string $post_type Post type. Required.
* @var string $post_name Post name.
* @var string $post_content Post content.
* @var string $post_title Post title. Required.
* @var string $post_type Post type. Required.
* @var string $post_name Post name.
* @var string $post_content Post content.
* }
* @return WP_Post|WP_Error Inserted auto-draft post object or error.
*/
@@ -809,7 +858,7 @@ final class WP_Customize_Nav_Menus {
$postarr['meta_input'] = array();
}
$postarr['meta_input']['_customize_draft_post_name'] = $postarr['post_name'];
$postarr['meta_input']['_customize_changeset_uuid'] = $this->manager->changeset_uuid();
$postarr['meta_input']['_customize_changeset_uuid'] = $this->manager->changeset_uuid();
unset( $postarr['post_name'] );
add_filter( 'wp_insert_post_empty_content', '__return_false', 1000 );
@@ -841,7 +890,7 @@ final class WP_Customize_Nav_Menus {
wp_send_json_error( 'missing_params', 400 );
}
$params = wp_unslash( $_POST['params'] );
$params = wp_unslash( $_POST['params'] );
$illegal_params = array_diff( array_keys( $params ), array( 'post_type', 'post_title' ) );
if ( ! empty( $illegal_params ) ) {
wp_send_json_error( 'illegal_params', 400 );
@@ -849,7 +898,7 @@ final class WP_Customize_Nav_Menus {
$params = array_merge(
array(
'post_type' => '',
'post_type' => '',
'post_title' => '',
),
$params
@@ -914,10 +963,12 @@ final class WP_Customize_Nav_Menus {
<span class="menu-item-title<# if ( ! data.title ) { #> no-title<# } #>">{{ data.title || wp.customize.Menus.data.l10n.untitled }}</span>
</span>
<button type="button" class="button-link item-add">
<span class="screen-reader-text"><?php
<span class="screen-reader-text">
<?php
/* translators: 1: Title of a menu item, 2: Type of a menu item */
printf( __( 'Add to menu: %1$s (%2$s)' ), '{{ data.title || wp.customize.Menus.data.l10n.untitled }}', '{{ data.type_label }}' );
?></span>
?>
</span>
</button>
</div>
</div>
@@ -997,7 +1048,7 @@ final class WP_Customize_Nav_Menus {
<div id="available-menu-items-search" class="accordion-section cannot-expand">
<div class="accordion-section-title">
<label class="screen-reader-text" for="menu-items-search"><?php _e( 'Search Menu Items' ); ?></label>
<input type="text" id="menu-items-search" placeholder="<?php esc_attr_e( 'Search menu items&hellip;' ) ?>" aria-describedby="menu-items-search-desc" />
<input type="text" id="menu-items-search" placeholder="<?php esc_attr_e( 'Search menu items&hellip;' ); ?>" aria-describedby="menu-items-search-desc" />
<p class="screen-reader-text" id="menu-items-search-desc"><?php _e( 'The search results will be updated as you type.' ); ?></p>
<span class="spinner"></span>
</div>
@@ -1008,7 +1059,7 @@ final class WP_Customize_Nav_Menus {
<?php
// Ensure the page post type comes first in the list.
$item_types = $this->available_item_types();
$item_types = $this->available_item_types();
$page_item_type = null;
foreach ( $item_types as $i => $item_type ) {
if ( isset( $item_type['object'] ) && 'page' === $item_type['object'] ) {
@@ -1049,9 +1100,12 @@ final class WP_Customize_Nav_Menus {
<span class="spinner"></span>
<span class="no-items"><?php _e( 'No items' ); ?></span>
<button type="button" class="button-link" aria-expanded="false">
<span class="screen-reader-text"><?php
<span class="screen-reader-text">
<?php
/* translators: %s: Title of a section with menu items */
printf( __( 'Toggle section: %s' ), esc_html( $available_item_type['title'] ) ); ?></span>
printf( __( 'Toggle section: %s' ), esc_html( $available_item_type['title'] ) );
?>
</span>
<span class="toggle-indicator" aria-hidden="true"></span>
</button>
</h4>
@@ -1231,11 +1285,11 @@ final class WP_Customize_Nav_Menus {
}
$target_status = 'attachment' === get_post_type( $post_id ) ? 'inherit' : 'publish';
$args = array(
'ID' => $post_id,
$args = array(
'ID' => $post_id,
'post_status' => $target_status,
);
$post_name = get_post_meta( $post_id, '_customize_draft_post_name', true );
$post_name = get_post_meta( $post_id, '_customize_draft_post_name', true );
if ( $post_name ) {
$args['post_name'] = $post_name;
}
@@ -1294,7 +1348,7 @@ final class WP_Customize_Nav_Menus {
// Empty out args which may not be JSON-serializable.
if ( ! $can_partial_refresh ) {
$exported_args['fallback_cb'] = '';
$exported_args['walker'] = '';
$exported_args['walker'] = '';
}
/*
@@ -1308,7 +1362,7 @@ final class WP_Customize_Nav_Menus {
ksort( $exported_args );
$exported_args['args_hmac'] = $this->hash_nav_menu_args( $exported_args );
$args['customize_preview_nav_menus_args'] = $exported_args;
$args['customize_preview_nav_menus_args'] = $exported_args;
$this->preview_nav_menu_instance_args[ $exported_args['args_hmac'] ] = $exported_args;
return $args;
}
@@ -1328,9 +1382,9 @@ final class WP_Customize_Nav_Menus {
*/
public function filter_wp_nav_menu( $nav_menu_content, $args ) {
if ( isset( $args->customize_preview_nav_menus_args['can_partial_refresh'] ) && $args->customize_preview_nav_menus_args['can_partial_refresh'] ) {
$attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) );
$attributes .= ' data-customize-partial-type="nav_menu_instance"';
$attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) );
$attributes = sprintf( ' data-customize-partial-id="%s"', esc_attr( 'nav_menu_instance[' . $args->customize_preview_nav_menus_args['args_hmac'] . ']' ) );
$attributes .= ' data-customize-partial-type="nav_menu_instance"';
$attributes .= sprintf( ' data-customize-partial-placement-context="%s"', esc_attr( wp_json_encode( $args->customize_preview_nav_menus_args ) ) );
$nav_menu_content = preg_replace( '#^(<\w+)#', '$1 ' . str_replace( '\\', '\\\\', $attributes ), $nav_menu_content, 1 );
}
return $nav_menu_content;