Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Include nav menu items and their postmeta in requests to fetch settings #263

Merged
merged 2 commits into from Sep 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions js/customize-posts.js
Expand Up @@ -220,7 +220,7 @@
_.each( postIds, function( postId ) {
var postType, postData, id;
postType = component.fetchedPosts[ postId ];
if ( postType ) {
if ( postType && 'nav_menu_item' !== postType ) {
id = 'post[' + postType + '][' + String( postId ) + ']';
postData = {
postType: postType,
Expand Down Expand Up @@ -286,8 +286,14 @@
component.addPostSettings = function addPostSettings( settings ) {
var postIds = [];
_.each( settings, function( settingArgs, id ) {
var setting, parsedSettingId = component.parseSettingId( id );
var setting, matches, parsedSettingId = component.parseSettingId( id );
if ( ! parsedSettingId ) {

// Special case: make sure the fetch of a nav menu item is recorded so that it is not re-fetched later.
matches = id.match( /^nav_menu_item\[(-?\d+)]$/ );
if ( matches ) {
component.fetchedPosts[ parseInt( matches[1], 10 ) ] = 'nav_menu_item';
}
return;
}
postIds.push( parsedSettingId.postId );
Expand Down Expand Up @@ -326,7 +332,7 @@
component.addPostSection = function( settingId ) {
var section, parsedSettingId, sectionId, panelId, sectionType, Constructor, htmlParser, postTypeObj;
parsedSettingId = component.parseSettingId( settingId );
if ( ! parsedSettingId ) {
if ( ! parsedSettingId || 'post' !== parsedSettingId.settingType ) {
throw new Error( 'Bad setting ID' );
}
postTypeObj = component.data.postTypes[ parsedSettingId.postType ];
Expand Down
26 changes: 19 additions & 7 deletions php/class-wp-customize-posts.php
Expand Up @@ -497,7 +497,7 @@ public function filter_customize_dynamic_setting_class( $class, $setting_id, $ar
/**
* Add all postmeta settings for all registered postmeta for a given post type instance.
*
* @param WP_Post $post Post ID.
* @param WP_Post $post Post.
* @return array
*/
public function register_post_type_meta_settings( $post ) {
Expand Down Expand Up @@ -1101,19 +1101,31 @@ public function get_settings( array $post_ids ) {
$query = new WP_Query( array(
'post__in' => $post_ids,
'ignore_sticky_posts' => true,
'post_type' => get_post_types( array(), 'names' ), // @todo Not ideal.
'post_status' => get_post_stati( array(), 'names' ), // @todo Not ideal.
'post_type' => get_post_types( array(), 'names' ),
'post_status' => get_post_stati( array(), 'names' ),
) );
$post_setting_ids = array_map( array( 'WP_Customize_Post_Setting', 'get_post_setting_id' ), $query->posts );
if ( ! empty( $post_setting_ids ) ) {
$this->manager->add_dynamic_settings( $post_setting_ids );
$post_setting_ids = array();
foreach ( $query->posts as $post ) {
if ( 'nav_menu_item' === $post->post_type ) {
$post_setting_ids[] = sprintf( 'nav_menu_item[%d]', $post->ID );
} else {
$post_setting_ids[] = WP_Customize_Post_Setting::get_post_setting_id( $post );
}
}
$this->manager->add_dynamic_settings( $post_setting_ids );
foreach ( $query->posts as $post ) {
$this->register_post_type_meta_settings( $post );
}
$settings = array();
foreach ( $this->manager->settings() as $setting ) {
if ( $setting instanceof WP_Customize_Post_Setting || $setting instanceof WP_Customize_Postmeta_Setting ) {
$is_requested_setting_type = (
$setting instanceof WP_Customize_Post_Setting
||
$setting instanceof WP_Customize_Postmeta_Setting
||
$setting instanceof WP_Customize_Nav_Menu_Item_Setting
);
if ( $is_requested_setting_type && in_array( $setting->post_id, $post_ids, true ) ) {
$settings[ $setting->id ] = $setting;
}
}
Expand Down
10 changes: 8 additions & 2 deletions tests/php/test-class-wp-customize-posts.php
Expand Up @@ -694,16 +694,22 @@ public function test_get_settings() {
$published_post_id = $this->factory()->post->create( array( 'post_status' => 'publish', 'post_name' => 'foo' ) );
$trashed_post_id = $this->factory()->post->create( array( 'post_status' => 'private', 'post_name' => 'bar' ) );
$draft_page_id = $this->factory()->post->create( array( 'post_status' => 'draft', 'post_name' => 'quux', 'post_type' => 'page' ) );
$nav_menu_id = wp_create_nav_menu( 'Test' );
$nav_menu_item_id = wp_update_nav_menu_item( $nav_menu_id, 0, array(
'menu-item-type' => 'custom',
'menu-item-title' => 'Example',
'menu-item-url' => 'http://example.com/',
) );
$this->posts->register_post_type_meta( 'post', 'baz' );
wp_trash_post( $trashed_post_id );

$settings_params = $this->posts->get_settings( array( $published_post_id, $trashed_post_id, $draft_page_id ) );
$this->assertCount( 5, $settings_params );
$settings_params = $this->posts->get_settings( array( $published_post_id, $trashed_post_id, $draft_page_id, $nav_menu_item_id ) );
$this->assertEqualSets(
array(
WP_Customize_Post_Setting::get_post_setting_id( get_post( $published_post_id ) ),
WP_Customize_Post_Setting::get_post_setting_id( get_post( $trashed_post_id ) ),
WP_Customize_Post_Setting::get_post_setting_id( get_post( $draft_page_id ) ),
sprintf( 'nav_menu_item[%s]', $nav_menu_item_id ),
WP_Customize_Postmeta_Setting::get_post_meta_setting_id( get_post( $published_post_id ), 'baz' ),
WP_Customize_Postmeta_Setting::get_post_meta_setting_id( get_post( $trashed_post_id ), 'baz' )
),
Expand Down