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

Improve compat with posts & pages created as stubs in 4.7 #320

Merged
merged 2 commits into from
Nov 23, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions php/class-wp-customize-posts-preview.php
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,14 @@ public function export_preview_data() {
}
}

// Ensure that page/post stubs are included among the queried posts.
if ( $this->component->manager->nav_menus && $this->component->manager->get_setting( 'nav_menus_created_posts' ) ) {
$this->queried_post_ids = array_merge(
$this->queried_post_ids,
$this->component->manager->get_setting( 'nav_menus_created_posts' )->value()
);
}

$exported = array(
'isPostPreview' => is_preview(),
'isSingular' => is_singular(),
Expand Down
30 changes: 30 additions & 0 deletions php/class-wp-customize-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public function __construct( WP_Customize_Manager $manager ) {
add_action( 'init', array( $this, 'register_meta' ), 100 );
add_filter( 'customize_dynamic_setting_args', array( $this, 'filter_customize_dynamic_setting_args' ), 10, 2 );
add_filter( 'customize_dynamic_setting_class', array( $this, 'filter_customize_dynamic_setting_class' ), 5, 3 );
add_filter( 'customize_sanitize_nav_menus_created_posts', array( $this, 'filter_out_nav_menus_created_posts_for_customized_posts' ), 20 );
add_filter( 'customize_save_response', array( $this, 'filter_customize_save_response_for_conflicts' ), 10, 2 );
add_filter( 'customize_save_response', array( $this, 'filter_customize_save_response_to_export_saved_values' ), 10, 2 );
add_action( 'customize_controls_print_footer_scripts', array( $this, 'render_templates' ) );
Expand Down Expand Up @@ -547,6 +548,35 @@ public function filter_customize_dynamic_setting_class( $class, $setting_id, $ar
return $class;
}

/**
* Filter the value for `nav_menus_created_posts` to remove post IDs for posts which being fully customized.
*
* If an ID is present among `nav_menus_created_posts` while also among the customized posts,
* then a conflict will arise. For example, if a post stub gets edited with its status changed
* to 'private' then when `WP_Customize_Nav_Menus::save_nav_menus_created_posts()` runs
* it can override it to be 'publish` if the setting gets updated after the post setting
* is saved. If, on the other hand, the `nav_menus_created_posts` setting is processed
* first then the subsequent save for the `post` setting can fail due to post conflict locking.
*
* @param array $post_ids IDs for post/page stubs.
* @return array IDs for posts that do not have post settings.
*/
public function filter_out_nav_menus_created_posts_for_customized_posts( $post_ids ) {
$non_customized_post_ids = array();
foreach ( $post_ids as $post_id ) {
$post = get_post( $post_id );
if ( ! $post ) {
continue;
}
$setting_id = WP_Customize_Post_Setting::get_post_setting_id( $post );
if ( $this->manager->get_setting( $setting_id ) ) {
continue;
}
$non_customized_post_ids[] = $post_id;
}
return $non_customized_post_ids;
}

/**
* Add all postmeta settings for all registered postmeta for a given post type instance.
*
Expand Down