author | ymh <ymh.work@gmail.com> |
Mon, 08 Sep 2025 19:44:41 +0200 | |
changeset 23 | 417f20492bf7 |
parent 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 |
* |
|
273 |
* @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
|
274 |
* @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
|
275 |
* @return WP_Post|false The autosaved data or false on failure or when no autosave exists. |
0 | 276 |
*/ |
277 |
function wp_get_post_autosave( $post_id, $user_id = 0 ) { |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
278 |
$args = array( |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
279 |
'post_type' => 'revision', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
280 |
'post_status' => 'inherit', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
281 |
'post_parent' => $post_id, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
282 |
'name' => $post_id . '-autosave-v1', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
283 |
'posts_per_page' => 1, |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
284 |
'orderby' => 'date', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
285 |
'order' => 'DESC', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
286 |
'fields' => 'ids', |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
287 |
'no_found_rows' => true, |
16 | 288 |
); |
289 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
290 |
if ( 0 !== $user_id ) { |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
291 |
$args['author'] = $user_id; |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
292 |
} |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
293 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
294 |
$query = new WP_Query( $args ); |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
295 |
|
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
296 |
if ( ! $query->have_posts() ) { |
16 | 297 |
return false; |
0 | 298 |
} |
299 |
||
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
300 |
return get_post( $query->posts[0] ); |
0 | 301 |
} |
302 |
||
303 |
/** |
|
304 |
* Determines if the specified post is a revision. |
|
305 |
* |
|
306 |
* @since 2.6.0 |
|
307 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
308 |
* @param int|WP_Post $post Post ID or post object. |
16 | 309 |
* @return int|false ID of revision's parent on success, false if not a revision. |
0 | 310 |
*/ |
311 |
function wp_is_post_revision( $post ) { |
|
16 | 312 |
$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
|
313 |
|
16 | 314 |
if ( ! $post ) { |
0 | 315 |
return false; |
9 | 316 |
} |
0 | 317 |
|
318 |
return (int) $post->post_parent; |
|
319 |
} |
|
320 |
||
321 |
/** |
|
322 |
* Determines if the specified post is an autosave. |
|
323 |
* |
|
324 |
* @since 2.6.0 |
|
325 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
* @param int|WP_Post $post Post ID or post object. |
16 | 327 |
* @return int|false ID of autosave's parent on success, false if not a revision. |
0 | 328 |
*/ |
329 |
function wp_is_post_autosave( $post ) { |
|
16 | 330 |
$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
|
331 |
|
16 | 332 |
if ( ! $post ) { |
0 | 333 |
return false; |
9 | 334 |
} |
0 | 335 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
336 |
if ( str_contains( $post->post_name, "{$post->post_parent}-autosave" ) ) { |
0 | 337 |
return (int) $post->post_parent; |
9 | 338 |
} |
0 | 339 |
|
340 |
return false; |
|
341 |
} |
|
342 |
||
343 |
/** |
|
344 |
* Inserts post data into the posts table as a post revision. |
|
345 |
* |
|
346 |
* @since 2.6.0 |
|
347 |
* @access private |
|
348 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* @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
|
350 |
* @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
|
351 |
* Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
* @return int|WP_Error WP_Error or 0 if error, new revision ID if success. |
0 | 353 |
*/ |
354 |
function _wp_put_post_revision( $post = null, $autosave = false ) { |
|
9 | 355 |
if ( is_object( $post ) ) { |
0 | 356 |
$post = get_object_vars( $post ); |
9 | 357 |
} elseif ( ! is_array( $post ) ) { |
358 |
$post = get_post( $post, ARRAY_A ); |
|
359 |
} |
|
0 | 360 |
|
9 | 361 |
if ( ! $post || empty( $post['ID'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
9 | 363 |
} |
0 | 364 |
|
16 | 365 |
if ( isset( $post['post_type'] ) && 'revision' === $post['post_type'] ) { |
0 | 366 |
return new WP_Error( 'post_type', __( 'Cannot create a revision of a revision' ) ); |
9 | 367 |
} |
0 | 368 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
$post = _wp_post_revision_data( $post, $autosave ); |
16 | 370 |
$post = wp_slash( $post ); // Since data is from DB. |
0 | 371 |
|
18 | 372 |
$revision_id = wp_insert_post( $post, true ); |
9 | 373 |
if ( is_wp_error( $revision_id ) ) { |
0 | 374 |
return $revision_id; |
9 | 375 |
} |
0 | 376 |
|
5 | 377 |
if ( $revision_id ) { |
378 |
/** |
|
379 |
* Fires once a revision has been saved. |
|
380 |
* |
|
381 |
* @since 2.6.0 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
382 |
* @since 6.4.0 The post_id parameter was added. |
5 | 383 |
* |
384 |
* @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
|
385 |
* @param int $post_id Post ID. |
5 | 386 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
387 |
do_action( '_wp_put_post_revision', $revision_id, $post['post_parent'] ); |
5 | 388 |
} |
0 | 389 |
|
390 |
return $revision_id; |
|
391 |
} |
|
392 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
393 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
394 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
395 |
* Save the revisioned meta fields. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
396 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
397 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
398 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
399 |
* @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
|
400 |
* @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
|
401 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
402 |
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
|
403 |
$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
|
404 |
if ( ! $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
405 |
return; |
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 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
408 |
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
|
409 |
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
|
410 |
_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
|
411 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
412 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
413 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
414 |
|
0 | 415 |
/** |
416 |
* Gets a post revision. |
|
417 |
* |
|
418 |
* @since 2.6.0 |
|
419 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
420 |
* @param int|WP_Post $post Post ID or post object. |
16 | 421 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
422 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
423 |
* respectively. Default OBJECT. |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
424 |
* @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
|
425 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 426 |
*/ |
9 | 427 |
function wp_get_post_revision( &$post, $output = OBJECT, $filter = 'raw' ) { |
16 | 428 |
$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
|
429 |
|
16 | 430 |
if ( ! $revision ) { |
0 | 431 |
return $revision; |
9 | 432 |
} |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
433 |
|
9 | 434 |
if ( 'revision' !== $revision->post_type ) { |
5 | 435 |
return null; |
9 | 436 |
} |
0 | 437 |
|
18 | 438 |
if ( OBJECT === $output ) { |
0 | 439 |
return $revision; |
18 | 440 |
} elseif ( ARRAY_A === $output ) { |
9 | 441 |
$_revision = get_object_vars( $revision ); |
0 | 442 |
return $_revision; |
18 | 443 |
} elseif ( ARRAY_N === $output ) { |
9 | 444 |
$_revision = array_values( get_object_vars( $revision ) ); |
0 | 445 |
return $_revision; |
446 |
} |
|
447 |
||
448 |
return $revision; |
|
449 |
} |
|
450 |
||
451 |
/** |
|
452 |
* Restores a post to the specified revision. |
|
453 |
* |
|
454 |
* Can restore a past revision using all fields of the post revision, or only selected fields. |
|
455 |
* |
|
456 |
* @since 2.6.0 |
|
457 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
458 |
* @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
|
459 |
* @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
|
460 |
* @return int|false|null Null if error, false if no fields to restore, (int) post ID if success. |
0 | 461 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
462 |
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
|
463 |
$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
|
464 |
|
16 | 465 |
if ( ! $revision ) { |
0 | 466 |
return $revision; |
9 | 467 |
} |
0 | 468 |
|
9 | 469 |
if ( ! is_array( $fields ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
470 |
$fields = array_keys( _wp_post_revision_fields( $revision ) ); |
9 | 471 |
} |
0 | 472 |
|
473 |
$update = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
474 |
foreach ( array_intersect( array_keys( $revision ), $fields ) as $field ) { |
9 | 475 |
$update[ $field ] = $revision[ $field ]; |
0 | 476 |
} |
477 |
||
9 | 478 |
if ( ! $update ) { |
0 | 479 |
return false; |
9 | 480 |
} |
0 | 481 |
|
482 |
$update['ID'] = $revision['post_parent']; |
|
483 |
||
16 | 484 |
$update = wp_slash( $update ); // Since data is from DB. |
0 | 485 |
|
486 |
$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
|
487 |
|
9 | 488 |
if ( ! $post_id || is_wp_error( $post_id ) ) { |
0 | 489 |
return $post_id; |
9 | 490 |
} |
0 | 491 |
|
16 | 492 |
// Update last edit user. |
0 | 493 |
update_post_meta( $post_id, '_edit_last', get_current_user_id() ); |
494 |
||
5 | 495 |
/** |
496 |
* Fires after a post revision has been restored. |
|
497 |
* |
|
498 |
* @since 2.6.0 |
|
499 |
* |
|
500 |
* @param int $post_id Post ID. |
|
501 |
* @param int $revision_id Post revision ID. |
|
502 |
*/ |
|
0 | 503 |
do_action( 'wp_restore_post_revision', $post_id, $revision['ID'] ); |
504 |
||
505 |
return $post_id; |
|
506 |
} |
|
507 |
||
508 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
509 |
* 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
|
510 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
511 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
512 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
513 |
* @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
|
514 |
* @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
|
515 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
516 |
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
|
517 |
$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
|
518 |
if ( ! $post_type ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
519 |
return; |
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 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
522 |
// Restore revisioned meta fields. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
523 |
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
|
524 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
525 |
// Clear any existing meta. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
526 |
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
|
527 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
528 |
_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
|
529 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
530 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
531 |
|
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 |
* 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
|
534 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
535 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
536 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
537 |
* @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
|
538 |
* @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
|
539 |
* @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
|
540 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
541 |
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
|
542 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
543 |
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
|
544 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
545 |
* 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
|
546 |
* 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
|
547 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
548 |
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
|
549 |
} |
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 |
|
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 |
* 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
|
554 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
555 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
556 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
557 |
* @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
|
558 |
* @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
|
559 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
560 |
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
|
561 |
$registered_meta = array_merge( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
562 |
get_registered_meta_keys( 'post' ), |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
563 |
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
|
564 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
565 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
566 |
$wp_revisioned_meta_keys = array(); |
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 |
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
|
569 |
if ( $args['revisions_enabled'] ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
570 |
$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
|
571 |
} |
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_keys( $wp_revisioned_meta_keys ); |
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 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
577 |
* 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
|
578 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
579 |
* @since 6.4.0 |
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 |
* @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
|
582 |
* @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
|
583 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
584 |
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
|
585 |
} |
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 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
588 |
* 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
|
589 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
590 |
* @since 6.4.0 |
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 |
* @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
|
593 |
* @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
|
594 |
* @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
|
595 |
* @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
|
596 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
$post_has_changed = true; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
601 |
break; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
602 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
603 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
604 |
return $post_has_changed; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
605 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
606 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
607 |
/** |
0 | 608 |
* Deletes a revision. |
609 |
* |
|
610 |
* Deletes the row from the posts table corresponding to the specified revision. |
|
611 |
* |
|
612 |
* @since 2.6.0 |
|
613 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
614 |
* @param int|WP_Post $revision Revision ID or revision object. |
19 | 615 |
* @return WP_Post|false|null Null or false if error, deleted post object if success. |
0 | 616 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
617 |
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
|
618 |
$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
|
619 |
|
16 | 620 |
if ( ! $revision ) { |
0 | 621 |
return $revision; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
622 |
} |
0 | 623 |
|
624 |
$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
|
625 |
|
5 | 626 |
if ( $delete ) { |
627 |
/** |
|
628 |
* Fires once a post revision has been deleted. |
|
629 |
* |
|
630 |
* @since 2.6.0 |
|
631 |
* |
|
16 | 632 |
* @param int $revision_id Post revision ID. |
633 |
* @param WP_Post $revision Post revision object. |
|
5 | 634 |
*/ |
0 | 635 |
do_action( 'wp_delete_post_revision', $revision->ID, $revision ); |
5 | 636 |
} |
0 | 637 |
|
638 |
return $delete; |
|
639 |
} |
|
640 |
||
641 |
/** |
|
642 |
* Returns all revisions of specified post. |
|
643 |
* |
|
644 |
* @since 2.6.0 |
|
645 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
646 |
* @see get_children() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
647 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
648 |
* @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
|
649 |
* @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
|
650 |
* @return WP_Post[]|int[] Array of revision objects or IDs, or an empty array if none. |
0 | 651 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
652 |
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
|
653 |
$post = get_post( $post ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
654 |
|
9 | 655 |
if ( ! $post || empty( $post->ID ) ) { |
0 | 656 |
return array(); |
9 | 657 |
} |
0 | 658 |
|
9 | 659 |
$defaults = array( |
660 |
'order' => 'DESC', |
|
661 |
'orderby' => 'date ID', |
|
662 |
'check_enabled' => true, |
|
663 |
); |
|
664 |
$args = wp_parse_args( $args, $defaults ); |
|
0 | 665 |
|
9 | 666 |
if ( $args['check_enabled'] && ! wp_revisions_enabled( $post ) ) { |
0 | 667 |
return array(); |
9 | 668 |
} |
0 | 669 |
|
9 | 670 |
$args = array_merge( |
671 |
$args, |
|
672 |
array( |
|
673 |
'post_parent' => $post->ID, |
|
674 |
'post_type' => 'revision', |
|
675 |
'post_status' => 'inherit', |
|
676 |
) |
|
677 |
); |
|
0 | 678 |
|
16 | 679 |
$revisions = get_children( $args ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
680 |
|
16 | 681 |
if ( ! $revisions ) { |
0 | 682 |
return array(); |
9 | 683 |
} |
0 | 684 |
|
685 |
return $revisions; |
|
686 |
} |
|
687 |
||
688 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
689 |
* 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
|
690 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
691 |
* @since 6.1.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
692 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
693 |
* @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
|
694 |
* @return array|WP_Error { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
695 |
* 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
|
696 |
* 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
|
697 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
698 |
* @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
|
699 |
* @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
|
700 |
* } |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
701 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
702 |
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
|
703 |
$post = get_post( $post ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
704 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
705 |
if ( ! $post ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
706 |
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
|
707 |
} |
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 |
if ( ! wp_revisions_enabled( $post ) ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
710 |
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
|
711 |
} |
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 |
$args = array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
714 |
'post_parent' => $post->ID, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
715 |
'fields' => 'ids', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
716 |
'post_type' => 'revision', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
717 |
'post_status' => 'inherit', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
718 |
'order' => 'DESC', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
719 |
'orderby' => 'date ID', |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
720 |
'posts_per_page' => 1, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
721 |
'ignore_sticky_posts' => true, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
722 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
723 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
724 |
$revision_query = new WP_Query(); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
725 |
$revisions = $revision_query->query( $args ); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
726 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
727 |
if ( ! $revisions ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
728 |
return array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
729 |
'latest_id' => 0, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
730 |
'count' => 0, |
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 |
} |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
733 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
734 |
return array( |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
735 |
'latest_id' => $revisions[0], |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
736 |
'count' => $revision_query->found_posts, |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
737 |
); |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
738 |
} |
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 |
/** |
19 | 741 |
* Returns the url for viewing and potentially restoring revisions of a given post. |
742 |
* |
|
743 |
* @since 5.9.0 |
|
744 |
* |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
745 |
* @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
|
746 |
* @return string|null The URL for editing revisions on the given post, otherwise null. |
19 | 747 |
*/ |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
748 |
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
|
749 |
$post = get_post( $post ); |
19 | 750 |
|
751 |
if ( ! $post instanceof WP_Post ) { |
|
752 |
return null; |
|
753 |
} |
|
754 |
||
755 |
// If the post is a revision, return early. |
|
756 |
if ( 'revision' === $post->post_type ) { |
|
757 |
return get_edit_post_link( $post ); |
|
758 |
} |
|
759 |
||
760 |
if ( ! wp_revisions_enabled( $post ) ) { |
|
761 |
return null; |
|
762 |
} |
|
763 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
764 |
$revisions = wp_get_latest_revision_id_and_total_count( $post->ID ); |
19 | 765 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
766 |
if ( is_wp_error( $revisions ) || 0 === $revisions['count'] ) { |
19 | 767 |
return null; |
768 |
} |
|
769 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
770 |
return get_edit_post_link( $revisions['latest_id'] ); |
19 | 771 |
} |
772 |
||
773 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
774 |
* Determines whether revisions are enabled for a given post. |
0 | 775 |
* |
776 |
* @since 3.6.0 |
|
777 |
* |
|
5 | 778 |
* @param WP_Post $post The post object. |
0 | 779 |
* @return bool True if number of revisions to keep isn't zero, false otherwise. |
780 |
*/ |
|
781 |
function wp_revisions_enabled( $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
return wp_revisions_to_keep( $post ) !== 0; |
0 | 783 |
} |
784 |
||
785 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
786 |
* Determines how many revisions to retain for a given post. |
5 | 787 |
* |
788 |
* By default, an infinite number of revisions are kept. |
|
789 |
* |
|
790 |
* The constant WP_POST_REVISIONS can be set in wp-config to specify the limit |
|
791 |
* of revisions to keep. |
|
0 | 792 |
* |
793 |
* @since 3.6.0 |
|
794 |
* |
|
5 | 795 |
* @param WP_Post $post The post object. |
0 | 796 |
* @return int The number of revisions to keep. |
797 |
*/ |
|
798 |
function wp_revisions_to_keep( $post ) { |
|
799 |
$num = WP_POST_REVISIONS; |
|
800 |
||
9 | 801 |
if ( true === $num ) { |
0 | 802 |
$num = -1; |
9 | 803 |
} else { |
18 | 804 |
$num = (int) $num; |
9 | 805 |
} |
0 | 806 |
|
9 | 807 |
if ( ! post_type_supports( $post->post_type, 'revisions' ) ) { |
0 | 808 |
$num = 0; |
9 | 809 |
} |
0 | 810 |
|
5 | 811 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
812 |
* Filters the number of revisions to save for the given post. |
5 | 813 |
* |
814 |
* Overrides the value of WP_POST_REVISIONS. |
|
815 |
* |
|
816 |
* @since 3.6.0 |
|
817 |
* |
|
818 |
* @param int $num Number of revisions to store. |
|
819 |
* @param WP_Post $post Post object. |
|
820 |
*/ |
|
18 | 821 |
$num = apply_filters( 'wp_revisions_to_keep', $num, $post ); |
822 |
||
823 |
/** |
|
824 |
* Filters the number of revisions to save for the given post by its post type. |
|
825 |
* |
|
826 |
* Overrides both the value of WP_POST_REVISIONS and the {@see 'wp_revisions_to_keep'} filter. |
|
827 |
* |
|
828 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
|
829 |
* the post type slug. |
|
830 |
* |
|
19 | 831 |
* Possible hook names include: |
832 |
* |
|
833 |
* - `wp_post_revisions_to_keep` |
|
834 |
* - `wp_page_revisions_to_keep` |
|
835 |
* |
|
18 | 836 |
* @since 5.8.0 |
837 |
* |
|
838 |
* @param int $num Number of revisions to store. |
|
839 |
* @param WP_Post $post Post object. |
|
840 |
*/ |
|
841 |
$num = apply_filters( "wp_{$post->post_type}_revisions_to_keep", $num, $post ); |
|
842 |
||
843 |
return (int) $num; |
|
0 | 844 |
} |
845 |
||
846 |
/** |
|
847 |
* Sets up the post object for preview based on the post autosave. |
|
848 |
* |
|
849 |
* @since 2.7.0 |
|
850 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
851 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
852 |
* @param WP_Post $post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
853 |
* @return WP_Post|false |
0 | 854 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
855 |
function _set_preview( $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
if ( ! is_object( $post ) ) { |
0 | 857 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
858 |
} |
0 | 859 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
860 |
$preview = wp_get_post_autosave( $post->ID ); |
19 | 861 |
|
862 |
if ( is_object( $preview ) ) { |
|
863 |
$preview = sanitize_post( $preview ); |
|
0 | 864 |
|
19 | 865 |
$post->post_content = $preview->post_content; |
866 |
$post->post_title = $preview->post_title; |
|
867 |
$post->post_excerpt = $preview->post_excerpt; |
|
868 |
} |
|
0 | 869 |
|
870 |
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
|
871 |
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
|
872 |
add_filter( 'get_post_metadata', '_wp_preview_meta_filter', 10, 4 ); |
0 | 873 |
|
874 |
return $post; |
|
875 |
} |
|
876 |
||
877 |
/** |
|
878 |
* Filters the latest content for preview from the post autosave. |
|
879 |
* |
|
880 |
* @since 2.7.0 |
|
881 |
* @access private |
|
882 |
*/ |
|
883 |
function _show_post_preview() { |
|
9 | 884 |
if ( isset( $_GET['preview_id'] ) && isset( $_GET['preview_nonce'] ) ) { |
0 | 885 |
$id = (int) $_GET['preview_id']; |
886 |
||
9 | 887 |
if ( false === wp_verify_nonce( $_GET['preview_nonce'], 'post_preview_' . $id ) ) { |
888 |
wp_die( __( 'Sorry, you are not allowed to preview drafts.' ), 403 ); |
|
889 |
} |
|
0 | 890 |
|
9 | 891 |
add_filter( 'the_preview', '_set_preview' ); |
0 | 892 |
} |
893 |
} |
|
894 |
||
895 |
/** |
|
896 |
* Filters terms lookup to set the post format. |
|
897 |
* |
|
898 |
* @since 3.6.0 |
|
899 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
900 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
901 |
* @param array $terms |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
902 |
* @param int $post_id |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
903 |
* @param string $taxonomy |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
904 |
* @return array |
0 | 905 |
*/ |
906 |
function _wp_preview_terms_filter( $terms, $post_id, $taxonomy ) { |
|
16 | 907 |
$post = get_post(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
908 |
|
16 | 909 |
if ( ! $post ) { |
0 | 910 |
return $terms; |
9 | 911 |
} |
0 | 912 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
913 |
if ( empty( $_REQUEST['post_format'] ) || $post->ID !== $post_id |
16 | 914 |
|| 'post_format' !== $taxonomy || 'revision' === $post->post_type |
915 |
) { |
|
0 | 916 |
return $terms; |
9 | 917 |
} |
0 | 918 |
|
16 | 919 |
if ( 'standard' === $_REQUEST['post_format'] ) { |
0 | 920 |
$terms = array(); |
16 | 921 |
} else { |
922 |
$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
|
923 |
|
16 | 924 |
if ( $term ) { |
925 |
$terms = array( $term ); // Can only have one post format. |
|
926 |
} |
|
9 | 927 |
} |
0 | 928 |
|
929 |
return $terms; |
|
930 |
} |
|
931 |
||
932 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
* Filters post thumbnail lookup to set the post thumbnail. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
934 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
936 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
937 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
938 |
* @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
|
939 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
940 |
* @param string $meta_key Meta key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
941 |
* @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
|
942 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
943 |
function _wp_preview_post_thumbnail_filter( $value, $post_id, $meta_key ) { |
16 | 944 |
$post = get_post(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
945 |
|
16 | 946 |
if ( ! $post ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
947 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
948 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
949 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
950 |
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
|
951 |
|| $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
|
952 |
|| '_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
|
953 |
) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
954 |
return $value; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
955 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
956 |
|
18 | 957 |
$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
|
958 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
959 |
if ( $thumbnail_id <= 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
960 |
return ''; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
961 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
962 |
|
18 | 963 |
return (string) $thumbnail_id; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
964 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
965 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
966 |
/** |
0 | 967 |
* Gets the post revision version. |
968 |
* |
|
969 |
* @since 3.6.0 |
|
970 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
971 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
* @param WP_Post $revision |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
* @return int|false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
974 |
*/ |
0 | 975 |
function _wp_get_post_revision_version( $revision ) { |
9 | 976 |
if ( is_object( $revision ) ) { |
0 | 977 |
$revision = get_object_vars( $revision ); |
9 | 978 |
} elseif ( ! is_array( $revision ) ) { |
0 | 979 |
return false; |
9 | 980 |
} |
0 | 981 |
|
9 | 982 |
if ( preg_match( '/^\d+-(?:autosave|revision)-v(\d+)$/', $revision['post_name'], $matches ) ) { |
0 | 983 |
return (int) $matches[1]; |
9 | 984 |
} |
0 | 985 |
|
986 |
return 0; |
|
987 |
} |
|
988 |
||
989 |
/** |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
990 |
* Upgrades the revisions author, adds the current post as a revision and sets the revisions version to 1. |
0 | 991 |
* |
992 |
* @since 3.6.0 |
|
993 |
* @access private |
|
994 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
995 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
996 |
* |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
997 |
* @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
|
998 |
* @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
|
999 |
* @return bool true if the revisions were upgraded, false if problems. |
0 | 1000 |
*/ |
1001 |
function _wp_upgrade_revisions_of_post( $post, $revisions ) { |
|
1002 |
global $wpdb; |
|
1003 |
||
16 | 1004 |
// Add post option exclusively. |
9 | 1005 |
$lock = "revision-upgrade-{$post->ID}"; |
1006 |
$now = time(); |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1007 |
$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
|
1008 |
|
0 | 1009 |
if ( ! $result ) { |
16 | 1010 |
// If we couldn't get a lock, see how old the previous lock is. |
0 | 1011 |
$locked = get_option( $lock ); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1012 |
|
0 | 1013 |
if ( ! $locked ) { |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1014 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1015 |
* 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
|
1016 |
* Something broken has happened. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1017 |
*/ |
0 | 1018 |
return false; |
1019 |
} |
|
1020 |
||
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1021 |
if ( $locked > $now - HOUR_IN_SECONDS ) { |
16 | 1022 |
// Lock is not too old: some other process may be upgrading this post. Bail. |
0 | 1023 |
return false; |
1024 |
} |
|
1025 |
||
16 | 1026 |
// Lock is too old - update it (below) and continue. |
0 | 1027 |
} |
1028 |
||
1029 |
// If we could get a lock, re-"add" the option to fire all the correct filters. |
|
1030 |
update_option( $lock, $now ); |
|
1031 |
||
1032 |
reset( $revisions ); |
|
1033 |
$add_last = true; |
|
1034 |
||
1035 |
do { |
|
1036 |
$this_revision = current( $revisions ); |
|
1037 |
$prev_revision = next( $revisions ); |
|
1038 |
||
1039 |
$this_revision_version = _wp_get_post_revision_version( $this_revision ); |
|
1040 |
||
16 | 1041 |
// Something terrible happened. |
9 | 1042 |
if ( false === $this_revision_version ) { |
0 | 1043 |
continue; |
9 | 1044 |
} |
0 | 1045 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1046 |
/* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1047 |
* 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
|
1048 |
* 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
|
1049 |
*/ |
0 | 1050 |
if ( 0 < $this_revision_version ) { |
1051 |
$add_last = false; |
|
1052 |
continue; |
|
1053 |
} |
|
1054 |
||
16 | 1055 |
// Always update the revision version. |
0 | 1056 |
$update = array( |
1057 |
'post_name' => preg_replace( '/^(\d+-(?:autosave|revision))[\d-]*$/', '$1-v1', $this_revision->post_name ), |
|
1058 |
); |
|
1059 |
||
16 | 1060 |
/* |
1061 |
* If this revision is the oldest revision of the post, i.e. no $prev_revision, |
|
1062 |
* the correct post_author is probably $post->post_author, but that's only a good guess. |
|
1063 |
* Update the revision version only and Leave the author as-is. |
|
1064 |
*/ |
|
0 | 1065 |
if ( $prev_revision ) { |
1066 |
$prev_revision_version = _wp_get_post_revision_version( $prev_revision ); |
|
1067 |
||
1068 |
// If the previous revision is already up to date, it no longer has the information we need :( |
|
9 | 1069 |
if ( $prev_revision_version < 1 ) { |
0 | 1070 |
$update['post_author'] = $prev_revision->post_author; |
9 | 1071 |
} |
0 | 1072 |
} |
1073 |
||
16 | 1074 |
// Upgrade this revision. |
0 | 1075 |
$result = $wpdb->update( $wpdb->posts, $update, array( 'ID' => $this_revision->ID ) ); |
1076 |
||
9 | 1077 |
if ( $result ) { |
0 | 1078 |
wp_cache_delete( $this_revision->ID, 'posts' ); |
9 | 1079 |
} |
0 | 1080 |
} while ( $prev_revision ); |
1081 |
||
1082 |
delete_option( $lock ); |
|
1083 |
||
1084 |
// Add a copy of the post as latest revision. |
|
9 | 1085 |
if ( $add_last ) { |
0 | 1086 |
wp_save_post_revision( $post->ID ); |
9 | 1087 |
} |
0 | 1088 |
|
1089 |
return true; |
|
1090 |
} |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1091 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1092 |
/** |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1093 |
* 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
|
1094 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1095 |
* Filters revisioned meta keys only. |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1096 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1097 |
* @since 6.4.0 |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1098 |
* |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1099 |
* @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
|
1100 |
* @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
|
1101 |
* @param string $meta_key Meta key to filter a value for. |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1102 |
* @param bool $single Whether to return a single value. |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1103 |
* @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
|
1104 |
* 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
|
1105 |
* 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
|
1106 |
*/ |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1107 |
function _wp_preview_meta_filter( $value, $object_id, $meta_key, $single ) { |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1108 |
$post = get_post(); |
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1109 |
|
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1110 |
if ( empty( $post ) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1111 |
|| $post->ID !== $object_id |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1112 |
|| ! in_array( $meta_key, wp_post_revision_meta_keys( $post->post_type ), true ) |
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1113 |
|| 'revision' === $post->post_type |
21
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 |
return $value; |
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 |
|
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1118 |
$preview = wp_get_post_autosave( $post->ID ); |
22
8c2e4d02f4ef
Update WordPress to latest version (6.7)
ymh <ymh.work@gmail.com>
parents:
21
diff
changeset
|
1119 |
|
21
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1120 |
if ( false === $preview ) { |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1121 |
return $value; |
48c4eec2b7e6
Add CLAUDE.md documentation and sync WordPress core files
ymh <ymh.work@gmail.com>
parents:
19
diff
changeset
|
1122 |
} |
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 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
|
1125 |
} |