Customize: Keep alive auto-drafts created for page/post stubs when parent changeset is updated, and delete when changeset is garbage-collected.
Props utkarshpatel, westonruter. See #31089. Fixes #39715. git-svn-id: https://develop.svn.wordpress.org/trunk@40676 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
8eade12087
commit
77fe870bd5
@ -325,6 +325,7 @@ add_action( 'welcome_panel', 'wp_welcome_panel'
|
|||||||
add_action( 'delete_post', '_wp_delete_post_menu_item' );
|
add_action( 'delete_post', '_wp_delete_post_menu_item' );
|
||||||
add_action( 'delete_term', '_wp_delete_tax_menu_item', 10, 3 );
|
add_action( 'delete_term', '_wp_delete_tax_menu_item', 10, 3 );
|
||||||
add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );
|
add_action( 'transition_post_status', '_wp_auto_add_pages_to_menu', 10, 3 );
|
||||||
|
add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
|
||||||
|
|
||||||
// Post Thumbnail CSS class filtering
|
// Post Thumbnail CSS class filtering
|
||||||
add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
|
add_action( 'begin_fetch_post_thumbnail_html', '_wp_post_thumbnail_class_filter_add' );
|
||||||
@ -400,6 +401,7 @@ add_action( 'plugins_loaded', '_wp_customize_include' );
|
|||||||
add_action( 'transition_post_status', '_wp_customize_publish_changeset', 10, 3 );
|
add_action( 'transition_post_status', '_wp_customize_publish_changeset', 10, 3 );
|
||||||
add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
|
add_action( 'admin_enqueue_scripts', '_wp_customize_loader_settings' );
|
||||||
add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
|
add_action( 'delete_attachment', '_delete_attachment_theme_mod' );
|
||||||
|
add_action( 'transition_post_status', '_wp_keep_alive_customize_changeset_dependent_auto_drafts', 20, 3 );
|
||||||
|
|
||||||
// Calendar widget cache
|
// Calendar widget cache
|
||||||
add_action( 'save_post', 'delete_get_calendar_cache' );
|
add_action( 'save_post', 'delete_get_calendar_cache' );
|
||||||
|
@ -978,3 +978,31 @@ function _wp_auto_add_pages_to_menu( $new_status, $old_status, $post ) {
|
|||||||
wp_update_nav_menu_item( $menu_id, 0, $args );
|
wp_update_nav_menu_item( $menu_id, 0, $args );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete auto-draft posts associated with the supplied changeset.
|
||||||
|
*
|
||||||
|
* @since 4.8.0
|
||||||
|
* @access private
|
||||||
|
*
|
||||||
|
* @param int $post_id Post ID for the customize_changeset.
|
||||||
|
*/
|
||||||
|
function _wp_delete_customize_changeset_dependent_auto_drafts( $post_id ) {
|
||||||
|
$post = get_post( $post_id );
|
||||||
|
|
||||||
|
if ( ! $post || 'customize_changeset' !== $post->post_type ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = json_decode( $post->post_content, true );
|
||||||
|
if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
remove_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
|
||||||
|
foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
|
||||||
|
if ( ! empty( $post_id ) && 'auto-draft' === get_post_status( $post_id ) ) {
|
||||||
|
wp_delete_post( $post_id, true );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add_action( 'delete_post', '_wp_delete_customize_changeset_dependent_auto_drafts' );
|
||||||
|
}
|
||||||
|
@ -3059,3 +3059,65 @@ function is_customize_preview() {
|
|||||||
|
|
||||||
return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
|
return ( $wp_customize instanceof WP_Customize_Manager ) && $wp_customize->is_preview();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure that auto-draft posts get their post_date bumped to prevent premature garbage-collection.
|
||||||
|
*
|
||||||
|
* When a changeset is updated but remains an auto-draft, ensure the post_date
|
||||||
|
* for the auto-draft posts remains the same so that it will be
|
||||||
|
* garbage-collected at the same time by `wp_delete_auto_drafts()`. Otherwise,
|
||||||
|
* if the changeset is updated to be a draft then update the posts
|
||||||
|
* to have a far-future post_date so that they will never be garbage collected
|
||||||
|
* unless the changeset post itself is deleted.
|
||||||
|
*
|
||||||
|
* @since 4.8.0
|
||||||
|
* @access private
|
||||||
|
* @see wp_delete_auto_drafts()
|
||||||
|
*
|
||||||
|
* @param string $new_status Transition to this post status.
|
||||||
|
* @param string $old_status Previous post status.
|
||||||
|
* @param \WP_Post $post Post data.
|
||||||
|
* @global wpdb $wpdb
|
||||||
|
*/
|
||||||
|
function _wp_keep_alive_customize_changeset_dependent_auto_drafts( $new_status, $old_status, $post ) {
|
||||||
|
global $wpdb;
|
||||||
|
unset( $old_status );
|
||||||
|
|
||||||
|
// Short-circuit if not a changeset or if the changeset was published.
|
||||||
|
if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( 'auto-draft' === $new_status ) {
|
||||||
|
/*
|
||||||
|
* Keep the post date for the post matching the changeset
|
||||||
|
* so that it will not be garbage-collected before the changeset.
|
||||||
|
*/
|
||||||
|
$new_post_date = $post->post_date;
|
||||||
|
} else {
|
||||||
|
/*
|
||||||
|
* Since the changeset no longer has an auto-draft (and it is not published)
|
||||||
|
* it is now a persistent changeset, a long-lived draft, and so any
|
||||||
|
* associated auto-draft posts should have their dates
|
||||||
|
* pushed out very far into the future to prevent them from ever
|
||||||
|
* being garbage-collected.
|
||||||
|
*/
|
||||||
|
$new_post_date = gmdate( 'Y-m-d H:i:d', strtotime( '+100 years' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = json_decode( $post->post_content, true );
|
||||||
|
if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
|
||||||
|
if ( empty( $post_id ) || 'auto-draft' !== get_post_status( $post_id ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$wpdb->update(
|
||||||
|
$wpdb->posts,
|
||||||
|
array( 'post_date' => $new_post_date ), // Note wp_delete_auto_drafts() only looks at this this date.
|
||||||
|
array( 'ID' => $post_id )
|
||||||
|
);
|
||||||
|
clean_post_cache( $post_id );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -523,4 +523,34 @@ class Test_Nav_Menus extends WP_UnitTestCase {
|
|||||||
|
|
||||||
$this->assertNotContains( 'menu-item-home', $classes );
|
$this->assertNotContains( 'menu-item-home', $classes );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test _wp_delete_customize_changeset_dependent_auto_drafts.
|
||||||
|
*
|
||||||
|
* @covers _wp_delete_customize_changeset_dependent_auto_drafts()
|
||||||
|
*/
|
||||||
|
function test_wp_delete_customize_changeset_dependent_auto_drafts() {
|
||||||
|
$nav_created_post_ids = $this->factory()->post->create_many(2, array(
|
||||||
|
'post_status' => 'auto-draft',
|
||||||
|
) );
|
||||||
|
$data = array(
|
||||||
|
'nav_menus_created_posts' => array(
|
||||||
|
'value' => $nav_created_post_ids,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
wp_set_current_user( self::factory()->user->create( array(
|
||||||
|
'role' => 'administrator',
|
||||||
|
) ) );
|
||||||
|
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
|
||||||
|
$wp_customize = new WP_Customize_Manager();
|
||||||
|
do_action( 'customize_register', $wp_customize );
|
||||||
|
$wp_customize->save_changeset_post( array(
|
||||||
|
'data' => $data,
|
||||||
|
) );
|
||||||
|
$this->assertInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[0] ) );
|
||||||
|
$this->assertInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
|
||||||
|
wp_delete_post( $wp_customize->changeset_post_id(), true );
|
||||||
|
$this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[0] ) );
|
||||||
|
$this->assertNotInstanceOf( 'WP_Post', get_post( $nav_created_post_ids[1] ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -309,4 +309,36 @@ class Tests_Theme extends WP_UnitTestCase {
|
|||||||
$this->assertEquals($template, get_template());
|
$this->assertEquals($template, get_template());
|
||||||
$this->assertEquals($style, get_stylesheet());
|
$this->assertEquals($style, get_stylesheet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test _wp_keep_alive_customize_changeset_dependent_auto_drafts.
|
||||||
|
*
|
||||||
|
* @covers _wp_keep_alive_customize_changeset_dependent_auto_drafts()
|
||||||
|
*/
|
||||||
|
function test_wp_keep_alive_customize_changeset_dependent_auto_drafts() {
|
||||||
|
$nav_created_post_ids = $this->factory()->post->create_many(2, array(
|
||||||
|
'post_status' => 'auto-draft',
|
||||||
|
) );
|
||||||
|
$data = array(
|
||||||
|
'nav_menus_created_posts' => array(
|
||||||
|
'value' => $nav_created_post_ids,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
wp_set_current_user( self::factory()->user->create( array( 'role' => 'administrator' ) ) );
|
||||||
|
require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
|
||||||
|
$wp_customize = new WP_Customize_Manager();
|
||||||
|
do_action( 'customize_register', $wp_customize );
|
||||||
|
$wp_customize->save_changeset_post( array(
|
||||||
|
'data' => $data,
|
||||||
|
) );
|
||||||
|
$this->assertEquals( get_post( $nav_created_post_ids[0] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
|
||||||
|
$this->assertEquals( get_post( $nav_created_post_ids[1] )->post_date, get_post( $wp_customize->changeset_post_id() )->post_date );
|
||||||
|
$wp_customize->save_changeset_post( array(
|
||||||
|
'status' => 'draft',
|
||||||
|
'data' => $data,
|
||||||
|
) );
|
||||||
|
$expected_year = date( 'Y' ) + 100;
|
||||||
|
$this->assertEquals( $expected_year, date( 'Y', strtotime( get_post( $nav_created_post_ids[0] )->post_date ) ) );
|
||||||
|
$this->assertEquals( $expected_year, date( 'Y', strtotime( get_post( $nav_created_post_ids[1] )->post_date ) ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user