author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core Post API |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage Post |
|
7 |
*/ |
|
8 |
||
9 |
// |
|
16 | 10 |
// Post Type registration. |
0 | 11 |
// |
12 |
||
13 |
/** |
|
14 |
* Creates the initial post types when 'init' action is fired. |
|
15 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* See {@see 'init'}. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* |
0 | 18 |
* @since 2.9.0 |
19 |
*/ |
|
20 |
function create_initial_post_types() { |
|
19 | 21 |
WP_Post_Type::reset_default_labels(); |
22 |
||
9 | 23 |
register_post_type( |
24 |
'post', |
|
25 |
array( |
|
26 |
'labels' => array( |
|
27 |
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ), |
|
28 |
), |
|
29 |
'public' => true, |
|
30 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
31 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
32 |
'capability_type' => 'post', |
|
33 |
'map_meta_cap' => true, |
|
34 |
'menu_position' => 5, |
|
16 | 35 |
'menu_icon' => 'dashicons-admin-post', |
9 | 36 |
'hierarchical' => false, |
37 |
'rewrite' => false, |
|
38 |
'query_var' => false, |
|
39 |
'delete_with_user' => true, |
|
40 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), |
|
41 |
'show_in_rest' => true, |
|
42 |
'rest_base' => 'posts', |
|
43 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
44 |
) |
|
45 |
); |
|
46 |
||
47 |
register_post_type( |
|
48 |
'page', |
|
49 |
array( |
|
50 |
'labels' => array( |
|
51 |
'name_admin_bar' => _x( 'Page', 'add new from admin bar' ), |
|
52 |
), |
|
53 |
'public' => true, |
|
54 |
'publicly_queryable' => false, |
|
55 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
56 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
57 |
'capability_type' => 'page', |
|
58 |
'map_meta_cap' => true, |
|
59 |
'menu_position' => 20, |
|
16 | 60 |
'menu_icon' => 'dashicons-admin-page', |
9 | 61 |
'hierarchical' => true, |
62 |
'rewrite' => false, |
|
63 |
'query_var' => false, |
|
64 |
'delete_with_user' => true, |
|
65 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), |
|
66 |
'show_in_rest' => true, |
|
67 |
'rest_base' => 'pages', |
|
68 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
69 |
) |
|
70 |
); |
|
71 |
||
72 |
register_post_type( |
|
73 |
'attachment', |
|
74 |
array( |
|
75 |
'labels' => array( |
|
76 |
'name' => _x( 'Media', 'post type general name' ), |
|
77 |
'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), |
|
78 |
'add_new' => _x( 'Add New', 'add new media' ), |
|
79 |
'edit_item' => __( 'Edit Media' ), |
|
80 |
'view_item' => __( 'View Attachment Page' ), |
|
81 |
'attributes' => __( 'Attachment Attributes' ), |
|
82 |
), |
|
83 |
'public' => true, |
|
84 |
'show_ui' => true, |
|
85 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
86 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
87 |
'capability_type' => 'post', |
|
88 |
'capabilities' => array( |
|
89 |
'create_posts' => 'upload_files', |
|
90 |
), |
|
91 |
'map_meta_cap' => true, |
|
16 | 92 |
'menu_icon' => 'dashicons-admin-media', |
9 | 93 |
'hierarchical' => false, |
94 |
'rewrite' => false, |
|
95 |
'query_var' => false, |
|
96 |
'show_in_nav_menus' => false, |
|
97 |
'delete_with_user' => true, |
|
98 |
'supports' => array( 'title', 'author', 'comments' ), |
|
99 |
'show_in_rest' => true, |
|
100 |
'rest_base' => 'media', |
|
101 |
'rest_controller_class' => 'WP_REST_Attachments_Controller', |
|
102 |
) |
|
103 |
); |
|
5 | 104 |
add_post_type_support( 'attachment:audio', 'thumbnail' ); |
105 |
add_post_type_support( 'attachment:video', 'thumbnail' ); |
|
0 | 106 |
|
9 | 107 |
register_post_type( |
108 |
'revision', |
|
109 |
array( |
|
110 |
'labels' => array( |
|
111 |
'name' => __( 'Revisions' ), |
|
112 |
'singular_name' => __( 'Revision' ), |
|
113 |
), |
|
114 |
'public' => false, |
|
115 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
116 |
'_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ |
|
117 |
'capability_type' => 'post', |
|
118 |
'map_meta_cap' => true, |
|
119 |
'hierarchical' => false, |
|
120 |
'rewrite' => false, |
|
121 |
'query_var' => false, |
|
122 |
'can_export' => false, |
|
123 |
'delete_with_user' => true, |
|
124 |
'supports' => array( 'author' ), |
|
125 |
) |
|
126 |
); |
|
127 |
||
128 |
register_post_type( |
|
129 |
'nav_menu_item', |
|
130 |
array( |
|
19 | 131 |
'labels' => array( |
9 | 132 |
'name' => __( 'Navigation Menu Items' ), |
133 |
'singular_name' => __( 'Navigation Menu Item' ), |
|
134 |
), |
|
19 | 135 |
'public' => false, |
136 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
137 |
'hierarchical' => false, |
|
138 |
'rewrite' => false, |
|
139 |
'delete_with_user' => false, |
|
140 |
'query_var' => false, |
|
141 |
'map_meta_cap' => true, |
|
142 |
'capability_type' => array( 'edit_theme_options', 'edit_theme_options' ), |
|
143 |
'capabilities' => array( |
|
144 |
// Meta Capabilities. |
|
145 |
'edit_post' => 'edit_post', |
|
146 |
'read_post' => 'read_post', |
|
147 |
'delete_post' => 'delete_post', |
|
148 |
// Primitive Capabilities. |
|
149 |
'edit_posts' => 'edit_theme_options', |
|
150 |
'edit_others_posts' => 'edit_theme_options', |
|
151 |
'delete_posts' => 'edit_theme_options', |
|
152 |
'publish_posts' => 'edit_theme_options', |
|
153 |
'read_private_posts' => 'edit_theme_options', |
|
154 |
'read' => 'read', |
|
155 |
'delete_private_posts' => 'edit_theme_options', |
|
156 |
'delete_published_posts' => 'edit_theme_options', |
|
157 |
'delete_others_posts' => 'edit_theme_options', |
|
158 |
'edit_private_posts' => 'edit_theme_options', |
|
159 |
'edit_published_posts' => 'edit_theme_options', |
|
160 |
), |
|
161 |
'show_in_rest' => true, |
|
162 |
'rest_base' => 'menu-items', |
|
163 |
'rest_controller_class' => 'WP_REST_Menu_Items_Controller', |
|
9 | 164 |
) |
165 |
); |
|
166 |
||
167 |
register_post_type( |
|
168 |
'custom_css', |
|
169 |
array( |
|
170 |
'labels' => array( |
|
171 |
'name' => __( 'Custom CSS' ), |
|
172 |
'singular_name' => __( 'Custom CSS' ), |
|
173 |
), |
|
174 |
'public' => false, |
|
175 |
'hierarchical' => false, |
|
176 |
'rewrite' => false, |
|
177 |
'query_var' => false, |
|
178 |
'delete_with_user' => false, |
|
179 |
'can_export' => true, |
|
180 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
181 |
'supports' => array( 'title', 'revisions' ), |
|
182 |
'capabilities' => array( |
|
183 |
'delete_posts' => 'edit_theme_options', |
|
184 |
'delete_post' => 'edit_theme_options', |
|
185 |
'delete_published_posts' => 'edit_theme_options', |
|
186 |
'delete_private_posts' => 'edit_theme_options', |
|
187 |
'delete_others_posts' => 'edit_theme_options', |
|
188 |
'edit_post' => 'edit_css', |
|
189 |
'edit_posts' => 'edit_css', |
|
190 |
'edit_others_posts' => 'edit_css', |
|
191 |
'edit_published_posts' => 'edit_css', |
|
192 |
'read_post' => 'read', |
|
193 |
'read_private_posts' => 'read', |
|
194 |
'publish_posts' => 'edit_theme_options', |
|
195 |
), |
|
196 |
) |
|
197 |
); |
|
198 |
||
199 |
register_post_type( |
|
200 |
'customize_changeset', |
|
201 |
array( |
|
202 |
'labels' => array( |
|
203 |
'name' => _x( 'Changesets', 'post type general name' ), |
|
204 |
'singular_name' => _x( 'Changeset', 'post type singular name' ), |
|
205 |
'add_new' => _x( 'Add New', 'Customize Changeset' ), |
|
206 |
'add_new_item' => __( 'Add New Changeset' ), |
|
207 |
'new_item' => __( 'New Changeset' ), |
|
208 |
'edit_item' => __( 'Edit Changeset' ), |
|
209 |
'view_item' => __( 'View Changeset' ), |
|
210 |
'all_items' => __( 'All Changesets' ), |
|
211 |
'search_items' => __( 'Search Changesets' ), |
|
212 |
'not_found' => __( 'No changesets found.' ), |
|
213 |
'not_found_in_trash' => __( 'No changesets found in Trash.' ), |
|
214 |
), |
|
215 |
'public' => false, |
|
216 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
217 |
'map_meta_cap' => true, |
|
218 |
'hierarchical' => false, |
|
219 |
'rewrite' => false, |
|
220 |
'query_var' => false, |
|
221 |
'can_export' => false, |
|
222 |
'delete_with_user' => false, |
|
223 |
'supports' => array( 'title', 'author' ), |
|
224 |
'capability_type' => 'customize_changeset', |
|
225 |
'capabilities' => array( |
|
226 |
'create_posts' => 'customize', |
|
227 |
'delete_others_posts' => 'customize', |
|
228 |
'delete_post' => 'customize', |
|
229 |
'delete_posts' => 'customize', |
|
230 |
'delete_private_posts' => 'customize', |
|
231 |
'delete_published_posts' => 'customize', |
|
232 |
'edit_others_posts' => 'customize', |
|
233 |
'edit_post' => 'customize', |
|
234 |
'edit_posts' => 'customize', |
|
235 |
'edit_private_posts' => 'customize', |
|
236 |
'edit_published_posts' => 'do_not_allow', |
|
237 |
'publish_posts' => 'customize', |
|
238 |
'read' => 'read', |
|
239 |
'read_post' => 'customize', |
|
240 |
'read_private_posts' => 'customize', |
|
241 |
), |
|
242 |
) |
|
243 |
); |
|
244 |
||
245 |
register_post_type( |
|
246 |
'oembed_cache', |
|
247 |
array( |
|
248 |
'labels' => array( |
|
249 |
'name' => __( 'oEmbed Responses' ), |
|
250 |
'singular_name' => __( 'oEmbed Response' ), |
|
251 |
), |
|
252 |
'public' => false, |
|
253 |
'hierarchical' => false, |
|
254 |
'rewrite' => false, |
|
255 |
'query_var' => false, |
|
256 |
'delete_with_user' => false, |
|
257 |
'can_export' => false, |
|
258 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
259 |
'supports' => array(), |
|
260 |
) |
|
261 |
); |
|
262 |
||
263 |
register_post_type( |
|
264 |
'user_request', |
|
265 |
array( |
|
266 |
'labels' => array( |
|
267 |
'name' => __( 'User Requests' ), |
|
268 |
'singular_name' => __( 'User Request' ), |
|
269 |
), |
|
270 |
'public' => false, |
|
271 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
272 |
'hierarchical' => false, |
|
273 |
'rewrite' => false, |
|
274 |
'query_var' => false, |
|
275 |
'can_export' => false, |
|
276 |
'delete_with_user' => false, |
|
277 |
'supports' => array(), |
|
278 |
) |
|
279 |
); |
|
280 |
||
281 |
register_post_type( |
|
282 |
'wp_block', |
|
283 |
array( |
|
284 |
'labels' => array( |
|
18 | 285 |
'name' => _x( 'Reusable blocks', 'post type general name' ), |
286 |
'singular_name' => _x( 'Reusable block', 'post type singular name' ), |
|
287 |
'add_new' => _x( 'Add New', 'Reusable block' ), |
|
288 |
'add_new_item' => __( 'Add new Reusable block' ), |
|
289 |
'new_item' => __( 'New Reusable block' ), |
|
290 |
'edit_item' => __( 'Edit Reusable block' ), |
|
291 |
'view_item' => __( 'View Reusable block' ), |
|
292 |
'all_items' => __( 'All Reusable blocks' ), |
|
293 |
'search_items' => __( 'Search Reusable blocks' ), |
|
294 |
'not_found' => __( 'No reusable blocks found.' ), |
|
295 |
'not_found_in_trash' => __( 'No reusable blocks found in Trash.' ), |
|
296 |
'filter_items_list' => __( 'Filter reusable blocks list' ), |
|
297 |
'items_list_navigation' => __( 'Reusable blocks list navigation' ), |
|
298 |
'items_list' => __( 'Reusable blocks list' ), |
|
299 |
'item_published' => __( 'Reusable block published.' ), |
|
300 |
'item_published_privately' => __( 'Reusable block published privately.' ), |
|
301 |
'item_reverted_to_draft' => __( 'Reusable block reverted to draft.' ), |
|
302 |
'item_scheduled' => __( 'Reusable block scheduled.' ), |
|
303 |
'item_updated' => __( 'Reusable block updated.' ), |
|
9 | 304 |
), |
305 |
'public' => false, |
|
306 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
307 |
'show_ui' => true, |
|
308 |
'show_in_menu' => false, |
|
309 |
'rewrite' => false, |
|
310 |
'show_in_rest' => true, |
|
311 |
'rest_base' => 'blocks', |
|
312 |
'rest_controller_class' => 'WP_REST_Blocks_Controller', |
|
313 |
'capability_type' => 'block', |
|
314 |
'capabilities' => array( |
|
315 |
// You need to be able to edit posts, in order to read blocks in their raw form. |
|
316 |
'read' => 'edit_posts', |
|
317 |
// You need to be able to publish posts, in order to create blocks. |
|
318 |
'create_posts' => 'publish_posts', |
|
319 |
'edit_posts' => 'edit_posts', |
|
320 |
'edit_published_posts' => 'edit_published_posts', |
|
321 |
'delete_published_posts' => 'delete_published_posts', |
|
322 |
'edit_others_posts' => 'edit_others_posts', |
|
323 |
'delete_others_posts' => 'delete_others_posts', |
|
324 |
), |
|
325 |
'map_meta_cap' => true, |
|
326 |
'supports' => array( |
|
327 |
'title', |
|
328 |
'editor', |
|
18 | 329 |
'revisions', |
330 |
), |
|
331 |
) |
|
332 |
); |
|
333 |
||
334 |
register_post_type( |
|
335 |
'wp_template', |
|
336 |
array( |
|
337 |
'labels' => array( |
|
19 | 338 |
'name' => _x( 'Templates', 'post type general name' ), |
339 |
'singular_name' => _x( 'Template', 'post type singular name' ), |
|
18 | 340 |
'add_new' => _x( 'Add New', 'Template' ), |
341 |
'add_new_item' => __( 'Add New Template' ), |
|
342 |
'new_item' => __( 'New Template' ), |
|
343 |
'edit_item' => __( 'Edit Template' ), |
|
344 |
'view_item' => __( 'View Template' ), |
|
19 | 345 |
'all_items' => __( 'Templates' ), |
18 | 346 |
'search_items' => __( 'Search Templates' ), |
347 |
'parent_item_colon' => __( 'Parent Template:' ), |
|
348 |
'not_found' => __( 'No templates found.' ), |
|
349 |
'not_found_in_trash' => __( 'No templates found in Trash.' ), |
|
350 |
'archives' => __( 'Template archives' ), |
|
351 |
'insert_into_item' => __( 'Insert into template' ), |
|
352 |
'uploaded_to_this_item' => __( 'Uploaded to this template' ), |
|
353 |
'filter_items_list' => __( 'Filter templates list' ), |
|
354 |
'items_list_navigation' => __( 'Templates list navigation' ), |
|
355 |
'items_list' => __( 'Templates list' ), |
|
356 |
), |
|
357 |
'description' => __( 'Templates to include in your theme.' ), |
|
358 |
'public' => false, |
|
359 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
360 |
'has_archive' => false, |
|
361 |
'show_ui' => false, |
|
362 |
'show_in_menu' => false, |
|
363 |
'show_in_rest' => true, |
|
364 |
'rewrite' => false, |
|
365 |
'rest_base' => 'templates', |
|
366 |
'rest_controller_class' => 'WP_REST_Templates_Controller', |
|
367 |
'capability_type' => array( 'template', 'templates' ), |
|
368 |
'capabilities' => array( |
|
369 |
'create_posts' => 'edit_theme_options', |
|
370 |
'delete_posts' => 'edit_theme_options', |
|
371 |
'delete_others_posts' => 'edit_theme_options', |
|
372 |
'delete_private_posts' => 'edit_theme_options', |
|
373 |
'delete_published_posts' => 'edit_theme_options', |
|
374 |
'edit_posts' => 'edit_theme_options', |
|
375 |
'edit_others_posts' => 'edit_theme_options', |
|
376 |
'edit_private_posts' => 'edit_theme_options', |
|
377 |
'edit_published_posts' => 'edit_theme_options', |
|
378 |
'publish_posts' => 'edit_theme_options', |
|
379 |
'read' => 'edit_theme_options', |
|
380 |
'read_private_posts' => 'edit_theme_options', |
|
381 |
), |
|
382 |
'map_meta_cap' => true, |
|
383 |
'supports' => array( |
|
384 |
'title', |
|
385 |
'slug', |
|
386 |
'excerpt', |
|
387 |
'editor', |
|
388 |
'revisions', |
|
19 | 389 |
'author', |
390 |
), |
|
391 |
) |
|
392 |
); |
|
393 |
||
394 |
register_post_type( |
|
395 |
'wp_template_part', |
|
396 |
array( |
|
397 |
'labels' => array( |
|
398 |
'name' => _x( 'Template Parts', 'post type general name' ), |
|
399 |
'singular_name' => _x( 'Template Part', 'post type singular name' ), |
|
400 |
'add_new' => _x( 'Add New', 'Template Part' ), |
|
401 |
'add_new_item' => __( 'Add New Template Part' ), |
|
402 |
'new_item' => __( 'New Template Part' ), |
|
403 |
'edit_item' => __( 'Edit Template Part' ), |
|
404 |
'view_item' => __( 'View Template Part' ), |
|
405 |
'all_items' => __( 'Template Parts' ), |
|
406 |
'search_items' => __( 'Search Template Parts' ), |
|
407 |
'parent_item_colon' => __( 'Parent Template Part:' ), |
|
408 |
'not_found' => __( 'No template parts found.' ), |
|
409 |
'not_found_in_trash' => __( 'No template parts found in Trash.' ), |
|
410 |
'archives' => __( 'Template part archives' ), |
|
411 |
'insert_into_item' => __( 'Insert into template part' ), |
|
412 |
'uploaded_to_this_item' => __( 'Uploaded to this template part' ), |
|
413 |
'filter_items_list' => __( 'Filter template parts list' ), |
|
414 |
'items_list_navigation' => __( 'Template parts list navigation' ), |
|
415 |
'items_list' => __( 'Template parts list' ), |
|
416 |
), |
|
417 |
'description' => __( 'Template parts to include in your templates.' ), |
|
418 |
'public' => false, |
|
419 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
420 |
'has_archive' => false, |
|
421 |
'show_ui' => false, |
|
422 |
'show_in_menu' => false, |
|
423 |
'show_in_rest' => true, |
|
424 |
'rewrite' => false, |
|
425 |
'rest_base' => 'template-parts', |
|
426 |
'rest_controller_class' => 'WP_REST_Templates_Controller', |
|
427 |
'map_meta_cap' => true, |
|
428 |
'capabilities' => array( |
|
429 |
'create_posts' => 'edit_theme_options', |
|
430 |
'delete_posts' => 'edit_theme_options', |
|
431 |
'delete_others_posts' => 'edit_theme_options', |
|
432 |
'delete_private_posts' => 'edit_theme_options', |
|
433 |
'delete_published_posts' => 'edit_theme_options', |
|
434 |
'edit_posts' => 'edit_theme_options', |
|
435 |
'edit_others_posts' => 'edit_theme_options', |
|
436 |
'edit_private_posts' => 'edit_theme_options', |
|
437 |
'edit_published_posts' => 'edit_theme_options', |
|
438 |
'publish_posts' => 'edit_theme_options', |
|
439 |
'read' => 'edit_theme_options', |
|
440 |
'read_private_posts' => 'edit_theme_options', |
|
441 |
), |
|
442 |
'supports' => array( |
|
443 |
'title', |
|
444 |
'slug', |
|
445 |
'excerpt', |
|
446 |
'editor', |
|
447 |
'revisions', |
|
448 |
'author', |
|
449 |
), |
|
450 |
) |
|
451 |
); |
|
452 |
||
453 |
register_post_type( |
|
454 |
'wp_global_styles', |
|
455 |
array( |
|
456 |
'label' => _x( 'Global Styles', 'post type general name' ), |
|
457 |
'description' => __( 'Global styles to include in themes.' ), |
|
458 |
'public' => false, |
|
459 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
460 |
'show_ui' => false, |
|
461 |
'show_in_rest' => false, |
|
462 |
'rewrite' => false, |
|
463 |
'capabilities' => array( |
|
464 |
'read' => 'edit_theme_options', |
|
465 |
'create_posts' => 'edit_theme_options', |
|
466 |
'edit_posts' => 'edit_theme_options', |
|
467 |
'edit_published_posts' => 'edit_theme_options', |
|
468 |
'delete_published_posts' => 'edit_theme_options', |
|
469 |
'edit_others_posts' => 'edit_theme_options', |
|
470 |
'delete_others_posts' => 'edit_theme_options', |
|
471 |
), |
|
472 |
'map_meta_cap' => true, |
|
473 |
'supports' => array( |
|
474 |
'title', |
|
475 |
'editor', |
|
476 |
'revisions', |
|
477 |
), |
|
478 |
) |
|
479 |
); |
|
480 |
||
481 |
register_post_type( |
|
482 |
'wp_navigation', |
|
483 |
array( |
|
484 |
'labels' => array( |
|
485 |
'name' => _x( 'Navigation Menus', 'post type general name' ), |
|
486 |
'singular_name' => _x( 'Navigation Menu', 'post type singular name' ), |
|
487 |
'add_new' => _x( 'Add New', 'Navigation Menu' ), |
|
488 |
'add_new_item' => __( 'Add New Navigation Menu' ), |
|
489 |
'new_item' => __( 'New Navigation Menu' ), |
|
490 |
'edit_item' => __( 'Edit Navigation Menu' ), |
|
491 |
'view_item' => __( 'View Navigation Menu' ), |
|
492 |
'all_items' => __( 'Navigation Menus' ), |
|
493 |
'search_items' => __( 'Search Navigation Menus' ), |
|
494 |
'parent_item_colon' => __( 'Parent Navigation Menu:' ), |
|
495 |
'not_found' => __( 'No Navigation Menu found.' ), |
|
496 |
'not_found_in_trash' => __( 'No Navigation Menu found in Trash.' ), |
|
497 |
'archives' => __( 'Navigation Menu archives' ), |
|
498 |
'insert_into_item' => __( 'Insert into Navigation Menu' ), |
|
499 |
'uploaded_to_this_item' => __( 'Uploaded to this Navigation Menu' ), |
|
500 |
'filter_items_list' => __( 'Filter Navigation Menu list' ), |
|
501 |
'items_list_navigation' => __( 'Navigation Menus list navigation' ), |
|
502 |
'items_list' => __( 'Navigation Menus list' ), |
|
503 |
), |
|
504 |
'description' => __( 'Navigation menus that can be inserted into your site.' ), |
|
505 |
'public' => false, |
|
506 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
507 |
'has_archive' => false, |
|
508 |
'show_ui' => true, |
|
509 |
'show_in_menu' => false, |
|
510 |
'show_in_admin_bar' => false, |
|
511 |
'show_in_rest' => true, |
|
512 |
'rewrite' => false, |
|
513 |
'map_meta_cap' => true, |
|
514 |
'capabilities' => array( |
|
515 |
'edit_others_posts' => 'edit_theme_options', |
|
516 |
'delete_posts' => 'edit_theme_options', |
|
517 |
'publish_posts' => 'edit_theme_options', |
|
518 |
'create_posts' => 'edit_theme_options', |
|
519 |
'read_private_posts' => 'edit_theme_options', |
|
520 |
'delete_private_posts' => 'edit_theme_options', |
|
521 |
'delete_published_posts' => 'edit_theme_options', |
|
522 |
'delete_others_posts' => 'edit_theme_options', |
|
523 |
'edit_private_posts' => 'edit_theme_options', |
|
524 |
'edit_published_posts' => 'edit_theme_options', |
|
525 |
'edit_posts' => 'edit_theme_options', |
|
526 |
), |
|
527 |
'rest_base' => 'navigation', |
|
528 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
529 |
'supports' => array( |
|
530 |
'title', |
|
531 |
'editor', |
|
532 |
'revisions', |
|
9 | 533 |
), |
534 |
) |
|
535 |
); |
|
536 |
||
537 |
register_post_status( |
|
538 |
'publish', |
|
539 |
array( |
|
540 |
'label' => _x( 'Published', 'post status' ), |
|
541 |
'public' => true, |
|
542 |
'_builtin' => true, /* internal use only. */ |
|
16 | 543 |
/* translators: %s: Number of published posts. */ |
544 |
'label_count' => _n_noop( |
|
545 |
'Published <span class="count">(%s)</span>', |
|
546 |
'Published <span class="count">(%s)</span>' |
|
547 |
), |
|
9 | 548 |
) |
549 |
); |
|
550 |
||
551 |
register_post_status( |
|
552 |
'future', |
|
553 |
array( |
|
554 |
'label' => _x( 'Scheduled', 'post status' ), |
|
555 |
'protected' => true, |
|
556 |
'_builtin' => true, /* internal use only. */ |
|
16 | 557 |
/* translators: %s: Number of scheduled posts. */ |
558 |
'label_count' => _n_noop( |
|
559 |
'Scheduled <span class="count">(%s)</span>', |
|
560 |
'Scheduled <span class="count">(%s)</span>' |
|
561 |
), |
|
9 | 562 |
) |
563 |
); |
|
564 |
||
565 |
register_post_status( |
|
566 |
'draft', |
|
567 |
array( |
|
16 | 568 |
'label' => _x( 'Draft', 'post status' ), |
569 |
'protected' => true, |
|
570 |
'_builtin' => true, /* internal use only. */ |
|
571 |
/* translators: %s: Number of draft posts. */ |
|
572 |
'label_count' => _n_noop( |
|
573 |
'Draft <span class="count">(%s)</span>', |
|
574 |
'Drafts <span class="count">(%s)</span>' |
|
575 |
), |
|
576 |
'date_floating' => true, |
|
9 | 577 |
) |
578 |
); |
|
579 |
||
580 |
register_post_status( |
|
581 |
'pending', |
|
582 |
array( |
|
16 | 583 |
'label' => _x( 'Pending', 'post status' ), |
584 |
'protected' => true, |
|
585 |
'_builtin' => true, /* internal use only. */ |
|
586 |
/* translators: %s: Number of pending posts. */ |
|
587 |
'label_count' => _n_noop( |
|
588 |
'Pending <span class="count">(%s)</span>', |
|
589 |
'Pending <span class="count">(%s)</span>' |
|
590 |
), |
|
591 |
'date_floating' => true, |
|
9 | 592 |
) |
593 |
); |
|
594 |
||
595 |
register_post_status( |
|
596 |
'private', |
|
597 |
array( |
|
598 |
'label' => _x( 'Private', 'post status' ), |
|
599 |
'private' => true, |
|
600 |
'_builtin' => true, /* internal use only. */ |
|
16 | 601 |
/* translators: %s: Number of private posts. */ |
602 |
'label_count' => _n_noop( |
|
603 |
'Private <span class="count">(%s)</span>', |
|
604 |
'Private <span class="count">(%s)</span>' |
|
605 |
), |
|
9 | 606 |
) |
607 |
); |
|
608 |
||
609 |
register_post_status( |
|
610 |
'trash', |
|
611 |
array( |
|
612 |
'label' => _x( 'Trash', 'post status' ), |
|
613 |
'internal' => true, |
|
614 |
'_builtin' => true, /* internal use only. */ |
|
16 | 615 |
/* translators: %s: Number of trashed posts. */ |
616 |
'label_count' => _n_noop( |
|
617 |
'Trash <span class="count">(%s)</span>', |
|
618 |
'Trash <span class="count">(%s)</span>' |
|
619 |
), |
|
9 | 620 |
'show_in_admin_status_list' => true, |
621 |
) |
|
622 |
); |
|
623 |
||
624 |
register_post_status( |
|
625 |
'auto-draft', |
|
626 |
array( |
|
16 | 627 |
'label' => 'auto-draft', |
628 |
'internal' => true, |
|
629 |
'_builtin' => true, /* internal use only. */ |
|
630 |
'date_floating' => true, |
|
9 | 631 |
) |
632 |
); |
|
633 |
||
634 |
register_post_status( |
|
635 |
'inherit', |
|
636 |
array( |
|
637 |
'label' => 'inherit', |
|
638 |
'internal' => true, |
|
639 |
'_builtin' => true, /* internal use only. */ |
|
640 |
'exclude_from_search' => false, |
|
641 |
) |
|
642 |
); |
|
643 |
||
644 |
register_post_status( |
|
645 |
'request-pending', |
|
646 |
array( |
|
647 |
'label' => _x( 'Pending', 'request status' ), |
|
648 |
'internal' => true, |
|
649 |
'_builtin' => true, /* internal use only. */ |
|
16 | 650 |
/* translators: %s: Number of pending requests. */ |
651 |
'label_count' => _n_noop( |
|
652 |
'Pending <span class="count">(%s)</span>', |
|
653 |
'Pending <span class="count">(%s)</span>' |
|
654 |
), |
|
9 | 655 |
'exclude_from_search' => false, |
656 |
) |
|
657 |
); |
|
658 |
||
659 |
register_post_status( |
|
660 |
'request-confirmed', |
|
661 |
array( |
|
662 |
'label' => _x( 'Confirmed', 'request status' ), |
|
663 |
'internal' => true, |
|
664 |
'_builtin' => true, /* internal use only. */ |
|
16 | 665 |
/* translators: %s: Number of confirmed requests. */ |
666 |
'label_count' => _n_noop( |
|
667 |
'Confirmed <span class="count">(%s)</span>', |
|
668 |
'Confirmed <span class="count">(%s)</span>' |
|
669 |
), |
|
9 | 670 |
'exclude_from_search' => false, |
671 |
) |
|
672 |
); |
|
673 |
||
674 |
register_post_status( |
|
675 |
'request-failed', |
|
676 |
array( |
|
677 |
'label' => _x( 'Failed', 'request status' ), |
|
678 |
'internal' => true, |
|
679 |
'_builtin' => true, /* internal use only. */ |
|
16 | 680 |
/* translators: %s: Number of failed requests. */ |
681 |
'label_count' => _n_noop( |
|
682 |
'Failed <span class="count">(%s)</span>', |
|
683 |
'Failed <span class="count">(%s)</span>' |
|
684 |
), |
|
9 | 685 |
'exclude_from_search' => false, |
686 |
) |
|
687 |
); |
|
688 |
||
689 |
register_post_status( |
|
690 |
'request-completed', |
|
691 |
array( |
|
692 |
'label' => _x( 'Completed', 'request status' ), |
|
693 |
'internal' => true, |
|
694 |
'_builtin' => true, /* internal use only. */ |
|
16 | 695 |
/* translators: %s: Number of completed requests. */ |
696 |
'label_count' => _n_noop( |
|
697 |
'Completed <span class="count">(%s)</span>', |
|
698 |
'Completed <span class="count">(%s)</span>' |
|
699 |
), |
|
9 | 700 |
'exclude_from_search' => false, |
701 |
) |
|
702 |
); |
|
0 | 703 |
} |
704 |
||
705 |
/** |
|
706 |
* Retrieve attached file path based on attachment ID. |
|
707 |
* |
|
708 |
* By default the path will go through the 'get_attached_file' filter, but |
|
709 |
* passing a true to the $unfiltered argument of get_attached_file() will |
|
710 |
* return the file path unfiltered. |
|
711 |
* |
|
712 |
* The function works by getting the single post meta name, named |
|
713 |
* '_wp_attached_file' and returning it. This is a convenience function to |
|
714 |
* prevent looking up the meta name and provide a mechanism for sending the |
|
715 |
* attached filename through a filter. |
|
716 |
* |
|
717 |
* @since 2.0.0 |
|
718 |
* |
|
5 | 719 |
* @param int $attachment_id Attachment ID. |
720 |
* @param bool $unfiltered Optional. Whether to apply filters. Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
721 |
* @return string|false The file path to where the attached file should be, false otherwise. |
0 | 722 |
*/ |
723 |
function get_attached_file( $attachment_id, $unfiltered = false ) { |
|
724 |
$file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
725 |
|
5 | 726 |
// If the file is relative, prepend upload dir. |
16 | 727 |
if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { |
728 |
$uploads = wp_get_upload_dir(); |
|
729 |
if ( false === $uploads['error'] ) { |
|
730 |
$file = $uploads['basedir'] . "/$file"; |
|
731 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
732 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
733 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
if ( $unfiltered ) { |
0 | 735 |
return $file; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
} |
5 | 737 |
|
738 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
* Filters the attached file based on the given ID. |
5 | 740 |
* |
741 |
* @since 2.1.0 |
|
742 |
* |
|
16 | 743 |
* @param string|false $file The file path to where the attached file should be, false otherwise. |
744 |
* @param int $attachment_id Attachment ID. |
|
5 | 745 |
*/ |
0 | 746 |
return apply_filters( 'get_attached_file', $file, $attachment_id ); |
747 |
} |
|
748 |
||
749 |
/** |
|
750 |
* Update attachment file path based on attachment ID. |
|
751 |
* |
|
752 |
* Used to update the file path of the attachment, which uses post meta name |
|
753 |
* '_wp_attached_file' to store the path of the attachment. |
|
754 |
* |
|
755 |
* @since 2.1.0 |
|
5 | 756 |
* |
757 |
* @param int $attachment_id Attachment ID. |
|
758 |
* @param string $file File path for the attachment. |
|
0 | 759 |
* @return bool True on success, false on failure. |
760 |
*/ |
|
761 |
function update_attached_file( $attachment_id, $file ) { |
|
9 | 762 |
if ( ! get_post( $attachment_id ) ) { |
0 | 763 |
return false; |
9 | 764 |
} |
0 | 765 |
|
5 | 766 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
767 |
* Filters the path to the attached file to update. |
5 | 768 |
* |
769 |
* @since 2.1.0 |
|
770 |
* |
|
771 |
* @param string $file Path to the attached file to update. |
|
772 |
* @param int $attachment_id Attachment ID. |
|
773 |
*/ |
|
0 | 774 |
$file = apply_filters( 'update_attached_file', $file, $attachment_id ); |
5 | 775 |
|
16 | 776 |
$file = _wp_relative_upload_path( $file ); |
777 |
if ( $file ) { |
|
0 | 778 |
return update_post_meta( $attachment_id, '_wp_attached_file', $file ); |
9 | 779 |
} else { |
0 | 780 |
return delete_post_meta( $attachment_id, '_wp_attached_file' ); |
9 | 781 |
} |
0 | 782 |
} |
783 |
||
784 |
/** |
|
785 |
* Return relative path to an uploaded file. |
|
786 |
* |
|
787 |
* The path is relative to the current upload dir. |
|
788 |
* |
|
789 |
* @since 2.9.0 |
|
9 | 790 |
* @access private |
5 | 791 |
* |
792 |
* @param string $path Full path to the file. |
|
793 |
* @return string Relative path on success, unchanged path on failure. |
|
0 | 794 |
*/ |
795 |
function _wp_relative_upload_path( $path ) { |
|
796 |
$new_path = $path; |
|
797 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
798 |
$uploads = wp_get_upload_dir(); |
0 | 799 |
if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { |
800 |
$new_path = str_replace( $uploads['basedir'], '', $new_path ); |
|
801 |
$new_path = ltrim( $new_path, '/' ); |
|
802 |
} |
|
803 |
||
5 | 804 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
805 |
* Filters the relative path to an uploaded file. |
5 | 806 |
* |
807 |
* @since 2.9.0 |
|
808 |
* |
|
809 |
* @param string $new_path Relative path to the file. |
|
810 |
* @param string $path Full path to the file. |
|
811 |
*/ |
|
0 | 812 |
return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
813 |
} |
|
814 |
||
815 |
/** |
|
816 |
* Retrieve all children of the post parent ID. |
|
817 |
* |
|
818 |
* Normally, without any enhancements, the children would apply to pages. In the |
|
819 |
* context of the inner workings of WordPress, pages, posts, and attachments |
|
820 |
* share the same table, so therefore the functionality could apply to any one |
|
821 |
* of them. It is then noted that while this function does not work on posts, it |
|
822 |
* does not mean that it won't work on posts. It is recommended that you know |
|
823 |
* what context you wish to retrieve the children of. |
|
824 |
* |
|
825 |
* Attachments may also be made the child of a post, so if that is an accurate |
|
826 |
* statement (which needs to be verified), it would then be possible to get |
|
827 |
* all of the attachments for a post. Attachments have since changed since |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
828 |
* version 2.5, so this is most likely inaccurate, but serves generally as an |
0 | 829 |
* example of what is possible. |
830 |
* |
|
831 |
* The arguments listed as defaults are for this function and also of the |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
832 |
* get_posts() function. The arguments are combined with the get_children defaults |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
833 |
* and are then passed to the get_posts() function, which accepts additional arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
834 |
* You can replace the defaults in this function, listed below and the additional |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
835 |
* arguments listed in the get_posts() function. |
0 | 836 |
* |
837 |
* The 'post_parent' is the most important argument and important attention |
|
838 |
* needs to be paid to the $args parameter. If you pass either an object or an |
|
839 |
* integer (number), then just the 'post_parent' is grabbed and everything else |
|
840 |
* is lost. If you don't specify any arguments, then it is assumed that you are |
|
841 |
* in The Loop and the post parent will be grabbed for from the current post. |
|
842 |
* |
|
843 |
* The 'post_parent' argument is the ID to get the children. The 'numberposts' |
|
844 |
* is the amount of posts to retrieve that has a default of '-1', which is |
|
845 |
* used to get all of the posts. Giving a number higher than 0 will only |
|
846 |
* retrieve that amount of posts. |
|
847 |
* |
|
848 |
* The 'post_type' and 'post_status' arguments can be used to choose what |
|
849 |
* criteria of posts to retrieve. The 'post_type' can be anything, but WordPress |
|
850 |
* post types are 'post', 'pages', and 'attachments'. The 'post_status' |
|
851 |
* argument will accept any post status within the write administration panels. |
|
852 |
* |
|
853 |
* @since 2.0.0 |
|
854 |
* |
|
5 | 855 |
* @see get_posts() |
856 |
* @todo Check validity of description. |
|
857 |
* |
|
16 | 858 |
* @global WP_Post $post Global post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
859 |
* |
5 | 860 |
* @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. |
16 | 861 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
862 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
863 |
* respectively. Default OBJECT. |
|
864 |
* @return WP_Post[]|int[] Array of post objects or post IDs. |
|
0 | 865 |
*/ |
5 | 866 |
function get_children( $args = '', $output = OBJECT ) { |
0 | 867 |
$kids = array(); |
868 |
if ( empty( $args ) ) { |
|
869 |
if ( isset( $GLOBALS['post'] ) ) { |
|
9 | 870 |
$args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent ); |
0 | 871 |
} else { |
872 |
return $kids; |
|
873 |
} |
|
874 |
} elseif ( is_object( $args ) ) { |
|
9 | 875 |
$args = array( 'post_parent' => (int) $args->post_parent ); |
0 | 876 |
} elseif ( is_numeric( $args ) ) { |
9 | 877 |
$args = array( 'post_parent' => (int) $args ); |
0 | 878 |
} |
879 |
||
880 |
$defaults = array( |
|
9 | 881 |
'numberposts' => -1, |
882 |
'post_type' => 'any', |
|
883 |
'post_status' => 'any', |
|
884 |
'post_parent' => 0, |
|
0 | 885 |
); |
886 |
||
16 | 887 |
$parsed_args = wp_parse_args( $args, $defaults ); |
888 |
||
889 |
$children = get_posts( $parsed_args ); |
|
0 | 890 |
|
9 | 891 |
if ( ! $children ) { |
0 | 892 |
return $kids; |
9 | 893 |
} |
894 |
||
16 | 895 |
if ( ! empty( $parsed_args['fields'] ) ) { |
0 | 896 |
return $children; |
9 | 897 |
} |
898 |
||
899 |
update_post_cache( $children ); |
|
900 |
||
901 |
foreach ( $children as $key => $child ) { |
|
902 |
$kids[ $child->ID ] = $children[ $key ]; |
|
903 |
} |
|
0 | 904 |
|
18 | 905 |
if ( OBJECT === $output ) { |
0 | 906 |
return $kids; |
18 | 907 |
} elseif ( ARRAY_A === $output ) { |
5 | 908 |
$weeuns = array(); |
909 |
foreach ( (array) $kids as $kid ) { |
|
9 | 910 |
$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] ); |
5 | 911 |
} |
0 | 912 |
return $weeuns; |
18 | 913 |
} elseif ( ARRAY_N === $output ) { |
5 | 914 |
$babes = array(); |
915 |
foreach ( (array) $kids as $kid ) { |
|
9 | 916 |
$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) ); |
5 | 917 |
} |
0 | 918 |
return $babes; |
919 |
} else { |
|
920 |
return $kids; |
|
921 |
} |
|
922 |
} |
|
923 |
||
924 |
/** |
|
925 |
* Get extended entry info (<!--more-->). |
|
926 |
* |
|
927 |
* There should not be any space after the second dash and before the word |
|
928 |
* 'more'. There can be text or space(s) after the word 'more', but won't be |
|
929 |
* referenced. |
|
930 |
* |
|
931 |
* The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before |
|
5 | 932 |
* the `<!--more-->`. The 'extended' key has the content after the |
933 |
* `<!--more-->` comment. The 'more_text' key has the custom "Read More" text. |
|
0 | 934 |
* |
935 |
* @since 1.0.0 |
|
936 |
* |
|
937 |
* @param string $post Post content. |
|
16 | 938 |
* @return string[] { |
939 |
* Extended entry info. |
|
940 |
* |
|
941 |
* @type string $main Content before the more tag. |
|
942 |
* @type string $extended Content after the more tag. |
|
943 |
* @type string $more_text Custom read more text, or empty string. |
|
944 |
* } |
|
0 | 945 |
*/ |
5 | 946 |
function get_extended( $post ) { |
16 | 947 |
// Match the new style more links. |
9 | 948 |
if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) { |
949 |
list($main, $extended) = explode( $matches[0], $post, 2 ); |
|
950 |
$more_text = $matches[1]; |
|
0 | 951 |
} else { |
9 | 952 |
$main = $post; |
953 |
$extended = ''; |
|
0 | 954 |
$more_text = ''; |
955 |
} |
|
956 |
||
16 | 957 |
// Leading and trailing whitespace. |
9 | 958 |
$main = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main ); |
959 |
$extended = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended ); |
|
960 |
$more_text = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $more_text ); |
|
961 |
||
962 |
return array( |
|
963 |
'main' => $main, |
|
964 |
'extended' => $extended, |
|
965 |
'more_text' => $more_text, |
|
966 |
); |
|
0 | 967 |
} |
968 |
||
969 |
/** |
|
970 |
* Retrieves post data given a post ID or post object. |
|
971 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
972 |
* See sanitize_post() for optional $filter values. Also, the parameter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
973 |
* `$post`, must be given as a variable, since it is passed by reference. |
0 | 974 |
* |
975 |
* @since 1.5.1 |
|
5 | 976 |
* |
16 | 977 |
* @global WP_Post $post Global post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
* |
19 | 979 |
* @param int|WP_Post|null $post Optional. Post ID or post object. `null`, `false`, `0` and other PHP falsey values |
980 |
* return the current global post inside the loop. A numerically valid post ID that |
|
981 |
* points to a non-existent post returns `null`. Defaults to global $post. |
|
16 | 982 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
983 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
984 |
* respectively. Default OBJECT. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
* @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
986 |
* or 'display'. Default 'raw'. |
5 | 987 |
* @return WP_Post|array|null Type corresponding to $output on success or null on failure. |
988 |
* When $output is OBJECT, a `WP_Post` instance is returned. |
|
0 | 989 |
*/ |
990 |
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { |
|
9 | 991 |
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) { |
0 | 992 |
$post = $GLOBALS['post']; |
9 | 993 |
} |
0 | 994 |
|
5 | 995 |
if ( $post instanceof WP_Post ) { |
0 | 996 |
$_post = $post; |
997 |
} elseif ( is_object( $post ) ) { |
|
998 |
if ( empty( $post->filter ) ) { |
|
999 |
$_post = sanitize_post( $post, 'raw' ); |
|
1000 |
$_post = new WP_Post( $_post ); |
|
16 | 1001 |
} elseif ( 'raw' === $post->filter ) { |
0 | 1002 |
$_post = new WP_Post( $post ); |
1003 |
} else { |
|
1004 |
$_post = WP_Post::get_instance( $post->ID ); |
|
1005 |
} |
|
1006 |
} else { |
|
1007 |
$_post = WP_Post::get_instance( $post ); |
|
1008 |
} |
|
1009 |
||
9 | 1010 |
if ( ! $_post ) { |
0 | 1011 |
return null; |
9 | 1012 |
} |
0 | 1013 |
|
1014 |
$_post = $_post->filter( $filter ); |
|
1015 |
||
18 | 1016 |
if ( ARRAY_A === $output ) { |
0 | 1017 |
return $_post->to_array(); |
18 | 1018 |
} elseif ( ARRAY_N === $output ) { |
0 | 1019 |
return array_values( $_post->to_array() ); |
9 | 1020 |
} |
0 | 1021 |
|
1022 |
return $_post; |
|
1023 |
} |
|
1024 |
||
1025 |
/** |
|
18 | 1026 |
* Retrieves the IDs of the ancestors of a post. |
0 | 1027 |
* |
1028 |
* @since 2.5.0 |
|
1029 |
* |
|
5 | 1030 |
* @param int|WP_Post $post Post ID or post object. |
18 | 1031 |
* @return int[] Array of ancestor IDs or empty array if there are none. |
0 | 1032 |
*/ |
1033 |
function get_post_ancestors( $post ) { |
|
1034 |
$post = get_post( $post ); |
|
1035 |
||
9 | 1036 |
if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) { |
0 | 1037 |
return array(); |
9 | 1038 |
} |
0 | 1039 |
|
1040 |
$ancestors = array(); |
|
1041 |
||
16 | 1042 |
$id = $post->post_parent; |
1043 |
$ancestors[] = $id; |
|
0 | 1044 |
|
1045 |
while ( $ancestor = get_post( $id ) ) { |
|
1046 |
// Loop detection: If the ancestor has been seen before, break. |
|
16 | 1047 |
if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) { |
0 | 1048 |
break; |
9 | 1049 |
} |
0 | 1050 |
|
16 | 1051 |
$id = $ancestor->post_parent; |
1052 |
$ancestors[] = $id; |
|
0 | 1053 |
} |
1054 |
||
1055 |
return $ancestors; |
|
1056 |
} |
|
1057 |
||
1058 |
/** |
|
1059 |
* Retrieve data from a post field based on Post ID. |
|
1060 |
* |
|
1061 |
* Examples of the post field will be, 'post_type', 'post_status', 'post_content', |
|
1062 |
* etc and based off of the post object property or key names. |
|
1063 |
* |
|
1064 |
* The context values are based off of the taxonomy filter functions and |
|
1065 |
* supported values are found within those functions. |
|
1066 |
* |
|
1067 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1068 |
* @since 4.5.0 The `$post` parameter was made optional. |
5 | 1069 |
* |
1070 |
* @see sanitize_post_field() |
|
1071 |
* |
|
1072 |
* @param string $field Post field name. |
|
9 | 1073 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 1074 |
* @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db', |
1075 |
* or 'display'. Default 'display'. |
|
0 | 1076 |
* @return string The value of the post field on success, empty string on failure. |
1077 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1078 |
function get_post_field( $field, $post = null, $context = 'display' ) { |
0 | 1079 |
$post = get_post( $post ); |
1080 |
||
9 | 1081 |
if ( ! $post ) { |
0 | 1082 |
return ''; |
9 | 1083 |
} |
1084 |
||
1085 |
if ( ! isset( $post->$field ) ) { |
|
0 | 1086 |
return ''; |
9 | 1087 |
} |
1088 |
||
1089 |
return sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|
0 | 1090 |
} |
1091 |
||
1092 |
/** |
|
1093 |
* Retrieve the mime type of an attachment based on the ID. |
|
1094 |
* |
|
1095 |
* This function can be used with any post type, but it makes more sense with |
|
1096 |
* attachments. |
|
1097 |
* |
|
1098 |
* @since 2.0.0 |
|
1099 |
* |
|
9 | 1100 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 1101 |
* @return string|false The mime type on success, false on failure. |
0 | 1102 |
*/ |
9 | 1103 |
function get_post_mime_type( $post = null ) { |
1104 |
$post = get_post( $post ); |
|
1105 |
||
1106 |
if ( is_object( $post ) ) { |
|
0 | 1107 |
return $post->post_mime_type; |
9 | 1108 |
} |
0 | 1109 |
|
1110 |
return false; |
|
1111 |
} |
|
1112 |
||
1113 |
/** |
|
9 | 1114 |
* Retrieve the post status based on the post ID. |
0 | 1115 |
* |
1116 |
* If the post ID is of an attachment, then the parent post status will be given |
|
1117 |
* instead. |
|
1118 |
* |
|
1119 |
* @since 2.0.0 |
|
1120 |
* |
|
19 | 1121 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 1122 |
* @return string|false Post status on success, false on failure. |
0 | 1123 |
*/ |
9 | 1124 |
function get_post_status( $post = null ) { |
1125 |
$post = get_post( $post ); |
|
1126 |
||
1127 |
if ( ! is_object( $post ) ) { |
|
0 | 1128 |
return false; |
9 | 1129 |
} |
0 | 1130 |
|
18 | 1131 |
$post_status = $post->post_status; |
1132 |
||
1133 |
if ( |
|
1134 |
'attachment' === $post->post_type && |
|
1135 |
'inherit' === $post_status |
|
1136 |
) { |
|
1137 |
if ( |
|
1138 |
0 === $post->post_parent || |
|
1139 |
! get_post( $post->post_parent ) || |
|
1140 |
$post->ID === $post->post_parent |
|
1141 |
) { |
|
1142 |
// Unattached attachments with inherit status are assumed to be published. |
|
1143 |
$post_status = 'publish'; |
|
1144 |
} elseif ( 'trash' === get_post_status( $post->post_parent ) ) { |
|
1145 |
// Get parent status prior to trashing. |
|
1146 |
$post_status = get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); |
|
1147 |
if ( ! $post_status ) { |
|
1148 |
// Assume publish as above. |
|
1149 |
$post_status = 'publish'; |
|
1150 |
} |
|
1151 |
} else { |
|
1152 |
$post_status = get_post_status( $post->post_parent ); |
|
9 | 1153 |
} |
18 | 1154 |
} elseif ( |
1155 |
'attachment' === $post->post_type && |
|
1156 |
! in_array( $post_status, array( 'private', 'trash', 'auto-draft' ), true ) |
|
1157 |
) { |
|
1158 |
/* |
|
1159 |
* Ensure uninherited attachments have a permitted status either 'private', 'trash', 'auto-draft'. |
|
1160 |
* This is to match the logic in wp_insert_post(). |
|
1161 |
* |
|
1162 |
* Note: 'inherit' is excluded from this check as it is resolved to the parent post's |
|
1163 |
* status in the logic block above. |
|
1164 |
*/ |
|
1165 |
$post_status = 'publish'; |
|
0 | 1166 |
} |
1167 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1168 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1169 |
* Filters the post status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1170 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1171 |
* @since 4.4.0 |
18 | 1172 |
* @since 5.7.0 The attachment post type is now passed through this filter. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1173 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
* @param string $post_status The post status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1175 |
* @param WP_Post $post The post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
*/ |
18 | 1177 |
return apply_filters( 'get_post_status', $post_status, $post ); |
0 | 1178 |
} |
1179 |
||
1180 |
/** |
|
1181 |
* Retrieve all of the WordPress supported post statuses. |
|
1182 |
* |
|
1183 |
* Posts have a limited set of valid status values, this provides the |
|
1184 |
* post_status values and descriptions. |
|
1185 |
* |
|
1186 |
* @since 2.5.0 |
|
1187 |
* |
|
16 | 1188 |
* @return string[] Array of post status labels keyed by their status. |
0 | 1189 |
*/ |
1190 |
function get_post_statuses() { |
|
1191 |
$status = array( |
|
5 | 1192 |
'draft' => __( 'Draft' ), |
1193 |
'pending' => __( 'Pending Review' ), |
|
1194 |
'private' => __( 'Private' ), |
|
9 | 1195 |
'publish' => __( 'Published' ), |
0 | 1196 |
); |
1197 |
||
1198 |
return $status; |
|
1199 |
} |
|
1200 |
||
1201 |
/** |
|
1202 |
* Retrieve all of the WordPress support page statuses. |
|
1203 |
* |
|
1204 |
* Pages have a limited set of valid status values, this provides the |
|
1205 |
* post_status values and descriptions. |
|
1206 |
* |
|
1207 |
* @since 2.5.0 |
|
1208 |
* |
|
16 | 1209 |
* @return string[] Array of page status labels keyed by their status. |
0 | 1210 |
*/ |
1211 |
function get_page_statuses() { |
|
1212 |
$status = array( |
|
5 | 1213 |
'draft' => __( 'Draft' ), |
1214 |
'private' => __( 'Private' ), |
|
9 | 1215 |
'publish' => __( 'Published' ), |
0 | 1216 |
); |
1217 |
||
1218 |
return $status; |
|
1219 |
} |
|
1220 |
||
1221 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
* Return statuses for privacy requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1223 |
* |
9 | 1224 |
* @since 4.9.6 |
1225 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1226 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1227 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1229 |
function _wp_privacy_statuses() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
return array( |
16 | 1231 |
'request-pending' => _x( 'Pending', 'request status' ), // Pending confirmation from user. |
1232 |
'request-confirmed' => _x( 'Confirmed', 'request status' ), // User has confirmed the action. |
|
1233 |
'request-failed' => _x( 'Failed', 'request status' ), // User failed to confirm the action. |
|
1234 |
'request-completed' => _x( 'Completed', 'request status' ), // Admin has handled the request. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1235 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1236 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
/** |
0 | 1239 |
* Register a post status. Do not use before init. |
1240 |
* |
|
1241 |
* A simple function for creating or modifying a post status based on the |
|
1242 |
* parameters given. The function will accept an array (second optional |
|
1243 |
* parameter), along with a string for the post status name. |
|
1244 |
* |
|
1245 |
* Arguments prefixed with an _underscore shouldn't be used by plugins and themes. |
|
1246 |
* |
|
1247 |
* @since 3.0.0 |
|
16 | 1248 |
* |
19 | 1249 |
* @global stdClass[] $wp_post_statuses Inserts new post status object into the list |
0 | 1250 |
* |
16 | 1251 |
* @param string $post_status Name of the post status. |
5 | 1252 |
* @param array|string $args { |
1253 |
* Optional. Array or string of post status arguments. |
|
1254 |
* |
|
1255 |
* @type bool|string $label A descriptive name for the post status marked |
|
1256 |
* for translation. Defaults to value of $post_status. |
|
1257 |
* @type bool|array $label_count Descriptive text to use for nooped plurals. |
|
16 | 1258 |
* Default array of $label, twice. |
5 | 1259 |
* @type bool $exclude_from_search Whether to exclude posts with this post status |
1260 |
* from search results. Default is value of $internal. |
|
1261 |
* @type bool $_builtin Whether the status is built-in. Core-use only. |
|
1262 |
* Default false. |
|
1263 |
* @type bool $public Whether posts of this status should be shown |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
* in the front end of the site. Default false. |
5 | 1265 |
* @type bool $internal Whether the status is for internal use only. |
1266 |
* Default false. |
|
1267 |
* @type bool $protected Whether posts with this status should be protected. |
|
1268 |
* Default false. |
|
1269 |
* @type bool $private Whether posts with this status should be private. |
|
1270 |
* Default false. |
|
1271 |
* @type bool $publicly_queryable Whether posts with this status should be publicly- |
|
1272 |
* queryable. Default is value of $public. |
|
1273 |
* @type bool $show_in_admin_all_list Whether to include posts in the edit listing for |
|
16 | 1274 |
* their post type. Default is the opposite value |
1275 |
* of $internal. |
|
5 | 1276 |
* @type bool $show_in_admin_status_list Show in the list of statuses with post counts at |
1277 |
* the top of the edit listings, |
|
1278 |
* e.g. All (12) | Published (9) | My Custom Status (2) |
|
16 | 1279 |
* Default is the opposite value of $internal. |
1280 |
* @type bool $date_floating Whether the post has a floating creation date. |
|
1281 |
* Default to false. |
|
5 | 1282 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
* @return object |
0 | 1284 |
*/ |
5 | 1285 |
function register_post_status( $post_status, $args = array() ) { |
0 | 1286 |
global $wp_post_statuses; |
1287 |
||
9 | 1288 |
if ( ! is_array( $wp_post_statuses ) ) { |
0 | 1289 |
$wp_post_statuses = array(); |
9 | 1290 |
} |
0 | 1291 |
|
1292 |
// Args prefixed with an underscore are reserved for internal use. |
|
1293 |
$defaults = array( |
|
9 | 1294 |
'label' => false, |
1295 |
'label_count' => false, |
|
1296 |
'exclude_from_search' => null, |
|
1297 |
'_builtin' => false, |
|
1298 |
'public' => null, |
|
1299 |
'internal' => null, |
|
1300 |
'protected' => null, |
|
1301 |
'private' => null, |
|
1302 |
'publicly_queryable' => null, |
|
0 | 1303 |
'show_in_admin_status_list' => null, |
9 | 1304 |
'show_in_admin_all_list' => null, |
16 | 1305 |
'date_floating' => null, |
0 | 1306 |
); |
9 | 1307 |
$args = wp_parse_args( $args, $defaults ); |
1308 |
$args = (object) $args; |
|
1309 |
||
1310 |
$post_status = sanitize_key( $post_status ); |
|
1311 |
$args->name = $post_status; |
|
0 | 1312 |
|
5 | 1313 |
// Set various defaults. |
9 | 1314 |
if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) { |
0 | 1315 |
$args->internal = true; |
9 | 1316 |
} |
1317 |
||
1318 |
if ( null === $args->public ) { |
|
0 | 1319 |
$args->public = false; |
9 | 1320 |
} |
1321 |
||
1322 |
if ( null === $args->private ) { |
|
0 | 1323 |
$args->private = false; |
9 | 1324 |
} |
1325 |
||
1326 |
if ( null === $args->protected ) { |
|
0 | 1327 |
$args->protected = false; |
9 | 1328 |
} |
1329 |
||
1330 |
if ( null === $args->internal ) { |
|
0 | 1331 |
$args->internal = false; |
9 | 1332 |
} |
1333 |
||
1334 |
if ( null === $args->publicly_queryable ) { |
|
0 | 1335 |
$args->publicly_queryable = $args->public; |
9 | 1336 |
} |
1337 |
||
1338 |
if ( null === $args->exclude_from_search ) { |
|
0 | 1339 |
$args->exclude_from_search = $args->internal; |
9 | 1340 |
} |
1341 |
||
1342 |
if ( null === $args->show_in_admin_all_list ) { |
|
1343 |
$args->show_in_admin_all_list = ! $args->internal; |
|
1344 |
} |
|
1345 |
||
1346 |
if ( null === $args->show_in_admin_status_list ) { |
|
1347 |
$args->show_in_admin_status_list = ! $args->internal; |
|
1348 |
} |
|
1349 |
||
16 | 1350 |
if ( null === $args->date_floating ) { |
1351 |
$args->date_floating = false; |
|
1352 |
} |
|
1353 |
||
9 | 1354 |
if ( false === $args->label ) { |
0 | 1355 |
$args->label = $post_status; |
9 | 1356 |
} |
1357 |
||
1358 |
if ( false === $args->label_count ) { |
|
1359 |
// phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
$args->label_count = _n_noop( $args->label, $args->label ); |
9 | 1361 |
} |
1362 |
||
1363 |
$wp_post_statuses[ $post_status ] = $args; |
|
0 | 1364 |
|
1365 |
return $args; |
|
1366 |
} |
|
1367 |
||
1368 |
/** |
|
5 | 1369 |
* Retrieve a post status object by name. |
1370 |
* |
|
0 | 1371 |
* @since 3.0.0 |
5 | 1372 |
* |
19 | 1373 |
* @global stdClass[] $wp_post_statuses List of post statuses. |
5 | 1374 |
* |
1375 |
* @see register_post_status() |
|
1376 |
* |
|
1377 |
* @param string $post_status The name of a registered post status. |
|
19 | 1378 |
* @return stdClass|null A post status object. |
0 | 1379 |
*/ |
1380 |
function get_post_status_object( $post_status ) { |
|
1381 |
global $wp_post_statuses; |
|
1382 |
||
9 | 1383 |
if ( empty( $wp_post_statuses[ $post_status ] ) ) { |
0 | 1384 |
return null; |
9 | 1385 |
} |
1386 |
||
1387 |
return $wp_post_statuses[ $post_status ]; |
|
0 | 1388 |
} |
1389 |
||
1390 |
/** |
|
5 | 1391 |
* Get a list of post statuses. |
1392 |
* |
|
0 | 1393 |
* @since 3.0.0 |
5 | 1394 |
* |
19 | 1395 |
* @global stdClass[] $wp_post_statuses List of post statuses. |
5 | 1396 |
* |
1397 |
* @see register_post_status() |
|
1398 |
* |
|
1399 |
* @param array|string $args Optional. Array or string of post status arguments to compare against |
|
1400 |
* properties of the global `$wp_post_statuses objects`. Default empty array. |
|
1401 |
* @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'. |
|
1402 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one element |
|
1403 |
* from the array needs to match; 'and' means all elements must match. |
|
1404 |
* Default 'and'. |
|
19 | 1405 |
* @return string[]|stdClass[] A list of post status names or objects. |
0 | 1406 |
*/ |
1407 |
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { |
|
1408 |
global $wp_post_statuses; |
|
1409 |
||
16 | 1410 |
$field = ( 'names' === $output ) ? 'name' : false; |
9 | 1411 |
|
1412 |
return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field ); |
|
0 | 1413 |
} |
1414 |
||
1415 |
/** |
|
1416 |
* Whether the post type is hierarchical. |
|
1417 |
* |
|
1418 |
* A false return value might also mean that the post type does not exist. |
|
1419 |
* |
|
1420 |
* @since 3.0.0 |
|
5 | 1421 |
* |
1422 |
* @see get_post_type_object() |
|
0 | 1423 |
* |
1424 |
* @param string $post_type Post type name |
|
1425 |
* @return bool Whether post type is hierarchical. |
|
1426 |
*/ |
|
1427 |
function is_post_type_hierarchical( $post_type ) { |
|
9 | 1428 |
if ( ! post_type_exists( $post_type ) ) { |
0 | 1429 |
return false; |
9 | 1430 |
} |
0 | 1431 |
|
1432 |
$post_type = get_post_type_object( $post_type ); |
|
1433 |
return $post_type->hierarchical; |
|
1434 |
} |
|
1435 |
||
1436 |
/** |
|
9 | 1437 |
* Determines whether a post type is registered. |
1438 |
* |
|
1439 |
* For more information on this and similar theme functions, check out |
|
1440 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1441 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 1442 |
* |
1443 |
* @since 3.0.0 |
|
5 | 1444 |
* |
1445 |
* @see get_post_type_object() |
|
1446 |
* |
|
1447 |
* @param string $post_type Post type name. |
|
0 | 1448 |
* @return bool Whether post type is registered. |
1449 |
*/ |
|
1450 |
function post_type_exists( $post_type ) { |
|
1451 |
return (bool) get_post_type_object( $post_type ); |
|
1452 |
} |
|
1453 |
||
1454 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1455 |
* Retrieves the post type of the current post or of a given post. |
0 | 1456 |
* |
1457 |
* @since 2.1.0 |
|
1458 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
* @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1460 |
* @return string|false Post type on success, false on failure. |
0 | 1461 |
*/ |
1462 |
function get_post_type( $post = null ) { |
|
16 | 1463 |
$post = get_post( $post ); |
1464 |
if ( $post ) { |
|
0 | 1465 |
return $post->post_type; |
9 | 1466 |
} |
0 | 1467 |
|
1468 |
return false; |
|
1469 |
} |
|
1470 |
||
1471 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1472 |
* Retrieves a post type object by name. |
5 | 1473 |
* |
0 | 1474 |
* @since 3.0.0 |
9 | 1475 |
* @since 4.6.0 Object returned is now an instance of `WP_Post_Type`. |
5 | 1476 |
* |
1477 |
* @global array $wp_post_types List of post types. |
|
1478 |
* |
|
1479 |
* @see register_post_type() |
|
1480 |
* |
|
1481 |
* @param string $post_type The name of a registered post type. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1482 |
* @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. |
0 | 1483 |
*/ |
1484 |
function get_post_type_object( $post_type ) { |
|
1485 |
global $wp_post_types; |
|
1486 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1487 |
if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { |
0 | 1488 |
return null; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
return $wp_post_types[ $post_type ]; |
0 | 1492 |
} |
1493 |
||
1494 |
/** |
|
1495 |
* Get a list of all registered post type objects. |
|
1496 |
* |
|
1497 |
* @since 2.9.0 |
|
5 | 1498 |
* |
1499 |
* @global array $wp_post_types List of post types. |
|
1500 |
* |
|
1501 |
* @see register_post_type() for accepted arguments. |
|
1502 |
* |
|
1503 |
* @param array|string $args Optional. An array of key => value arguments to match against |
|
1504 |
* the post type objects. Default empty array. |
|
1505 |
* @param string $output Optional. The type of output to return. Accepts post type 'names' |
|
1506 |
* or 'objects'. Default 'names'. |
|
1507 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one |
|
1508 |
* element from the array needs to match; 'and' means all elements |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1509 |
* must match; 'not' means no elements may match. Default 'and'. |
9 | 1510 |
* @return string[]|WP_Post_Type[] An array of post type names or objects. |
0 | 1511 |
*/ |
1512 |
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { |
|
1513 |
global $wp_post_types; |
|
1514 |
||
16 | 1515 |
$field = ( 'names' === $output ) ? 'name' : false; |
9 | 1516 |
|
1517 |
return wp_filter_object_list( $wp_post_types, $args, $operator, $field ); |
|
0 | 1518 |
} |
1519 |
||
1520 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1521 |
* Registers a post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1522 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1523 |
* Note: Post type registrations should not be hooked before the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1524 |
* {@see 'init'} action. Also, any taxonomy connections should be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1525 |
* registered via the `$taxonomies` argument to ensure consistency |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1526 |
* when hooks such as {@see 'parse_query'} or {@see 'pre_get_posts'} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1527 |
* are used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1528 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1529 |
* Post types can support any number of built-in core features such |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1530 |
* as meta boxes, custom fields, post thumbnails, post statuses, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1531 |
* comments, and more. See the `$supports` argument for a complete |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1532 |
* list of supported features. |
0 | 1533 |
* |
1534 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1535 |
* @since 3.0.0 The `show_ui` argument is now enforced on the new post screen. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1536 |
* @since 4.4.0 The `show_ui` argument is now enforced on the post type listing |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1537 |
* screen and post editing screen. |
9 | 1538 |
* @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`. |
1539 |
* @since 4.7.0 Introduced `show_in_rest`, `rest_base` and `rest_controller_class` |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
* arguments to register the post type in REST API. |
18 | 1541 |
* @since 5.0.0 The `template` and `template_lock` arguments were added. |
16 | 1542 |
* @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature. |
19 | 1543 |
* @since 5.9.0 The `rest_namespace` argument was added. |
18 | 1544 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1545 |
* @global array $wp_post_types List of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
* |
16 | 1547 |
* @param string $post_type Post type key. Must not exceed 20 characters and may |
1548 |
* only contain lowercase alphanumeric characters, dashes, |
|
1549 |
* and underscores. See sanitize_key(). |
|
5 | 1550 |
* @param array|string $args { |
1551 |
* Array or string of arguments for registering a post type. |
|
1552 |
* |
|
18 | 1553 |
* @type string $label Name of the post type shown in the menu. Usually plural. |
1554 |
* Default is value of $labels['name']. |
|
1555 |
* @type string[] $labels An array of labels for this post type. If not set, post |
|
1556 |
* labels are inherited for non-hierarchical types and page |
|
1557 |
* labels for hierarchical ones. See get_post_type_labels() for a full |
|
1558 |
* list of supported labels. |
|
1559 |
* @type string $description A short descriptive summary of what the post type is. |
|
1560 |
* Default empty. |
|
1561 |
* @type bool $public Whether a post type is intended for use publicly either via |
|
1562 |
* the admin interface or by front-end users. While the default |
|
1563 |
* settings of $exclude_from_search, $publicly_queryable, $show_ui, |
|
19 | 1564 |
* and $show_in_nav_menus are inherited from $public, each does not |
18 | 1565 |
* rely on this relationship and controls a very specific intention. |
1566 |
* Default false. |
|
1567 |
* @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. |
|
1568 |
* @type bool $exclude_from_search Whether to exclude posts with this post type from front end search |
|
1569 |
* results. Default is the opposite value of $public. |
|
1570 |
* @type bool $publicly_queryable Whether queries can be performed on the front end for the post type |
|
1571 |
* as part of parse_request(). Endpoints would include: |
|
1572 |
* * ?post_type={post_type_key} |
|
1573 |
* * ?{post_type_key}={single_post_slug} |
|
1574 |
* * ?{post_type_query_var}={single_post_slug} |
|
1575 |
* If not set, the default is inherited from $public. |
|
1576 |
* @type bool $show_ui Whether to generate and allow a UI for managing this post type in the |
|
1577 |
* admin. Default is value of $public. |
|
1578 |
* @type bool|string $show_in_menu Where to show the post type in the admin menu. To work, $show_ui |
|
1579 |
* must be true. If true, the post type is shown in its own top level |
|
1580 |
* menu. If false, no menu is shown. If a string of an existing top |
|
19 | 1581 |
* level menu ('tools.php' or 'edit.php?post_type=page', for example), the |
1582 |
* post type will be placed as a sub-menu of that. |
|
18 | 1583 |
* Default is value of $show_ui. |
1584 |
* @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. |
|
1585 |
* Default is value of $public. |
|
1586 |
* @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value |
|
1587 |
* of $show_in_menu. |
|
1588 |
* @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true |
|
1589 |
* for the post type to be available in the block editor. |
|
19 | 1590 |
* @type string $rest_base To change the base URL of REST API route. Default is $post_type. |
1591 |
* @type string $rest_namespace To change the namespace URL of REST API route. Default is wp/v2. |
|
1592 |
* @type string $rest_controller_class REST API controller class name. Default is 'WP_REST_Posts_Controller'. |
|
18 | 1593 |
* @type int $menu_position The position in the menu order the post type should appear. To work, |
1594 |
* $show_in_menu must be true. Default null (at the bottom). |
|
19 | 1595 |
* @type string $menu_icon The URL to the icon to be used for this menu. Pass a base64-encoded |
18 | 1596 |
* SVG using a data URI, which will be colored to match the color scheme |
1597 |
* -- this should begin with 'data:image/svg+xml;base64,'. Pass the name |
|
1598 |
* of a Dashicons helper class to use a font icon, e.g. |
|
1599 |
* 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty |
|
1600 |
* so an icon can be added via CSS. Defaults to use the posts icon. |
|
19 | 1601 |
* @type string|array $capability_type The string to use to build the read, edit, and delete capabilities. |
18 | 1602 |
* May be passed as an array to allow for alternative plurals when using |
1603 |
* this argument as a base to construct the capabilities, e.g. |
|
1604 |
* array('story', 'stories'). Default 'post'. |
|
1605 |
* @type string[] $capabilities Array of capabilities for this post type. $capability_type is used |
|
1606 |
* as a base to construct capabilities by default. |
|
1607 |
* See get_post_type_capabilities(). |
|
1608 |
* @type bool $map_meta_cap Whether to use the internal default meta capability handling. |
|
1609 |
* Default false. |
|
1610 |
* @type array $supports Core feature(s) the post type supports. Serves as an alias for calling |
|
1611 |
* add_post_type_support() directly. Core features include 'title', |
|
1612 |
* 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
|
1613 |
* 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
|
1614 |
* Additionally, the 'revisions' feature dictates whether the post type |
|
1615 |
* will store revisions, and the 'comments' feature dictates whether the |
|
1616 |
* comments count will show on the edit screen. A feature can also be |
|
1617 |
* specified as an array of arguments to provide additional information |
|
1618 |
* about supporting that feature. |
|
1619 |
* Example: `array( 'my_feature', array( 'field' => 'value' ) )`. |
|
1620 |
* Default is an array containing 'title' and 'editor'. |
|
1621 |
* @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the |
|
1622 |
* edit form. Do remove_meta_box() and add_meta_box() calls in the |
|
1623 |
* callback. Default null. |
|
1624 |
* @type string[] $taxonomies An array of taxonomy identifiers that will be registered for the |
|
1625 |
* post type. Taxonomies can be registered later with register_taxonomy() |
|
1626 |
* or register_taxonomy_for_object_type(). |
|
1627 |
* Default empty array. |
|
1628 |
* @type bool|string $has_archive Whether there should be post type archives, or if a string, the |
|
1629 |
* archive slug to use. Will generate the proper rewrite rules if |
|
1630 |
* $rewrite is enabled. Default false. |
|
1631 |
* @type bool|array $rewrite { |
|
5 | 1632 |
* Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
1633 |
* Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be |
|
1634 |
* passed with any of these keys: |
|
1635 |
* |
|
1636 |
* @type string $slug Customize the permastruct slug. Defaults to $post_type key. |
|
1637 |
* @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. |
|
1638 |
* Default true. |
|
1639 |
* @type bool $feeds Whether the feed permastruct should be built for this post type. |
|
1640 |
* Default is value of $has_archive. |
|
1641 |
* @type bool $pages Whether the permastruct should provide for pagination. Default true. |
|
18 | 1642 |
* @type int $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, |
5 | 1643 |
* inherits from $permalink_epmask. If not specified and permalink_epmask |
1644 |
* is not set, defaults to EP_PERMALINK. |
|
1645 |
* } |
|
18 | 1646 |
* @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type |
1647 |
* key. If false, a post type cannot be loaded at |
|
1648 |
* ?{query_var}={post_slug}. If specified as a string, the query |
|
1649 |
* ?{query_var_string}={post_slug} will be valid. |
|
1650 |
* @type bool $can_export Whether to allow this post type to be exported. Default true. |
|
1651 |
* @type bool $delete_with_user Whether to delete posts of this type when deleting a user. |
|
1652 |
* * If true, posts of this type belonging to the user will be moved |
|
1653 |
* to Trash when the user is deleted. |
|
1654 |
* * If false, posts of this type belonging to the user will *not* |
|
1655 |
* be trashed or deleted. |
|
1656 |
* * If not set (the default), posts are trashed if post type supports |
|
1657 |
* the 'author' feature. Otherwise posts are not trashed or deleted. |
|
1658 |
* Default null. |
|
1659 |
* @type array $template Array of blocks to use as the default initial state for an editor |
|
1660 |
* session. Each item should be an array containing block name and |
|
1661 |
* optional attributes. Default empty array. |
|
1662 |
* @type string|false $template_lock Whether the block template should be locked if $template is set. |
|
1663 |
* * If set to 'all', the user is unable to insert new blocks, |
|
1664 |
* move existing blocks and delete blocks. |
|
1665 |
* * If set to 'insert', the user is able to move existing blocks |
|
1666 |
* but is unable to insert new blocks and delete blocks. |
|
1667 |
* Default false. |
|
1668 |
* @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or |
|
1669 |
* "built-in" post_type. Default false. |
|
1670 |
* @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of |
|
1671 |
* this post type. Default 'post.php?post=%d'. |
|
5 | 1672 |
* } |
16 | 1673 |
* @return WP_Post_Type|WP_Error The registered post type object on success, |
1674 |
* WP_Error object on failure. |
|
0 | 1675 |
*/ |
1676 |
function register_post_type( $post_type, $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1677 |
global $wp_post_types; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1678 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1679 |
if ( ! is_array( $wp_post_types ) ) { |
0 | 1680 |
$wp_post_types = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1681 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1682 |
|
16 | 1683 |
// Sanitize post type name. |
0 | 1684 |
$post_type = sanitize_key( $post_type ); |
1685 |
||
5 | 1686 |
if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1687 |
_doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' ); |
5 | 1688 |
return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); |
1689 |
} |
|
0 | 1690 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1691 |
$post_type_object = new WP_Post_Type( $post_type, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
$post_type_object->add_supports(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1693 |
$post_type_object->add_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1694 |
$post_type_object->register_meta_boxes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1695 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1696 |
$wp_post_types[ $post_type ] = $post_type_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1697 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1698 |
$post_type_object->add_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1699 |
$post_type_object->register_taxonomies(); |
0 | 1700 |
|
5 | 1701 |
/** |
1702 |
* Fires after a post type is registered. |
|
1703 |
* |
|
1704 |
* @since 3.3.0 |
|
9 | 1705 |
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
5 | 1706 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1707 |
* @param string $post_type Post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1708 |
* @param WP_Post_Type $post_type_object Arguments used to register the post type. |
5 | 1709 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1710 |
do_action( 'registered_post_type', $post_type, $post_type_object ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1711 |
|
19 | 1712 |
/** |
1713 |
* Fires after a specific post type is registered. |
|
1714 |
* |
|
1715 |
* The dynamic portion of the filter name, `$post_type`, refers to the post type key. |
|
1716 |
* |
|
1717 |
* Possible hook names include: |
|
1718 |
* |
|
1719 |
* - `registered_post_type_post` |
|
1720 |
* - `registered_post_type_page` |
|
1721 |
* |
|
1722 |
* @since 6.0.0 |
|
1723 |
* |
|
1724 |
* @param string $post_type Post type. |
|
1725 |
* @param WP_Post_Type $post_type_object Arguments used to register the post type. |
|
1726 |
*/ |
|
1727 |
do_action( "registered_post_type_{$post_type}", $post_type, $post_type_object ); |
|
1728 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1729 |
return $post_type_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1730 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1731 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1732 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
* Unregisters a post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1734 |
* |
19 | 1735 |
* Cannot be used to unregister built-in post types. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1736 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1737 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1738 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1739 |
* @global array $wp_post_types List of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1740 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1741 |
* @param string $post_type Post type to unregister. |
18 | 1742 |
* @return true|WP_Error True on success, WP_Error on failure or if the post type doesn't exist. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1743 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1744 |
function unregister_post_type( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1745 |
global $wp_post_types; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1746 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1747 |
if ( ! post_type_exists( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1749 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1750 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1751 |
$post_type_object = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1752 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1753 |
// Do not allow unregistering internal post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1754 |
if ( $post_type_object->_builtin ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1755 |
return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1756 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1757 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1758 |
$post_type_object->remove_supports(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
$post_type_object->remove_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1760 |
$post_type_object->unregister_meta_boxes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1761 |
$post_type_object->remove_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1762 |
$post_type_object->unregister_taxonomies(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1763 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1764 |
unset( $wp_post_types[ $post_type ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1765 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1766 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1767 |
* Fires after a post type was unregistered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1768 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1769 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1770 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1771 |
* @param string $post_type Post type key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1772 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1773 |
do_action( 'unregistered_post_type', $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1774 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1775 |
return true; |
0 | 1776 |
} |
1777 |
||
1778 |
/** |
|
5 | 1779 |
* Build an object with all post type capabilities out of a post type object |
0 | 1780 |
* |
1781 |
* Post type capabilities use the 'capability_type' argument as a base, if the |
|
1782 |
* capability is not set in the 'capabilities' argument array or if the |
|
1783 |
* 'capabilities' argument is not supplied. |
|
1784 |
* |
|
1785 |
* The capability_type argument can optionally be registered as an array, with |
|
1786 |
* the first value being singular and the second plural, e.g. array('story, 'stories') |
|
1787 |
* Otherwise, an 's' will be added to the value for the plural form. After |
|
1788 |
* registration, capability_type will always be a string of the singular value. |
|
1789 |
* |
|
16 | 1790 |
* By default, eight keys are accepted as part of the capabilities array: |
0 | 1791 |
* |
1792 |
* - edit_post, read_post, and delete_post are meta capabilities, which are then |
|
1793 |
* generally mapped to corresponding primitive capabilities depending on the |
|
1794 |
* context, which would be the post being edited/read/deleted and the user or |
|
1795 |
* role being checked. Thus these capabilities would generally not be granted |
|
1796 |
* directly to users or roles. |
|
1797 |
* |
|
1798 |
* - edit_posts - Controls whether objects of this post type can be edited. |
|
1799 |
* - edit_others_posts - Controls whether objects of this type owned by other users |
|
1800 |
* can be edited. If the post type does not support an author, then this will |
|
1801 |
* behave like edit_posts. |
|
16 | 1802 |
* - delete_posts - Controls whether objects of this post type can be deleted. |
0 | 1803 |
* - publish_posts - Controls publishing objects of this post type. |
1804 |
* - read_private_posts - Controls whether private objects can be read. |
|
1805 |
* |
|
16 | 1806 |
* These five primitive capabilities are checked in core in various locations. |
1807 |
* There are also six other primitive capabilities which are not referenced |
|
0 | 1808 |
* directly in core, except in map_meta_cap(), which takes the three aforementioned |
1809 |
* meta capabilities and translates them into one or more primitive capabilities |
|
1810 |
* that must then be checked against the user or role, depending on the context. |
|
1811 |
* |
|
1812 |
* - read - Controls whether objects of this post type can be read. |
|
1813 |
* - delete_private_posts - Controls whether private objects can be deleted. |
|
1814 |
* - delete_published_posts - Controls whether published objects can be deleted. |
|
1815 |
* - delete_others_posts - Controls whether objects owned by other users can be |
|
1816 |
* can be deleted. If the post type does not support an author, then this will |
|
1817 |
* behave like delete_posts. |
|
1818 |
* - edit_private_posts - Controls whether private objects can be edited. |
|
1819 |
* - edit_published_posts - Controls whether published objects can be edited. |
|
1820 |
* |
|
1821 |
* These additional capabilities are only used in map_meta_cap(). Thus, they are |
|
1822 |
* only assigned by default if the post type is registered with the 'map_meta_cap' |
|
1823 |
* argument set to true (default is false). |
|
1824 |
* |
|
5 | 1825 |
* @since 3.0.0 |
16 | 1826 |
* @since 5.4.0 'delete_posts' is included in default capabilities. |
5 | 1827 |
* |
1828 |
* @see register_post_type() |
|
0 | 1829 |
* @see map_meta_cap() |
5 | 1830 |
* |
1831 |
* @param object $args Post type registration arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1832 |
* @return object Object with all the capabilities as member variables. |
0 | 1833 |
*/ |
1834 |
function get_post_type_capabilities( $args ) { |
|
9 | 1835 |
if ( ! is_array( $args->capability_type ) ) { |
0 | 1836 |
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); |
9 | 1837 |
} |
0 | 1838 |
|
1839 |
// Singular base for meta capabilities, plural base for primitive capabilities. |
|
1840 |
list( $singular_base, $plural_base ) = $args->capability_type; |
|
1841 |
||
1842 |
$default_capabilities = array( |
|
16 | 1843 |
// Meta capabilities. |
9 | 1844 |
'edit_post' => 'edit_' . $singular_base, |
1845 |
'read_post' => 'read_' . $singular_base, |
|
1846 |
'delete_post' => 'delete_' . $singular_base, |
|
0 | 1847 |
// Primitive capabilities used outside of map_meta_cap(): |
9 | 1848 |
'edit_posts' => 'edit_' . $plural_base, |
1849 |
'edit_others_posts' => 'edit_others_' . $plural_base, |
|
16 | 1850 |
'delete_posts' => 'delete_' . $plural_base, |
9 | 1851 |
'publish_posts' => 'publish_' . $plural_base, |
0 | 1852 |
'read_private_posts' => 'read_private_' . $plural_base, |
1853 |
); |
|
1854 |
||
1855 |
// Primitive capabilities used within map_meta_cap(): |
|
1856 |
if ( $args->map_meta_cap ) { |
|
1857 |
$default_capabilities_for_mapping = array( |
|
1858 |
'read' => 'read', |
|
9 | 1859 |
'delete_private_posts' => 'delete_private_' . $plural_base, |
0 | 1860 |
'delete_published_posts' => 'delete_published_' . $plural_base, |
9 | 1861 |
'delete_others_posts' => 'delete_others_' . $plural_base, |
1862 |
'edit_private_posts' => 'edit_private_' . $plural_base, |
|
1863 |
'edit_published_posts' => 'edit_published_' . $plural_base, |
|
0 | 1864 |
); |
9 | 1865 |
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); |
0 | 1866 |
} |
1867 |
||
1868 |
$capabilities = array_merge( $default_capabilities, $args->capabilities ); |
|
1869 |
||
1870 |
// Post creation capability simply maps to edit_posts by default: |
|
9 | 1871 |
if ( ! isset( $capabilities['create_posts'] ) ) { |
0 | 1872 |
$capabilities['create_posts'] = $capabilities['edit_posts']; |
9 | 1873 |
} |
0 | 1874 |
|
1875 |
// Remember meta capabilities for future reference. |
|
9 | 1876 |
if ( $args->map_meta_cap ) { |
0 | 1877 |
_post_type_meta_capabilities( $capabilities ); |
9 | 1878 |
} |
0 | 1879 |
|
1880 |
return (object) $capabilities; |
|
1881 |
} |
|
1882 |
||
1883 |
/** |
|
5 | 1884 |
* Store or return a list of post type meta caps for map_meta_cap(). |
0 | 1885 |
* |
1886 |
* @since 3.1.0 |
|
1887 |
* @access private |
|
5 | 1888 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1889 |
* @global array $post_type_meta_caps Used to store meta capabilities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1890 |
* |
16 | 1891 |
* @param string[] $capabilities Post type meta capabilities. |
0 | 1892 |
*/ |
1893 |
function _post_type_meta_capabilities( $capabilities = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1894 |
global $post_type_meta_caps; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1895 |
|
0 | 1896 |
foreach ( $capabilities as $core => $custom ) { |
16 | 1897 |
if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1898 |
$post_type_meta_caps[ $custom ] = $core; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1899 |
} |
0 | 1900 |
} |
1901 |
} |
|
1902 |
||
1903 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1904 |
* Builds an object with all post type labels out of a post type object. |
0 | 1905 |
* |
1906 |
* Accepted keys of the label array in the post type object: |
|
5 | 1907 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1908 |
* - `name` - General name for the post type, usually plural. The same and overridden |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1909 |
* by `$post_type_object->label`. Default is 'Posts' / 'Pages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1910 |
* - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1911 |
* - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. |
16 | 1912 |
* When internationalizing this string, please use a {@link https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#disambiguation-by-context gettext context} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1913 |
* matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1914 |
* - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1915 |
* - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1916 |
* - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1917 |
* - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1918 |
* - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1919 |
* - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1920 |
* - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. |
16 | 1921 |
* - `not_found_in_trash` - Label used when no items are in the Trash. Default is 'No posts found in Trash' / |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1922 |
* 'No pages found in Trash'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1923 |
* - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1924 |
* post types. Default is 'Parent Page:'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1925 |
* - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1926 |
* - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1927 |
* - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1928 |
* - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1929 |
* - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' / |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1930 |
* 'Uploaded to this page'. |
16 | 1931 |
* - `featured_image` - Label for the featured image meta box title. Default is 'Featured image'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1932 |
* - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1933 |
* - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1934 |
* - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1935 |
* - `menu_name` - Label for the menu name. Default is the same as `name`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
* - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' / |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
* 'Filter pages list'. |
18 | 1938 |
* - `filter_by_date` - Label for the date filter in list tables. Default is 'Filter by date'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1939 |
* - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' / |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
* 'Pages list navigation'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1941 |
* - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. |
9 | 1942 |
* - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' |
1943 |
* - `item_published_privately` - Label used when an item is published with private visibility. |
|
1944 |
* Default is 'Post published privately.' / 'Page published privately.' |
|
1945 |
* - `item_reverted_to_draft` - Label used when an item is switched to a draft. |
|
1946 |
* Default is 'Post reverted to draft.' / 'Page reverted to draft.' |
|
1947 |
* - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / |
|
1948 |
* 'Page scheduled.' |
|
1949 |
* - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' |
|
18 | 1950 |
* - `item_link` - Title for a navigation link block variation. Default is 'Post Link' / 'Page Link'. |
1951 |
* - `item_link_description` - Description for a navigation link block variation. Default is 'A link to a post.' / |
|
1952 |
* 'A link to a page.' |
|
5 | 1953 |
* |
1954 |
* Above, the first default value is for non-hierarchical post types (like posts) |
|
1955 |
* and the second one is for hierarchical post types (like pages). |
|
0 | 1956 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1957 |
* Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1958 |
* |
0 | 1959 |
* @since 3.0.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1960 |
* @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1961 |
* and `use_featured_image` labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1962 |
* @since 4.4.0 Added the `archives`, `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1963 |
* `items_list_navigation`, and `items_list` labels. |
9 | 1964 |
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1965 |
* @since 4.7.0 Added the `view_items` and `attributes` labels. |
9 | 1966 |
* @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, |
1967 |
* `item_scheduled`, and `item_updated` labels. |
|
18 | 1968 |
* @since 5.7.0 Added the `filter_by_date` label. |
1969 |
* @since 5.8.0 Added the `item_link` and `item_link_description` labels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1970 |
* |
0 | 1971 |
* @access private |
1972 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1973 |
* @param object|WP_Post_Type $post_type_object Post type object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1974 |
* @return object Object with all the labels as member variables. |
0 | 1975 |
*/ |
1976 |
function get_post_type_labels( $post_type_object ) { |
|
19 | 1977 |
$nohier_vs_hier_defaults = WP_Post_Type::get_default_labels(); |
18 | 1978 |
|
0 | 1979 |
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
1980 |
||
1981 |
$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
|
1982 |
||
1983 |
$post_type = $post_type_object->name; |
|
5 | 1984 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1985 |
$default_labels = clone $labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1986 |
|
5 | 1987 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1988 |
* Filters the labels of a specific post type. |
5 | 1989 |
* |
1990 |
* The dynamic portion of the hook name, `$post_type`, refers to |
|
1991 |
* the post type slug. |
|
1992 |
* |
|
18 | 1993 |
* Possible hook names include: |
1994 |
* |
|
1995 |
* - `post_type_labels_post` |
|
1996 |
* - `post_type_labels_page` |
|
1997 |
* - `post_type_labels_attachment` |
|
1998 |
* |
|
5 | 1999 |
* @since 3.5.0 |
2000 |
* |
|
2001 |
* @see get_post_type_labels() for the full list of labels. |
|
2002 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2003 |
* @param object $labels Object with labels for the post type as member variables. |
5 | 2004 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2005 |
$labels = apply_filters( "post_type_labels_{$post_type}", $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2006 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2007 |
// Ensure that the filtered labels contain all required default values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2008 |
$labels = (object) array_merge( (array) $default_labels, (array) $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2009 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2010 |
return $labels; |
0 | 2011 |
} |
2012 |
||
2013 |
/** |
|
5 | 2014 |
* Build an object with custom-something object (post type, taxonomy) labels |
2015 |
* out of a custom-something object |
|
2016 |
* |
|
2017 |
* @since 3.0.0 |
|
0 | 2018 |
* @access private |
5 | 2019 |
* |
2020 |
* @param object $object A custom-something object. |
|
2021 |
* @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2022 |
* @return object Object containing labels for the given custom-something object. |
0 | 2023 |
*/ |
2024 |
function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { |
|
2025 |
$object->labels = (array) $object->labels; |
|
2026 |
||
9 | 2027 |
if ( isset( $object->label ) && empty( $object->labels['name'] ) ) { |
0 | 2028 |
$object->labels['name'] = $object->label; |
9 | 2029 |
} |
2030 |
||
2031 |
if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) { |
|
0 | 2032 |
$object->labels['singular_name'] = $object->labels['name']; |
9 | 2033 |
} |
2034 |
||
2035 |
if ( ! isset( $object->labels['name_admin_bar'] ) ) { |
|
0 | 2036 |
$object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; |
9 | 2037 |
} |
2038 |
||
2039 |
if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) { |
|
0 | 2040 |
$object->labels['menu_name'] = $object->labels['name']; |
9 | 2041 |
} |
2042 |
||
2043 |
if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) { |
|
0 | 2044 |
$object->labels['all_items'] = $object->labels['menu_name']; |
9 | 2045 |
} |
2046 |
||
2047 |
if ( ! isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2048 |
$object->labels['archives'] = $object->labels['all_items']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2049 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2050 |
|
5 | 2051 |
$defaults = array(); |
2052 |
foreach ( $nohier_vs_hier_defaults as $key => $value ) { |
|
9 | 2053 |
$defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0]; |
2054 |
} |
|
2055 |
$labels = array_merge( $defaults, $object->labels ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2056 |
$object->labels = (object) $object->labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2057 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2058 |
return (object) $labels; |
0 | 2059 |
} |
2060 |
||
2061 |
/** |
|
5 | 2062 |
* Add submenus for post types. |
0 | 2063 |
* |
2064 |
* @access private |
|
2065 |
* @since 3.1.0 |
|
2066 |
*/ |
|
2067 |
function _add_post_type_submenus() { |
|
2068 |
foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { |
|
2069 |
$ptype_obj = get_post_type_object( $ptype ); |
|
5 | 2070 |
// Sub-menus only. |
16 | 2071 |
if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) { |
0 | 2072 |
continue; |
9 | 2073 |
} |
0 | 2074 |
add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); |
2075 |
} |
|
2076 |
} |
|
2077 |
||
2078 |
/** |
|
16 | 2079 |
* Registers support of certain features for a post type. |
0 | 2080 |
* |
5 | 2081 |
* All core features are directly associated with a functional area of the edit |
2082 |
* screen, such as the editor or a meta box. Features include: 'title', 'editor', |
|
2083 |
* 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', |
|
2084 |
* 'thumbnail', 'custom-fields', and 'post-formats'. |
|
2085 |
* |
|
2086 |
* Additionally, the 'revisions' feature dictates whether the post type will |
|
2087 |
* store revisions, and the 'comments' feature dictates whether the comments |
|
2088 |
* count will show on the edit screen. |
|
0 | 2089 |
* |
16 | 2090 |
* A third, optional parameter can also be passed along with a feature to provide |
2091 |
* additional information about supporting that feature. |
|
2092 |
* |
|
2093 |
* Example usage: |
|
2094 |
* |
|
2095 |
* add_post_type_support( 'my_post_type', 'comments' ); |
|
2096 |
* add_post_type_support( 'my_post_type', array( |
|
2097 |
* 'author', 'excerpt', |
|
2098 |
* ) ); |
|
2099 |
* add_post_type_support( 'my_post_type', 'my_feature', array( |
|
2100 |
* 'field' => 'value', |
|
2101 |
* ) ); |
|
2102 |
* |
|
0 | 2103 |
* @since 3.0.0 |
16 | 2104 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
2105 |
* by adding it to the function signature. |
|
5 | 2106 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2107 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2108 |
* |
5 | 2109 |
* @param string $post_type The post type for which to add the feature. |
2110 |
* @param string|array $feature The feature being added, accepts an array of |
|
2111 |
* feature strings or a single string. |
|
16 | 2112 |
* @param mixed ...$args Optional extra arguments to pass along with certain features. |
2113 |
*/ |
|
2114 |
function add_post_type_support( $post_type, $feature, ...$args ) { |
|
0 | 2115 |
global $_wp_post_type_features; |
2116 |
||
2117 |
$features = (array) $feature; |
|
9 | 2118 |
foreach ( $features as $feature ) { |
16 | 2119 |
if ( $args ) { |
2120 |
$_wp_post_type_features[ $post_type ][ $feature ] = $args; |
|
2121 |
} else { |
|
9 | 2122 |
$_wp_post_type_features[ $post_type ][ $feature ] = true; |
2123 |
} |
|
0 | 2124 |
} |
2125 |
} |
|
2126 |
||
2127 |
/** |
|
2128 |
* Remove support for a feature from a post type. |
|
2129 |
* |
|
2130 |
* @since 3.0.0 |
|
5 | 2131 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2132 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2133 |
* |
5 | 2134 |
* @param string $post_type The post type for which to remove the feature. |
2135 |
* @param string $feature The feature being removed. |
|
0 | 2136 |
*/ |
2137 |
function remove_post_type_support( $post_type, $feature ) { |
|
2138 |
global $_wp_post_type_features; |
|
2139 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2140 |
unset( $_wp_post_type_features[ $post_type ][ $feature ] ); |
0 | 2141 |
} |
2142 |
||
2143 |
/** |
|
2144 |
* Get all the post type features |
|
2145 |
* |
|
2146 |
* @since 3.4.0 |
|
5 | 2147 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2148 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2149 |
* |
5 | 2150 |
* @param string $post_type The post type. |
2151 |
* @return array Post type supports list. |
|
0 | 2152 |
*/ |
2153 |
function get_all_post_type_supports( $post_type ) { |
|
2154 |
global $_wp_post_type_features; |
|
2155 |
||
9 | 2156 |
if ( isset( $_wp_post_type_features[ $post_type ] ) ) { |
2157 |
return $_wp_post_type_features[ $post_type ]; |
|
2158 |
} |
|
0 | 2159 |
|
2160 |
return array(); |
|
2161 |
} |
|
2162 |
||
2163 |
/** |
|
5 | 2164 |
* Check a post type's support for a given feature. |
0 | 2165 |
* |
2166 |
* @since 3.0.0 |
|
5 | 2167 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2168 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2169 |
* |
5 | 2170 |
* @param string $post_type The post type being checked. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2171 |
* @param string $feature The feature being checked. |
5 | 2172 |
* @return bool Whether the post type supports the given feature. |
0 | 2173 |
*/ |
2174 |
function post_type_supports( $post_type, $feature ) { |
|
2175 |
global $_wp_post_type_features; |
|
2176 |
||
9 | 2177 |
return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) ); |
0 | 2178 |
} |
2179 |
||
2180 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2181 |
* Retrieves a list of post type names that support a specific feature. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2182 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2183 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2184 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2185 |
* @global array $_wp_post_type_features Post type features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2186 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2187 |
* @param array|string $feature Single feature or an array of features the post types should support. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2188 |
* @param string $operator Optional. The logical operation to perform. 'or' means |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2189 |
* only one element from the array needs to match; 'and' |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2190 |
* means all elements must match; 'not' means no elements may |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2191 |
* match. Default 'and'. |
16 | 2192 |
* @return string[] A list of post type names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2193 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2194 |
function get_post_types_by_support( $feature, $operator = 'and' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2195 |
global $_wp_post_type_features; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2196 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2197 |
$features = array_fill_keys( (array) $feature, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2198 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2199 |
return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2200 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2201 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2202 |
/** |
5 | 2203 |
* Update the post type for the post ID. |
0 | 2204 |
* |
2205 |
* The page or post cache will be cleaned for the post ID. |
|
2206 |
* |
|
2207 |
* @since 2.5.0 |
|
2208 |
* |
|
5 | 2209 |
* @global wpdb $wpdb WordPress database abstraction object. |
2210 |
* |
|
2211 |
* @param int $post_id Optional. Post ID to change post type. Default 0. |
|
2212 |
* @param string $post_type Optional. Post type. Accepts 'post' or 'page' to |
|
2213 |
* name a few. Default 'post'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2214 |
* @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. |
0 | 2215 |
*/ |
2216 |
function set_post_type( $post_id = 0, $post_type = 'post' ) { |
|
2217 |
global $wpdb; |
|
2218 |
||
9 | 2219 |
$post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' ); |
2220 |
$return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) ); |
|
0 | 2221 |
|
2222 |
clean_post_cache( $post_id ); |
|
2223 |
||
2224 |
return $return; |
|
2225 |
} |
|
2226 |
||
2227 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2228 |
* Determines whether a post type is considered "viewable". |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2229 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2230 |
* For built-in post types such as posts and pages, the 'public' value will be evaluated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2231 |
* For all others, the 'publicly_queryable' value will be used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2232 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2233 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2234 |
* @since 4.5.0 Added the ability to pass a post type name in addition to object. |
9 | 2235 |
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
19 | 2236 |
* @since 5.9.0 Added `is_post_type_viewable` hook to filter the result. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2237 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2238 |
* @param string|WP_Post_Type $post_type Post type name or object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2239 |
* @return bool Whether the post type should be considered viewable. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2240 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2241 |
function is_post_type_viewable( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2242 |
if ( is_scalar( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2243 |
$post_type = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2244 |
if ( ! $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2245 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2246 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2247 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2248 |
|
18 | 2249 |
if ( ! is_object( $post_type ) ) { |
2250 |
return false; |
|
2251 |
} |
|
2252 |
||
19 | 2253 |
$is_viewable = $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); |
2254 |
||
2255 |
/** |
|
2256 |
* Filters whether a post type is considered "viewable". |
|
2257 |
* |
|
2258 |
* The returned filtered value must be a boolean type to ensure |
|
2259 |
* `is_post_type_viewable()` only returns a boolean. This strictness |
|
2260 |
* is by design to maintain backwards-compatibility and guard against |
|
2261 |
* potential type errors in PHP 8.1+. Non-boolean values (even falsey |
|
2262 |
* and truthy values) will result in the function returning false. |
|
2263 |
* |
|
2264 |
* @since 5.9.0 |
|
2265 |
* |
|
2266 |
* @param bool $is_viewable Whether the post type is "viewable" (strict type). |
|
2267 |
* @param WP_Post_Type $post_type Post type object. |
|
2268 |
*/ |
|
2269 |
return true === apply_filters( 'is_post_type_viewable', $is_viewable, $post_type ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2270 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2271 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2272 |
/** |
18 | 2273 |
* Determine whether a post status is considered "viewable". |
2274 |
* |
|
19 | 2275 |
* For built-in post statuses such as publish and private, the 'public' value will be evaluated. |
18 | 2276 |
* For all others, the 'publicly_queryable' value will be used. |
2277 |
* |
|
2278 |
* @since 5.7.0 |
|
19 | 2279 |
* @since 5.9.0 Added `is_post_status_viewable` hook to filter the result. |
18 | 2280 |
* |
2281 |
* @param string|stdClass $post_status Post status name or object. |
|
2282 |
* @return bool Whether the post status should be considered viewable. |
|
2283 |
*/ |
|
2284 |
function is_post_status_viewable( $post_status ) { |
|
2285 |
if ( is_scalar( $post_status ) ) { |
|
2286 |
$post_status = get_post_status_object( $post_status ); |
|
2287 |
if ( ! $post_status ) { |
|
2288 |
return false; |
|
2289 |
} |
|
2290 |
} |
|
2291 |
||
2292 |
if ( |
|
2293 |
! is_object( $post_status ) || |
|
2294 |
$post_status->internal || |
|
2295 |
$post_status->protected |
|
2296 |
) { |
|
2297 |
return false; |
|
2298 |
} |
|
2299 |
||
19 | 2300 |
$is_viewable = $post_status->publicly_queryable || ( $post_status->_builtin && $post_status->public ); |
2301 |
||
2302 |
/** |
|
2303 |
* Filters whether a post status is considered "viewable". |
|
2304 |
* |
|
2305 |
* The returned filtered value must be a boolean type to ensure |
|
2306 |
* `is_post_status_viewable()` only returns a boolean. This strictness |
|
2307 |
* is by design to maintain backwards-compatibility and guard against |
|
2308 |
* potential type errors in PHP 8.1+. Non-boolean values (even falsey |
|
2309 |
* and truthy values) will result in the function returning false. |
|
2310 |
* |
|
2311 |
* @since 5.9.0 |
|
2312 |
* |
|
2313 |
* @param bool $is_viewable Whether the post status is "viewable" (strict type). |
|
2314 |
* @param stdClass $post_status Post status object. |
|
2315 |
*/ |
|
2316 |
return true === apply_filters( 'is_post_status_viewable', $is_viewable, $post_status ); |
|
18 | 2317 |
} |
2318 |
||
2319 |
/** |
|
2320 |
* Determine whether a post is publicly viewable. |
|
2321 |
* |
|
2322 |
* Posts are considered publicly viewable if both the post status and post type |
|
2323 |
* are viewable. |
|
2324 |
* |
|
2325 |
* @since 5.7.0 |
|
2326 |
* |
|
2327 |
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
|
2328 |
* @return bool Whether the post is publicly viewable. |
|
2329 |
*/ |
|
2330 |
function is_post_publicly_viewable( $post = null ) { |
|
2331 |
$post = get_post( $post ); |
|
2332 |
||
2333 |
if ( ! $post ) { |
|
2334 |
return false; |
|
2335 |
} |
|
2336 |
||
2337 |
$post_type = get_post_type( $post ); |
|
2338 |
$post_status = get_post_status( $post ); |
|
2339 |
||
2340 |
return is_post_type_viewable( $post_type ) && is_post_status_viewable( $post_status ); |
|
2341 |
} |
|
2342 |
||
2343 |
/** |
|
9 | 2344 |
* Retrieves an array of the latest posts, or posts matching the given criteria. |
0 | 2345 |
* |
18 | 2346 |
* For more information on the accepted arguments, see the |
2347 |
* {@link https://developer.wordpress.org/reference/classes/wp_query/ |
|
2348 |
* WP_Query} documentation in the Developer Handbook. |
|
2349 |
* |
|
2350 |
* The `$ignore_sticky_posts` and `$no_found_rows` arguments are ignored by |
|
2351 |
* this function and both are set to `true`. |
|
2352 |
* |
|
0 | 2353 |
* The defaults are as follows: |
2354 |
* |
|
2355 |
* @since 1.2.0 |
|
5 | 2356 |
* |
18 | 2357 |
* @see WP_Query |
5 | 2358 |
* @see WP_Query::parse_query() |
2359 |
* |
|
2360 |
* @param array $args { |
|
19 | 2361 |
* Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all available arguments. |
5 | 2362 |
* |
18 | 2363 |
* @type int $numberposts Total number of posts to retrieve. Is an alias of `$posts_per_page` |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2364 |
* in WP_Query. Accepts -1 for all. Default 5. |
5 | 2365 |
* @type int|string $category Category ID or comma-separated list of IDs (this or any children). |
18 | 2366 |
* Is an alias of `$cat` in WP_Query. Default 0. |
2367 |
* @type int[] $include An array of post IDs to retrieve, sticky posts will be included. |
|
2368 |
* Is an alias of `$post__in` in WP_Query. Default empty array. |
|
2369 |
* @type int[] $exclude An array of post IDs not to retrieve. Default empty array. |
|
5 | 2370 |
* @type bool $suppress_filters Whether to suppress filters. Default true. |
2371 |
* } |
|
9 | 2372 |
* @return WP_Post[]|int[] Array of post objects or post IDs. |
0 | 2373 |
*/ |
5 | 2374 |
function get_posts( $args = null ) { |
0 | 2375 |
$defaults = array( |
9 | 2376 |
'numberposts' => 5, |
2377 |
'category' => 0, |
|
2378 |
'orderby' => 'date', |
|
2379 |
'order' => 'DESC', |
|
2380 |
'include' => array(), |
|
2381 |
'exclude' => array(), |
|
2382 |
'meta_key' => '', |
|
2383 |
'meta_value' => '', |
|
2384 |
'post_type' => 'post', |
|
2385 |
'suppress_filters' => true, |
|
0 | 2386 |
); |
2387 |
||
16 | 2388 |
$parsed_args = wp_parse_args( $args, $defaults ); |
2389 |
if ( empty( $parsed_args['post_status'] ) ) { |
|
2390 |
$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish'; |
|
2391 |
} |
|
2392 |
if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) { |
|
2393 |
$parsed_args['posts_per_page'] = $parsed_args['numberposts']; |
|
2394 |
} |
|
2395 |
if ( ! empty( $parsed_args['category'] ) ) { |
|
2396 |
$parsed_args['cat'] = $parsed_args['category']; |
|
2397 |
} |
|
2398 |
if ( ! empty( $parsed_args['include'] ) ) { |
|
2399 |
$incposts = wp_parse_id_list( $parsed_args['include'] ); |
|
2400 |
$parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included. |
|
2401 |
$parsed_args['post__in'] = $incposts; |
|
2402 |
} elseif ( ! empty( $parsed_args['exclude'] ) ) { |
|
2403 |
$parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] ); |
|
2404 |
} |
|
2405 |
||
2406 |
$parsed_args['ignore_sticky_posts'] = true; |
|
2407 |
$parsed_args['no_found_rows'] = true; |
|
0 | 2408 |
|
2409 |
$get_posts = new WP_Query; |
|
16 | 2410 |
return $get_posts->query( $parsed_args ); |
0 | 2411 |
|
2412 |
} |
|
2413 |
||
2414 |
// |
|
16 | 2415 |
// Post meta functions. |
0 | 2416 |
// |
2417 |
||
2418 |
/** |
|
9 | 2419 |
* Adds a meta field to the given post. |
0 | 2420 |
* |
2421 |
* Post meta data is called "Custom Fields" on the Administration Screen. |
|
2422 |
* |
|
2423 |
* @since 1.5.0 |
|
5 | 2424 |
* |
2425 |
* @param int $post_id Post ID. |
|
2426 |
* @param string $meta_key Metadata name. |
|
2427 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
2428 |
* @param bool $unique Optional. Whether the same key should not be added. |
|
2429 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2430 |
* @return int|false Meta ID on success, false on failure. |
0 | 2431 |
*/ |
5 | 2432 |
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { |
2433 |
// Make sure meta is added to the post, not a revision. |
|
9 | 2434 |
$the_post = wp_is_post_revision( $post_id ); |
2435 |
if ( $the_post ) { |
|
0 | 2436 |
$post_id = $the_post; |
9 | 2437 |
} |
2438 |
||
2439 |
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); |
|
0 | 2440 |
} |
2441 |
||
2442 |
/** |
|
9 | 2443 |
* Deletes a post meta field for the given post ID. |
0 | 2444 |
* |
2445 |
* You can match based on the key, or key and value. Removing based on key and |
|
2446 |
* value, will keep from removing duplicate metadata with the same key. It also |
|
9 | 2447 |
* allows removing all metadata matching the key, if needed. |
0 | 2448 |
* |
2449 |
* @since 1.5.0 |
|
5 | 2450 |
* |
2451 |
* @param int $post_id Post ID. |
|
2452 |
* @param string $meta_key Metadata name. |
|
16 | 2453 |
* @param mixed $meta_value Optional. Metadata value. If provided, |
2454 |
* rows will only be removed that match the value. |
|
2455 |
* Must be serializable if non-scalar. Default empty. |
|
0 | 2456 |
* @return bool True on success, false on failure. |
2457 |
*/ |
|
5 | 2458 |
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { |
19 | 2459 |
// Make sure meta is deleted from the post, not from a revision. |
9 | 2460 |
$the_post = wp_is_post_revision( $post_id ); |
2461 |
if ( $the_post ) { |
|
0 | 2462 |
$post_id = $the_post; |
9 | 2463 |
} |
2464 |
||
2465 |
return delete_metadata( 'post', $post_id, $meta_key, $meta_value ); |
|
0 | 2466 |
} |
2467 |
||
2468 |
/** |
|
9 | 2469 |
* Retrieves a post meta field for the given post ID. |
0 | 2470 |
* |
2471 |
* @since 1.5.0 |
|
5 | 2472 |
* |
2473 |
* @param int $post_id Post ID. |
|
16 | 2474 |
* @param string $key Optional. The meta key to retrieve. By default, |
2475 |
* returns data for all keys. Default empty. |
|
2476 |
* @param bool $single Optional. Whether to return a single value. |
|
18 | 2477 |
* This parameter has no effect if `$key` is not specified. |
16 | 2478 |
* Default false. |
18 | 2479 |
* @return mixed An array of values if `$single` is false. |
2480 |
* The value of the meta field if `$single` is true. |
|
2481 |
* False for an invalid `$post_id` (non-numeric, zero, or negative value). |
|
2482 |
* An empty string if a valid but non-existing post ID is passed. |
|
0 | 2483 |
*/ |
5 | 2484 |
function get_post_meta( $post_id, $key = '', $single = false ) { |
9 | 2485 |
return get_metadata( 'post', $post_id, $key, $single ); |
0 | 2486 |
} |
2487 |
||
2488 |
/** |
|
9 | 2489 |
* Updates a post meta field based on the given post ID. |
2490 |
* |
|
2491 |
* Use the `$prev_value` parameter to differentiate between meta fields with the |
|
0 | 2492 |
* same key and post ID. |
2493 |
* |
|
9 | 2494 |
* If the meta field for the post does not exist, it will be added and its ID returned. |
2495 |
* |
|
2496 |
* Can be used in place of add_post_meta(). |
|
0 | 2497 |
* |
2498 |
* @since 1.5.0 |
|
5 | 2499 |
* |
2500 |
* @param int $post_id Post ID. |
|
2501 |
* @param string $meta_key Metadata key. |
|
2502 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
9 | 2503 |
* @param mixed $prev_value Optional. Previous value to check before updating. |
16 | 2504 |
* If specified, only update existing metadata entries with |
2505 |
* this value. Otherwise, update all entries. Default empty. |
|
2506 |
* @return int|bool Meta ID if the key didn't exist, true on successful update, |
|
2507 |
* false on failure or if the value passed to the function |
|
2508 |
* is the same as the one that is already in the database. |
|
0 | 2509 |
*/ |
5 | 2510 |
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { |
19 | 2511 |
// Make sure meta is updated for the post, not for a revision. |
9 | 2512 |
$the_post = wp_is_post_revision( $post_id ); |
2513 |
if ( $the_post ) { |
|
0 | 2514 |
$post_id = $the_post; |
9 | 2515 |
} |
2516 |
||
2517 |
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); |
|
0 | 2518 |
} |
2519 |
||
2520 |
/** |
|
9 | 2521 |
* Deletes everything from post meta matching the given meta key. |
0 | 2522 |
* |
2523 |
* @since 2.3.0 |
|
2524 |
* |
|
2525 |
* @param string $post_meta_key Key to search for when deleting. |
|
5 | 2526 |
* @return bool Whether the post meta key was deleted from the database. |
0 | 2527 |
*/ |
5 | 2528 |
function delete_post_meta_by_key( $post_meta_key ) { |
9 | 2529 |
return delete_metadata( 'post', null, $post_meta_key, '', true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2530 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2531 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2532 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2533 |
* Registers a meta key for posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2534 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2535 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2536 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2537 |
* @param string $post_type Post type to register a meta key for. Pass an empty string |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2538 |
* to register the meta key across all existing post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2539 |
* @param string $meta_key The meta key to register. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2540 |
* @param array $args Data used to describe the meta key when registered. See |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2541 |
* {@see register_meta()} for a list of supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2542 |
* @return bool True if the meta key was successfully registered, false if not. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2543 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2544 |
function register_post_meta( $post_type, $meta_key, array $args ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2545 |
$args['object_subtype'] = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2546 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2547 |
return register_meta( 'post', $meta_key, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2548 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2549 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2550 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2551 |
* Unregisters a meta key for posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2552 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2553 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2554 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2555 |
* @param string $post_type Post type the meta key is currently registered for. Pass |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2556 |
* an empty string if the meta key is registered across all |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2557 |
* existing post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2558 |
* @param string $meta_key The meta key to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2559 |
* @return bool True on success, false if the meta key was not previously registered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2560 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2561 |
function unregister_post_meta( $post_type, $meta_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2562 |
return unregister_meta_key( 'post', $meta_key, $post_type ); |
0 | 2563 |
} |
2564 |
||
2565 |
/** |
|
2566 |
* Retrieve post meta fields, based on post ID. |
|
2567 |
* |
|
2568 |
* The post meta fields are retrieved from the cache where possible, |
|
2569 |
* so the function is optimized to be called more than once. |
|
2570 |
* |
|
2571 |
* @since 1.2.0 |
|
5 | 2572 |
* |
19 | 2573 |
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. |
2574 |
* @return mixed An array of values. |
|
2575 |
* False for an invalid `$post_id` (non-numeric, zero, or negative value). |
|
2576 |
* An empty string if a valid but non-existing post ID is passed. |
|
0 | 2577 |
*/ |
2578 |
function get_post_custom( $post_id = 0 ) { |
|
2579 |
$post_id = absint( $post_id ); |
|
9 | 2580 |
if ( ! $post_id ) { |
0 | 2581 |
$post_id = get_the_ID(); |
9 | 2582 |
} |
0 | 2583 |
|
2584 |
return get_post_meta( $post_id ); |
|
2585 |
} |
|
2586 |
||
2587 |
/** |
|
2588 |
* Retrieve meta field names for a post. |
|
2589 |
* |
|
2590 |
* If there are no meta fields, then nothing (null) will be returned. |
|
2591 |
* |
|
2592 |
* @since 1.2.0 |
|
5 | 2593 |
* |
19 | 2594 |
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2595 |
* @return array|void Array of the keys, if retrieved. |
0 | 2596 |
*/ |
2597 |
function get_post_custom_keys( $post_id = 0 ) { |
|
2598 |
$custom = get_post_custom( $post_id ); |
|
2599 |
||
9 | 2600 |
if ( ! is_array( $custom ) ) { |
0 | 2601 |
return; |
9 | 2602 |
} |
2603 |
||
16 | 2604 |
$keys = array_keys( $custom ); |
2605 |
if ( $keys ) { |
|
0 | 2606 |
return $keys; |
9 | 2607 |
} |
0 | 2608 |
} |
2609 |
||
2610 |
/** |
|
2611 |
* Retrieve values for a custom post field. |
|
2612 |
* |
|
2613 |
* The parameters must not be considered optional. All of the post meta fields |
|
2614 |
* will be retrieved and only the meta field key values returned. |
|
2615 |
* |
|
2616 |
* @since 1.2.0 |
|
5 | 2617 |
* |
2618 |
* @param string $key Optional. Meta field key. Default empty. |
|
19 | 2619 |
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2620 |
* @return array|null Meta field values. |
0 | 2621 |
*/ |
2622 |
function get_post_custom_values( $key = '', $post_id = 0 ) { |
|
9 | 2623 |
if ( ! $key ) { |
0 | 2624 |
return null; |
9 | 2625 |
} |
2626 |
||
2627 |
$custom = get_post_custom( $post_id ); |
|
2628 |
||
2629 |
return isset( $custom[ $key ] ) ? $custom[ $key ] : null; |
|
0 | 2630 |
} |
2631 |
||
2632 |
/** |
|
9 | 2633 |
* Determines whether a post is sticky. |
0 | 2634 |
* |
2635 |
* Sticky posts should remain at the top of The Loop. If the post ID is not |
|
2636 |
* given, then The Loop ID for the current post will be used. |
|
2637 |
* |
|
9 | 2638 |
* For more information on this and similar theme functions, check out |
2639 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
2640 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
2641 |
* |
|
0 | 2642 |
* @since 2.7.0 |
2643 |
* |
|
19 | 2644 |
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. |
0 | 2645 |
* @return bool Whether post is sticky. |
2646 |
*/ |
|
2647 |
function is_sticky( $post_id = 0 ) { |
|
2648 |
$post_id = absint( $post_id ); |
|
2649 |
||
9 | 2650 |
if ( ! $post_id ) { |
0 | 2651 |
$post_id = get_the_ID(); |
9 | 2652 |
} |
0 | 2653 |
|
2654 |
$stickies = get_option( 'sticky_posts' ); |
|
2655 |
||
16 | 2656 |
if ( is_array( $stickies ) ) { |
2657 |
$stickies = array_map( 'intval', $stickies ); |
|
2658 |
$is_sticky = in_array( $post_id, $stickies, true ); |
|
2659 |
} else { |
|
2660 |
$is_sticky = false; |
|
2661 |
} |
|
2662 |
||
2663 |
/** |
|
2664 |
* Filters whether a post is sticky. |
|
2665 |
* |
|
2666 |
* @since 5.3.0 |
|
2667 |
* |
|
2668 |
* @param bool $is_sticky Whether a post is sticky. |
|
2669 |
* @param int $post_id Post ID. |
|
2670 |
*/ |
|
2671 |
return apply_filters( 'is_sticky', $is_sticky, $post_id ); |
|
0 | 2672 |
} |
2673 |
||
2674 |
/** |
|
18 | 2675 |
* Sanitizes every post field. |
0 | 2676 |
* |
5 | 2677 |
* If the context is 'raw', then the post object or array will get minimal |
2678 |
* sanitization of the integer fields. |
|
0 | 2679 |
* |
2680 |
* @since 2.3.0 |
|
5 | 2681 |
* |
2682 |
* @see sanitize_post_field() |
|
2683 |
* |
|
18 | 2684 |
* @param object|WP_Post|array $post The post object or array |
5 | 2685 |
* @param string $context Optional. How to sanitize post fields. |
18 | 2686 |
* Accepts 'raw', 'edit', 'db', 'display', |
2687 |
* 'attribute', or 'js'. Default 'display'. |
|
2688 |
* @return object|WP_Post|array The now sanitized post object or array (will be the |
|
2689 |
* same type as `$post`). |
|
0 | 2690 |
*/ |
5 | 2691 |
function sanitize_post( $post, $context = 'display' ) { |
9 | 2692 |
if ( is_object( $post ) ) { |
5 | 2693 |
// Check if post already filtered for this context. |
9 | 2694 |
if ( isset( $post->filter ) && $context == $post->filter ) { |
0 | 2695 |
return $post; |
9 | 2696 |
} |
2697 |
if ( ! isset( $post->ID ) ) { |
|
0 | 2698 |
$post->ID = 0; |
9 | 2699 |
} |
2700 |
foreach ( array_keys( get_object_vars( $post ) ) as $field ) { |
|
2701 |
$post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|
2702 |
} |
|
0 | 2703 |
$post->filter = $context; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2704 |
} elseif ( is_array( $post ) ) { |
5 | 2705 |
// Check if post already filtered for this context. |
9 | 2706 |
if ( isset( $post['filter'] ) && $context == $post['filter'] ) { |
0 | 2707 |
return $post; |
9 | 2708 |
} |
2709 |
if ( ! isset( $post['ID'] ) ) { |
|
0 | 2710 |
$post['ID'] = 0; |
9 | 2711 |
} |
2712 |
foreach ( array_keys( $post ) as $field ) { |
|
2713 |
$post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context ); |
|
2714 |
} |
|
0 | 2715 |
$post['filter'] = $context; |
2716 |
} |
|
2717 |
return $post; |
|
2718 |
} |
|
2719 |
||
2720 |
/** |
|
16 | 2721 |
* Sanitizes a post field based on context. |
0 | 2722 |
* |
5 | 2723 |
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and |
2724 |
* 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts |
|
2725 |
* are treated like 'display' when calling filters. |
|
0 | 2726 |
* |
2727 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2728 |
* @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. |
5 | 2729 |
* |
2730 |
* @param string $field The Post Object field name. |
|
2731 |
* @param mixed $value The Post Object value. |
|
2732 |
* @param int $post_id Post ID. |
|
16 | 2733 |
* @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2734 |
* 'db', 'display', 'attribute' and 'js'. Default 'display'. |
0 | 2735 |
* @return mixed Sanitized value. |
2736 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2737 |
function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { |
9 | 2738 |
$int_fields = array( 'ID', 'post_parent', 'menu_order' ); |
16 | 2739 |
if ( in_array( $field, $int_fields, true ) ) { |
0 | 2740 |
$value = (int) $value; |
9 | 2741 |
} |
0 | 2742 |
|
5 | 2743 |
// Fields which contain arrays of integers. |
0 | 2744 |
$array_int_fields = array( 'ancestors' ); |
16 | 2745 |
if ( in_array( $field, $array_int_fields, true ) ) { |
9 | 2746 |
$value = array_map( 'absint', $value ); |
0 | 2747 |
return $value; |
2748 |
} |
|
2749 |
||
16 | 2750 |
if ( 'raw' === $context ) { |
0 | 2751 |
return $value; |
9 | 2752 |
} |
0 | 2753 |
|
2754 |
$prefixed = false; |
|
9 | 2755 |
if ( false !== strpos( $field, 'post_' ) ) { |
2756 |
$prefixed = true; |
|
2757 |
$field_no_prefix = str_replace( 'post_', '', $field ); |
|
0 | 2758 |
} |
2759 |
||
16 | 2760 |
if ( 'edit' === $context ) { |
9 | 2761 |
$format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' ); |
0 | 2762 |
|
2763 |
if ( $prefixed ) { |
|
5 | 2764 |
|
2765 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2766 |
* Filters the value of a specific post field to edit. |
5 | 2767 |
* |
2768 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2769 |
* field name. |
|
2770 |
* |
|
2771 |
* @since 2.3.0 |
|
2772 |
* |
|
2773 |
* @param mixed $value Value of the post field. |
|
2774 |
* @param int $post_id Post ID. |
|
2775 |
*/ |
|
2776 |
$value = apply_filters( "edit_{$field}", $value, $post_id ); |
|
2777 |
||
2778 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2779 |
* Filters the value of a specific post field to edit. |
5 | 2780 |
* |
2781 |
* The dynamic portion of the hook name, `$field_no_prefix`, refers to |
|
2782 |
* the post field name. |
|
2783 |
* |
|
2784 |
* @since 2.3.0 |
|
2785 |
* |
|
2786 |
* @param mixed $value Value of the post field. |
|
2787 |
* @param int $post_id Post ID. |
|
2788 |
*/ |
|
2789 |
$value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); |
|
0 | 2790 |
} else { |
5 | 2791 |
$value = apply_filters( "edit_post_{$field}", $value, $post_id ); |
0 | 2792 |
} |
2793 |
||
16 | 2794 |
if ( in_array( $field, $format_to_edit, true ) ) { |
2795 |
if ( 'post_content' === $field ) { |
|
9 | 2796 |
$value = format_to_edit( $value, user_can_richedit() ); |
2797 |
} else { |
|
2798 |
$value = format_to_edit( $value ); |
|
2799 |
} |
|
0 | 2800 |
} else { |
9 | 2801 |
$value = esc_attr( $value ); |
0 | 2802 |
} |
16 | 2803 |
} elseif ( 'db' === $context ) { |
0 | 2804 |
if ( $prefixed ) { |
5 | 2805 |
|
2806 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2807 |
* Filters the value of a specific post field before saving. |
5 | 2808 |
* |
2809 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2810 |
* field name. |
|
2811 |
* |
|
2812 |
* @since 2.3.0 |
|
2813 |
* |
|
2814 |
* @param mixed $value Value of the post field. |
|
2815 |
*/ |
|
2816 |
$value = apply_filters( "pre_{$field}", $value ); |
|
2817 |
||
2818 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2819 |
* Filters the value of a specific field before saving. |
5 | 2820 |
* |
2821 |
* The dynamic portion of the hook name, `$field_no_prefix`, refers |
|
2822 |
* to the post field name. |
|
2823 |
* |
|
2824 |
* @since 2.3.0 |
|
2825 |
* |
|
2826 |
* @param mixed $value Value of the post field. |
|
2827 |
*/ |
|
2828 |
$value = apply_filters( "{$field_no_prefix}_save_pre", $value ); |
|
0 | 2829 |
} else { |
5 | 2830 |
$value = apply_filters( "pre_post_{$field}", $value ); |
2831 |
||
2832 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2833 |
* Filters the value of a specific post field before saving. |
5 | 2834 |
* |
2835 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2836 |
* field name. |
|
2837 |
* |
|
2838 |
* @since 2.3.0 |
|
2839 |
* |
|
2840 |
* @param mixed $value Value of the post field. |
|
2841 |
*/ |
|
2842 |
$value = apply_filters( "{$field}_pre", $value ); |
|
0 | 2843 |
} |
2844 |
} else { |
|
5 | 2845 |
|
0 | 2846 |
// Use display filters by default. |
5 | 2847 |
if ( $prefixed ) { |
2848 |
||
2849 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2850 |
* Filters the value of a specific post field for display. |
5 | 2851 |
* |
2852 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2853 |
* field name. |
|
2854 |
* |
|
2855 |
* @since 2.3.0 |
|
2856 |
* |
|
2857 |
* @param mixed $value Value of the prefixed post field. |
|
2858 |
* @param int $post_id Post ID. |
|
18 | 2859 |
* @param string $context Context for how to sanitize the field. |
2860 |
* Accepts 'raw', 'edit', 'db', 'display', |
|
2861 |
* 'attribute', or 'js'. Default 'display'. |
|
5 | 2862 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2863 |
$value = apply_filters( "{$field}", $value, $post_id, $context ); |
5 | 2864 |
} else { |
2865 |
$value = apply_filters( "post_{$field}", $value, $post_id, $context ); |
|
2866 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2867 |
|
16 | 2868 |
if ( 'attribute' === $context ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2869 |
$value = esc_attr( $value ); |
16 | 2870 |
} elseif ( 'js' === $context ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2871 |
$value = esc_js( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2872 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2873 |
} |
0 | 2874 |
|
18 | 2875 |
// Restore the type for integer fields after esc_attr(). |
2876 |
if ( in_array( $field, $int_fields, true ) ) { |
|
2877 |
$value = (int) $value; |
|
2878 |
} |
|
2879 |
||
0 | 2880 |
return $value; |
2881 |
} |
|
2882 |
||
2883 |
/** |
|
2884 |
* Make a post sticky. |
|
2885 |
* |
|
2886 |
* Sticky posts should be displayed at the top of the front page. |
|
2887 |
* |
|
2888 |
* @since 2.7.0 |
|
2889 |
* |
|
2890 |
* @param int $post_id Post ID. |
|
2891 |
*/ |
|
5 | 2892 |
function stick_post( $post_id ) { |
16 | 2893 |
$post_id = (int) $post_id; |
9 | 2894 |
$stickies = get_option( 'sticky_posts' ); |
18 | 2895 |
$updated = false; |
9 | 2896 |
|
2897 |
if ( ! is_array( $stickies ) ) { |
|
19 | 2898 |
$stickies = array(); |
18 | 2899 |
} else { |
2900 |
$stickies = array_unique( array_map( 'intval', $stickies ) ); |
|
2901 |
} |
|
16 | 2902 |
|
2903 |
if ( ! in_array( $post_id, $stickies, true ) ) { |
|
0 | 2904 |
$stickies[] = $post_id; |
18 | 2905 |
$updated = update_option( 'sticky_posts', array_values( $stickies ) ); |
2906 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2907 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2908 |
if ( $updated ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2909 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2910 |
* Fires once a post has been added to the sticky list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2911 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2912 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2913 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2914 |
* @param int $post_id ID of the post that was stuck. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2915 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2916 |
do_action( 'post_stuck', $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2917 |
} |
0 | 2918 |
} |
2919 |
||
2920 |
/** |
|
5 | 2921 |
* Un-stick a post. |
0 | 2922 |
* |
2923 |
* Sticky posts should be displayed at the top of the front page. |
|
2924 |
* |
|
2925 |
* @since 2.7.0 |
|
2926 |
* |
|
2927 |
* @param int $post_id Post ID. |
|
2928 |
*/ |
|
5 | 2929 |
function unstick_post( $post_id ) { |
16 | 2930 |
$post_id = (int) $post_id; |
9 | 2931 |
$stickies = get_option( 'sticky_posts' ); |
2932 |
||
2933 |
if ( ! is_array( $stickies ) ) { |
|
0 | 2934 |
return; |
9 | 2935 |
} |
2936 |
||
18 | 2937 |
$stickies = array_values( array_unique( array_map( 'intval', $stickies ) ) ); |
16 | 2938 |
|
2939 |
if ( ! in_array( $post_id, $stickies, true ) ) { |
|
0 | 2940 |
return; |
9 | 2941 |
} |
2942 |
||
16 | 2943 |
$offset = array_search( $post_id, $stickies, true ); |
9 | 2944 |
if ( false === $offset ) { |
0 | 2945 |
return; |
9 | 2946 |
} |
2947 |
||
2948 |
array_splice( $stickies, $offset, 1 ); |
|
0 | 2949 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2950 |
$updated = update_option( 'sticky_posts', $stickies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2951 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2952 |
if ( $updated ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2953 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2954 |
* Fires once a post has been removed from the sticky list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2955 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2956 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2957 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2958 |
* @param int $post_id ID of the post that was unstuck. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2959 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2960 |
do_action( 'post_unstuck', $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2961 |
} |
0 | 2962 |
} |
2963 |
||
2964 |
/** |
|
5 | 2965 |
* Return the cache key for wp_count_posts() based on the passed arguments. |
2966 |
* |
|
2967 |
* @since 3.9.0 |
|
9 | 2968 |
* @access private |
5 | 2969 |
* |
2970 |
* @param string $type Optional. Post type to retrieve count Default 'post'. |
|
2971 |
* @param string $perm Optional. 'readable' or empty. Default empty. |
|
2972 |
* @return string The cache key. |
|
2973 |
*/ |
|
2974 |
function _count_posts_cache_key( $type = 'post', $perm = '' ) { |
|
2975 |
$cache_key = 'posts-' . $type; |
|
16 | 2976 |
|
2977 |
if ( 'readable' === $perm && is_user_logged_in() ) { |
|
5 | 2978 |
$post_type_object = get_post_type_object( $type ); |
16 | 2979 |
|
5 | 2980 |
if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
2981 |
$cache_key .= '_' . $perm . '_' . get_current_user_id(); |
|
2982 |
} |
|
2983 |
} |
|
16 | 2984 |
|
5 | 2985 |
return $cache_key; |
2986 |
} |
|
2987 |
||
2988 |
/** |
|
0 | 2989 |
* Count number of posts of a post type and if user has permissions to view. |
2990 |
* |
|
2991 |
* This function provides an efficient method of finding the amount of post's |
|
2992 |
* type a blog has. Another method is to count the amount of items in |
|
2993 |
* get_posts(), but that method has a lot of overhead with doing so. Therefore, |
|
2994 |
* when developing for 2.5+, use this function instead. |
|
2995 |
* |
|
2996 |
* The $perm parameter checks for 'readable' value and if the user can read |
|
2997 |
* private posts, it will display that for the user that is signed in. |
|
2998 |
* |
|
2999 |
* @since 2.5.0 |
|
3000 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3001 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3002 |
* |
5 | 3003 |
* @param string $type Optional. Post type to retrieve count. Default 'post'. |
3004 |
* @param string $perm Optional. 'readable' or empty. Default empty. |
|
19 | 3005 |
* @return stdClass Number of posts for each status. |
0 | 3006 |
*/ |
3007 |
function wp_count_posts( $type = 'post', $perm = '' ) { |
|
3008 |
global $wpdb; |
|
3009 |
||
9 | 3010 |
if ( ! post_type_exists( $type ) ) { |
0 | 3011 |
return new stdClass; |
9 | 3012 |
} |
0 | 3013 |
|
5 | 3014 |
$cache_key = _count_posts_cache_key( $type, $perm ); |
3015 |
||
3016 |
$counts = wp_cache_get( $cache_key, 'counts' ); |
|
3017 |
if ( false !== $counts ) { |
|
16 | 3018 |
// We may have cached this before every status was registered. |
3019 |
foreach ( get_post_stati() as $status ) { |
|
3020 |
if ( ! isset( $counts->{$status} ) ) { |
|
3021 |
$counts->{$status} = 0; |
|
3022 |
} |
|
3023 |
} |
|
3024 |
||
5 | 3025 |
/** This filter is documented in wp-includes/post.php */ |
3026 |
return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|
3027 |
} |
|
0 | 3028 |
|
3029 |
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; |
|
16 | 3030 |
|
3031 |
if ( 'readable' === $perm && is_user_logged_in() ) { |
|
9 | 3032 |
$post_type_object = get_post_type_object( $type ); |
5 | 3033 |
if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
9 | 3034 |
$query .= $wpdb->prepare( |
3035 |
" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", |
|
5 | 3036 |
get_current_user_id() |
3037 |
); |
|
0 | 3038 |
} |
3039 |
} |
|
16 | 3040 |
|
0 | 3041 |
$query .= ' GROUP BY post_status'; |
3042 |
||
5 | 3043 |
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); |
9 | 3044 |
$counts = array_fill_keys( get_post_stati(), 0 ); |
5 | 3045 |
|
3046 |
foreach ( $results as $row ) { |
|
3047 |
$counts[ $row['post_status'] ] = $row['num_posts']; |
|
0 | 3048 |
} |
3049 |
||
5 | 3050 |
$counts = (object) $counts; |
3051 |
wp_cache_set( $cache_key, $counts, 'counts' ); |
|
3052 |
||
0 | 3053 |
/** |
3054 |
* Modify returned post counts by status for the current post type. |
|
3055 |
* |
|
3056 |
* @since 3.7.0 |
|
3057 |
* |
|
19 | 3058 |
* @param stdClass $counts An object containing the current post_type's post |
3059 |
* counts by status. |
|
3060 |
* @param string $type Post type. |
|
3061 |
* @param string $perm The permission to determine if the posts are 'readable' |
|
3062 |
* by the current user. |
|
0 | 3063 |
*/ |
3064 |
return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|
3065 |
} |
|
3066 |
||
3067 |
/** |
|
3068 |
* Count number of attachments for the mime type(s). |
|
3069 |
* |
|
3070 |
* If you set the optional mime_type parameter, then an array will still be |
|
3071 |
* returned, but will only have the item you are looking for. It does not give |
|
3072 |
* you the number of attachments that are children of a post. You can get that |
|
3073 |
* by counting the number of children that post has. |
|
3074 |
* |
|
3075 |
* @since 2.5.0 |
|
3076 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3077 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3078 |
* |
18 | 3079 |
* @param string|string[] $mime_type Optional. Array or comma-separated list of |
3080 |
* MIME patterns. Default empty. |
|
19 | 3081 |
* @return stdClass An object containing the attachment counts by mime type. |
0 | 3082 |
*/ |
3083 |
function wp_count_attachments( $mime_type = '' ) { |
|
3084 |
global $wpdb; |
|
3085 |
||
9 | 3086 |
$and = wp_post_mime_type_where( $mime_type ); |
0 | 3087 |
$count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); |
3088 |
||
3089 |
$counts = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3090 |
foreach ( (array) $count as $row ) { |
0 | 3091 |
$counts[ $row['post_mime_type'] ] = $row['num_posts']; |
3092 |
} |
|
9 | 3093 |
$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); |
0 | 3094 |
|
3095 |
/** |
|
3096 |
* Modify returned attachment counts by mime type. |
|
3097 |
* |
|
3098 |
* @since 3.7.0 |
|
3099 |
* |
|
19 | 3100 |
* @param stdClass $counts An object containing the attachment counts by |
18 | 3101 |
* mime type. |
3102 |
* @param string|string[] $mime_type Array or comma-separated list of MIME patterns. |
|
0 | 3103 |
*/ |
3104 |
return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); |
|
3105 |
} |
|
3106 |
||
3107 |
/** |
|
5 | 3108 |
* Get default post mime types. |
0 | 3109 |
* |
3110 |
* @since 2.9.0 |
|
16 | 3111 |
* @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups. |
0 | 3112 |
* |
5 | 3113 |
* @return array List of post mime types. |
0 | 3114 |
*/ |
3115 |
function get_post_mime_types() { |
|
16 | 3116 |
$post_mime_types = array( // array( adj, noun ) |
3117 |
'image' => array( |
|
3118 |
__( 'Images' ), |
|
3119 |
__( 'Manage Images' ), |
|
3120 |
/* translators: %s: Number of images. */ |
|
3121 |
_n_noop( |
|
3122 |
'Image <span class="count">(%s)</span>', |
|
3123 |
'Images <span class="count">(%s)</span>' |
|
3124 |
), |
|
3125 |
), |
|
3126 |
'audio' => array( |
|
19 | 3127 |
_x( 'Audio', 'file type group' ), |
16 | 3128 |
__( 'Manage Audio' ), |
3129 |
/* translators: %s: Number of audio files. */ |
|
3130 |
_n_noop( |
|
3131 |
'Audio <span class="count">(%s)</span>', |
|
3132 |
'Audio <span class="count">(%s)</span>' |
|
3133 |
), |
|
3134 |
), |
|
3135 |
'video' => array( |
|
19 | 3136 |
_x( 'Video', 'file type group' ), |
16 | 3137 |
__( 'Manage Video' ), |
3138 |
/* translators: %s: Number of video files. */ |
|
3139 |
_n_noop( |
|
3140 |
'Video <span class="count">(%s)</span>', |
|
3141 |
'Video <span class="count">(%s)</span>' |
|
3142 |
), |
|
3143 |
), |
|
3144 |
'document' => array( |
|
3145 |
__( 'Documents' ), |
|
3146 |
__( 'Manage Documents' ), |
|
3147 |
/* translators: %s: Number of documents. */ |
|
3148 |
_n_noop( |
|
3149 |
'Document <span class="count">(%s)</span>', |
|
3150 |
'Documents <span class="count">(%s)</span>' |
|
3151 |
), |
|
3152 |
), |
|
3153 |
'spreadsheet' => array( |
|
3154 |
__( 'Spreadsheets' ), |
|
3155 |
__( 'Manage Spreadsheets' ), |
|
3156 |
/* translators: %s: Number of spreadsheets. */ |
|
3157 |
_n_noop( |
|
3158 |
'Spreadsheet <span class="count">(%s)</span>', |
|
3159 |
'Spreadsheets <span class="count">(%s)</span>' |
|
3160 |
), |
|
3161 |
), |
|
3162 |
'archive' => array( |
|
3163 |
_x( 'Archives', 'file type group' ), |
|
3164 |
__( 'Manage Archives' ), |
|
3165 |
/* translators: %s: Number of archives. */ |
|
3166 |
_n_noop( |
|
3167 |
'Archive <span class="count">(%s)</span>', |
|
3168 |
'Archives <span class="count">(%s)</span>' |
|
3169 |
), |
|
3170 |
), |
|
0 | 3171 |
); |
3172 |
||
16 | 3173 |
$ext_types = wp_get_ext_types(); |
3174 |
$mime_types = wp_get_mime_types(); |
|
3175 |
||
3176 |
foreach ( $post_mime_types as $group => $labels ) { |
|
3177 |
if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) { |
|
3178 |
continue; |
|
3179 |
} |
|
3180 |
||
3181 |
if ( ! isset( $ext_types[ $group ] ) ) { |
|
3182 |
unset( $post_mime_types[ $group ] ); |
|
3183 |
continue; |
|
3184 |
} |
|
3185 |
||
3186 |
$group_mime_types = array(); |
|
3187 |
foreach ( $ext_types[ $group ] as $extension ) { |
|
3188 |
foreach ( $mime_types as $exts => $mime ) { |
|
3189 |
if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { |
|
3190 |
$group_mime_types[] = $mime; |
|
3191 |
break; |
|
3192 |
} |
|
3193 |
} |
|
3194 |
} |
|
3195 |
$group_mime_types = implode( ',', array_unique( $group_mime_types ) ); |
|
3196 |
||
3197 |
$post_mime_types[ $group_mime_types ] = $labels; |
|
3198 |
unset( $post_mime_types[ $group ] ); |
|
3199 |
} |
|
3200 |
||
5 | 3201 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3202 |
* Filters the default list of post mime types. |
5 | 3203 |
* |
3204 |
* @since 2.5.0 |
|
3205 |
* |
|
3206 |
* @param array $post_mime_types Default list of post mime types. |
|
3207 |
*/ |
|
3208 |
return apply_filters( 'post_mime_types', $post_mime_types ); |
|
0 | 3209 |
} |
3210 |
||
3211 |
/** |
|
3212 |
* Check a MIME-Type against a list. |
|
3213 |
* |
|
3214 |
* If the wildcard_mime_types parameter is a string, it must be comma separated |
|
3215 |
* list. If the real_mime_types is a string, it is also comma separated to |
|
3216 |
* create the list. |
|
3217 |
* |
|
3218 |
* @since 2.5.0 |
|
3219 |
* |
|
18 | 3220 |
* @param string|string[] $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*) |
3221 |
* or flash (same as *flash*). |
|
3222 |
* @param string|string[] $real_mime_types Real post mime type values. |
|
5 | 3223 |
* @return array array(wildcard=>array(real types)). |
0 | 3224 |
*/ |
5 | 3225 |
function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { |
0 | 3226 |
$matches = array(); |
5 | 3227 |
if ( is_string( $wildcard_mime_types ) ) { |
3228 |
$wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) ); |
|
3229 |
} |
|
3230 |
if ( is_string( $real_mime_types ) ) { |
|
3231 |
$real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) ); |
|
3232 |
} |
|
3233 |
||
3234 |
$patternses = array(); |
|
9 | 3235 |
$wild = '[-._a-z0-9]*'; |
5 | 3236 |
|
0 | 3237 |
foreach ( (array) $wildcard_mime_types as $type ) { |
5 | 3238 |
$mimes = array_map( 'trim', explode( ',', $type ) ); |
3239 |
foreach ( $mimes as $mime ) { |
|
16 | 3240 |
$regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); |
3241 |
||
9 | 3242 |
$patternses[][ $type ] = "^$regex$"; |
16 | 3243 |
|
5 | 3244 |
if ( false === strpos( $mime, '/' ) ) { |
9 | 3245 |
$patternses[][ $type ] = "^$regex/"; |
3246 |
$patternses[][ $type ] = $regex; |
|
5 | 3247 |
} |
0 | 3248 |
} |
3249 |
} |
|
5 | 3250 |
asort( $patternses ); |
3251 |
||
3252 |
foreach ( $patternses as $patterns ) { |
|
3253 |
foreach ( $patterns as $type => $pattern ) { |
|
3254 |
foreach ( (array) $real_mime_types as $real ) { |
|
16 | 3255 |
if ( preg_match( "#$pattern#", $real ) |
3256 |
&& ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) ) |
|
3257 |
) { |
|
9 | 3258 |
$matches[ $type ][] = $real; |
5 | 3259 |
} |
3260 |
} |
|
3261 |
} |
|
3262 |
} |
|
16 | 3263 |
|
0 | 3264 |
return $matches; |
3265 |
} |
|
3266 |
||
3267 |
/** |
|
3268 |
* Convert MIME types into SQL. |
|
3269 |
* |
|
3270 |
* @since 2.5.0 |
|
3271 |
* |
|
18 | 3272 |
* @param string|string[] $post_mime_types List of mime types or comma separated string |
3273 |
* of mime types. |
|
3274 |
* @param string $table_alias Optional. Specify a table alias, if needed. |
|
3275 |
* Default empty. |
|
0 | 3276 |
* @return string The SQL AND clause for mime searching. |
3277 |
*/ |
|
5 | 3278 |
function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { |
9 | 3279 |
$where = ''; |
3280 |
$wildcards = array( '', '%', '%/%' ); |
|
3281 |
if ( is_string( $post_mime_types ) ) { |
|
3282 |
$post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); |
|
3283 |
} |
|
5 | 3284 |
|
3285 |
$wheres = array(); |
|
3286 |
||
0 | 3287 |
foreach ( (array) $post_mime_types as $mime_type ) { |
9 | 3288 |
$mime_type = preg_replace( '/\s/', '', $mime_type ); |
3289 |
$slashpos = strpos( $mime_type, '/' ); |
|
0 | 3290 |
if ( false !== $slashpos ) { |
9 | 3291 |
$mime_group = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) ); |
3292 |
$mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) ); |
|
3293 |
if ( empty( $mime_subgroup ) ) { |
|
0 | 3294 |
$mime_subgroup = '*'; |
9 | 3295 |
} else { |
3296 |
$mime_subgroup = str_replace( '/', '', $mime_subgroup ); |
|
3297 |
} |
|
0 | 3298 |
$mime_pattern = "$mime_group/$mime_subgroup"; |
3299 |
} else { |
|
9 | 3300 |
$mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); |
3301 |
if ( false === strpos( $mime_pattern, '*' ) ) { |
|
0 | 3302 |
$mime_pattern .= '/*'; |
9 | 3303 |
} |
0 | 3304 |
} |
3305 |
||
9 | 3306 |
$mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); |
3307 |
||
16 | 3308 |
if ( in_array( $mime_type, $wildcards, true ) ) { |
0 | 3309 |
return ''; |
9 | 3310 |
} |
3311 |
||
3312 |
if ( false !== strpos( $mime_pattern, '%' ) ) { |
|
3313 |
$wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
|
3314 |
} else { |
|
3315 |
$wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
|
3316 |
} |
|
3317 |
} |
|
16 | 3318 |
|
9 | 3319 |
if ( ! empty( $wheres ) ) { |
18 | 3320 |
$where = ' AND (' . implode( ' OR ', $wheres ) . ') '; |
9 | 3321 |
} |
16 | 3322 |
|
0 | 3323 |
return $where; |
3324 |
} |
|
3325 |
||
3326 |
/** |
|
5 | 3327 |
* Trash or delete a post or page. |
3328 |
* |
|
3329 |
* When the post and page is permanently deleted, everything that is tied to |
|
3330 |
* it is deleted also. This includes comments, post meta fields, and terms |
|
3331 |
* associated with the post. |
|
3332 |
* |
|
16 | 3333 |
* The post or page is moved to Trash instead of permanently deleted unless |
3334 |
* Trash is disabled, item is already in the Trash, or $force_delete is true. |
|
0 | 3335 |
* |
3336 |
* @since 1.0.0 |
|
5 | 3337 |
* |
3338 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
3339 |
* @see wp_delete_attachment() |
|
3340 |
* @see wp_trash_post() |
|
3341 |
* |
|
3342 |
* @param int $postid Optional. Post ID. Default 0. |
|
16 | 3343 |
* @param bool $force_delete Optional. Whether to bypass Trash and force deletion. |
5 | 3344 |
* Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3345 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 3346 |
*/ |
3347 |
function wp_delete_post( $postid = 0, $force_delete = false ) { |
|
3348 |
global $wpdb; |
|
3349 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3350 |
$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3351 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3352 |
if ( ! $post ) { |
0 | 3353 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3354 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3355 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3356 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3357 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3358 |
if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3359 |
return wp_trash_post( $postid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3360 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3361 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3362 |
if ( 'attachment' === $post->post_type ) { |
0 | 3363 |
return wp_delete_attachment( $postid, $force_delete ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3364 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3365 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3366 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3367 |
* Filters whether a post deletion should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3368 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3369 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3370 |
* |
19 | 3371 |
* @param WP_Post|false|null $delete Whether to go forward with deletion. @TODO description |
3372 |
* @param WP_Post $post Post object. |
|
3373 |
* @param bool $force_delete Whether to bypass the Trash. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3374 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3375 |
$check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3376 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3377 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3378 |
} |
0 | 3379 |
|
5 | 3380 |
/** |
3381 |
* Fires before a post is deleted, at the start of wp_delete_post(). |
|
3382 |
* |
|
3383 |
* @since 3.2.0 |
|
16 | 3384 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3385 |
* |
3386 |
* @see wp_delete_post() |
|
3387 |
* |
|
16 | 3388 |
* @param int $postid Post ID. |
3389 |
* @param WP_Post $post Post object. |
|
5 | 3390 |
*/ |
16 | 3391 |
do_action( 'before_delete_post', $postid, $post ); |
0 | 3392 |
|
9 | 3393 |
delete_post_meta( $postid, '_wp_trash_meta_status' ); |
3394 |
delete_post_meta( $postid, '_wp_trash_meta_time' ); |
|
3395 |
||
3396 |
wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) ); |
|
3397 |
||
3398 |
$parent_data = array( 'post_parent' => $post->post_parent ); |
|
0 | 3399 |
$parent_where = array( 'post_parent' => $postid ); |
3400 |
||
3401 |
if ( is_post_type_hierarchical( $post->post_type ) ) { |
|
5 | 3402 |
// Point children of this page to its parent, also clean the cache of affected children. |
0 | 3403 |
$children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); |
9 | 3404 |
$children = $wpdb->get_results( $children_query ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3405 |
if ( $children ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3406 |
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3407 |
} |
0 | 3408 |
} |
3409 |
||
5 | 3410 |
// Do raw query. wp_get_post_revisions() is filtered. |
0 | 3411 |
$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
3412 |
// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
|
9 | 3413 |
foreach ( $revision_ids as $revision_id ) { |
0 | 3414 |
wp_delete_post_revision( $revision_id ); |
9 | 3415 |
} |
0 | 3416 |
|
5 | 3417 |
// Point all attachments to this post up one level. |
0 | 3418 |
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
3419 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3420 |
wp_defer_comment_counting( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3421 |
|
19 | 3422 |
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $postid ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3423 |
foreach ( $comment_ids as $comment_id ) { |
0 | 3424 |
wp_delete_comment( $comment_id, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3425 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3426 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3427 |
wp_defer_comment_counting( false ); |
0 | 3428 |
|
9 | 3429 |
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) ); |
3430 |
foreach ( $post_meta_ids as $mid ) { |
|
0 | 3431 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 3432 |
} |
0 | 3433 |
|
5 | 3434 |
/** |
3435 |
* Fires immediately before a post is deleted from the database. |
|
3436 |
* |
|
3437 |
* @since 1.2.0 |
|
16 | 3438 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3439 |
* |
16 | 3440 |
* @param int $postid Post ID. |
3441 |
* @param WP_Post $post Post object. |
|
5 | 3442 |
*/ |
16 | 3443 |
do_action( 'delete_post', $postid, $post ); |
3444 |
||
5 | 3445 |
$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); |
3446 |
if ( ! $result ) { |
|
3447 |
return false; |
|
3448 |
} |
|
3449 |
||
3450 |
/** |
|
3451 |
* Fires immediately after a post is deleted from the database. |
|
3452 |
* |
|
3453 |
* @since 2.2.0 |
|
16 | 3454 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3455 |
* |
16 | 3456 |
* @param int $postid Post ID. |
3457 |
* @param WP_Post $post Post object. |
|
5 | 3458 |
*/ |
16 | 3459 |
do_action( 'deleted_post', $postid, $post ); |
0 | 3460 |
|
3461 |
clean_post_cache( $post ); |
|
3462 |
||
3463 |
if ( is_post_type_hierarchical( $post->post_type ) && $children ) { |
|
9 | 3464 |
foreach ( $children as $child ) { |
0 | 3465 |
clean_post_cache( $child ); |
9 | 3466 |
} |
3467 |
} |
|
3468 |
||
3469 |
wp_clear_scheduled_hook( 'publish_future_post', array( $postid ) ); |
|
0 | 3470 |
|
5 | 3471 |
/** |
3472 |
* Fires after a post is deleted, at the conclusion of wp_delete_post(). |
|
3473 |
* |
|
3474 |
* @since 3.2.0 |
|
16 | 3475 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3476 |
* |
3477 |
* @see wp_delete_post() |
|
3478 |
* |
|
16 | 3479 |
* @param int $postid Post ID. |
3480 |
* @param WP_Post $post Post object. |
|
5 | 3481 |
*/ |
16 | 3482 |
do_action( 'after_delete_post', $postid, $post ); |
0 | 3483 |
|
3484 |
return $post; |
|
3485 |
} |
|
3486 |
||
3487 |
/** |
|
5 | 3488 |
* Reset the page_on_front, show_on_front, and page_for_post settings when |
3489 |
* a linked page is deleted or trashed. |
|
0 | 3490 |
* |
3491 |
* Also ensures the post is no longer sticky. |
|
3492 |
* |
|
5 | 3493 |
* @since 3.7.0 |
0 | 3494 |
* @access private |
5 | 3495 |
* |
3496 |
* @param int $post_id Post ID. |
|
0 | 3497 |
*/ |
3498 |
function _reset_front_page_settings_for_post( $post_id ) { |
|
3499 |
$post = get_post( $post_id ); |
|
16 | 3500 |
|
3501 |
if ( 'page' === $post->post_type ) { |
|
9 | 3502 |
/* |
16 | 3503 |
* If the page is defined in option page_on_front or post_for_posts, |
3504 |
* adjust the corresponding options. |
|
3505 |
*/ |
|
0 | 3506 |
if ( get_option( 'page_on_front' ) == $post->ID ) { |
3507 |
update_option( 'show_on_front', 'posts' ); |
|
3508 |
update_option( 'page_on_front', 0 ); |
|
3509 |
} |
|
3510 |
if ( get_option( 'page_for_posts' ) == $post->ID ) { |
|
16 | 3511 |
update_option( 'page_for_posts', 0 ); |
0 | 3512 |
} |
3513 |
} |
|
16 | 3514 |
|
0 | 3515 |
unstick_post( $post->ID ); |
3516 |
} |
|
3517 |
||
3518 |
/** |
|
5 | 3519 |
* Move a post or page to the Trash |
0 | 3520 |
* |
16 | 3521 |
* If Trash is disabled, the post or page is permanently deleted. |
0 | 3522 |
* |
3523 |
* @since 2.9.0 |
|
5 | 3524 |
* |
3525 |
* @see wp_delete_post() |
|
3526 |
* |
|
19 | 3527 |
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post` |
3528 |
* if `EMPTY_TRASH_DAYS` equals true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3529 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 3530 |
*/ |
5 | 3531 |
function wp_trash_post( $post_id = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3532 |
if ( ! EMPTY_TRASH_DAYS ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3533 |
return wp_delete_post( $post_id, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3534 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3535 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3536 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3537 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3538 |
if ( ! $post ) { |
0 | 3539 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3540 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3541 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3542 |
if ( 'trash' === $post->post_status ) { |
0 | 3543 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3544 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3545 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3546 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3547 |
* Filters whether a post trashing should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3548 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3549 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3550 |
* |
16 | 3551 |
* @param bool|null $trash Whether to go forward with trashing. |
3552 |
* @param WP_Post $post Post object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3553 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3554 |
$check = apply_filters( 'pre_trash_post', null, $post ); |
18 | 3555 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3556 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3557 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3558 |
} |
0 | 3559 |
|
5 | 3560 |
/** |
16 | 3561 |
* Fires before a post is sent to the Trash. |
5 | 3562 |
* |
3563 |
* @since 3.3.0 |
|
3564 |
* |
|
3565 |
* @param int $post_id Post ID. |
|
3566 |
*/ |
|
3567 |
do_action( 'wp_trash_post', $post_id ); |
|
0 | 3568 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3569 |
add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3570 |
add_post_meta( $post_id, '_wp_trash_meta_time', time() ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3571 |
|
16 | 3572 |
$post_updated = wp_update_post( |
9 | 3573 |
array( |
3574 |
'ID' => $post_id, |
|
3575 |
'post_status' => 'trash', |
|
3576 |
) |
|
3577 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3578 |
|
16 | 3579 |
if ( ! $post_updated ) { |
3580 |
return false; |
|
3581 |
} |
|
3582 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3583 |
wp_trash_post_comments( $post_id ); |
0 | 3584 |
|
5 | 3585 |
/** |
16 | 3586 |
* Fires after a post is sent to the Trash. |
5 | 3587 |
* |
3588 |
* @since 2.9.0 |
|
3589 |
* |
|
3590 |
* @param int $post_id Post ID. |
|
3591 |
*/ |
|
3592 |
do_action( 'trashed_post', $post_id ); |
|
0 | 3593 |
|
3594 |
return $post; |
|
3595 |
} |
|
3596 |
||
3597 |
/** |
|
18 | 3598 |
* Restores a post from the Trash. |
0 | 3599 |
* |
3600 |
* @since 2.9.0 |
|
18 | 3601 |
* @since 5.6.0 An untrashed post is now returned to 'draft' status by default, except for |
3602 |
* attachments which are returned to their original 'inherit' status. |
|
3603 |
* |
|
19 | 3604 |
* @param int $post_id Optional. Post ID. Default is the ID of the global `$post`. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3605 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 3606 |
*/ |
5 | 3607 |
function wp_untrash_post( $post_id = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3608 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3609 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3610 |
if ( ! $post ) { |
0 | 3611 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3612 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3613 |
|
18 | 3614 |
$post_id = $post->ID; |
3615 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3616 |
if ( 'trash' !== $post->post_status ) { |
0 | 3617 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3618 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3619 |
|
18 | 3620 |
$previous_status = get_post_meta( $post_id, '_wp_trash_meta_status', true ); |
3621 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3622 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3623 |
* Filters whether a post untrashing should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3624 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3625 |
* @since 4.9.0 |
18 | 3626 |
* @since 5.6.0 The `$previous_status` parameter was added. |
3627 |
* |
|
3628 |
* @param bool|null $untrash Whether to go forward with untrashing. |
|
3629 |
* @param WP_Post $post Post object. |
|
3630 |
* @param string $previous_status The status of the post at the point where it was trashed. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3631 |
*/ |
18 | 3632 |
$check = apply_filters( 'pre_untrash_post', null, $post, $previous_status ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3633 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3634 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3635 |
} |
0 | 3636 |
|
5 | 3637 |
/** |
16 | 3638 |
* Fires before a post is restored from the Trash. |
5 | 3639 |
* |
3640 |
* @since 2.9.0 |
|
18 | 3641 |
* @since 5.6.0 The `$previous_status` parameter was added. |
3642 |
* |
|
3643 |
* @param int $post_id Post ID. |
|
3644 |
* @param string $previous_status The status of the post at the point where it was trashed. |
|
5 | 3645 |
*/ |
18 | 3646 |
do_action( 'untrash_post', $post_id, $previous_status ); |
3647 |
||
3648 |
$new_status = ( 'attachment' === $post->post_type ) ? 'inherit' : 'draft'; |
|
3649 |
||
3650 |
/** |
|
3651 |
* Filters the status that a post gets assigned when it is restored from the trash (untrashed). |
|
3652 |
* |
|
3653 |
* By default posts that are restored will be assigned a status of 'draft'. Return the value of `$previous_status` |
|
3654 |
* in order to assign the status that the post had before it was trashed. The `wp_untrash_post_set_previous_status()` |
|
3655 |
* function is available for this. |
|
3656 |
* |
|
3657 |
* Prior to WordPress 5.6.0, restored posts were always assigned their original status. |
|
3658 |
* |
|
3659 |
* @since 5.6.0 |
|
3660 |
* |
|
3661 |
* @param string $new_status The new status of the post being restored. |
|
3662 |
* @param int $post_id The ID of the post being restored. |
|
3663 |
* @param string $previous_status The status of the post at the point where it was trashed. |
|
3664 |
*/ |
|
3665 |
$post_status = apply_filters( 'wp_untrash_post_status', $new_status, $post_id, $previous_status ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3666 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3667 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3668 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3669 |
|
16 | 3670 |
$post_updated = wp_update_post( |
9 | 3671 |
array( |
3672 |
'ID' => $post_id, |
|
3673 |
'post_status' => $post_status, |
|
3674 |
) |
|
3675 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3676 |
|
16 | 3677 |
if ( ! $post_updated ) { |
3678 |
return false; |
|
3679 |
} |
|
3680 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3681 |
wp_untrash_post_comments( $post_id ); |
0 | 3682 |
|
5 | 3683 |
/** |
16 | 3684 |
* Fires after a post is restored from the Trash. |
5 | 3685 |
* |
3686 |
* @since 2.9.0 |
|
18 | 3687 |
* @since 5.6.0 The `$previous_status` parameter was added. |
3688 |
* |
|
3689 |
* @param int $post_id Post ID. |
|
3690 |
* @param string $previous_status The status of the post at the point where it was trashed. |
|
5 | 3691 |
*/ |
18 | 3692 |
do_action( 'untrashed_post', $post_id, $previous_status ); |
0 | 3693 |
|
3694 |
return $post; |
|
3695 |
} |
|
3696 |
||
3697 |
/** |
|
16 | 3698 |
* Moves comments for a post to the Trash. |
0 | 3699 |
* |
3700 |
* @since 2.9.0 |
|
5 | 3701 |
* |
3702 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
3703 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3704 |
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3705 |
* @return mixed|void False on failure. |
0 | 3706 |
*/ |
5 | 3707 |
function wp_trash_post_comments( $post = null ) { |
0 | 3708 |
global $wpdb; |
3709 |
||
9 | 3710 |
$post = get_post( $post ); |
18 | 3711 |
|
3712 |
if ( ! $post ) { |
|
0 | 3713 |
return; |
9 | 3714 |
} |
0 | 3715 |
|
3716 |
$post_id = $post->ID; |
|
3717 |
||
5 | 3718 |
/** |
16 | 3719 |
* Fires before comments are sent to the Trash. |
5 | 3720 |
* |
3721 |
* @since 2.9.0 |
|
3722 |
* |
|
3723 |
* @param int $post_id Post ID. |
|
3724 |
*/ |
|
3725 |
do_action( 'trash_post_comments', $post_id ); |
|
0 | 3726 |
|
9 | 3727 |
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); |
18 | 3728 |
|
3729 |
if ( ! $comments ) { |
|
0 | 3730 |
return; |
9 | 3731 |
} |
0 | 3732 |
|
5 | 3733 |
// Cache current status for each comment. |
0 | 3734 |
$statuses = array(); |
9 | 3735 |
foreach ( $comments as $comment ) { |
3736 |
$statuses[ $comment->comment_ID ] = $comment->comment_approved; |
|
3737 |
} |
|
3738 |
add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses ); |
|
0 | 3739 |
|
5 | 3740 |
// Set status for all comments to post-trashed. |
9 | 3741 |
$result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) ); |
3742 |
||
3743 |
clean_comment_cache( array_keys( $statuses ) ); |
|
0 | 3744 |
|
5 | 3745 |
/** |
16 | 3746 |
* Fires after comments are sent to the Trash. |
5 | 3747 |
* |
3748 |
* @since 2.9.0 |
|
3749 |
* |
|
3750 |
* @param int $post_id Post ID. |
|
3751 |
* @param array $statuses Array of comment statuses. |
|
3752 |
*/ |
|
3753 |
do_action( 'trashed_post_comments', $post_id, $statuses ); |
|
0 | 3754 |
|
3755 |
return $result; |
|
3756 |
} |
|
3757 |
||
3758 |
/** |
|
16 | 3759 |
* Restore comments for a post from the Trash. |
0 | 3760 |
* |
3761 |
* @since 2.9.0 |
|
5 | 3762 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3763 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3764 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3765 |
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3766 |
* @return true|void |
0 | 3767 |
*/ |
5 | 3768 |
function wp_untrash_post_comments( $post = null ) { |
0 | 3769 |
global $wpdb; |
3770 |
||
9 | 3771 |
$post = get_post( $post ); |
18 | 3772 |
|
3773 |
if ( ! $post ) { |
|
0 | 3774 |
return; |
9 | 3775 |
} |
0 | 3776 |
|
3777 |
$post_id = $post->ID; |
|
3778 |
||
9 | 3779 |
$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true ); |
3780 |
||
18 | 3781 |
if ( ! $statuses ) { |
0 | 3782 |
return true; |
9 | 3783 |
} |
0 | 3784 |
|
5 | 3785 |
/** |
16 | 3786 |
* Fires before comments are restored for a post from the Trash. |
5 | 3787 |
* |
3788 |
* @since 2.9.0 |
|
3789 |
* |
|
3790 |
* @param int $post_id Post ID. |
|
3791 |
*/ |
|
3792 |
do_action( 'untrash_post_comments', $post_id ); |
|
3793 |
||
3794 |
// Restore each comment to its original status. |
|
0 | 3795 |
$group_by_status = array(); |
9 | 3796 |
foreach ( $statuses as $comment_id => $comment_status ) { |
3797 |
$group_by_status[ $comment_status ][] = $comment_id; |
|
3798 |
} |
|
0 | 3799 |
|
3800 |
foreach ( $group_by_status as $status => $comments ) { |
|
3801 |
// Sanity check. This shouldn't happen. |
|
16 | 3802 |
if ( 'post-trashed' === $status ) { |
0 | 3803 |
$status = '0'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3804 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3805 |
$comments_in = implode( ', ', array_map( 'intval', $comments ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3806 |
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); |
0 | 3807 |
} |
3808 |
||
9 | 3809 |
clean_comment_cache( array_keys( $statuses ) ); |
3810 |
||
3811 |
delete_post_meta( $post_id, '_wp_trash_meta_comments_status' ); |
|
0 | 3812 |
|
5 | 3813 |
/** |
16 | 3814 |
* Fires after comments are restored for a post from the Trash. |
5 | 3815 |
* |
3816 |
* @since 2.9.0 |
|
3817 |
* |
|
3818 |
* @param int $post_id Post ID. |
|
3819 |
*/ |
|
3820 |
do_action( 'untrashed_post_comments', $post_id ); |
|
0 | 3821 |
} |
3822 |
||
3823 |
/** |
|
3824 |
* Retrieve the list of categories for a post. |
|
3825 |
* |
|
3826 |
* Compatibility layer for themes and plugins. Also an easy layer of abstraction |
|
3827 |
* away from the complexity of the taxonomy layer. |
|
3828 |
* |
|
3829 |
* @since 2.1.0 |
|
3830 |
* |
|
5 | 3831 |
* @see wp_get_object_terms() |
3832 |
* |
|
3833 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the |
|
3834 |
* global $post. Default 0. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3835 |
* @param array $args Optional. Category query parameters. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3836 |
* See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3837 |
* @return array|WP_Error List of categories. If the `$fields` argument passed via `$args` is 'all' or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3838 |
* 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` |
16 | 3839 |
* is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3840 |
* WP_Error object if 'category' taxonomy doesn't exist. |
0 | 3841 |
*/ |
3842 |
function wp_get_post_categories( $post_id = 0, $args = array() ) { |
|
3843 |
$post_id = (int) $post_id; |
|
3844 |
||
9 | 3845 |
$defaults = array( 'fields' => 'ids' ); |
3846 |
$args = wp_parse_args( $args, $defaults ); |
|
3847 |
||
3848 |
$cats = wp_get_object_terms( $post_id, 'category', $args ); |
|
0 | 3849 |
return $cats; |
3850 |
} |
|
3851 |
||
3852 |
/** |
|
3853 |
* Retrieve the tags for a post. |
|
3854 |
* |
|
3855 |
* There is only one default for this function, called 'fields' and by default |
|
3856 |
* is set to 'all'. There are other defaults that can be overridden in |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3857 |
* wp_get_object_terms(). |
0 | 3858 |
* |
3859 |
* @since 2.3.0 |
|
3860 |
* |
|
5 | 3861 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3862 |
* global $post. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3863 |
* @param array $args Optional. Tag query parameters. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3864 |
* See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3865 |
* @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3866 |
* WP_Error object if 'post_tag' taxonomy doesn't exist. |
0 | 3867 |
*/ |
3868 |
function wp_get_post_tags( $post_id = 0, $args = array() ) { |
|
9 | 3869 |
return wp_get_post_terms( $post_id, 'post_tag', $args ); |
0 | 3870 |
} |
3871 |
||
3872 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3873 |
* Retrieves the terms for a post. |
0 | 3874 |
* |
3875 |
* @since 2.8.0 |
|
3876 |
* |
|
18 | 3877 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the |
3878 |
* global $post. Default 0. |
|
3879 |
* @param string|string[] $taxonomy Optional. The taxonomy slug or array of slugs for which |
|
3880 |
* to retrieve terms. Default 'post_tag'. |
|
3881 |
* @param array $args { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3882 |
* Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3883 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3884 |
* @type string $fields Term fields to retrieve. Default 'all'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3885 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3886 |
* @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3887 |
* WP_Error object if `$taxonomy` doesn't exist. |
0 | 3888 |
*/ |
3889 |
function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { |
|
3890 |
$post_id = (int) $post_id; |
|
3891 |
||
9 | 3892 |
$defaults = array( 'fields' => 'all' ); |
3893 |
$args = wp_parse_args( $args, $defaults ); |
|
3894 |
||
3895 |
$tags = wp_get_object_terms( $post_id, $taxonomy, $args ); |
|
0 | 3896 |
|
3897 |
return $tags; |
|
3898 |
} |
|
3899 |
||
3900 |
/** |
|
5 | 3901 |
* Retrieve a number of recent posts. |
0 | 3902 |
* |
3903 |
* @since 1.0.0 |
|
5 | 3904 |
* |
3905 |
* @see get_posts() |
|
3906 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3907 |
* @param array $args Optional. Arguments to retrieve posts. Default empty array. |
16 | 3908 |
* @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which |
3909 |
* correspond to a WP_Post object or an associative array, respectively. |
|
3910 |
* Default ARRAY_A. |
|
3911 |
* @return array|false Array of recent posts, where the type of each element is determined |
|
3912 |
* by the `$output` parameter. Empty array on failure. |
|
0 | 3913 |
*/ |
3914 |
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { |
|
3915 |
||
3916 |
if ( is_numeric( $args ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3917 |
_deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); |
0 | 3918 |
$args = array( 'numberposts' => absint( $args ) ); |
3919 |
} |
|
3920 |
||
5 | 3921 |
// Set default arguments. |
0 | 3922 |
$defaults = array( |
9 | 3923 |
'numberposts' => 10, |
3924 |
'offset' => 0, |
|
3925 |
'category' => 0, |
|
3926 |
'orderby' => 'post_date', |
|
3927 |
'order' => 'DESC', |
|
3928 |
'include' => '', |
|
3929 |
'exclude' => '', |
|
3930 |
'meta_key' => '', |
|
3931 |
'meta_value' => '', |
|
3932 |
'post_type' => 'post', |
|
3933 |
'post_status' => 'draft, publish, future, pending, private', |
|
3934 |
'suppress_filters' => true, |
|
0 | 3935 |
); |
3936 |
||
16 | 3937 |
$parsed_args = wp_parse_args( $args, $defaults ); |
3938 |
||
3939 |
$results = get_posts( $parsed_args ); |
|
0 | 3940 |
|
5 | 3941 |
// Backward compatibility. Prior to 3.1 expected posts to be returned in array. |
18 | 3942 |
if ( ARRAY_A === $output ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3943 |
foreach ( $results as $key => $result ) { |
9 | 3944 |
$results[ $key ] = get_object_vars( $result ); |
0 | 3945 |
} |
3946 |
return $results ? $results : array(); |
|
3947 |
} |
|
3948 |
||
3949 |
return $results ? $results : false; |
|
3950 |
||
3951 |
} |
|
3952 |
||
3953 |
/** |
|
3954 |
* Insert or update a post. |
|
3955 |
* |
|
3956 |
* If the $postarr parameter has 'ID' set to a value, then post will be updated. |
|
3957 |
* |
|
3958 |
* You can set the post date manually, by setting the values for 'post_date' |
|
3959 |
* and 'post_date_gmt' keys. You can close the comments or open the comments by |
|
3960 |
* setting the value for 'comment_status' key. |
|
3961 |
* |
|
3962 |
* @since 1.0.0 |
|
18 | 3963 |
* @since 2.6.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. |
5 | 3964 |
* @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3965 |
* @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. |
18 | 3966 |
* @since 5.6.0 Added the `$fire_after_hooks` parameter. |
5 | 3967 |
* |
3968 |
* @see sanitize_post() |
|
3969 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 3970 |
* |
3971 |
* @param array $postarr { |
|
3972 |
* An array of elements that make up a post to update or insert. |
|
3973 |
* |
|
5 | 3974 |
* @type int $ID The post ID. If equal to something other than 0, |
3975 |
* the post with that ID will be updated. Default 0. |
|
3976 |
* @type int $post_author The ID of the user who added the post. Default is |
|
3977 |
* the current user ID. |
|
3978 |
* @type string $post_date The date of the post. Default is the current time. |
|
3979 |
* @type string $post_date_gmt The date of the post in the GMT timezone. Default is |
|
3980 |
* the value of `$post_date`. |
|
19 | 3981 |
* @type string $post_content The post content. Default empty. |
5 | 3982 |
* @type string $post_content_filtered The filtered post content. Default empty. |
3983 |
* @type string $post_title The post title. Default empty. |
|
3984 |
* @type string $post_excerpt The post excerpt. Default empty. |
|
3985 |
* @type string $post_status The post status. Default 'draft'. |
|
3986 |
* @type string $post_type The post type. Default 'post'. |
|
3987 |
* @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. |
|
3988 |
* Default is the value of 'default_comment_status' option. |
|
3989 |
* @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. |
|
3990 |
* Default is the value of 'default_ping_status' option. |
|
3991 |
* @type string $post_password The password to access the post. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3992 |
* @type string $post_name The post name. Default is the sanitized post title |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3993 |
* when creating a new post. |
5 | 3994 |
* @type string $to_ping Space or carriage return-separated list of URLs to ping. |
3995 |
* Default empty. |
|
3996 |
* @type string $pinged Space or carriage return-separated list of URLs that have |
|
3997 |
* been pinged. Default empty. |
|
3998 |
* @type string $post_modified The date when the post was last modified. Default is |
|
3999 |
* the current time. |
|
4000 |
* @type string $post_modified_gmt The date when the post was last modified in the GMT |
|
4001 |
* timezone. Default is the current time. |
|
4002 |
* @type int $post_parent Set this for the post it belongs to, if any. Default 0. |
|
4003 |
* @type int $menu_order The order the post should be displayed in. Default 0. |
|
4004 |
* @type string $post_mime_type The mime type of the post. Default empty. |
|
4005 |
* @type string $guid Global Unique ID for referencing the post. Default empty. |
|
18 | 4006 |
* @type int $import_id The post ID to be used when inserting a new post. |
4007 |
* If specified, must not match any existing post ID. Default 0. |
|
4008 |
* @type int[] $post_category Array of category IDs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4009 |
* Defaults to value of the 'default_category' option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4010 |
* @type array $tags_input Array of tag names, slugs, or IDs. Default empty. |
19 | 4011 |
* @type array $tax_input An array of taxonomy terms keyed by their taxonomy name. |
4012 |
* If the taxonomy is hierarchical, the term list needs to be |
|
4013 |
* either an array of term IDs or a comma-separated string of IDs. |
|
4014 |
* If the taxonomy is non-hierarchical, the term list can be an array |
|
4015 |
* that contains term names or slugs, or a comma-separated string |
|
4016 |
* of names or slugs. This is because, in hierarchical taxonomy, |
|
4017 |
* child terms can have the same names with different parent terms, |
|
4018 |
* so the only way to connect them is using ID. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4019 |
* @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. |
0 | 4020 |
* } |
18 | 4021 |
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
4022 |
* @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
|
0 | 4023 |
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
4024 |
*/ |
|
18 | 4025 |
function wp_insert_post( $postarr, $wp_error = false, $fire_after_hooks = true ) { |
0 | 4026 |
global $wpdb; |
4027 |
||
16 | 4028 |
// Capture original pre-sanitized array for passing into filters. |
4029 |
$unsanitized_postarr = $postarr; |
|
4030 |
||
0 | 4031 |
$user_id = get_current_user_id(); |
4032 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4033 |
$defaults = array( |
9 | 4034 |
'post_author' => $user_id, |
4035 |
'post_content' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4036 |
'post_content_filtered' => '', |
9 | 4037 |
'post_title' => '', |
4038 |
'post_excerpt' => '', |
|
4039 |
'post_status' => 'draft', |
|
4040 |
'post_type' => 'post', |
|
4041 |
'comment_status' => '', |
|
4042 |
'ping_status' => '', |
|
4043 |
'post_password' => '', |
|
4044 |
'to_ping' => '', |
|
4045 |
'pinged' => '', |
|
4046 |
'post_parent' => 0, |
|
4047 |
'menu_order' => 0, |
|
4048 |
'guid' => '', |
|
4049 |
'import_id' => 0, |
|
4050 |
'context' => '', |
|
18 | 4051 |
'post_date' => '', |
4052 |
'post_date_gmt' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4053 |
); |
0 | 4054 |
|
9 | 4055 |
$postarr = wp_parse_args( $postarr, $defaults ); |
4056 |
||
4057 |
unset( $postarr['filter'] ); |
|
4058 |
||
4059 |
$postarr = sanitize_post( $postarr, 'db' ); |
|
0 | 4060 |
|
4061 |
// Are we updating or creating? |
|
4062 |
$post_ID = 0; |
|
9 | 4063 |
$update = false; |
4064 |
$guid = $postarr['guid']; |
|
5 | 4065 |
|
4066 |
if ( ! empty( $postarr['ID'] ) ) { |
|
0 | 4067 |
$update = true; |
4068 |
||
5 | 4069 |
// Get the post ID and GUID. |
9 | 4070 |
$post_ID = $postarr['ID']; |
0 | 4071 |
$post_before = get_post( $post_ID ); |
16 | 4072 |
|
0 | 4073 |
if ( is_null( $post_before ) ) { |
5 | 4074 |
if ( $wp_error ) { |
0 | 4075 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
5 | 4076 |
} |
0 | 4077 |
return 0; |
4078 |
} |
|
4079 |
||
9 | 4080 |
$guid = get_post_field( 'guid', $post_ID ); |
4081 |
$previous_status = get_post_field( 'post_status', $post_ID ); |
|
0 | 4082 |
} else { |
4083 |
$previous_status = 'new'; |
|
18 | 4084 |
$post_before = null; |
0 | 4085 |
} |
4086 |
||
5 | 4087 |
$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; |
4088 |
||
9 | 4089 |
$post_title = $postarr['post_title']; |
5 | 4090 |
$post_content = $postarr['post_content']; |
4091 |
$post_excerpt = $postarr['post_excerpt']; |
|
16 | 4092 |
|
5 | 4093 |
if ( isset( $postarr['post_name'] ) ) { |
4094 |
$post_name = $postarr['post_name']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4095 |
} elseif ( $update ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4096 |
// For an update, don't modify the post_name if it wasn't supplied as an argument. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4097 |
$post_name = $post_before->post_name; |
0 | 4098 |
} |
4099 |
||
5 | 4100 |
$maybe_empty = 'attachment' !== $post_type |
4101 |
&& ! $post_content && ! $post_title && ! $post_excerpt |
|
4102 |
&& post_type_supports( $post_type, 'editor' ) |
|
4103 |
&& post_type_supports( $post_type, 'title' ) |
|
4104 |
&& post_type_supports( $post_type, 'excerpt' ); |
|
4105 |
||
4106 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4107 |
* Filters whether the post should be considered "empty". |
5 | 4108 |
* |
4109 |
* The post is considered "empty" if both: |
|
4110 |
* 1. The post type supports the title, editor, and excerpt fields |
|
4111 |
* 2. The title, editor, and excerpt fields are all empty |
|
4112 |
* |
|
16 | 4113 |
* Returning a truthy value from the filter will effectively short-circuit |
4114 |
* the new post being inserted and return 0. If $wp_error is true, a WP_Error |
|
5 | 4115 |
* will be returned instead. |
4116 |
* |
|
4117 |
* @since 3.3.0 |
|
4118 |
* |
|
4119 |
* @param bool $maybe_empty Whether the post should be considered "empty". |
|
4120 |
* @param array $postarr Array of post data. |
|
4121 |
*/ |
|
4122 |
if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { |
|
4123 |
if ( $wp_error ) { |
|
4124 |
return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); |
|
4125 |
} else { |
|
4126 |
return 0; |
|
4127 |
} |
|
4128 |
} |
|
4129 |
||
4130 |
$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; |
|
16 | 4131 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4132 |
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) { |
5 | 4133 |
$post_status = 'inherit'; |
4134 |
} |
|
4135 |
||
4136 |
if ( ! empty( $postarr['post_category'] ) ) { |
|
4137 |
// Filter out empty terms. |
|
4138 |
$post_category = array_filter( $postarr['post_category'] ); |
|
4139 |
} |
|
0 | 4140 |
|
4141 |
// Make sure we set a valid category. |
|
16 | 4142 |
if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) { |
0 | 4143 |
// 'post' requires at least one category. |
16 | 4144 |
if ( 'post' === $post_type && 'auto-draft' !== $post_status ) { |
9 | 4145 |
$post_category = array( get_option( 'default_category' ) ); |
5 | 4146 |
} else { |
0 | 4147 |
$post_category = array(); |
5 | 4148 |
} |
0 | 4149 |
} |
4150 |
||
9 | 4151 |
/* |
4152 |
* Don't allow contributors to set the post slug for pending review posts. |
|
4153 |
* |
|
4154 |
* For new posts check the primitive capability, for updates check the meta capability. |
|
4155 |
*/ |
|
4156 |
$post_type_object = get_post_type_object( $post_type ); |
|
4157 |
||
4158 |
if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
|
4159 |
$post_name = ''; |
|
4160 |
} elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) { |
|
0 | 4161 |
$post_name = ''; |
5 | 4162 |
} |
4163 |
||
4164 |
/* |
|
4165 |
* Create a valid post name. Drafts and pending posts are allowed to have |
|
4166 |
* an empty post name. |
|
4167 |
*/ |
|
9 | 4168 |
if ( empty( $post_name ) ) { |
16 | 4169 |
if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) { |
9 | 4170 |
$post_name = sanitize_title( $post_title ); |
5 | 4171 |
} else { |
0 | 4172 |
$post_name = ''; |
5 | 4173 |
} |
0 | 4174 |
} else { |
4175 |
// On updates, we need to check to see if it's using the old, fixed sanitization context. |
|
4176 |
$check_name = sanitize_title( $post_name, '', 'old-save' ); |
|
16 | 4177 |
|
5 | 4178 |
if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { |
0 | 4179 |
$post_name = $check_name; |
5 | 4180 |
} else { // new post, or slug has changed. |
9 | 4181 |
$post_name = sanitize_title( $post_name ); |
5 | 4182 |
} |
4183 |
} |
|
4184 |
||
4185 |
/* |
|
18 | 4186 |
* Resolve the post date from any provided post date or post date GMT strings; |
4187 |
* if none are provided, the date will be set to now. |
|
5 | 4188 |
*/ |
18 | 4189 |
$post_date = wp_resolve_post_date( $postarr['post_date'], $postarr['post_date_gmt'] ); |
4190 |
if ( ! $post_date ) { |
|
5 | 4191 |
if ( $wp_error ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4192 |
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
5 | 4193 |
} else { |
4194 |
return 0; |
|
0 | 4195 |
} |
5 | 4196 |
} |
4197 |
||
16 | 4198 |
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) { |
4199 |
if ( ! in_array( $post_status, get_post_stati( array( 'date_floating' => true ) ), true ) ) { |
|
5 | 4200 |
$post_date_gmt = get_gmt_from_date( $post_date ); |
4201 |
} else { |
|
0 | 4202 |
$post_date_gmt = '0000-00-00 00:00:00'; |
5 | 4203 |
} |
4204 |
} else { |
|
4205 |
$post_date_gmt = $postarr['post_date_gmt']; |
|
0 | 4206 |
} |
4207 |
||
16 | 4208 |
if ( $update || '0000-00-00 00:00:00' === $post_date ) { |
0 | 4209 |
$post_modified = current_time( 'mysql' ); |
4210 |
$post_modified_gmt = current_time( 'mysql', 1 ); |
|
4211 |
} else { |
|
4212 |
$post_modified = $post_date; |
|
4213 |
$post_modified_gmt = $post_date_gmt; |
|
4214 |
} |
|
4215 |
||
5 | 4216 |
if ( 'attachment' !== $post_type ) { |
16 | 4217 |
$now = gmdate( 'Y-m-d H:i:s' ); |
4218 |
||
4219 |
if ( 'publish' === $post_status ) { |
|
4220 |
if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) { |
|
5 | 4221 |
$post_status = 'future'; |
4222 |
} |
|
16 | 4223 |
} elseif ( 'future' === $post_status ) { |
4224 |
if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) { |
|
5 | 4225 |
$post_status = 'publish'; |
4226 |
} |
|
4227 |
} |
|
4228 |
} |
|
4229 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4230 |
// Comment status. |
5 | 4231 |
if ( empty( $postarr['comment_status'] ) ) { |
4232 |
if ( $update ) { |
|
4233 |
$comment_status = 'closed'; |
|
4234 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4235 |
$comment_status = get_default_comment_status( $post_type ); |
5 | 4236 |
} |
4237 |
} else { |
|
4238 |
$comment_status = $postarr['comment_status']; |
|
0 | 4239 |
} |
4240 |
||
5 | 4241 |
// These variables are needed by compact() later. |
4242 |
$post_content_filtered = $postarr['post_content_filtered']; |
|
9 | 4243 |
$post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; |
4244 |
$ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; |
|
4245 |
$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; |
|
4246 |
$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; |
|
4247 |
$import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; |
|
5 | 4248 |
|
4249 |
/* |
|
4250 |
* The 'wp_insert_post_parent' filter expects all variables to be present. |
|
4251 |
* Previously, these variables would have already been extracted |
|
4252 |
*/ |
|
4253 |
if ( isset( $postarr['menu_order'] ) ) { |
|
4254 |
$menu_order = (int) $postarr['menu_order']; |
|
4255 |
} else { |
|
4256 |
$menu_order = 0; |
|
4257 |
} |
|
4258 |
||
4259 |
$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; |
|
16 | 4260 |
if ( 'private' === $post_status ) { |
5 | 4261 |
$post_password = ''; |
4262 |
} |
|
4263 |
||
4264 |
if ( isset( $postarr['post_parent'] ) ) { |
|
4265 |
$post_parent = (int) $postarr['post_parent']; |
|
4266 |
} else { |
|
4267 |
$post_parent = 0; |
|
0 | 4268 |
} |
5 | 4269 |
|
9 | 4270 |
$new_postarr = array_merge( |
4271 |
array( |
|
4272 |
'ID' => $post_ID, |
|
4273 |
), |
|
4274 |
compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) |
|
4275 |
); |
|
4276 |
||
5 | 4277 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4278 |
* Filters the post parent -- used to check for and prevent hierarchy loops. |
5 | 4279 |
* |
4280 |
* @since 3.1.0 |
|
4281 |
* |
|
4282 |
* @param int $post_parent Post parent ID. |
|
4283 |
* @param int $post_ID Post ID. |
|
4284 |
* @param array $new_postarr Array of parsed post data. |
|
4285 |
* @param array $postarr Array of sanitized, but otherwise unmodified post data. |
|
4286 |
*/ |
|
9 | 4287 |
$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr ); |
0 | 4288 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4289 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4290 |
* If the post is being untrashed and it has a desired slug stored in post meta, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4291 |
* reassign it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4292 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4293 |
if ( 'trash' === $previous_status && 'trash' !== $post_status ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4294 |
$desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); |
16 | 4295 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4296 |
if ( $desired_post_slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4297 |
delete_post_meta( $post_ID, '_wp_desired_post_slug' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4298 |
$post_name = $desired_post_slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4299 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4300 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4301 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4302 |
// If a trashed post has the desired slug, change it and let this post have it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4303 |
if ( 'trash' !== $post_status && $post_name ) { |
16 | 4304 |
/** |
4305 |
* Filters whether or not to add a `__trashed` suffix to trashed posts that match the name of the updated post. |
|
4306 |
* |
|
4307 |
* @since 5.4.0 |
|
4308 |
* |
|
4309 |
* @param bool $add_trashed_suffix Whether to attempt to add the suffix. |
|
4310 |
* @param string $post_name The name of the post being updated. |
|
4311 |
* @param int $post_ID Post ID. |
|
4312 |
*/ |
|
4313 |
$add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_ID ); |
|
4314 |
||
4315 |
if ( $add_trashed_suffix ) { |
|
4316 |
wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); |
|
4317 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4318 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4320 |
// When trashing an existing post, change its slug to allow non-trashed posts to use it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4321 |
if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4322 |
$post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4323 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4324 |
|
5 | 4325 |
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); |
4326 |
||
4327 |
// Don't unslash. |
|
4328 |
$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; |
|
4329 |
||
4330 |
// Expected_slashed (everything!). |
|
4331 |
$data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ); |
|
4332 |
||
4333 |
$emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
|
4334 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4335 |
foreach ( $emoji_fields as $emoji_field ) { |
5 | 4336 |
if ( isset( $data[ $emoji_field ] ) ) { |
4337 |
$charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); |
|
16 | 4338 |
|
5 | 4339 |
if ( 'utf8' === $charset ) { |
4340 |
$data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); |
|
4341 |
} |
|
4342 |
} |
|
4343 |
} |
|
4344 |
||
4345 |
if ( 'attachment' === $post_type ) { |
|
4346 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4347 |
* Filters attachment post data before it is updated in or added to the database. |
5 | 4348 |
* |
4349 |
* @since 3.9.0 |
|
19 | 4350 |
* @since 5.4.1 The `$unsanitized_postarr` parameter was added. |
4351 |
* @since 6.0.0 The `$update` parameter was added. |
|
5 | 4352 |
* |
16 | 4353 |
* @param array $data An array of slashed, sanitized, and processed attachment post data. |
4354 |
* @param array $postarr An array of slashed and sanitized attachment post data, but not processed. |
|
4355 |
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data |
|
4356 |
* as originally passed to wp_insert_post(). |
|
19 | 4357 |
* @param bool $update Whether this is an existing attachment post being updated. |
5 | 4358 |
*/ |
19 | 4359 |
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr, $update ); |
5 | 4360 |
} else { |
4361 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4362 |
* Filters slashed post data just before it is inserted into the database. |
5 | 4363 |
* |
4364 |
* @since 2.7.0 |
|
19 | 4365 |
* @since 5.4.1 The `$unsanitized_postarr` parameter was added. |
4366 |
* @since 6.0.0 The `$update` parameter was added. |
|
5 | 4367 |
* |
16 | 4368 |
* @param array $data An array of slashed, sanitized, and processed post data. |
4369 |
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data. |
|
4370 |
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as |
|
4371 |
* originally passed to wp_insert_post(). |
|
19 | 4372 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4373 |
*/ |
19 | 4374 |
$data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr, $update ); |
16 | 4375 |
} |
4376 |
||
9 | 4377 |
$data = wp_unslash( $data ); |
0 | 4378 |
$where = array( 'ID' => $post_ID ); |
4379 |
||
4380 |
if ( $update ) { |
|
5 | 4381 |
/** |
4382 |
* Fires immediately before an existing post is updated in the database. |
|
4383 |
* |
|
4384 |
* @since 2.5.0 |
|
4385 |
* |
|
4386 |
* @param int $post_ID Post ID. |
|
4387 |
* @param array $data Array of unslashed post data. |
|
4388 |
*/ |
|
0 | 4389 |
do_action( 'pre_post_update', $post_ID, $data ); |
16 | 4390 |
|
0 | 4391 |
if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
5 | 4392 |
if ( $wp_error ) { |
16 | 4393 |
if ( 'attachment' === $post_type ) { |
4394 |
$message = __( 'Could not update attachment in the database.' ); |
|
4395 |
} else { |
|
4396 |
$message = __( 'Could not update post in the database.' ); |
|
4397 |
} |
|
4398 |
||
4399 |
return new WP_Error( 'db_update_error', $message, $wpdb->last_error ); |
|
5 | 4400 |
} else { |
0 | 4401 |
return 0; |
5 | 4402 |
} |
0 | 4403 |
} |
4404 |
} else { |
|
5 | 4405 |
// If there is a suggested ID, use it if not already present. |
4406 |
if ( ! empty( $import_id ) ) { |
|
0 | 4407 |
$import_id = (int) $import_id; |
16 | 4408 |
|
9 | 4409 |
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { |
0 | 4410 |
$data['ID'] = $import_id; |
4411 |
} |
|
4412 |
} |
|
16 | 4413 |
|
0 | 4414 |
if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { |
5 | 4415 |
if ( $wp_error ) { |
16 | 4416 |
if ( 'attachment' === $post_type ) { |
4417 |
$message = __( 'Could not insert attachment into the database.' ); |
|
4418 |
} else { |
|
4419 |
$message = __( 'Could not insert post into the database.' ); |
|
4420 |
} |
|
4421 |
||
4422 |
return new WP_Error( 'db_insert_error', $message, $wpdb->last_error ); |
|
5 | 4423 |
} else { |
0 | 4424 |
return 0; |
5 | 4425 |
} |
0 | 4426 |
} |
16 | 4427 |
|
0 | 4428 |
$post_ID = (int) $wpdb->insert_id; |
4429 |
||
5 | 4430 |
// Use the newly generated $post_ID. |
0 | 4431 |
$where = array( 'ID' => $post_ID ); |
4432 |
} |
|
4433 |
||
16 | 4434 |
if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4435 |
$data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); |
16 | 4436 |
|
0 | 4437 |
$wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4438 |
clean_post_cache( $post_ID ); |
0 | 4439 |
} |
4440 |
||
5 | 4441 |
if ( is_object_in_taxonomy( $post_type, 'category' ) ) { |
0 | 4442 |
wp_set_post_categories( $post_ID, $post_category ); |
5 | 4443 |
} |
4444 |
||
4445 |
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { |
|
4446 |
wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
|
4447 |
} |
|
4448 |
||
16 | 4449 |
// Add default term for all associated custom taxonomies. |
4450 |
if ( 'auto-draft' !== $post_status ) { |
|
4451 |
foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
|
4452 |
||
4453 |
if ( ! empty( $tax_object->default_term ) ) { |
|
4454 |
||
4455 |
// Filter out empty terms. |
|
18 | 4456 |
if ( isset( $postarr['tax_input'][ $taxonomy ] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { |
16 | 4457 |
$postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); |
4458 |
} |
|
4459 |
||
4460 |
// Passed custom taxonomy list overwrites the existing list if not empty. |
|
4461 |
$terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) ); |
|
4462 |
if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
|
4463 |
$postarr['tax_input'][ $taxonomy ] = $terms; |
|
4464 |
} |
|
4465 |
||
4466 |
if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
|
4467 |
$default_term_id = get_option( 'default_term_' . $taxonomy ); |
|
4468 |
if ( ! empty( $default_term_id ) ) { |
|
4469 |
$postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id ); |
|
4470 |
} |
|
4471 |
} |
|
4472 |
} |
|
4473 |
} |
|
4474 |
} |
|
4475 |
||
5 | 4476 |
// New-style support for all custom taxonomies. |
4477 |
if ( ! empty( $postarr['tax_input'] ) ) { |
|
4478 |
foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { |
|
9 | 4479 |
$taxonomy_obj = get_taxonomy( $taxonomy ); |
16 | 4480 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4481 |
if ( ! $taxonomy_obj ) { |
16 | 4482 |
/* translators: %s: Taxonomy name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4483 |
_doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4484 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4485 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4486 |
|
5 | 4487 |
// array = hierarchical, string = non-hierarchical. |
4488 |
if ( is_array( $tags ) ) { |
|
9 | 4489 |
$tags = array_filter( $tags ); |
5 | 4490 |
} |
16 | 4491 |
|
5 | 4492 |
if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
0 | 4493 |
wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
5 | 4494 |
} |
0 | 4495 |
} |
4496 |
} |
|
4497 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4498 |
if ( ! empty( $postarr['meta_input'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4499 |
foreach ( $postarr['meta_input'] as $field => $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4500 |
update_post_meta( $post_ID, $field, $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4501 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4502 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4503 |
|
0 | 4504 |
$current_guid = get_post_field( 'guid', $post_ID ); |
4505 |
||
5 | 4506 |
// Set GUID. |
16 | 4507 |
if ( ! $update && '' === $current_guid ) { |
0 | 4508 |
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
5 | 4509 |
} |
4510 |
||
4511 |
if ( 'attachment' === $postarr['post_type'] ) { |
|
4512 |
if ( ! empty( $postarr['file'] ) ) { |
|
4513 |
update_attached_file( $post_ID, $postarr['file'] ); |
|
4514 |
} |
|
4515 |
||
4516 |
if ( ! empty( $postarr['context'] ) ) { |
|
4517 |
add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); |
|
4518 |
} |
|
4519 |
} |
|
0 | 4520 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4521 |
// Set or remove featured image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4522 |
if ( isset( $postarr['_thumbnail_id'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4523 |
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; |
16 | 4524 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4525 |
if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4526 |
if ( wp_attachment_is( 'audio', $post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4527 |
$thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4528 |
} elseif ( wp_attachment_is( 'video', $post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4529 |
$thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4530 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4531 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4532 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4533 |
if ( $thumbnail_support ) { |
18 | 4534 |
$thumbnail_id = (int) $postarr['_thumbnail_id']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4535 |
if ( -1 === $thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4536 |
delete_post_thumbnail( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4537 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4538 |
set_post_thumbnail( $post_ID, $thumbnail_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4539 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4540 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4541 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4542 |
|
0 | 4543 |
clean_post_cache( $post_ID ); |
4544 |
||
5 | 4545 |
$post = get_post( $post_ID ); |
4546 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4547 |
if ( ! empty( $postarr['page_template'] ) ) { |
5 | 4548 |
$post->page_template = $postarr['page_template']; |
9 | 4549 |
$page_templates = wp_get_theme()->get_page_templates( $post ); |
16 | 4550 |
|
4551 |
if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { |
|
5 | 4552 |
if ( $wp_error ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4553 |
return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); |
5 | 4554 |
} |
16 | 4555 |
|
5 | 4556 |
update_post_meta( $post_ID, '_wp_page_template', 'default' ); |
4557 |
} else { |
|
4558 |
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); |
|
0 | 4559 |
} |
4560 |
} |
|
4561 |
||
5 | 4562 |
if ( 'attachment' !== $postarr['post_type'] ) { |
4563 |
wp_transition_post_status( $data['post_status'], $previous_status, $post ); |
|
4564 |
} else { |
|
4565 |
if ( $update ) { |
|
4566 |
/** |
|
4567 |
* Fires once an existing attachment has been updated. |
|
4568 |
* |
|
4569 |
* @since 2.0.0 |
|
4570 |
* |
|
4571 |
* @param int $post_ID Attachment ID. |
|
4572 |
*/ |
|
4573 |
do_action( 'edit_attachment', $post_ID ); |
|
16 | 4574 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4575 |
$post_after = get_post( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4576 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4577 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4578 |
* Fires once an existing attachment has been updated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4579 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4580 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4581 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4582 |
* @param int $post_ID Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4583 |
* @param WP_Post $post_after Post object following the update. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4584 |
* @param WP_Post $post_before Post object before the update. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4585 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4586 |
do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); |
5 | 4587 |
} else { |
4588 |
||
4589 |
/** |
|
4590 |
* Fires once an attachment has been added. |
|
4591 |
* |
|
4592 |
* @since 2.0.0 |
|
4593 |
* |
|
4594 |
* @param int $post_ID Attachment ID. |
|
4595 |
*/ |
|
4596 |
do_action( 'add_attachment', $post_ID ); |
|
4597 |
} |
|
4598 |
||
4599 |
return $post_ID; |
|
4600 |
} |
|
0 | 4601 |
|
4602 |
if ( $update ) { |
|
5 | 4603 |
/** |
4604 |
* Fires once an existing post has been updated. |
|
4605 |
* |
|
9 | 4606 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
4607 |
* the post type slug. |
|
4608 |
* |
|
19 | 4609 |
* Possible hook names include: |
4610 |
* |
|
4611 |
* - `edit_post_post` |
|
4612 |
* - `edit_post_page` |
|
4613 |
* |
|
9 | 4614 |
* @since 5.1.0 |
4615 |
* |
|
4616 |
* @param int $post_ID Post ID. |
|
4617 |
* @param WP_Post $post Post object. |
|
4618 |
*/ |
|
4619 |
do_action( "edit_post_{$post->post_type}", $post_ID, $post ); |
|
4620 |
||
4621 |
/** |
|
4622 |
* Fires once an existing post has been updated. |
|
4623 |
* |
|
5 | 4624 |
* @since 1.2.0 |
4625 |
* |
|
4626 |
* @param int $post_ID Post ID. |
|
4627 |
* @param WP_Post $post Post object. |
|
4628 |
*/ |
|
4629 |
do_action( 'edit_post', $post_ID, $post ); |
|
9 | 4630 |
|
4631 |
$post_after = get_post( $post_ID ); |
|
5 | 4632 |
|
4633 |
/** |
|
4634 |
* Fires once an existing post has been updated. |
|
4635 |
* |
|
4636 |
* @since 3.0.0 |
|
4637 |
* |
|
4638 |
* @param int $post_ID Post ID. |
|
4639 |
* @param WP_Post $post_after Post object following the update. |
|
4640 |
* @param WP_Post $post_before Post object before the update. |
|
4641 |
*/ |
|
9 | 4642 |
do_action( 'post_updated', $post_ID, $post_after, $post_before ); |
0 | 4643 |
} |
4644 |
||
5 | 4645 |
/** |
4646 |
* Fires once a post has been saved. |
|
4647 |
* |
|
4648 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
|
4649 |
* the post type slug. |
|
4650 |
* |
|
19 | 4651 |
* Possible hook names include: |
4652 |
* |
|
4653 |
* - `save_post_post` |
|
4654 |
* - `save_post_page` |
|
4655 |
* |
|
5 | 4656 |
* @since 3.7.0 |
4657 |
* |
|
4658 |
* @param int $post_ID Post ID. |
|
4659 |
* @param WP_Post $post Post object. |
|
16 | 4660 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4661 |
*/ |
0 | 4662 |
do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); |
5 | 4663 |
|
4664 |
/** |
|
4665 |
* Fires once a post has been saved. |
|
4666 |
* |
|
4667 |
* @since 1.5.0 |
|
4668 |
* |
|
4669 |
* @param int $post_ID Post ID. |
|
4670 |
* @param WP_Post $post Post object. |
|
16 | 4671 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4672 |
*/ |
0 | 4673 |
do_action( 'save_post', $post_ID, $post, $update ); |
5 | 4674 |
|
4675 |
/** |
|
4676 |
* Fires once a post has been saved. |
|
4677 |
* |
|
4678 |
* @since 2.0.0 |
|
4679 |
* |
|
4680 |
* @param int $post_ID Post ID. |
|
4681 |
* @param WP_Post $post Post object. |
|
16 | 4682 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4683 |
*/ |
0 | 4684 |
do_action( 'wp_insert_post', $post_ID, $post, $update ); |
4685 |
||
18 | 4686 |
if ( $fire_after_hooks ) { |
4687 |
wp_after_insert_post( $post, $update, $post_before ); |
|
4688 |
} |
|
4689 |
||
0 | 4690 |
return $post_ID; |
4691 |
} |
|
4692 |
||
4693 |
/** |
|
4694 |
* Update a post with new post data. |
|
4695 |
* |
|
4696 |
* The date does not have to be set for drafts. You can set the date and it will |
|
4697 |
* not be overridden. |
|
4698 |
* |
|
4699 |
* @since 1.0.0 |
|
18 | 4700 |
* @since 3.5.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. |
4701 |
* @since 5.6.0 Added the `$fire_after_hooks` parameter. |
|
4702 |
* |
|
4703 |
* @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, |
|
4704 |
* objects are not. See wp_insert_post() for accepted arguments. |
|
4705 |
* Default array. |
|
4706 |
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
|
4707 |
* @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
|
16 | 4708 |
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
0 | 4709 |
*/ |
18 | 4710 |
function wp_update_post( $postarr = array(), $wp_error = false, $fire_after_hooks = true ) { |
9 | 4711 |
if ( is_object( $postarr ) ) { |
5 | 4712 |
// Non-escaped post was passed. |
9 | 4713 |
$postarr = get_object_vars( $postarr ); |
4714 |
$postarr = wp_slash( $postarr ); |
|
0 | 4715 |
} |
4716 |
||
5 | 4717 |
// First, get all of the original fields. |
9 | 4718 |
$post = get_post( $postarr['ID'], ARRAY_A ); |
0 | 4719 |
|
4720 |
if ( is_null( $post ) ) { |
|
9 | 4721 |
if ( $wp_error ) { |
0 | 4722 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
9 | 4723 |
} |
0 | 4724 |
return 0; |
4725 |
} |
|
4726 |
||
4727 |
// Escape data pulled from DB. |
|
9 | 4728 |
$post = wp_slash( $post ); |
0 | 4729 |
|
4730 |
// Passed post category list overwrites existing category list if not empty. |
|
9 | 4731 |
if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] ) |
16 | 4732 |
&& count( $postarr['post_category'] ) > 0 |
4733 |
) { |
|
0 | 4734 |
$post_cats = $postarr['post_category']; |
9 | 4735 |
} else { |
0 | 4736 |
$post_cats = $post['post_category']; |
9 | 4737 |
} |
0 | 4738 |
|
5 | 4739 |
// Drafts shouldn't be assigned a date unless explicitly done so by the user. |
16 | 4740 |
if ( isset( $post['post_status'] ) |
4741 |
&& in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) |
|
4742 |
&& empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] ) |
|
4743 |
) { |
|
0 | 4744 |
$clear_date = true; |
9 | 4745 |
} else { |
0 | 4746 |
$clear_date = false; |
9 | 4747 |
} |
0 | 4748 |
|
4749 |
// Merge old and new fields with new fields overwriting old ones. |
|
9 | 4750 |
$postarr = array_merge( $post, $postarr ); |
0 | 4751 |
$postarr['post_category'] = $post_cats; |
4752 |
if ( $clear_date ) { |
|
9 | 4753 |
$postarr['post_date'] = current_time( 'mysql' ); |
0 | 4754 |
$postarr['post_date_gmt'] = ''; |
4755 |
} |
|
4756 |
||
16 | 4757 |
if ( 'attachment' === $postarr['post_type'] ) { |
9 | 4758 |
return wp_insert_attachment( $postarr, false, 0, $wp_error ); |
4759 |
} |
|
0 | 4760 |
|
16 | 4761 |
// Discard 'tags_input' parameter if it's the same as existing post tags. |
4762 |
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) { |
|
4763 |
$tags = get_the_terms( $postarr['ID'], 'post_tag' ); |
|
4764 |
$tag_names = array(); |
|
4765 |
||
4766 |
if ( $tags && ! is_wp_error( $tags ) ) { |
|
4767 |
$tag_names = wp_list_pluck( $tags, 'name' ); |
|
4768 |
} |
|
4769 |
||
4770 |
if ( $postarr['tags_input'] === $tag_names ) { |
|
4771 |
unset( $postarr['tags_input'] ); |
|
4772 |
} |
|
4773 |
} |
|
4774 |
||
18 | 4775 |
return wp_insert_post( $postarr, $wp_error, $fire_after_hooks ); |
0 | 4776 |
} |
4777 |
||
4778 |
/** |
|
4779 |
* Publish a post by transitioning the post status. |
|
4780 |
* |
|
4781 |
* @since 2.1.0 |
|
5 | 4782 |
* |
4783 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4784 |
* |
|
4785 |
* @param int|WP_Post $post Post ID or post object. |
|
0 | 4786 |
*/ |
4787 |
function wp_publish_post( $post ) { |
|
4788 |
global $wpdb; |
|
4789 |
||
16 | 4790 |
$post = get_post( $post ); |
18 | 4791 |
|
16 | 4792 |
if ( ! $post ) { |
0 | 4793 |
return; |
9 | 4794 |
} |
4795 |
||
16 | 4796 |
if ( 'publish' === $post->post_status ) { |
0 | 4797 |
return; |
9 | 4798 |
} |
0 | 4799 |
|
18 | 4800 |
$post_before = get_post( $post->ID ); |
4801 |
||
4802 |
// Ensure at least one term is applied for taxonomies with a default term. |
|
4803 |
foreach ( get_object_taxonomies( $post->post_type, 'object' ) as $taxonomy => $tax_object ) { |
|
4804 |
// Skip taxonomy if no default term is set. |
|
4805 |
if ( |
|
4806 |
'category' !== $taxonomy && |
|
4807 |
empty( $tax_object->default_term ) |
|
4808 |
) { |
|
4809 |
continue; |
|
4810 |
} |
|
4811 |
||
4812 |
// Do not modify previously set terms. |
|
4813 |
if ( ! empty( get_the_terms( $post, $taxonomy ) ) ) { |
|
4814 |
continue; |
|
4815 |
} |
|
4816 |
||
4817 |
if ( 'category' === $taxonomy ) { |
|
4818 |
$default_term_id = (int) get_option( 'default_category', 0 ); |
|
4819 |
} else { |
|
4820 |
$default_term_id = (int) get_option( 'default_term_' . $taxonomy, 0 ); |
|
4821 |
} |
|
4822 |
||
4823 |
if ( ! $default_term_id ) { |
|
4824 |
continue; |
|
4825 |
} |
|
4826 |
wp_set_post_terms( $post->ID, array( $default_term_id ), $taxonomy ); |
|
4827 |
} |
|
4828 |
||
0 | 4829 |
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); |
4830 |
||
4831 |
clean_post_cache( $post->ID ); |
|
4832 |
||
9 | 4833 |
$old_status = $post->post_status; |
0 | 4834 |
$post->post_status = 'publish'; |
4835 |
wp_transition_post_status( 'publish', $old_status, $post ); |
|
4836 |
||
5 | 4837 |
/** This action is documented in wp-includes/post.php */ |
9 | 4838 |
do_action( "edit_post_{$post->post_type}", $post->ID, $post ); |
4839 |
||
4840 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4841 |
do_action( 'edit_post', $post->ID, $post ); |
5 | 4842 |
|
4843 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4844 |
do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
5 | 4845 |
|
4846 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4847 |
do_action( 'save_post', $post->ID, $post, true ); |
5 | 4848 |
|
4849 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4850 |
do_action( 'wp_insert_post', $post->ID, $post, true ); |
18 | 4851 |
|
4852 |
wp_after_insert_post( $post, true, $post_before ); |
|
0 | 4853 |
} |
4854 |
||
4855 |
/** |
|
4856 |
* Publish future post and make sure post ID has future post status. |
|
4857 |
* |
|
4858 |
* Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
|
4859 |
* from publishing drafts, etc. |
|
4860 |
* |
|
4861 |
* @since 2.5.0 |
|
4862 |
* |
|
5 | 4863 |
* @param int|WP_Post $post_id Post ID or post object. |
0 | 4864 |
*/ |
5 | 4865 |
function check_and_publish_future_post( $post_id ) { |
9 | 4866 |
$post = get_post( $post_id ); |
4867 |
||
18 | 4868 |
if ( ! $post ) { |
0 | 4869 |
return; |
9 | 4870 |
} |
4871 |
||
16 | 4872 |
if ( 'future' !== $post->post_status ) { |
0 | 4873 |
return; |
9 | 4874 |
} |
0 | 4875 |
|
4876 |
$time = strtotime( $post->post_date_gmt . ' GMT' ); |
|
4877 |
||
5 | 4878 |
// Uh oh, someone jumped the gun! |
4879 |
if ( $time > time() ) { |
|
16 | 4880 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // Clear anything else in the system. |
0 | 4881 |
wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); |
4882 |
return; |
|
4883 |
} |
|
4884 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4885 |
// wp_publish_post() returns no meaningful value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4886 |
wp_publish_post( $post_id ); |
0 | 4887 |
} |
4888 |
||
4889 |
/** |
|
18 | 4890 |
* Uses wp_checkdate to return a valid Gregorian-calendar value for post_date. |
4891 |
* If post_date is not provided, this first checks post_date_gmt if provided, |
|
4892 |
* then falls back to use the current time. |
|
4893 |
* |
|
4894 |
* For back-compat purposes in wp_insert_post, an empty post_date and an invalid |
|
4895 |
* post_date_gmt will continue to return '1970-01-01 00:00:00' rather than false. |
|
4896 |
* |
|
4897 |
* @since 5.7.0 |
|
4898 |
* |
|
4899 |
* @param string $post_date The date in mysql format. |
|
4900 |
* @param string $post_date_gmt The GMT date in mysql format. |
|
4901 |
* @return string|false A valid Gregorian-calendar date string, or false on failure. |
|
4902 |
*/ |
|
4903 |
function wp_resolve_post_date( $post_date = '', $post_date_gmt = '' ) { |
|
4904 |
// If the date is empty, set the date to now. |
|
4905 |
if ( empty( $post_date ) || '0000-00-00 00:00:00' === $post_date ) { |
|
4906 |
if ( empty( $post_date_gmt ) || '0000-00-00 00:00:00' === $post_date_gmt ) { |
|
4907 |
$post_date = current_time( 'mysql' ); |
|
4908 |
} else { |
|
4909 |
$post_date = get_date_from_gmt( $post_date_gmt ); |
|
4910 |
} |
|
4911 |
} |
|
4912 |
||
4913 |
// Validate the date. |
|
4914 |
$month = substr( $post_date, 5, 2 ); |
|
4915 |
$day = substr( $post_date, 8, 2 ); |
|
4916 |
$year = substr( $post_date, 0, 4 ); |
|
4917 |
||
4918 |
$valid_date = wp_checkdate( $month, $day, $year, $post_date ); |
|
4919 |
||
4920 |
if ( ! $valid_date ) { |
|
4921 |
return false; |
|
4922 |
} |
|
4923 |
return $post_date; |
|
4924 |
} |
|
4925 |
||
4926 |
/** |
|
0 | 4927 |
* Computes a unique slug for the post, when given the desired slug and some post details. |
4928 |
* |
|
4929 |
* @since 2.8.0 |
|
4930 |
* |
|
16 | 4931 |
* @global wpdb $wpdb WordPress database abstraction object. |
4932 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
|
5 | 4933 |
* |
4934 |
* @param string $slug The desired slug (post_name). |
|
4935 |
* @param int $post_ID Post ID. |
|
4936 |
* @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
|
4937 |
* @param string $post_type Post type. |
|
4938 |
* @param int $post_parent Post parent ID. |
|
4939 |
* @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
|
0 | 4940 |
*/ |
4941 |
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
|
16 | 4942 |
if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) |
4943 |
|| ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type |
|
4944 |
) { |
|
0 | 4945 |
return $slug; |
9 | 4946 |
} |
4947 |
||
4948 |
/** |
|
4949 |
* Filters the post slug before it is generated to be unique. |
|
4950 |
* |
|
4951 |
* Returning a non-null value will short-circuit the |
|
4952 |
* unique slug generation, returning the passed value instead. |
|
4953 |
* |
|
4954 |
* @since 5.1.0 |
|
4955 |
* |
|
16 | 4956 |
* @param string|null $override_slug Short-circuit return value. |
4957 |
* @param string $slug The desired slug (post_name). |
|
4958 |
* @param int $post_ID Post ID. |
|
4959 |
* @param string $post_status The post status. |
|
4960 |
* @param string $post_type Post type. |
|
4961 |
* @param int $post_parent Post parent ID. |
|
9 | 4962 |
*/ |
4963 |
$override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent ); |
|
4964 |
if ( null !== $override_slug ) { |
|
4965 |
return $override_slug; |
|
4966 |
} |
|
0 | 4967 |
|
4968 |
global $wpdb, $wp_rewrite; |
|
4969 |
||
4970 |
$original_slug = $slug; |
|
4971 |
||
4972 |
$feeds = $wp_rewrite->feeds; |
|
9 | 4973 |
if ( ! is_array( $feeds ) ) { |
0 | 4974 |
$feeds = array(); |
9 | 4975 |
} |
0 | 4976 |
|
16 | 4977 |
if ( 'attachment' === $post_type ) { |
0 | 4978 |
// Attachment slugs must be unique across all types. |
9 | 4979 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
0 | 4980 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); |
4981 |
||
5 | 4982 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4983 |
* Filters whether the post slug would make a bad attachment slug. |
5 | 4984 |
* |
4985 |
* @since 3.1.0 |
|
4986 |
* |
|
4987 |
* @param bool $bad_slug Whether the slug would be bad as an attachment slug. |
|
4988 |
* @param string $slug The post slug. |
|
4989 |
*/ |
|
16 | 4990 |
$is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ); |
4991 |
||
4992 |
if ( $post_name_check |
|
4993 |
|| in_array( $slug, $feeds, true ) || 'embed' === $slug |
|
4994 |
|| $is_bad_attachment_slug |
|
4995 |
) { |
|
0 | 4996 |
$suffix = 2; |
4997 |
do { |
|
9 | 4998 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
0 | 4999 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); |
5000 |
$suffix++; |
|
5001 |
} while ( $post_name_check ); |
|
5002 |
$slug = $alt_post_name; |
|
5003 |
} |
|
5 | 5004 |
} elseif ( is_post_type_hierarchical( $post_type ) ) { |
16 | 5005 |
if ( 'nav_menu_item' === $post_type ) { |
0 | 5006 |
return $slug; |
9 | 5007 |
} |
5 | 5008 |
|
5009 |
/* |
|
5010 |
* Page slugs must be unique within their own trees. Pages are in a separate |
|
5011 |
* namespace than posts so page slugs are allowed to overlap post slugs. |
|
5012 |
*/ |
|
9 | 5013 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; |
5 | 5014 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); |
5015 |
||
5016 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5017 |
* Filters whether the post slug would make a bad hierarchical post slug. |
5 | 5018 |
* |
5019 |
* @since 3.1.0 |
|
5020 |
* |
|
5021 |
* @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. |
|
5022 |
* @param string $slug The post slug. |
|
5023 |
* @param string $post_type Post type. |
|
5024 |
* @param int $post_parent Post parent ID. |
|
5025 |
*/ |
|
16 | 5026 |
$is_bad_hierarchical_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ); |
5027 |
||
5028 |
if ( $post_name_check |
|
5029 |
|| in_array( $slug, $feeds, true ) || 'embed' === $slug |
|
5030 |
|| preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) |
|
5031 |
|| $is_bad_hierarchical_slug |
|
5032 |
) { |
|
0 | 5033 |
$suffix = 2; |
5034 |
do { |
|
9 | 5035 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5 | 5036 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); |
0 | 5037 |
$suffix++; |
5038 |
} while ( $post_name_check ); |
|
5039 |
$slug = $alt_post_name; |
|
5040 |
} |
|
5041 |
} else { |
|
5042 |
// Post slugs must be unique across all posts. |
|
9 | 5043 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
0 | 5044 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
5045 |
||
16 | 5046 |
$post = get_post( $post_ID ); |
5047 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5048 |
// Prevent new post slugs that could result in URLs that conflict with date archives. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5049 |
$conflicts_with_date_archive = false; |
16 | 5050 |
if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { |
18 | 5051 |
$slug_num = (int) $slug; |
16 | 5052 |
|
5053 |
if ( $slug_num ) { |
|
5054 |
$permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); |
|
5055 |
$postname_index = array_search( '%postname%', $permastructs, true ); |
|
5056 |
||
5057 |
/* |
|
5058 |
* Potential date clashes are as follows: |
|
5059 |
* |
|
5060 |
* - Any integer in the first permastruct position could be a year. |
|
5061 |
* - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. |
|
5062 |
* - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. |
|
5063 |
*/ |
|
5064 |
if ( 0 === $postname_index || |
|
5065 |
( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || |
|
5066 |
( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) |
|
5067 |
) { |
|
5068 |
$conflicts_with_date_archive = true; |
|
5069 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5070 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5071 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5072 |
|
5 | 5073 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5074 |
* Filters whether the post slug would be bad as a flat slug. |
5 | 5075 |
* |
5076 |
* @since 3.1.0 |
|
5077 |
* |
|
5078 |
* @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
5079 |
* @param string $slug The post slug. |
|
5080 |
* @param string $post_type Post type. |
|
5081 |
*/ |
|
16 | 5082 |
$is_bad_flat_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ); |
5083 |
||
5084 |
if ( $post_name_check |
|
5085 |
|| in_array( $slug, $feeds, true ) || 'embed' === $slug |
|
5086 |
|| $conflicts_with_date_archive |
|
5087 |
|| $is_bad_flat_slug |
|
5088 |
) { |
|
0 | 5089 |
$suffix = 2; |
5090 |
do { |
|
9 | 5091 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
0 | 5092 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
5093 |
$suffix++; |
|
5094 |
} while ( $post_name_check ); |
|
5095 |
$slug = $alt_post_name; |
|
5096 |
} |
|
5097 |
} |
|
5098 |
||
5 | 5099 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5100 |
* Filters the unique post slug. |
5 | 5101 |
* |
5102 |
* @since 3.3.0 |
|
5103 |
* |
|
5104 |
* @param string $slug The post slug. |
|
5105 |
* @param int $post_ID Post ID. |
|
5106 |
* @param string $post_status The post status. |
|
5107 |
* @param string $post_type Post type. |
|
5108 |
* @param int $post_parent Post parent ID |
|
5109 |
* @param string $original_slug The original post slug. |
|
5110 |
*/ |
|
0 | 5111 |
return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); |
5112 |
} |
|
5113 |
||
5114 |
/** |
|
5 | 5115 |
* Truncate a post slug. |
0 | 5116 |
* |
5117 |
* @since 3.6.0 |
|
5118 |
* @access private |
|
5 | 5119 |
* |
5120 |
* @see utf8_uri_encode() |
|
5121 |
* |
|
5122 |
* @param string $slug The slug to truncate. |
|
5123 |
* @param int $length Optional. Max length of the slug. Default 200 (characters). |
|
0 | 5124 |
* @return string The truncated slug. |
5125 |
*/ |
|
5126 |
function _truncate_post_slug( $slug, $length = 200 ) { |
|
5127 |
if ( strlen( $slug ) > $length ) { |
|
5128 |
$decoded_slug = urldecode( $slug ); |
|
9 | 5129 |
if ( $decoded_slug === $slug ) { |
0 | 5130 |
$slug = substr( $slug, 0, $length ); |
9 | 5131 |
} else { |
19 | 5132 |
$slug = utf8_uri_encode( $decoded_slug, $length, true ); |
9 | 5133 |
} |
0 | 5134 |
} |
5135 |
||
5136 |
return rtrim( $slug, '-' ); |
|
5137 |
} |
|
5138 |
||
5139 |
/** |
|
5 | 5140 |
* Add tags to a post. |
5141 |
* |
|
5142 |
* @see wp_set_post_tags() |
|
5143 |
* |
|
0 | 5144 |
* @since 2.3.0 |
5145 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5146 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5147 |
* @param string|array $tags Optional. An array of tags to set for the post, or a string of tags |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5148 |
* separated by commas. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5149 |
* @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. |
0 | 5150 |
*/ |
5 | 5151 |
function wp_add_post_tags( $post_id = 0, $tags = '' ) { |
9 | 5152 |
return wp_set_post_tags( $post_id, $tags, true ); |
0 | 5153 |
} |
5154 |
||
5155 |
/** |
|
5156 |
* Set the tags for a post. |
|
5157 |
* |
|
5158 |
* @since 2.3.0 |
|
5 | 5159 |
* |
5160 |
* @see wp_set_object_terms() |
|
5161 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5162 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5163 |
* @param string|array $tags Optional. An array of tags to set for the post, or a string of tags |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5164 |
* separated by commas. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5165 |
* @param bool $append Optional. If true, don't delete existing tags, just add on. If false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5166 |
* replace the tags with the new tags. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5167 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
0 | 5168 |
*/ |
5169 |
function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
|
9 | 5170 |
return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); |
0 | 5171 |
} |
5172 |
||
5173 |
/** |
|
5174 |
* Set the terms for a post. |
|
5175 |
* |
|
5176 |
* @since 2.8.0 |
|
5 | 5177 |
* |
5178 |
* @see wp_set_object_terms() |
|
5179 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5180 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5181 |
* @param string|array $tags Optional. An array of terms to set for the post, or a string of terms |
9 | 5182 |
* separated by commas. Hierarchical taxonomies must always pass IDs rather |
5183 |
* than names so that children with the same names but different parents |
|
5184 |
* aren't confused. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5185 |
* @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5186 |
* @param bool $append Optional. If true, don't delete existing terms, just add on. If false, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5187 |
* replace the terms with the new terms. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5188 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
0 | 5189 |
*/ |
5190 |
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { |
|
5191 |
$post_id = (int) $post_id; |
|
5192 |
||
9 | 5193 |
if ( ! $post_id ) { |
0 | 5194 |
return false; |
9 | 5195 |
} |
5196 |
||
5197 |
if ( empty( $tags ) ) { |
|
0 | 5198 |
$tags = array(); |
9 | 5199 |
} |
0 | 5200 |
|
5201 |
if ( ! is_array( $tags ) ) { |
|
5202 |
$comma = _x( ',', 'tag delimiter' ); |
|
9 | 5203 |
if ( ',' !== $comma ) { |
0 | 5204 |
$tags = str_replace( $comma, ',', $tags ); |
9 | 5205 |
} |
0 | 5206 |
$tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) ); |
5207 |
} |
|
5208 |
||
5 | 5209 |
/* |
5210 |
* Hierarchical taxonomies must always pass IDs rather than names so that |
|
5211 |
* children with the same names but different parents aren't confused. |
|
5212 |
*/ |
|
0 | 5213 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
5214 |
$tags = array_unique( array_map( 'intval', $tags ) ); |
|
5215 |
} |
|
5216 |
||
5217 |
return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); |
|
5218 |
} |
|
5219 |
||
5220 |
/** |
|
5221 |
* Set categories for a post. |
|
5222 |
* |
|
16 | 5223 |
* If no categories are provided, the default category is used. |
0 | 5224 |
* |
5225 |
* @since 2.1.0 |
|
5226 |
* |
|
5 | 5227 |
* @param int $post_ID Optional. The Post ID. Does not default to the ID |
5228 |
* of the global $post. Default 0. |
|
18 | 5229 |
* @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category. |
5 | 5230 |
* Default empty array. |
9 | 5231 |
* @param bool $append If true, don't delete existing categories, just add on. |
5232 |
* If false, replace the categories with the new categories. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5233 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. |
0 | 5234 |
*/ |
5235 |
function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { |
|
9 | 5236 |
$post_ID = (int) $post_ID; |
5237 |
$post_type = get_post_type( $post_ID ); |
|
0 | 5238 |
$post_status = get_post_status( $post_ID ); |
16 | 5239 |
|
5240 |
// If $post_categories isn't already an array, make it one. |
|
0 | 5241 |
$post_categories = (array) $post_categories; |
16 | 5242 |
|
0 | 5243 |
if ( empty( $post_categories ) ) { |
16 | 5244 |
/** |
5245 |
* Filters post types (in addition to 'post') that require a default category. |
|
5246 |
* |
|
5247 |
* @since 5.5.0 |
|
5248 |
* |
|
5249 |
* @param string[] $post_types An array of post type names. Default empty array. |
|
5250 |
*/ |
|
5251 |
$default_category_post_types = apply_filters( 'default_category_post_types', array() ); |
|
5252 |
||
5253 |
// Regular posts always require a default category. |
|
5254 |
$default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) ); |
|
5255 |
||
5256 |
if ( in_array( $post_type, $default_category_post_types, true ) |
|
5257 |
&& is_object_in_taxonomy( $post_type, 'category' ) |
|
5258 |
&& 'auto-draft' !== $post_status |
|
5259 |
) { |
|
9 | 5260 |
$post_categories = array( get_option( 'default_category' ) ); |
5261 |
$append = false; |
|
0 | 5262 |
} else { |
5263 |
$post_categories = array(); |
|
5264 |
} |
|
16 | 5265 |
} elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) { |
0 | 5266 |
return true; |
5267 |
} |
|
5268 |
||
5269 |
return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); |
|
5270 |
} |
|
5271 |
||
5272 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5273 |
* Fires actions related to the transitioning of a post's status. |
0 | 5274 |
* |
5 | 5275 |
* When a post is saved, the post status is "transitioned" from one status to another, |
5276 |
* though this does not always mean the status has actually changed before and after |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5277 |
* the save. This function fires a number of action hooks related to that transition: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5278 |
* the generic {@see 'transition_post_status'} action, as well as the dynamic hooks |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5279 |
* {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5280 |
* that the function does not transition the post object in the database. |
5 | 5281 |
* |
5282 |
* For instance: When publishing a post for the first time, the post status may transition |
|
5283 |
* from 'draft' – or some other status – to 'publish'. However, if a post is already |
|
5284 |
* published and is simply being updated, the "old" and "new" statuses may both be 'publish' |
|
5285 |
* before and after the transition. |
|
0 | 5286 |
* |
5287 |
* @since 2.3.0 |
|
5 | 5288 |
* |
5289 |
* @param string $new_status Transition to this post status. |
|
5290 |
* @param string $old_status Previous post status. |
|
5291 |
* @param WP_Post $post Post data. |
|
0 | 5292 |
*/ |
5 | 5293 |
function wp_transition_post_status( $new_status, $old_status, $post ) { |
5294 |
/** |
|
5295 |
* Fires when a post is transitioned from one status to another. |
|
5296 |
* |
|
5297 |
* @since 2.3.0 |
|
5298 |
* |
|
5299 |
* @param string $new_status New post status. |
|
5300 |
* @param string $old_status Old post status. |
|
5301 |
* @param WP_Post $post Post object. |
|
5302 |
*/ |
|
5303 |
do_action( 'transition_post_status', $new_status, $old_status, $post ); |
|
5304 |
||
5305 |
/** |
|
5306 |
* Fires when a post is transitioned from one status to another. |
|
5307 |
* |
|
18 | 5308 |
* The dynamic portions of the hook name, `$new_status` and `$old_status`, |
5 | 5309 |
* refer to the old and new post statuses, respectively. |
5310 |
* |
|
19 | 5311 |
* Possible hook names include: |
5312 |
* |
|
5313 |
* - `draft_to_publish` |
|
5314 |
* - `publish_to_trash` |
|
5315 |
* - `pending_to_draft` |
|
5316 |
* |
|
5 | 5317 |
* @since 2.3.0 |
5318 |
* |
|
5319 |
* @param WP_Post $post Post object. |
|
5320 |
*/ |
|
5321 |
do_action( "{$old_status}_to_{$new_status}", $post ); |
|
5322 |
||
5323 |
/** |
|
5324 |
* Fires when a post is transitioned from one status to another. |
|
5325 |
* |
|
5326 |
* The dynamic portions of the hook name, `$new_status` and `$post->post_type`, |
|
5327 |
* refer to the new post status and post type, respectively. |
|
5328 |
* |
|
18 | 5329 |
* Possible hook names include: |
5330 |
* |
|
5331 |
* - `draft_post` |
|
5332 |
* - `future_post` |
|
5333 |
* - `pending_post` |
|
5334 |
* - `private_post` |
|
5335 |
* - `publish_post` |
|
5336 |
* - `trash_post` |
|
5337 |
* - `draft_page` |
|
5338 |
* - `future_page` |
|
5339 |
* - `pending_page` |
|
5340 |
* - `private_page` |
|
5341 |
* - `publish_page` |
|
5342 |
* - `trash_page` |
|
5343 |
* - `publish_attachment` |
|
5344 |
* - `trash_attachment` |
|
5345 |
* |
|
5 | 5346 |
* Please note: When this action is hooked using a particular post status (like |
5347 |
* 'publish', as `publish_{$post->post_type}`), it will fire both when a post is |
|
5348 |
* first transitioned to that status from something else, as well as upon |
|
5349 |
* subsequent post updates (old and new status are both the same). |
|
5350 |
* |
|
5351 |
* Therefore, if you are looking to only fire a callback when a post is first |
|
5352 |
* transitioned to a status, use the {@see 'transition_post_status'} hook instead. |
|
5353 |
* |
|
5354 |
* @since 2.3.0 |
|
19 | 5355 |
* @since 5.9.0 Added `$old_status` parameter. |
5356 |
* |
|
5357 |
* @param int $post_id Post ID. |
|
5358 |
* @param WP_Post $post Post object. |
|
5359 |
* @param string $old_status Old post status. |
|
5 | 5360 |
*/ |
19 | 5361 |
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post, $old_status ); |
0 | 5362 |
} |
5363 |
||
18 | 5364 |
/** |
5365 |
* Fires actions after a post, its terms and meta data has been saved. |
|
5366 |
* |
|
5367 |
* @since 5.6.0 |
|
5368 |
* |
|
5369 |
* @param int|WP_Post $post The post ID or object that has been saved. |
|
5370 |
* @param bool $update Whether this is an existing post being updated. |
|
5371 |
* @param null|WP_Post $post_before Null for new posts, the WP_Post object prior |
|
5372 |
* to the update for updated posts. |
|
5373 |
*/ |
|
5374 |
function wp_after_insert_post( $post, $update, $post_before ) { |
|
5375 |
$post = get_post( $post ); |
|
5376 |
if ( ! $post ) { |
|
5377 |
return; |
|
5378 |
} |
|
5379 |
||
5380 |
$post_id = $post->ID; |
|
5381 |
||
5382 |
/** |
|
5383 |
* Fires once a post, its terms and meta data has been saved. |
|
5384 |
* |
|
5385 |
* @since 5.6.0 |
|
5386 |
* |
|
5387 |
* @param int $post_id Post ID. |
|
5388 |
* @param WP_Post $post Post object. |
|
5389 |
* @param bool $update Whether this is an existing post being updated. |
|
5390 |
* @param null|WP_Post $post_before Null for new posts, the WP_Post object prior |
|
5391 |
* to the update for updated posts. |
|
5392 |
*/ |
|
5393 |
do_action( 'wp_after_insert_post', $post_id, $post, $update, $post_before ); |
|
5394 |
} |
|
5395 |
||
0 | 5396 |
// |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5397 |
// Comment, trackback, and pingback functions. |
0 | 5398 |
// |
5399 |
||
5400 |
/** |
|
5 | 5401 |
* Add a URL to those already pinged. |
0 | 5402 |
* |
5403 |
* @since 1.5.0 |
|
9 | 5404 |
* @since 4.7.0 `$post_id` can be a WP_Post object. |
5405 |
* @since 4.7.0 `$uri` can be an array of URIs. |
|
5 | 5406 |
* |
5407 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5408 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5409 |
* @param int|WP_Post $post_id Post object or ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5410 |
* @param string|array $uri Ping URI or array of URIs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5411 |
* @return int|false How many rows were updated. |
0 | 5412 |
*/ |
5 | 5413 |
function add_ping( $post_id, $uri ) { |
0 | 5414 |
global $wpdb; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5415 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5416 |
$post = get_post( $post_id ); |
18 | 5417 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5418 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5419 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5420 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5421 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5422 |
$pung = trim( $post->pinged ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5423 |
$pung = preg_split( '/\s/', $pung ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5424 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5425 |
if ( is_array( $uri ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5426 |
$pung = array_merge( $pung, $uri ); |
9 | 5427 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5428 |
$pung[] = $uri; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5429 |
} |
9 | 5430 |
$new = implode( "\n", $pung ); |
5 | 5431 |
|
5432 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5433 |
* Filters the new ping URL to add for the given post. |
5 | 5434 |
* |
5435 |
* @since 2.0.0 |
|
5436 |
* |
|
5437 |
* @param string $new New ping URL to add. |
|
5438 |
*/ |
|
5439 |
$new = apply_filters( 'add_ping', $new ); |
|
5440 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5441 |
$return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5442 |
clean_post_cache( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5443 |
return $return; |
0 | 5444 |
} |
5445 |
||
5446 |
/** |
|
5447 |
* Retrieve enclosures already enclosed for a post. |
|
5448 |
* |
|
5449 |
* @since 1.5.0 |
|
5450 |
* |
|
5451 |
* @param int $post_id Post ID. |
|
16 | 5452 |
* @return string[] Array of enclosures for the given post. |
0 | 5453 |
*/ |
5 | 5454 |
function get_enclosed( $post_id ) { |
0 | 5455 |
$custom_fields = get_post_custom( $post_id ); |
9 | 5456 |
$pung = array(); |
5457 |
if ( ! is_array( $custom_fields ) ) { |
|
0 | 5458 |
return $pung; |
9 | 5459 |
} |
0 | 5460 |
|
5461 |
foreach ( $custom_fields as $key => $val ) { |
|
16 | 5462 |
if ( 'enclosure' !== $key || ! is_array( $val ) ) { |
0 | 5463 |
continue; |
9 | 5464 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5465 |
foreach ( $val as $enc ) { |
0 | 5466 |
$enclosure = explode( "\n", $enc ); |
9 | 5467 |
$pung[] = trim( $enclosure[0] ); |
0 | 5468 |
} |
5469 |
} |
|
5 | 5470 |
|
5471 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5472 |
* Filters the list of enclosures already enclosed for the given post. |
5 | 5473 |
* |
5474 |
* @since 2.0.0 |
|
5475 |
* |
|
16 | 5476 |
* @param string[] $pung Array of enclosures for the given post. |
5477 |
* @param int $post_id Post ID. |
|
5 | 5478 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5479 |
return apply_filters( 'get_enclosed', $pung, $post_id ); |
0 | 5480 |
} |
5481 |
||
5482 |
/** |
|
5483 |
* Retrieve URLs already pinged for a post. |
|
5484 |
* |
|
5485 |
* @since 1.5.0 |
|
5 | 5486 |
* |
9 | 5487 |
* @since 4.7.0 `$post_id` can be a WP_Post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5488 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5489 |
* @param int|WP_Post $post_id Post ID or object. |
18 | 5490 |
* @return string[]|false Array of URLs already pinged for the given post, false if the post is not found. |
0 | 5491 |
*/ |
5 | 5492 |
function get_pung( $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5493 |
$post = get_post( $post_id ); |
18 | 5494 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5495 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5496 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5497 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5498 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5499 |
$pung = trim( $post->pinged ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5500 |
$pung = preg_split( '/\s/', $pung ); |
5 | 5501 |
|
5502 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5503 |
* Filters the list of already-pinged URLs for the given post. |
5 | 5504 |
* |
5505 |
* @since 2.0.0 |
|
5506 |
* |
|
9 | 5507 |
* @param string[] $pung Array of URLs already pinged for the given post. |
5 | 5508 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5509 |
return apply_filters( 'get_pung', $pung ); |
0 | 5510 |
} |
5511 |
||
5512 |
/** |
|
5513 |
* Retrieve URLs that need to be pinged. |
|
5514 |
* |
|
5515 |
* @since 1.5.0 |
|
9 | 5516 |
* @since 4.7.0 `$post_id` can be a WP_Post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5517 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5518 |
* @param int|WP_Post $post_id Post Object or ID |
16 | 5519 |
* @return string[]|false List of URLs yet to ping. |
0 | 5520 |
*/ |
5 | 5521 |
function get_to_ping( $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5522 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5523 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5524 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5525 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5526 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5527 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5528 |
$to_ping = sanitize_trackback_urls( $post->to_ping ); |
9 | 5529 |
$to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY ); |
5 | 5530 |
|
5531 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5532 |
* Filters the list of URLs yet to ping for the given post. |
5 | 5533 |
* |
5534 |
* @since 2.0.0 |
|
5535 |
* |
|
16 | 5536 |
* @param string[] $to_ping List of URLs yet to ping. |
5 | 5537 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5538 |
return apply_filters( 'get_to_ping', $to_ping ); |
0 | 5539 |
} |
5540 |
||
5541 |
/** |
|
5542 |
* Do trackbacks for a list of URLs. |
|
5543 |
* |
|
5544 |
* @since 1.0.0 |
|
5545 |
* |
|
5 | 5546 |
* @param string $tb_list Comma separated list of URLs. |
5547 |
* @param int $post_id Post ID. |
|
0 | 5548 |
*/ |
5 | 5549 |
function trackback_url_list( $tb_list, $post_id ) { |
0 | 5550 |
if ( ! empty( $tb_list ) ) { |
5 | 5551 |
// Get post data. |
5552 |
$postdata = get_post( $post_id, ARRAY_A ); |
|
5553 |
||
5554 |
// Form an excerpt. |
|
5555 |
$excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); |
|
5556 |
||
5557 |
if ( strlen( $excerpt ) > 255 ) { |
|
5558 |
$excerpt = substr( $excerpt, 0, 252 ) . '…'; |
|
0 | 5559 |
} |
5560 |
||
5 | 5561 |
$trackback_urls = explode( ',', $tb_list ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5562 |
foreach ( (array) $trackback_urls as $tb_url ) { |
5 | 5563 |
$tb_url = trim( $tb_url ); |
5564 |
trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); |
|
0 | 5565 |
} |
5566 |
} |
|
5567 |
} |
|
5568 |
||
5569 |
// |
|
16 | 5570 |
// Page functions. |
0 | 5571 |
// |
5572 |
||
5573 |
/** |
|
5574 |
* Get a list of page IDs. |
|
5575 |
* |
|
5576 |
* @since 2.0.0 |
|
5 | 5577 |
* |
5578 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 5579 |
* |
16 | 5580 |
* @return string[] List of page IDs as strings. |
0 | 5581 |
*/ |
5582 |
function get_all_page_ids() { |
|
5583 |
global $wpdb; |
|
5584 |
||
9 | 5585 |
$page_ids = wp_cache_get( 'all_page_ids', 'posts' ); |
0 | 5586 |
if ( ! is_array( $page_ids ) ) { |
9 | 5587 |
$page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" ); |
5588 |
wp_cache_add( 'all_page_ids', $page_ids, 'posts' ); |
|
0 | 5589 |
} |
5590 |
||
5591 |
return $page_ids; |
|
5592 |
} |
|
5593 |
||
5594 |
/** |
|
5595 |
* Retrieves page data given a page ID or page object. |
|
5596 |
* |
|
5597 |
* Use get_post() instead of get_page(). |
|
5598 |
* |
|
5599 |
* @since 1.5.1 |
|
5 | 5600 |
* @deprecated 3.5.0 Use get_post() |
5601 |
* |
|
16 | 5602 |
* @param int|WP_Post $page Page object or page ID. Passed by reference. |
5603 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
5604 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
5605 |
* respectively. Default OBJECT. |
|
5606 |
* @param string $filter Optional. How the return value should be filtered. Accepts 'raw', |
|
5607 |
* 'edit', 'db', 'display'. Default 'raw'. |
|
5608 |
* @return WP_Post|array|null WP_Post or array on success, null on failure. |
|
0 | 5609 |
*/ |
9 | 5610 |
function get_page( $page, $output = OBJECT, $filter = 'raw' ) { |
0 | 5611 |
return get_post( $page, $output, $filter ); |
5612 |
} |
|
5613 |
||
5614 |
/** |
|
5615 |
* Retrieves a page given its path. |
|
5616 |
* |
|
5617 |
* @since 2.1.0 |
|
5 | 5618 |
* |
5619 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5620 |
* |
|
5621 |
* @param string $page_path Page path. |
|
16 | 5622 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
5623 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
5624 |
* respectively. Default OBJECT. |
|
5 | 5625 |
* @param string|array $post_type Optional. Post type or array of post types. Default 'page'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5626 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 5627 |
*/ |
5 | 5628 |
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { |
0 | 5629 |
global $wpdb; |
5630 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5631 |
$last_changed = wp_cache_get_last_changed( 'posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5632 |
|
9 | 5633 |
$hash = md5( $page_path . serialize( $post_type ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5634 |
$cache_key = "get_page_by_path:$hash:$last_changed"; |
9 | 5635 |
$cached = wp_cache_get( $cache_key, 'posts' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5636 |
if ( false !== $cached ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5637 |
// Special case: '0' is a bad `$page_path`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5638 |
if ( '0' === $cached || 0 === $cached ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5639 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5640 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5641 |
return get_post( $cached, $output ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5642 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5643 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5644 |
|
9 | 5645 |
$page_path = rawurlencode( urldecode( $page_path ) ); |
5646 |
$page_path = str_replace( '%2F', '/', $page_path ); |
|
5647 |
$page_path = str_replace( '%20', ' ', $page_path ); |
|
5648 |
$parts = explode( '/', trim( $page_path, '/' ) ); |
|
5649 |
$parts = array_map( 'sanitize_title_for_query', $parts ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5650 |
$escaped_parts = esc_sql( $parts ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5651 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5652 |
$in_string = "'" . implode( "','", $escaped_parts ) . "'"; |
5 | 5653 |
|
5654 |
if ( is_array( $post_type ) ) { |
|
5655 |
$post_types = $post_type; |
|
5656 |
} else { |
|
5657 |
$post_types = array( $post_type, 'attachment' ); |
|
5658 |
} |
|
5659 |
||
9 | 5660 |
$post_types = esc_sql( $post_types ); |
5 | 5661 |
$post_type_in_string = "'" . implode( "','", $post_types ) . "'"; |
9 | 5662 |
$sql = " |
5 | 5663 |
SELECT ID, post_name, post_parent, post_type |
5664 |
FROM $wpdb->posts |
|
5665 |
WHERE post_name IN ($in_string) |
|
5666 |
AND post_type IN ($post_type_in_string) |
|
5667 |
"; |
|
5668 |
||
5669 |
$pages = $wpdb->get_results( $sql, OBJECT_K ); |
|
0 | 5670 |
|
5671 |
$revparts = array_reverse( $parts ); |
|
5672 |
||
5673 |
$foundid = 0; |
|
5674 |
foreach ( (array) $pages as $page ) { |
|
5675 |
if ( $page->post_name == $revparts[0] ) { |
|
5676 |
$count = 0; |
|
9 | 5677 |
$p = $page; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5678 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5679 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5680 |
* Loop through the given path parts from right to left, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5681 |
* ensuring each matches the post ancestry. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5682 |
*/ |
16 | 5683 |
while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) { |
0 | 5684 |
$count++; |
5685 |
$parent = $pages[ $p->post_parent ]; |
|
9 | 5686 |
if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) { |
0 | 5687 |
break; |
9 | 5688 |
} |
0 | 5689 |
$p = $parent; |
5690 |
} |
|
5691 |
||
16 | 5692 |
if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) { |
0 | 5693 |
$foundid = $page->ID; |
9 | 5694 |
if ( $page->post_type == $post_type ) { |
0 | 5695 |
break; |
9 | 5696 |
} |
0 | 5697 |
} |
5698 |
} |
|
5699 |
} |
|
5700 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5701 |
// We cache misses as well as hits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5702 |
wp_cache_set( $cache_key, $foundid, 'posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5703 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5704 |
if ( $foundid ) { |
0 | 5705 |
return get_post( $foundid, $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5706 |
} |
19 | 5707 |
|
5708 |
return null; |
|
0 | 5709 |
} |
5710 |
||
5711 |
/** |
|
5712 |
* Retrieve a page given its title. |
|
5713 |
* |
|
16 | 5714 |
* If more than one post uses the same title, the post with the smallest ID will be returned. |
5715 |
* Be careful: in case of more than one post having the same title, it will check the oldest |
|
5716 |
* publication date, not the smallest ID. |
|
5717 |
* |
|
5718 |
* Because this function uses the MySQL '=' comparison, $page_title will usually be matched |
|
5719 |
* as case-insensitive with default collation. |
|
5720 |
* |
|
0 | 5721 |
* @since 2.1.0 |
16 | 5722 |
* @since 3.0.0 The `$post_type` parameter was added. |
5 | 5723 |
* |
5724 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5725 |
* |
|
16 | 5726 |
* @param string $page_title Page title. |
5727 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
5728 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
5729 |
* respectively. Default OBJECT. |
|
5 | 5730 |
* @param string|array $post_type Optional. Post type or array of post types. Default 'page'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5731 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 5732 |
*/ |
5 | 5733 |
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { |
0 | 5734 |
global $wpdb; |
5 | 5735 |
|
5736 |
if ( is_array( $post_type ) ) { |
|
9 | 5737 |
$post_type = esc_sql( $post_type ); |
5 | 5738 |
$post_type_in_string = "'" . implode( "','", $post_type ) . "'"; |
9 | 5739 |
$sql = $wpdb->prepare( |
5740 |
" |
|
5 | 5741 |
SELECT ID |
5742 |
FROM $wpdb->posts |
|
5743 |
WHERE post_title = %s |
|
5744 |
AND post_type IN ($post_type_in_string) |
|
9 | 5745 |
", |
5746 |
$page_title |
|
5747 |
); |
|
5 | 5748 |
} else { |
9 | 5749 |
$sql = $wpdb->prepare( |
5750 |
" |
|
5 | 5751 |
SELECT ID |
5752 |
FROM $wpdb->posts |
|
5753 |
WHERE post_title = %s |
|
5754 |
AND post_type = %s |
|
9 | 5755 |
", |
5756 |
$page_title, |
|
5757 |
$post_type |
|
5758 |
); |
|
5 | 5759 |
} |
5760 |
||
5761 |
$page = $wpdb->get_var( $sql ); |
|
5762 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5763 |
if ( $page ) { |
0 | 5764 |
return get_post( $page, $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5765 |
} |
19 | 5766 |
|
5767 |
return null; |
|
0 | 5768 |
} |
5769 |
||
5770 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5771 |
* Identify descendants of a given page ID in a list of page objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5772 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5773 |
* Descendants are identified from the `$pages` array passed to the function. No database queries are performed. |
0 | 5774 |
* |
5775 |
* @since 1.5.1 |
|
5776 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5777 |
* @param int $page_id Page ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5778 |
* @param array $pages List of page objects from which descendants should be identified. |
5 | 5779 |
* @return array List of page children. |
0 | 5780 |
*/ |
5 | 5781 |
function get_page_children( $page_id, $pages ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5782 |
// Build a hash of ID -> children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5783 |
$children = array(); |
0 | 5784 |
foreach ( (array) $pages as $page ) { |
18 | 5785 |
$children[ (int) $page->post_parent ][] = $page; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5786 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5787 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5788 |
$page_list = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5789 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5790 |
// Start the search by looking at immediate children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5791 |
if ( isset( $children[ $page_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5792 |
// Always start at the end of the stack in order to preserve original `$pages` order. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5793 |
$to_look = array_reverse( $children[ $page_id ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5795 |
while ( $to_look ) { |
9 | 5796 |
$p = array_pop( $to_look ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5797 |
$page_list[] = $p; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5798 |
if ( isset( $children[ $p->ID ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5799 |
foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5800 |
// Append to the `$to_look` stack to descend the tree. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5801 |
$to_look[] = $child; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5802 |
} |
5 | 5803 |
} |
0 | 5804 |
} |
5805 |
} |
|
5 | 5806 |
|
0 | 5807 |
return $page_list; |
5808 |
} |
|
5809 |
||
5810 |
/** |
|
5811 |
* Order the pages with children under parents in a flat list. |
|
5812 |
* |
|
5813 |
* It uses auxiliary structure to hold parent-children relationships and |
|
5814 |
* runs in O(N) complexity |
|
5815 |
* |
|
5816 |
* @since 2.0.0 |
|
5817 |
* |
|
16 | 5818 |
* @param WP_Post[] $pages Posts array (passed by reference). |
5819 |
* @param int $page_id Optional. Parent page ID. Default 0. |
|
5820 |
* @return string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents. |
|
0 | 5821 |
*/ |
5822 |
function get_page_hierarchy( &$pages, $page_id = 0 ) { |
|
5823 |
if ( empty( $pages ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5824 |
return array(); |
0 | 5825 |
} |
5826 |
||
5827 |
$children = array(); |
|
5828 |
foreach ( (array) $pages as $p ) { |
|
18 | 5829 |
$parent_id = (int) $p->post_parent; |
0 | 5830 |
$children[ $parent_id ][] = $p; |
5831 |
} |
|
5832 |
||
5833 |
$result = array(); |
|
5834 |
_page_traverse_name( $page_id, $children, $result ); |
|
5835 |
||
5836 |
return $result; |
|
5837 |
} |
|
5838 |
||
5839 |
/** |
|
5 | 5840 |
* Traverse and return all the nested children post names of a root page. |
5841 |
* |
|
0 | 5842 |
* $children contains parent-children relations |
5843 |
* |
|
5844 |
* @since 2.9.0 |
|
9 | 5845 |
* @access private |
5 | 5846 |
* |
5847 |
* @see _page_traverse_name() |
|
5848 |
* |
|
16 | 5849 |
* @param int $page_id Page ID. |
5850 |
* @param array $children Parent-children relations (passed by reference). |
|
5851 |
* @param string[] $result Array of page names keyed by ID (passed by reference). |
|
0 | 5852 |
*/ |
9 | 5853 |
function _page_traverse_name( $page_id, &$children, &$result ) { |
5854 |
if ( isset( $children[ $page_id ] ) ) { |
|
5855 |
foreach ( (array) $children[ $page_id ] as $child ) { |
|
0 | 5856 |
$result[ $child->ID ] = $child->post_name; |
5857 |
_page_traverse_name( $child->ID, $children, $result ); |
|
5858 |
} |
|
5859 |
} |
|
5860 |
} |
|
5861 |
||
5862 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5863 |
* Build the URI path for a page. |
0 | 5864 |
* |
5865 |
* Sub pages will be in the "directory" under the parent page post name. |
|
5866 |
* |
|
5867 |
* @since 1.5.0 |
|
16 | 5868 |
* @since 4.6.0 The `$page` parameter was made optional. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5869 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5870 |
* @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. |
0 | 5871 |
* @return string|false Page URI, false on error. |
5872 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5873 |
function get_page_uri( $page = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5874 |
if ( ! $page instanceof WP_Post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5875 |
$page = get_post( $page ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5876 |
} |
0 | 5877 |
|
9 | 5878 |
if ( ! $page ) { |
0 | 5879 |
return false; |
9 | 5880 |
} |
0 | 5881 |
|
5882 |
$uri = $page->post_name; |
|
5883 |
||
5884 |
foreach ( $page->ancestors as $parent ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5885 |
$parent = get_post( $parent ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5886 |
if ( $parent && $parent->post_name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5887 |
$uri = $parent->post_name . '/' . $uri; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5888 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5889 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5890 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5891 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5892 |
* Filters the URI for a page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5893 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5894 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5895 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5896 |
* @param string $uri Page URI. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5897 |
* @param WP_Post $page Page object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5898 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5899 |
return apply_filters( 'get_page_uri', $uri, $page ); |
0 | 5900 |
} |
5901 |
||
5902 |
/** |
|
18 | 5903 |
* Retrieve an array of pages (or hierarchical post type items). |
0 | 5904 |
* |
5 | 5905 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 5906 |
* |
5907 |
* @since 1.5.0 |
|
5908 |
* |
|
5 | 5909 |
* @param array|string $args { |
5910 |
* Optional. Array or string of arguments to retrieve pages. |
|
5911 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5912 |
* @type int $child_of Page ID to return child and grandchild pages of. Note: The value |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5913 |
* of `$hierarchical` has no bearing on whether `$child_of` returns |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5914 |
* hierarchical results. Default 0, or no restriction. |
5 | 5915 |
* @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. |
5916 |
* @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', |
|
5917 |
* 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', |
|
5918 |
* 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. |
|
5919 |
* 'post_' can be omitted for any values that start with it. |
|
5920 |
* Default 'post_title'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5921 |
* @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5922 |
* `$child_of` also being false, both arguments will be disregarded. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5923 |
* Default true. |
18 | 5924 |
* @type int[] $exclude Array of page IDs to exclude. Default empty array. |
5925 |
* @type int[] $include Array of page IDs to include. Cannot be used with `$child_of`, |
|
5 | 5926 |
* `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. |
5927 |
* Default empty array. |
|
5928 |
* @type string $meta_key Only include pages with this meta key. Default empty. |
|
5929 |
* @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. |
|
5930 |
* Default empty. |
|
5931 |
* @type string $authors A comma-separated list of author IDs. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5932 |
* @type int $parent Page ID to return direct children of. Default -1, or no restriction. |
18 | 5933 |
* @type string|int[] $exclude_tree Comma-separated string or array of page IDs to exclude. |
5 | 5934 |
* Default empty array. |
5935 |
* @type int $number The number of pages to return. Default 0, or all pages. |
|
5936 |
* @type int $offset The number of pages to skip before returning. Requires `$number`. |
|
5937 |
* Default 0. |
|
5938 |
* @type string $post_type The post type to query. Default 'page'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5939 |
* @type string|array $post_status A comma-separated list or array of post statuses to include. |
5 | 5940 |
* Default 'publish'. |
0 | 5941 |
* } |
18 | 5942 |
* @return WP_Post[]|int[]|false Array of pages (or hierarchical post type items). Boolean false if the |
5943 |
* specified post type is not hierarchical or the specified status is not |
|
5944 |
* supported by the post type. |
|
0 | 5945 |
*/ |
5946 |
function get_pages( $args = array() ) { |
|
5947 |
global $wpdb; |
|
5948 |
||
5949 |
$defaults = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5950 |
'child_of' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5951 |
'sort_order' => 'ASC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5952 |
'sort_column' => 'post_title', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5953 |
'hierarchical' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5954 |
'exclude' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5955 |
'include' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5956 |
'meta_key' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5957 |
'meta_value' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5958 |
'authors' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5959 |
'parent' => -1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5960 |
'exclude_tree' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5961 |
'number' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5962 |
'offset' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5963 |
'post_type' => 'page', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5964 |
'post_status' => 'publish', |
0 | 5965 |
); |
5966 |
||
16 | 5967 |
$parsed_args = wp_parse_args( $args, $defaults ); |
5968 |
||
5969 |
$number = (int) $parsed_args['number']; |
|
5970 |
$offset = (int) $parsed_args['offset']; |
|
5971 |
$child_of = (int) $parsed_args['child_of']; |
|
5972 |
$hierarchical = $parsed_args['hierarchical']; |
|
5973 |
$exclude = $parsed_args['exclude']; |
|
5974 |
$meta_key = $parsed_args['meta_key']; |
|
5975 |
$meta_value = $parsed_args['meta_value']; |
|
5976 |
$parent = $parsed_args['parent']; |
|
5977 |
$post_status = $parsed_args['post_status']; |
|
5 | 5978 |
|
5979 |
// Make sure the post type is hierarchical. |
|
0 | 5980 |
$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); |
16 | 5981 |
if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) { |
5 | 5982 |
return false; |
5983 |
} |
|
5984 |
||
5985 |
if ( $parent > 0 && ! $child_of ) { |
|
0 | 5986 |
$hierarchical = false; |
5 | 5987 |
} |
5988 |
||
5989 |
// Make sure we have a valid post status. |
|
5990 |
if ( ! is_array( $post_status ) ) { |
|
0 | 5991 |
$post_status = explode( ',', $post_status ); |
5 | 5992 |
} |
5993 |
if ( array_diff( $post_status, get_post_stati() ) ) { |
|
5994 |
return false; |
|
5995 |
} |
|
5996 |
||
5997 |
// $args can be whatever, only use the args defined in defaults to compute the key. |
|
16 | 5998 |
$key = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5999 |
$last_changed = wp_cache_get_last_changed( 'posts' ); |
0 | 6000 |
|
6001 |
$cache_key = "get_pages:$key:$last_changed"; |
|
9 | 6002 |
$cache = wp_cache_get( $cache_key, 'posts' ); |
6003 |
if ( false !== $cache ) { |
|
18 | 6004 |
_prime_post_caches( $cache, false, false ); |
6005 |
||
5 | 6006 |
// Convert to WP_Post instances. |
0 | 6007 |
$pages = array_map( 'get_post', $cache ); |
5 | 6008 |
/** This filter is documented in wp-includes/post.php */ |
16 | 6009 |
$pages = apply_filters( 'get_pages', $pages, $parsed_args ); |
18 | 6010 |
|
0 | 6011 |
return $pages; |
6012 |
} |
|
6013 |
||
6014 |
$inclusions = ''; |
|
16 | 6015 |
if ( ! empty( $parsed_args['include'] ) ) { |
6016 |
$child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. |
|
9 | 6017 |
$parent = -1; |
6018 |
$exclude = ''; |
|
6019 |
$meta_key = ''; |
|
6020 |
$meta_value = ''; |
|
0 | 6021 |
$hierarchical = false; |
16 | 6022 |
$incpages = wp_parse_id_list( $parsed_args['include'] ); |
5 | 6023 |
if ( ! empty( $incpages ) ) { |
9 | 6024 |
$inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; |
5 | 6025 |
} |
0 | 6026 |
} |
6027 |
||
6028 |
$exclusions = ''; |
|
6029 |
if ( ! empty( $exclude ) ) { |
|
6030 |
$expages = wp_parse_id_list( $exclude ); |
|
5 | 6031 |
if ( ! empty( $expages ) ) { |
9 | 6032 |
$exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; |
5 | 6033 |
} |
0 | 6034 |
} |
6035 |
||
6036 |
$author_query = ''; |
|
16 | 6037 |
if ( ! empty( $parsed_args['authors'] ) ) { |
6038 |
$post_authors = wp_parse_list( $parsed_args['authors'] ); |
|
0 | 6039 |
|
6040 |
if ( ! empty( $post_authors ) ) { |
|
6041 |
foreach ( $post_authors as $post_author ) { |
|
16 | 6042 |
// Do we have an author id or an author login? |
18 | 6043 |
if ( 0 == (int) $post_author ) { |
9 | 6044 |
$post_author = get_user_by( 'login', $post_author ); |
5 | 6045 |
if ( empty( $post_author ) ) { |
0 | 6046 |
continue; |
5 | 6047 |
} |
6048 |
if ( empty( $post_author->ID ) ) { |
|
0 | 6049 |
continue; |
5 | 6050 |
} |
0 | 6051 |
$post_author = $post_author->ID; |
6052 |
} |
|
6053 |
||
16 | 6054 |
if ( '' === $author_query ) { |
9 | 6055 |
$author_query = $wpdb->prepare( ' post_author = %d ', $post_author ); |
5 | 6056 |
} else { |
9 | 6057 |
$author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author ); |
5 | 6058 |
} |
0 | 6059 |
} |
16 | 6060 |
if ( '' !== $author_query ) { |
0 | 6061 |
$author_query = " AND ($author_query)"; |
5 | 6062 |
} |
0 | 6063 |
} |
6064 |
} |
|
6065 |
||
9 | 6066 |
$join = ''; |
0 | 6067 |
$where = "$exclusions $inclusions "; |
6068 |
if ( '' !== $meta_key || '' !== $meta_value ) { |
|
6069 |
$join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
|
6070 |
||
16 | 6071 |
// meta_key and meta_value might be slashed. |
9 | 6072 |
$meta_key = wp_unslash( $meta_key ); |
6073 |
$meta_value = wp_unslash( $meta_value ); |
|
5 | 6074 |
if ( '' !== $meta_key ) { |
9 | 6075 |
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key ); |
5 | 6076 |
} |
6077 |
if ( '' !== $meta_value ) { |
|
9 | 6078 |
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value ); |
5 | 6079 |
} |
0 | 6080 |
} |
6081 |
||
6082 |
if ( is_array( $parent ) ) { |
|
6083 |
$post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); |
|
5 | 6084 |
if ( ! empty( $post_parent__in ) ) { |
0 | 6085 |
$where .= " AND post_parent IN ($post_parent__in)"; |
5 | 6086 |
} |
0 | 6087 |
} elseif ( $parent >= 0 ) { |
9 | 6088 |
$where .= $wpdb->prepare( ' AND post_parent = %d ', $parent ); |
0 | 6089 |
} |
6090 |
||
16 | 6091 |
if ( 1 === count( $post_status ) ) { |
6092 |
$where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) ); |
|
0 | 6093 |
} else { |
9 | 6094 |
$post_status = implode( "', '", $post_status ); |
16 | 6095 |
$where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] ); |
0 | 6096 |
} |
6097 |
||
6098 |
$orderby_array = array(); |
|
9 | 6099 |
$allowed_keys = array( |
6100 |
'author', |
|
6101 |
'post_author', |
|
6102 |
'date', |
|
6103 |
'post_date', |
|
6104 |
'title', |
|
6105 |
'post_title', |
|
6106 |
'name', |
|
6107 |
'post_name', |
|
6108 |
'modified', |
|
6109 |
'post_modified', |
|
6110 |
'modified_gmt', |
|
6111 |
'post_modified_gmt', |
|
6112 |
'menu_order', |
|
6113 |
'parent', |
|
6114 |
'post_parent', |
|
6115 |
'ID', |
|
6116 |
'rand', |
|
6117 |
'comment_count', |
|
6118 |
); |
|
5 | 6119 |
|
16 | 6120 |
foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) { |
0 | 6121 |
$orderby = trim( $orderby ); |
16 | 6122 |
if ( ! in_array( $orderby, $allowed_keys, true ) ) { |
0 | 6123 |
continue; |
5 | 6124 |
} |
0 | 6125 |
|
6126 |
switch ( $orderby ) { |
|
6127 |
case 'menu_order': |
|
6128 |
break; |
|
6129 |
case 'ID': |
|
6130 |
$orderby = "$wpdb->posts.ID"; |
|
6131 |
break; |
|
6132 |
case 'rand': |
|
6133 |
$orderby = 'RAND()'; |
|
6134 |
break; |
|
6135 |
case 'comment_count': |
|
6136 |
$orderby = "$wpdb->posts.comment_count"; |
|
6137 |
break; |
|
6138 |
default: |
|
5 | 6139 |
if ( 0 === strpos( $orderby, 'post_' ) ) { |
0 | 6140 |
$orderby = "$wpdb->posts." . $orderby; |
5 | 6141 |
} else { |
0 | 6142 |
$orderby = "$wpdb->posts.post_" . $orderby; |
5 | 6143 |
} |
0 | 6144 |
} |
6145 |
||
6146 |
$orderby_array[] = $orderby; |
|
6147 |
||
6148 |
} |
|
6149 |
$sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; |
|
6150 |
||
16 | 6151 |
$sort_order = strtoupper( $parsed_args['sort_order'] ); |
6152 |
if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) { |
|
0 | 6153 |
$sort_order = 'ASC'; |
5 | 6154 |
} |
0 | 6155 |
|
9 | 6156 |
$query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
0 | 6157 |
$query .= $author_query; |
9 | 6158 |
$query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order; |
0 | 6159 |
|
5 | 6160 |
if ( ! empty( $number ) ) { |
0 | 6161 |
$query .= ' LIMIT ' . $offset . ',' . $number; |
5 | 6162 |
} |
0 | 6163 |
|
9 | 6164 |
$pages = $wpdb->get_results( $query ); |
6165 |
||
6166 |
if ( empty( $pages ) ) { |
|
6167 |
wp_cache_set( $cache_key, array(), 'posts' ); |
|
6168 |
||
5 | 6169 |
/** This filter is documented in wp-includes/post.php */ |
16 | 6170 |
$pages = apply_filters( 'get_pages', array(), $parsed_args ); |
18 | 6171 |
|
0 | 6172 |
return $pages; |
6173 |
} |
|
6174 |
||
5 | 6175 |
// Sanitize before caching so it'll only get done once. |
9 | 6176 |
$num_pages = count( $pages ); |
6177 |
for ( $i = 0; $i < $num_pages; $i++ ) { |
|
6178 |
$pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); |
|
0 | 6179 |
} |
6180 |
||
6181 |
// Update cache. |
|
6182 |
update_post_cache( $pages ); |
|
6183 |
||
5 | 6184 |
if ( $child_of || $hierarchical ) { |
9 | 6185 |
$pages = get_page_children( $child_of, $pages ); |
5 | 6186 |
} |
6187 |
||
16 | 6188 |
if ( ! empty( $parsed_args['exclude_tree'] ) ) { |
6189 |
$exclude = wp_parse_id_list( $parsed_args['exclude_tree'] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6190 |
foreach ( $exclude as $id ) { |
5 | 6191 |
$children = get_page_children( $id, $pages ); |
6192 |
foreach ( $children as $child ) { |
|
6193 |
$exclude[] = $child->ID; |
|
6194 |
} |
|
6195 |
} |
|
6196 |
||
6197 |
$num_pages = count( $pages ); |
|
0 | 6198 |
for ( $i = 0; $i < $num_pages; $i++ ) { |
16 | 6199 |
if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) { |
9 | 6200 |
unset( $pages[ $i ] ); |
5 | 6201 |
} |
0 | 6202 |
} |
6203 |
} |
|
6204 |
||
6205 |
$page_structure = array(); |
|
5 | 6206 |
foreach ( $pages as $page ) { |
0 | 6207 |
$page_structure[] = $page->ID; |
5 | 6208 |
} |
0 | 6209 |
|
6210 |
wp_cache_set( $cache_key, $page_structure, 'posts' ); |
|
6211 |
||
16 | 6212 |
// Convert to WP_Post instances. |
0 | 6213 |
$pages = array_map( 'get_post', $pages ); |
6214 |
||
5 | 6215 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6216 |
* Filters the retrieved list of pages. |
5 | 6217 |
* |
6218 |
* @since 2.1.0 |
|
6219 |
* |
|
16 | 6220 |
* @param WP_Post[] $pages Array of page objects. |
6221 |
* @param array $parsed_args Array of get_pages() arguments. |
|
5 | 6222 |
*/ |
16 | 6223 |
return apply_filters( 'get_pages', $pages, $parsed_args ); |
0 | 6224 |
} |
6225 |
||
6226 |
// |
|
16 | 6227 |
// Attachment functions. |
0 | 6228 |
// |
6229 |
||
6230 |
/** |
|
9 | 6231 |
* Determines whether an attachment URI is local and really an attachment. |
6232 |
* |
|
6233 |
* For more information on this and similar theme functions, check out |
|
6234 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
6235 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 6236 |
* |
6237 |
* @since 2.0.0 |
|
6238 |
* |
|
6239 |
* @param string $url URL to check |
|
6240 |
* @return bool True on success, false on failure. |
|
6241 |
*/ |
|
9 | 6242 |
function is_local_attachment( $url ) { |
6243 |
if ( strpos( $url, home_url() ) === false ) { |
|
0 | 6244 |
return false; |
9 | 6245 |
} |
6246 |
if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) { |
|
0 | 6247 |
return true; |
9 | 6248 |
} |
16 | 6249 |
|
6250 |
$id = url_to_postid( $url ); |
|
6251 |
if ( $id ) { |
|
9 | 6252 |
$post = get_post( $id ); |
16 | 6253 |
if ( 'attachment' === $post->post_type ) { |
0 | 6254 |
return true; |
9 | 6255 |
} |
0 | 6256 |
} |
6257 |
return false; |
|
6258 |
} |
|
6259 |
||
6260 |
/** |
|
6261 |
* Insert an attachment. |
|
6262 |
* |
|
5 | 6263 |
* If you set the 'ID' in the $args parameter, it will mean that you are |
0 | 6264 |
* updating and attempt to update the attachment. You can also set the |
6265 |
* attachment name or title by setting the key 'post_name' or 'post_title'. |
|
6266 |
* |
|
6267 |
* You can set the dates for the attachment manually by setting the 'post_date' |
|
6268 |
* and 'post_date_gmt' keys' values. |
|
6269 |
* |
|
6270 |
* By default, the comments will use the default settings for whether the |
|
6271 |
* comments are allowed. You can close them manually or keep them open by |
|
6272 |
* setting the value for the 'comment_status' key. |
|
6273 |
* |
|
6274 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6275 |
* @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. |
18 | 6276 |
* @since 5.6.0 Added the `$fire_after_hooks` parameter. |
5 | 6277 |
* |
6278 |
* @see wp_insert_post() |
|
6279 |
* |
|
18 | 6280 |
* @param string|array $args Arguments for inserting an attachment. |
6281 |
* @param string|false $file Optional. Filename. |
|
6282 |
* @param int $parent Optional. Parent post ID. |
|
6283 |
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
|
6284 |
* @param bool $fire_after_hooks Optional. Whether to fire the after insert hooks. Default true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6285 |
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. |
0 | 6286 |
*/ |
18 | 6287 |
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false, $fire_after_hooks = true ) { |
5 | 6288 |
$defaults = array( |
6289 |
'file' => $file, |
|
9 | 6290 |
'post_parent' => 0, |
5 | 6291 |
); |
6292 |
||
6293 |
$data = wp_parse_args( $args, $defaults ); |
|
6294 |
||
6295 |
if ( ! empty( $parent ) ) { |
|
6296 |
$data['post_parent'] = $parent; |
|
0 | 6297 |
} |
5 | 6298 |
|
6299 |
$data['post_type'] = 'attachment'; |
|
6300 |
||
18 | 6301 |
return wp_insert_post( $data, $wp_error, $fire_after_hooks ); |
0 | 6302 |
} |
6303 |
||
6304 |
/** |
|
5 | 6305 |
* Trash or delete an attachment. |
0 | 6306 |
* |
6307 |
* When an attachment is permanently deleted, the file will also be removed. |
|
6308 |
* Deletion removes all post meta fields, taxonomy, comments, etc. associated |
|
6309 |
* with the attachment (except the main post). |
|
6310 |
* |
|
16 | 6311 |
* The attachment is moved to the Trash instead of permanently deleted unless Trash |
6312 |
* for media is disabled, item is already in the Trash, or $force_delete is true. |
|
0 | 6313 |
* |
6314 |
* @since 2.0.0 |
|
5 | 6315 |
* |
6316 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
6317 |
* |
|
6318 |
* @param int $post_id Attachment ID. |
|
16 | 6319 |
* @param bool $force_delete Optional. Whether to bypass Trash and force deletion. |
5 | 6320 |
* Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6321 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 6322 |
*/ |
6323 |
function wp_delete_attachment( $post_id, $force_delete = false ) { |
|
6324 |
global $wpdb; |
|
6325 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6326 |
$post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6327 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6328 |
if ( ! $post ) { |
0 | 6329 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6330 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6331 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6332 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6333 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6334 |
if ( 'attachment' !== $post->post_type ) { |
0 | 6335 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6336 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6337 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6338 |
if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) { |
0 | 6339 |
return wp_trash_post( $post_id ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6340 |
} |
0 | 6341 |
|
16 | 6342 |
/** |
6343 |
* Filters whether an attachment deletion should take place. |
|
6344 |
* |
|
6345 |
* @since 5.5.0 |
|
6346 |
* |
|
19 | 6347 |
* @param WP_Post|false|null $delete Whether to go forward with deletion. @TODO description |
6348 |
* @param WP_Post $post Post object. |
|
6349 |
* @param bool $force_delete Whether to bypass the Trash. |
|
16 | 6350 |
*/ |
6351 |
$check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete ); |
|
6352 |
if ( null !== $check ) { |
|
6353 |
return $check; |
|
6354 |
} |
|
6355 |
||
9 | 6356 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
6357 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|
6358 |
||
6359 |
$meta = wp_get_attachment_metadata( $post_id ); |
|
0 | 6360 |
$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
9 | 6361 |
$file = get_attached_file( $post_id ); |
6362 |
||
19 | 6363 |
if ( is_multisite() && is_string( $file ) && ! empty( $file ) ) { |
18 | 6364 |
clean_dirsize_cache( $file ); |
9 | 6365 |
} |
0 | 6366 |
|
5 | 6367 |
/** |
6368 |
* Fires before an attachment is deleted, at the start of wp_delete_attachment(). |
|
6369 |
* |
|
6370 |
* @since 2.0.0 |
|
16 | 6371 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 6372 |
* |
16 | 6373 |
* @param int $post_id Attachment ID. |
6374 |
* @param WP_Post $post Post object. |
|
5 | 6375 |
*/ |
16 | 6376 |
do_action( 'delete_attachment', $post_id, $post ); |
0 | 6377 |
|
9 | 6378 |
wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) ); |
6379 |
wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); |
|
0 | 6380 |
|
5 | 6381 |
// Delete all for any posts. |
6382 |
delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); |
|
0 | 6383 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6384 |
wp_defer_comment_counting( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6385 |
|
19 | 6386 |
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d ORDER BY comment_ID DESC", $post_id ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6387 |
foreach ( $comment_ids as $comment_id ) { |
0 | 6388 |
wp_delete_comment( $comment_id, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6389 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6390 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6391 |
wp_defer_comment_counting( false ); |
0 | 6392 |
|
9 | 6393 |
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) ); |
6394 |
foreach ( $post_meta_ids as $mid ) { |
|
0 | 6395 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 6396 |
} |
0 | 6397 |
|
5 | 6398 |
/** This action is documented in wp-includes/post.php */ |
16 | 6399 |
do_action( 'delete_post', $post_id, $post ); |
5 | 6400 |
$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); |
6401 |
if ( ! $result ) { |
|
6402 |
return false; |
|
6403 |
} |
|
6404 |
/** This action is documented in wp-includes/post.php */ |
|
16 | 6405 |
do_action( 'deleted_post', $post_id, $post ); |
0 | 6406 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6407 |
wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); |
0 | 6408 |
|
6409 |
clean_post_cache( $post ); |
|
6410 |
||
6411 |
return $post; |
|
6412 |
} |
|
6413 |
||
6414 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6415 |
* Deletes all files that belong to the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6416 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6417 |
* @since 4.9.7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6418 |
* |
18 | 6419 |
* @global wpdb $wpdb WordPress database abstraction object. |
6420 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6421 |
* @param int $post_id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6422 |
* @param array $meta The attachment's meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6423 |
* @param array $backup_sizes The meta data for the attachment's backup images. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6424 |
* @param string $file Absolute path to the attachment's file. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6425 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6426 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6427 |
function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6428 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6429 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6430 |
$uploadpath = wp_get_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6431 |
$deleted = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6432 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6433 |
if ( ! empty( $meta['thumb'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6434 |
// Don't delete the thumb if another attachment uses it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6435 |
if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) { |
9 | 6436 |
$thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file ); |
16 | 6437 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6438 |
if ( ! empty( $thumbfile ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6439 |
$thumbfile = path_join( $uploadpath['basedir'], $thumbfile ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6440 |
$thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6441 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6442 |
if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6443 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6444 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6445 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6446 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6447 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6448 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6449 |
// Remove intermediate and backup images if there are any. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6450 |
if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6451 |
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
16 | 6452 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6453 |
foreach ( $meta['sizes'] as $size => $sizeinfo ) { |
9 | 6454 |
$intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file ); |
16 | 6455 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6456 |
if ( ! empty( $intermediate_file ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6457 |
$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6458 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6459 |
if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6460 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6461 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6462 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6463 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6464 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6465 |
|
16 | 6466 |
if ( ! empty( $meta['original_image'] ) ) { |
6467 |
if ( empty( $intermediate_dir ) ) { |
|
6468 |
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
|
6469 |
} |
|
6470 |
||
6471 |
$original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file ); |
|
6472 |
||
6473 |
if ( ! empty( $original_image ) ) { |
|
6474 |
$original_image = path_join( $uploadpath['basedir'], $original_image ); |
|
6475 |
||
6476 |
if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) { |
|
6477 |
$deleted = false; |
|
6478 |
} |
|
6479 |
} |
|
6480 |
} |
|
6481 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6482 |
if ( is_array( $backup_sizes ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6483 |
$del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) ); |
16 | 6484 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6485 |
foreach ( $backup_sizes as $size ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6486 |
$del_file = path_join( dirname( $meta['file'] ), $size['file'] ); |
16 | 6487 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6488 |
if ( ! empty( $del_file ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6489 |
$del_file = path_join( $uploadpath['basedir'], $del_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6490 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6491 |
if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6492 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6493 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6494 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6495 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6496 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6497 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6498 |
if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6499 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6500 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6501 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6502 |
return $deleted; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6503 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6504 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6505 |
/** |
16 | 6506 |
* Retrieves attachment metadata for attachment ID. |
0 | 6507 |
* |
6508 |
* @since 2.1.0 |
|
6509 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6510 |
* @param int $attachment_id Attachment post ID. Defaults to global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6511 |
* @param bool $unfiltered Optional. If true, filters are not run. Default false. |
16 | 6512 |
* @return array|false { |
6513 |
* Attachment metadata. False on failure. |
|
6514 |
* |
|
6515 |
* @type int $width The width of the attachment. |
|
6516 |
* @type int $height The height of the attachment. |
|
6517 |
* @type string $file The file path relative to `wp-content/uploads`. |
|
6518 |
* @type array $sizes Keys are size slugs, each value is an array containing |
|
6519 |
* 'file', 'width', 'height', and 'mime-type'. |
|
6520 |
* @type array $image_meta Image metadata. |
|
19 | 6521 |
* @type int $filesize File size of the attachment. |
16 | 6522 |
* } |
0 | 6523 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6524 |
function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6525 |
$attachment_id = (int) $attachment_id; |
16 | 6526 |
|
18 | 6527 |
if ( ! $attachment_id ) { |
6528 |
$post = get_post(); |
|
6529 |
||
6530 |
if ( ! $post ) { |
|
6531 |
return false; |
|
6532 |
} |
|
6533 |
||
6534 |
$attachment_id = $post->ID; |
|
6535 |
} |
|
6536 |
||
6537 |
$data = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); |
|
6538 |
||
6539 |
if ( ! $data ) { |
|
0 | 6540 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6541 |
} |
0 | 6542 |
|
9 | 6543 |
if ( $unfiltered ) { |
0 | 6544 |
return $data; |
9 | 6545 |
} |
0 | 6546 |
|
5 | 6547 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6548 |
* Filters the attachment meta data. |
5 | 6549 |
* |
6550 |
* @since 2.1.0 |
|
6551 |
* |
|
18 | 6552 |
* @param array $data Array of meta data for the given attachment. |
6553 |
* @param int $attachment_id Attachment post ID. |
|
5 | 6554 |
*/ |
18 | 6555 |
return apply_filters( 'wp_get_attachment_metadata', $data, $attachment_id ); |
0 | 6556 |
} |
6557 |
||
6558 |
/** |
|
16 | 6559 |
* Updates metadata for an attachment. |
0 | 6560 |
* |
6561 |
* @since 2.1.0 |
|
6562 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6563 |
* @param int $attachment_id Attachment post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6564 |
* @param array $data Attachment meta data. |
18 | 6565 |
* @return int|false False if $post is invalid. |
0 | 6566 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6567 |
function wp_update_attachment_metadata( $attachment_id, $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6568 |
$attachment_id = (int) $attachment_id; |
16 | 6569 |
|
6570 |
$post = get_post( $attachment_id ); |
|
18 | 6571 |
|
16 | 6572 |
if ( ! $post ) { |
0 | 6573 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6574 |
} |
0 | 6575 |
|
5 | 6576 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6577 |
* Filters the updated attachment meta data. |
5 | 6578 |
* |
6579 |
* @since 2.1.0 |
|
6580 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6581 |
* @param array $data Array of updated attachment meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6582 |
* @param int $attachment_id Attachment post ID. |
5 | 6583 |
*/ |
16 | 6584 |
$data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); |
6585 |
if ( $data ) { |
|
0 | 6586 |
return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); |
9 | 6587 |
} else { |
0 | 6588 |
return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
9 | 6589 |
} |
0 | 6590 |
} |
6591 |
||
6592 |
/** |
|
6593 |
* Retrieve the URL for an attachment. |
|
6594 |
* |
|
6595 |
* @since 2.1.0 |
|
6596 |
* |
|
19 | 6597 |
* @global string $pagenow The filename of the current screen. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6598 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6599 |
* @param int $attachment_id Optional. Attachment post ID. Defaults to global $post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6600 |
* @return string|false Attachment URL, otherwise false. |
0 | 6601 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6602 |
function wp_get_attachment_url( $attachment_id = 0 ) { |
18 | 6603 |
global $pagenow; |
6604 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6605 |
$attachment_id = (int) $attachment_id; |
16 | 6606 |
|
6607 |
$post = get_post( $attachment_id ); |
|
18 | 6608 |
|
16 | 6609 |
if ( ! $post ) { |
0 | 6610 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6611 |
} |
0 | 6612 |
|
16 | 6613 |
if ( 'attachment' !== $post->post_type ) { |
0 | 6614 |
return false; |
9 | 6615 |
} |
0 | 6616 |
|
6617 |
$url = ''; |
|
5 | 6618 |
// Get attached file. |
16 | 6619 |
$file = get_post_meta( $post->ID, '_wp_attached_file', true ); |
6620 |
if ( $file ) { |
|
5 | 6621 |
// Get upload directory. |
16 | 6622 |
$uploads = wp_get_upload_dir(); |
6623 |
if ( $uploads && false === $uploads['error'] ) { |
|
5 | 6624 |
// Check that the upload base exists in the file location. |
6625 |
if ( 0 === strpos( $file, $uploads['basedir'] ) ) { |
|
6626 |
// Replace file location with url location. |
|
9 | 6627 |
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); |
6628 |
} elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) { |
|
16 | 6629 |
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads). |
9 | 6630 |
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); |
5 | 6631 |
} else { |
6632 |
// It's a newly-uploaded file, therefore $file is relative to the basedir. |
|
6633 |
$url = $uploads['baseurl'] . "/$file"; |
|
6634 |
} |
|
0 | 6635 |
} |
6636 |
} |
|
6637 |
||
5 | 6638 |
/* |
6639 |
* If any of the above options failed, Fallback on the GUID as used pre-2.7, |
|
6640 |
* not recommended to rely upon this. |
|
6641 |
*/ |
|
18 | 6642 |
if ( ! $url ) { |
0 | 6643 |
$url = get_the_guid( $post->ID ); |
5 | 6644 |
} |
6645 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6646 |
// On SSL front end, URLs should be HTTPS. |
18 | 6647 |
if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $pagenow ) { |
5 | 6648 |
$url = set_url_scheme( $url ); |
6649 |
} |
|
6650 |
||
6651 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6652 |
* Filters the attachment URL. |
5 | 6653 |
* |
6654 |
* @since 2.1.0 |
|
6655 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6656 |
* @param string $url URL for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6657 |
* @param int $attachment_id Attachment post ID. |
5 | 6658 |
*/ |
0 | 6659 |
$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
6660 |
||
18 | 6661 |
if ( ! $url ) { |
0 | 6662 |
return false; |
9 | 6663 |
} |
0 | 6664 |
|
6665 |
return $url; |
|
6666 |
} |
|
6667 |
||
6668 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6669 |
* Retrieves the caption for an attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6670 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6671 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6672 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6673 |
* @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
18 | 6674 |
* @return string|false Attachment caption on success, false on failure. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6675 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6676 |
function wp_get_attachment_caption( $post_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6677 |
$post_id = (int) $post_id; |
16 | 6678 |
$post = get_post( $post_id ); |
18 | 6679 |
|
16 | 6680 |
if ( ! $post ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6681 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6682 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6683 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6684 |
if ( 'attachment' !== $post->post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6685 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6686 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6687 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6688 |
$caption = $post->post_excerpt; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6689 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6690 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6691 |
* Filters the attachment caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6692 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6693 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6694 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6695 |
* @param string $caption Caption for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6696 |
* @param int $post_id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6697 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6698 |
return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6699 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6700 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6701 |
/** |
0 | 6702 |
* Retrieve thumbnail for an attachment. |
6703 |
* |
|
6704 |
* @since 2.1.0 |
|
6705 |
* |
|
19 | 6706 |
* @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
18 | 6707 |
* @return string|false Thumbnail file path on success, false on failure. |
0 | 6708 |
*/ |
6709 |
function wp_get_attachment_thumb_file( $post_id = 0 ) { |
|
6710 |
$post_id = (int) $post_id; |
|
16 | 6711 |
$post = get_post( $post_id ); |
18 | 6712 |
|
16 | 6713 |
if ( ! $post ) { |
0 | 6714 |
return false; |
9 | 6715 |
} |
16 | 6716 |
|
6717 |
$imagedata = wp_get_attachment_metadata( $post->ID ); |
|
6718 |
if ( ! is_array( $imagedata ) ) { |
|
0 | 6719 |
return false; |
9 | 6720 |
} |
0 | 6721 |
|
6722 |
$file = get_attached_file( $post->ID ); |
|
6723 |
||
16 | 6724 |
if ( ! empty( $imagedata['thumb'] ) ) { |
6725 |
$thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ); |
|
6726 |
if ( file_exists( $thumbfile ) ) { |
|
6727 |
/** |
|
6728 |
* Filters the attachment thumbnail file path. |
|
6729 |
* |
|
6730 |
* @since 2.1.0 |
|
6731 |
* |
|
6732 |
* @param string $thumbfile File path to the attachment thumbnail. |
|
6733 |
* @param int $post_id Attachment ID. |
|
6734 |
*/ |
|
6735 |
return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
|
6736 |
} |
|
5 | 6737 |
} |
0 | 6738 |
return false; |
6739 |
} |
|
6740 |
||
6741 |
/** |
|
6742 |
* Retrieve URL for an attachment thumbnail. |
|
6743 |
* |
|
6744 |
* @since 2.1.0 |
|
6745 |
* |
|
19 | 6746 |
* @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
18 | 6747 |
* @return string|false Thumbnail URL on success, false on failure. |
0 | 6748 |
*/ |
6749 |
function wp_get_attachment_thumb_url( $post_id = 0 ) { |
|
6750 |
$post_id = (int) $post_id; |
|
16 | 6751 |
$post = get_post( $post_id ); |
18 | 6752 |
|
16 | 6753 |
if ( ! $post ) { |
0 | 6754 |
return false; |
9 | 6755 |
} |
16 | 6756 |
|
6757 |
$url = wp_get_attachment_url( $post->ID ); |
|
6758 |
if ( ! $url ) { |
|
0 | 6759 |
return false; |
9 | 6760 |
} |
0 | 6761 |
|
6762 |
$sized = image_downsize( $post_id, 'thumbnail' ); |
|
9 | 6763 |
if ( $sized ) { |
0 | 6764 |
return $sized[0]; |
9 | 6765 |
} |
6766 |
||
16 | 6767 |
$thumb = wp_get_attachment_thumb_file( $post->ID ); |
6768 |
if ( ! $thumb ) { |
|
0 | 6769 |
return false; |
9 | 6770 |
} |
6771 |
||
6772 |
$url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url ); |
|
0 | 6773 |
|
5 | 6774 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6775 |
* Filters the attachment thumbnail URL. |
5 | 6776 |
* |
6777 |
* @since 2.1.0 |
|
6778 |
* |
|
6779 |
* @param string $url URL for the attachment thumbnail. |
|
6780 |
* @param int $post_id Attachment ID. |
|
6781 |
*/ |
|
0 | 6782 |
return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
6783 |
} |
|
6784 |
||
6785 |
/** |
|
5 | 6786 |
* Verifies an attachment is of a given type. |
6787 |
* |
|
6788 |
* @since 4.2.0 |
|
6789 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6790 |
* @param string $type Attachment type. Accepts 'image', 'audio', or 'video'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6791 |
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
5 | 6792 |
* @return bool True if one of the accepted types, false otherwise. |
6793 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6794 |
function wp_attachment_is( $type, $post = null ) { |
16 | 6795 |
$post = get_post( $post ); |
18 | 6796 |
|
16 | 6797 |
if ( ! $post ) { |
5 | 6798 |
return false; |
6799 |
} |
|
6800 |
||
16 | 6801 |
$file = get_attached_file( $post->ID ); |
18 | 6802 |
|
16 | 6803 |
if ( ! $file ) { |
5 | 6804 |
return false; |
6805 |
} |
|
6806 |
||
6807 |
if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { |
|
6808 |
return true; |
|
6809 |
} |
|
6810 |
||
6811 |
$check = wp_check_filetype( $file ); |
|
18 | 6812 |
|
5 | 6813 |
if ( empty( $check['ext'] ) ) { |
6814 |
return false; |
|
6815 |
} |
|
6816 |
||
6817 |
$ext = $check['ext']; |
|
6818 |
||
6819 |
if ( 'import' !== $post->post_mime_type ) { |
|
6820 |
return $type === $ext; |
|
6821 |
} |
|
6822 |
||
6823 |
switch ( $type ) { |
|
9 | 6824 |
case 'image': |
18 | 6825 |
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'webp' ); |
16 | 6826 |
return in_array( $ext, $image_exts, true ); |
9 | 6827 |
|
6828 |
case 'audio': |
|
16 | 6829 |
return in_array( $ext, wp_get_audio_extensions(), true ); |
9 | 6830 |
|
6831 |
case 'video': |
|
16 | 6832 |
return in_array( $ext, wp_get_video_extensions(), true ); |
9 | 6833 |
|
6834 |
default: |
|
6835 |
return $type === $ext; |
|
5 | 6836 |
} |
6837 |
} |
|
6838 |
||
6839 |
/** |
|
9 | 6840 |
* Determines whether an attachment is an image. |
6841 |
* |
|
6842 |
* For more information on this and similar theme functions, check out |
|
6843 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
6844 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 6845 |
* |
6846 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6847 |
* @since 4.2.0 Modified into wrapper for wp_attachment_is() and |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6848 |
* allowed WP_Post object to be passed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6849 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6850 |
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
5 | 6851 |
* @return bool Whether the attachment is an image. |
0 | 6852 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6853 |
function wp_attachment_is_image( $post = null ) { |
5 | 6854 |
return wp_attachment_is( 'image', $post ); |
0 | 6855 |
} |
6856 |
||
6857 |
/** |
|
16 | 6858 |
* Retrieve the icon for a MIME type or attachment. |
0 | 6859 |
* |
6860 |
* @since 2.1.0 |
|
6861 |
* |
|
6862 |
* @param string|int $mime MIME type or attachment ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6863 |
* @return string|false Icon, false otherwise. |
0 | 6864 |
*/ |
6865 |
function wp_mime_type_icon( $mime = 0 ) { |
|
9 | 6866 |
if ( ! is_numeric( $mime ) ) { |
6867 |
$icon = wp_cache_get( "mime_type_icon_$mime" ); |
|
6868 |
} |
|
0 | 6869 |
|
6870 |
$post_id = 0; |
|
9 | 6871 |
if ( empty( $icon ) ) { |
0 | 6872 |
$post_mimes = array(); |
9 | 6873 |
if ( is_numeric( $mime ) ) { |
0 | 6874 |
$mime = (int) $mime; |
16 | 6875 |
$post = get_post( $mime ); |
6876 |
if ( $post ) { |
|
0 | 6877 |
$post_id = (int) $post->ID; |
9 | 6878 |
$file = get_attached_file( $post_id ); |
6879 |
$ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file ); |
|
6880 |
if ( ! empty( $ext ) ) { |
|
0 | 6881 |
$post_mimes[] = $ext; |
16 | 6882 |
$ext_type = wp_ext2type( $ext ); |
6883 |
if ( $ext_type ) { |
|
0 | 6884 |
$post_mimes[] = $ext_type; |
9 | 6885 |
} |
0 | 6886 |
} |
6887 |
$mime = $post->post_mime_type; |
|
6888 |
} else { |
|
6889 |
$mime = 0; |
|
6890 |
} |
|
6891 |
} else { |
|
6892 |
$post_mimes[] = $mime; |
|
6893 |
} |
|
6894 |
||
9 | 6895 |
$icon_files = wp_cache_get( 'icon_files' ); |
6896 |
||
6897 |
if ( ! is_array( $icon_files ) ) { |
|
5 | 6898 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6899 |
* Filters the icon directory path. |
5 | 6900 |
* |
6901 |
* @since 2.0.0 |
|
6902 |
* |
|
6903 |
* @param string $path Icon directory absolute path. |
|
6904 |
*/ |
|
6905 |
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); |
|
6906 |
||
6907 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6908 |
* Filters the icon directory URI. |
5 | 6909 |
* |
6910 |
* @since 2.0.0 |
|
6911 |
* |
|
6912 |
* @param string $uri Icon directory URI. |
|
6913 |
*/ |
|
6914 |
$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); |
|
6915 |
||
6916 |
/** |
|
16 | 6917 |
* Filters the array of icon directory URIs. |
5 | 6918 |
* |
6919 |
* @since 2.5.0 |
|
6920 |
* |
|
16 | 6921 |
* @param string[] $uris Array of icon directory URIs keyed by directory absolute path. |
5 | 6922 |
*/ |
9 | 6923 |
$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); |
0 | 6924 |
$icon_files = array(); |
6925 |
while ( $dirs ) { |
|
6926 |
$keys = array_keys( $dirs ); |
|
9 | 6927 |
$dir = array_shift( $keys ); |
6928 |
$uri = array_shift( $dirs ); |
|
16 | 6929 |
$dh = opendir( $dir ); |
6930 |
if ( $dh ) { |
|
9 | 6931 |
while ( false !== $file = readdir( $dh ) ) { |
6932 |
$file = wp_basename( $file ); |
|
16 | 6933 |
if ( '.' === substr( $file, 0, 1 ) ) { |
0 | 6934 |
continue; |
6935 |
} |
|
16 | 6936 |
|
6937 |
$ext = strtolower( substr( $file, -4 ) ); |
|
6938 |
if ( ! in_array( $ext, array( '.png', '.gif', '.jpg' ), true ) ) { |
|
9 | 6939 |
if ( is_dir( "$dir/$file" ) ) { |
6940 |
$dirs[ "$dir/$file" ] = "$uri/$file"; |
|
6941 |
} |
|
6942 |
continue; |
|
6943 |
} |
|
6944 |
$icon_files[ "$dir/$file" ] = "$uri/$file"; |
|
0 | 6945 |
} |
9 | 6946 |
closedir( $dh ); |
0 | 6947 |
} |
6948 |
} |
|
6949 |
wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); |
|
6950 |
} |
|
6951 |
||
5 | 6952 |
$types = array(); |
9 | 6953 |
// Icon wp_basename - extension = MIME wildcard. |
6954 |
foreach ( $icon_files as $file => $uri ) { |
|
6955 |
$types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ]; |
|
0 | 6956 |
} |
6957 |
||
9 | 6958 |
if ( ! empty( $mime ) ) { |
6959 |
$post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) ); |
|
6960 |
$post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 ); |
|
6961 |
$post_mimes[] = str_replace( '/', '_', $mime ); |
|
6962 |
} |
|
6963 |
||
6964 |
$matches = wp_match_mime_types( array_keys( $types ), $post_mimes ); |
|
6965 |
$matches['default'] = array( 'default' ); |
|
0 | 6966 |
|
6967 |
foreach ( $matches as $match => $wilds ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6968 |
foreach ( $wilds as $wild ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6969 |
if ( ! isset( $types[ $wild ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6970 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6971 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6972 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6973 |
$icon = $types[ $wild ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6974 |
if ( ! is_numeric( $mime ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6975 |
wp_cache_add( "mime_type_icon_$mime", $icon ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6976 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6977 |
break 2; |
0 | 6978 |
} |
6979 |
} |
|
6980 |
} |
|
6981 |
||
5 | 6982 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6983 |
* Filters the mime type icon. |
5 | 6984 |
* |
6985 |
* @since 2.1.0 |
|
6986 |
* |
|
6987 |
* @param string $icon Path to the mime type icon. |
|
6988 |
* @param string $mime Mime type. |
|
6989 |
* @param int $post_id Attachment ID. Will equal 0 if the function passed |
|
6990 |
* the mime type. |
|
6991 |
*/ |
|
6992 |
return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); |
|
0 | 6993 |
} |
6994 |
||
6995 |
/** |
|
5 | 6996 |
* Check for changed slugs for published post objects and save the old slug. |
0 | 6997 |
* |
6998 |
* The function is used when a post object of any type is updated, |
|
6999 |
* by comparing the current and previous post objects. |
|
7000 |
* |
|
7001 |
* If the slug was changed and not already part of the old slugs then it will be |
|
7002 |
* added to the post meta field ('_wp_old_slug') for storing old slugs for that |
|
7003 |
* post. |
|
7004 |
* |
|
7005 |
* The most logically usage of this function is redirecting changed post objects, so |
|
7006 |
* that those that linked to an changed post will be redirected to the new post. |
|
7007 |
* |
|
7008 |
* @since 2.1.0 |
|
7009 |
* |
|
5 | 7010 |
* @param int $post_id Post ID. |
7011 |
* @param WP_Post $post The Post Object |
|
7012 |
* @param WP_Post $post_before The Previous Post Object |
|
0 | 7013 |
*/ |
5 | 7014 |
function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7015 |
// Don't bother if it hasn't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7016 |
if ( $post->post_name == $post_before->post_name ) { |
0 | 7017 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7018 |
} |
0 | 7019 |
|
5 | 7020 |
// We're only concerned with published, non-hierarchical objects. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7021 |
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { |
0 | 7022 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7023 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7024 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7025 |
$old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); |
0 | 7026 |
|
5 | 7027 |
// If we haven't added this old slug before, add it now. |
16 | 7028 |
if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7029 |
add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7030 |
} |
0 | 7031 |
|
5 | 7032 |
// If the new slug was used previously, delete it from the list. |
16 | 7033 |
if ( in_array( $post->post_name, $old_slugs, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7034 |
delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7035 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7036 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7037 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7038 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7039 |
* Check for changed dates for published post objects and save the old date. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7040 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7041 |
* The function is used when a post object of any type is updated, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7042 |
* by comparing the current and previous post objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7043 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7044 |
* If the date was changed and not already part of the old dates then it will be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7045 |
* added to the post meta field ('_wp_old_date') for storing old dates for that |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7046 |
* post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7047 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7048 |
* The most logically usage of this function is redirecting changed post objects, so |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7049 |
* that those that linked to an changed post will be redirected to the new post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7050 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7051 |
* @since 4.9.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7052 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7053 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7054 |
* @param WP_Post $post The Post Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7055 |
* @param WP_Post $post_before The Previous Post Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7056 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7057 |
function wp_check_for_changed_dates( $post_id, $post, $post_before ) { |
16 | 7058 |
$previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) ); |
7059 |
$new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) ); |
|
7060 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7061 |
// Don't bother if it hasn't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7062 |
if ( $new_date == $previous_date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7063 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7064 |
} |
16 | 7065 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7066 |
// We're only concerned with published, non-hierarchical objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7067 |
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7068 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7069 |
} |
16 | 7070 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7071 |
$old_dates = (array) get_post_meta( $post_id, '_wp_old_date' ); |
16 | 7072 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7073 |
// If we haven't added this old date before, add it now. |
16 | 7074 |
if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7075 |
add_post_meta( $post_id, '_wp_old_date', $previous_date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7076 |
} |
16 | 7077 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7078 |
// If the new slug was used previously, delete it from the list. |
16 | 7079 |
if ( in_array( $new_date, $old_dates, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7080 |
delete_post_meta( $post_id, '_wp_old_date', $new_date ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7081 |
} |
0 | 7082 |
} |
7083 |
||
7084 |
/** |
|
7085 |
* Retrieve the private post SQL based on capability. |
|
7086 |
* |
|
7087 |
* This function provides a standardized way to appropriately select on the |
|
7088 |
* post_status of a post type. The function will return a piece of SQL code |
|
7089 |
* that can be added to a WHERE clause; this SQL is constructed to allow all |
|
7090 |
* published posts, and all private posts to which the user has access. |
|
7091 |
* |
|
7092 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7093 |
* @since 4.3.0 Added the ability to pass an array to `$post_type`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7094 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7095 |
* @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. |
0 | 7096 |
* @return string SQL code that can be added to a where clause. |
7097 |
*/ |
|
7098 |
function get_private_posts_cap_sql( $post_type ) { |
|
7099 |
return get_posts_by_author_sql( $post_type, false ); |
|
7100 |
} |
|
7101 |
||
7102 |
/** |
|
7103 |
* Retrieve the post SQL based on capability, author, and type. |
|
7104 |
* |
|
7105 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7106 |
* @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. |
5 | 7107 |
* |
7108 |
* @see get_private_posts_cap_sql() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7109 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7110 |
* |
16 | 7111 |
* @param string|string[] $post_type Single post type or an array of post types. |
7112 |
* @param bool $full Optional. Returns a full WHERE statement instead of just |
|
7113 |
* an 'andalso' term. Default true. |
|
7114 |
* @param int $post_author Optional. Query posts having a single author ID. Default null. |
|
7115 |
* @param bool $public_only Optional. Only return public posts. Skips cap checks for |
|
7116 |
* $current_user. Default false. |
|
0 | 7117 |
* @return string SQL WHERE code that can be added to a query. |
7118 |
*/ |
|
7119 |
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { |
|
7120 |
global $wpdb; |
|
7121 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7122 |
if ( is_array( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7123 |
$post_types = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7124 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7125 |
$post_types = array( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7126 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7127 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7128 |
$post_type_clauses = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7129 |
foreach ( $post_types as $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7130 |
$post_type_obj = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7131 |
if ( ! $post_type_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7132 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7133 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7134 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7135 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7136 |
* Filters the capability to read private posts for a custom post type |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7137 |
* when generating SQL for getting posts by author. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7139 |
* @since 2.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7140 |
* @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7141 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7142 |
* @param string $cap Capability. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7143 |
*/ |
16 | 7144 |
$cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' ); |
7145 |
if ( ! $cap ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7146 |
$cap = current_user_can( $post_type_obj->cap->read_private_posts ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7147 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7148 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7149 |
// Only need to check the cap if $public_only is false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7150 |
$post_status_sql = "post_status = 'publish'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7151 |
if ( false === $public_only ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7152 |
if ( $cap ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7153 |
// Does the user have the capability to view private posts? Guess so. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7154 |
$post_status_sql .= " OR post_status = 'private'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7155 |
} elseif ( is_user_logged_in() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7156 |
// Users can view their own private posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7157 |
$id = get_current_user_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7158 |
if ( null === $post_author || ! $full ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7159 |
$post_status_sql .= " OR post_status = 'private' AND post_author = $id"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7160 |
} elseif ( $id == (int) $post_author ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7161 |
$post_status_sql .= " OR post_status = 'private'"; |
16 | 7162 |
} // Else none. |
7163 |
} // Else none. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7164 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7165 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7166 |
$post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7167 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7168 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7169 |
if ( empty( $post_type_clauses ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7170 |
return $full ? 'WHERE 1 = 0' : '1 = 0'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7171 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7172 |
|
9 | 7173 |
$sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )'; |
5 | 7174 |
|
7175 |
if ( null !== $post_author ) { |
|
7176 |
$sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); |
|
7177 |
} |
|
7178 |
||
7179 |
if ( $full ) { |
|
7180 |
$sql = 'WHERE ' . $sql; |
|
7181 |
} |
|
0 | 7182 |
|
7183 |
return $sql; |
|
7184 |
} |
|
7185 |
||
7186 |
/** |
|
16 | 7187 |
* Retrieves the most recent time that a post on the site was published. |
0 | 7188 |
* |
7189 |
* The server timezone is the default and is the difference between GMT and |
|
16 | 7190 |
* server time. The 'blog' value is the date when the last post was posted. |
7191 |
* The 'gmt' is when the last post was posted in GMT formatted date. |
|
0 | 7192 |
* |
7193 |
* @since 0.71 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7194 |
* @since 4.4.0 The `$post_type` argument was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7195 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7196 |
* @param string $timezone Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7197 |
* 'server' uses the server's internal timezone. |
16 | 7198 |
* 'blog' uses the `post_date` field, which proxies to the timezone set for the site. |
7199 |
* 'gmt' uses the `post_date_gmt` field. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7200 |
* Default 'server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7201 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
16 | 7202 |
* @return string The date of the last post, or false on failure. |
0 | 7203 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7204 |
function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { |
16 | 7205 |
$lastpostdate = _get_last_post_time( $timezone, 'date', $post_type ); |
7206 |
||
5 | 7207 |
/** |
16 | 7208 |
* Filters the most recent time that a post on the site was published. |
5 | 7209 |
* |
7210 |
* @since 2.3.0 |
|
16 | 7211 |
* @since 5.5.0 Added the `$post_type` parameter. |
5 | 7212 |
* |
16 | 7213 |
* @param string|false $lastpostdate The most recent time that a post was published, |
7214 |
* in 'Y-m-d H:i:s' format. False on failure. |
|
7215 |
* @param string $timezone Location to use for getting the post published date. |
|
7216 |
* See get_lastpostdate() for accepted `$timezone` values. |
|
7217 |
* @param string $post_type The post type to check. |
|
5 | 7218 |
*/ |
16 | 7219 |
return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type ); |
7220 |
} |
|
7221 |
||
7222 |
/** |
|
7223 |
* Get the most recent time that a post on the site was modified. |
|
0 | 7224 |
* |
7225 |
* The server timezone is the default and is the difference between GMT and |
|
16 | 7226 |
* server time. The 'blog' value is just when the last post was modified. |
7227 |
* The 'gmt' is when the last post was modified in GMT time. |
|
0 | 7228 |
* |
7229 |
* @since 1.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7230 |
* @since 4.4.0 The `$post_type` argument was added. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7231 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7232 |
* @param string $timezone Optional. The timezone for the timestamp. See get_lastpostdate() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7233 |
* for information on accepted values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7234 |
* Default 'server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7235 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
16 | 7236 |
* @return string The timestamp in 'Y-m-d H:i:s' format, or false on failure. |
0 | 7237 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7238 |
function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7239 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7240 |
* Pre-filter the return value of get_lastpostmodified() before the query is run. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7241 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7242 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7243 |
* |
16 | 7244 |
* @param string|false $lastpostmodified The most recent time that a post was modified, |
7245 |
* in 'Y-m-d H:i:s' format, or false. Returning anything |
|
7246 |
* other than false will short-circuit the function. |
|
7247 |
* @param string $timezone Location to use for getting the post modified date. |
|
7248 |
* See get_lastpostdate() for accepted `$timezone` values. |
|
7249 |
* @param string $post_type The post type to check. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7250 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7251 |
$lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); |
16 | 7252 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7253 |
if ( false !== $lastpostmodified ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7254 |
return $lastpostmodified; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7255 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7256 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7257 |
$lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); |
16 | 7258 |
$lastpostdate = get_lastpostdate( $timezone, $post_type ); |
7259 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7260 |
if ( $lastpostdate > $lastpostmodified ) { |
0 | 7261 |
$lastpostmodified = $lastpostdate; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7262 |
} |
0 | 7263 |
|
5 | 7264 |
/** |
16 | 7265 |
* Filters the most recent time that a post on the site was modified. |
5 | 7266 |
* |
7267 |
* @since 2.3.0 |
|
16 | 7268 |
* @since 5.5.0 Added the `$post_type` parameter. |
5 | 7269 |
* |
16 | 7270 |
* @param string|false $lastpostmodified The most recent time that a post was modified, |
7271 |
* in 'Y-m-d H:i:s' format. False on failure. |
|
7272 |
* @param string $timezone Location to use for getting the post modified date. |
|
7273 |
* See get_lastpostdate() for accepted `$timezone` values. |
|
7274 |
* @param string $post_type The post type to check. |
|
5 | 7275 |
*/ |
16 | 7276 |
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone, $post_type ); |
7277 |
} |
|
7278 |
||
7279 |
/** |
|
7280 |
* Gets the timestamp of the last time any post was modified or published. |
|
5 | 7281 |
* |
0 | 7282 |
* @since 3.1.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7283 |
* @since 4.4.0 The `$post_type` argument was added. |
5 | 7284 |
* @access private |
7285 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7286 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7287 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7288 |
* @param string $timezone The timezone for the timestamp. See get_lastpostdate(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7289 |
* for information on accepted values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7290 |
* @param string $field Post field to check. Accepts 'date' or 'modified'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7291 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
16 | 7292 |
* @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure. |
0 | 7293 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7294 |
function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { |
0 | 7295 |
global $wpdb; |
7296 |
||
16 | 7297 |
if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) { |
0 | 7298 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7299 |
} |
0 | 7300 |
|
7301 |
$timezone = strtolower( $timezone ); |
|
7302 |
||
7303 |
$key = "lastpost{$field}:$timezone"; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7304 |
if ( 'any' !== $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7305 |
$key .= ':' . sanitize_key( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7306 |
} |
0 | 7307 |
|
7308 |
$date = wp_cache_get( $key, 'timeinfo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7309 |
if ( false !== $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7310 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7311 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7312 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7313 |
if ( 'any' === $post_type ) { |
0 | 7314 |
$post_types = get_post_types( array( 'public' => true ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7315 |
array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); |
0 | 7316 |
$post_types = "'" . implode( "', '", $post_types ) . "'"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7317 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7318 |
$post_types = "'" . sanitize_key( $post_type ) . "'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7319 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7320 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7321 |
switch ( $timezone ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7322 |
case 'gmt': |
9 | 7323 |
$date = $wpdb->get_var( "SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7324 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7325 |
case 'blog': |
9 | 7326 |
$date = $wpdb->get_var( "SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7327 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7328 |
case 'server': |
16 | 7329 |
$add_seconds_server = gmdate( 'Z' ); |
9 | 7330 |
$date = $wpdb->get_var( "SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7331 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7332 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7333 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7334 |
if ( $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7335 |
wp_cache_set( $key, $date, 'timeinfo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7336 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7337 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7338 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7339 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7340 |
return false; |
0 | 7341 |
} |
7342 |
||
7343 |
/** |
|
7344 |
* Updates posts in cache. |
|
7345 |
* |
|
7346 |
* @since 1.5.1 |
|
7347 |
* |
|
16 | 7348 |
* @param WP_Post[] $posts Array of post objects (passed by reference). |
0 | 7349 |
*/ |
7350 |
function update_post_cache( &$posts ) { |
|
9 | 7351 |
if ( ! $posts ) { |
0 | 7352 |
return; |
9 | 7353 |
} |
7354 |
||
19 | 7355 |
$data = array(); |
9 | 7356 |
foreach ( $posts as $post ) { |
19 | 7357 |
if ( empty( $post->filter ) || 'raw' !== $post->filter ) { |
7358 |
$post = sanitize_post( $post, 'raw' ); |
|
7359 |
} |
|
7360 |
$data[ $post->ID ] = $post; |
|
7361 |
} |
|
7362 |
wp_cache_add_multiple( $data, 'posts' ); |
|
0 | 7363 |
} |
7364 |
||
7365 |
/** |
|
7366 |
* Will clean the post in the cache. |
|
7367 |
* |
|
7368 |
* Cleaning means delete from the cache of the post. Will call to clean the term |
|
7369 |
* object cache associated with the post ID. |
|
7370 |
* |
|
7371 |
* This function not run if $_wp_suspend_cache_invalidation is not empty. See |
|
7372 |
* wp_suspend_cache_invalidation(). |
|
7373 |
* |
|
7374 |
* @since 2.0.0 |
|
7375 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7376 |
* @global bool $_wp_suspend_cache_invalidation |
5 | 7377 |
* |
7378 |
* @param int|WP_Post $post Post ID or post object to remove from the cache. |
|
0 | 7379 |
*/ |
7380 |
function clean_post_cache( $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7381 |
global $_wp_suspend_cache_invalidation; |
0 | 7382 |
|
9 | 7383 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
0 | 7384 |
return; |
9 | 7385 |
} |
0 | 7386 |
|
7387 |
$post = get_post( $post ); |
|
18 | 7388 |
|
7389 |
if ( ! $post ) { |
|
0 | 7390 |
return; |
9 | 7391 |
} |
0 | 7392 |
|
7393 |
wp_cache_delete( $post->ID, 'posts' ); |
|
7394 |
wp_cache_delete( $post->ID, 'post_meta' ); |
|
7395 |
||
7396 |
clean_object_term_cache( $post->ID, $post->post_type ); |
|
7397 |
||
7398 |
wp_cache_delete( 'wp_get_archives', 'general' ); |
|
7399 |
||
5 | 7400 |
/** |
7401 |
* Fires immediately after the given post's cache is cleaned. |
|
7402 |
* |
|
7403 |
* @since 2.5.0 |
|
7404 |
* |
|
7405 |
* @param int $post_id Post ID. |
|
7406 |
* @param WP_Post $post Post object. |
|
7407 |
*/ |
|
0 | 7408 |
do_action( 'clean_post_cache', $post->ID, $post ); |
7409 |
||
16 | 7410 |
if ( 'page' === $post->post_type ) { |
0 | 7411 |
wp_cache_delete( 'all_page_ids', 'posts' ); |
5 | 7412 |
|
7413 |
/** |
|
7414 |
* Fires immediately after the given page's cache is cleaned. |
|
7415 |
* |
|
7416 |
* @since 2.5.0 |
|
7417 |
* |
|
7418 |
* @param int $post_id Post ID. |
|
7419 |
*/ |
|
0 | 7420 |
do_action( 'clean_page_cache', $post->ID ); |
7421 |
} |
|
7422 |
||
7423 |
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
7424 |
} |
|
7425 |
||
7426 |
/** |
|
7427 |
* Call major cache updating functions for list of Post objects. |
|
7428 |
* |
|
7429 |
* @since 1.5.0 |
|
7430 |
* |
|
16 | 7431 |
* @param WP_Post[] $posts Array of Post objects |
7432 |
* @param string $post_type Optional. Post type. Default 'post'. |
|
7433 |
* @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
|
7434 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|
0 | 7435 |
*/ |
5 | 7436 |
function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { |
0 | 7437 |
// No point in doing all this work if we didn't match any posts. |
9 | 7438 |
if ( ! $posts ) { |
0 | 7439 |
return; |
9 | 7440 |
} |
7441 |
||
7442 |
update_post_cache( $posts ); |
|
0 | 7443 |
|
7444 |
$post_ids = array(); |
|
9 | 7445 |
foreach ( $posts as $post ) { |
0 | 7446 |
$post_ids[] = $post->ID; |
9 | 7447 |
} |
7448 |
||
7449 |
if ( ! $post_type ) { |
|
0 | 7450 |
$post_type = 'any'; |
9 | 7451 |
} |
0 | 7452 |
|
7453 |
if ( $update_term_cache ) { |
|
9 | 7454 |
if ( is_array( $post_type ) ) { |
0 | 7455 |
$ptypes = $post_type; |
16 | 7456 |
} elseif ( 'any' === $post_type ) { |
5 | 7457 |
$ptypes = array(); |
0 | 7458 |
// Just use the post_types in the supplied posts. |
5 | 7459 |
foreach ( $posts as $post ) { |
0 | 7460 |
$ptypes[] = $post->post_type; |
5 | 7461 |
} |
9 | 7462 |
$ptypes = array_unique( $ptypes ); |
0 | 7463 |
} else { |
9 | 7464 |
$ptypes = array( $post_type ); |
0 | 7465 |
} |
7466 |
||
9 | 7467 |
if ( ! empty( $ptypes ) ) { |
7468 |
update_object_term_cache( $post_ids, $ptypes ); |
|
7469 |
} |
|
7470 |
} |
|
7471 |
||
7472 |
if ( $update_meta_cache ) { |
|
7473 |
update_postmeta_cache( $post_ids ); |
|
7474 |
} |
|
0 | 7475 |
} |
7476 |
||
7477 |
/** |
|
7478 |
* Updates metadata cache for list of post IDs. |
|
7479 |
* |
|
7480 |
* Performs SQL query to retrieve the metadata for the post IDs and updates the |
|
7481 |
* metadata cache for the posts. Therefore, the functions, which call this |
|
7482 |
* function, do not need to perform SQL queries on their own. |
|
7483 |
* |
|
7484 |
* @since 2.1.0 |
|
7485 |
* |
|
16 | 7486 |
* @param int[] $post_ids Array of post IDs. |
7487 |
* @return array|false An array of metadata on success, false if there is nothing to update. |
|
0 | 7488 |
*/ |
5 | 7489 |
function update_postmeta_cache( $post_ids ) { |
9 | 7490 |
return update_meta_cache( 'post', $post_ids ); |
0 | 7491 |
} |
7492 |
||
7493 |
/** |
|
7494 |
* Will clean the attachment in the cache. |
|
7495 |
* |
|
7496 |
* Cleaning means delete from the cache. Optionally will clean the term |
|
7497 |
* object cache associated with the attachment ID. |
|
7498 |
* |
|
5 | 7499 |
* This function will not run if $_wp_suspend_cache_invalidation is not empty. |
7500 |
* |
|
0 | 7501 |
* @since 3.0.0 |
7502 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7503 |
* @global bool $_wp_suspend_cache_invalidation |
5 | 7504 |
* |
7505 |
* @param int $id The attachment ID in the cache to clean. |
|
7506 |
* @param bool $clean_terms Optional. Whether to clean terms cache. Default false. |
|
0 | 7507 |
*/ |
5 | 7508 |
function clean_attachment_cache( $id, $clean_terms = false ) { |
0 | 7509 |
global $_wp_suspend_cache_invalidation; |
7510 |
||
9 | 7511 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
0 | 7512 |
return; |
9 | 7513 |
} |
0 | 7514 |
|
7515 |
$id = (int) $id; |
|
7516 |
||
9 | 7517 |
wp_cache_delete( $id, 'posts' ); |
7518 |
wp_cache_delete( $id, 'post_meta' ); |
|
7519 |
||
7520 |
if ( $clean_terms ) { |
|
7521 |
clean_object_term_cache( $id, 'attachment' ); |
|
7522 |
} |
|
0 | 7523 |
|
5 | 7524 |
/** |
7525 |
* Fires after the given attachment's cache is cleaned. |
|
7526 |
* |
|
7527 |
* @since 3.0.0 |
|
7528 |
* |
|
7529 |
* @param int $id Attachment ID. |
|
7530 |
*/ |
|
7531 |
do_action( 'clean_attachment_cache', $id ); |
|
0 | 7532 |
} |
7533 |
||
7534 |
// |
|
16 | 7535 |
// Hooks. |
0 | 7536 |
// |
7537 |
||
7538 |
/** |
|
7539 |
* Hook for managing future post transitions to published. |
|
7540 |
* |
|
7541 |
* @since 2.3.0 |
|
7542 |
* @access private |
|
5 | 7543 |
* |
7544 |
* @see wp_clear_scheduled_hook() |
|
7545 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
7546 |
* |
|
7547 |
* @param string $new_status New post status. |
|
7548 |
* @param string $old_status Previous post status. |
|
7549 |
* @param WP_Post $post Post object. |
|
0 | 7550 |
*/ |
5 | 7551 |
function _transition_post_status( $new_status, $old_status, $post ) { |
0 | 7552 |
global $wpdb; |
7553 |
||
16 | 7554 |
if ( 'publish' !== $old_status && 'publish' === $new_status ) { |
5 | 7555 |
// Reset GUID if transitioning to publish and it is empty. |
16 | 7556 |
if ( '' === get_the_guid( $post->ID ) ) { |
0 | 7557 |
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); |
9 | 7558 |
} |
5 | 7559 |
|
7560 |
/** |
|
7561 |
* Fires when a post's status is transitioned from private to published. |
|
7562 |
* |
|
7563 |
* @since 1.5.0 |
|
16 | 7564 |
* @deprecated 2.3.0 Use {@see 'private_to_publish'} instead. |
5 | 7565 |
* |
7566 |
* @param int $post_id Post ID. |
|
7567 |
*/ |
|
16 | 7568 |
do_action_deprecated( 'private_to_published', array( $post->ID ), '2.3.0', 'private_to_publish' ); |
0 | 7569 |
} |
7570 |
||
5 | 7571 |
// If published posts changed clear the lastpostmodified cache. |
16 | 7572 |
if ( 'publish' === $new_status || 'publish' === $old_status ) { |
0 | 7573 |
foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { |
7574 |
wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); |
|
7575 |
wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7576 |
wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); |
0 | 7577 |
} |
7578 |
} |
|
7579 |
||
5 | 7580 |
if ( $new_status !== $old_status ) { |
7581 |
wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); |
|
7582 |
wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); |
|
7583 |
} |
|
7584 |
||
0 | 7585 |
// Always clears the hook in case the post status bounced from future to draft. |
9 | 7586 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
0 | 7587 |
} |
7588 |
||
7589 |
/** |
|
7590 |
* Hook used to schedule publication for a post marked for the future. |
|
7591 |
* |
|
7592 |
* The $post properties used and must exist are 'ID' and 'post_date_gmt'. |
|
7593 |
* |
|
7594 |
* @since 2.3.0 |
|
7595 |
* @access private |
|
7596 |
* |
|
5 | 7597 |
* @param int $deprecated Not used. Can be set to null. Never implemented. Not marked |
7598 |
* as deprecated with _deprecated_argument() as it conflicts with |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7599 |
* wp_transition_post_status() and the default filter for _future_post_hook(). |
5 | 7600 |
* @param WP_Post $post Post object. |
0 | 7601 |
*/ |
7602 |
function _future_post_hook( $deprecated, $post ) { |
|
7603 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
|
9 | 7604 |
wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) ); |
0 | 7605 |
} |
7606 |
||
7607 |
/** |
|
7608 |
* Hook to schedule pings and enclosures when a post is published. |
|
7609 |
* |
|
5 | 7610 |
* Uses XMLRPC_REQUEST and WP_IMPORTING constants. |
7611 |
* |
|
0 | 7612 |
* @since 2.3.0 |
7613 |
* @access private |
|
5 | 7614 |
* |
19 | 7615 |
* @param int $post_id The ID of the post being published. |
0 | 7616 |
*/ |
5 | 7617 |
function _publish_post_hook( $post_id ) { |
7618 |
if ( defined( 'XMLRPC_REQUEST' ) ) { |
|
7619 |
/** |
|
7620 |
* Fires when _publish_post_hook() is called during an XML-RPC request. |
|
7621 |
* |
|
7622 |
* @since 2.1.0 |
|
7623 |
* |
|
7624 |
* @param int $post_id Post ID. |
|
7625 |
*/ |
|
7626 |
do_action( 'xmlrpc_publish_post', $post_id ); |
|
7627 |
} |
|
0 | 7628 |
|
9 | 7629 |
if ( defined( 'WP_IMPORTING' ) ) { |
0 | 7630 |
return; |
9 | 7631 |
} |
7632 |
||
7633 |
if ( get_option( 'default_pingback_flag' ) ) { |
|
16 | 7634 |
add_post_meta( $post_id, '_pingme', '1', true ); |
7635 |
} |
|
7636 |
add_post_meta( $post_id, '_encloseme', '1', true ); |
|
7637 |
||
7638 |
$to_ping = get_to_ping( $post_id ); |
|
7639 |
if ( ! empty( $to_ping ) ) { |
|
7640 |
add_post_meta( $post_id, '_trackbackme', '1' ); |
|
7641 |
} |
|
0 | 7642 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7643 |
if ( ! wp_next_scheduled( 'do_pings' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7644 |
wp_schedule_single_event( time(), 'do_pings' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7645 |
} |
0 | 7646 |
} |
7647 |
||
7648 |
/** |
|
9 | 7649 |
* Returns the ID of the post's parent. |
0 | 7650 |
* |
7651 |
* @since 3.1.0 |
|
19 | 7652 |
* @since 5.9.0 The `$post` parameter was made optional. |
7653 |
* |
|
7654 |
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
|
16 | 7655 |
* @return int|false Post parent ID (which can be 0 if there is no parent), |
7656 |
* or false if the post does not exist. |
|
0 | 7657 |
*/ |
19 | 7658 |
function wp_get_post_parent_id( $post = null ) { |
9 | 7659 |
$post = get_post( $post ); |
7660 |
if ( ! $post || is_wp_error( $post ) ) { |
|
0 | 7661 |
return false; |
9 | 7662 |
} |
0 | 7663 |
return (int) $post->post_parent; |
7664 |
} |
|
7665 |
||
7666 |
/** |
|
5 | 7667 |
* Check the given subset of the post hierarchy for hierarchy loops. |
7668 |
* |
|
7669 |
* Prevents loops from forming and breaks those that it finds. Attached |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7670 |
* to the {@see 'wp_insert_post_parent'} filter. |
0 | 7671 |
* |
7672 |
* @since 3.1.0 |
|
5 | 7673 |
* |
7674 |
* @see wp_find_hierarchy_loop() |
|
0 | 7675 |
* |
7676 |
* @param int $post_parent ID of the parent for the post we're checking. |
|
5 | 7677 |
* @param int $post_ID ID of the post we're checking. |
7678 |
* @return int The new post_parent for the post, 0 otherwise. |
|
0 | 7679 |
*/ |
7680 |
function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { |
|
5 | 7681 |
// Nothing fancy here - bail. |
9 | 7682 |
if ( ! $post_parent ) { |
0 | 7683 |
return 0; |
9 | 7684 |
} |
0 | 7685 |
|
5 | 7686 |
// New post can't cause a loop. |
18 | 7687 |
if ( ! $post_ID ) { |
0 | 7688 |
return $post_parent; |
9 | 7689 |
} |
0 | 7690 |
|
5 | 7691 |
// Can't be its own parent. |
9 | 7692 |
if ( $post_parent == $post_ID ) { |
0 | 7693 |
return 0; |
9 | 7694 |
} |
0 | 7695 |
|
5 | 7696 |
// Now look for larger loops. |
16 | 7697 |
$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ); |
7698 |
if ( ! $loop ) { |
|
7699 |
return $post_parent; // No loop. |
|
9 | 7700 |
} |
0 | 7701 |
|
5 | 7702 |
// Setting $post_parent to the given value causes a loop. |
9 | 7703 |
if ( isset( $loop[ $post_ID ] ) ) { |
0 | 7704 |
return 0; |
9 | 7705 |
} |
0 | 7706 |
|
7707 |
// There's a loop, but it doesn't contain $post_ID. Break the loop. |
|
9 | 7708 |
foreach ( array_keys( $loop ) as $loop_member ) { |
7709 |
wp_update_post( |
|
7710 |
array( |
|
7711 |
'ID' => $loop_member, |
|
7712 |
'post_parent' => 0, |
|
7713 |
) |
|
7714 |
); |
|
7715 |
} |
|
0 | 7716 |
|
7717 |
return $post_parent; |
|
7718 |
} |
|
7719 |
||
7720 |
/** |
|
9 | 7721 |
* Sets the post thumbnail (featured image) for the given post. |
0 | 7722 |
* |
7723 |
* @since 3.1.0 |
|
7724 |
* |
|
5 | 7725 |
* @param int|WP_Post $post Post ID or post object where thumbnail should be attached. |
7726 |
* @param int $thumbnail_id Thumbnail to attach. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7727 |
* @return int|bool True on success, false on failure. |
0 | 7728 |
*/ |
7729 |
function set_post_thumbnail( $post, $thumbnail_id ) { |
|
9 | 7730 |
$post = get_post( $post ); |
0 | 7731 |
$thumbnail_id = absint( $thumbnail_id ); |
7732 |
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { |
|
9 | 7733 |
if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) { |
0 | 7734 |
return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); |
9 | 7735 |
} else { |
0 | 7736 |
return delete_post_meta( $post->ID, '_thumbnail_id' ); |
9 | 7737 |
} |
0 | 7738 |
} |
7739 |
return false; |
|
7740 |
} |
|
7741 |
||
7742 |
/** |
|
9 | 7743 |
* Removes the thumbnail (featured image) from the given post. |
0 | 7744 |
* |
7745 |
* @since 3.3.0 |
|
7746 |
* |
|
9 | 7747 |
* @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed. |
0 | 7748 |
* @return bool True on success, false on failure. |
7749 |
*/ |
|
7750 |
function delete_post_thumbnail( $post ) { |
|
7751 |
$post = get_post( $post ); |
|
9 | 7752 |
if ( $post ) { |
0 | 7753 |
return delete_post_meta( $post->ID, '_thumbnail_id' ); |
9 | 7754 |
} |
0 | 7755 |
return false; |
7756 |
} |
|
7757 |
||
7758 |
/** |
|
5 | 7759 |
* Delete auto-drafts for new posts that are > 7 days old. |
0 | 7760 |
* |
7761 |
* @since 3.4.0 |
|
5 | 7762 |
* |
7763 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 7764 |
*/ |
7765 |
function wp_delete_auto_drafts() { |
|
7766 |
global $wpdb; |
|
7767 |
||
5 | 7768 |
// Cleanup old auto-drafts more than 7 days old. |
0 | 7769 |
$old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" ); |
5 | 7770 |
foreach ( (array) $old_posts as $delete ) { |
7771 |
// Force delete. |
|
7772 |
wp_delete_post( $delete, true ); |
|
7773 |
} |
|
0 | 7774 |
} |
7775 |
||
7776 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7777 |
* Queues posts for lazy-loading of term meta. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7778 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7779 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7780 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7781 |
* @param array $posts Array of WP_Post objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7782 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7783 |
function wp_queue_posts_for_term_meta_lazyload( $posts ) { |
16 | 7784 |
$post_type_taxonomies = array(); |
7785 |
$term_ids = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7786 |
foreach ( $posts as $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7787 |
if ( ! ( $post instanceof WP_Post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7788 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7789 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7790 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7791 |
if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7792 |
$post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7793 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7795 |
foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7796 |
// Term cache should already be primed by `update_post_term_cache()`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7797 |
$terms = get_object_term_cache( $post->ID, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7798 |
if ( false !== $terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7799 |
foreach ( $terms as $term ) { |
18 | 7800 |
if ( ! in_array( $term->term_id, $term_ids, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7801 |
$term_ids[] = $term->term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7802 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7803 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7804 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7805 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7806 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7807 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7808 |
if ( $term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7809 |
$lazyloader = wp_metadata_lazyloader(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7810 |
$lazyloader->queue_objects( 'term', $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7811 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7812 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7813 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7814 |
/** |
5 | 7815 |
* Update the custom taxonomies' term counts when a post's status is changed. |
7816 |
* |
|
7817 |
* For example, default posts term counts (for custom taxonomies) don't include |
|
7818 |
* private / draft posts. |
|
7819 |
* |
|
7820 |
* @since 3.3.0 |
|
0 | 7821 |
* @access private |
5 | 7822 |
* |
7823 |
* @param string $new_status New post status. |
|
7824 |
* @param string $old_status Old post status. |
|
7825 |
* @param WP_Post $post Post object. |
|
0 | 7826 |
*/ |
7827 |
function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { |
|
7828 |
// Update counts for the post's terms. |
|
7829 |
foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { |
|
7830 |
$tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); |
|
7831 |
wp_update_term_count( $tt_ids, $taxonomy ); |
|
7832 |
} |
|
7833 |
} |
|
7834 |
||
7835 |
/** |
|
18 | 7836 |
* Adds any posts from the given IDs to the cache that do not already exist in cache. |
0 | 7837 |
* |
7838 |
* @since 3.4.0 |
|
7839 |
* @access private |
|
7840 |
* |
|
5 | 7841 |
* @see update_post_caches() |
7842 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7843 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7844 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7845 |
* @param array $ids ID list. |
5 | 7846 |
* @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
7847 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|
0 | 7848 |
*/ |
7849 |
function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { |
|
7850 |
global $wpdb; |
|
7851 |
||
7852 |
$non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); |
|
9 | 7853 |
if ( ! empty( $non_cached_ids ) ) { |
18 | 7854 |
$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", implode( ',', $non_cached_ids ) ) ); |
0 | 7855 |
|
7856 |
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); |
|
7857 |
} |
|
7858 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7859 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7860 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7861 |
* Adds a suffix if any trashed posts have a given slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7862 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7863 |
* Store its desired (i.e. current) slug so it can try to reclaim it |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7864 |
* if the post is untrashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7865 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7866 |
* For internal use. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7867 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7868 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7869 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7870 |
* |
19 | 7871 |
* @param string $post_name Post slug. |
18 | 7872 |
* @param int $post_ID Optional. Post ID that should be ignored. Default 0. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7873 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7874 |
function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { |
9 | 7875 |
$trashed_posts_with_desired_slug = get_posts( |
7876 |
array( |
|
7877 |
'name' => $post_name, |
|
7878 |
'post_status' => 'trash', |
|
7879 |
'post_type' => 'any', |
|
7880 |
'nopaging' => true, |
|
7881 |
'post__not_in' => array( $post_ID ), |
|
7882 |
) |
|
7883 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7884 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7885 |
if ( ! empty( $trashed_posts_with_desired_slug ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7886 |
foreach ( $trashed_posts_with_desired_slug as $_post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7887 |
wp_add_trashed_suffix_to_post_name_for_post( $_post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7888 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7889 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7890 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7891 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7892 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7893 |
* Adds a trashed suffix for a given post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7894 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7895 |
* Store its desired (i.e. current) slug so it can try to reclaim it |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7896 |
* if the post is untrashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7897 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7898 |
* For internal use. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7899 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7900 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7901 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7902 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7903 |
* @param WP_Post $post The post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7904 |
* @return string New slug for the post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7905 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7906 |
function wp_add_trashed_suffix_to_post_name_for_post( $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7907 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7908 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7909 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7910 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7911 |
if ( '__trashed' === substr( $post->post_name, -9 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7912 |
return $post->post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7913 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7914 |
add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7915 |
$post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7916 |
$wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7917 |
clean_post_cache( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7918 |
return $post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7919 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7920 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7921 |
/** |
18 | 7922 |
* Filters the SQL clauses of an attachment query to include filenames. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7923 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7924 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7925 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7926 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7927 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7928 |
* |
16 | 7929 |
* @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
7930 |
* DISTINCT, fields (SELECT), and LIMITS clauses. |
|
7931 |
* @return string[] The modified array of clauses. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7932 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7933 |
function _filter_query_attachment_filenames( $clauses ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7934 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7935 |
remove_filter( 'posts_clauses', __FUNCTION__ ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7936 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7937 |
// Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7938 |
$clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7939 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7940 |
$clauses['groupby'] = "{$wpdb->posts}.ID"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7941 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7942 |
$clauses['where'] = preg_replace( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7943 |
"/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
9 | 7944 |
'$0 OR ( sq1.meta_value $1 $2 )', |
7945 |
$clauses['where'] |
|
7946 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7947 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7948 |
return $clauses; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7949 |
} |
9 | 7950 |
|
7951 |
/** |
|
7952 |
* Sets the last changed time for the 'posts' cache group. |
|
7953 |
* |
|
7954 |
* @since 5.0.0 |
|
7955 |
*/ |
|
7956 |
function wp_cache_set_posts_last_changed() { |
|
7957 |
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
7958 |
} |
|
7959 |
||
7960 |
/** |
|
7961 |
* Get all available post MIME types for a given post type. |
|
7962 |
* |
|
7963 |
* @since 2.5.0 |
|
7964 |
* |
|
7965 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
7966 |
* |
|
7967 |
* @param string $type |
|
7968 |
* @return mixed |
|
7969 |
*/ |
|
7970 |
function get_available_post_mime_types( $type = 'attachment' ) { |
|
7971 |
global $wpdb; |
|
7972 |
||
7973 |
$types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) ); |
|
7974 |
return $types; |
|
7975 |
} |
|
16 | 7976 |
|
7977 |
/** |
|
7978 |
* Retrieves the path to an uploaded image file. |
|
7979 |
* |
|
7980 |
* Similar to `get_attached_file()` however some images may have been processed after uploading |
|
7981 |
* to make them suitable for web use. In this case the attached "full" size file is usually replaced |
|
7982 |
* with a scaled down version of the original image. This function always returns the path |
|
7983 |
* to the originally uploaded image file. |
|
7984 |
* |
|
7985 |
* @since 5.3.0 |
|
7986 |
* @since 5.4.0 Added the `$unfiltered` parameter. |
|
7987 |
* |
|
7988 |
* @param int $attachment_id Attachment ID. |
|
7989 |
* @param bool $unfiltered Optional. Passed through to `get_attached_file()`. Default false. |
|
7990 |
* @return string|false Path to the original image file or false if the attachment is not an image. |
|
7991 |
*/ |
|
7992 |
function wp_get_original_image_path( $attachment_id, $unfiltered = false ) { |
|
7993 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
|
7994 |
return false; |
|
7995 |
} |
|
7996 |
||
7997 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
7998 |
$image_file = get_attached_file( $attachment_id, $unfiltered ); |
|
7999 |
||
8000 |
if ( empty( $image_meta['original_image'] ) ) { |
|
8001 |
$original_image = $image_file; |
|
8002 |
} else { |
|
8003 |
$original_image = path_join( dirname( $image_file ), $image_meta['original_image'] ); |
|
8004 |
} |
|
8005 |
||
8006 |
/** |
|
8007 |
* Filters the path to the original image. |
|
8008 |
* |
|
8009 |
* @since 5.3.0 |
|
8010 |
* |
|
8011 |
* @param string $original_image Path to original image file. |
|
8012 |
* @param int $attachment_id Attachment ID. |
|
8013 |
*/ |
|
8014 |
return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id ); |
|
8015 |
} |
|
8016 |
||
8017 |
/** |
|
8018 |
* Retrieve the URL to an original attachment image. |
|
8019 |
* |
|
8020 |
* Similar to `wp_get_attachment_url()` however some images may have been |
|
8021 |
* processed after uploading. In this case this function returns the URL |
|
8022 |
* to the originally uploaded image file. |
|
8023 |
* |
|
8024 |
* @since 5.3.0 |
|
8025 |
* |
|
8026 |
* @param int $attachment_id Attachment post ID. |
|
8027 |
* @return string|false Attachment image URL, false on error or if the attachment is not an image. |
|
8028 |
*/ |
|
8029 |
function wp_get_original_image_url( $attachment_id ) { |
|
8030 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
|
8031 |
return false; |
|
8032 |
} |
|
8033 |
||
8034 |
$image_url = wp_get_attachment_url( $attachment_id ); |
|
8035 |
||
18 | 8036 |
if ( ! $image_url ) { |
16 | 8037 |
return false; |
8038 |
} |
|
8039 |
||
8040 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
8041 |
||
8042 |
if ( empty( $image_meta['original_image'] ) ) { |
|
8043 |
$original_image_url = $image_url; |
|
8044 |
} else { |
|
8045 |
$original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] ); |
|
8046 |
} |
|
8047 |
||
8048 |
/** |
|
8049 |
* Filters the URL to the original attachment image. |
|
8050 |
* |
|
8051 |
* @since 5.3.0 |
|
8052 |
* |
|
8053 |
* @param string $original_image_url URL to original image. |
|
8054 |
* @param int $attachment_id Attachment ID. |
|
8055 |
*/ |
|
8056 |
return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id ); |
|
8057 |
} |
|
18 | 8058 |
|
8059 |
/** |
|
8060 |
* Filter callback which sets the status of an untrashed post to its previous status. |
|
8061 |
* |
|
8062 |
* This can be used as a callback on the `wp_untrash_post_status` filter. |
|
8063 |
* |
|
8064 |
* @since 5.6.0 |
|
8065 |
* |
|
8066 |
* @param string $new_status The new status of the post being restored. |
|
8067 |
* @param int $post_id The ID of the post being restored. |
|
8068 |
* @param string $previous_status The status of the post at the point where it was trashed. |
|
8069 |
* @return string The new status of the post. |
|
8070 |
*/ |
|
8071 |
function wp_untrash_post_set_previous_status( $new_status, $post_id, $previous_status ) { |
|
8072 |
return $previous_status; |
|
8073 |
} |