Make WordPress Core

Ticket #39715: proof-of-concept.php

File proof-of-concept.php, 3.2 KB (added by westonruter, 5 years ago)

Proof of concept (plugin)

Line 
1<?php
2
3// Sync status of nav menus-created posts with the status of the changeset.
4add_action( 'transition_post_status', 'keep_alive_auto_drafts_at_transition_post_status', 20, 3 );
5add_action( 'delete_post', 'delete_changeset_nav_menus_created_auto_draft_posts' );
6
7/**
8 * Make sure that auto-draft posts get their post_date bumped to prevent premature garbage-collection.
9 *
10 * When a changeset is updated but remains an auto-draft, ensure the post_date
11 * for the auto-draft posts remains the same so that it will be
12 * garbage-collected at the same time by `wp_delete_auto_drafts()`. Otherwise,
13 * if the changeset is updated to be a draft then update the posts
14 * to have a far-future post_date so that they will never be garbage collected
15 * unless the changeset post itself is deleted.
16 *
17 * @see wp_delete_auto_drafts()
18 *
19 * @param string   $new_status Transition to this post status.
20 * @param string   $old_status Previous post status.
21 * @param \WP_Post $post Post data.
22 */
23function keep_alive_auto_drafts_at_transition_post_status( $new_status, $old_status, $post ) {
24        unset( $old_status );
25        global $wpdb;
26
27        // Short-circuit if not a changeset or if the changeset was published.
28        if ( 'customize_changeset' !== $post->post_type || 'publish' === $new_status ) {
29                return;
30        }
31
32        if ( 'auto-draft' === $new_status ) {
33                /*
34                 * Keep the post date for the post matching the changeset
35                 * so that it will not be garbage-collected before the changeset.
36                 */
37                $new_post_date = $post->post_date;
38        } else {
39                /*
40                 * Since the changeset no longer has an auto-draft (and it is not published)
41                 * it is now a persistent changeset, a long-lived draft, and so any
42                 * associated auto-draft posts should have their dates
43                 * pushed out very far into the future to prevent them from ever
44                 * being garbage-collected.
45                 */
46                $new_post_date = gmdate( 'Y-m-d H:i:d', strtotime( '+100 years' ) );
47        }
48
49        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
50        $wp_customize = new \WP_Customize_Manager( array( 'changeset_uuid' => $post->post_name ) );
51        $data = $wp_customize->changeset_data();
52        if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
53                return;
54        }
55
56        foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
57                if ( 'auto-draft' !== get_post_status( $post_id ) ) {
58                        continue;
59                }
60
61                $wpdb->update(
62                        $wpdb->posts,
63                        array( 'post_date' => $new_post_date ), // Note wp_delete_auto_drafts() only looks at this this date.
64                        array( 'ID' => $post_id )
65                );
66                clean_post_cache( $post_id );
67        }
68}
69
70/**
71 * Delete auto-draft posts associated with the supplied changeset.
72 *
73 * @param int $post_id Post ID for the customize_changeset.
74 */
75function delete_changeset_nav_menus_created_auto_draft_posts( $post_id ) {
76        $post = get_post( $post_id );
77        if ( ! $post || 'customize_changeset' !== $post->post_type ) {
78                return;
79        }
80
81        require_once ABSPATH . WPINC . '/class-wp-customize-manager.php';
82        $wp_customize = new \WP_Customize_Manager( array( 'changeset_uuid' => $post->post_name ) );
83        $data = $wp_customize->changeset_data();
84        if ( empty( $data['nav_menus_created_posts']['value'] ) ) {
85                return;
86        }
87        foreach ( $data['nav_menus_created_posts']['value'] as $post_id ) {
88                if ( 'auto-draft' === get_post_status( $post_id ) ) {
89                        wp_delete_post( $post->ID, true );
90                }
91        }
92}