author | ymh <ymh.work@gmail.com> |
Fri, 05 Sep 2025 18:40:08 +0200 | |
changeset 21 | 48c4eec2b7e6 |
parent 19 | 3d72ae0968f4 |
child 22 | 8c2e4d02f4ef |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Post revision functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Post_Revisions |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Determines which fields of posts are to be saved in revisions. |
|
11 |
* |
|
12 |
* @since 2.6.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
* @since 4.5.0 A `WP_Post` object can now be passed to the `$post` parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @since 4.5.0 The optional `$autosave` parameter was deprecated and renamed to `$deprecated`. |
0 | 15 |
* @access private |
16 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* @param array|WP_Post $post Optional. A post array or a WP_Post object being processed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
18 |
* for insertion as a post revision. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
19 |
* @param bool $deprecated Not used. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
20 |
* @return string[] Array of fields that can be versioned. |
0 | 21 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
22 |
function _wp_post_revision_fields( $post = array(), $deprecated = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
23 |
static $fields = null; |
0 | 24 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
25 |
if ( ! is_array( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
26 |
$post = get_post( $post, ARRAY_A ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
27 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
28 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
29 |
if ( is_null( $fields ) ) { |
16 | 30 |
// Allow these to be versioned. |
0 | 31 |
$fields = array( |
9 | 32 |
'post_title' => __( 'Title' ), |
0 | 33 |
'post_content' => __( 'Content' ), |
34 |
'post_excerpt' => __( 'Excerpt' ), |
|
35 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
36 |
} |
0 | 37 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
38 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
39 |
* Filters the list of fields saved in post revisions. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
40 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
41 |
* Included by default: 'post_title', 'post_content' and 'post_excerpt'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
42 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
43 |
* Disallowed fields: 'ID', 'post_name', 'post_parent', 'post_date', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
* 'post_date_gmt', 'post_status', 'post_type', 'comment_count', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* and 'post_author'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* @since 2.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* @since 4.5.0 The `$post` parameter was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
50 |
* @param string[] $fields List of fields to revision. Contains 'post_title', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
51 |
* 'post_content', and 'post_excerpt' by default. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
52 |
* @param array $post A post array being processed for insertion as a post revision. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
$fields = apply_filters( '_wp_post_revision_fields', $fields, $post ); |
0 | 55 |
|
16 | 56 |
// WP uses these internally either in versioning or elsewhere - they cannot be versioned. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
foreach ( array( 'ID', 'post_name', 'post_parent', 'post_date', 'post_date_gmt', 'post_status', 'post_type', 'comment_count', 'post_author' ) as $protect ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
unset( $fields[ $protect ] ); |
0 | 59 |
} |
60 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
61 |
return $fields; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
62 |
} |
0 | 63 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* Returns a post array ready to be inserted into the posts table as a post revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
67 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
68 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
69 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* @param array|WP_Post $post Optional. A post array or a WP_Post object to be processed |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* for insertion as a post revision. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* @param bool $autosave Optional. Is the revision an autosave? Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* @return array Post array ready to be inserted as a post revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
function _wp_post_revision_data( $post = array(), $autosave = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
if ( ! is_array( $post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
$post = get_post( $post, ARRAY_A ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
} |
0 | 79 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
$fields = _wp_post_revision_fields( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
81 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
82 |
$revision_data = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
foreach ( array_intersect( array_keys( $post ), array_keys( $fields ) ) as $field ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
$revision_data[ $field ] = $post[ $field ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
88 |
$revision_data['post_parent'] = $post['ID']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
$revision_data['post_status'] = 'inherit'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
$revision_data['post_type'] = 'revision'; |
16 | 91 |
$revision_data['post_name'] = $autosave ? "$post[ID]-autosave-v1" : "$post[ID]-revision-v1"; // "1" is the revisioning system version. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
$revision_data['post_date'] = isset( $post['post_modified'] ) ? $post['post_modified'] : ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
$revision_data['post_date_gmt'] = isset( $post['post_modified_gmt'] ) ? $post['post_modified_gmt'] : ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
return $revision_data; |
0 | 96 |
} |
97 |
||
98 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
99 |
* Saves revisions for a post after all changes have been made. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
100 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
101 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
102 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
103 |
* @param int $post_id The post id that was inserted. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
104 |
* @param WP_Post $post The post object that was inserted. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
105 |
* @param bool $update Whether this insert is updating an existing post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
106 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
107 |
function wp_save_post_revision_on_insert( $post_id, $post, $update ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
108 |
if ( ! $update ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
109 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
110 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
111 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
112 |
if ( ! has_action( 'post_updated', 'wp_save_post_revision' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
113 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
114 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
115 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
116 |
wp_save_post_revision( $post_id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
117 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
118 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
119 |
/** |
5 | 120 |
* Creates a revision for the current version of a post. |
0 | 121 |
* |
5 | 122 |
* Typically used immediately after a post update, as every update is a revision, |
123 |
* and the most recent revision always matches the current post. |
|
0 | 124 |
* |
125 |
* @since 2.6.0 |
|
126 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
127 |
* @param int $post_id The ID of the post to save as a revision. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
128 |
* @return int|WP_Error|void Void or 0 if error, new revision ID, if success. |
0 | 129 |
*/ |
130 |
function wp_save_post_revision( $post_id ) { |
|
9 | 131 |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { |
0 | 132 |
return; |
9 | 133 |
} |
0 | 134 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
135 |
// Prevent saving post revisions if revisions should be saved on wp_after_insert_post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
136 |
if ( doing_action( 'post_updated' ) && has_action( 'wp_after_insert_post', 'wp_save_post_revision_on_insert' ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
137 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
138 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
139 |
|
16 | 140 |
$post = get_post( $post_id ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
141 |
|
16 | 142 |
if ( ! $post ) { |
0 | 143 |
return; |
9 | 144 |
} |
0 | 145 |
|
9 | 146 |
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
0 | 147 |
return; |
9 | 148 |
} |
0 | 149 |
|
16 | 150 |
if ( 'auto-draft' === $post->post_status ) { |
0 | 151 |
return; |
9 | 152 |
} |
0 | 153 |
|
9 | 154 |
if ( ! wp_revisions_enabled( $post ) ) { |
0 | 155 |
return; |
9 | 156 |
} |
0 | 157 |
|
16 | 158 |
/* |
159 |
* Compare the proposed update with the last stored revision verifying that |
|
160 |
* they are different, unless a plugin tells us to always save regardless. |
|
161 |
* If no previous revisions, save one. |
|
162 |
*/ |
|
163 |
$revisions = wp_get_post_revisions( $post_id ); |
|
164 |
if ( $revisions ) { |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
165 |
// Grab the latest revision, but not an autosave. |
0 | 166 |
foreach ( $revisions as $revision ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
167 |
if ( str_contains( $revision->post_name, "{$revision->post_parent}-revision" ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
168 |
$latest_revision = $revision; |
0 | 169 |
break; |
170 |
} |
|
171 |
} |
|
172 |
||
5 | 173 |
/** |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
174 |
* Filters whether the post has changed since the latest revision. |
5 | 175 |
* |
176 |
* By default a revision is saved only if one of the revisioned fields has changed. |
|
177 |
* This filter can override that so a revision is saved even if nothing has changed. |
|
178 |
* |
|
179 |
* @since 3.6.0 |
|
180 |
* |
|
181 |
* @param bool $check_for_changes Whether to check for changes before saving a new revision. |
|
182 |
* Default true. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
183 |
* @param WP_Post $latest_revision The latest revision post object. |
5 | 184 |
* @param WP_Post $post The post object. |
185 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
186 |
if ( isset( $latest_revision ) && apply_filters( 'wp_save_post_revision_check_for_changes', true, $latest_revision, $post ) ) { |
0 | 187 |
$post_has_changed = false; |
188 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
189 |
foreach ( array_keys( _wp_post_revision_fields( $post ) ) as $field ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
190 |
if ( normalize_whitespace( $post->$field ) !== normalize_whitespace( $latest_revision->$field ) ) { |
0 | 191 |
$post_has_changed = true; |
192 |
break; |
|
193 |
} |
|
194 |
} |
|
5 | 195 |
|
196 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
197 |
* Filters whether a post has changed. |
5 | 198 |
* |
199 |
* By default a revision is saved only if one of the revisioned fields has changed. |
|
200 |
* This filter allows for additional checks to determine if there were changes. |
|
201 |
* |
|
202 |
* @since 4.1.0 |
|
203 |
* |
|
204 |
* @param bool $post_has_changed Whether the post has changed. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
205 |
* @param WP_Post $latest_revision The latest revision post object. |
5 | 206 |
* @param WP_Post $post The post object. |
207 |
*/ |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
208 |
$post_has_changed = (bool) apply_filters( 'wp_save_post_revision_post_has_changed', $post_has_changed, $latest_revision, $post ); |
5 | 209 |
|
16 | 210 |
// Don't save revision if post unchanged. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
211 |
if ( ! $post_has_changed ) { |
0 | 212 |
return; |
5 | 213 |
} |
0 | 214 |
} |
215 |
} |
|
216 |
||
217 |
$return = _wp_put_post_revision( $post ); |
|
218 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
219 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
220 |
* If a limit for the number of revisions to keep has been set, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
221 |
* delete the oldest ones. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
222 |
*/ |
0 | 223 |
$revisions_to_keep = wp_revisions_to_keep( $post ); |
224 |
||
9 | 225 |
if ( $revisions_to_keep < 0 ) { |
0 | 226 |
return $return; |
9 | 227 |
} |
0 | 228 |
|
229 |
$revisions = wp_get_post_revisions( $post_id, array( 'order' => 'ASC' ) ); |
|
230 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
231 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
232 |
* Filters the revisions to be considered for deletion. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
233 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
234 |
* @since 6.2.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
235 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
236 |
* @param WP_Post[] $revisions Array of revisions, or an empty array if none. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
237 |
* @param int $post_id The ID of the post to save as a revision. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
238 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
239 |
$revisions = apply_filters( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
240 |
'wp_save_post_revision_revisions_before_deletion', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
241 |
$revisions, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
242 |
$post_id |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
243 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
244 |
|
9 | 245 |
$delete = count( $revisions ) - $revisions_to_keep; |
0 | 246 |
|
9 | 247 |
if ( $delete < 1 ) { |
0 | 248 |
return $return; |
9 | 249 |
} |
0 | 250 |
|
251 |
$revisions = array_slice( $revisions, 0, $delete ); |
|
252 |
||
9 | 253 |
for ( $i = 0; isset( $revisions[ $i ] ); $i++ ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
254 |
if ( str_contains( $revisions[ $i ]->post_name, 'autosave' ) ) { |
0 | 255 |
continue; |
9 | 256 |
} |
0 | 257 |
|
258 |
wp_delete_post_revision( $revisions[ $i ]->ID ); |
|
259 |
} |
|
260 |
||
261 |
return $return; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
265 |
* Retrieves the autosaved data of the specified post. |
0 | 266 |
* |
16 | 267 |
* Returns a post object with the information that was autosaved for the specified post. |
268 |
* If the optional $user_id is passed, returns the autosave for that user, otherwise |
|
269 |
* returns the latest autosave. |
|
0 | 270 |
* |
271 |
* @since 2.6.0 |
|
272 |
* |
|
18 | 273 |
* @global wpdb $wpdb WordPress database abstraction object. |
274 |
* |
|
0 | 275 |
* @param int $post_id The post ID. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
276 |
* @param int $user_id Optional. The post author ID. Default 0. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
* @return WP_Post|false The autosaved data or false on failure or when no autosave exists. |
0 | 278 |
*/ |
279 |
function wp_get_post_autosave( $post_id, $user_id = 0 ) { |
|
16 | 280 |
global $wpdb; |
281 |
||
282 |
$autosave_name = $post_id . '-autosave-v1'; |
|
283 |
$user_id_query = ( 0 !== $user_id ) ? "AND post_author = $user_id" : null; |
|
0 | 284 |
|
16 | 285 |
// Construct the autosave query. |
286 |
$autosave_query = " |
|
287 |
SELECT * |
|
288 |
FROM $wpdb->posts |
|
289 |
WHERE post_parent = %d |
|
290 |
AND post_type = 'revision' |
|
291 |
AND post_status = 'inherit' |
|
292 |
AND post_name = %s " . $user_id_query . ' |
|
293 |
ORDER BY post_date DESC |
|
294 |
LIMIT 1'; |
|
0 | 295 |
|
16 | 296 |
$autosave = $wpdb->get_results( |
297 |
$wpdb->prepare( |
|
298 |
$autosave_query, |
|
299 |
$post_id, |
|
300 |
$autosave_name |
|
301 |
) |
|
302 |
); |
|
303 |
||
304 |
if ( ! $autosave ) { |
|
305 |
return false; |
|
0 | 306 |
} |
307 |
||
16 | 308 |
return get_post( $autosave[0] ); |
0 | 309 |
} |
310 |
||
311 |
/** |
|
312 |
* Determines if the specified post is a revision. |
|
313 |
* |
|
314 |
* @since 2.6.0 |
|
315 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
316 |
* @param int|WP_Post $post Post ID or post object. |
16 | 317 |
* @return int|false ID of revision's parent on success, false if not a revision. |
0 | 318 |
*/ |
319 |
function wp_is_post_revision( $post ) { |
|
16 | 320 |
$post = wp_get_post_revision( $post ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
321 |
|
16 | 322 |
if ( ! $post ) { |
0 | 323 |
return false; |
9 | 324 |
} |
0 | 325 |
|
326 |
return (int) $post->post_parent; |
|
327 |
} |
|
328 |
||
329 |
/** |
|
330 |
* Determines if the specified post is an autosave. |
|
331 |
* |
|
332 |
* @since 2.6.0 |
|
333 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
* @param int|WP_Post $post Post ID or post object. |
16 | 335 |
* @return int|false ID of autosave's parent on success, false if not a revision. |
0 | 336 |
*/ |
337 |
function wp_is_post_autosave( $post ) { |
|
16 | 338 |
$post = wp_get_post_revision( $post ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
339 |
|
16 | 340 |
if ( ! $post ) { |
0 | 341 |
return false; |
9 | 342 |
} |
0 | 343 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
344 |
if ( str_contains( $post->post_name, "{$post->post_parent}-autosave" ) ) { |
0 | 345 |
return (int) $post->post_parent; |
9 | 346 |
} |
0 | 347 |
|
348 |
return false; |
|
349 |
} |
|
350 |
||
351 |
/** |
|
352 |
* Inserts post data into the posts table as a post revision. |
|
353 |
* |
|
354 |
* @since 2.6.0 |
|
355 |
* @access private |
|
356 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
* @param int|WP_Post|array|null $post Post ID, post object OR post array. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
358 |
* @param bool $autosave Optional. Whether the revision is an autosave or not. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
359 |
* Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
* @return int|WP_Error WP_Error or 0 if error, new revision ID if success. |
0 | 361 |
*/ |
362 |
function _wp_put_post_revision( $post = null, $autosave = false ) { |
|
9 | 363 |
if ( is_object( $post ) ) { |
0 | 364 |
$post = get_object_vars( $post ); |
9 | 365 |
} elseif ( ! is_array( $post ) ) { |
366 |
$post = get_post( $post, ARRAY_A ); |
|
367 |
} |
|
0 | 368 |
|
9 | 369 |
if ( ! $post || empty( $post['ID'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
9 | 371 |
} |
0 | 372 |
|
16 | 373 |
if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) { |
0 | 374 |
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
9 | 375 |
} |
0 | 376 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
377 |
$post = _wp_post_revision_data( $post, $autosave ); |
16 | 378 |
$post = wp_slash( $post ); // Since data is from DB. |
0 | 379 |
|
18 | 380 |
$revision_id = wp_insert_post( $post, true ); |
9 | 381 |
if ( is_wp_error( $revision_id ) ) { |
0 | 382 |
return $revision_id; |
9 | 383 |
} |
0 | 384 |
|
5 | 385 |
if ( $revision_id ) { |
386 |
/** |
|
387 |
* Fires once a revision has been saved. |
|
388 |
* |
|
389 |
* @since 2.6.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
390 |
* @since 6.4.0 The post_id parameter was added. |
5 | 391 |
* |
392 |
* @param int $revision_id Post revision ID. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
393 |
* @param int $post_id Post ID. |
5 | 394 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
395 |
do_action( '_wp_put_post_revision', $revision_id, $post['post_parent'] ); |
5 | 396 |
} |
0 | 397 |
|
398 |
return $revision_id; |
|
399 |
} |
|
400 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
401 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
402 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
403 |
* Save the revisioned meta fields. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
404 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
405 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
406 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
407 |
* @param int $revision_id The ID of the revision to save the meta to. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
408 |
* @param int $post_id The ID of the post the revision is associated with. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
409 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
410 |
function wp_save_revisioned_meta_fields( $revision_id, $post_id ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
411 |
$post_type = get_post_type( $post_id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
412 |
if ( ! $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
413 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
414 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
415 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
416 |
foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
417 |
if ( metadata_exists( 'post', $post_id, $meta_key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
418 |
_wp_copy_post_meta( $post_id, $revision_id, $meta_key ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
419 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
420 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
421 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
422 |
|
0 | 423 |
/** |
424 |
* Gets a post revision. |
|
425 |
* |
|
426 |
* @since 2.6.0 |
|
427 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
428 |
* @param int|WP_Post $post Post ID or post object. |
16 | 429 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
430 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
431 |
* respectively. Default OBJECT. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
432 |
* @param string $filter Optional sanitization filter. See sanitize_post(). Default 'raw'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
433 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 434 |
*/ |
9 | 435 |
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { |
16 | 436 |
$revision = get_post( $post, OBJECT, $filter ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
437 |
|
16 | 438 |
if ( ! $revision ) { |
0 | 439 |
return $revision; |
9 | 440 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
441 |
|
9 | 442 |
if ( 'revision' !== $revision->post_type ) { |
5 | 443 |
return null; |
9 | 444 |
} |
0 | 445 |
|
18 | 446 |
if ( OBJECT === $output ) { |
0 | 447 |
return $revision; |
18 | 448 |
} elseif ( ARRAY_A === $output ) { |
9 | 449 |
$_revision = get_object_vars( $revision ); |
0 | 450 |
return $_revision; |
18 | 451 |
} elseif ( ARRAY_N === $output ) { |
9 | 452 |
$_revision = array_values( get_object_vars( $revision ) ); |
0 | 453 |
return $_revision; |
454 |
} |
|
455 |
||
456 |
return $revision; |
|
457 |
} |
|
458 |
||
459 |
/** |
|
460 |
* Restores a post to the specified revision. |
|
461 |
* |
|
462 |
* Can restore a past revision using all fields of the post revision, or only selected fields. |
|
463 |
* |
|
464 |
* @since 2.6.0 |
|
465 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
466 |
* @param int|WP_Post $revision Revision ID or revision object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
467 |
* @param array $fields Optional. What fields to restore from. Defaults to all. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
468 |
* @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. |
0 | 469 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
470 |
function wp_restore_post_revision( $revision, $fields = null ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
471 |
$revision = wp_get_post_revision( $revision, ARRAY_A ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
472 |
|
16 | 473 |
if ( ! $revision ) { |
0 | 474 |
return $revision; |
9 | 475 |
} |
0 | 476 |
|
9 | 477 |
if ( ! is_array( $fields ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
478 |
$fields = array_keys( _wp_post_revision_fields( $revision ) ); |
9 | 479 |
} |
0 | 480 |
|
481 |
$update = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
482 |
foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
9 | 483 |
$update[ $field ] = $revision[ $field ]; |
0 | 484 |
} |
485 |
||
9 | 486 |
if ( ! $update ) { |
0 | 487 |
return false; |
9 | 488 |
} |
0 | 489 |
|
490 |
$update['ID'] = $revision['post_parent']; |
|
491 |
||
16 | 492 |
$update = wp_slash( $update ); // Since data is from DB. |
0 | 493 |
|
494 |
$post_id = wp_update_post( $update ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
495 |
|
9 | 496 |
if ( ! $post_id || is_wp_error( $post_id ) ) { |
0 | 497 |
return $post_id; |
9 | 498 |
} |
0 | 499 |
|
16 | 500 |
// Update last edit user. |
0 | 501 |
update_post_meta( $post_id, '_edit_last', get_current_user_id() ); |
502 |
||
5 | 503 |
/** |
504 |
* Fires after a post revision has been restored. |
|
505 |
* |
|
506 |
* @since 2.6.0 |
|
507 |
* |
|
508 |
* @param int $post_id Post ID. |
|
509 |
* @param int $revision_id Post revision ID. |
|
510 |
*/ |
|
0 | 511 |
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); |
512 |
||
513 |
return $post_id; |
|
514 |
} |
|
515 |
||
516 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
517 |
* Restore the revisioned meta values for a post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
518 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
519 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
520 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
521 |
* @param int $post_id The ID of the post to restore the meta to. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
522 |
* @param int $revision_id The ID of the revision to restore the meta from. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
523 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
524 |
function wp_restore_post_revision_meta( $post_id, $revision_id ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
525 |
$post_type = get_post_type( $post_id ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
if ( ! $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
527 |
return; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
528 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
529 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
530 |
// Restore revisioned meta fields. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
531 |
foreach ( wp_post_revision_meta_keys( $post_type ) as $meta_key ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
532 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
533 |
// Clear any existing meta. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
534 |
delete_post_meta( $post_id, $meta_key ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
535 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
536 |
_wp_copy_post_meta( $revision_id, $post_id, $meta_key ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
537 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
538 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
539 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
540 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
541 |
* Copy post meta for the given key from one post to another. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
542 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
543 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
544 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
* @param int $source_post_id Post ID to copy meta value(s) from. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
546 |
* @param int $target_post_id Post ID to copy meta value(s) to. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
547 |
* @param string $meta_key Meta key to copy. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
548 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
549 |
function _wp_copy_post_meta( $source_post_id, $target_post_id, $meta_key ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
550 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
551 |
foreach ( get_post_meta( $source_post_id, $meta_key ) as $meta_value ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
552 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
553 |
* We use add_metadata() function vs add_post_meta() here |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
554 |
* to allow for a revision post target OR regular post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
555 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
556 |
add_metadata( 'post', $target_post_id, $meta_key, wp_slash( $meta_value ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
557 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
558 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
559 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
560 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
561 |
* Determine which post meta fields should be revisioned. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
562 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
563 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
564 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
565 |
* @param string $post_type The post type being revisioned. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
566 |
* @return array An array of meta keys to be revisioned. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
567 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
568 |
function wp_post_revision_meta_keys( $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
569 |
$registered_meta = array_merge( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
570 |
get_registered_meta_keys( 'post' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
571 |
get_registered_meta_keys( 'post', $post_type ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
572 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
573 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
574 |
$wp_revisioned_meta_keys = array(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
575 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
576 |
foreach ( $registered_meta as $name => $args ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
if ( $args['revisions_enabled'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
578 |
$wp_revisioned_meta_keys[ $name ] = true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
579 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
580 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
581 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
582 |
$wp_revisioned_meta_keys = array_keys( $wp_revisioned_meta_keys ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
583 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
584 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
585 |
* Filter the list of post meta keys to be revisioned. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
586 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
587 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
588 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
589 |
* @param array $keys An array of meta fields to be revisioned. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
590 |
* @param string $post_type The post type being revisioned. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
591 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
592 |
return apply_filters( 'wp_post_revision_meta_keys', $wp_revisioned_meta_keys, $post_type ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
593 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
594 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
595 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
596 |
* Check whether revisioned post meta fields have changed. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
597 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
598 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
599 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
600 |
* @param bool $post_has_changed Whether the post has changed. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
601 |
* @param WP_Post $last_revision The last revision post object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
602 |
* @param WP_Post $post The post object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
603 |
* @return bool Whether the post has changed. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
604 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
605 |
function wp_check_revisioned_meta_fields_have_changed( $post_has_changed, WP_Post $last_revision, WP_Post $post ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
606 |
foreach ( wp_post_revision_meta_keys( $post->post_type ) as $meta_key ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
607 |
if ( get_post_meta( $post->ID, $meta_key ) !== get_post_meta( $last_revision->ID, $meta_key ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
608 |
$post_has_changed = true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
609 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
610 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
611 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
612 |
return $post_has_changed; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
613 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
614 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
615 |
/** |
0 | 616 |
* Deletes a revision. |
617 |
* |
|
618 |
* Deletes the row from the posts table corresponding to the specified revision. |
|
619 |
* |
|
620 |
* @since 2.6.0 |
|
621 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
622 |
* @param int|WP_Post $revision Revision ID or revision object. |
19 | 623 |
* @return WP_Post|false|null Null or false if error, deleted post object if success. |
0 | 624 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
625 |
function wp_delete_post_revision( $revision ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
626 |
$revision = wp_get_post_revision( $revision ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
627 |
|
16 | 628 |
if ( ! $revision ) { |
0 | 629 |
return $revision; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
630 |
} |
0 | 631 |
|
632 |
$delete = wp_delete_post( $revision->ID ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
633 |
|
5 | 634 |
if ( $delete ) { |
635 |
/** |
|
636 |
* Fires once a post revision has been deleted. |
|
637 |
* |
|
638 |
* @since 2.6.0 |
|
639 |
* |
|
16 | 640 |
* @param int $revision_id Post revision ID. |
641 |
* @param WP_Post $revision Post revision object. |
|
5 | 642 |
*/ |
0 | 643 |
do_action( 'wp_delete_post_revision', $revision->ID, $revision ); |
5 | 644 |
} |
0 | 645 |
|
646 |
return $delete; |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* Returns all revisions of specified post. |
|
651 |
* |
|
652 |
* @since 2.6.0 |
|
653 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
654 |
* @see get_children() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
655 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
656 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
657 |
* @param array|null $args Optional. Arguments for retrieving post revisions. Default null. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
658 |
* @return WP_Post[]|int[] Array of revision objects or IDs, or an empty array if none. |
0 | 659 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
660 |
function wp_get_post_revisions( $post = 0, $args = null ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
661 |
$post = get_post( $post ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
662 |
|
9 | 663 |
if ( ! $post || empty( $post->ID ) ) { |
0 | 664 |
return array(); |
9 | 665 |
} |
0 | 666 |
|
9 | 667 |
$defaults = array( |
668 |
'order' => 'DESC', |
|
669 |
'orderby' => 'date ID', |
|
670 |
'check_enabled' => true, |
|
671 |
); |
|
672 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 673 |
|
9 | 674 |
if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) { |
0 | 675 |
return array(); |
9 | 676 |
} |
0 | 677 |
|
9 | 678 |
$args = array_merge( |
679 |
$args, |
|
680 |
array( |
|
681 |
'post_parent' => $post->ID, |
|
682 |
'post_type' => 'revision', |
|
683 |
'post_status' => 'inherit', |
|
684 |
) |
|
685 |
); |
|
0 | 686 |
|
16 | 687 |
$revisions = get_children( $args ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
688 |
|
16 | 689 |
if ( ! $revisions ) { |
0 | 690 |
return array(); |
9 | 691 |
} |
0 | 692 |
|
693 |
return $revisions; |
|
694 |
} |
|
695 |
||
696 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
697 |
* Returns the latest revision ID and count of revisions for a post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
698 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
699 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
700 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
701 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
702 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
703 |
* Returns associative array with latest revision ID and total count, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
704 |
* or a WP_Error if the post does not exist or revisions are not enabled. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
705 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
706 |
* @type int $latest_id The latest revision post ID or 0 if no revisions exist. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
707 |
* @type int $count The total count of revisions for the given post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
708 |
* } |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
709 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
710 |
function wp_get_latest_revision_id_and_total_count( $post = 0 ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
711 |
$post = get_post( $post ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
712 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
713 |
if ( ! $post ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
714 |
return new WP_Error( 'invalid_post', __( 'Invalid post.' ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
715 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
716 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
717 |
if ( ! wp_revisions_enabled( $post ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
718 |
return new WP_Error( 'revisions_not_enabled', __( 'Revisions not enabled.' ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
719 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
720 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
721 |
$args = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
722 |
'post_parent' => $post->ID, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
723 |
'fields' => 'ids', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
724 |
'post_type' => 'revision', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
725 |
'post_status' => 'inherit', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
726 |
'order' => 'DESC', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
727 |
'orderby' => 'date ID', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
728 |
'posts_per_page' => 1, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
729 |
'ignore_sticky_posts' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
730 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
731 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
732 |
$revision_query = new WP_Query(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
733 |
$revisions = $revision_query->query( $args ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
734 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
735 |
if ( ! $revisions ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
736 |
return array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
737 |
'latest_id' => 0, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
738 |
'count' => 0, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
739 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
740 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
741 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
742 |
return array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
743 |
'latest_id' => $revisions[0], |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
744 |
'count' => $revision_query->found_posts, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
745 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
746 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
747 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
748 |
/** |
19 | 749 |
* Returns the url for viewing and potentially restoring revisions of a given post. |
750 |
* |
|
751 |
* @since 5.9.0 |
|
752 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
753 |
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global `$post`. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
754 |
* @return string|null The URL for editing revisions on the given post, otherwise null. |
19 | 755 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
756 |
function wp_get_post_revisions_url( $post = 0 ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
757 |
$post = get_post( $post ); |
19 | 758 |
|
759 |
if ( ! $post instanceof WP_Post ) { |
|
760 |
return null; |
|
761 |
} |
|
762 |
||
763 |
// If the post is a revision, return early. |
|
764 |
if ( 'revision' === $post->post_type ) { |
|
765 |
return get_edit_post_link( $post ); |
|
766 |
} |
|
767 |
||
768 |
if ( ! wp_revisions_enabled( $post ) ) { |
|
769 |
return null; |
|
770 |
} |
|
771 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
772 |
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID ); |
19 | 773 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
774 |
if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) { |
19 | 775 |
return null; |
776 |
} |
|
777 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
778 |
return get_edit_post_link( $revisions['latest_id'] ); |
19 | 779 |
} |
780 |
||
781 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
782 |
* Determines whether revisions are enabled for a given post. |
0 | 783 |
* |
784 |
* @since 3.6.0 |
|
785 |
* |
|
5 | 786 |
* @param WP_Post $post The post object. |
0 | 787 |
* @return bool True if number of revisions to keep isn't zero, false otherwise. |
788 |
*/ |
|
789 |
function wp_revisions_enabled( $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
790 |
return wp_revisions_to_keep( $post ) !== 0; |
0 | 791 |
} |
792 |
||
793 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
794 |
* Determines how many revisions to retain for a given post. |
5 | 795 |
* |
796 |
* By default, an infinite number of revisions are kept. |
|
797 |
* |
|
798 |
* The constant WP_POST_REVISIONS can be set in wp-config to specify the limit |
|
799 |
* of revisions to keep. |
|
0 | 800 |
* |
801 |
* @since 3.6.0 |
|
802 |
* |
|
5 | 803 |
* @param WP_Post $post The post object. |
0 | 804 |
* @return int The number of revisions to keep. |
805 |
*/ |
|
806 |
function wp_revisions_to_keep( $post ) { |
|
807 |
$num = WP_POST_REVISIONS; |
|
808 |
||
9 | 809 |
if ( true === $num ) { |
0 | 810 |
$num = -1; |
9 | 811 |
} else { |
18 | 812 |
$num = (int) $num; |
9 | 813 |
} |
0 | 814 |
|
9 | 815 |
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
0 | 816 |
$num = 0; |
9 | 817 |
} |
0 | 818 |
|
5 | 819 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
820 |
* Filters the number of revisions to save for the given post. |
5 | 821 |
* |
822 |
* Overrides the value of WP_POST_REVISIONS. |
|
823 |
* |
|
824 |
* @since 3.6.0 |
|
825 |
* |
|
826 |
* @param int $num Number of revisions to store. |
|
827 |
* @param WP_Post $post Post object. |
|
828 |
*/ |
|
18 | 829 |
$num = apply_filters( 'wp_revisions_to_keep', $num, $post ); |
830 |
||
831 |
/** |
|
832 |
* Filters the number of revisions to save for the given post by its post type. |
|
833 |
* |
|
834 |
* Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter. |
|
835 |
* |
|
836 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
|
837 |
* the post type slug. |
|
838 |
* |
|
19 | 839 |
* Possible hook names include: |
840 |
* |
|
841 |
* - `wp_post_revisions_to_keep` |
|
842 |
* - `wp_page_revisions_to_keep` |
|
843 |
* |
|
18 | 844 |
* @since 5.8.0 |
845 |
* |
|
846 |
* @param int $num Number of revisions to store. |
|
847 |
* @param WP_Post $post Post object. |
|
848 |
*/ |
|
849 |
$num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post ); |
|
850 |
||
851 |
return (int) $num; |
|
0 | 852 |
} |
853 |
||
854 |
/** |
|
855 |
* Sets up the post object for preview based on the post autosave. |
|
856 |
* |
|
857 |
* @since 2.7.0 |
|
858 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
* @param WP_Post $post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
861 |
* @return WP_Post|false |
0 | 862 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
863 |
function _set_preview( $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
864 |
if ( ! is_object( $post ) ) { |
0 | 865 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
} |
0 | 867 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
$preview = wp_get_post_autosave( $post->ID ); |
19 | 869 |
|
870 |
if ( is_object( $preview ) ) { |
|
871 |
$preview = sanitize_post( $preview ); |
|
0 | 872 |
|
19 | 873 |
$post->post_content = $preview->post_content; |
874 |
$post->post_title = $preview->post_title; |
|
875 |
$post->post_excerpt = $preview->post_excerpt; |
|
876 |
} |
|
0 | 877 |
|
878 |
add_filter( 'get_the_terms', '_wp_preview_terms_filter', 10, 3 ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
879 |
add_filter( 'get_post_metadata', '_wp_preview_post_thumbnail_filter', 10, 3 ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
880 |
add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); |
0 | 881 |
|
882 |
return $post; |
|
883 |
} |
|
884 |
||
885 |
/** |
|
886 |
* Filters the latest content for preview from the post autosave. |
|
887 |
* |
|
888 |
* @since 2.7.0 |
|
889 |
* @access private |
|
890 |
*/ |
|
891 |
function _show_post_preview() { |
|
9 | 892 |
if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) { |
0 | 893 |
$id = (int) $_GET['preview_id']; |
894 |
||
9 | 895 |
if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) { |
896 |
wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 ); |
|
897 |
} |
|
0 | 898 |
|
9 | 899 |
add_filter( 'the_preview', '_set_preview' ); |
0 | 900 |
} |
901 |
} |
|
902 |
||
903 |
/** |
|
904 |
* Filters terms lookup to set the post format. |
|
905 |
* |
|
906 |
* @since 3.6.0 |
|
907 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
908 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
909 |
* @param array $terms |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
910 |
* @param int $post_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
911 |
* @param string $taxonomy |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
912 |
* @return array |
0 | 913 |
*/ |
914 |
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { |
|
16 | 915 |
$post = get_post(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
916 |
|
16 | 917 |
if ( ! $post ) { |
0 | 918 |
return $terms; |
9 | 919 |
} |
0 | 920 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
921 |
if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id |
16 | 922 |
|| 'post_format' !== $taxonomy || 'revision' === $post->post_type |
923 |
) { |
|
0 | 924 |
return $terms; |
9 | 925 |
} |
0 | 926 |
|
16 | 927 |
if ( 'standard' === $_REQUEST['post_format'] ) { |
0 | 928 |
$terms = array(); |
16 | 929 |
} else { |
930 |
$term = get_term_by( 'slug', 'post-format-' . sanitize_key( $_REQUEST['post_format'] ), 'post_format' ); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
931 |
|
16 | 932 |
if ( $term ) { |
933 |
$terms = array( $term ); // Can only have one post format. |
|
934 |
} |
|
9 | 935 |
} |
0 | 936 |
|
937 |
return $terms; |
|
938 |
} |
|
939 |
||
940 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
* Filters post thumbnail lookup to set the post thumbnail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
942 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
943 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
944 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
945 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
946 |
* @param null|array|string $value The value to return - a single metadata value, or an array of values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
947 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
948 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
949 |
* @return null|array The default return value or the post thumbnail meta array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
950 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
951 |
function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) { |
16 | 952 |
$post = get_post(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
953 |
|
16 | 954 |
if ( ! $post ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
957 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
958 |
if ( empty( $_REQUEST['_thumbnail_id'] ) || empty( $_REQUEST['preview_id'] ) |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
959 |
|| $post->ID !== $post_id || $post_id !== (int) $_REQUEST['preview_id'] |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
960 |
|| '_thumbnail_id' !== $meta_key || 'revision' === $post->post_type |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
961 |
) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
963 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
|
18 | 965 |
$thumbnail_id = (int) $_REQUEST['_thumbnail_id']; |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
966 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
967 |
if ( $thumbnail_id <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
968 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
969 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
970 |
|
18 | 971 |
return (string) $thumbnail_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
/** |
0 | 975 |
* Gets the post revision version. |
976 |
* |
|
977 |
* @since 3.6.0 |
|
978 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
980 |
* @param WP_Post $revision |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
981 |
* @return int|false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
*/ |
0 | 983 |
function _wp_get_post_revision_version( $revision ) { |
9 | 984 |
if ( is_object( $revision ) ) { |
0 | 985 |
$revision = get_object_vars( $revision ); |
9 | 986 |
} elseif ( ! is_array( $revision ) ) { |
0 | 987 |
return false; |
9 | 988 |
} |
0 | 989 |
|
9 | 990 |
if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) { |
0 | 991 |
return (int) $matches[1]; |
9 | 992 |
} |
0 | 993 |
|
994 |
return 0; |
|
995 |
} |
|
996 |
||
997 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
998 |
* Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1. |
0 | 999 |
* |
1000 |
* @since 3.6.0 |
|
1001 |
* @access private |
|
1002 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1003 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1005 |
* @param WP_Post $post Post object. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1006 |
* @param array $revisions Current revisions of the post. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1007 |
* @return bool true if the revisions were upgraded, false if problems. |
0 | 1008 |
*/ |
1009 |
function _wp_upgrade_revisions_of_post( $post, $revisions ) { |
|
1010 |
global $wpdb; |
|
1011 |
||
16 | 1012 |
// Add post option exclusively. |
9 | 1013 |
$lock = "revision-upgrade-{$post->ID}"; |
1014 |
$now = time(); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1015 |
$result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, 'off') /* LOCK */", $lock, $now ) ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1016 |
|
0 | 1017 |
if ( ! $result ) { |
16 | 1018 |
// If we couldn't get a lock, see how old the previous lock is. |
0 | 1019 |
$locked = get_option( $lock ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1020 |
|
0 | 1021 |
if ( ! $locked ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1022 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1023 |
* Can't write to the lock, and can't read the lock. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1024 |
* Something broken has happened. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1025 |
*/ |
0 | 1026 |
return false; |
1027 |
} |
|
1028 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1029 |
if ( $locked > $now - HOUR_IN_SECONDS ) { |
16 | 1030 |
// Lock is not too old: some other process may be upgrading this post. Bail. |
0 | 1031 |
return false; |
1032 |
} |
|
1033 |
||
16 | 1034 |
// Lock is too old - update it (below) and continue. |
0 | 1035 |
} |
1036 |
||
1037 |
// If we could get a lock, re-"add" the option to fire all the correct filters. |
|
1038 |
update_option( $lock, $now ); |
|
1039 |
||
1040 |
reset( $revisions ); |
|
1041 |
$add_last = true; |
|
1042 |
||
1043 |
do { |
|
1044 |
$this_revision = current( $revisions ); |
|
1045 |
$prev_revision = next( $revisions ); |
|
1046 |
||
1047 |
$this_revision_version = _wp_get_post_revision_version( $this_revision ); |
|
1048 |
||
16 | 1049 |
// Something terrible happened. |
9 | 1050 |
if ( false === $this_revision_version ) { |
0 | 1051 |
continue; |
9 | 1052 |
} |
0 | 1053 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1054 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1055 |
* 1 is the latest revision version, so we're already up to date. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1056 |
* No need to add a copy of the post as latest revision. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1057 |
*/ |
0 | 1058 |
if ( 0 < $this_revision_version ) { |
1059 |
$add_last = false; |
|
1060 |
continue; |
|
1061 |
} |
|
1062 |
||
16 | 1063 |
// Always update the revision version. |
0 | 1064 |
$update = array( |
1065 |
'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ), |
|
1066 |
); |
|
1067 |
||
16 | 1068 |
/* |
1069 |
* If this revision is the oldest revision of the post, i.e. no $prev_revision, |
|
1070 |
* the correct post_author is probably $post->post_author, but that's only a good guess. |
|
1071 |
* Update the revision version only and Leave the author as-is. |
|
1072 |
*/ |
|
0 | 1073 |
if ( $prev_revision ) { |
1074 |
$prev_revision_version = _wp_get_post_revision_version( $prev_revision ); |
|
1075 |
||
1076 |
// If the previous revision is already up to date, it no longer has the information we need :( |
|
9 | 1077 |
if ( $prev_revision_version < 1 ) { |
0 | 1078 |
$update['post_author'] = $prev_revision->post_author; |
9 | 1079 |
} |
0 | 1080 |
} |
1081 |
||
16 | 1082 |
// Upgrade this revision. |
0 | 1083 |
$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); |
1084 |
||
9 | 1085 |
if ( $result ) { |
0 | 1086 |
wp_cache_delete( $this_revision->ID, 'posts' ); |
9 | 1087 |
} |
0 | 1088 |
} while ( $prev_revision ); |
1089 |
||
1090 |
delete_option( $lock ); |
|
1091 |
||
1092 |
// Add a copy of the post as latest revision. |
|
9 | 1093 |
if ( $add_last ) { |
0 | 1094 |
wp_save_post_revision( $post->ID ); |
9 | 1095 |
} |
0 | 1096 |
|
1097 |
return true; |
|
1098 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1099 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1100 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1101 |
* Filters preview post meta retrieval to get values from the autosave. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1102 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1103 |
* Filters revisioned meta keys only. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1104 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1105 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1106 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1107 |
* @param mixed $value Meta value to filter. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1108 |
* @param int $object_id Object ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1109 |
* @param string $meta_key Meta key to filter a value for. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1110 |
* @param bool $single Whether to return a single value. Default false. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1111 |
* @return mixed Original meta value if the meta key isn't revisioned, the object doesn't exist, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1112 |
* the post type is a revision or the post ID doesn't match the object ID. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1113 |
* Otherwise, the revisioned meta value is returned for the preview. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1114 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1115 |
function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1116 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1117 |
$post = get_post(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1118 |
if ( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1119 |
empty( $post ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1120 |
$post->ID !== $object_id || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1121 |
! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true ) || |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1122 |
'revision' === $post->post_type |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1123 |
) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1124 |
return $value; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1125 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1126 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1127 |
$preview = wp_get_post_autosave( $post->ID ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1128 |
if ( false === $preview ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1129 |
return $value; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1130 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1131 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1132 |
return get_post_meta( $preview->ID, $meta_key, $single ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1133 |
} |