author | ymh <ymh.work@gmail.com> |
Wed, 16 Oct 2019 11:23:38 +0200 | |
changeset 14 | 00ac8f60d73f |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
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 |
// |
|
10 |
// Post Type Registration |
|
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() { |
|
9 | 21 |
register_post_type( |
22 |
'post', |
|
23 |
array( |
|
24 |
'labels' => array( |
|
25 |
'name_admin_bar' => _x( 'Post', 'add new from admin bar' ), |
|
26 |
), |
|
27 |
'public' => true, |
|
28 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
29 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
30 |
'capability_type' => 'post', |
|
31 |
'map_meta_cap' => true, |
|
32 |
'menu_position' => 5, |
|
33 |
'hierarchical' => false, |
|
34 |
'rewrite' => false, |
|
35 |
'query_var' => false, |
|
36 |
'delete_with_user' => true, |
|
37 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), |
|
38 |
'show_in_rest' => true, |
|
39 |
'rest_base' => 'posts', |
|
40 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
41 |
) |
|
42 |
); |
|
43 |
||
44 |
register_post_type( |
|
45 |
'page', |
|
46 |
array( |
|
47 |
'labels' => array( |
|
48 |
'name_admin_bar' => _x( 'Page', 'add new from admin bar' ), |
|
49 |
), |
|
50 |
'public' => true, |
|
51 |
'publicly_queryable' => false, |
|
52 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
53 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
54 |
'capability_type' => 'page', |
|
55 |
'map_meta_cap' => true, |
|
56 |
'menu_position' => 20, |
|
57 |
'hierarchical' => true, |
|
58 |
'rewrite' => false, |
|
59 |
'query_var' => false, |
|
60 |
'delete_with_user' => true, |
|
61 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), |
|
62 |
'show_in_rest' => true, |
|
63 |
'rest_base' => 'pages', |
|
64 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
65 |
) |
|
66 |
); |
|
67 |
||
68 |
register_post_type( |
|
69 |
'attachment', |
|
70 |
array( |
|
71 |
'labels' => array( |
|
72 |
'name' => _x( 'Media', 'post type general name' ), |
|
73 |
'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), |
|
74 |
'add_new' => _x( 'Add New', 'add new media' ), |
|
75 |
'edit_item' => __( 'Edit Media' ), |
|
76 |
'view_item' => __( 'View Attachment Page' ), |
|
77 |
'attributes' => __( 'Attachment Attributes' ), |
|
78 |
), |
|
79 |
'public' => true, |
|
80 |
'show_ui' => true, |
|
81 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
82 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
83 |
'capability_type' => 'post', |
|
84 |
'capabilities' => array( |
|
85 |
'create_posts' => 'upload_files', |
|
86 |
), |
|
87 |
'map_meta_cap' => true, |
|
88 |
'hierarchical' => false, |
|
89 |
'rewrite' => false, |
|
90 |
'query_var' => false, |
|
91 |
'show_in_nav_menus' => false, |
|
92 |
'delete_with_user' => true, |
|
93 |
'supports' => array( 'title', 'author', 'comments' ), |
|
94 |
'show_in_rest' => true, |
|
95 |
'rest_base' => 'media', |
|
96 |
'rest_controller_class' => 'WP_REST_Attachments_Controller', |
|
97 |
) |
|
98 |
); |
|
5 | 99 |
add_post_type_support( 'attachment:audio', 'thumbnail' ); |
100 |
add_post_type_support( 'attachment:video', 'thumbnail' ); |
|
0 | 101 |
|
9 | 102 |
register_post_type( |
103 |
'revision', |
|
104 |
array( |
|
105 |
'labels' => array( |
|
106 |
'name' => __( 'Revisions' ), |
|
107 |
'singular_name' => __( 'Revision' ), |
|
108 |
), |
|
109 |
'public' => false, |
|
110 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
111 |
'_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ |
|
112 |
'capability_type' => 'post', |
|
113 |
'map_meta_cap' => true, |
|
114 |
'hierarchical' => false, |
|
115 |
'rewrite' => false, |
|
116 |
'query_var' => false, |
|
117 |
'can_export' => false, |
|
118 |
'delete_with_user' => true, |
|
119 |
'supports' => array( 'author' ), |
|
120 |
) |
|
121 |
); |
|
122 |
||
123 |
register_post_type( |
|
124 |
'nav_menu_item', |
|
125 |
array( |
|
126 |
'labels' => array( |
|
127 |
'name' => __( 'Navigation Menu Items' ), |
|
128 |
'singular_name' => __( 'Navigation Menu Item' ), |
|
129 |
), |
|
130 |
'public' => false, |
|
131 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
132 |
'hierarchical' => false, |
|
133 |
'rewrite' => false, |
|
134 |
'delete_with_user' => false, |
|
135 |
'query_var' => false, |
|
136 |
) |
|
137 |
); |
|
138 |
||
139 |
register_post_type( |
|
140 |
'custom_css', |
|
141 |
array( |
|
142 |
'labels' => array( |
|
143 |
'name' => __( 'Custom CSS' ), |
|
144 |
'singular_name' => __( 'Custom CSS' ), |
|
145 |
), |
|
146 |
'public' => false, |
|
147 |
'hierarchical' => false, |
|
148 |
'rewrite' => false, |
|
149 |
'query_var' => false, |
|
150 |
'delete_with_user' => false, |
|
151 |
'can_export' => true, |
|
152 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
153 |
'supports' => array( 'title', 'revisions' ), |
|
154 |
'capabilities' => array( |
|
155 |
'delete_posts' => 'edit_theme_options', |
|
156 |
'delete_post' => 'edit_theme_options', |
|
157 |
'delete_published_posts' => 'edit_theme_options', |
|
158 |
'delete_private_posts' => 'edit_theme_options', |
|
159 |
'delete_others_posts' => 'edit_theme_options', |
|
160 |
'edit_post' => 'edit_css', |
|
161 |
'edit_posts' => 'edit_css', |
|
162 |
'edit_others_posts' => 'edit_css', |
|
163 |
'edit_published_posts' => 'edit_css', |
|
164 |
'read_post' => 'read', |
|
165 |
'read_private_posts' => 'read', |
|
166 |
'publish_posts' => 'edit_theme_options', |
|
167 |
), |
|
168 |
) |
|
169 |
); |
|
170 |
||
171 |
register_post_type( |
|
172 |
'customize_changeset', |
|
173 |
array( |
|
174 |
'labels' => array( |
|
175 |
'name' => _x( 'Changesets', 'post type general name' ), |
|
176 |
'singular_name' => _x( 'Changeset', 'post type singular name' ), |
|
177 |
'menu_name' => _x( 'Changesets', 'admin menu' ), |
|
178 |
'name_admin_bar' => _x( 'Changeset', 'add new on admin bar' ), |
|
179 |
'add_new' => _x( 'Add New', 'Customize Changeset' ), |
|
180 |
'add_new_item' => __( 'Add New Changeset' ), |
|
181 |
'new_item' => __( 'New Changeset' ), |
|
182 |
'edit_item' => __( 'Edit Changeset' ), |
|
183 |
'view_item' => __( 'View Changeset' ), |
|
184 |
'all_items' => __( 'All Changesets' ), |
|
185 |
'search_items' => __( 'Search Changesets' ), |
|
186 |
'not_found' => __( 'No changesets found.' ), |
|
187 |
'not_found_in_trash' => __( 'No changesets found in Trash.' ), |
|
188 |
), |
|
189 |
'public' => false, |
|
190 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
191 |
'map_meta_cap' => true, |
|
192 |
'hierarchical' => false, |
|
193 |
'rewrite' => false, |
|
194 |
'query_var' => false, |
|
195 |
'can_export' => false, |
|
196 |
'delete_with_user' => false, |
|
197 |
'supports' => array( 'title', 'author' ), |
|
198 |
'capability_type' => 'customize_changeset', |
|
199 |
'capabilities' => array( |
|
200 |
'create_posts' => 'customize', |
|
201 |
'delete_others_posts' => 'customize', |
|
202 |
'delete_post' => 'customize', |
|
203 |
'delete_posts' => 'customize', |
|
204 |
'delete_private_posts' => 'customize', |
|
205 |
'delete_published_posts' => 'customize', |
|
206 |
'edit_others_posts' => 'customize', |
|
207 |
'edit_post' => 'customize', |
|
208 |
'edit_posts' => 'customize', |
|
209 |
'edit_private_posts' => 'customize', |
|
210 |
'edit_published_posts' => 'do_not_allow', |
|
211 |
'publish_posts' => 'customize', |
|
212 |
'read' => 'read', |
|
213 |
'read_post' => 'customize', |
|
214 |
'read_private_posts' => 'customize', |
|
215 |
), |
|
216 |
) |
|
217 |
); |
|
218 |
||
219 |
register_post_type( |
|
220 |
'oembed_cache', |
|
221 |
array( |
|
222 |
'labels' => array( |
|
223 |
'name' => __( 'oEmbed Responses' ), |
|
224 |
'singular_name' => __( 'oEmbed Response' ), |
|
225 |
), |
|
226 |
'public' => false, |
|
227 |
'hierarchical' => false, |
|
228 |
'rewrite' => false, |
|
229 |
'query_var' => false, |
|
230 |
'delete_with_user' => false, |
|
231 |
'can_export' => false, |
|
232 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
233 |
'supports' => array(), |
|
234 |
) |
|
235 |
); |
|
236 |
||
237 |
register_post_type( |
|
238 |
'user_request', |
|
239 |
array( |
|
240 |
'labels' => array( |
|
241 |
'name' => __( 'User Requests' ), |
|
242 |
'singular_name' => __( 'User Request' ), |
|
243 |
), |
|
244 |
'public' => false, |
|
245 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
246 |
'hierarchical' => false, |
|
247 |
'rewrite' => false, |
|
248 |
'query_var' => false, |
|
249 |
'can_export' => false, |
|
250 |
'delete_with_user' => false, |
|
251 |
'supports' => array(), |
|
252 |
) |
|
253 |
); |
|
254 |
||
255 |
register_post_type( |
|
256 |
'wp_block', |
|
257 |
array( |
|
258 |
'labels' => array( |
|
259 |
'name' => _x( 'Blocks', 'post type general name' ), |
|
260 |
'singular_name' => _x( 'Block', 'post type singular name' ), |
|
261 |
'menu_name' => _x( 'Blocks', 'admin menu' ), |
|
262 |
'name_admin_bar' => _x( 'Block', 'add new on admin bar' ), |
|
263 |
'add_new' => _x( 'Add New', 'Block' ), |
|
264 |
'add_new_item' => __( 'Add New Block' ), |
|
265 |
'new_item' => __( 'New Block' ), |
|
266 |
'edit_item' => __( 'Edit Block' ), |
|
267 |
'view_item' => __( 'View Block' ), |
|
268 |
'all_items' => __( 'All Blocks' ), |
|
269 |
'search_items' => __( 'Search Blocks' ), |
|
270 |
'not_found' => __( 'No blocks found.' ), |
|
271 |
'not_found_in_trash' => __( 'No blocks found in Trash.' ), |
|
272 |
'filter_items_list' => __( 'Filter blocks list' ), |
|
273 |
'items_list_navigation' => __( 'Blocks list navigation' ), |
|
274 |
'items_list' => __( 'Blocks list' ), |
|
275 |
'item_published' => __( 'Block published.' ), |
|
276 |
'item_published_privately' => __( 'Block published privately.' ), |
|
277 |
'item_reverted_to_draft' => __( 'Block reverted to draft.' ), |
|
278 |
'item_scheduled' => __( 'Block scheduled.' ), |
|
279 |
'item_updated' => __( 'Block updated.' ), |
|
280 |
), |
|
281 |
'public' => false, |
|
282 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
283 |
'show_ui' => true, |
|
284 |
'show_in_menu' => false, |
|
285 |
'rewrite' => false, |
|
286 |
'show_in_rest' => true, |
|
287 |
'rest_base' => 'blocks', |
|
288 |
'rest_controller_class' => 'WP_REST_Blocks_Controller', |
|
289 |
'capability_type' => 'block', |
|
290 |
'capabilities' => array( |
|
291 |
// You need to be able to edit posts, in order to read blocks in their raw form. |
|
292 |
'read' => 'edit_posts', |
|
293 |
// You need to be able to publish posts, in order to create blocks. |
|
294 |
'create_posts' => 'publish_posts', |
|
295 |
'edit_posts' => 'edit_posts', |
|
296 |
'edit_published_posts' => 'edit_published_posts', |
|
297 |
'delete_published_posts' => 'delete_published_posts', |
|
298 |
'edit_others_posts' => 'edit_others_posts', |
|
299 |
'delete_others_posts' => 'delete_others_posts', |
|
300 |
), |
|
301 |
'map_meta_cap' => true, |
|
302 |
'supports' => array( |
|
303 |
'title', |
|
304 |
'editor', |
|
305 |
), |
|
306 |
) |
|
307 |
); |
|
308 |
||
309 |
register_post_status( |
|
310 |
'publish', |
|
311 |
array( |
|
312 |
'label' => _x( 'Published', 'post status' ), |
|
313 |
'public' => true, |
|
314 |
'_builtin' => true, /* internal use only. */ |
|
315 |
'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ), |
|
316 |
) |
|
317 |
); |
|
318 |
||
319 |
register_post_status( |
|
320 |
'future', |
|
321 |
array( |
|
322 |
'label' => _x( 'Scheduled', 'post status' ), |
|
323 |
'protected' => true, |
|
324 |
'_builtin' => true, /* internal use only. */ |
|
325 |
'label_count' => _n_noop( 'Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ), |
|
326 |
) |
|
327 |
); |
|
328 |
||
329 |
register_post_status( |
|
330 |
'draft', |
|
331 |
array( |
|
332 |
'label' => _x( 'Draft', 'post status' ), |
|
333 |
'protected' => true, |
|
334 |
'_builtin' => true, /* internal use only. */ |
|
335 |
'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ), |
|
336 |
) |
|
337 |
); |
|
338 |
||
339 |
register_post_status( |
|
340 |
'pending', |
|
341 |
array( |
|
342 |
'label' => _x( 'Pending', 'post status' ), |
|
343 |
'protected' => true, |
|
344 |
'_builtin' => true, /* internal use only. */ |
|
345 |
'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ), |
|
346 |
) |
|
347 |
); |
|
348 |
||
349 |
register_post_status( |
|
350 |
'private', |
|
351 |
array( |
|
352 |
'label' => _x( 'Private', 'post status' ), |
|
353 |
'private' => true, |
|
354 |
'_builtin' => true, /* internal use only. */ |
|
355 |
'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ), |
|
356 |
) |
|
357 |
); |
|
358 |
||
359 |
register_post_status( |
|
360 |
'trash', |
|
361 |
array( |
|
362 |
'label' => _x( 'Trash', 'post status' ), |
|
363 |
'internal' => true, |
|
364 |
'_builtin' => true, /* internal use only. */ |
|
365 |
'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ), |
|
366 |
'show_in_admin_status_list' => true, |
|
367 |
) |
|
368 |
); |
|
369 |
||
370 |
register_post_status( |
|
371 |
'auto-draft', |
|
372 |
array( |
|
373 |
'label' => 'auto-draft', |
|
374 |
'internal' => true, |
|
375 |
'_builtin' => true, /* internal use only. */ |
|
376 |
) |
|
377 |
); |
|
378 |
||
379 |
register_post_status( |
|
380 |
'inherit', |
|
381 |
array( |
|
382 |
'label' => 'inherit', |
|
383 |
'internal' => true, |
|
384 |
'_builtin' => true, /* internal use only. */ |
|
385 |
'exclude_from_search' => false, |
|
386 |
) |
|
387 |
); |
|
388 |
||
389 |
register_post_status( |
|
390 |
'request-pending', |
|
391 |
array( |
|
392 |
'label' => _x( 'Pending', 'request status' ), |
|
393 |
'internal' => true, |
|
394 |
'_builtin' => true, /* internal use only. */ |
|
395 |
'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ), |
|
396 |
'exclude_from_search' => false, |
|
397 |
) |
|
398 |
); |
|
399 |
||
400 |
register_post_status( |
|
401 |
'request-confirmed', |
|
402 |
array( |
|
403 |
'label' => _x( 'Confirmed', 'request status' ), |
|
404 |
'internal' => true, |
|
405 |
'_builtin' => true, /* internal use only. */ |
|
406 |
'label_count' => _n_noop( 'Confirmed <span class="count">(%s)</span>', 'Confirmed <span class="count">(%s)</span>' ), |
|
407 |
'exclude_from_search' => false, |
|
408 |
) |
|
409 |
); |
|
410 |
||
411 |
register_post_status( |
|
412 |
'request-failed', |
|
413 |
array( |
|
414 |
'label' => _x( 'Failed', 'request status' ), |
|
415 |
'internal' => true, |
|
416 |
'_builtin' => true, /* internal use only. */ |
|
417 |
'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>' ), |
|
418 |
'exclude_from_search' => false, |
|
419 |
) |
|
420 |
); |
|
421 |
||
422 |
register_post_status( |
|
423 |
'request-completed', |
|
424 |
array( |
|
425 |
'label' => _x( 'Completed', 'request status' ), |
|
426 |
'internal' => true, |
|
427 |
'_builtin' => true, /* internal use only. */ |
|
428 |
'label_count' => _n_noop( 'Completed <span class="count">(%s)</span>', 'Completed <span class="count">(%s)</span>' ), |
|
429 |
'exclude_from_search' => false, |
|
430 |
) |
|
431 |
); |
|
0 | 432 |
} |
433 |
||
434 |
/** |
|
435 |
* Retrieve attached file path based on attachment ID. |
|
436 |
* |
|
437 |
* By default the path will go through the 'get_attached_file' filter, but |
|
438 |
* passing a true to the $unfiltered argument of get_attached_file() will |
|
439 |
* return the file path unfiltered. |
|
440 |
* |
|
441 |
* The function works by getting the single post meta name, named |
|
442 |
* '_wp_attached_file' and returning it. This is a convenience function to |
|
443 |
* prevent looking up the meta name and provide a mechanism for sending the |
|
444 |
* attached filename through a filter. |
|
445 |
* |
|
446 |
* @since 2.0.0 |
|
447 |
* |
|
5 | 448 |
* @param int $attachment_id Attachment ID. |
449 |
* @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
|
450 |
* @return string|false The file path to where the attached file should be, false otherwise. |
0 | 451 |
*/ |
452 |
function get_attached_file( $attachment_id, $unfiltered = false ) { |
|
453 |
$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
|
454 |
|
5 | 455 |
// If the file is relative, prepend upload dir. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
456 |
if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) && ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) ) { |
0 | 457 |
$file = $uploads['basedir'] . "/$file"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
458 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
459 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
460 |
if ( $unfiltered ) { |
0 | 461 |
return $file; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
462 |
} |
5 | 463 |
|
464 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
465 |
* Filters the attached file based on the given ID. |
5 | 466 |
* |
467 |
* @since 2.1.0 |
|
468 |
* |
|
469 |
* @param string $file Path to attached file. |
|
470 |
* @param int $attachment_id Attachment ID. |
|
471 |
*/ |
|
0 | 472 |
return apply_filters( 'get_attached_file', $file, $attachment_id ); |
473 |
} |
|
474 |
||
475 |
/** |
|
476 |
* Update attachment file path based on attachment ID. |
|
477 |
* |
|
478 |
* Used to update the file path of the attachment, which uses post meta name |
|
479 |
* '_wp_attached_file' to store the path of the attachment. |
|
480 |
* |
|
481 |
* @since 2.1.0 |
|
5 | 482 |
* |
483 |
* @param int $attachment_id Attachment ID. |
|
484 |
* @param string $file File path for the attachment. |
|
0 | 485 |
* @return bool True on success, false on failure. |
486 |
*/ |
|
487 |
function update_attached_file( $attachment_id, $file ) { |
|
9 | 488 |
if ( ! get_post( $attachment_id ) ) { |
0 | 489 |
return false; |
9 | 490 |
} |
0 | 491 |
|
5 | 492 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
493 |
* Filters the path to the attached file to update. |
5 | 494 |
* |
495 |
* @since 2.1.0 |
|
496 |
* |
|
497 |
* @param string $file Path to the attached file to update. |
|
498 |
* @param int $attachment_id Attachment ID. |
|
499 |
*/ |
|
0 | 500 |
$file = apply_filters( 'update_attached_file', $file, $attachment_id ); |
5 | 501 |
|
9 | 502 |
if ( $file = _wp_relative_upload_path( $file ) ) { |
0 | 503 |
return update_post_meta( $attachment_id, '_wp_attached_file', $file ); |
9 | 504 |
} else { |
0 | 505 |
return delete_post_meta( $attachment_id, '_wp_attached_file' ); |
9 | 506 |
} |
0 | 507 |
} |
508 |
||
509 |
/** |
|
510 |
* Return relative path to an uploaded file. |
|
511 |
* |
|
512 |
* The path is relative to the current upload dir. |
|
513 |
* |
|
514 |
* @since 2.9.0 |
|
9 | 515 |
* @access private |
5 | 516 |
* |
517 |
* @param string $path Full path to the file. |
|
518 |
* @return string Relative path on success, unchanged path on failure. |
|
0 | 519 |
*/ |
520 |
function _wp_relative_upload_path( $path ) { |
|
521 |
$new_path = $path; |
|
522 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
$uploads = wp_get_upload_dir(); |
0 | 524 |
if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { |
525 |
$new_path = str_replace( $uploads['basedir'], '', $new_path ); |
|
526 |
$new_path = ltrim( $new_path, '/' ); |
|
527 |
} |
|
528 |
||
5 | 529 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
530 |
* Filters the relative path to an uploaded file. |
5 | 531 |
* |
532 |
* @since 2.9.0 |
|
533 |
* |
|
534 |
* @param string $new_path Relative path to the file. |
|
535 |
* @param string $path Full path to the file. |
|
536 |
*/ |
|
0 | 537 |
return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
538 |
} |
|
539 |
||
540 |
/** |
|
541 |
* Retrieve all children of the post parent ID. |
|
542 |
* |
|
543 |
* Normally, without any enhancements, the children would apply to pages. In the |
|
544 |
* context of the inner workings of WordPress, pages, posts, and attachments |
|
545 |
* share the same table, so therefore the functionality could apply to any one |
|
546 |
* of them. It is then noted that while this function does not work on posts, it |
|
547 |
* does not mean that it won't work on posts. It is recommended that you know |
|
548 |
* what context you wish to retrieve the children of. |
|
549 |
* |
|
550 |
* Attachments may also be made the child of a post, so if that is an accurate |
|
551 |
* statement (which needs to be verified), it would then be possible to get |
|
552 |
* 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
|
553 |
* version 2.5, so this is most likely inaccurate, but serves generally as an |
0 | 554 |
* example of what is possible. |
555 |
* |
|
556 |
* 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
|
557 |
* 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
|
558 |
* 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
|
559 |
* 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
|
560 |
* arguments listed in the get_posts() function. |
0 | 561 |
* |
562 |
* The 'post_parent' is the most important argument and important attention |
|
563 |
* needs to be paid to the $args parameter. If you pass either an object or an |
|
564 |
* integer (number), then just the 'post_parent' is grabbed and everything else |
|
565 |
* is lost. If you don't specify any arguments, then it is assumed that you are |
|
566 |
* in The Loop and the post parent will be grabbed for from the current post. |
|
567 |
* |
|
568 |
* The 'post_parent' argument is the ID to get the children. The 'numberposts' |
|
569 |
* is the amount of posts to retrieve that has a default of '-1', which is |
|
570 |
* used to get all of the posts. Giving a number higher than 0 will only |
|
571 |
* retrieve that amount of posts. |
|
572 |
* |
|
573 |
* The 'post_type' and 'post_status' arguments can be used to choose what |
|
574 |
* criteria of posts to retrieve. The 'post_type' can be anything, but WordPress |
|
575 |
* post types are 'post', 'pages', and 'attachments'. The 'post_status' |
|
576 |
* argument will accept any post status within the write administration panels. |
|
577 |
* |
|
578 |
* @since 2.0.0 |
|
579 |
* |
|
5 | 580 |
* @see get_posts() |
581 |
* @todo Check validity of description. |
|
582 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
583 |
* @global WP_Post $post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
584 |
* |
5 | 585 |
* @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
586 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
587 |
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
5 | 588 |
* @return array Array of children, where the type of each element is determined by $output parameter. |
589 |
* Empty array on failure. |
|
0 | 590 |
*/ |
5 | 591 |
function get_children( $args = '', $output = OBJECT ) { |
0 | 592 |
$kids = array(); |
593 |
if ( empty( $args ) ) { |
|
594 |
if ( isset( $GLOBALS['post'] ) ) { |
|
9 | 595 |
$args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent ); |
0 | 596 |
} else { |
597 |
return $kids; |
|
598 |
} |
|
599 |
} elseif ( is_object( $args ) ) { |
|
9 | 600 |
$args = array( 'post_parent' => (int) $args->post_parent ); |
0 | 601 |
} elseif ( is_numeric( $args ) ) { |
9 | 602 |
$args = array( 'post_parent' => (int) $args ); |
0 | 603 |
} |
604 |
||
605 |
$defaults = array( |
|
9 | 606 |
'numberposts' => -1, |
607 |
'post_type' => 'any', |
|
608 |
'post_status' => 'any', |
|
609 |
'post_parent' => 0, |
|
0 | 610 |
); |
611 |
||
612 |
$r = wp_parse_args( $args, $defaults ); |
|
613 |
||
614 |
$children = get_posts( $r ); |
|
615 |
||
9 | 616 |
if ( ! $children ) { |
0 | 617 |
return $kids; |
9 | 618 |
} |
619 |
||
620 |
if ( ! empty( $r['fields'] ) ) { |
|
0 | 621 |
return $children; |
9 | 622 |
} |
623 |
||
624 |
update_post_cache( $children ); |
|
625 |
||
626 |
foreach ( $children as $key => $child ) { |
|
627 |
$kids[ $child->ID ] = $children[ $key ]; |
|
628 |
} |
|
0 | 629 |
|
630 |
if ( $output == OBJECT ) { |
|
631 |
return $kids; |
|
632 |
} elseif ( $output == ARRAY_A ) { |
|
5 | 633 |
$weeuns = array(); |
634 |
foreach ( (array) $kids as $kid ) { |
|
9 | 635 |
$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] ); |
5 | 636 |
} |
0 | 637 |
return $weeuns; |
638 |
} elseif ( $output == ARRAY_N ) { |
|
5 | 639 |
$babes = array(); |
640 |
foreach ( (array) $kids as $kid ) { |
|
9 | 641 |
$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) ); |
5 | 642 |
} |
0 | 643 |
return $babes; |
644 |
} else { |
|
645 |
return $kids; |
|
646 |
} |
|
647 |
} |
|
648 |
||
649 |
/** |
|
650 |
* Get extended entry info (<!--more-->). |
|
651 |
* |
|
652 |
* There should not be any space after the second dash and before the word |
|
653 |
* 'more'. There can be text or space(s) after the word 'more', but won't be |
|
654 |
* referenced. |
|
655 |
* |
|
656 |
* The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before |
|
5 | 657 |
* the `<!--more-->`. The 'extended' key has the content after the |
658 |
* `<!--more-->` comment. The 'more_text' key has the custom "Read More" text. |
|
0 | 659 |
* |
660 |
* @since 1.0.0 |
|
661 |
* |
|
662 |
* @param string $post Post content. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
663 |
* @return array Post before ('main'), after ('extended'), and custom read more ('more_text'). |
0 | 664 |
*/ |
5 | 665 |
function get_extended( $post ) { |
666 |
//Match the new style more links. |
|
9 | 667 |
if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) { |
668 |
list($main, $extended) = explode( $matches[0], $post, 2 ); |
|
669 |
$more_text = $matches[1]; |
|
0 | 670 |
} else { |
9 | 671 |
$main = $post; |
672 |
$extended = ''; |
|
0 | 673 |
$more_text = ''; |
674 |
} |
|
675 |
||
5 | 676 |
// leading and trailing whitespace. |
9 | 677 |
$main = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main ); |
678 |
$extended = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended ); |
|
679 |
$more_text = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $more_text ); |
|
680 |
||
681 |
return array( |
|
682 |
'main' => $main, |
|
683 |
'extended' => $extended, |
|
684 |
'more_text' => $more_text, |
|
685 |
); |
|
0 | 686 |
} |
687 |
||
688 |
/** |
|
689 |
* Retrieves post data given a post ID or post object. |
|
690 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
691 |
* 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
|
692 |
* `$post`, must be given as a variable, since it is passed by reference. |
0 | 693 |
* |
694 |
* @since 1.5.1 |
|
5 | 695 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
696 |
* @global WP_Post $post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
697 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
698 |
* @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
|
699 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
700 |
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
701 |
* @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
|
702 |
* or 'display'. Default 'raw'. |
5 | 703 |
* @return WP_Post|array|null Type corresponding to $output on success or null on failure. |
704 |
* When $output is OBJECT, a `WP_Post` instance is returned. |
|
0 | 705 |
*/ |
706 |
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { |
|
9 | 707 |
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) { |
0 | 708 |
$post = $GLOBALS['post']; |
9 | 709 |
} |
0 | 710 |
|
5 | 711 |
if ( $post instanceof WP_Post ) { |
0 | 712 |
$_post = $post; |
713 |
} elseif ( is_object( $post ) ) { |
|
714 |
if ( empty( $post->filter ) ) { |
|
715 |
$_post = sanitize_post( $post, 'raw' ); |
|
716 |
$_post = new WP_Post( $_post ); |
|
717 |
} elseif ( 'raw' == $post->filter ) { |
|
718 |
$_post = new WP_Post( $post ); |
|
719 |
} else { |
|
720 |
$_post = WP_Post::get_instance( $post->ID ); |
|
721 |
} |
|
722 |
} else { |
|
723 |
$_post = WP_Post::get_instance( $post ); |
|
724 |
} |
|
725 |
||
9 | 726 |
if ( ! $_post ) { |
0 | 727 |
return null; |
9 | 728 |
} |
0 | 729 |
|
730 |
$_post = $_post->filter( $filter ); |
|
731 |
||
9 | 732 |
if ( $output == ARRAY_A ) { |
0 | 733 |
return $_post->to_array(); |
9 | 734 |
} elseif ( $output == ARRAY_N ) { |
0 | 735 |
return array_values( $_post->to_array() ); |
9 | 736 |
} |
0 | 737 |
|
738 |
return $_post; |
|
739 |
} |
|
740 |
||
741 |
/** |
|
742 |
* Retrieve ancestors of a post. |
|
743 |
* |
|
744 |
* @since 2.5.0 |
|
745 |
* |
|
5 | 746 |
* @param int|WP_Post $post Post ID or post object. |
0 | 747 |
* @return array Ancestor IDs or empty array if none are found. |
748 |
*/ |
|
749 |
function get_post_ancestors( $post ) { |
|
750 |
$post = get_post( $post ); |
|
751 |
||
9 | 752 |
if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) { |
0 | 753 |
return array(); |
9 | 754 |
} |
0 | 755 |
|
756 |
$ancestors = array(); |
|
757 |
||
758 |
$id = $ancestors[] = $post->post_parent; |
|
759 |
||
760 |
while ( $ancestor = get_post( $id ) ) { |
|
761 |
// Loop detection: If the ancestor has been seen before, break. |
|
9 | 762 |
if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) ) { |
0 | 763 |
break; |
9 | 764 |
} |
0 | 765 |
|
766 |
$id = $ancestors[] = $ancestor->post_parent; |
|
767 |
} |
|
768 |
||
769 |
return $ancestors; |
|
770 |
} |
|
771 |
||
772 |
/** |
|
773 |
* Retrieve data from a post field based on Post ID. |
|
774 |
* |
|
775 |
* Examples of the post field will be, 'post_type', 'post_status', 'post_content', |
|
776 |
* etc and based off of the post object property or key names. |
|
777 |
* |
|
778 |
* The context values are based off of the taxonomy filter functions and |
|
779 |
* supported values are found within those functions. |
|
780 |
* |
|
781 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
782 |
* @since 4.5.0 The `$post` parameter was made optional. |
5 | 783 |
* |
784 |
* @see sanitize_post_field() |
|
785 |
* |
|
786 |
* @param string $field Post field name. |
|
9 | 787 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 788 |
* @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db', |
789 |
* or 'display'. Default 'display'. |
|
0 | 790 |
* @return string The value of the post field on success, empty string on failure. |
791 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
792 |
function get_post_field( $field, $post = null, $context = 'display' ) { |
0 | 793 |
$post = get_post( $post ); |
794 |
||
9 | 795 |
if ( ! $post ) { |
0 | 796 |
return ''; |
9 | 797 |
} |
798 |
||
799 |
if ( ! isset( $post->$field ) ) { |
|
0 | 800 |
return ''; |
9 | 801 |
} |
802 |
||
803 |
return sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|
0 | 804 |
} |
805 |
||
806 |
/** |
|
807 |
* Retrieve the mime type of an attachment based on the ID. |
|
808 |
* |
|
809 |
* This function can be used with any post type, but it makes more sense with |
|
810 |
* attachments. |
|
811 |
* |
|
812 |
* @since 2.0.0 |
|
813 |
* |
|
9 | 814 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 815 |
* @return string|false The mime type on success, false on failure. |
0 | 816 |
*/ |
9 | 817 |
function get_post_mime_type( $post = null ) { |
818 |
$post = get_post( $post ); |
|
819 |
||
820 |
if ( is_object( $post ) ) { |
|
0 | 821 |
return $post->post_mime_type; |
9 | 822 |
} |
0 | 823 |
|
824 |
return false; |
|
825 |
} |
|
826 |
||
827 |
/** |
|
9 | 828 |
* Retrieve the post status based on the post ID. |
0 | 829 |
* |
830 |
* If the post ID is of an attachment, then the parent post status will be given |
|
831 |
* instead. |
|
832 |
* |
|
833 |
* @since 2.0.0 |
|
834 |
* |
|
9 | 835 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.. |
5 | 836 |
* @return string|false Post status on success, false on failure. |
0 | 837 |
*/ |
9 | 838 |
function get_post_status( $post = null ) { |
839 |
$post = get_post( $post ); |
|
840 |
||
841 |
if ( ! is_object( $post ) ) { |
|
0 | 842 |
return false; |
9 | 843 |
} |
0 | 844 |
|
845 |
if ( 'attachment' == $post->post_type ) { |
|
9 | 846 |
if ( 'private' == $post->post_status ) { |
0 | 847 |
return 'private'; |
9 | 848 |
} |
0 | 849 |
|
5 | 850 |
// Unattached attachments are assumed to be published. |
9 | 851 |
if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent ) ) { |
0 | 852 |
return 'publish'; |
9 | 853 |
} |
0 | 854 |
|
5 | 855 |
// Inherit status from the parent. |
856 |
if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) { |
|
857 |
$parent_post_status = get_post_status( $post->post_parent ); |
|
858 |
if ( 'trash' == $parent_post_status ) { |
|
859 |
return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); |
|
860 |
} else { |
|
861 |
return $parent_post_status; |
|
862 |
} |
|
863 |
} |
|
0 | 864 |
} |
865 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
866 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
867 |
* Filters the post status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
868 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
869 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
870 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
871 |
* @param string $post_status The post status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
872 |
* @param WP_Post $post The post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
873 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
874 |
return apply_filters( 'get_post_status', $post->post_status, $post ); |
0 | 875 |
} |
876 |
||
877 |
/** |
|
878 |
* Retrieve all of the WordPress supported post statuses. |
|
879 |
* |
|
880 |
* Posts have a limited set of valid status values, this provides the |
|
881 |
* post_status values and descriptions. |
|
882 |
* |
|
883 |
* @since 2.5.0 |
|
884 |
* |
|
885 |
* @return array List of post statuses. |
|
886 |
*/ |
|
887 |
function get_post_statuses() { |
|
888 |
$status = array( |
|
5 | 889 |
'draft' => __( 'Draft' ), |
890 |
'pending' => __( 'Pending Review' ), |
|
891 |
'private' => __( 'Private' ), |
|
9 | 892 |
'publish' => __( 'Published' ), |
0 | 893 |
); |
894 |
||
895 |
return $status; |
|
896 |
} |
|
897 |
||
898 |
/** |
|
899 |
* Retrieve all of the WordPress support page statuses. |
|
900 |
* |
|
901 |
* Pages have a limited set of valid status values, this provides the |
|
902 |
* post_status values and descriptions. |
|
903 |
* |
|
904 |
* @since 2.5.0 |
|
905 |
* |
|
906 |
* @return array List of page statuses. |
|
907 |
*/ |
|
908 |
function get_page_statuses() { |
|
909 |
$status = array( |
|
5 | 910 |
'draft' => __( 'Draft' ), |
911 |
'private' => __( 'Private' ), |
|
9 | 912 |
'publish' => __( 'Published' ), |
0 | 913 |
); |
914 |
||
915 |
return $status; |
|
916 |
} |
|
917 |
||
918 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
919 |
* Return statuses for privacy requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
920 |
* |
9 | 921 |
* @since 4.9.6 |
922 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
923 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
924 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
925 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
926 |
function _wp_privacy_statuses() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
return array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
'request-pending' => __( 'Pending' ), // Pending confirmation from user. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
'request-confirmed' => __( 'Confirmed' ), // User has confirmed the action. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
'request-failed' => __( 'Failed' ), // User failed to confirm the action. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
'request-completed' => __( 'Completed' ), // Admin has handled the request. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
932 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
933 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
934 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
935 |
/** |
0 | 936 |
* Register a post status. Do not use before init. |
937 |
* |
|
938 |
* A simple function for creating or modifying a post status based on the |
|
939 |
* parameters given. The function will accept an array (second optional |
|
940 |
* parameter), along with a string for the post status name. |
|
941 |
* |
|
942 |
* Arguments prefixed with an _underscore shouldn't be used by plugins and themes. |
|
943 |
* |
|
944 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
945 |
* @global array $wp_post_statuses Inserts new post status object into the list |
0 | 946 |
* |
947 |
* @param string $post_status Name of the post status. |
|
5 | 948 |
* @param array|string $args { |
949 |
* Optional. Array or string of post status arguments. |
|
950 |
* |
|
951 |
* @type bool|string $label A descriptive name for the post status marked |
|
952 |
* for translation. Defaults to value of $post_status. |
|
953 |
* @type bool|array $label_count Descriptive text to use for nooped plurals. |
|
954 |
* Default array of $label, twice |
|
955 |
* @type bool $exclude_from_search Whether to exclude posts with this post status |
|
956 |
* from search results. Default is value of $internal. |
|
957 |
* @type bool $_builtin Whether the status is built-in. Core-use only. |
|
958 |
* Default false. |
|
959 |
* @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
|
960 |
* in the front end of the site. Default false. |
5 | 961 |
* @type bool $internal Whether the status is for internal use only. |
962 |
* Default false. |
|
963 |
* @type bool $protected Whether posts with this status should be protected. |
|
964 |
* Default false. |
|
965 |
* @type bool $private Whether posts with this status should be private. |
|
966 |
* Default false. |
|
967 |
* @type bool $publicly_queryable Whether posts with this status should be publicly- |
|
968 |
* queryable. Default is value of $public. |
|
969 |
* @type bool $show_in_admin_all_list Whether to include posts in the edit listing for |
|
970 |
* their post type. Default is value of $internal. |
|
971 |
* @type bool $show_in_admin_status_list Show in the list of statuses with post counts at |
|
972 |
* the top of the edit listings, |
|
973 |
* e.g. All (12) | Published (9) | My Custom Status (2) |
|
974 |
* Default is value of $internal. |
|
975 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
976 |
* @return object |
0 | 977 |
*/ |
5 | 978 |
function register_post_status( $post_status, $args = array() ) { |
0 | 979 |
global $wp_post_statuses; |
980 |
||
9 | 981 |
if ( ! is_array( $wp_post_statuses ) ) { |
0 | 982 |
$wp_post_statuses = array(); |
9 | 983 |
} |
0 | 984 |
|
985 |
// Args prefixed with an underscore are reserved for internal use. |
|
986 |
$defaults = array( |
|
9 | 987 |
'label' => false, |
988 |
'label_count' => false, |
|
989 |
'exclude_from_search' => null, |
|
990 |
'_builtin' => false, |
|
991 |
'public' => null, |
|
992 |
'internal' => null, |
|
993 |
'protected' => null, |
|
994 |
'private' => null, |
|
995 |
'publicly_queryable' => null, |
|
0 | 996 |
'show_in_admin_status_list' => null, |
9 | 997 |
'show_in_admin_all_list' => null, |
0 | 998 |
); |
9 | 999 |
$args = wp_parse_args( $args, $defaults ); |
1000 |
$args = (object) $args; |
|
1001 |
||
1002 |
$post_status = sanitize_key( $post_status ); |
|
1003 |
$args->name = $post_status; |
|
0 | 1004 |
|
5 | 1005 |
// Set various defaults. |
9 | 1006 |
if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) { |
0 | 1007 |
$args->internal = true; |
9 | 1008 |
} |
1009 |
||
1010 |
if ( null === $args->public ) { |
|
0 | 1011 |
$args->public = false; |
9 | 1012 |
} |
1013 |
||
1014 |
if ( null === $args->private ) { |
|
0 | 1015 |
$args->private = false; |
9 | 1016 |
} |
1017 |
||
1018 |
if ( null === $args->protected ) { |
|
0 | 1019 |
$args->protected = false; |
9 | 1020 |
} |
1021 |
||
1022 |
if ( null === $args->internal ) { |
|
0 | 1023 |
$args->internal = false; |
9 | 1024 |
} |
1025 |
||
1026 |
if ( null === $args->publicly_queryable ) { |
|
0 | 1027 |
$args->publicly_queryable = $args->public; |
9 | 1028 |
} |
1029 |
||
1030 |
if ( null === $args->exclude_from_search ) { |
|
0 | 1031 |
$args->exclude_from_search = $args->internal; |
9 | 1032 |
} |
1033 |
||
1034 |
if ( null === $args->show_in_admin_all_list ) { |
|
1035 |
$args->show_in_admin_all_list = ! $args->internal; |
|
1036 |
} |
|
1037 |
||
1038 |
if ( null === $args->show_in_admin_status_list ) { |
|
1039 |
$args->show_in_admin_status_list = ! $args->internal; |
|
1040 |
} |
|
1041 |
||
1042 |
if ( false === $args->label ) { |
|
0 | 1043 |
$args->label = $post_status; |
9 | 1044 |
} |
1045 |
||
1046 |
if ( false === $args->label_count ) { |
|
1047 |
// 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
|
1048 |
$args->label_count = _n_noop( $args->label, $args->label ); |
9 | 1049 |
} |
1050 |
||
1051 |
$wp_post_statuses[ $post_status ] = $args; |
|
0 | 1052 |
|
1053 |
return $args; |
|
1054 |
} |
|
1055 |
||
1056 |
/** |
|
5 | 1057 |
* Retrieve a post status object by name. |
1058 |
* |
|
0 | 1059 |
* @since 3.0.0 |
5 | 1060 |
* |
1061 |
* @global array $wp_post_statuses List of post statuses. |
|
1062 |
* |
|
1063 |
* @see register_post_status() |
|
1064 |
* |
|
1065 |
* @param string $post_status The name of a registered post status. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1066 |
* @return object|null A post status object. |
0 | 1067 |
*/ |
1068 |
function get_post_status_object( $post_status ) { |
|
1069 |
global $wp_post_statuses; |
|
1070 |
||
9 | 1071 |
if ( empty( $wp_post_statuses[ $post_status ] ) ) { |
0 | 1072 |
return null; |
9 | 1073 |
} |
1074 |
||
1075 |
return $wp_post_statuses[ $post_status ]; |
|
0 | 1076 |
} |
1077 |
||
1078 |
/** |
|
5 | 1079 |
* Get a list of post statuses. |
1080 |
* |
|
0 | 1081 |
* @since 3.0.0 |
5 | 1082 |
* |
1083 |
* @global array $wp_post_statuses List of post statuses. |
|
1084 |
* |
|
1085 |
* @see register_post_status() |
|
1086 |
* |
|
1087 |
* @param array|string $args Optional. Array or string of post status arguments to compare against |
|
1088 |
* properties of the global `$wp_post_statuses objects`. Default empty array. |
|
1089 |
* @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'. |
|
1090 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one element |
|
1091 |
* from the array needs to match; 'and' means all elements must match. |
|
1092 |
* Default 'and'. |
|
1093 |
* @return array A list of post status names or objects. |
|
0 | 1094 |
*/ |
1095 |
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { |
|
1096 |
global $wp_post_statuses; |
|
1097 |
||
9 | 1098 |
$field = ( 'names' == $output ) ? 'name' : false; |
1099 |
||
1100 |
return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field ); |
|
0 | 1101 |
} |
1102 |
||
1103 |
/** |
|
1104 |
* Whether the post type is hierarchical. |
|
1105 |
* |
|
1106 |
* A false return value might also mean that the post type does not exist. |
|
1107 |
* |
|
1108 |
* @since 3.0.0 |
|
5 | 1109 |
* |
1110 |
* @see get_post_type_object() |
|
0 | 1111 |
* |
1112 |
* @param string $post_type Post type name |
|
1113 |
* @return bool Whether post type is hierarchical. |
|
1114 |
*/ |
|
1115 |
function is_post_type_hierarchical( $post_type ) { |
|
9 | 1116 |
if ( ! post_type_exists( $post_type ) ) { |
0 | 1117 |
return false; |
9 | 1118 |
} |
0 | 1119 |
|
1120 |
$post_type = get_post_type_object( $post_type ); |
|
1121 |
return $post_type->hierarchical; |
|
1122 |
} |
|
1123 |
||
1124 |
/** |
|
9 | 1125 |
* Determines whether a post type is registered. |
1126 |
* |
|
1127 |
* For more information on this and similar theme functions, check out |
|
1128 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1129 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 1130 |
* |
1131 |
* @since 3.0.0 |
|
5 | 1132 |
* |
1133 |
* @see get_post_type_object() |
|
1134 |
* |
|
1135 |
* @param string $post_type Post type name. |
|
0 | 1136 |
* @return bool Whether post type is registered. |
1137 |
*/ |
|
1138 |
function post_type_exists( $post_type ) { |
|
1139 |
return (bool) get_post_type_object( $post_type ); |
|
1140 |
} |
|
1141 |
||
1142 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1143 |
* Retrieves the post type of the current post or of a given post. |
0 | 1144 |
* |
1145 |
* @since 2.1.0 |
|
1146 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1147 |
* @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
|
1148 |
* @return string|false Post type on success, false on failure. |
0 | 1149 |
*/ |
1150 |
function get_post_type( $post = null ) { |
|
9 | 1151 |
if ( $post = get_post( $post ) ) { |
0 | 1152 |
return $post->post_type; |
9 | 1153 |
} |
0 | 1154 |
|
1155 |
return false; |
|
1156 |
} |
|
1157 |
||
1158 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1159 |
* Retrieves a post type object by name. |
5 | 1160 |
* |
0 | 1161 |
* @since 3.0.0 |
9 | 1162 |
* @since 4.6.0 Object returned is now an instance of `WP_Post_Type`. |
5 | 1163 |
* |
1164 |
* @global array $wp_post_types List of post types. |
|
1165 |
* |
|
1166 |
* @see register_post_type() |
|
1167 |
* |
|
1168 |
* @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
|
1169 |
* @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. |
0 | 1170 |
*/ |
1171 |
function get_post_type_object( $post_type ) { |
|
1172 |
global $wp_post_types; |
|
1173 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1174 |
if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { |
0 | 1175 |
return null; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1176 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1177 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1178 |
return $wp_post_types[ $post_type ]; |
0 | 1179 |
} |
1180 |
||
1181 |
/** |
|
1182 |
* Get a list of all registered post type objects. |
|
1183 |
* |
|
1184 |
* @since 2.9.0 |
|
5 | 1185 |
* |
1186 |
* @global array $wp_post_types List of post types. |
|
1187 |
* |
|
1188 |
* @see register_post_type() for accepted arguments. |
|
1189 |
* |
|
1190 |
* @param array|string $args Optional. An array of key => value arguments to match against |
|
1191 |
* the post type objects. Default empty array. |
|
1192 |
* @param string $output Optional. The type of output to return. Accepts post type 'names' |
|
1193 |
* or 'objects'. Default 'names'. |
|
1194 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one |
|
1195 |
* 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
|
1196 |
* must match; 'not' means no elements may match. Default 'and'. |
9 | 1197 |
* @return string[]|WP_Post_Type[] An array of post type names or objects. |
0 | 1198 |
*/ |
1199 |
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { |
|
1200 |
global $wp_post_types; |
|
1201 |
||
9 | 1202 |
$field = ( 'names' == $output ) ? 'name' : false; |
1203 |
||
1204 |
return wp_filter_object_list( $wp_post_types, $args, $operator, $field ); |
|
0 | 1205 |
} |
1206 |
||
1207 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1208 |
* Registers a post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1209 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1210 |
* 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
|
1211 |
* {@see 'init'} action. Also, any taxonomy connections should be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1212 |
* registered via the `$taxonomies` argument to ensure consistency |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1213 |
* 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
|
1214 |
* are used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1216 |
* 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
|
1217 |
* as meta boxes, custom fields, post thumbnails, post statuses, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1218 |
* 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
|
1219 |
* list of supported features. |
0 | 1220 |
* |
1221 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1222 |
* @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
|
1223 |
* @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
|
1224 |
* screen and post editing screen. |
9 | 1225 |
* @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`. |
1226 |
* @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
|
1227 |
* arguments to register the post type in REST API. |
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 |
* @global array $wp_post_types List of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1230 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1231 |
* @param string $post_type Post type key. Must not exceed 20 characters and may |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1232 |
* only contain lowercase alphanumeric characters, dashes, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1233 |
* and underscores. See sanitize_key(). |
5 | 1234 |
* @param array|string $args { |
1235 |
* Array or string of arguments for registering a post type. |
|
1236 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1237 |
* @type string $label Name of the post type shown in the menu. Usually plural. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1238 |
* Default is value of $labels['name']. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1239 |
* @type array $labels An array of labels for this post type. If not set, post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1240 |
* labels are inherited for non-hierarchical types and page |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1241 |
* labels for hierarchical ones. See get_post_type_labels() for a full |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1242 |
* list of supported labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
* @type string $description A short descriptive summary of what the post type is. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1244 |
* Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
* @type bool $public Whether a post type is intended for use publicly either via |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
* the admin interface or by front-end users. While the default |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
* settings of $exclude_from_search, $publicly_queryable, $show_ui, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1248 |
* and $show_in_nav_menus are inherited from public, each does not |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1249 |
* rely on this relationship and controls a very specific intention. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1250 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1251 |
* @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1252 |
* @type bool $exclude_from_search Whether to exclude posts with this post type from front end search |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1253 |
* results. Default is the opposite value of $public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1254 |
* @type bool $publicly_queryable Whether queries can be performed on the front end for the post type |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1255 |
* as part of parse_request(). Endpoints would include: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1256 |
* * ?post_type={post_type_key} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1257 |
* * ?{post_type_key}={single_post_slug} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1258 |
* * ?{post_type_query_var}={single_post_slug} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1259 |
* If not set, the default is inherited from $public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1260 |
* @type bool $show_ui Whether to generate and allow a UI for managing this post type in the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1261 |
* admin. Default is value of $public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1262 |
* @type bool $show_in_menu Where to show the post type in the admin menu. To work, $show_ui |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1263 |
* must be true. If true, the post type is shown in its own top level |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1264 |
* menu. If false, no menu is shown. If a string of an existing top |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1265 |
* level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1266 |
* type will be placed as a sub-menu of that. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1267 |
* Default is value of $show_ui. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1268 |
* @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1269 |
* Default is value $public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1270 |
* @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1271 |
* of $show_in_menu. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1272 |
* @type bool $show_in_rest Whether to add the post type route in the REST API 'wp/v2' namespace. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1273 |
* @type string $rest_base To change the base url of REST API route. Default is $post_type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1274 |
* @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Posts_Controller'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1275 |
* @type int $menu_position The position in the menu order the post type should appear. To work, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1276 |
* $show_in_menu must be true. Default null (at the bottom). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
* @type string $menu_icon The url to the icon to be used for this menu. Pass a base64-encoded |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
* SVG using a data URI, which will be colored to match the color scheme |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* -- this should begin with 'data:image/svg+xml;base64,'. Pass the name |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1280 |
* of a Dashicons helper class to use a font icon, e.g. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
* 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
* so an icon can be added via CSS. Defaults to use the posts icon. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1283 |
* @type string $capability_type The string to use to build the read, edit, and delete capabilities. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
* May be passed as an array to allow for alternative plurals when using |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1285 |
* this argument as a base to construct the capabilities, e.g. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1286 |
* array('story', 'stories'). Default 'post'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
* @type array $capabilities Array of capabilities for this post type. $capability_type is used |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1288 |
* as a base to construct capabilities by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1289 |
* See get_post_type_capabilities(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1290 |
* @type bool $map_meta_cap Whether to use the internal default meta capability handling. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1292 |
* @type array $supports Core feature(s) the post type supports. Serves as an alias for calling |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1293 |
* add_post_type_support() directly. Core features include 'title', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1294 |
* 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1295 |
* 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1296 |
* Additionally, the 'revisions' feature dictates whether the post type |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1297 |
* will store revisions, and the 'comments' feature dictates whether the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1298 |
* comments count will show on the edit screen. Defaults is an array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
* containing 'title' and 'editor'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
* @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1301 |
* edit form. Do remove_meta_box() and add_meta_box() calls in the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1302 |
* callback. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1303 |
* @type array $taxonomies An array of taxonomy identifiers that will be registered for the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1304 |
* post type. Taxonomies can be registered later with register_taxonomy() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1305 |
* or register_taxonomy_for_object_type(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1306 |
* Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
* @type bool|string $has_archive Whether there should be post type archives, or if a string, the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1308 |
* archive slug to use. Will generate the proper rewrite rules if |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
* $rewrite is enabled. Default false. |
5 | 1310 |
* @type bool|array $rewrite { |
1311 |
* Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
|
1312 |
* Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be |
|
1313 |
* passed with any of these keys: |
|
1314 |
* |
|
1315 |
* @type string $slug Customize the permastruct slug. Defaults to $post_type key. |
|
1316 |
* @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. |
|
1317 |
* Default true. |
|
1318 |
* @type bool $feeds Whether the feed permastruct should be built for this post type. |
|
1319 |
* Default is value of $has_archive. |
|
1320 |
* @type bool $pages Whether the permastruct should provide for pagination. Default true. |
|
1321 |
* @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, |
|
1322 |
* inherits from $permalink_epmask. If not specified and permalink_epmask |
|
1323 |
* is not set, defaults to EP_PERMALINK. |
|
1324 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1325 |
* @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
* key. If false, a post type cannot be loaded at |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1327 |
* ?{query_var}={post_slug}. If specified as a string, the query |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
* ?{query_var_string}={post_slug} will be valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1329 |
* @type bool $can_export Whether to allow this post type to be exported. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1330 |
* @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1331 |
* posts of this type belonging to the user will be moved to trash |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1332 |
* when then user is deleted. If false, posts of this type belonging |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
* to the user will *not* be trashed or deleted. If not set (the default), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1334 |
* posts are trashed if post_type_supports('author'). Otherwise posts |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1335 |
* are not trashed or deleted. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1336 |
* @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1337 |
* "built-in" post_type. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
* @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1339 |
* this post type. Default 'post.php?post=%d'. |
5 | 1340 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1341 |
* @return WP_Post_Type|WP_Error The registered post type object, or an error object. |
0 | 1342 |
*/ |
1343 |
function register_post_type( $post_type, $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
global $wp_post_types; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1345 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1346 |
if ( ! is_array( $wp_post_types ) ) { |
0 | 1347 |
$wp_post_types = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1348 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1349 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1350 |
// Sanitize post type name |
0 | 1351 |
$post_type = sanitize_key( $post_type ); |
1352 |
||
5 | 1353 |
if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1354 |
_doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' ); |
5 | 1355 |
return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); |
1356 |
} |
|
0 | 1357 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
$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
|
1359 |
$post_type_object->add_supports(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
$post_type_object->add_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
$post_type_object->register_meta_boxes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1362 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
$wp_post_types[ $post_type ] = $post_type_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1364 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
$post_type_object->add_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
$post_type_object->register_taxonomies(); |
0 | 1367 |
|
5 | 1368 |
/** |
1369 |
* Fires after a post type is registered. |
|
1370 |
* |
|
1371 |
* @since 3.3.0 |
|
9 | 1372 |
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
5 | 1373 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
* @param string $post_type Post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1375 |
* @param WP_Post_Type $post_type_object Arguments used to register the post type. |
5 | 1376 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
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
|
1378 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1379 |
return $post_type_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1382 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1383 |
* Unregisters a post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1384 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1385 |
* Can not be used to unregister built-in post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1386 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1387 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1388 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1389 |
* @global array $wp_post_types List of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1390 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1391 |
* @param string $post_type Post type to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1392 |
* @return bool|WP_Error True on success, WP_Error on failure or if the post type doesn't exist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1393 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1394 |
function unregister_post_type( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1395 |
global $wp_post_types; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1396 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1397 |
if ( ! post_type_exists( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1398 |
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
|
1399 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1400 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1401 |
$post_type_object = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1402 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
// Do not allow unregistering internal post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1404 |
if ( $post_type_object->_builtin ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1405 |
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
|
1406 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1407 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1408 |
$post_type_object->remove_supports(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1409 |
$post_type_object->remove_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
$post_type_object->unregister_meta_boxes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1411 |
$post_type_object->remove_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
$post_type_object->unregister_taxonomies(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1413 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1414 |
unset( $wp_post_types[ $post_type ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1415 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1416 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1417 |
* Fires after a post type was unregistered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1418 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1420 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1421 |
* @param string $post_type Post type key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1422 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
do_action( 'unregistered_post_type', $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1425 |
return true; |
0 | 1426 |
} |
1427 |
||
1428 |
/** |
|
5 | 1429 |
* Build an object with all post type capabilities out of a post type object |
0 | 1430 |
* |
1431 |
* Post type capabilities use the 'capability_type' argument as a base, if the |
|
1432 |
* capability is not set in the 'capabilities' argument array or if the |
|
1433 |
* 'capabilities' argument is not supplied. |
|
1434 |
* |
|
1435 |
* The capability_type argument can optionally be registered as an array, with |
|
1436 |
* the first value being singular and the second plural, e.g. array('story, 'stories') |
|
1437 |
* Otherwise, an 's' will be added to the value for the plural form. After |
|
1438 |
* registration, capability_type will always be a string of the singular value. |
|
1439 |
* |
|
1440 |
* By default, seven keys are accepted as part of the capabilities array: |
|
1441 |
* |
|
1442 |
* - edit_post, read_post, and delete_post are meta capabilities, which are then |
|
1443 |
* generally mapped to corresponding primitive capabilities depending on the |
|
1444 |
* context, which would be the post being edited/read/deleted and the user or |
|
1445 |
* role being checked. Thus these capabilities would generally not be granted |
|
1446 |
* directly to users or roles. |
|
1447 |
* |
|
1448 |
* - edit_posts - Controls whether objects of this post type can be edited. |
|
1449 |
* - edit_others_posts - Controls whether objects of this type owned by other users |
|
1450 |
* can be edited. If the post type does not support an author, then this will |
|
1451 |
* behave like edit_posts. |
|
1452 |
* - publish_posts - Controls publishing objects of this post type. |
|
1453 |
* - read_private_posts - Controls whether private objects can be read. |
|
1454 |
* |
|
1455 |
* These four primitive capabilities are checked in core in various locations. |
|
1456 |
* There are also seven other primitive capabilities which are not referenced |
|
1457 |
* directly in core, except in map_meta_cap(), which takes the three aforementioned |
|
1458 |
* meta capabilities and translates them into one or more primitive capabilities |
|
1459 |
* that must then be checked against the user or role, depending on the context. |
|
1460 |
* |
|
1461 |
* - read - Controls whether objects of this post type can be read. |
|
1462 |
* - delete_posts - Controls whether objects of this post type can be deleted. |
|
1463 |
* - delete_private_posts - Controls whether private objects can be deleted. |
|
1464 |
* - delete_published_posts - Controls whether published objects can be deleted. |
|
1465 |
* - delete_others_posts - Controls whether objects owned by other users can be |
|
1466 |
* can be deleted. If the post type does not support an author, then this will |
|
1467 |
* behave like delete_posts. |
|
1468 |
* - edit_private_posts - Controls whether private objects can be edited. |
|
1469 |
* - edit_published_posts - Controls whether published objects can be edited. |
|
1470 |
* |
|
1471 |
* These additional capabilities are only used in map_meta_cap(). Thus, they are |
|
1472 |
* only assigned by default if the post type is registered with the 'map_meta_cap' |
|
1473 |
* argument set to true (default is false). |
|
1474 |
* |
|
5 | 1475 |
* @since 3.0.0 |
1476 |
* |
|
1477 |
* @see register_post_type() |
|
0 | 1478 |
* @see map_meta_cap() |
5 | 1479 |
* |
1480 |
* @param object $args Post type registration arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1481 |
* @return object Object with all the capabilities as member variables. |
0 | 1482 |
*/ |
1483 |
function get_post_type_capabilities( $args ) { |
|
9 | 1484 |
if ( ! is_array( $args->capability_type ) ) { |
0 | 1485 |
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); |
9 | 1486 |
} |
0 | 1487 |
|
1488 |
// Singular base for meta capabilities, plural base for primitive capabilities. |
|
1489 |
list( $singular_base, $plural_base ) = $args->capability_type; |
|
1490 |
||
1491 |
$default_capabilities = array( |
|
1492 |
// Meta capabilities |
|
9 | 1493 |
'edit_post' => 'edit_' . $singular_base, |
1494 |
'read_post' => 'read_' . $singular_base, |
|
1495 |
'delete_post' => 'delete_' . $singular_base, |
|
0 | 1496 |
// Primitive capabilities used outside of map_meta_cap(): |
9 | 1497 |
'edit_posts' => 'edit_' . $plural_base, |
1498 |
'edit_others_posts' => 'edit_others_' . $plural_base, |
|
1499 |
'publish_posts' => 'publish_' . $plural_base, |
|
0 | 1500 |
'read_private_posts' => 'read_private_' . $plural_base, |
1501 |
); |
|
1502 |
||
1503 |
// Primitive capabilities used within map_meta_cap(): |
|
1504 |
if ( $args->map_meta_cap ) { |
|
1505 |
$default_capabilities_for_mapping = array( |
|
1506 |
'read' => 'read', |
|
9 | 1507 |
'delete_posts' => 'delete_' . $plural_base, |
1508 |
'delete_private_posts' => 'delete_private_' . $plural_base, |
|
0 | 1509 |
'delete_published_posts' => 'delete_published_' . $plural_base, |
9 | 1510 |
'delete_others_posts' => 'delete_others_' . $plural_base, |
1511 |
'edit_private_posts' => 'edit_private_' . $plural_base, |
|
1512 |
'edit_published_posts' => 'edit_published_' . $plural_base, |
|
0 | 1513 |
); |
9 | 1514 |
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); |
0 | 1515 |
} |
1516 |
||
1517 |
$capabilities = array_merge( $default_capabilities, $args->capabilities ); |
|
1518 |
||
1519 |
// Post creation capability simply maps to edit_posts by default: |
|
9 | 1520 |
if ( ! isset( $capabilities['create_posts'] ) ) { |
0 | 1521 |
$capabilities['create_posts'] = $capabilities['edit_posts']; |
9 | 1522 |
} |
0 | 1523 |
|
1524 |
// Remember meta capabilities for future reference. |
|
9 | 1525 |
if ( $args->map_meta_cap ) { |
0 | 1526 |
_post_type_meta_capabilities( $capabilities ); |
9 | 1527 |
} |
0 | 1528 |
|
1529 |
return (object) $capabilities; |
|
1530 |
} |
|
1531 |
||
1532 |
/** |
|
5 | 1533 |
* Store or return a list of post type meta caps for map_meta_cap(). |
0 | 1534 |
* |
1535 |
* @since 3.1.0 |
|
1536 |
* @access private |
|
5 | 1537 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1538 |
* @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
|
1539 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1540 |
* @param array $capabilities Post type meta capabilities. |
0 | 1541 |
*/ |
1542 |
function _post_type_meta_capabilities( $capabilities = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1543 |
global $post_type_meta_caps; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1544 |
|
0 | 1545 |
foreach ( $capabilities as $core => $custom ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1546 |
if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1547 |
$post_type_meta_caps[ $custom ] = $core; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1548 |
} |
0 | 1549 |
} |
1550 |
} |
|
1551 |
||
1552 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1553 |
* Builds an object with all post type labels out of a post type object. |
0 | 1554 |
* |
1555 |
* Accepted keys of the label array in the post type object: |
|
5 | 1556 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
* - `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
|
1558 |
* by `$post_type_object->label`. Default is 'Posts' / 'Pages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1559 |
* - `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
|
1560 |
* - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1561 |
* When internationalizing this string, please use a {@link https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1562 |
* 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
|
1563 |
* - `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
|
1564 |
* - `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
|
1565 |
* - `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
|
1566 |
* - `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
|
1567 |
* - `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
|
1568 |
* - `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
|
1569 |
* - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1570 |
* - `not_found_in_trash` - Label used when no items are in the trash. Default is 'No posts found in Trash' / |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1571 |
* 'No pages found in Trash'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1572 |
* - `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
|
1573 |
* post types. Default is 'Parent Page:'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1574 |
* - `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
|
1575 |
* - `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
|
1576 |
* - `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
|
1577 |
* - `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
|
1578 |
* - `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
|
1579 |
* 'Uploaded to this page'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1580 |
* - `featured_image` - Label for the Featured Image meta box title. Default is 'Featured Image'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1581 |
* - `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
|
1582 |
* - `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
|
1583 |
* - `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
|
1584 |
* - `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
|
1585 |
* - `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
|
1586 |
* 'Filter pages list'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1587 |
* - `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
|
1588 |
* 'Pages list navigation'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1589 |
* - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. |
9 | 1590 |
* - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' |
1591 |
* - `item_published_privately` - Label used when an item is published with private visibility. |
|
1592 |
* Default is 'Post published privately.' / 'Page published privately.' |
|
1593 |
* - `item_reverted_to_draft` - Label used when an item is switched to a draft. |
|
1594 |
* Default is 'Post reverted to draft.' / 'Page reverted to draft.' |
|
1595 |
* - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / |
|
1596 |
* 'Page scheduled.' |
|
1597 |
* - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' |
|
5 | 1598 |
* |
1599 |
* Above, the first default value is for non-hierarchical post types (like posts) |
|
1600 |
* and the second one is for hierarchical post types (like pages). |
|
0 | 1601 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1602 |
* 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
|
1603 |
* |
0 | 1604 |
* @since 3.0.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1605 |
* @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
|
1606 |
* and `use_featured_image` labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1607 |
* @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
|
1608 |
* `items_list_navigation`, and `items_list` labels. |
9 | 1609 |
* @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
|
1610 |
* @since 4.7.0 Added the `view_items` and `attributes` labels. |
9 | 1611 |
* @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, |
1612 |
* `item_scheduled`, and `item_updated` labels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1613 |
* |
0 | 1614 |
* @access private |
1615 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1616 |
* @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
|
1617 |
* @return object Object with all the labels as member variables. |
0 | 1618 |
*/ |
1619 |
function get_post_type_labels( $post_type_object ) { |
|
9 | 1620 |
$nohier_vs_hier_defaults = array( |
1621 |
'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ), |
|
1622 |
'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ), |
|
1623 |
'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ), |
|
1624 |
'add_new_item' => array( __( 'Add New Post' ), __( 'Add New Page' ) ), |
|
1625 |
'edit_item' => array( __( 'Edit Post' ), __( 'Edit Page' ) ), |
|
1626 |
'new_item' => array( __( 'New Post' ), __( 'New Page' ) ), |
|
1627 |
'view_item' => array( __( 'View Post' ), __( 'View Page' ) ), |
|
1628 |
'view_items' => array( __( 'View Posts' ), __( 'View Pages' ) ), |
|
1629 |
'search_items' => array( __( 'Search Posts' ), __( 'Search Pages' ) ), |
|
1630 |
'not_found' => array( __( 'No posts found.' ), __( 'No pages found.' ) ), |
|
1631 |
'not_found_in_trash' => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ), |
|
1632 |
'parent_item_colon' => array( null, __( 'Parent Page:' ) ), |
|
1633 |
'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ), |
|
1634 |
'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ), |
|
1635 |
'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ), |
|
1636 |
'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ), |
|
1637 |
'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ), |
|
1638 |
'featured_image' => array( _x( 'Featured Image', 'post' ), _x( 'Featured Image', 'page' ) ), |
|
1639 |
'set_featured_image' => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ), |
|
1640 |
'remove_featured_image' => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ), |
|
1641 |
'use_featured_image' => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ), |
|
1642 |
'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ), |
|
1643 |
'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ), |
|
1644 |
'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ), |
|
1645 |
'item_published' => array( __( 'Post published.' ), __( 'Page published.' ) ), |
|
1646 |
'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ), |
|
1647 |
'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ), |
|
1648 |
'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ), |
|
1649 |
'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ), |
|
0 | 1650 |
); |
1651 |
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
|
1652 |
||
1653 |
$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
|
1654 |
||
1655 |
$post_type = $post_type_object->name; |
|
5 | 1656 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1657 |
$default_labels = clone $labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1658 |
|
5 | 1659 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1660 |
* Filters the labels of a specific post type. |
5 | 1661 |
* |
1662 |
* The dynamic portion of the hook name, `$post_type`, refers to |
|
1663 |
* the post type slug. |
|
1664 |
* |
|
1665 |
* @since 3.5.0 |
|
1666 |
* |
|
1667 |
* @see get_post_type_labels() for the full list of labels. |
|
1668 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1669 |
* @param object $labels Object with labels for the post type as member variables. |
5 | 1670 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1671 |
$labels = apply_filters( "post_type_labels_{$post_type}", $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1672 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1673 |
// 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
|
1674 |
$labels = (object) array_merge( (array) $default_labels, (array) $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1675 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1676 |
return $labels; |
0 | 1677 |
} |
1678 |
||
1679 |
/** |
|
5 | 1680 |
* Build an object with custom-something object (post type, taxonomy) labels |
1681 |
* out of a custom-something object |
|
1682 |
* |
|
1683 |
* @since 3.0.0 |
|
0 | 1684 |
* @access private |
5 | 1685 |
* |
1686 |
* @param object $object A custom-something object. |
|
1687 |
* @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
|
1688 |
* @return object Object containing labels for the given custom-something object. |
0 | 1689 |
*/ |
1690 |
function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { |
|
1691 |
$object->labels = (array) $object->labels; |
|
1692 |
||
9 | 1693 |
if ( isset( $object->label ) && empty( $object->labels['name'] ) ) { |
0 | 1694 |
$object->labels['name'] = $object->label; |
9 | 1695 |
} |
1696 |
||
1697 |
if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) { |
|
0 | 1698 |
$object->labels['singular_name'] = $object->labels['name']; |
9 | 1699 |
} |
1700 |
||
1701 |
if ( ! isset( $object->labels['name_admin_bar'] ) ) { |
|
0 | 1702 |
$object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; |
9 | 1703 |
} |
1704 |
||
1705 |
if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) { |
|
0 | 1706 |
$object->labels['menu_name'] = $object->labels['name']; |
9 | 1707 |
} |
1708 |
||
1709 |
if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) { |
|
0 | 1710 |
$object->labels['all_items'] = $object->labels['menu_name']; |
9 | 1711 |
} |
1712 |
||
1713 |
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
|
1714 |
$object->labels['archives'] = $object->labels['all_items']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1715 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1716 |
|
5 | 1717 |
$defaults = array(); |
1718 |
foreach ( $nohier_vs_hier_defaults as $key => $value ) { |
|
9 | 1719 |
$defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0]; |
1720 |
} |
|
1721 |
$labels = array_merge( $defaults, $object->labels ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1722 |
$object->labels = (object) $object->labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1723 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1724 |
return (object) $labels; |
0 | 1725 |
} |
1726 |
||
1727 |
/** |
|
5 | 1728 |
* Add submenus for post types. |
0 | 1729 |
* |
1730 |
* @access private |
|
1731 |
* @since 3.1.0 |
|
1732 |
*/ |
|
1733 |
function _add_post_type_submenus() { |
|
1734 |
foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { |
|
1735 |
$ptype_obj = get_post_type_object( $ptype ); |
|
5 | 1736 |
// Sub-menus only. |
9 | 1737 |
if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) { |
0 | 1738 |
continue; |
9 | 1739 |
} |
0 | 1740 |
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" ); |
1741 |
} |
|
1742 |
} |
|
1743 |
||
1744 |
/** |
|
1745 |
* Register support of certain features for a post type. |
|
1746 |
* |
|
5 | 1747 |
* All core features are directly associated with a functional area of the edit |
1748 |
* screen, such as the editor or a meta box. Features include: 'title', 'editor', |
|
1749 |
* 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', |
|
1750 |
* 'thumbnail', 'custom-fields', and 'post-formats'. |
|
1751 |
* |
|
1752 |
* Additionally, the 'revisions' feature dictates whether the post type will |
|
1753 |
* store revisions, and the 'comments' feature dictates whether the comments |
|
1754 |
* count will show on the edit screen. |
|
0 | 1755 |
* |
1756 |
* @since 3.0.0 |
|
5 | 1757 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1758 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1759 |
* |
5 | 1760 |
* @param string $post_type The post type for which to add the feature. |
1761 |
* @param string|array $feature The feature being added, accepts an array of |
|
1762 |
* feature strings or a single string. |
|
0 | 1763 |
*/ |
1764 |
function add_post_type_support( $post_type, $feature ) { |
|
1765 |
global $_wp_post_type_features; |
|
1766 |
||
1767 |
$features = (array) $feature; |
|
9 | 1768 |
foreach ( $features as $feature ) { |
1769 |
if ( func_num_args() == 2 ) { |
|
1770 |
$_wp_post_type_features[ $post_type ][ $feature ] = true; |
|
1771 |
} else { |
|
1772 |
$_wp_post_type_features[ $post_type ][ $feature ] = array_slice( func_get_args(), 2 ); |
|
1773 |
} |
|
0 | 1774 |
} |
1775 |
} |
|
1776 |
||
1777 |
/** |
|
1778 |
* Remove support for a feature from a post type. |
|
1779 |
* |
|
1780 |
* @since 3.0.0 |
|
5 | 1781 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1782 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1783 |
* |
5 | 1784 |
* @param string $post_type The post type for which to remove the feature. |
1785 |
* @param string $feature The feature being removed. |
|
0 | 1786 |
*/ |
1787 |
function remove_post_type_support( $post_type, $feature ) { |
|
1788 |
global $_wp_post_type_features; |
|
1789 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1790 |
unset( $_wp_post_type_features[ $post_type ][ $feature ] ); |
0 | 1791 |
} |
1792 |
||
1793 |
/** |
|
1794 |
* Get all the post type features |
|
1795 |
* |
|
1796 |
* @since 3.4.0 |
|
5 | 1797 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1798 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1799 |
* |
5 | 1800 |
* @param string $post_type The post type. |
1801 |
* @return array Post type supports list. |
|
0 | 1802 |
*/ |
1803 |
function get_all_post_type_supports( $post_type ) { |
|
1804 |
global $_wp_post_type_features; |
|
1805 |
||
9 | 1806 |
if ( isset( $_wp_post_type_features[ $post_type ] ) ) { |
1807 |
return $_wp_post_type_features[ $post_type ]; |
|
1808 |
} |
|
0 | 1809 |
|
1810 |
return array(); |
|
1811 |
} |
|
1812 |
||
1813 |
/** |
|
5 | 1814 |
* Check a post type's support for a given feature. |
0 | 1815 |
* |
1816 |
* @since 3.0.0 |
|
5 | 1817 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1818 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1819 |
* |
5 | 1820 |
* @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
|
1821 |
* @param string $feature The feature being checked. |
5 | 1822 |
* @return bool Whether the post type supports the given feature. |
0 | 1823 |
*/ |
1824 |
function post_type_supports( $post_type, $feature ) { |
|
1825 |
global $_wp_post_type_features; |
|
1826 |
||
9 | 1827 |
return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) ); |
0 | 1828 |
} |
1829 |
||
1830 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1831 |
* 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
|
1832 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1833 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1834 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1835 |
* @global array $_wp_post_type_features Post type features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1836 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1837 |
* @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
|
1838 |
* @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
|
1839 |
* 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
|
1840 |
* 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
|
1841 |
* match. Default 'and'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1842 |
* @return array A list of post type names. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1843 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1844 |
function get_post_types_by_support( $feature, $operator = 'and' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1845 |
global $_wp_post_type_features; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1846 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1847 |
$features = array_fill_keys( (array) $feature, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1848 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1849 |
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
|
1850 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1851 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1852 |
/** |
5 | 1853 |
* Update the post type for the post ID. |
0 | 1854 |
* |
1855 |
* The page or post cache will be cleaned for the post ID. |
|
1856 |
* |
|
1857 |
* @since 2.5.0 |
|
1858 |
* |
|
5 | 1859 |
* @global wpdb $wpdb WordPress database abstraction object. |
1860 |
* |
|
1861 |
* @param int $post_id Optional. Post ID to change post type. Default 0. |
|
1862 |
* @param string $post_type Optional. Post type. Accepts 'post' or 'page' to |
|
1863 |
* name a few. Default 'post'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1864 |
* @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. |
0 | 1865 |
*/ |
1866 |
function set_post_type( $post_id = 0, $post_type = 'post' ) { |
|
1867 |
global $wpdb; |
|
1868 |
||
9 | 1869 |
$post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' ); |
1870 |
$return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) ); |
|
0 | 1871 |
|
1872 |
clean_post_cache( $post_id ); |
|
1873 |
||
1874 |
return $return; |
|
1875 |
} |
|
1876 |
||
1877 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1878 |
* Determines whether a post type is considered "viewable". |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1879 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1880 |
* 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
|
1881 |
* 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
|
1882 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1883 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1884 |
* @since 4.5.0 Added the ability to pass a post type name in addition to object. |
9 | 1885 |
* @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
|
1886 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1887 |
* @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
|
1888 |
* @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
|
1889 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1890 |
function is_post_type_viewable( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1891 |
if ( is_scalar( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1892 |
$post_type = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1893 |
if ( ! $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1894 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1895 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1896 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1897 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1898 |
return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1899 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1900 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1901 |
/** |
9 | 1902 |
* Retrieves an array of the latest posts, or posts matching the given criteria. |
0 | 1903 |
* |
1904 |
* The defaults are as follows: |
|
1905 |
* |
|
1906 |
* @since 1.2.0 |
|
5 | 1907 |
* |
1908 |
* @see WP_Query::parse_query() |
|
1909 |
* |
|
1910 |
* @param array $args { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1911 |
* Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all |
5 | 1912 |
* available arguments. |
1913 |
* |
|
1914 |
* @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
|
1915 |
* in WP_Query. Accepts -1 for all. Default 5. |
5 | 1916 |
* @type int|string $category Category ID or comma-separated list of IDs (this or any children). |
1917 |
* Is an alias of $cat in WP_Query. Default 0. |
|
1918 |
* @type array $include An array of post IDs to retrieve, sticky posts will be included. |
|
1919 |
* Is an alias of $post__in in WP_Query. Default empty array. |
|
1920 |
* @type array $exclude An array of post IDs not to retrieve. Default empty array. |
|
1921 |
* @type bool $suppress_filters Whether to suppress filters. Default true. |
|
1922 |
* } |
|
9 | 1923 |
* @return WP_Post[]|int[] Array of post objects or post IDs. |
0 | 1924 |
*/ |
5 | 1925 |
function get_posts( $args = null ) { |
0 | 1926 |
$defaults = array( |
9 | 1927 |
'numberposts' => 5, |
1928 |
'category' => 0, |
|
1929 |
'orderby' => 'date', |
|
1930 |
'order' => 'DESC', |
|
1931 |
'include' => array(), |
|
1932 |
'exclude' => array(), |
|
1933 |
'meta_key' => '', |
|
1934 |
'meta_value' => '', |
|
1935 |
'post_type' => 'post', |
|
1936 |
'suppress_filters' => true, |
|
0 | 1937 |
); |
1938 |
||
1939 |
$r = wp_parse_args( $args, $defaults ); |
|
9 | 1940 |
if ( empty( $r['post_status'] ) ) { |
0 | 1941 |
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; |
9 | 1942 |
} |
1943 |
if ( ! empty( $r['numberposts'] ) && empty( $r['posts_per_page'] ) ) { |
|
0 | 1944 |
$r['posts_per_page'] = $r['numberposts']; |
9 | 1945 |
} |
1946 |
if ( ! empty( $r['category'] ) ) { |
|
0 | 1947 |
$r['cat'] = $r['category']; |
9 | 1948 |
} |
1949 |
if ( ! empty( $r['include'] ) ) { |
|
1950 |
$incposts = wp_parse_id_list( $r['include'] ); |
|
1951 |
$r['posts_per_page'] = count( $incposts ); // only the number of posts included |
|
1952 |
$r['post__in'] = $incposts; |
|
1953 |
} elseif ( ! empty( $r['exclude'] ) ) { |
|
0 | 1954 |
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); |
9 | 1955 |
} |
0 | 1956 |
|
1957 |
$r['ignore_sticky_posts'] = true; |
|
9 | 1958 |
$r['no_found_rows'] = true; |
0 | 1959 |
|
1960 |
$get_posts = new WP_Query; |
|
9 | 1961 |
return $get_posts->query( $r ); |
0 | 1962 |
|
1963 |
} |
|
1964 |
||
1965 |
// |
|
1966 |
// Post meta functions |
|
1967 |
// |
|
1968 |
||
1969 |
/** |
|
9 | 1970 |
* Adds a meta field to the given post. |
0 | 1971 |
* |
1972 |
* Post meta data is called "Custom Fields" on the Administration Screen. |
|
1973 |
* |
|
1974 |
* @since 1.5.0 |
|
5 | 1975 |
* |
1976 |
* @param int $post_id Post ID. |
|
1977 |
* @param string $meta_key Metadata name. |
|
1978 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
1979 |
* @param bool $unique Optional. Whether the same key should not be added. |
|
1980 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1981 |
* @return int|false Meta ID on success, false on failure. |
0 | 1982 |
*/ |
5 | 1983 |
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { |
1984 |
// Make sure meta is added to the post, not a revision. |
|
9 | 1985 |
$the_post = wp_is_post_revision( $post_id ); |
1986 |
if ( $the_post ) { |
|
0 | 1987 |
$post_id = $the_post; |
9 | 1988 |
} |
1989 |
||
1990 |
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); |
|
0 | 1991 |
} |
1992 |
||
1993 |
/** |
|
9 | 1994 |
* Deletes a post meta field for the given post ID. |
0 | 1995 |
* |
1996 |
* You can match based on the key, or key and value. Removing based on key and |
|
1997 |
* value, will keep from removing duplicate metadata with the same key. It also |
|
9 | 1998 |
* allows removing all metadata matching the key, if needed. |
0 | 1999 |
* |
2000 |
* @since 1.5.0 |
|
5 | 2001 |
* |
2002 |
* @param int $post_id Post ID. |
|
2003 |
* @param string $meta_key Metadata name. |
|
2004 |
* @param mixed $meta_value Optional. Metadata value. Must be serializable if |
|
2005 |
* non-scalar. Default empty. |
|
0 | 2006 |
* @return bool True on success, false on failure. |
2007 |
*/ |
|
5 | 2008 |
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { |
2009 |
// Make sure meta is added to the post, not a revision. |
|
9 | 2010 |
$the_post = wp_is_post_revision( $post_id ); |
2011 |
if ( $the_post ) { |
|
0 | 2012 |
$post_id = $the_post; |
9 | 2013 |
} |
2014 |
||
2015 |
return delete_metadata( 'post', $post_id, $meta_key, $meta_value ); |
|
0 | 2016 |
} |
2017 |
||
2018 |
/** |
|
9 | 2019 |
* Retrieves a post meta field for the given post ID. |
0 | 2020 |
* |
2021 |
* @since 1.5.0 |
|
5 | 2022 |
* |
2023 |
* @param int $post_id Post ID. |
|
2024 |
* @param string $key Optional. The meta key to retrieve. By default, returns |
|
2025 |
* data for all keys. Default empty. |
|
9 | 2026 |
* @param bool $single Optional. If true, returns only the first value for the specified meta key. |
2027 |
* This parameter has no effect if $key is not specified. Default false. |
|
2028 |
* @return mixed Will be an array if $single is false. Will be value of the meta |
|
5 | 2029 |
* field if $single is true. |
0 | 2030 |
*/ |
5 | 2031 |
function get_post_meta( $post_id, $key = '', $single = false ) { |
9 | 2032 |
return get_metadata( 'post', $post_id, $key, $single ); |
0 | 2033 |
} |
2034 |
||
2035 |
/** |
|
9 | 2036 |
* Updates a post meta field based on the given post ID. |
2037 |
* |
|
2038 |
* Use the `$prev_value` parameter to differentiate between meta fields with the |
|
0 | 2039 |
* same key and post ID. |
2040 |
* |
|
9 | 2041 |
* If the meta field for the post does not exist, it will be added and its ID returned. |
2042 |
* |
|
2043 |
* Can be used in place of add_post_meta(). |
|
0 | 2044 |
* |
2045 |
* @since 1.5.0 |
|
5 | 2046 |
* |
2047 |
* @param int $post_id Post ID. |
|
2048 |
* @param string $meta_key Metadata key. |
|
2049 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
9 | 2050 |
* @param mixed $prev_value Optional. Previous value to check before updating. |
2051 |
* @return int|bool The new meta field ID if a field with the given key didn't exist and was |
|
2052 |
* therefore added, true on successful update, false on failure. |
|
0 | 2053 |
*/ |
5 | 2054 |
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { |
2055 |
// Make sure meta is added to the post, not a revision. |
|
9 | 2056 |
$the_post = wp_is_post_revision( $post_id ); |
2057 |
if ( $the_post ) { |
|
0 | 2058 |
$post_id = $the_post; |
9 | 2059 |
} |
2060 |
||
2061 |
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); |
|
0 | 2062 |
} |
2063 |
||
2064 |
/** |
|
9 | 2065 |
* Deletes everything from post meta matching the given meta key. |
0 | 2066 |
* |
2067 |
* @since 2.3.0 |
|
2068 |
* |
|
2069 |
* @param string $post_meta_key Key to search for when deleting. |
|
5 | 2070 |
* @return bool Whether the post meta key was deleted from the database. |
0 | 2071 |
*/ |
5 | 2072 |
function delete_post_meta_by_key( $post_meta_key ) { |
9 | 2073 |
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
|
2074 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2075 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2076 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2077 |
* Registers a meta key for posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2078 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2079 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2080 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2081 |
* @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
|
2082 |
* 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
|
2083 |
* @param string $meta_key The meta key to register. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2084 |
* @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
|
2085 |
* {@see register_meta()} for a list of supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2086 |
* @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
|
2087 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2088 |
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
|
2089 |
$args['object_subtype'] = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2090 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2091 |
return register_meta( 'post', $meta_key, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2092 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2093 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2094 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2095 |
* Unregisters a meta key for posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2096 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2097 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2098 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2099 |
* @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
|
2100 |
* 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
|
2101 |
* existing post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2102 |
* @param string $meta_key The meta key to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2103 |
* @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
|
2104 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2105 |
function unregister_post_meta( $post_type, $meta_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2106 |
return unregister_meta_key( 'post', $meta_key, $post_type ); |
0 | 2107 |
} |
2108 |
||
2109 |
/** |
|
2110 |
* Retrieve post meta fields, based on post ID. |
|
2111 |
* |
|
2112 |
* The post meta fields are retrieved from the cache where possible, |
|
2113 |
* so the function is optimized to be called more than once. |
|
2114 |
* |
|
2115 |
* @since 1.2.0 |
|
5 | 2116 |
* |
2117 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|
2118 |
* @return array Post meta for the given post. |
|
0 | 2119 |
*/ |
2120 |
function get_post_custom( $post_id = 0 ) { |
|
2121 |
$post_id = absint( $post_id ); |
|
9 | 2122 |
if ( ! $post_id ) { |
0 | 2123 |
$post_id = get_the_ID(); |
9 | 2124 |
} |
0 | 2125 |
|
2126 |
return get_post_meta( $post_id ); |
|
2127 |
} |
|
2128 |
||
2129 |
/** |
|
2130 |
* Retrieve meta field names for a post. |
|
2131 |
* |
|
2132 |
* If there are no meta fields, then nothing (null) will be returned. |
|
2133 |
* |
|
2134 |
* @since 1.2.0 |
|
5 | 2135 |
* |
2136 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2137 |
* @return array|void Array of the keys, if retrieved. |
0 | 2138 |
*/ |
2139 |
function get_post_custom_keys( $post_id = 0 ) { |
|
2140 |
$custom = get_post_custom( $post_id ); |
|
2141 |
||
9 | 2142 |
if ( ! is_array( $custom ) ) { |
0 | 2143 |
return; |
9 | 2144 |
} |
2145 |
||
2146 |
if ( $keys = array_keys( $custom ) ) { |
|
0 | 2147 |
return $keys; |
9 | 2148 |
} |
0 | 2149 |
} |
2150 |
||
2151 |
/** |
|
2152 |
* Retrieve values for a custom post field. |
|
2153 |
* |
|
2154 |
* The parameters must not be considered optional. All of the post meta fields |
|
2155 |
* will be retrieved and only the meta field key values returned. |
|
2156 |
* |
|
2157 |
* @since 1.2.0 |
|
5 | 2158 |
* |
2159 |
* @param string $key Optional. Meta field key. Default empty. |
|
2160 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2161 |
* @return array|null Meta field values. |
0 | 2162 |
*/ |
2163 |
function get_post_custom_values( $key = '', $post_id = 0 ) { |
|
9 | 2164 |
if ( ! $key ) { |
0 | 2165 |
return null; |
9 | 2166 |
} |
2167 |
||
2168 |
$custom = get_post_custom( $post_id ); |
|
2169 |
||
2170 |
return isset( $custom[ $key ] ) ? $custom[ $key ] : null; |
|
0 | 2171 |
} |
2172 |
||
2173 |
/** |
|
9 | 2174 |
* Determines whether a post is sticky. |
0 | 2175 |
* |
2176 |
* Sticky posts should remain at the top of The Loop. If the post ID is not |
|
2177 |
* given, then The Loop ID for the current post will be used. |
|
2178 |
* |
|
9 | 2179 |
* For more information on this and similar theme functions, check out |
2180 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
2181 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
2182 |
* |
|
0 | 2183 |
* @since 2.7.0 |
2184 |
* |
|
5 | 2185 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
0 | 2186 |
* @return bool Whether post is sticky. |
2187 |
*/ |
|
2188 |
function is_sticky( $post_id = 0 ) { |
|
2189 |
$post_id = absint( $post_id ); |
|
2190 |
||
9 | 2191 |
if ( ! $post_id ) { |
0 | 2192 |
$post_id = get_the_ID(); |
9 | 2193 |
} |
0 | 2194 |
|
2195 |
$stickies = get_option( 'sticky_posts' ); |
|
2196 |
||
9 | 2197 |
if ( ! is_array( $stickies ) ) { |
0 | 2198 |
return false; |
9 | 2199 |
} |
2200 |
||
2201 |
if ( in_array( $post_id, $stickies ) ) { |
|
0 | 2202 |
return true; |
9 | 2203 |
} |
0 | 2204 |
|
2205 |
return false; |
|
2206 |
} |
|
2207 |
||
2208 |
/** |
|
2209 |
* Sanitize every post field. |
|
2210 |
* |
|
5 | 2211 |
* If the context is 'raw', then the post object or array will get minimal |
2212 |
* sanitization of the integer fields. |
|
0 | 2213 |
* |
2214 |
* @since 2.3.0 |
|
5 | 2215 |
* |
2216 |
* @see sanitize_post_field() |
|
2217 |
* |
|
2218 |
* @param object|WP_Post|array $post The Post Object or Array |
|
2219 |
* @param string $context Optional. How to sanitize post fields. |
|
2220 |
* Accepts 'raw', 'edit', 'db', or 'display'. |
|
2221 |
* Default 'display'. |
|
2222 |
* @return object|WP_Post|array The now sanitized Post Object or Array (will be the |
|
2223 |
* same type as $post). |
|
0 | 2224 |
*/ |
5 | 2225 |
function sanitize_post( $post, $context = 'display' ) { |
9 | 2226 |
if ( is_object( $post ) ) { |
5 | 2227 |
// Check if post already filtered for this context. |
9 | 2228 |
if ( isset( $post->filter ) && $context == $post->filter ) { |
0 | 2229 |
return $post; |
9 | 2230 |
} |
2231 |
if ( ! isset( $post->ID ) ) { |
|
0 | 2232 |
$post->ID = 0; |
9 | 2233 |
} |
2234 |
foreach ( array_keys( get_object_vars( $post ) ) as $field ) { |
|
2235 |
$post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|
2236 |
} |
|
0 | 2237 |
$post->filter = $context; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2238 |
} elseif ( is_array( $post ) ) { |
5 | 2239 |
// Check if post already filtered for this context. |
9 | 2240 |
if ( isset( $post['filter'] ) && $context == $post['filter'] ) { |
0 | 2241 |
return $post; |
9 | 2242 |
} |
2243 |
if ( ! isset( $post['ID'] ) ) { |
|
0 | 2244 |
$post['ID'] = 0; |
9 | 2245 |
} |
2246 |
foreach ( array_keys( $post ) as $field ) { |
|
2247 |
$post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context ); |
|
2248 |
} |
|
0 | 2249 |
$post['filter'] = $context; |
2250 |
} |
|
2251 |
return $post; |
|
2252 |
} |
|
2253 |
||
2254 |
/** |
|
2255 |
* Sanitize post field based on context. |
|
2256 |
* |
|
5 | 2257 |
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and |
2258 |
* 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts |
|
2259 |
* are treated like 'display' when calling filters. |
|
0 | 2260 |
* |
2261 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2262 |
* @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. |
5 | 2263 |
* |
2264 |
* @param string $field The Post Object field name. |
|
2265 |
* @param mixed $value The Post Object value. |
|
2266 |
* @param int $post_id Post ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2267 |
* @param string $context Optional. How to sanitize post fields. Looks for 'raw', 'edit', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2268 |
* 'db', 'display', 'attribute' and 'js'. Default 'display'. |
0 | 2269 |
* @return mixed Sanitized value. |
2270 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2271 |
function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { |
9 | 2272 |
$int_fields = array( 'ID', 'post_parent', 'menu_order' ); |
2273 |
if ( in_array( $field, $int_fields ) ) { |
|
0 | 2274 |
$value = (int) $value; |
9 | 2275 |
} |
0 | 2276 |
|
5 | 2277 |
// Fields which contain arrays of integers. |
0 | 2278 |
$array_int_fields = array( 'ancestors' ); |
9 | 2279 |
if ( in_array( $field, $array_int_fields ) ) { |
2280 |
$value = array_map( 'absint', $value ); |
|
0 | 2281 |
return $value; |
2282 |
} |
|
2283 |
||
9 | 2284 |
if ( 'raw' == $context ) { |
0 | 2285 |
return $value; |
9 | 2286 |
} |
0 | 2287 |
|
2288 |
$prefixed = false; |
|
9 | 2289 |
if ( false !== strpos( $field, 'post_' ) ) { |
2290 |
$prefixed = true; |
|
2291 |
$field_no_prefix = str_replace( 'post_', '', $field ); |
|
0 | 2292 |
} |
2293 |
||
2294 |
if ( 'edit' == $context ) { |
|
9 | 2295 |
$format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' ); |
0 | 2296 |
|
2297 |
if ( $prefixed ) { |
|
5 | 2298 |
|
2299 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2300 |
* Filters the value of a specific post field to edit. |
5 | 2301 |
* |
2302 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2303 |
* field name. |
|
2304 |
* |
|
2305 |
* @since 2.3.0 |
|
2306 |
* |
|
2307 |
* @param mixed $value Value of the post field. |
|
2308 |
* @param int $post_id Post ID. |
|
2309 |
*/ |
|
2310 |
$value = apply_filters( "edit_{$field}", $value, $post_id ); |
|
2311 |
||
2312 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2313 |
* Filters the value of a specific post field to edit. |
5 | 2314 |
* |
2315 |
* The dynamic portion of the hook name, `$field_no_prefix`, refers to |
|
2316 |
* the post field name. |
|
2317 |
* |
|
2318 |
* @since 2.3.0 |
|
2319 |
* |
|
2320 |
* @param mixed $value Value of the post field. |
|
2321 |
* @param int $post_id Post ID. |
|
2322 |
*/ |
|
2323 |
$value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); |
|
0 | 2324 |
} else { |
5 | 2325 |
$value = apply_filters( "edit_post_{$field}", $value, $post_id ); |
0 | 2326 |
} |
2327 |
||
9 | 2328 |
if ( in_array( $field, $format_to_edit ) ) { |
2329 |
if ( 'post_content' == $field ) { |
|
2330 |
$value = format_to_edit( $value, user_can_richedit() ); |
|
2331 |
} else { |
|
2332 |
$value = format_to_edit( $value ); |
|
2333 |
} |
|
0 | 2334 |
} else { |
9 | 2335 |
$value = esc_attr( $value ); |
0 | 2336 |
} |
5 | 2337 |
} elseif ( 'db' == $context ) { |
0 | 2338 |
if ( $prefixed ) { |
5 | 2339 |
|
2340 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2341 |
* Filters the value of a specific post field before saving. |
5 | 2342 |
* |
2343 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2344 |
* field name. |
|
2345 |
* |
|
2346 |
* @since 2.3.0 |
|
2347 |
* |
|
2348 |
* @param mixed $value Value of the post field. |
|
2349 |
*/ |
|
2350 |
$value = apply_filters( "pre_{$field}", $value ); |
|
2351 |
||
2352 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2353 |
* Filters the value of a specific field before saving. |
5 | 2354 |
* |
2355 |
* The dynamic portion of the hook name, `$field_no_prefix`, refers |
|
2356 |
* to the post field name. |
|
2357 |
* |
|
2358 |
* @since 2.3.0 |
|
2359 |
* |
|
2360 |
* @param mixed $value Value of the post field. |
|
2361 |
*/ |
|
2362 |
$value = apply_filters( "{$field_no_prefix}_save_pre", $value ); |
|
0 | 2363 |
} else { |
5 | 2364 |
$value = apply_filters( "pre_post_{$field}", $value ); |
2365 |
||
2366 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2367 |
* Filters the value of a specific post field before saving. |
5 | 2368 |
* |
2369 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2370 |
* field name. |
|
2371 |
* |
|
2372 |
* @since 2.3.0 |
|
2373 |
* |
|
2374 |
* @param mixed $value Value of the post field. |
|
2375 |
*/ |
|
2376 |
$value = apply_filters( "{$field}_pre", $value ); |
|
0 | 2377 |
} |
2378 |
} else { |
|
5 | 2379 |
|
0 | 2380 |
// Use display filters by default. |
5 | 2381 |
if ( $prefixed ) { |
2382 |
||
2383 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2384 |
* Filters the value of a specific post field for display. |
5 | 2385 |
* |
2386 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2387 |
* field name. |
|
2388 |
* |
|
2389 |
* @since 2.3.0 |
|
2390 |
* |
|
2391 |
* @param mixed $value Value of the prefixed post field. |
|
2392 |
* @param int $post_id Post ID. |
|
2393 |
* @param string $context Context for how to sanitize the field. Possible |
|
2394 |
* values include 'raw', 'edit', 'db', 'display', |
|
2395 |
* 'attribute' and 'js'. |
|
2396 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2397 |
$value = apply_filters( "{$field}", $value, $post_id, $context ); |
5 | 2398 |
} else { |
2399 |
$value = apply_filters( "post_{$field}", $value, $post_id, $context ); |
|
2400 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2401 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2402 |
if ( 'attribute' == $context ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2403 |
$value = esc_attr( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2404 |
} elseif ( 'js' == $context ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2405 |
$value = esc_js( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2406 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2407 |
} |
0 | 2408 |
|
2409 |
return $value; |
|
2410 |
} |
|
2411 |
||
2412 |
/** |
|
2413 |
* Make a post sticky. |
|
2414 |
* |
|
2415 |
* Sticky posts should be displayed at the top of the front page. |
|
2416 |
* |
|
2417 |
* @since 2.7.0 |
|
2418 |
* |
|
2419 |
* @param int $post_id Post ID. |
|
2420 |
*/ |
|
5 | 2421 |
function stick_post( $post_id ) { |
9 | 2422 |
$stickies = get_option( 'sticky_posts' ); |
2423 |
||
2424 |
if ( ! is_array( $stickies ) ) { |
|
2425 |
$stickies = array( $post_id ); |
|
2426 |
} |
|
2427 |
||
2428 |
if ( ! in_array( $post_id, $stickies ) ) { |
|
0 | 2429 |
$stickies[] = $post_id; |
9 | 2430 |
} |
0 | 2431 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2432 |
$updated = update_option( 'sticky_posts', $stickies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2433 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2434 |
if ( $updated ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2435 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2436 |
* 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
|
2437 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2438 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2439 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2440 |
* @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
|
2441 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2442 |
do_action( 'post_stuck', $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2443 |
} |
0 | 2444 |
} |
2445 |
||
2446 |
/** |
|
5 | 2447 |
* Un-stick a post. |
0 | 2448 |
* |
2449 |
* Sticky posts should be displayed at the top of the front page. |
|
2450 |
* |
|
2451 |
* @since 2.7.0 |
|
2452 |
* |
|
2453 |
* @param int $post_id Post ID. |
|
2454 |
*/ |
|
5 | 2455 |
function unstick_post( $post_id ) { |
9 | 2456 |
$stickies = get_option( 'sticky_posts' ); |
2457 |
||
2458 |
if ( ! is_array( $stickies ) ) { |
|
0 | 2459 |
return; |
9 | 2460 |
} |
2461 |
||
2462 |
if ( ! in_array( $post_id, $stickies ) ) { |
|
0 | 2463 |
return; |
9 | 2464 |
} |
2465 |
||
2466 |
$offset = array_search( $post_id, $stickies ); |
|
2467 |
if ( false === $offset ) { |
|
0 | 2468 |
return; |
9 | 2469 |
} |
2470 |
||
2471 |
array_splice( $stickies, $offset, 1 ); |
|
0 | 2472 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2473 |
$updated = update_option( 'sticky_posts', $stickies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2474 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2475 |
if ( $updated ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2476 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2477 |
* 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
|
2478 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2479 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2480 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2481 |
* @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
|
2482 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2483 |
do_action( 'post_unstuck', $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2484 |
} |
0 | 2485 |
} |
2486 |
||
2487 |
/** |
|
5 | 2488 |
* Return the cache key for wp_count_posts() based on the passed arguments. |
2489 |
* |
|
2490 |
* @since 3.9.0 |
|
9 | 2491 |
* @access private |
5 | 2492 |
* |
2493 |
* @param string $type Optional. Post type to retrieve count Default 'post'. |
|
2494 |
* @param string $perm Optional. 'readable' or empty. Default empty. |
|
2495 |
* @return string The cache key. |
|
2496 |
*/ |
|
2497 |
function _count_posts_cache_key( $type = 'post', $perm = '' ) { |
|
2498 |
$cache_key = 'posts-' . $type; |
|
2499 |
if ( 'readable' == $perm && is_user_logged_in() ) { |
|
2500 |
$post_type_object = get_post_type_object( $type ); |
|
2501 |
if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
|
2502 |
$cache_key .= '_' . $perm . '_' . get_current_user_id(); |
|
2503 |
} |
|
2504 |
} |
|
2505 |
return $cache_key; |
|
2506 |
} |
|
2507 |
||
2508 |
/** |
|
0 | 2509 |
* Count number of posts of a post type and if user has permissions to view. |
2510 |
* |
|
2511 |
* This function provides an efficient method of finding the amount of post's |
|
2512 |
* type a blog has. Another method is to count the amount of items in |
|
2513 |
* get_posts(), but that method has a lot of overhead with doing so. Therefore, |
|
2514 |
* when developing for 2.5+, use this function instead. |
|
2515 |
* |
|
2516 |
* The $perm parameter checks for 'readable' value and if the user can read |
|
2517 |
* private posts, it will display that for the user that is signed in. |
|
2518 |
* |
|
2519 |
* @since 2.5.0 |
|
2520 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2521 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2522 |
* |
5 | 2523 |
* @param string $type Optional. Post type to retrieve count. Default 'post'. |
2524 |
* @param string $perm Optional. 'readable' or empty. Default empty. |
|
2525 |
* @return object Number of posts for each status. |
|
0 | 2526 |
*/ |
2527 |
function wp_count_posts( $type = 'post', $perm = '' ) { |
|
2528 |
global $wpdb; |
|
2529 |
||
9 | 2530 |
if ( ! post_type_exists( $type ) ) { |
0 | 2531 |
return new stdClass; |
9 | 2532 |
} |
0 | 2533 |
|
5 | 2534 |
$cache_key = _count_posts_cache_key( $type, $perm ); |
2535 |
||
2536 |
$counts = wp_cache_get( $cache_key, 'counts' ); |
|
2537 |
if ( false !== $counts ) { |
|
2538 |
/** This filter is documented in wp-includes/post.php */ |
|
2539 |
return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|
2540 |
} |
|
0 | 2541 |
|
2542 |
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; |
|
2543 |
if ( 'readable' == $perm && is_user_logged_in() ) { |
|
9 | 2544 |
$post_type_object = get_post_type_object( $type ); |
5 | 2545 |
if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
9 | 2546 |
$query .= $wpdb->prepare( |
2547 |
" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", |
|
5 | 2548 |
get_current_user_id() |
2549 |
); |
|
0 | 2550 |
} |
2551 |
} |
|
2552 |
$query .= ' GROUP BY post_status'; |
|
2553 |
||
5 | 2554 |
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); |
9 | 2555 |
$counts = array_fill_keys( get_post_stati(), 0 ); |
5 | 2556 |
|
2557 |
foreach ( $results as $row ) { |
|
2558 |
$counts[ $row['post_status'] ] = $row['num_posts']; |
|
0 | 2559 |
} |
2560 |
||
5 | 2561 |
$counts = (object) $counts; |
2562 |
wp_cache_set( $cache_key, $counts, 'counts' ); |
|
2563 |
||
0 | 2564 |
/** |
2565 |
* Modify returned post counts by status for the current post type. |
|
2566 |
* |
|
2567 |
* @since 3.7.0 |
|
2568 |
* |
|
5 | 2569 |
* @param object $counts An object containing the current post_type's post |
2570 |
* counts by status. |
|
2571 |
* @param string $type Post type. |
|
2572 |
* @param string $perm The permission to determine if the posts are 'readable' |
|
2573 |
* by the current user. |
|
0 | 2574 |
*/ |
2575 |
return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|
2576 |
} |
|
2577 |
||
2578 |
/** |
|
2579 |
* Count number of attachments for the mime type(s). |
|
2580 |
* |
|
2581 |
* If you set the optional mime_type parameter, then an array will still be |
|
2582 |
* returned, but will only have the item you are looking for. It does not give |
|
2583 |
* you the number of attachments that are children of a post. You can get that |
|
2584 |
* by counting the number of children that post has. |
|
2585 |
* |
|
2586 |
* @since 2.5.0 |
|
2587 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2588 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2589 |
* |
5 | 2590 |
* @param string|array $mime_type Optional. Array or comma-separated list of |
2591 |
* MIME patterns. Default empty. |
|
2592 |
* @return object An object containing the attachment counts by mime type. |
|
0 | 2593 |
*/ |
2594 |
function wp_count_attachments( $mime_type = '' ) { |
|
2595 |
global $wpdb; |
|
2596 |
||
9 | 2597 |
$and = wp_post_mime_type_where( $mime_type ); |
0 | 2598 |
$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 ); |
2599 |
||
2600 |
$counts = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2601 |
foreach ( (array) $count as $row ) { |
0 | 2602 |
$counts[ $row['post_mime_type'] ] = $row['num_posts']; |
2603 |
} |
|
9 | 2604 |
$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); |
0 | 2605 |
|
2606 |
/** |
|
2607 |
* Modify returned attachment counts by mime type. |
|
2608 |
* |
|
2609 |
* @since 3.7.0 |
|
2610 |
* |
|
5 | 2611 |
* @param object $counts An object containing the attachment counts by |
2612 |
* mime type. |
|
2613 |
* @param string $mime_type The mime type pattern used to filter the attachments |
|
2614 |
* counted. |
|
0 | 2615 |
*/ |
2616 |
return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); |
|
2617 |
} |
|
2618 |
||
2619 |
/** |
|
5 | 2620 |
* Get default post mime types. |
0 | 2621 |
* |
2622 |
* @since 2.9.0 |
|
2623 |
* |
|
5 | 2624 |
* @return array List of post mime types. |
0 | 2625 |
*/ |
2626 |
function get_post_mime_types() { |
|
9 | 2627 |
$post_mime_types = array( // array( adj, noun ) |
2628 |
'image' => array( __( 'Images' ), __( 'Manage Images' ), _n_noop( 'Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>' ) ), |
|
2629 |
'audio' => array( __( 'Audio' ), __( 'Manage Audio' ), _n_noop( 'Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>' ) ), |
|
2630 |
'video' => array( __( 'Video' ), __( 'Manage Video' ), _n_noop( 'Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>' ) ), |
|
0 | 2631 |
); |
2632 |
||
5 | 2633 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2634 |
* Filters the default list of post mime types. |
5 | 2635 |
* |
2636 |
* @since 2.5.0 |
|
2637 |
* |
|
2638 |
* @param array $post_mime_types Default list of post mime types. |
|
2639 |
*/ |
|
2640 |
return apply_filters( 'post_mime_types', $post_mime_types ); |
|
0 | 2641 |
} |
2642 |
||
2643 |
/** |
|
2644 |
* Check a MIME-Type against a list. |
|
2645 |
* |
|
2646 |
* If the wildcard_mime_types parameter is a string, it must be comma separated |
|
2647 |
* list. If the real_mime_types is a string, it is also comma separated to |
|
2648 |
* create the list. |
|
2649 |
* |
|
2650 |
* @since 2.5.0 |
|
2651 |
* |
|
5 | 2652 |
* @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*) |
2653 |
* or flash (same as *flash*). |
|
2654 |
* @param string|array $real_mime_types Real post mime type values. |
|
2655 |
* @return array array(wildcard=>array(real types)). |
|
0 | 2656 |
*/ |
5 | 2657 |
function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { |
0 | 2658 |
$matches = array(); |
5 | 2659 |
if ( is_string( $wildcard_mime_types ) ) { |
2660 |
$wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) ); |
|
2661 |
} |
|
2662 |
if ( is_string( $real_mime_types ) ) { |
|
2663 |
$real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) ); |
|
2664 |
} |
|
2665 |
||
2666 |
$patternses = array(); |
|
9 | 2667 |
$wild = '[-._a-z0-9]*'; |
5 | 2668 |
|
0 | 2669 |
foreach ( (array) $wildcard_mime_types as $type ) { |
5 | 2670 |
$mimes = array_map( 'trim', explode( ',', $type ) ); |
2671 |
foreach ( $mimes as $mime ) { |
|
9 | 2672 |
$regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); |
2673 |
$patternses[][ $type ] = "^$regex$"; |
|
5 | 2674 |
if ( false === strpos( $mime, '/' ) ) { |
9 | 2675 |
$patternses[][ $type ] = "^$regex/"; |
2676 |
$patternses[][ $type ] = $regex; |
|
5 | 2677 |
} |
0 | 2678 |
} |
2679 |
} |
|
5 | 2680 |
asort( $patternses ); |
2681 |
||
2682 |
foreach ( $patternses as $patterns ) { |
|
2683 |
foreach ( $patterns as $type => $pattern ) { |
|
2684 |
foreach ( (array) $real_mime_types as $real ) { |
|
9 | 2685 |
if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ] ) ) ) { |
2686 |
$matches[ $type ][] = $real; |
|
5 | 2687 |
} |
2688 |
} |
|
2689 |
} |
|
2690 |
} |
|
0 | 2691 |
return $matches; |
2692 |
} |
|
2693 |
||
2694 |
/** |
|
2695 |
* Convert MIME types into SQL. |
|
2696 |
* |
|
2697 |
* @since 2.5.0 |
|
2698 |
* |
|
5 | 2699 |
* @param string|array $post_mime_types List of mime types or comma separated string |
2700 |
* of mime types. |
|
2701 |
* @param string $table_alias Optional. Specify a table alias, if needed. |
|
2702 |
* Default empty. |
|
0 | 2703 |
* @return string The SQL AND clause for mime searching. |
2704 |
*/ |
|
5 | 2705 |
function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { |
9 | 2706 |
$where = ''; |
2707 |
$wildcards = array( '', '%', '%/%' ); |
|
2708 |
if ( is_string( $post_mime_types ) ) { |
|
2709 |
$post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); |
|
2710 |
} |
|
5 | 2711 |
|
2712 |
$wheres = array(); |
|
2713 |
||
0 | 2714 |
foreach ( (array) $post_mime_types as $mime_type ) { |
9 | 2715 |
$mime_type = preg_replace( '/\s/', '', $mime_type ); |
2716 |
$slashpos = strpos( $mime_type, '/' ); |
|
0 | 2717 |
if ( false !== $slashpos ) { |
9 | 2718 |
$mime_group = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) ); |
2719 |
$mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) ); |
|
2720 |
if ( empty( $mime_subgroup ) ) { |
|
0 | 2721 |
$mime_subgroup = '*'; |
9 | 2722 |
} else { |
2723 |
$mime_subgroup = str_replace( '/', '', $mime_subgroup ); |
|
2724 |
} |
|
0 | 2725 |
$mime_pattern = "$mime_group/$mime_subgroup"; |
2726 |
} else { |
|
9 | 2727 |
$mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); |
2728 |
if ( false === strpos( $mime_pattern, '*' ) ) { |
|
0 | 2729 |
$mime_pattern .= '/*'; |
9 | 2730 |
} |
0 | 2731 |
} |
2732 |
||
9 | 2733 |
$mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); |
2734 |
||
2735 |
if ( in_array( $mime_type, $wildcards ) ) { |
|
0 | 2736 |
return ''; |
9 | 2737 |
} |
2738 |
||
2739 |
if ( false !== strpos( $mime_pattern, '%' ) ) { |
|
2740 |
$wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
|
2741 |
} else { |
|
2742 |
$wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
|
2743 |
} |
|
2744 |
} |
|
2745 |
if ( ! empty( $wheres ) ) { |
|
2746 |
$where = ' AND (' . join( ' OR ', $wheres ) . ') '; |
|
2747 |
} |
|
0 | 2748 |
return $where; |
2749 |
} |
|
2750 |
||
2751 |
/** |
|
5 | 2752 |
* Trash or delete a post or page. |
2753 |
* |
|
2754 |
* When the post and page is permanently deleted, everything that is tied to |
|
2755 |
* it is deleted also. This includes comments, post meta fields, and terms |
|
2756 |
* associated with the post. |
|
2757 |
* |
|
2758 |
* The post or page is moved to trash instead of permanently deleted unless |
|
2759 |
* trash is disabled, item is already in the trash, or $force_delete is true. |
|
0 | 2760 |
* |
2761 |
* @since 1.0.0 |
|
5 | 2762 |
* |
2763 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
2764 |
* @see wp_delete_attachment() |
|
2765 |
* @see wp_trash_post() |
|
2766 |
* |
|
2767 |
* @param int $postid Optional. Post ID. Default 0. |
|
2768 |
* @param bool $force_delete Optional. Whether to bypass trash and force deletion. |
|
2769 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2770 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 2771 |
*/ |
2772 |
function wp_delete_post( $postid = 0, $force_delete = false ) { |
|
2773 |
global $wpdb; |
|
2774 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2775 |
$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
|
2776 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2777 |
if ( ! $post ) { |
0 | 2778 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2779 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2780 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2781 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2782 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2783 |
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
|
2784 |
return wp_trash_post( $postid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2785 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2786 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2787 |
if ( 'attachment' === $post->post_type ) { |
0 | 2788 |
return wp_delete_attachment( $postid, $force_delete ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2789 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2790 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2791 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2792 |
* Filters whether a post deletion should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2793 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2794 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2795 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2796 |
* @param bool $delete Whether to go forward with deletion. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2797 |
* @param WP_Post $post Post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2798 |
* @param bool $force_delete Whether to bypass the trash. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2799 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2800 |
$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
|
2801 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2802 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2803 |
} |
0 | 2804 |
|
5 | 2805 |
/** |
2806 |
* Fires before a post is deleted, at the start of wp_delete_post(). |
|
2807 |
* |
|
2808 |
* @since 3.2.0 |
|
2809 |
* |
|
2810 |
* @see wp_delete_post() |
|
2811 |
* |
|
2812 |
* @param int $postid Post ID. |
|
2813 |
*/ |
|
2814 |
do_action( 'before_delete_post', $postid ); |
|
0 | 2815 |
|
9 | 2816 |
delete_post_meta( $postid, '_wp_trash_meta_status' ); |
2817 |
delete_post_meta( $postid, '_wp_trash_meta_time' ); |
|
2818 |
||
2819 |
wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) ); |
|
2820 |
||
2821 |
$parent_data = array( 'post_parent' => $post->post_parent ); |
|
0 | 2822 |
$parent_where = array( 'post_parent' => $postid ); |
2823 |
||
2824 |
if ( is_post_type_hierarchical( $post->post_type ) ) { |
|
5 | 2825 |
// Point children of this page to its parent, also clean the cache of affected children. |
0 | 2826 |
$children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); |
9 | 2827 |
$children = $wpdb->get_results( $children_query ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2828 |
if ( $children ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2829 |
$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
|
2830 |
} |
0 | 2831 |
} |
2832 |
||
5 | 2833 |
// Do raw query. wp_get_post_revisions() is filtered. |
0 | 2834 |
$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
2835 |
// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
|
9 | 2836 |
foreach ( $revision_ids as $revision_id ) { |
0 | 2837 |
wp_delete_post_revision( $revision_id ); |
9 | 2838 |
} |
0 | 2839 |
|
5 | 2840 |
// Point all attachments to this post up one level. |
0 | 2841 |
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
2842 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2843 |
wp_defer_comment_counting( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2844 |
|
9 | 2845 |
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2846 |
foreach ( $comment_ids as $comment_id ) { |
0 | 2847 |
wp_delete_comment( $comment_id, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2848 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2849 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2850 |
wp_defer_comment_counting( false ); |
0 | 2851 |
|
9 | 2852 |
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) ); |
2853 |
foreach ( $post_meta_ids as $mid ) { |
|
0 | 2854 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 2855 |
} |
0 | 2856 |
|
5 | 2857 |
/** |
2858 |
* Fires immediately before a post is deleted from the database. |
|
2859 |
* |
|
2860 |
* @since 1.2.0 |
|
2861 |
* |
|
2862 |
* @param int $postid Post ID. |
|
2863 |
*/ |
|
0 | 2864 |
do_action( 'delete_post', $postid ); |
5 | 2865 |
$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); |
2866 |
if ( ! $result ) { |
|
2867 |
return false; |
|
2868 |
} |
|
2869 |
||
2870 |
/** |
|
2871 |
* Fires immediately after a post is deleted from the database. |
|
2872 |
* |
|
2873 |
* @since 2.2.0 |
|
2874 |
* |
|
2875 |
* @param int $postid Post ID. |
|
2876 |
*/ |
|
0 | 2877 |
do_action( 'deleted_post', $postid ); |
2878 |
||
2879 |
clean_post_cache( $post ); |
|
2880 |
||
2881 |
if ( is_post_type_hierarchical( $post->post_type ) && $children ) { |
|
9 | 2882 |
foreach ( $children as $child ) { |
0 | 2883 |
clean_post_cache( $child ); |
9 | 2884 |
} |
2885 |
} |
|
2886 |
||
2887 |
wp_clear_scheduled_hook( 'publish_future_post', array( $postid ) ); |
|
0 | 2888 |
|
5 | 2889 |
/** |
2890 |
* Fires after a post is deleted, at the conclusion of wp_delete_post(). |
|
2891 |
* |
|
2892 |
* @since 3.2.0 |
|
2893 |
* |
|
2894 |
* @see wp_delete_post() |
|
2895 |
* |
|
2896 |
* @param int $postid Post ID. |
|
2897 |
*/ |
|
2898 |
do_action( 'after_delete_post', $postid ); |
|
0 | 2899 |
|
2900 |
return $post; |
|
2901 |
} |
|
2902 |
||
2903 |
/** |
|
5 | 2904 |
* Reset the page_on_front, show_on_front, and page_for_post settings when |
2905 |
* a linked page is deleted or trashed. |
|
0 | 2906 |
* |
2907 |
* Also ensures the post is no longer sticky. |
|
2908 |
* |
|
5 | 2909 |
* @since 3.7.0 |
0 | 2910 |
* @access private |
5 | 2911 |
* |
2912 |
* @param int $post_id Post ID. |
|
0 | 2913 |
*/ |
2914 |
function _reset_front_page_settings_for_post( $post_id ) { |
|
2915 |
$post = get_post( $post_id ); |
|
2916 |
if ( 'page' == $post->post_type ) { |
|
9 | 2917 |
/* |
2918 |
* If the page is defined in option page_on_front or post_for_posts, |
|
2919 |
* adjust the corresponding options. |
|
2920 |
*/ |
|
0 | 2921 |
if ( get_option( 'page_on_front' ) == $post->ID ) { |
2922 |
update_option( 'show_on_front', 'posts' ); |
|
2923 |
update_option( 'page_on_front', 0 ); |
|
2924 |
} |
|
2925 |
if ( get_option( 'page_for_posts' ) == $post->ID ) { |
|
2926 |
delete_option( 'page_for_posts', 0 ); |
|
2927 |
} |
|
2928 |
} |
|
2929 |
unstick_post( $post->ID ); |
|
2930 |
} |
|
2931 |
||
2932 |
/** |
|
5 | 2933 |
* Move a post or page to the Trash |
0 | 2934 |
* |
2935 |
* If trash is disabled, the post or page is permanently deleted. |
|
2936 |
* |
|
2937 |
* @since 2.9.0 |
|
5 | 2938 |
* |
2939 |
* @see wp_delete_post() |
|
2940 |
* |
|
2941 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post |
|
2942 |
* if EMPTY_TRASH_DAYS equals true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2943 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 2944 |
*/ |
5 | 2945 |
function wp_trash_post( $post_id = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2946 |
if ( ! EMPTY_TRASH_DAYS ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2947 |
return wp_delete_post( $post_id, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2948 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2949 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2950 |
$post = get_post( $post_id ); |
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 ( ! $post ) { |
0 | 2953 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2954 |
} |
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 |
if ( 'trash' === $post->post_status ) { |
0 | 2957 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2958 |
} |
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 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2961 |
* Filters whether a post trashing should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2962 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2963 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2964 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2965 |
* @param bool $trash Whether to go forward with trashing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2966 |
* @param WP_Post $post Post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2967 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2968 |
$check = apply_filters( 'pre_trash_post', null, $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2969 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2970 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2971 |
} |
0 | 2972 |
|
5 | 2973 |
/** |
2974 |
* Fires before a post is sent to the trash. |
|
2975 |
* |
|
2976 |
* @since 3.3.0 |
|
2977 |
* |
|
2978 |
* @param int $post_id Post ID. |
|
2979 |
*/ |
|
2980 |
do_action( 'wp_trash_post', $post_id ); |
|
0 | 2981 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2982 |
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
|
2983 |
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
|
2984 |
|
9 | 2985 |
wp_update_post( |
2986 |
array( |
|
2987 |
'ID' => $post_id, |
|
2988 |
'post_status' => 'trash', |
|
2989 |
) |
|
2990 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2991 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2992 |
wp_trash_post_comments( $post_id ); |
0 | 2993 |
|
5 | 2994 |
/** |
2995 |
* Fires after a post is sent to the trash. |
|
2996 |
* |
|
2997 |
* @since 2.9.0 |
|
2998 |
* |
|
2999 |
* @param int $post_id Post ID. |
|
3000 |
*/ |
|
3001 |
do_action( 'trashed_post', $post_id ); |
|
0 | 3002 |
|
3003 |
return $post; |
|
3004 |
} |
|
3005 |
||
3006 |
/** |
|
5 | 3007 |
* Restore a post or page from the Trash. |
0 | 3008 |
* |
3009 |
* @since 2.9.0 |
|
5 | 3010 |
* |
3011 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3012 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 3013 |
*/ |
5 | 3014 |
function wp_untrash_post( $post_id = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3015 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3016 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3017 |
if ( ! $post ) { |
0 | 3018 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3019 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3020 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3021 |
if ( 'trash' !== $post->post_status ) { |
0 | 3022 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3023 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3024 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3025 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3026 |
* Filters whether a post untrashing should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3027 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3028 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3029 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3030 |
* @param bool $untrash Whether to go forward with untrashing. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3031 |
* @param WP_Post $post Post object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3032 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3033 |
$check = apply_filters( 'pre_untrash_post', null, $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3034 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3035 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3036 |
} |
0 | 3037 |
|
5 | 3038 |
/** |
3039 |
* Fires before a post is restored from the trash. |
|
3040 |
* |
|
3041 |
* @since 2.9.0 |
|
3042 |
* |
|
3043 |
* @param int $post_id Post ID. |
|
3044 |
*/ |
|
3045 |
do_action( 'untrash_post', $post_id ); |
|
0 | 3046 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3047 |
$post_status = get_post_meta( $post_id, '_wp_trash_meta_status', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3048 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3049 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3050 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3051 |
|
9 | 3052 |
wp_update_post( |
3053 |
array( |
|
3054 |
'ID' => $post_id, |
|
3055 |
'post_status' => $post_status, |
|
3056 |
) |
|
3057 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3058 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3059 |
wp_untrash_post_comments( $post_id ); |
0 | 3060 |
|
5 | 3061 |
/** |
3062 |
* Fires after a post is restored from the trash. |
|
3063 |
* |
|
3064 |
* @since 2.9.0 |
|
3065 |
* |
|
3066 |
* @param int $post_id Post ID. |
|
3067 |
*/ |
|
3068 |
do_action( 'untrashed_post', $post_id ); |
|
0 | 3069 |
|
3070 |
return $post; |
|
3071 |
} |
|
3072 |
||
3073 |
/** |
|
5 | 3074 |
* Moves comments for a post to the trash. |
0 | 3075 |
* |
3076 |
* @since 2.9.0 |
|
5 | 3077 |
* |
3078 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
3079 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3080 |
* @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
|
3081 |
* @return mixed|void False on failure. |
0 | 3082 |
*/ |
5 | 3083 |
function wp_trash_post_comments( $post = null ) { |
0 | 3084 |
global $wpdb; |
3085 |
||
9 | 3086 |
$post = get_post( $post ); |
3087 |
if ( empty( $post ) ) { |
|
0 | 3088 |
return; |
9 | 3089 |
} |
0 | 3090 |
|
3091 |
$post_id = $post->ID; |
|
3092 |
||
5 | 3093 |
/** |
3094 |
* Fires before comments are sent to the trash. |
|
3095 |
* |
|
3096 |
* @since 2.9.0 |
|
3097 |
* |
|
3098 |
* @param int $post_id Post ID. |
|
3099 |
*/ |
|
3100 |
do_action( 'trash_post_comments', $post_id ); |
|
0 | 3101 |
|
9 | 3102 |
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); |
3103 |
if ( empty( $comments ) ) { |
|
0 | 3104 |
return; |
9 | 3105 |
} |
0 | 3106 |
|
5 | 3107 |
// Cache current status for each comment. |
0 | 3108 |
$statuses = array(); |
9 | 3109 |
foreach ( $comments as $comment ) { |
3110 |
$statuses[ $comment->comment_ID ] = $comment->comment_approved; |
|
3111 |
} |
|
3112 |
add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses ); |
|
0 | 3113 |
|
5 | 3114 |
// Set status for all comments to post-trashed. |
9 | 3115 |
$result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) ); |
3116 |
||
3117 |
clean_comment_cache( array_keys( $statuses ) ); |
|
0 | 3118 |
|
5 | 3119 |
/** |
3120 |
* Fires after comments are sent to the trash. |
|
3121 |
* |
|
3122 |
* @since 2.9.0 |
|
3123 |
* |
|
3124 |
* @param int $post_id Post ID. |
|
3125 |
* @param array $statuses Array of comment statuses. |
|
3126 |
*/ |
|
3127 |
do_action( 'trashed_post_comments', $post_id, $statuses ); |
|
0 | 3128 |
|
3129 |
return $result; |
|
3130 |
} |
|
3131 |
||
3132 |
/** |
|
5 | 3133 |
* Restore comments for a post from the trash. |
0 | 3134 |
* |
3135 |
* @since 2.9.0 |
|
5 | 3136 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3137 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3138 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3139 |
* @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
|
3140 |
* @return true|void |
0 | 3141 |
*/ |
5 | 3142 |
function wp_untrash_post_comments( $post = null ) { |
0 | 3143 |
global $wpdb; |
3144 |
||
9 | 3145 |
$post = get_post( $post ); |
3146 |
if ( empty( $post ) ) { |
|
0 | 3147 |
return; |
9 | 3148 |
} |
0 | 3149 |
|
3150 |
$post_id = $post->ID; |
|
3151 |
||
9 | 3152 |
$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true ); |
3153 |
||
3154 |
if ( empty( $statuses ) ) { |
|
0 | 3155 |
return true; |
9 | 3156 |
} |
0 | 3157 |
|
5 | 3158 |
/** |
3159 |
* Fires before comments are restored for a post from the trash. |
|
3160 |
* |
|
3161 |
* @since 2.9.0 |
|
3162 |
* |
|
3163 |
* @param int $post_id Post ID. |
|
3164 |
*/ |
|
3165 |
do_action( 'untrash_post_comments', $post_id ); |
|
3166 |
||
3167 |
// Restore each comment to its original status. |
|
0 | 3168 |
$group_by_status = array(); |
9 | 3169 |
foreach ( $statuses as $comment_id => $comment_status ) { |
3170 |
$group_by_status[ $comment_status ][] = $comment_id; |
|
3171 |
} |
|
0 | 3172 |
|
3173 |
foreach ( $group_by_status as $status => $comments ) { |
|
3174 |
// Sanity check. This shouldn't happen. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3175 |
if ( 'post-trashed' == $status ) { |
0 | 3176 |
$status = '0'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3177 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3178 |
$comments_in = implode( ', ', array_map( 'intval', $comments ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3179 |
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); |
0 | 3180 |
} |
3181 |
||
9 | 3182 |
clean_comment_cache( array_keys( $statuses ) ); |
3183 |
||
3184 |
delete_post_meta( $post_id, '_wp_trash_meta_comments_status' ); |
|
0 | 3185 |
|
5 | 3186 |
/** |
3187 |
* Fires after comments are restored for a post from the trash. |
|
3188 |
* |
|
3189 |
* @since 2.9.0 |
|
3190 |
* |
|
3191 |
* @param int $post_id Post ID. |
|
3192 |
*/ |
|
3193 |
do_action( 'untrashed_post_comments', $post_id ); |
|
0 | 3194 |
} |
3195 |
||
3196 |
/** |
|
3197 |
* Retrieve the list of categories for a post. |
|
3198 |
* |
|
3199 |
* Compatibility layer for themes and plugins. Also an easy layer of abstraction |
|
3200 |
* away from the complexity of the taxonomy layer. |
|
3201 |
* |
|
3202 |
* @since 2.1.0 |
|
3203 |
* |
|
5 | 3204 |
* @see wp_get_object_terms() |
3205 |
* |
|
3206 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the |
|
3207 |
* global $post. Default 0. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3208 |
* @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
|
3209 |
* See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3210 |
* @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
|
3211 |
* 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3212 |
* is 'ids', an array of category ids. If `$fields` is 'names', an array of category names. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3213 |
* WP_Error object if 'category' taxonomy doesn't exist. |
0 | 3214 |
*/ |
3215 |
function wp_get_post_categories( $post_id = 0, $args = array() ) { |
|
3216 |
$post_id = (int) $post_id; |
|
3217 |
||
9 | 3218 |
$defaults = array( 'fields' => 'ids' ); |
3219 |
$args = wp_parse_args( $args, $defaults ); |
|
3220 |
||
3221 |
$cats = wp_get_object_terms( $post_id, 'category', $args ); |
|
0 | 3222 |
return $cats; |
3223 |
} |
|
3224 |
||
3225 |
/** |
|
3226 |
* Retrieve the tags for a post. |
|
3227 |
* |
|
3228 |
* There is only one default for this function, called 'fields' and by default |
|
3229 |
* 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
|
3230 |
* wp_get_object_terms(). |
0 | 3231 |
* |
3232 |
* @since 2.3.0 |
|
3233 |
* |
|
5 | 3234 |
* @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
|
3235 |
* global $post. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3236 |
* @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
|
3237 |
* See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3238 |
* @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
|
3239 |
* WP_Error object if 'post_tag' taxonomy doesn't exist. |
0 | 3240 |
*/ |
3241 |
function wp_get_post_tags( $post_id = 0, $args = array() ) { |
|
9 | 3242 |
return wp_get_post_terms( $post_id, 'post_tag', $args ); |
0 | 3243 |
} |
3244 |
||
3245 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3246 |
* Retrieves the terms for a post. |
0 | 3247 |
* |
3248 |
* @since 2.8.0 |
|
3249 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3250 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3251 |
* global $post. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3252 |
* @param string|array $taxonomy Optional. The taxonomy slug or array of slugs for which |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3253 |
* to retrieve terms. Default 'post_tag'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3254 |
* @param array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3255 |
* 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
|
3256 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3257 |
* @type string $fields Term fields to retrieve. Default 'all'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3258 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3259 |
* @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
|
3260 |
* WP_Error object if `$taxonomy` doesn't exist. |
0 | 3261 |
*/ |
3262 |
function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { |
|
3263 |
$post_id = (int) $post_id; |
|
3264 |
||
9 | 3265 |
$defaults = array( 'fields' => 'all' ); |
3266 |
$args = wp_parse_args( $args, $defaults ); |
|
3267 |
||
3268 |
$tags = wp_get_object_terms( $post_id, $taxonomy, $args ); |
|
0 | 3269 |
|
3270 |
return $tags; |
|
3271 |
} |
|
3272 |
||
3273 |
/** |
|
5 | 3274 |
* Retrieve a number of recent posts. |
0 | 3275 |
* |
3276 |
* @since 1.0.0 |
|
5 | 3277 |
* |
3278 |
* @see get_posts() |
|
3279 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3280 |
* @param array $args Optional. Arguments to retrieve posts. Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3281 |
* @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which correspond to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3282 |
* a WP_Post object or an associative array, respectively. Default ARRAY_A. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3283 |
* @return array|false Array of recent posts, where the type of each element is determined by $output parameter. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3284 |
* Empty array on failure. |
0 | 3285 |
*/ |
3286 |
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { |
|
3287 |
||
3288 |
if ( is_numeric( $args ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3289 |
_deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); |
0 | 3290 |
$args = array( 'numberposts' => absint( $args ) ); |
3291 |
} |
|
3292 |
||
5 | 3293 |
// Set default arguments. |
0 | 3294 |
$defaults = array( |
9 | 3295 |
'numberposts' => 10, |
3296 |
'offset' => 0, |
|
3297 |
'category' => 0, |
|
3298 |
'orderby' => 'post_date', |
|
3299 |
'order' => 'DESC', |
|
3300 |
'include' => '', |
|
3301 |
'exclude' => '', |
|
3302 |
'meta_key' => '', |
|
3303 |
'meta_value' => '', |
|
3304 |
'post_type' => 'post', |
|
3305 |
'post_status' => 'draft, publish, future, pending, private', |
|
3306 |
'suppress_filters' => true, |
|
0 | 3307 |
); |
3308 |
||
3309 |
$r = wp_parse_args( $args, $defaults ); |
|
3310 |
||
3311 |
$results = get_posts( $r ); |
|
3312 |
||
5 | 3313 |
// Backward compatibility. Prior to 3.1 expected posts to be returned in array. |
9 | 3314 |
if ( ARRAY_A == $output ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3315 |
foreach ( $results as $key => $result ) { |
9 | 3316 |
$results[ $key ] = get_object_vars( $result ); |
0 | 3317 |
} |
3318 |
return $results ? $results : array(); |
|
3319 |
} |
|
3320 |
||
3321 |
return $results ? $results : false; |
|
3322 |
||
3323 |
} |
|
3324 |
||
3325 |
/** |
|
3326 |
* Insert or update a post. |
|
3327 |
* |
|
3328 |
* If the $postarr parameter has 'ID' set to a value, then post will be updated. |
|
3329 |
* |
|
3330 |
* You can set the post date manually, by setting the values for 'post_date' |
|
3331 |
* and 'post_date_gmt' keys. You can close the comments or open the comments by |
|
3332 |
* setting the value for 'comment_status' key. |
|
3333 |
* |
|
3334 |
* @since 1.0.0 |
|
5 | 3335 |
* @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
|
3336 |
* @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. |
5 | 3337 |
* |
3338 |
* @see sanitize_post() |
|
3339 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 3340 |
* |
3341 |
* @param array $postarr { |
|
3342 |
* An array of elements that make up a post to update or insert. |
|
3343 |
* |
|
5 | 3344 |
* @type int $ID The post ID. If equal to something other than 0, |
3345 |
* the post with that ID will be updated. Default 0. |
|
3346 |
* @type int $post_author The ID of the user who added the post. Default is |
|
3347 |
* the current user ID. |
|
3348 |
* @type string $post_date The date of the post. Default is the current time. |
|
3349 |
* @type string $post_date_gmt The date of the post in the GMT timezone. Default is |
|
3350 |
* the value of `$post_date`. |
|
3351 |
* @type mixed $post_content The post content. Default empty. |
|
3352 |
* @type string $post_content_filtered The filtered post content. Default empty. |
|
3353 |
* @type string $post_title The post title. Default empty. |
|
3354 |
* @type string $post_excerpt The post excerpt. Default empty. |
|
3355 |
* @type string $post_status The post status. Default 'draft'. |
|
3356 |
* @type string $post_type The post type. Default 'post'. |
|
3357 |
* @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. |
|
3358 |
* Default is the value of 'default_comment_status' option. |
|
3359 |
* @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. |
|
3360 |
* Default is the value of 'default_ping_status' option. |
|
3361 |
* @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
|
3362 |
* @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
|
3363 |
* when creating a new post. |
5 | 3364 |
* @type string $to_ping Space or carriage return-separated list of URLs to ping. |
3365 |
* Default empty. |
|
3366 |
* @type string $pinged Space or carriage return-separated list of URLs that have |
|
3367 |
* been pinged. Default empty. |
|
3368 |
* @type string $post_modified The date when the post was last modified. Default is |
|
3369 |
* the current time. |
|
3370 |
* @type string $post_modified_gmt The date when the post was last modified in the GMT |
|
3371 |
* timezone. Default is the current time. |
|
3372 |
* @type int $post_parent Set this for the post it belongs to, if any. Default 0. |
|
3373 |
* @type int $menu_order The order the post should be displayed in. Default 0. |
|
3374 |
* @type string $post_mime_type The mime type of the post. Default empty. |
|
3375 |
* @type string $guid Global Unique ID for referencing the post. Default empty. |
|
9 | 3376 |
* @type array $post_category Array of category IDs. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3377 |
* Defaults to value of the 'default_category' option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3378 |
* @type array $tags_input Array of tag names, slugs, or IDs. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3379 |
* @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3380 |
* @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. |
0 | 3381 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3382 |
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
0 | 3383 |
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
3384 |
*/ |
|
3385 |
function wp_insert_post( $postarr, $wp_error = false ) { |
|
3386 |
global $wpdb; |
|
3387 |
||
3388 |
$user_id = get_current_user_id(); |
|
3389 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3390 |
$defaults = array( |
9 | 3391 |
'post_author' => $user_id, |
3392 |
'post_content' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3393 |
'post_content_filtered' => '', |
9 | 3394 |
'post_title' => '', |
3395 |
'post_excerpt' => '', |
|
3396 |
'post_status' => 'draft', |
|
3397 |
'post_type' => 'post', |
|
3398 |
'comment_status' => '', |
|
3399 |
'ping_status' => '', |
|
3400 |
'post_password' => '', |
|
3401 |
'to_ping' => '', |
|
3402 |
'pinged' => '', |
|
3403 |
'post_parent' => 0, |
|
3404 |
'menu_order' => 0, |
|
3405 |
'guid' => '', |
|
3406 |
'import_id' => 0, |
|
3407 |
'context' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3408 |
); |
0 | 3409 |
|
9 | 3410 |
$postarr = wp_parse_args( $postarr, $defaults ); |
3411 |
||
3412 |
unset( $postarr['filter'] ); |
|
3413 |
||
3414 |
$postarr = sanitize_post( $postarr, 'db' ); |
|
0 | 3415 |
|
3416 |
// Are we updating or creating? |
|
3417 |
$post_ID = 0; |
|
9 | 3418 |
$update = false; |
3419 |
$guid = $postarr['guid']; |
|
5 | 3420 |
|
3421 |
if ( ! empty( $postarr['ID'] ) ) { |
|
0 | 3422 |
$update = true; |
3423 |
||
5 | 3424 |
// Get the post ID and GUID. |
9 | 3425 |
$post_ID = $postarr['ID']; |
0 | 3426 |
$post_before = get_post( $post_ID ); |
3427 |
if ( is_null( $post_before ) ) { |
|
5 | 3428 |
if ( $wp_error ) { |
0 | 3429 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
5 | 3430 |
} |
0 | 3431 |
return 0; |
3432 |
} |
|
3433 |
||
9 | 3434 |
$guid = get_post_field( 'guid', $post_ID ); |
3435 |
$previous_status = get_post_field( 'post_status', $post_ID ); |
|
0 | 3436 |
} else { |
3437 |
$previous_status = 'new'; |
|
3438 |
} |
|
3439 |
||
5 | 3440 |
$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; |
3441 |
||
9 | 3442 |
$post_title = $postarr['post_title']; |
5 | 3443 |
$post_content = $postarr['post_content']; |
3444 |
$post_excerpt = $postarr['post_excerpt']; |
|
3445 |
if ( isset( $postarr['post_name'] ) ) { |
|
3446 |
$post_name = $postarr['post_name']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3447 |
} elseif ( $update ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3448 |
// 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
|
3449 |
$post_name = $post_before->post_name; |
0 | 3450 |
} |
3451 |
||
5 | 3452 |
$maybe_empty = 'attachment' !== $post_type |
3453 |
&& ! $post_content && ! $post_title && ! $post_excerpt |
|
3454 |
&& post_type_supports( $post_type, 'editor' ) |
|
3455 |
&& post_type_supports( $post_type, 'title' ) |
|
3456 |
&& post_type_supports( $post_type, 'excerpt' ); |
|
3457 |
||
3458 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3459 |
* Filters whether the post should be considered "empty". |
5 | 3460 |
* |
3461 |
* The post is considered "empty" if both: |
|
3462 |
* 1. The post type supports the title, editor, and excerpt fields |
|
3463 |
* 2. The title, editor, and excerpt fields are all empty |
|
3464 |
* |
|
3465 |
* Returning a truthy value to the filter will effectively short-circuit |
|
3466 |
* the new post being inserted, returning 0. If $wp_error is true, a WP_Error |
|
3467 |
* will be returned instead. |
|
3468 |
* |
|
3469 |
* @since 3.3.0 |
|
3470 |
* |
|
3471 |
* @param bool $maybe_empty Whether the post should be considered "empty". |
|
3472 |
* @param array $postarr Array of post data. |
|
3473 |
*/ |
|
3474 |
if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { |
|
3475 |
if ( $wp_error ) { |
|
3476 |
return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); |
|
3477 |
} else { |
|
3478 |
return 0; |
|
3479 |
} |
|
3480 |
} |
|
3481 |
||
3482 |
$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3483 |
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) { |
5 | 3484 |
$post_status = 'inherit'; |
3485 |
} |
|
3486 |
||
3487 |
if ( ! empty( $postarr['post_category'] ) ) { |
|
3488 |
// Filter out empty terms. |
|
3489 |
$post_category = array_filter( $postarr['post_category'] ); |
|
3490 |
} |
|
0 | 3491 |
|
3492 |
// Make sure we set a valid category. |
|
5 | 3493 |
if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) { |
0 | 3494 |
// 'post' requires at least one category. |
5 | 3495 |
if ( 'post' == $post_type && 'auto-draft' != $post_status ) { |
9 | 3496 |
$post_category = array( get_option( 'default_category' ) ); |
5 | 3497 |
} else { |
0 | 3498 |
$post_category = array(); |
5 | 3499 |
} |
0 | 3500 |
} |
3501 |
||
9 | 3502 |
/* |
3503 |
* Don't allow contributors to set the post slug for pending review posts. |
|
3504 |
* |
|
3505 |
* For new posts check the primitive capability, for updates check the meta capability. |
|
3506 |
*/ |
|
3507 |
$post_type_object = get_post_type_object( $post_type ); |
|
3508 |
||
3509 |
if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
|
3510 |
$post_name = ''; |
|
3511 |
} elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) { |
|
0 | 3512 |
$post_name = ''; |
5 | 3513 |
} |
3514 |
||
3515 |
/* |
|
3516 |
* Create a valid post name. Drafts and pending posts are allowed to have |
|
3517 |
* an empty post name. |
|
3518 |
*/ |
|
9 | 3519 |
if ( empty( $post_name ) ) { |
3520 |
if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { |
|
3521 |
$post_name = sanitize_title( $post_title ); |
|
5 | 3522 |
} else { |
0 | 3523 |
$post_name = ''; |
5 | 3524 |
} |
0 | 3525 |
} else { |
3526 |
// On updates, we need to check to see if it's using the old, fixed sanitization context. |
|
3527 |
$check_name = sanitize_title( $post_name, '', 'old-save' ); |
|
5 | 3528 |
if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { |
0 | 3529 |
$post_name = $check_name; |
5 | 3530 |
} else { // new post, or slug has changed. |
9 | 3531 |
$post_name = sanitize_title( $post_name ); |
5 | 3532 |
} |
3533 |
} |
|
3534 |
||
3535 |
/* |
|
3536 |
* If the post date is empty (due to having been new or a draft) and status |
|
3537 |
* is not 'draft' or 'pending', set date to now. |
|
3538 |
*/ |
|
3539 |
if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3540 |
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3541 |
$post_date = current_time( 'mysql' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3542 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3543 |
$post_date = get_date_from_gmt( $postarr['post_date_gmt'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3544 |
} |
5 | 3545 |
} else { |
3546 |
$post_date = $postarr['post_date']; |
|
0 | 3547 |
} |
3548 |
||
5 | 3549 |
// Validate the date. |
9 | 3550 |
$mm = substr( $post_date, 5, 2 ); |
3551 |
$jj = substr( $post_date, 8, 2 ); |
|
3552 |
$aa = substr( $post_date, 0, 4 ); |
|
5 | 3553 |
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); |
3554 |
if ( ! $valid_date ) { |
|
3555 |
if ( $wp_error ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3556 |
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
5 | 3557 |
} else { |
3558 |
return 0; |
|
0 | 3559 |
} |
5 | 3560 |
} |
3561 |
||
3562 |
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { |
|
3563 |
if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { |
|
3564 |
$post_date_gmt = get_gmt_from_date( $post_date ); |
|
3565 |
} else { |
|
0 | 3566 |
$post_date_gmt = '0000-00-00 00:00:00'; |
5 | 3567 |
} |
3568 |
} else { |
|
3569 |
$post_date_gmt = $postarr['post_date_gmt']; |
|
0 | 3570 |
} |
3571 |
||
3572 |
if ( $update || '0000-00-00 00:00:00' == $post_date ) { |
|
3573 |
$post_modified = current_time( 'mysql' ); |
|
3574 |
$post_modified_gmt = current_time( 'mysql', 1 ); |
|
3575 |
} else { |
|
3576 |
$post_modified = $post_date; |
|
3577 |
$post_modified_gmt = $post_date_gmt; |
|
3578 |
} |
|
3579 |
||
5 | 3580 |
if ( 'attachment' !== $post_type ) { |
3581 |
if ( 'publish' == $post_status ) { |
|
9 | 3582 |
$now = gmdate( 'Y-m-d H:i:59' ); |
3583 |
if ( mysql2date( 'U', $post_date_gmt, false ) > mysql2date( 'U', $now, false ) ) { |
|
5 | 3584 |
$post_status = 'future'; |
3585 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3586 |
} elseif ( 'future' == $post_status ) { |
9 | 3587 |
$now = gmdate( 'Y-m-d H:i:59' ); |
3588 |
if ( mysql2date( 'U', $post_date_gmt, false ) <= mysql2date( 'U', $now, false ) ) { |
|
5 | 3589 |
$post_status = 'publish'; |
3590 |
} |
|
3591 |
} |
|
3592 |
} |
|
3593 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3594 |
// Comment status. |
5 | 3595 |
if ( empty( $postarr['comment_status'] ) ) { |
3596 |
if ( $update ) { |
|
3597 |
$comment_status = 'closed'; |
|
3598 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3599 |
$comment_status = get_default_comment_status( $post_type ); |
5 | 3600 |
} |
3601 |
} else { |
|
3602 |
$comment_status = $postarr['comment_status']; |
|
0 | 3603 |
} |
3604 |
||
5 | 3605 |
// These variables are needed by compact() later. |
3606 |
$post_content_filtered = $postarr['post_content_filtered']; |
|
9 | 3607 |
$post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; |
3608 |
$ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; |
|
3609 |
$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; |
|
3610 |
$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; |
|
3611 |
$import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; |
|
5 | 3612 |
|
3613 |
/* |
|
3614 |
* The 'wp_insert_post_parent' filter expects all variables to be present. |
|
3615 |
* Previously, these variables would have already been extracted |
|
3616 |
*/ |
|
3617 |
if ( isset( $postarr['menu_order'] ) ) { |
|
3618 |
$menu_order = (int) $postarr['menu_order']; |
|
3619 |
} else { |
|
3620 |
$menu_order = 0; |
|
3621 |
} |
|
3622 |
||
3623 |
$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; |
|
3624 |
if ( 'private' == $post_status ) { |
|
3625 |
$post_password = ''; |
|
3626 |
} |
|
3627 |
||
3628 |
if ( isset( $postarr['post_parent'] ) ) { |
|
3629 |
$post_parent = (int) $postarr['post_parent']; |
|
3630 |
} else { |
|
3631 |
$post_parent = 0; |
|
0 | 3632 |
} |
5 | 3633 |
|
9 | 3634 |
$new_postarr = array_merge( |
3635 |
array( |
|
3636 |
'ID' => $post_ID, |
|
3637 |
), |
|
3638 |
compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) |
|
3639 |
); |
|
3640 |
||
5 | 3641 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3642 |
* Filters the post parent -- used to check for and prevent hierarchy loops. |
5 | 3643 |
* |
3644 |
* @since 3.1.0 |
|
3645 |
* |
|
3646 |
* @param int $post_parent Post parent ID. |
|
3647 |
* @param int $post_ID Post ID. |
|
3648 |
* @param array $new_postarr Array of parsed post data. |
|
3649 |
* @param array $postarr Array of sanitized, but otherwise unmodified post data. |
|
3650 |
*/ |
|
9 | 3651 |
$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr ); |
0 | 3652 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3653 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3654 |
* 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
|
3655 |
* reassign it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3656 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3657 |
if ( 'trash' === $previous_status && 'trash' !== $post_status ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3658 |
$desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3659 |
if ( $desired_post_slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3660 |
delete_post_meta( $post_ID, '_wp_desired_post_slug' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3661 |
$post_name = $desired_post_slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3662 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3663 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3664 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3665 |
// 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
|
3666 |
if ( 'trash' !== $post_status && $post_name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3667 |
wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3668 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3669 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3670 |
// 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
|
3671 |
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
|
3672 |
$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
|
3673 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3674 |
|
5 | 3675 |
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); |
3676 |
||
3677 |
// Don't unslash. |
|
3678 |
$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; |
|
3679 |
||
3680 |
// Expected_slashed (everything!). |
|
3681 |
$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' ); |
|
3682 |
||
3683 |
$emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
|
3684 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3685 |
foreach ( $emoji_fields as $emoji_field ) { |
5 | 3686 |
if ( isset( $data[ $emoji_field ] ) ) { |
3687 |
$charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); |
|
3688 |
if ( 'utf8' === $charset ) { |
|
3689 |
$data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); |
|
3690 |
} |
|
3691 |
} |
|
3692 |
} |
|
3693 |
||
3694 |
if ( 'attachment' === $post_type ) { |
|
3695 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3696 |
* Filters attachment post data before it is updated in or added to the database. |
5 | 3697 |
* |
3698 |
* @since 3.9.0 |
|
3699 |
* |
|
3700 |
* @param array $data An array of sanitized attachment post data. |
|
3701 |
* @param array $postarr An array of unsanitized attachment post data. |
|
3702 |
*/ |
|
3703 |
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr ); |
|
3704 |
} else { |
|
3705 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3706 |
* Filters slashed post data just before it is inserted into the database. |
5 | 3707 |
* |
3708 |
* @since 2.7.0 |
|
3709 |
* |
|
3710 |
* @param array $data An array of slashed post data. |
|
3711 |
* @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|
3712 |
*/ |
|
3713 |
$data = apply_filters( 'wp_insert_post_data', $data, $postarr ); |
|
3714 |
} |
|
9 | 3715 |
$data = wp_unslash( $data ); |
0 | 3716 |
$where = array( 'ID' => $post_ID ); |
3717 |
||
3718 |
if ( $update ) { |
|
5 | 3719 |
/** |
3720 |
* Fires immediately before an existing post is updated in the database. |
|
3721 |
* |
|
3722 |
* @since 2.5.0 |
|
3723 |
* |
|
3724 |
* @param int $post_ID Post ID. |
|
3725 |
* @param array $data Array of unslashed post data. |
|
3726 |
*/ |
|
0 | 3727 |
do_action( 'pre_post_update', $post_ID, $data ); |
3728 |
if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
|
5 | 3729 |
if ( $wp_error ) { |
9 | 3730 |
return new WP_Error( 'db_update_error', __( 'Could not update post in the database' ), $wpdb->last_error ); |
5 | 3731 |
} else { |
0 | 3732 |
return 0; |
5 | 3733 |
} |
0 | 3734 |
} |
3735 |
} else { |
|
5 | 3736 |
// If there is a suggested ID, use it if not already present. |
3737 |
if ( ! empty( $import_id ) ) { |
|
0 | 3738 |
$import_id = (int) $import_id; |
9 | 3739 |
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { |
0 | 3740 |
$data['ID'] = $import_id; |
3741 |
} |
|
3742 |
} |
|
3743 |
if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { |
|
5 | 3744 |
if ( $wp_error ) { |
9 | 3745 |
return new WP_Error( 'db_insert_error', __( 'Could not insert post into the database' ), $wpdb->last_error ); |
5 | 3746 |
} else { |
0 | 3747 |
return 0; |
5 | 3748 |
} |
0 | 3749 |
} |
3750 |
$post_ID = (int) $wpdb->insert_id; |
|
3751 |
||
5 | 3752 |
// Use the newly generated $post_ID. |
0 | 3753 |
$where = array( 'ID' => $post_ID ); |
3754 |
} |
|
3755 |
||
5 | 3756 |
if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3757 |
$data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); |
0 | 3758 |
$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
|
3759 |
clean_post_cache( $post_ID ); |
0 | 3760 |
} |
3761 |
||
5 | 3762 |
if ( is_object_in_taxonomy( $post_type, 'category' ) ) { |
0 | 3763 |
wp_set_post_categories( $post_ID, $post_category ); |
5 | 3764 |
} |
3765 |
||
3766 |
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { |
|
3767 |
wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
|
3768 |
} |
|
3769 |
||
3770 |
// New-style support for all custom taxonomies. |
|
3771 |
if ( ! empty( $postarr['tax_input'] ) ) { |
|
3772 |
foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { |
|
9 | 3773 |
$taxonomy_obj = get_taxonomy( $taxonomy ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3774 |
if ( ! $taxonomy_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3775 |
/* translators: %s: taxonomy name */ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3776 |
_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
|
3777 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3778 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3779 |
|
5 | 3780 |
// array = hierarchical, string = non-hierarchical. |
3781 |
if ( is_array( $tags ) ) { |
|
9 | 3782 |
$tags = array_filter( $tags ); |
5 | 3783 |
} |
3784 |
if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
|
0 | 3785 |
wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
5 | 3786 |
} |
0 | 3787 |
} |
3788 |
} |
|
3789 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3790 |
if ( ! empty( $postarr['meta_input'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3791 |
foreach ( $postarr['meta_input'] as $field => $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3792 |
update_post_meta( $post_ID, $field, $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3793 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3794 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3795 |
|
0 | 3796 |
$current_guid = get_post_field( 'guid', $post_ID ); |
3797 |
||
5 | 3798 |
// Set GUID. |
3799 |
if ( ! $update && '' == $current_guid ) { |
|
0 | 3800 |
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
5 | 3801 |
} |
3802 |
||
3803 |
if ( 'attachment' === $postarr['post_type'] ) { |
|
3804 |
if ( ! empty( $postarr['file'] ) ) { |
|
3805 |
update_attached_file( $post_ID, $postarr['file'] ); |
|
3806 |
} |
|
3807 |
||
3808 |
if ( ! empty( $postarr['context'] ) ) { |
|
3809 |
add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); |
|
3810 |
} |
|
3811 |
} |
|
0 | 3812 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3813 |
// Set or remove featured image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3814 |
if ( isset( $postarr['_thumbnail_id'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3815 |
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3816 |
if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3817 |
if ( wp_attachment_is( 'audio', $post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3818 |
$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
|
3819 |
} elseif ( wp_attachment_is( 'video', $post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3820 |
$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
|
3821 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3822 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3823 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3824 |
if ( $thumbnail_support ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3825 |
$thumbnail_id = intval( $postarr['_thumbnail_id'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3826 |
if ( -1 === $thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3827 |
delete_post_thumbnail( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3828 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3829 |
set_post_thumbnail( $post_ID, $thumbnail_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3830 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3831 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3832 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3833 |
|
0 | 3834 |
clean_post_cache( $post_ID ); |
3835 |
||
5 | 3836 |
$post = get_post( $post_ID ); |
3837 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3838 |
if ( ! empty( $postarr['page_template'] ) ) { |
5 | 3839 |
$post->page_template = $postarr['page_template']; |
9 | 3840 |
$page_templates = wp_get_theme()->get_page_templates( $post ); |
5 | 3841 |
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { |
3842 |
if ( $wp_error ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3843 |
return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); |
5 | 3844 |
} |
3845 |
update_post_meta( $post_ID, '_wp_page_template', 'default' ); |
|
3846 |
} else { |
|
3847 |
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); |
|
0 | 3848 |
} |
3849 |
} |
|
3850 |
||
5 | 3851 |
if ( 'attachment' !== $postarr['post_type'] ) { |
3852 |
wp_transition_post_status( $data['post_status'], $previous_status, $post ); |
|
3853 |
} else { |
|
3854 |
if ( $update ) { |
|
3855 |
/** |
|
3856 |
* Fires once an existing attachment has been updated. |
|
3857 |
* |
|
3858 |
* @since 2.0.0 |
|
3859 |
* |
|
3860 |
* @param int $post_ID Attachment ID. |
|
3861 |
*/ |
|
3862 |
do_action( 'edit_attachment', $post_ID ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3863 |
$post_after = get_post( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3864 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3865 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3866 |
* Fires once an existing attachment has been updated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3867 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3868 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3869 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3870 |
* @param int $post_ID Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3871 |
* @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
|
3872 |
* @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
|
3873 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3874 |
do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); |
5 | 3875 |
} else { |
3876 |
||
3877 |
/** |
|
3878 |
* Fires once an attachment has been added. |
|
3879 |
* |
|
3880 |
* @since 2.0.0 |
|
3881 |
* |
|
3882 |
* @param int $post_ID Attachment ID. |
|
3883 |
*/ |
|
3884 |
do_action( 'add_attachment', $post_ID ); |
|
3885 |
} |
|
3886 |
||
3887 |
return $post_ID; |
|
3888 |
} |
|
0 | 3889 |
|
3890 |
if ( $update ) { |
|
5 | 3891 |
/** |
3892 |
* Fires once an existing post has been updated. |
|
3893 |
* |
|
9 | 3894 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
3895 |
* the post type slug. |
|
3896 |
* |
|
3897 |
* @since 5.1.0 |
|
3898 |
* |
|
3899 |
* @param int $post_ID Post ID. |
|
3900 |
* @param WP_Post $post Post object. |
|
3901 |
*/ |
|
3902 |
do_action( "edit_post_{$post->post_type}", $post_ID, $post ); |
|
3903 |
||
3904 |
/** |
|
3905 |
* Fires once an existing post has been updated. |
|
3906 |
* |
|
5 | 3907 |
* @since 1.2.0 |
3908 |
* |
|
3909 |
* @param int $post_ID Post ID. |
|
3910 |
* @param WP_Post $post Post object. |
|
3911 |
*/ |
|
3912 |
do_action( 'edit_post', $post_ID, $post ); |
|
9 | 3913 |
|
3914 |
$post_after = get_post( $post_ID ); |
|
5 | 3915 |
|
3916 |
/** |
|
3917 |
* Fires once an existing post has been updated. |
|
3918 |
* |
|
3919 |
* @since 3.0.0 |
|
3920 |
* |
|
3921 |
* @param int $post_ID Post ID. |
|
3922 |
* @param WP_Post $post_after Post object following the update. |
|
3923 |
* @param WP_Post $post_before Post object before the update. |
|
3924 |
*/ |
|
9 | 3925 |
do_action( 'post_updated', $post_ID, $post_after, $post_before ); |
0 | 3926 |
} |
3927 |
||
5 | 3928 |
/** |
3929 |
* Fires once a post has been saved. |
|
3930 |
* |
|
3931 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
|
3932 |
* the post type slug. |
|
3933 |
* |
|
3934 |
* @since 3.7.0 |
|
3935 |
* |
|
3936 |
* @param int $post_ID Post ID. |
|
3937 |
* @param WP_Post $post Post object. |
|
3938 |
* @param bool $update Whether this is an existing post being updated or not. |
|
3939 |
*/ |
|
0 | 3940 |
do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); |
5 | 3941 |
|
3942 |
/** |
|
3943 |
* Fires once a post has been saved. |
|
3944 |
* |
|
3945 |
* @since 1.5.0 |
|
3946 |
* |
|
3947 |
* @param int $post_ID Post ID. |
|
3948 |
* @param WP_Post $post Post object. |
|
3949 |
* @param bool $update Whether this is an existing post being updated or not. |
|
3950 |
*/ |
|
0 | 3951 |
do_action( 'save_post', $post_ID, $post, $update ); |
5 | 3952 |
|
3953 |
/** |
|
3954 |
* Fires once a post has been saved. |
|
3955 |
* |
|
3956 |
* @since 2.0.0 |
|
3957 |
* |
|
3958 |
* @param int $post_ID Post ID. |
|
3959 |
* @param WP_Post $post Post object. |
|
3960 |
* @param bool $update Whether this is an existing post being updated or not. |
|
3961 |
*/ |
|
0 | 3962 |
do_action( 'wp_insert_post', $post_ID, $post, $update ); |
3963 |
||
3964 |
return $post_ID; |
|
3965 |
} |
|
3966 |
||
3967 |
/** |
|
3968 |
* Update a post with new post data. |
|
3969 |
* |
|
3970 |
* The date does not have to be set for drafts. You can set the date and it will |
|
3971 |
* not be overridden. |
|
3972 |
* |
|
3973 |
* @since 1.0.0 |
|
3974 |
* |
|
5 | 3975 |
* @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, |
3976 |
* objects are not. Default array. |
|
3977 |
* @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. |
|
0 | 3978 |
* @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. |
3979 |
*/ |
|
3980 |
function wp_update_post( $postarr = array(), $wp_error = false ) { |
|
9 | 3981 |
if ( is_object( $postarr ) ) { |
5 | 3982 |
// Non-escaped post was passed. |
9 | 3983 |
$postarr = get_object_vars( $postarr ); |
3984 |
$postarr = wp_slash( $postarr ); |
|
0 | 3985 |
} |
3986 |
||
5 | 3987 |
// First, get all of the original fields. |
9 | 3988 |
$post = get_post( $postarr['ID'], ARRAY_A ); |
0 | 3989 |
|
3990 |
if ( is_null( $post ) ) { |
|
9 | 3991 |
if ( $wp_error ) { |
0 | 3992 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
9 | 3993 |
} |
0 | 3994 |
return 0; |
3995 |
} |
|
3996 |
||
3997 |
// Escape data pulled from DB. |
|
9 | 3998 |
$post = wp_slash( $post ); |
0 | 3999 |
|
4000 |
// Passed post category list overwrites existing category list if not empty. |
|
9 | 4001 |
if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] ) |
4002 |
&& 0 != count( $postarr['post_category'] ) ) { |
|
0 | 4003 |
$post_cats = $postarr['post_category']; |
9 | 4004 |
} else { |
0 | 4005 |
$post_cats = $post['post_category']; |
9 | 4006 |
} |
0 | 4007 |
|
5 | 4008 |
// Drafts shouldn't be assigned a date unless explicitly done so by the user. |
9 | 4009 |
if ( isset( $post['post_status'] ) && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) && empty( $postarr['edit_date'] ) && |
4010 |
( '0000-00-00 00:00:00' == $post['post_date_gmt'] ) ) { |
|
0 | 4011 |
$clear_date = true; |
9 | 4012 |
} else { |
0 | 4013 |
$clear_date = false; |
9 | 4014 |
} |
0 | 4015 |
|
4016 |
// Merge old and new fields with new fields overwriting old ones. |
|
9 | 4017 |
$postarr = array_merge( $post, $postarr ); |
0 | 4018 |
$postarr['post_category'] = $post_cats; |
4019 |
if ( $clear_date ) { |
|
9 | 4020 |
$postarr['post_date'] = current_time( 'mysql' ); |
0 | 4021 |
$postarr['post_date_gmt'] = ''; |
4022 |
} |
|
4023 |
||
9 | 4024 |
if ( $postarr['post_type'] == 'attachment' ) { |
4025 |
return wp_insert_attachment( $postarr, false, 0, $wp_error ); |
|
4026 |
} |
|
0 | 4027 |
|
4028 |
return wp_insert_post( $postarr, $wp_error ); |
|
4029 |
} |
|
4030 |
||
4031 |
/** |
|
4032 |
* Publish a post by transitioning the post status. |
|
4033 |
* |
|
4034 |
* @since 2.1.0 |
|
5 | 4035 |
* |
4036 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4037 |
* |
|
4038 |
* @param int|WP_Post $post Post ID or post object. |
|
0 | 4039 |
*/ |
4040 |
function wp_publish_post( $post ) { |
|
4041 |
global $wpdb; |
|
4042 |
||
9 | 4043 |
if ( ! $post = get_post( $post ) ) { |
0 | 4044 |
return; |
9 | 4045 |
} |
4046 |
||
4047 |
if ( 'publish' == $post->post_status ) { |
|
0 | 4048 |
return; |
9 | 4049 |
} |
0 | 4050 |
|
4051 |
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); |
|
4052 |
||
4053 |
clean_post_cache( $post->ID ); |
|
4054 |
||
9 | 4055 |
$old_status = $post->post_status; |
0 | 4056 |
$post->post_status = 'publish'; |
4057 |
wp_transition_post_status( 'publish', $old_status, $post ); |
|
4058 |
||
5 | 4059 |
/** This action is documented in wp-includes/post.php */ |
9 | 4060 |
do_action( "edit_post_{$post->post_type}", $post->ID, $post ); |
4061 |
||
4062 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4063 |
do_action( 'edit_post', $post->ID, $post ); |
5 | 4064 |
|
4065 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4066 |
do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
5 | 4067 |
|
4068 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4069 |
do_action( 'save_post', $post->ID, $post, true ); |
5 | 4070 |
|
4071 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4072 |
do_action( 'wp_insert_post', $post->ID, $post, true ); |
4073 |
} |
|
4074 |
||
4075 |
/** |
|
4076 |
* Publish future post and make sure post ID has future post status. |
|
4077 |
* |
|
4078 |
* Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
|
4079 |
* from publishing drafts, etc. |
|
4080 |
* |
|
4081 |
* @since 2.5.0 |
|
4082 |
* |
|
5 | 4083 |
* @param int|WP_Post $post_id Post ID or post object. |
0 | 4084 |
*/ |
5 | 4085 |
function check_and_publish_future_post( $post_id ) { |
9 | 4086 |
$post = get_post( $post_id ); |
4087 |
||
4088 |
if ( empty( $post ) ) { |
|
0 | 4089 |
return; |
9 | 4090 |
} |
4091 |
||
4092 |
if ( 'future' != $post->post_status ) { |
|
0 | 4093 |
return; |
9 | 4094 |
} |
0 | 4095 |
|
4096 |
$time = strtotime( $post->post_date_gmt . ' GMT' ); |
|
4097 |
||
5 | 4098 |
// Uh oh, someone jumped the gun! |
4099 |
if ( $time > time() ) { |
|
0 | 4100 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system |
4101 |
wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); |
|
4102 |
return; |
|
4103 |
} |
|
4104 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4105 |
// wp_publish_post() returns no meaningful value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4106 |
wp_publish_post( $post_id ); |
0 | 4107 |
} |
4108 |
||
4109 |
/** |
|
4110 |
* Computes a unique slug for the post, when given the desired slug and some post details. |
|
4111 |
* |
|
4112 |
* @since 2.8.0 |
|
4113 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4114 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 4115 |
* @global WP_Rewrite $wp_rewrite |
5 | 4116 |
* |
4117 |
* @param string $slug The desired slug (post_name). |
|
4118 |
* @param int $post_ID Post ID. |
|
4119 |
* @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
|
4120 |
* @param string $post_type Post type. |
|
4121 |
* @param int $post_parent Post parent ID. |
|
4122 |
* @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
|
0 | 4123 |
*/ |
4124 |
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
|
9 | 4125 |
if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) || 'user_request' === $post_type ) { |
0 | 4126 |
return $slug; |
9 | 4127 |
} |
4128 |
||
4129 |
/** |
|
4130 |
* Filters the post slug before it is generated to be unique. |
|
4131 |
* |
|
4132 |
* Returning a non-null value will short-circuit the |
|
4133 |
* unique slug generation, returning the passed value instead. |
|
4134 |
* |
|
4135 |
* @since 5.1.0 |
|
4136 |
* |
|
4137 |
* @param string $override_slug Short-circuit return value. |
|
4138 |
* @param string $slug The desired slug (post_name). |
|
4139 |
* @param int $post_ID Post ID. |
|
4140 |
* @param string $post_status The post status. |
|
4141 |
* @param string $post_type Post type. |
|
4142 |
* @param int $post_parent Post parent ID. |
|
4143 |
*/ |
|
4144 |
$override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent ); |
|
4145 |
if ( null !== $override_slug ) { |
|
4146 |
return $override_slug; |
|
4147 |
} |
|
0 | 4148 |
|
4149 |
global $wpdb, $wp_rewrite; |
|
4150 |
||
4151 |
$original_slug = $slug; |
|
4152 |
||
4153 |
$feeds = $wp_rewrite->feeds; |
|
9 | 4154 |
if ( ! is_array( $feeds ) ) { |
0 | 4155 |
$feeds = array(); |
9 | 4156 |
} |
0 | 4157 |
|
4158 |
if ( 'attachment' == $post_type ) { |
|
4159 |
// Attachment slugs must be unique across all types. |
|
9 | 4160 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
0 | 4161 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); |
4162 |
||
5 | 4163 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4164 |
* Filters whether the post slug would make a bad attachment slug. |
5 | 4165 |
* |
4166 |
* @since 3.1.0 |
|
4167 |
* |
|
4168 |
* @param bool $bad_slug Whether the slug would be bad as an attachment slug. |
|
4169 |
* @param string $slug The post slug. |
|
4170 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4171 |
if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { |
0 | 4172 |
$suffix = 2; |
4173 |
do { |
|
9 | 4174 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
0 | 4175 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); |
4176 |
$suffix++; |
|
4177 |
} while ( $post_name_check ); |
|
4178 |
$slug = $alt_post_name; |
|
4179 |
} |
|
5 | 4180 |
} elseif ( is_post_type_hierarchical( $post_type ) ) { |
9 | 4181 |
if ( 'nav_menu_item' == $post_type ) { |
0 | 4182 |
return $slug; |
9 | 4183 |
} |
5 | 4184 |
|
4185 |
/* |
|
4186 |
* Page slugs must be unique within their own trees. Pages are in a separate |
|
4187 |
* namespace than posts so page slugs are allowed to overlap post slugs. |
|
4188 |
*/ |
|
9 | 4189 |
$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 | 4190 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); |
4191 |
||
4192 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4193 |
* Filters whether the post slug would make a bad hierarchical post slug. |
5 | 4194 |
* |
4195 |
* @since 3.1.0 |
|
4196 |
* |
|
4197 |
* @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. |
|
4198 |
* @param string $slug The post slug. |
|
4199 |
* @param string $post_type Post type. |
|
4200 |
* @param int $post_parent Post parent ID. |
|
4201 |
*/ |
|
9 | 4202 |
if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { |
0 | 4203 |
$suffix = 2; |
4204 |
do { |
|
9 | 4205 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5 | 4206 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); |
0 | 4207 |
$suffix++; |
4208 |
} while ( $post_name_check ); |
|
4209 |
$slug = $alt_post_name; |
|
4210 |
} |
|
4211 |
} else { |
|
4212 |
// Post slugs must be unique across all posts. |
|
9 | 4213 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
0 | 4214 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
4215 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4216 |
// Prevent new post slugs that could result in URLs that conflict with date archives. |
9 | 4217 |
$post = get_post( $post_ID ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4218 |
$conflicts_with_date_archive = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4219 |
if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4220 |
$permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4221 |
$postname_index = array_search( '%postname%', $permastructs ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4222 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4223 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4224 |
* Potential date clashes are as follows: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4225 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4226 |
* - Any integer in the first permastruct position could be a year. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4227 |
* - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4228 |
* - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4229 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4230 |
if ( 0 === $postname_index || |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4231 |
( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4232 |
( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4233 |
) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4234 |
$conflicts_with_date_archive = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4235 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4236 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4237 |
|
5 | 4238 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4239 |
* Filters whether the post slug would be bad as a flat slug. |
5 | 4240 |
* |
4241 |
* @since 3.1.0 |
|
4242 |
* |
|
4243 |
* @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
4244 |
* @param string $slug The post slug. |
|
4245 |
* @param string $post_type Post type. |
|
4246 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4247 |
if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { |
0 | 4248 |
$suffix = 2; |
4249 |
do { |
|
9 | 4250 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
0 | 4251 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
4252 |
$suffix++; |
|
4253 |
} while ( $post_name_check ); |
|
4254 |
$slug = $alt_post_name; |
|
4255 |
} |
|
4256 |
} |
|
4257 |
||
5 | 4258 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4259 |
* Filters the unique post slug. |
5 | 4260 |
* |
4261 |
* @since 3.3.0 |
|
4262 |
* |
|
4263 |
* @param string $slug The post slug. |
|
4264 |
* @param int $post_ID Post ID. |
|
4265 |
* @param string $post_status The post status. |
|
4266 |
* @param string $post_type Post type. |
|
4267 |
* @param int $post_parent Post parent ID |
|
4268 |
* @param string $original_slug The original post slug. |
|
4269 |
*/ |
|
0 | 4270 |
return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); |
4271 |
} |
|
4272 |
||
4273 |
/** |
|
5 | 4274 |
* Truncate a post slug. |
0 | 4275 |
* |
4276 |
* @since 3.6.0 |
|
4277 |
* @access private |
|
5 | 4278 |
* |
4279 |
* @see utf8_uri_encode() |
|
4280 |
* |
|
4281 |
* @param string $slug The slug to truncate. |
|
4282 |
* @param int $length Optional. Max length of the slug. Default 200 (characters). |
|
0 | 4283 |
* @return string The truncated slug. |
4284 |
*/ |
|
4285 |
function _truncate_post_slug( $slug, $length = 200 ) { |
|
4286 |
if ( strlen( $slug ) > $length ) { |
|
4287 |
$decoded_slug = urldecode( $slug ); |
|
9 | 4288 |
if ( $decoded_slug === $slug ) { |
0 | 4289 |
$slug = substr( $slug, 0, $length ); |
9 | 4290 |
} else { |
0 | 4291 |
$slug = utf8_uri_encode( $decoded_slug, $length ); |
9 | 4292 |
} |
0 | 4293 |
} |
4294 |
||
4295 |
return rtrim( $slug, '-' ); |
|
4296 |
} |
|
4297 |
||
4298 |
/** |
|
5 | 4299 |
* Add tags to a post. |
4300 |
* |
|
4301 |
* @see wp_set_post_tags() |
|
4302 |
* |
|
0 | 4303 |
* @since 2.3.0 |
4304 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4305 |
* @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
|
4306 |
* @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
|
4307 |
* separated by commas. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4308 |
* @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. |
0 | 4309 |
*/ |
5 | 4310 |
function wp_add_post_tags( $post_id = 0, $tags = '' ) { |
9 | 4311 |
return wp_set_post_tags( $post_id, $tags, true ); |
0 | 4312 |
} |
4313 |
||
4314 |
/** |
|
4315 |
* Set the tags for a post. |
|
4316 |
* |
|
4317 |
* @since 2.3.0 |
|
5 | 4318 |
* |
4319 |
* @see wp_set_object_terms() |
|
4320 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4321 |
* @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
|
4322 |
* @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
|
4323 |
* separated by commas. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4324 |
* @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
|
4325 |
* replace the tags with the new tags. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4326 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
0 | 4327 |
*/ |
4328 |
function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
|
9 | 4329 |
return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); |
0 | 4330 |
} |
4331 |
||
4332 |
/** |
|
4333 |
* Set the terms for a post. |
|
4334 |
* |
|
4335 |
* @since 2.8.0 |
|
5 | 4336 |
* |
4337 |
* @see wp_set_object_terms() |
|
4338 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4339 |
* @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
|
4340 |
* @param string|array $tags Optional. An array of terms to set for the post, or a string of terms |
9 | 4341 |
* separated by commas. Hierarchical taxonomies must always pass IDs rather |
4342 |
* than names so that children with the same names but different parents |
|
4343 |
* aren't confused. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4344 |
* @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4345 |
* @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
|
4346 |
* replace the terms with the new terms. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4347 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
0 | 4348 |
*/ |
4349 |
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { |
|
4350 |
$post_id = (int) $post_id; |
|
4351 |
||
9 | 4352 |
if ( ! $post_id ) { |
0 | 4353 |
return false; |
9 | 4354 |
} |
4355 |
||
4356 |
if ( empty( $tags ) ) { |
|
0 | 4357 |
$tags = array(); |
9 | 4358 |
} |
0 | 4359 |
|
4360 |
if ( ! is_array( $tags ) ) { |
|
4361 |
$comma = _x( ',', 'tag delimiter' ); |
|
9 | 4362 |
if ( ',' !== $comma ) { |
0 | 4363 |
$tags = str_replace( $comma, ',', $tags ); |
9 | 4364 |
} |
0 | 4365 |
$tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) ); |
4366 |
} |
|
4367 |
||
5 | 4368 |
/* |
4369 |
* Hierarchical taxonomies must always pass IDs rather than names so that |
|
4370 |
* children with the same names but different parents aren't confused. |
|
4371 |
*/ |
|
0 | 4372 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
4373 |
$tags = array_unique( array_map( 'intval', $tags ) ); |
|
4374 |
} |
|
4375 |
||
4376 |
return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); |
|
4377 |
} |
|
4378 |
||
4379 |
/** |
|
4380 |
* Set categories for a post. |
|
4381 |
* |
|
4382 |
* If the post categories parameter is not set, then the default category is |
|
4383 |
* going used. |
|
4384 |
* |
|
4385 |
* @since 2.1.0 |
|
4386 |
* |
|
5 | 4387 |
* @param int $post_ID Optional. The Post ID. Does not default to the ID |
4388 |
* of the global $post. Default 0. |
|
9 | 4389 |
* @param array|int $post_categories Optional. List of category IDs, or the ID of a single category. |
5 | 4390 |
* Default empty array. |
9 | 4391 |
* @param bool $append If true, don't delete existing categories, just add on. |
4392 |
* 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
|
4393 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. |
0 | 4394 |
*/ |
4395 |
function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { |
|
9 | 4396 |
$post_ID = (int) $post_ID; |
4397 |
$post_type = get_post_type( $post_ID ); |
|
0 | 4398 |
$post_status = get_post_status( $post_ID ); |
4399 |
// If $post_categories isn't already an array, make it one: |
|
4400 |
$post_categories = (array) $post_categories; |
|
4401 |
if ( empty( $post_categories ) ) { |
|
4402 |
if ( 'post' == $post_type && 'auto-draft' != $post_status ) { |
|
9 | 4403 |
$post_categories = array( get_option( 'default_category' ) ); |
4404 |
$append = false; |
|
0 | 4405 |
} else { |
4406 |
$post_categories = array(); |
|
4407 |
} |
|
5 | 4408 |
} elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) { |
0 | 4409 |
return true; |
4410 |
} |
|
4411 |
||
4412 |
return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); |
|
4413 |
} |
|
4414 |
||
4415 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4416 |
* Fires actions related to the transitioning of a post's status. |
0 | 4417 |
* |
5 | 4418 |
* When a post is saved, the post status is "transitioned" from one status to another, |
4419 |
* 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
|
4420 |
* 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
|
4421 |
* 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
|
4422 |
* {@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
|
4423 |
* that the function does not transition the post object in the database. |
5 | 4424 |
* |
4425 |
* For instance: When publishing a post for the first time, the post status may transition |
|
4426 |
* from 'draft' – or some other status – to 'publish'. However, if a post is already |
|
4427 |
* published and is simply being updated, the "old" and "new" statuses may both be 'publish' |
|
4428 |
* before and after the transition. |
|
0 | 4429 |
* |
4430 |
* @since 2.3.0 |
|
5 | 4431 |
* |
4432 |
* @param string $new_status Transition to this post status. |
|
4433 |
* @param string $old_status Previous post status. |
|
4434 |
* @param WP_Post $post Post data. |
|
0 | 4435 |
*/ |
5 | 4436 |
function wp_transition_post_status( $new_status, $old_status, $post ) { |
4437 |
/** |
|
4438 |
* Fires when a post is transitioned from one status to another. |
|
4439 |
* |
|
4440 |
* @since 2.3.0 |
|
4441 |
* |
|
4442 |
* @param string $new_status New post status. |
|
4443 |
* @param string $old_status Old post status. |
|
4444 |
* @param WP_Post $post Post object. |
|
4445 |
*/ |
|
4446 |
do_action( 'transition_post_status', $new_status, $old_status, $post ); |
|
4447 |
||
4448 |
/** |
|
4449 |
* Fires when a post is transitioned from one status to another. |
|
4450 |
* |
|
4451 |
* The dynamic portions of the hook name, `$new_status` and `$old status`, |
|
4452 |
* refer to the old and new post statuses, respectively. |
|
4453 |
* |
|
4454 |
* @since 2.3.0 |
|
4455 |
* |
|
4456 |
* @param WP_Post $post Post object. |
|
4457 |
*/ |
|
4458 |
do_action( "{$old_status}_to_{$new_status}", $post ); |
|
4459 |
||
4460 |
/** |
|
4461 |
* Fires when a post is transitioned from one status to another. |
|
4462 |
* |
|
4463 |
* The dynamic portions of the hook name, `$new_status` and `$post->post_type`, |
|
4464 |
* refer to the new post status and post type, respectively. |
|
4465 |
* |
|
4466 |
* Please note: When this action is hooked using a particular post status (like |
|
4467 |
* 'publish', as `publish_{$post->post_type}`), it will fire both when a post is |
|
4468 |
* first transitioned to that status from something else, as well as upon |
|
4469 |
* subsequent post updates (old and new status are both the same). |
|
4470 |
* |
|
4471 |
* Therefore, if you are looking to only fire a callback when a post is first |
|
4472 |
* transitioned to a status, use the {@see 'transition_post_status'} hook instead. |
|
4473 |
* |
|
4474 |
* @since 2.3.0 |
|
4475 |
* |
|
4476 |
* @param int $post_id Post ID. |
|
4477 |
* @param WP_Post $post Post object. |
|
4478 |
*/ |
|
4479 |
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); |
|
0 | 4480 |
} |
4481 |
||
4482 |
// |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4483 |
// Comment, trackback, and pingback functions. |
0 | 4484 |
// |
4485 |
||
4486 |
/** |
|
5 | 4487 |
* Add a URL to those already pinged. |
0 | 4488 |
* |
4489 |
* @since 1.5.0 |
|
9 | 4490 |
* @since 4.7.0 `$post_id` can be a WP_Post object. |
4491 |
* @since 4.7.0 `$uri` can be an array of URIs. |
|
5 | 4492 |
* |
4493 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4494 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4495 |
* @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
|
4496 |
* @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
|
4497 |
* @return int|false How many rows were updated. |
0 | 4498 |
*/ |
5 | 4499 |
function add_ping( $post_id, $uri ) { |
0 | 4500 |
global $wpdb; |
7
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 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4503 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4504 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4505 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4506 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4507 |
$pung = trim( $post->pinged ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4508 |
$pung = preg_split( '/\s/', $pung ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4509 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4510 |
if ( is_array( $uri ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4511 |
$pung = array_merge( $pung, $uri ); |
9 | 4512 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4513 |
$pung[] = $uri; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4514 |
} |
9 | 4515 |
$new = implode( "\n", $pung ); |
5 | 4516 |
|
4517 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4518 |
* Filters the new ping URL to add for the given post. |
5 | 4519 |
* |
4520 |
* @since 2.0.0 |
|
4521 |
* |
|
4522 |
* @param string $new New ping URL to add. |
|
4523 |
*/ |
|
4524 |
$new = apply_filters( 'add_ping', $new ); |
|
4525 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4526 |
$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
|
4527 |
clean_post_cache( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4528 |
return $return; |
0 | 4529 |
} |
4530 |
||
4531 |
/** |
|
4532 |
* Retrieve enclosures already enclosed for a post. |
|
4533 |
* |
|
4534 |
* @since 1.5.0 |
|
4535 |
* |
|
4536 |
* @param int $post_id Post ID. |
|
5 | 4537 |
* @return array List of enclosures. |
0 | 4538 |
*/ |
5 | 4539 |
function get_enclosed( $post_id ) { |
0 | 4540 |
$custom_fields = get_post_custom( $post_id ); |
9 | 4541 |
$pung = array(); |
4542 |
if ( ! is_array( $custom_fields ) ) { |
|
0 | 4543 |
return $pung; |
9 | 4544 |
} |
0 | 4545 |
|
4546 |
foreach ( $custom_fields as $key => $val ) { |
|
9 | 4547 |
if ( 'enclosure' != $key || ! is_array( $val ) ) { |
0 | 4548 |
continue; |
9 | 4549 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4550 |
foreach ( $val as $enc ) { |
0 | 4551 |
$enclosure = explode( "\n", $enc ); |
9 | 4552 |
$pung[] = trim( $enclosure[0] ); |
0 | 4553 |
} |
4554 |
} |
|
5 | 4555 |
|
4556 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4557 |
* Filters the list of enclosures already enclosed for the given post. |
5 | 4558 |
* |
4559 |
* @since 2.0.0 |
|
4560 |
* |
|
4561 |
* @param array $pung Array of enclosures for the given post. |
|
4562 |
* @param int $post_id Post ID. |
|
4563 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4564 |
return apply_filters( 'get_enclosed', $pung, $post_id ); |
0 | 4565 |
} |
4566 |
||
4567 |
/** |
|
4568 |
* Retrieve URLs already pinged for a post. |
|
4569 |
* |
|
4570 |
* @since 1.5.0 |
|
5 | 4571 |
* |
9 | 4572 |
* @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
|
4573 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4574 |
* @param int|WP_Post $post_id Post ID or object. |
9 | 4575 |
* @return bool|string[] Array of URLs already pinged for the given post, false if the post is not found. |
0 | 4576 |
*/ |
5 | 4577 |
function get_pung( $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4578 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4579 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4580 |
return false; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4583 |
$pung = trim( $post->pinged ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4584 |
$pung = preg_split( '/\s/', $pung ); |
5 | 4585 |
|
4586 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4587 |
* Filters the list of already-pinged URLs for the given post. |
5 | 4588 |
* |
4589 |
* @since 2.0.0 |
|
4590 |
* |
|
9 | 4591 |
* @param string[] $pung Array of URLs already pinged for the given post. |
5 | 4592 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4593 |
return apply_filters( 'get_pung', $pung ); |
0 | 4594 |
} |
4595 |
||
4596 |
/** |
|
4597 |
* Retrieve URLs that need to be pinged. |
|
4598 |
* |
|
4599 |
* @since 1.5.0 |
|
9 | 4600 |
* @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
|
4601 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4602 |
* @param int|WP_Post $post_id Post Object or ID |
0 | 4603 |
* @return array |
4604 |
*/ |
|
5 | 4605 |
function get_to_ping( $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4606 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4607 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4608 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4609 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4610 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4611 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4612 |
$to_ping = sanitize_trackback_urls( $post->to_ping ); |
9 | 4613 |
$to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY ); |
5 | 4614 |
|
4615 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4616 |
* Filters the list of URLs yet to ping for the given post. |
5 | 4617 |
* |
4618 |
* @since 2.0.0 |
|
4619 |
* |
|
4620 |
* @param array $to_ping List of URLs yet to ping. |
|
4621 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4622 |
return apply_filters( 'get_to_ping', $to_ping ); |
0 | 4623 |
} |
4624 |
||
4625 |
/** |
|
4626 |
* Do trackbacks for a list of URLs. |
|
4627 |
* |
|
4628 |
* @since 1.0.0 |
|
4629 |
* |
|
5 | 4630 |
* @param string $tb_list Comma separated list of URLs. |
4631 |
* @param int $post_id Post ID. |
|
0 | 4632 |
*/ |
5 | 4633 |
function trackback_url_list( $tb_list, $post_id ) { |
0 | 4634 |
if ( ! empty( $tb_list ) ) { |
5 | 4635 |
// Get post data. |
4636 |
$postdata = get_post( $post_id, ARRAY_A ); |
|
4637 |
||
4638 |
// Form an excerpt. |
|
4639 |
$excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); |
|
4640 |
||
4641 |
if ( strlen( $excerpt ) > 255 ) { |
|
4642 |
$excerpt = substr( $excerpt, 0, 252 ) . '…'; |
|
0 | 4643 |
} |
4644 |
||
5 | 4645 |
$trackback_urls = explode( ',', $tb_list ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4646 |
foreach ( (array) $trackback_urls as $tb_url ) { |
5 | 4647 |
$tb_url = trim( $tb_url ); |
4648 |
trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); |
|
0 | 4649 |
} |
4650 |
} |
|
4651 |
} |
|
4652 |
||
4653 |
// |
|
4654 |
// Page functions |
|
4655 |
// |
|
4656 |
||
4657 |
/** |
|
4658 |
* Get a list of page IDs. |
|
4659 |
* |
|
4660 |
* @since 2.0.0 |
|
5 | 4661 |
* |
4662 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 4663 |
* |
4664 |
* @return array List of page IDs. |
|
4665 |
*/ |
|
4666 |
function get_all_page_ids() { |
|
4667 |
global $wpdb; |
|
4668 |
||
9 | 4669 |
$page_ids = wp_cache_get( 'all_page_ids', 'posts' ); |
0 | 4670 |
if ( ! is_array( $page_ids ) ) { |
9 | 4671 |
$page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" ); |
4672 |
wp_cache_add( 'all_page_ids', $page_ids, 'posts' ); |
|
0 | 4673 |
} |
4674 |
||
4675 |
return $page_ids; |
|
4676 |
} |
|
4677 |
||
4678 |
/** |
|
4679 |
* Retrieves page data given a page ID or page object. |
|
4680 |
* |
|
4681 |
* Use get_post() instead of get_page(). |
|
4682 |
* |
|
4683 |
* @since 1.5.1 |
|
5 | 4684 |
* @deprecated 3.5.0 Use get_post() |
4685 |
* |
|
4686 |
* @param mixed $page Page object or page ID. Passed by reference. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4687 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4688 |
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
5 | 4689 |
* @param string $filter Optional. How the return value should be filtered. Accepts 'raw', |
4690 |
* 'edit', 'db', 'display'. Default 'raw'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4691 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 4692 |
*/ |
9 | 4693 |
function get_page( $page, $output = OBJECT, $filter = 'raw' ) { |
0 | 4694 |
return get_post( $page, $output, $filter ); |
4695 |
} |
|
4696 |
||
4697 |
/** |
|
4698 |
* Retrieves a page given its path. |
|
4699 |
* |
|
4700 |
* @since 2.1.0 |
|
5 | 4701 |
* |
4702 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4703 |
* |
|
4704 |
* @param string $page_path Page path. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4705 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4706 |
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
5 | 4707 |
* @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
|
4708 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 4709 |
*/ |
5 | 4710 |
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { |
0 | 4711 |
global $wpdb; |
4712 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4713 |
$last_changed = wp_cache_get_last_changed( 'posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4714 |
|
9 | 4715 |
$hash = md5( $page_path . serialize( $post_type ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4716 |
$cache_key = "get_page_by_path:$hash:$last_changed"; |
9 | 4717 |
$cached = wp_cache_get( $cache_key, 'posts' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4718 |
if ( false !== $cached ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4719 |
// Special case: '0' is a bad `$page_path`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4720 |
if ( '0' === $cached || 0 === $cached ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4721 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4722 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4723 |
return get_post( $cached, $output ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4724 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4725 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4726 |
|
9 | 4727 |
$page_path = rawurlencode( urldecode( $page_path ) ); |
4728 |
$page_path = str_replace( '%2F', '/', $page_path ); |
|
4729 |
$page_path = str_replace( '%20', ' ', $page_path ); |
|
4730 |
$parts = explode( '/', trim( $page_path, '/' ) ); |
|
4731 |
$parts = array_map( 'sanitize_title_for_query', $parts ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4732 |
$escaped_parts = esc_sql( $parts ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4733 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4734 |
$in_string = "'" . implode( "','", $escaped_parts ) . "'"; |
5 | 4735 |
|
4736 |
if ( is_array( $post_type ) ) { |
|
4737 |
$post_types = $post_type; |
|
4738 |
} else { |
|
4739 |
$post_types = array( $post_type, 'attachment' ); |
|
4740 |
} |
|
4741 |
||
9 | 4742 |
$post_types = esc_sql( $post_types ); |
5 | 4743 |
$post_type_in_string = "'" . implode( "','", $post_types ) . "'"; |
9 | 4744 |
$sql = " |
5 | 4745 |
SELECT ID, post_name, post_parent, post_type |
4746 |
FROM $wpdb->posts |
|
4747 |
WHERE post_name IN ($in_string) |
|
4748 |
AND post_type IN ($post_type_in_string) |
|
4749 |
"; |
|
4750 |
||
4751 |
$pages = $wpdb->get_results( $sql, OBJECT_K ); |
|
0 | 4752 |
|
4753 |
$revparts = array_reverse( $parts ); |
|
4754 |
||
4755 |
$foundid = 0; |
|
4756 |
foreach ( (array) $pages as $page ) { |
|
4757 |
if ( $page->post_name == $revparts[0] ) { |
|
4758 |
$count = 0; |
|
9 | 4759 |
$p = $page; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4760 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4761 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4762 |
* 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
|
4763 |
* ensuring each matches the post ancestry. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4764 |
*/ |
0 | 4765 |
while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) { |
4766 |
$count++; |
|
4767 |
$parent = $pages[ $p->post_parent ]; |
|
9 | 4768 |
if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) { |
0 | 4769 |
break; |
9 | 4770 |
} |
0 | 4771 |
$p = $parent; |
4772 |
} |
|
4773 |
||
9 | 4774 |
if ( $p->post_parent == 0 && $count + 1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) { |
0 | 4775 |
$foundid = $page->ID; |
9 | 4776 |
if ( $page->post_type == $post_type ) { |
0 | 4777 |
break; |
9 | 4778 |
} |
0 | 4779 |
} |
4780 |
} |
|
4781 |
} |
|
4782 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4783 |
// We cache misses as well as hits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4784 |
wp_cache_set( $cache_key, $foundid, 'posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4785 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4786 |
if ( $foundid ) { |
0 | 4787 |
return get_post( $foundid, $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4788 |
} |
0 | 4789 |
} |
4790 |
||
4791 |
/** |
|
4792 |
* Retrieve a page given its title. |
|
4793 |
* |
|
4794 |
* @since 2.1.0 |
|
5 | 4795 |
* |
4796 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4797 |
* |
|
4798 |
* @param string $page_title Page title |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4799 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4800 |
* a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
5 | 4801 |
* @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
|
4802 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 4803 |
*/ |
5 | 4804 |
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { |
0 | 4805 |
global $wpdb; |
5 | 4806 |
|
4807 |
if ( is_array( $post_type ) ) { |
|
9 | 4808 |
$post_type = esc_sql( $post_type ); |
5 | 4809 |
$post_type_in_string = "'" . implode( "','", $post_type ) . "'"; |
9 | 4810 |
$sql = $wpdb->prepare( |
4811 |
" |
|
5 | 4812 |
SELECT ID |
4813 |
FROM $wpdb->posts |
|
4814 |
WHERE post_title = %s |
|
4815 |
AND post_type IN ($post_type_in_string) |
|
9 | 4816 |
", |
4817 |
$page_title |
|
4818 |
); |
|
5 | 4819 |
} else { |
9 | 4820 |
$sql = $wpdb->prepare( |
4821 |
" |
|
5 | 4822 |
SELECT ID |
4823 |
FROM $wpdb->posts |
|
4824 |
WHERE post_title = %s |
|
4825 |
AND post_type = %s |
|
9 | 4826 |
", |
4827 |
$page_title, |
|
4828 |
$post_type |
|
4829 |
); |
|
5 | 4830 |
} |
4831 |
||
4832 |
$page = $wpdb->get_var( $sql ); |
|
4833 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4834 |
if ( $page ) { |
0 | 4835 |
return get_post( $page, $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4836 |
} |
0 | 4837 |
} |
4838 |
||
4839 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4840 |
* 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
|
4841 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4842 |
* Descendants are identified from the `$pages` array passed to the function. No database queries are performed. |
0 | 4843 |
* |
4844 |
* @since 1.5.1 |
|
4845 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4846 |
* @param int $page_id Page ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4847 |
* @param array $pages List of page objects from which descendants should be identified. |
5 | 4848 |
* @return array List of page children. |
0 | 4849 |
*/ |
5 | 4850 |
function get_page_children( $page_id, $pages ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4851 |
// Build a hash of ID -> children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4852 |
$children = array(); |
0 | 4853 |
foreach ( (array) $pages as $page ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4854 |
$children[ intval( $page->post_parent ) ][] = $page; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4855 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4856 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4857 |
$page_list = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4858 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4859 |
// Start the search by looking at immediate children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4860 |
if ( isset( $children[ $page_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4861 |
// 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
|
4862 |
$to_look = array_reverse( $children[ $page_id ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4863 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4864 |
while ( $to_look ) { |
9 | 4865 |
$p = array_pop( $to_look ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4866 |
$page_list[] = $p; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4867 |
if ( isset( $children[ $p->ID ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4868 |
foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4869 |
// 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
|
4870 |
$to_look[] = $child; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4871 |
} |
5 | 4872 |
} |
0 | 4873 |
} |
4874 |
} |
|
5 | 4875 |
|
0 | 4876 |
return $page_list; |
4877 |
} |
|
4878 |
||
4879 |
/** |
|
4880 |
* Order the pages with children under parents in a flat list. |
|
4881 |
* |
|
4882 |
* It uses auxiliary structure to hold parent-children relationships and |
|
4883 |
* runs in O(N) complexity |
|
4884 |
* |
|
4885 |
* @since 2.0.0 |
|
4886 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4887 |
* @param array $pages Posts array (passed by reference). |
5 | 4888 |
* @param int $page_id Optional. Parent page ID. Default 0. |
0 | 4889 |
* @return array A list arranged by hierarchy. Children immediately follow their parents. |
4890 |
*/ |
|
4891 |
function get_page_hierarchy( &$pages, $page_id = 0 ) { |
|
4892 |
if ( empty( $pages ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4893 |
return array(); |
0 | 4894 |
} |
4895 |
||
4896 |
$children = array(); |
|
4897 |
foreach ( (array) $pages as $p ) { |
|
9 | 4898 |
$parent_id = intval( $p->post_parent ); |
0 | 4899 |
$children[ $parent_id ][] = $p; |
4900 |
} |
|
4901 |
||
4902 |
$result = array(); |
|
4903 |
_page_traverse_name( $page_id, $children, $result ); |
|
4904 |
||
4905 |
return $result; |
|
4906 |
} |
|
4907 |
||
4908 |
/** |
|
5 | 4909 |
* Traverse and return all the nested children post names of a root page. |
4910 |
* |
|
0 | 4911 |
* $children contains parent-children relations |
4912 |
* |
|
4913 |
* @since 2.9.0 |
|
9 | 4914 |
* @access private |
5 | 4915 |
* |
4916 |
* @see _page_traverse_name() |
|
4917 |
* |
|
4918 |
* @param int $page_id Page ID. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4919 |
* @param array $children Parent-children relations (passed by reference). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4920 |
* @param array $result Result (passed by reference). |
0 | 4921 |
*/ |
9 | 4922 |
function _page_traverse_name( $page_id, &$children, &$result ) { |
4923 |
if ( isset( $children[ $page_id ] ) ) { |
|
4924 |
foreach ( (array) $children[ $page_id ] as $child ) { |
|
0 | 4925 |
$result[ $child->ID ] = $child->post_name; |
4926 |
_page_traverse_name( $child->ID, $children, $result ); |
|
4927 |
} |
|
4928 |
} |
|
4929 |
} |
|
4930 |
||
4931 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4932 |
* Build the URI path for a page. |
0 | 4933 |
* |
4934 |
* Sub pages will be in the "directory" under the parent page post name. |
|
4935 |
* |
|
4936 |
* @since 1.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4937 |
* @since 4.6.0 Converted the `$page` parameter to optional. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4938 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4939 |
* @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. |
0 | 4940 |
* @return string|false Page URI, false on error. |
4941 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4942 |
function get_page_uri( $page = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4943 |
if ( ! $page instanceof WP_Post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4944 |
$page = get_post( $page ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4945 |
} |
0 | 4946 |
|
9 | 4947 |
if ( ! $page ) { |
0 | 4948 |
return false; |
9 | 4949 |
} |
0 | 4950 |
|
4951 |
$uri = $page->post_name; |
|
4952 |
||
4953 |
foreach ( $page->ancestors as $parent ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4954 |
$parent = get_post( $parent ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4955 |
if ( $parent && $parent->post_name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4956 |
$uri = $parent->post_name . '/' . $uri; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4957 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4958 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4959 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4960 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4961 |
* Filters the URI for a page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4962 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4963 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4964 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4965 |
* @param string $uri Page URI. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4966 |
* @param WP_Post $page Page object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4967 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4968 |
return apply_filters( 'get_page_uri', $uri, $page ); |
0 | 4969 |
} |
4970 |
||
4971 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4972 |
* Retrieve a list of pages (or hierarchical post type items). |
0 | 4973 |
* |
5 | 4974 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 4975 |
* |
4976 |
* @since 1.5.0 |
|
4977 |
* |
|
5 | 4978 |
* @param array|string $args { |
4979 |
* Optional. Array or string of arguments to retrieve pages. |
|
4980 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4981 |
* @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
|
4982 |
* 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
|
4983 |
* hierarchical results. Default 0, or no restriction. |
5 | 4984 |
* @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. |
4985 |
* @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', |
|
4986 |
* 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', |
|
4987 |
* 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. |
|
4988 |
* 'post_' can be omitted for any values that start with it. |
|
4989 |
* Default 'post_title'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4990 |
* @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
|
4991 |
* `$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
|
4992 |
* Default true. |
5 | 4993 |
* @type array $exclude Array of page IDs to exclude. Default empty array. |
4994 |
* @type array $include Array of page IDs to include. Cannot be used with `$child_of`, |
|
4995 |
* `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. |
|
4996 |
* Default empty array. |
|
4997 |
* @type string $meta_key Only include pages with this meta key. Default empty. |
|
4998 |
* @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. |
|
4999 |
* Default empty. |
|
5000 |
* @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
|
5001 |
* @type int $parent Page ID to return direct children of. Default -1, or no restriction. |
5 | 5002 |
* @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. |
5003 |
* Default empty array. |
|
5004 |
* @type int $number The number of pages to return. Default 0, or all pages. |
|
5005 |
* @type int $offset The number of pages to skip before returning. Requires `$number`. |
|
5006 |
* Default 0. |
|
5007 |
* @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
|
5008 |
* @type string|array $post_status A comma-separated list or array of post statuses to include. |
5 | 5009 |
* Default 'publish'. |
0 | 5010 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5011 |
* @return array|false List of pages matching defaults or `$args`. |
0 | 5012 |
*/ |
5013 |
function get_pages( $args = array() ) { |
|
5014 |
global $wpdb; |
|
5015 |
||
5016 |
$defaults = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5017 |
'child_of' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5018 |
'sort_order' => 'ASC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5019 |
'sort_column' => 'post_title', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5020 |
'hierarchical' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5021 |
'exclude' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5022 |
'include' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5023 |
'meta_key' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5024 |
'meta_value' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5025 |
'authors' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5026 |
'parent' => -1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5027 |
'exclude_tree' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5028 |
'number' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5029 |
'offset' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5030 |
'post_type' => 'page', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5031 |
'post_status' => 'publish', |
0 | 5032 |
); |
5033 |
||
5034 |
$r = wp_parse_args( $args, $defaults ); |
|
5 | 5035 |
|
9 | 5036 |
$number = (int) $r['number']; |
5037 |
$offset = (int) $r['offset']; |
|
5038 |
$child_of = (int) $r['child_of']; |
|
5 | 5039 |
$hierarchical = $r['hierarchical']; |
9 | 5040 |
$exclude = $r['exclude']; |
5041 |
$meta_key = $r['meta_key']; |
|
5042 |
$meta_value = $r['meta_value']; |
|
5043 |
$parent = $r['parent']; |
|
5044 |
$post_status = $r['post_status']; |
|
5 | 5045 |
|
5046 |
// Make sure the post type is hierarchical. |
|
0 | 5047 |
$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); |
5 | 5048 |
if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) { |
5049 |
return false; |
|
5050 |
} |
|
5051 |
||
5052 |
if ( $parent > 0 && ! $child_of ) { |
|
0 | 5053 |
$hierarchical = false; |
5 | 5054 |
} |
5055 |
||
5056 |
// Make sure we have a valid post status. |
|
5057 |
if ( ! is_array( $post_status ) ) { |
|
0 | 5058 |
$post_status = explode( ',', $post_status ); |
5 | 5059 |
} |
5060 |
if ( array_diff( $post_status, get_post_stati() ) ) { |
|
5061 |
return false; |
|
5062 |
} |
|
5063 |
||
5064 |
// $args can be whatever, only use the args defined in defaults to compute the key. |
|
9 | 5065 |
$key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5066 |
$last_changed = wp_cache_get_last_changed( 'posts' ); |
0 | 5067 |
|
5068 |
$cache_key = "get_pages:$key:$last_changed"; |
|
9 | 5069 |
$cache = wp_cache_get( $cache_key, 'posts' ); |
5070 |
if ( false !== $cache ) { |
|
5 | 5071 |
// Convert to WP_Post instances. |
0 | 5072 |
$pages = array_map( 'get_post', $cache ); |
5 | 5073 |
/** This filter is documented in wp-includes/post.php */ |
5074 |
$pages = apply_filters( 'get_pages', $pages, $r ); |
|
0 | 5075 |
return $pages; |
5076 |
} |
|
5077 |
||
5078 |
$inclusions = ''; |
|
5 | 5079 |
if ( ! empty( $r['include'] ) ) { |
9 | 5080 |
$child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include |
5081 |
$parent = -1; |
|
5082 |
$exclude = ''; |
|
5083 |
$meta_key = ''; |
|
5084 |
$meta_value = ''; |
|
0 | 5085 |
$hierarchical = false; |
9 | 5086 |
$incpages = wp_parse_id_list( $r['include'] ); |
5 | 5087 |
if ( ! empty( $incpages ) ) { |
9 | 5088 |
$inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; |
5 | 5089 |
} |
0 | 5090 |
} |
5091 |
||
5092 |
$exclusions = ''; |
|
5093 |
if ( ! empty( $exclude ) ) { |
|
5094 |
$expages = wp_parse_id_list( $exclude ); |
|
5 | 5095 |
if ( ! empty( $expages ) ) { |
9 | 5096 |
$exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; |
5 | 5097 |
} |
0 | 5098 |
} |
5099 |
||
5100 |
$author_query = ''; |
|
5 | 5101 |
if ( ! empty( $r['authors'] ) ) { |
9 | 5102 |
$post_authors = wp_parse_list( $r['authors'] ); |
0 | 5103 |
|
5104 |
if ( ! empty( $post_authors ) ) { |
|
5105 |
foreach ( $post_authors as $post_author ) { |
|
5106 |
//Do we have an author id or an author login? |
|
9 | 5107 |
if ( 0 == intval( $post_author ) ) { |
5108 |
$post_author = get_user_by( 'login', $post_author ); |
|
5 | 5109 |
if ( empty( $post_author ) ) { |
0 | 5110 |
continue; |
5 | 5111 |
} |
5112 |
if ( empty( $post_author->ID ) ) { |
|
0 | 5113 |
continue; |
5 | 5114 |
} |
0 | 5115 |
$post_author = $post_author->ID; |
5116 |
} |
|
5117 |
||
5 | 5118 |
if ( '' == $author_query ) { |
9 | 5119 |
$author_query = $wpdb->prepare( ' post_author = %d ', $post_author ); |
5 | 5120 |
} else { |
9 | 5121 |
$author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author ); |
5 | 5122 |
} |
0 | 5123 |
} |
5 | 5124 |
if ( '' != $author_query ) { |
0 | 5125 |
$author_query = " AND ($author_query)"; |
5 | 5126 |
} |
0 | 5127 |
} |
5128 |
} |
|
5129 |
||
9 | 5130 |
$join = ''; |
0 | 5131 |
$where = "$exclusions $inclusions "; |
5132 |
if ( '' !== $meta_key || '' !== $meta_value ) { |
|
5133 |
$join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
|
5134 |
||
5135 |
// meta_key and meta_value might be slashed |
|
9 | 5136 |
$meta_key = wp_unslash( $meta_key ); |
5137 |
$meta_value = wp_unslash( $meta_value ); |
|
5 | 5138 |
if ( '' !== $meta_key ) { |
9 | 5139 |
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key ); |
5 | 5140 |
} |
5141 |
if ( '' !== $meta_value ) { |
|
9 | 5142 |
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value ); |
5 | 5143 |
} |
0 | 5144 |
} |
5145 |
||
5146 |
if ( is_array( $parent ) ) { |
|
5147 |
$post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); |
|
5 | 5148 |
if ( ! empty( $post_parent__in ) ) { |
0 | 5149 |
$where .= " AND post_parent IN ($post_parent__in)"; |
5 | 5150 |
} |
0 | 5151 |
} elseif ( $parent >= 0 ) { |
9 | 5152 |
$where .= $wpdb->prepare( ' AND post_parent = %d ', $parent ); |
0 | 5153 |
} |
5154 |
||
5155 |
if ( 1 == count( $post_status ) ) { |
|
9 | 5156 |
$where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $r['post_type'], reset( $post_status ) ); |
0 | 5157 |
} else { |
9 | 5158 |
$post_status = implode( "', '", $post_status ); |
5 | 5159 |
$where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] ); |
0 | 5160 |
} |
5161 |
||
5162 |
$orderby_array = array(); |
|
9 | 5163 |
$allowed_keys = array( |
5164 |
'author', |
|
5165 |
'post_author', |
|
5166 |
'date', |
|
5167 |
'post_date', |
|
5168 |
'title', |
|
5169 |
'post_title', |
|
5170 |
'name', |
|
5171 |
'post_name', |
|
5172 |
'modified', |
|
5173 |
'post_modified', |
|
5174 |
'modified_gmt', |
|
5175 |
'post_modified_gmt', |
|
5176 |
'menu_order', |
|
5177 |
'parent', |
|
5178 |
'post_parent', |
|
5179 |
'ID', |
|
5180 |
'rand', |
|
5181 |
'comment_count', |
|
5182 |
); |
|
5 | 5183 |
|
5184 |
foreach ( explode( ',', $r['sort_column'] ) as $orderby ) { |
|
0 | 5185 |
$orderby = trim( $orderby ); |
5 | 5186 |
if ( ! in_array( $orderby, $allowed_keys ) ) { |
0 | 5187 |
continue; |
5 | 5188 |
} |
0 | 5189 |
|
5190 |
switch ( $orderby ) { |
|
5191 |
case 'menu_order': |
|
5192 |
break; |
|
5193 |
case 'ID': |
|
5194 |
$orderby = "$wpdb->posts.ID"; |
|
5195 |
break; |
|
5196 |
case 'rand': |
|
5197 |
$orderby = 'RAND()'; |
|
5198 |
break; |
|
5199 |
case 'comment_count': |
|
5200 |
$orderby = "$wpdb->posts.comment_count"; |
|
5201 |
break; |
|
5202 |
default: |
|
5 | 5203 |
if ( 0 === strpos( $orderby, 'post_' ) ) { |
0 | 5204 |
$orderby = "$wpdb->posts." . $orderby; |
5 | 5205 |
} else { |
0 | 5206 |
$orderby = "$wpdb->posts.post_" . $orderby; |
5 | 5207 |
} |
0 | 5208 |
} |
5209 |
||
5210 |
$orderby_array[] = $orderby; |
|
5211 |
||
5212 |
} |
|
5213 |
$sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; |
|
5214 |
||
5 | 5215 |
$sort_order = strtoupper( $r['sort_order'] ); |
5216 |
if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) { |
|
0 | 5217 |
$sort_order = 'ASC'; |
5 | 5218 |
} |
0 | 5219 |
|
9 | 5220 |
$query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
0 | 5221 |
$query .= $author_query; |
9 | 5222 |
$query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order; |
0 | 5223 |
|
5 | 5224 |
if ( ! empty( $number ) ) { |
0 | 5225 |
$query .= ' LIMIT ' . $offset . ',' . $number; |
5 | 5226 |
} |
0 | 5227 |
|
9 | 5228 |
$pages = $wpdb->get_results( $query ); |
5229 |
||
5230 |
if ( empty( $pages ) ) { |
|
5231 |
wp_cache_set( $cache_key, array(), 'posts' ); |
|
5232 |
||
5 | 5233 |
/** This filter is documented in wp-includes/post.php */ |
5234 |
$pages = apply_filters( 'get_pages', array(), $r ); |
|
0 | 5235 |
return $pages; |
5236 |
} |
|
5237 |
||
5 | 5238 |
// Sanitize before caching so it'll only get done once. |
9 | 5239 |
$num_pages = count( $pages ); |
5240 |
for ( $i = 0; $i < $num_pages; $i++ ) { |
|
5241 |
$pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); |
|
0 | 5242 |
} |
5243 |
||
5244 |
// Update cache. |
|
5245 |
update_post_cache( $pages ); |
|
5246 |
||
5 | 5247 |
if ( $child_of || $hierarchical ) { |
9 | 5248 |
$pages = get_page_children( $child_of, $pages ); |
5 | 5249 |
} |
5250 |
||
5251 |
if ( ! empty( $r['exclude_tree'] ) ) { |
|
5252 |
$exclude = wp_parse_id_list( $r['exclude_tree'] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5253 |
foreach ( $exclude as $id ) { |
5 | 5254 |
$children = get_page_children( $id, $pages ); |
5255 |
foreach ( $children as $child ) { |
|
5256 |
$exclude[] = $child->ID; |
|
5257 |
} |
|
5258 |
} |
|
5259 |
||
5260 |
$num_pages = count( $pages ); |
|
0 | 5261 |
for ( $i = 0; $i < $num_pages; $i++ ) { |
9 | 5262 |
if ( in_array( $pages[ $i ]->ID, $exclude ) ) { |
5263 |
unset( $pages[ $i ] ); |
|
5 | 5264 |
} |
0 | 5265 |
} |
5266 |
} |
|
5267 |
||
5268 |
$page_structure = array(); |
|
5 | 5269 |
foreach ( $pages as $page ) { |
0 | 5270 |
$page_structure[] = $page->ID; |
5 | 5271 |
} |
0 | 5272 |
|
5273 |
wp_cache_set( $cache_key, $page_structure, 'posts' ); |
|
5274 |
||
5275 |
// Convert to WP_Post instances |
|
5276 |
$pages = array_map( 'get_post', $pages ); |
|
5277 |
||
5 | 5278 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5279 |
* Filters the retrieved list of pages. |
5 | 5280 |
* |
5281 |
* @since 2.1.0 |
|
5282 |
* |
|
5283 |
* @param array $pages List of pages to retrieve. |
|
5284 |
* @param array $r Array of get_pages() arguments. |
|
5285 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5286 |
return apply_filters( 'get_pages', $pages, $r ); |
0 | 5287 |
} |
5288 |
||
5289 |
// |
|
5290 |
// Attachment functions |
|
5291 |
// |
|
5292 |
||
5293 |
/** |
|
9 | 5294 |
* Determines whether an attachment URI is local and really an attachment. |
5295 |
* |
|
5296 |
* For more information on this and similar theme functions, check out |
|
5297 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
5298 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 5299 |
* |
5300 |
* @since 2.0.0 |
|
5301 |
* |
|
5302 |
* @param string $url URL to check |
|
5303 |
* @return bool True on success, false on failure. |
|
5304 |
*/ |
|
9 | 5305 |
function is_local_attachment( $url ) { |
5306 |
if ( strpos( $url, home_url() ) === false ) { |
|
0 | 5307 |
return false; |
9 | 5308 |
} |
5309 |
if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) { |
|
0 | 5310 |
return true; |
9 | 5311 |
} |
5312 |
if ( $id = url_to_postid( $url ) ) { |
|
5313 |
$post = get_post( $id ); |
|
5314 |
if ( 'attachment' == $post->post_type ) { |
|
0 | 5315 |
return true; |
9 | 5316 |
} |
0 | 5317 |
} |
5318 |
return false; |
|
5319 |
} |
|
5320 |
||
5321 |
/** |
|
5322 |
* Insert an attachment. |
|
5323 |
* |
|
5 | 5324 |
* If you set the 'ID' in the $args parameter, it will mean that you are |
0 | 5325 |
* updating and attempt to update the attachment. You can also set the |
5326 |
* attachment name or title by setting the key 'post_name' or 'post_title'. |
|
5327 |
* |
|
5328 |
* You can set the dates for the attachment manually by setting the 'post_date' |
|
5329 |
* and 'post_date_gmt' keys' values. |
|
5330 |
* |
|
5331 |
* By default, the comments will use the default settings for whether the |
|
5332 |
* comments are allowed. You can close them manually or keep them open by |
|
5333 |
* setting the value for the 'comment_status' key. |
|
5334 |
* |
|
5335 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5336 |
* @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. |
5 | 5337 |
* |
5338 |
* @see wp_insert_post() |
|
5339 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5340 |
* @param string|array $args Arguments for inserting an attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5341 |
* @param string $file Optional. Filename. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5342 |
* @param int $parent Optional. Parent post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5343 |
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5344 |
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. |
0 | 5345 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5346 |
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) { |
5 | 5347 |
$defaults = array( |
5348 |
'file' => $file, |
|
9 | 5349 |
'post_parent' => 0, |
5 | 5350 |
); |
5351 |
||
5352 |
$data = wp_parse_args( $args, $defaults ); |
|
5353 |
||
5354 |
if ( ! empty( $parent ) ) { |
|
5355 |
$data['post_parent'] = $parent; |
|
0 | 5356 |
} |
5 | 5357 |
|
5358 |
$data['post_type'] = 'attachment'; |
|
5359 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5360 |
return wp_insert_post( $data, $wp_error ); |
0 | 5361 |
} |
5362 |
||
5363 |
/** |
|
5 | 5364 |
* Trash or delete an attachment. |
0 | 5365 |
* |
5366 |
* When an attachment is permanently deleted, the file will also be removed. |
|
5367 |
* Deletion removes all post meta fields, taxonomy, comments, etc. associated |
|
5368 |
* with the attachment (except the main post). |
|
5369 |
* |
|
5370 |
* The attachment is moved to the trash instead of permanently deleted unless trash |
|
5371 |
* for media is disabled, item is already in the trash, or $force_delete is true. |
|
5372 |
* |
|
5373 |
* @since 2.0.0 |
|
5 | 5374 |
* |
5375 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5376 |
* |
|
5377 |
* @param int $post_id Attachment ID. |
|
5378 |
* @param bool $force_delete Optional. Whether to bypass trash and force deletion. |
|
5379 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5380 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 5381 |
*/ |
5382 |
function wp_delete_attachment( $post_id, $force_delete = false ) { |
|
5383 |
global $wpdb; |
|
5384 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5385 |
$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
|
5386 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5387 |
if ( ! $post ) { |
0 | 5388 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5389 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5390 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5391 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5392 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5393 |
if ( 'attachment' !== $post->post_type ) { |
0 | 5394 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5395 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5396 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5397 |
if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) { |
0 | 5398 |
return wp_trash_post( $post_id ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5399 |
} |
0 | 5400 |
|
9 | 5401 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
5402 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|
5403 |
||
5404 |
$meta = wp_get_attachment_metadata( $post_id ); |
|
0 | 5405 |
$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
9 | 5406 |
$file = get_attached_file( $post_id ); |
5407 |
||
5408 |
if ( is_multisite() ) { |
|
0 | 5409 |
delete_transient( 'dirsize_cache' ); |
9 | 5410 |
} |
0 | 5411 |
|
5 | 5412 |
/** |
5413 |
* Fires before an attachment is deleted, at the start of wp_delete_attachment(). |
|
5414 |
* |
|
5415 |
* @since 2.0.0 |
|
5416 |
* |
|
5417 |
* @param int $post_id Attachment ID. |
|
5418 |
*/ |
|
5419 |
do_action( 'delete_attachment', $post_id ); |
|
0 | 5420 |
|
9 | 5421 |
wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) ); |
5422 |
wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); |
|
0 | 5423 |
|
5 | 5424 |
// Delete all for any posts. |
5425 |
delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); |
|
0 | 5426 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5427 |
wp_defer_comment_counting( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5428 |
|
9 | 5429 |
$comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5430 |
foreach ( $comment_ids as $comment_id ) { |
0 | 5431 |
wp_delete_comment( $comment_id, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5432 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5433 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5434 |
wp_defer_comment_counting( false ); |
0 | 5435 |
|
9 | 5436 |
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) ); |
5437 |
foreach ( $post_meta_ids as $mid ) { |
|
0 | 5438 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 5439 |
} |
0 | 5440 |
|
5 | 5441 |
/** This action is documented in wp-includes/post.php */ |
0 | 5442 |
do_action( 'delete_post', $post_id ); |
5 | 5443 |
$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); |
5444 |
if ( ! $result ) { |
|
5445 |
return false; |
|
5446 |
} |
|
5447 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 5448 |
do_action( 'deleted_post', $post_id ); |
5449 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5450 |
wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); |
0 | 5451 |
|
5452 |
clean_post_cache( $post ); |
|
5453 |
||
5454 |
return $post; |
|
5455 |
} |
|
5456 |
||
5457 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5458 |
* Deletes all files that belong to the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5459 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5460 |
* @since 4.9.7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5461 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5462 |
* @param int $post_id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5463 |
* @param array $meta The attachment's meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5464 |
* @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
|
5465 |
* @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
|
5466 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5467 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5468 |
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
|
5469 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5470 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5471 |
$uploadpath = wp_get_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5472 |
$deleted = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5473 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5474 |
if ( ! empty( $meta['thumb'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5475 |
// 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
|
5476 |
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 | 5477 |
$thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5478 |
if ( ! empty( $thumbfile ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5479 |
$thumbfile = path_join( $uploadpath['basedir'], $thumbfile ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5480 |
$thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5481 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5482 |
if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5483 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5484 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5485 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5486 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5487 |
} |
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 |
// Remove intermediate and backup images if there are any. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5490 |
if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5491 |
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5492 |
foreach ( $meta['sizes'] as $size => $sizeinfo ) { |
9 | 5493 |
$intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5494 |
if ( ! empty( $intermediate_file ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5495 |
$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5496 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5497 |
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
|
5498 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5499 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5500 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5501 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5502 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5503 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5504 |
if ( is_array( $backup_sizes ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5505 |
$del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5506 |
foreach ( $backup_sizes as $size ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5507 |
$del_file = path_join( dirname( $meta['file'] ), $size['file'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5508 |
if ( ! empty( $del_file ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5509 |
$del_file = path_join( $uploadpath['basedir'], $del_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5510 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5511 |
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
|
5512 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5513 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5514 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5515 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5516 |
} |
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 |
if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5519 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5520 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5521 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5522 |
return $deleted; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5525 |
/** |
0 | 5526 |
* Retrieve attachment meta field for attachment ID. |
5527 |
* |
|
5528 |
* @since 2.1.0 |
|
5529 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5530 |
* @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
|
5531 |
* @param bool $unfiltered Optional. If true, filters are not run. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5532 |
* @return mixed Attachment meta field. False on failure. |
0 | 5533 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5534 |
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
|
5535 |
$attachment_id = (int) $attachment_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5536 |
if ( ! $post = get_post( $attachment_id ) ) { |
0 | 5537 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5538 |
} |
0 | 5539 |
|
5540 |
$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); |
|
5541 |
||
9 | 5542 |
if ( $unfiltered ) { |
0 | 5543 |
return $data; |
9 | 5544 |
} |
0 | 5545 |
|
5 | 5546 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5547 |
* Filters the attachment meta data. |
5 | 5548 |
* |
5549 |
* @since 2.1.0 |
|
5550 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5551 |
* @param array|bool $data Array of meta data for the given attachment, or false |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5552 |
* if the object does not exist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5553 |
* @param int $attachment_id Attachment post ID. |
5 | 5554 |
*/ |
0 | 5555 |
return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); |
5556 |
} |
|
5557 |
||
5558 |
/** |
|
5559 |
* Update metadata for an attachment. |
|
5560 |
* |
|
5561 |
* @since 2.1.0 |
|
5562 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5563 |
* @param int $attachment_id Attachment post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5564 |
* @param array $data Attachment meta data. |
5 | 5565 |
* @return int|bool False if $post is invalid. |
0 | 5566 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5567 |
function wp_update_attachment_metadata( $attachment_id, $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5568 |
$attachment_id = (int) $attachment_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5569 |
if ( ! $post = get_post( $attachment_id ) ) { |
0 | 5570 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5571 |
} |
0 | 5572 |
|
5 | 5573 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5574 |
* Filters the updated attachment meta data. |
5 | 5575 |
* |
5576 |
* @since 2.1.0 |
|
5577 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5578 |
* @param array $data Array of updated attachment meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5579 |
* @param int $attachment_id Attachment post ID. |
5 | 5580 |
*/ |
9 | 5581 |
if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) ) { |
0 | 5582 |
return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); |
9 | 5583 |
} else { |
0 | 5584 |
return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
9 | 5585 |
} |
0 | 5586 |
} |
5587 |
||
5588 |
/** |
|
5589 |
* Retrieve the URL for an attachment. |
|
5590 |
* |
|
5591 |
* @since 2.1.0 |
|
5592 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5593 |
* @global string $pagenow |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5594 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5595 |
* @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
|
5596 |
* @return string|false Attachment URL, otherwise false. |
0 | 5597 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5598 |
function wp_get_attachment_url( $attachment_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5599 |
$attachment_id = (int) $attachment_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5600 |
if ( ! $post = get_post( $attachment_id ) ) { |
0 | 5601 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5602 |
} |
0 | 5603 |
|
9 | 5604 |
if ( 'attachment' != $post->post_type ) { |
0 | 5605 |
return false; |
9 | 5606 |
} |
0 | 5607 |
|
5608 |
$url = ''; |
|
5 | 5609 |
// Get attached file. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5610 |
if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) { |
5 | 5611 |
// Get upload directory. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5612 |
if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) { |
5 | 5613 |
// Check that the upload base exists in the file location. |
5614 |
if ( 0 === strpos( $file, $uploads['basedir'] ) ) { |
|
5615 |
// Replace file location with url location. |
|
9 | 5616 |
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); |
5617 |
} elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5618 |
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads) |
9 | 5619 |
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); |
5 | 5620 |
} else { |
5621 |
// It's a newly-uploaded file, therefore $file is relative to the basedir. |
|
5622 |
$url = $uploads['baseurl'] . "/$file"; |
|
5623 |
} |
|
0 | 5624 |
} |
5625 |
} |
|
5626 |
||
5 | 5627 |
/* |
5628 |
* If any of the above options failed, Fallback on the GUID as used pre-2.7, |
|
5629 |
* not recommended to rely upon this. |
|
5630 |
*/ |
|
9 | 5631 |
if ( empty( $url ) ) { |
0 | 5632 |
$url = get_the_guid( $post->ID ); |
5 | 5633 |
} |
5634 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5635 |
// On SSL front end, URLs should be HTTPS. |
5 | 5636 |
if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) { |
5637 |
$url = set_url_scheme( $url ); |
|
5638 |
} |
|
5639 |
||
5640 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5641 |
* Filters the attachment URL. |
5 | 5642 |
* |
5643 |
* @since 2.1.0 |
|
5644 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5645 |
* @param string $url URL for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5646 |
* @param int $attachment_id Attachment post ID. |
5 | 5647 |
*/ |
0 | 5648 |
$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
5649 |
||
9 | 5650 |
if ( empty( $url ) ) { |
0 | 5651 |
return false; |
9 | 5652 |
} |
0 | 5653 |
|
5654 |
return $url; |
|
5655 |
} |
|
5656 |
||
5657 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5658 |
* Retrieves the caption for an attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5659 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5660 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5661 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5662 |
* @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5663 |
* @return string|false False on failure. Attachment caption on success. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5664 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5665 |
function wp_get_attachment_caption( $post_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5666 |
$post_id = (int) $post_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5667 |
if ( ! $post = get_post( $post_id ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5668 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5669 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5670 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5671 |
if ( 'attachment' !== $post->post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5672 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5673 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5674 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5675 |
$caption = $post->post_excerpt; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5676 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5677 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5678 |
* Filters the attachment caption. |
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 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5681 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5682 |
* @param string $caption Caption for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5683 |
* @param int $post_id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5684 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5685 |
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
|
5686 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5687 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5688 |
/** |
0 | 5689 |
* Retrieve thumbnail for an attachment. |
5690 |
* |
|
5691 |
* @since 2.1.0 |
|
5692 |
* |
|
5 | 5693 |
* @param int $post_id Optional. Attachment ID. Default 0. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5694 |
* @return string|false False on failure. Thumbnail file path on success. |
0 | 5695 |
*/ |
5696 |
function wp_get_attachment_thumb_file( $post_id = 0 ) { |
|
5697 |
$post_id = (int) $post_id; |
|
9 | 5698 |
if ( ! $post = get_post( $post_id ) ) { |
0 | 5699 |
return false; |
9 | 5700 |
} |
5701 |
if ( ! is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) { |
|
0 | 5702 |
return false; |
9 | 5703 |
} |
0 | 5704 |
|
5705 |
$file = get_attached_file( $post->ID ); |
|
5706 |
||
9 | 5707 |
if ( ! empty( $imagedata['thumb'] ) && ( $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ) ) && file_exists( $thumbfile ) ) { |
5 | 5708 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5709 |
* Filters the attachment thumbnail file path. |
5 | 5710 |
* |
5711 |
* @since 2.1.0 |
|
5712 |
* |
|
5713 |
* @param string $thumbfile File path to the attachment thumbnail. |
|
5714 |
* @param int $post_id Attachment ID. |
|
5715 |
*/ |
|
0 | 5716 |
return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
5 | 5717 |
} |
0 | 5718 |
return false; |
5719 |
} |
|
5720 |
||
5721 |
/** |
|
5722 |
* Retrieve URL for an attachment thumbnail. |
|
5723 |
* |
|
5724 |
* @since 2.1.0 |
|
5725 |
* |
|
5 | 5726 |
* @param int $post_id Optional. Attachment ID. Default 0. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5727 |
* @return string|false False on failure. Thumbnail URL on success. |
0 | 5728 |
*/ |
5729 |
function wp_get_attachment_thumb_url( $post_id = 0 ) { |
|
5730 |
$post_id = (int) $post_id; |
|
9 | 5731 |
if ( ! $post = get_post( $post_id ) ) { |
0 | 5732 |
return false; |
9 | 5733 |
} |
5734 |
if ( ! $url = wp_get_attachment_url( $post->ID ) ) { |
|
0 | 5735 |
return false; |
9 | 5736 |
} |
0 | 5737 |
|
5738 |
$sized = image_downsize( $post_id, 'thumbnail' ); |
|
9 | 5739 |
if ( $sized ) { |
0 | 5740 |
return $sized[0]; |
9 | 5741 |
} |
5742 |
||
5743 |
if ( ! $thumb = wp_get_attachment_thumb_file( $post->ID ) ) { |
|
0 | 5744 |
return false; |
9 | 5745 |
} |
5746 |
||
5747 |
$url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url ); |
|
0 | 5748 |
|
5 | 5749 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5750 |
* Filters the attachment thumbnail URL. |
5 | 5751 |
* |
5752 |
* @since 2.1.0 |
|
5753 |
* |
|
5754 |
* @param string $url URL for the attachment thumbnail. |
|
5755 |
* @param int $post_id Attachment ID. |
|
5756 |
*/ |
|
0 | 5757 |
return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
5758 |
} |
|
5759 |
||
5760 |
/** |
|
5 | 5761 |
* Verifies an attachment is of a given type. |
5762 |
* |
|
5763 |
* @since 4.2.0 |
|
5764 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5765 |
* @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
|
5766 |
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
5 | 5767 |
* @return bool True if one of the accepted types, false otherwise. |
5768 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5769 |
function wp_attachment_is( $type, $post = null ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5770 |
if ( ! $post = get_post( $post ) ) { |
5 | 5771 |
return false; |
5772 |
} |
|
5773 |
||
5774 |
if ( ! $file = get_attached_file( $post->ID ) ) { |
|
5775 |
return false; |
|
5776 |
} |
|
5777 |
||
5778 |
if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { |
|
5779 |
return true; |
|
5780 |
} |
|
5781 |
||
5782 |
$check = wp_check_filetype( $file ); |
|
5783 |
if ( empty( $check['ext'] ) ) { |
|
5784 |
return false; |
|
5785 |
} |
|
5786 |
||
5787 |
$ext = $check['ext']; |
|
5788 |
||
5789 |
if ( 'import' !== $post->post_mime_type ) { |
|
5790 |
return $type === $ext; |
|
5791 |
} |
|
5792 |
||
5793 |
switch ( $type ) { |
|
9 | 5794 |
case 'image': |
5795 |
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); |
|
5796 |
return in_array( $ext, $image_exts ); |
|
5797 |
||
5798 |
case 'audio': |
|
5799 |
return in_array( $ext, wp_get_audio_extensions() ); |
|
5800 |
||
5801 |
case 'video': |
|
5802 |
return in_array( $ext, wp_get_video_extensions() ); |
|
5803 |
||
5804 |
default: |
|
5805 |
return $type === $ext; |
|
5 | 5806 |
} |
5807 |
} |
|
5808 |
||
5809 |
/** |
|
9 | 5810 |
* Determines whether an attachment is an image. |
5811 |
* |
|
5812 |
* For more information on this and similar theme functions, check out |
|
5813 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
5814 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 5815 |
* |
5816 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5817 |
* @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
|
5818 |
* allowed WP_Post object to be passed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5819 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5820 |
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
5 | 5821 |
* @return bool Whether the attachment is an image. |
0 | 5822 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5823 |
function wp_attachment_is_image( $post = null ) { |
5 | 5824 |
return wp_attachment_is( 'image', $post ); |
0 | 5825 |
} |
5826 |
||
5827 |
/** |
|
5828 |
* Retrieve the icon for a MIME type. |
|
5829 |
* |
|
5830 |
* @since 2.1.0 |
|
5831 |
* |
|
5832 |
* @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
|
5833 |
* @return string|false Icon, false otherwise. |
0 | 5834 |
*/ |
5835 |
function wp_mime_type_icon( $mime = 0 ) { |
|
9 | 5836 |
if ( ! is_numeric( $mime ) ) { |
5837 |
$icon = wp_cache_get( "mime_type_icon_$mime" ); |
|
5838 |
} |
|
0 | 5839 |
|
5840 |
$post_id = 0; |
|
9 | 5841 |
if ( empty( $icon ) ) { |
0 | 5842 |
$post_mimes = array(); |
9 | 5843 |
if ( is_numeric( $mime ) ) { |
0 | 5844 |
$mime = (int) $mime; |
5845 |
if ( $post = get_post( $mime ) ) { |
|
5846 |
$post_id = (int) $post->ID; |
|
9 | 5847 |
$file = get_attached_file( $post_id ); |
5848 |
$ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file ); |
|
5849 |
if ( ! empty( $ext ) ) { |
|
0 | 5850 |
$post_mimes[] = $ext; |
9 | 5851 |
if ( $ext_type = wp_ext2type( $ext ) ) { |
0 | 5852 |
$post_mimes[] = $ext_type; |
9 | 5853 |
} |
0 | 5854 |
} |
5855 |
$mime = $post->post_mime_type; |
|
5856 |
} else { |
|
5857 |
$mime = 0; |
|
5858 |
} |
|
5859 |
} else { |
|
5860 |
$post_mimes[] = $mime; |
|
5861 |
} |
|
5862 |
||
9 | 5863 |
$icon_files = wp_cache_get( 'icon_files' ); |
5864 |
||
5865 |
if ( ! is_array( $icon_files ) ) { |
|
5 | 5866 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5867 |
* Filters the icon directory path. |
5 | 5868 |
* |
5869 |
* @since 2.0.0 |
|
5870 |
* |
|
5871 |
* @param string $path Icon directory absolute path. |
|
5872 |
*/ |
|
5873 |
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); |
|
5874 |
||
5875 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5876 |
* Filters the icon directory URI. |
5 | 5877 |
* |
5878 |
* @since 2.0.0 |
|
5879 |
* |
|
5880 |
* @param string $uri Icon directory URI. |
|
5881 |
*/ |
|
5882 |
$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); |
|
5883 |
||
5884 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5885 |
* Filters the list of icon directory URIs. |
5 | 5886 |
* |
5887 |
* @since 2.5.0 |
|
5888 |
* |
|
5889 |
* @param array $uris List of icon directory URIs. |
|
5890 |
*/ |
|
9 | 5891 |
$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); |
0 | 5892 |
$icon_files = array(); |
5893 |
while ( $dirs ) { |
|
5894 |
$keys = array_keys( $dirs ); |
|
9 | 5895 |
$dir = array_shift( $keys ); |
5896 |
$uri = array_shift( $dirs ); |
|
5897 |
if ( $dh = opendir( $dir ) ) { |
|
5898 |
while ( false !== $file = readdir( $dh ) ) { |
|
5899 |
$file = wp_basename( $file ); |
|
5900 |
if ( substr( $file, 0, 1 ) == '.' ) { |
|
0 | 5901 |
continue; |
5902 |
} |
|
9 | 5903 |
if ( ! in_array( strtolower( substr( $file, -4 ) ), array( '.png', '.gif', '.jpg' ) ) ) { |
5904 |
if ( is_dir( "$dir/$file" ) ) { |
|
5905 |
$dirs[ "$dir/$file" ] = "$uri/$file"; |
|
5906 |
} |
|
5907 |
continue; |
|
5908 |
} |
|
5909 |
$icon_files[ "$dir/$file" ] = "$uri/$file"; |
|
0 | 5910 |
} |
9 | 5911 |
closedir( $dh ); |
0 | 5912 |
} |
5913 |
} |
|
5914 |
wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); |
|
5915 |
} |
|
5916 |
||
5 | 5917 |
$types = array(); |
9 | 5918 |
// Icon wp_basename - extension = MIME wildcard. |
5919 |
foreach ( $icon_files as $file => $uri ) { |
|
5920 |
$types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ]; |
|
0 | 5921 |
} |
5922 |
||
9 | 5923 |
if ( ! empty( $mime ) ) { |
5924 |
$post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) ); |
|
5925 |
$post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 ); |
|
5926 |
$post_mimes[] = str_replace( '/', '_', $mime ); |
|
5927 |
} |
|
5928 |
||
5929 |
$matches = wp_match_mime_types( array_keys( $types ), $post_mimes ); |
|
5930 |
$matches['default'] = array( 'default' ); |
|
0 | 5931 |
|
5932 |
foreach ( $matches as $match => $wilds ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5933 |
foreach ( $wilds as $wild ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5934 |
if ( ! isset( $types[ $wild ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5935 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5936 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5937 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5938 |
$icon = $types[ $wild ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5939 |
if ( ! is_numeric( $mime ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5940 |
wp_cache_add( "mime_type_icon_$mime", $icon ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5941 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5942 |
break 2; |
0 | 5943 |
} |
5944 |
} |
|
5945 |
} |
|
5946 |
||
5 | 5947 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5948 |
* Filters the mime type icon. |
5 | 5949 |
* |
5950 |
* @since 2.1.0 |
|
5951 |
* |
|
5952 |
* @param string $icon Path to the mime type icon. |
|
5953 |
* @param string $mime Mime type. |
|
5954 |
* @param int $post_id Attachment ID. Will equal 0 if the function passed |
|
5955 |
* the mime type. |
|
5956 |
*/ |
|
5957 |
return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); |
|
0 | 5958 |
} |
5959 |
||
5960 |
/** |
|
5 | 5961 |
* Check for changed slugs for published post objects and save the old slug. |
0 | 5962 |
* |
5963 |
* The function is used when a post object of any type is updated, |
|
5964 |
* by comparing the current and previous post objects. |
|
5965 |
* |
|
5966 |
* If the slug was changed and not already part of the old slugs then it will be |
|
5967 |
* added to the post meta field ('_wp_old_slug') for storing old slugs for that |
|
5968 |
* post. |
|
5969 |
* |
|
5970 |
* The most logically usage of this function is redirecting changed post objects, so |
|
5971 |
* that those that linked to an changed post will be redirected to the new post. |
|
5972 |
* |
|
5973 |
* @since 2.1.0 |
|
5974 |
* |
|
5 | 5975 |
* @param int $post_id Post ID. |
5976 |
* @param WP_Post $post The Post Object |
|
5977 |
* @param WP_Post $post_before The Previous Post Object |
|
0 | 5978 |
*/ |
5 | 5979 |
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
|
5980 |
// Don't bother if it hasn't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5981 |
if ( $post->post_name == $post_before->post_name ) { |
0 | 5982 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5983 |
} |
0 | 5984 |
|
5 | 5985 |
// 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
|
5986 |
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { |
0 | 5987 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5988 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5989 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5990 |
$old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); |
0 | 5991 |
|
5 | 5992 |
// If we haven't added this old slug before, add it now. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5993 |
if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5994 |
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
|
5995 |
} |
0 | 5996 |
|
5 | 5997 |
// If the new slug was used previously, delete it from the list. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5998 |
if ( in_array( $post->post_name, $old_slugs ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5999 |
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
|
6000 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6001 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6002 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6003 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6004 |
* 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
|
6005 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6006 |
* 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
|
6007 |
* by comparing the current and previous post objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6008 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6009 |
* 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
|
6010 |
* 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
|
6011 |
* post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6012 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6013 |
* 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
|
6014 |
* 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
|
6015 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6016 |
* @since 4.9.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6017 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6018 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6019 |
* @param WP_Post $post The Post Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6020 |
* @param WP_Post $post_before The Previous Post Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6021 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6022 |
function wp_check_for_changed_dates( $post_id, $post, $post_before ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6023 |
$previous_date = date( 'Y-m-d', strtotime( $post_before->post_date ) ); |
9 | 6024 |
$new_date = date( 'Y-m-d', strtotime( $post->post_date ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6025 |
// Don't bother if it hasn't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6026 |
if ( $new_date == $previous_date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6027 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6028 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6029 |
// We're only concerned with published, non-hierarchical objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6030 |
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
|
6031 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6032 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6033 |
$old_dates = (array) get_post_meta( $post_id, '_wp_old_date' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6034 |
// If we haven't added this old date before, add it now. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6035 |
if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6036 |
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
|
6037 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6038 |
// If the new slug was used previously, delete it from the list. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6039 |
if ( in_array( $new_date, $old_dates ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6040 |
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
|
6041 |
} |
0 | 6042 |
} |
6043 |
||
6044 |
/** |
|
6045 |
* Retrieve the private post SQL based on capability. |
|
6046 |
* |
|
6047 |
* This function provides a standardized way to appropriately select on the |
|
6048 |
* post_status of a post type. The function will return a piece of SQL code |
|
6049 |
* that can be added to a WHERE clause; this SQL is constructed to allow all |
|
6050 |
* published posts, and all private posts to which the user has access. |
|
6051 |
* |
|
6052 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6053 |
* @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
|
6054 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6055 |
* @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. |
0 | 6056 |
* @return string SQL code that can be added to a where clause. |
6057 |
*/ |
|
6058 |
function get_private_posts_cap_sql( $post_type ) { |
|
6059 |
return get_posts_by_author_sql( $post_type, false ); |
|
6060 |
} |
|
6061 |
||
6062 |
/** |
|
6063 |
* Retrieve the post SQL based on capability, author, and type. |
|
6064 |
* |
|
6065 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6066 |
* @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. |
5 | 6067 |
* |
6068 |
* @see get_private_posts_cap_sql() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6069 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6070 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6071 |
* @param array|string $post_type Single post type or an array of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6072 |
* @param bool $full Optional. Returns a full WHERE statement instead of just |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6073 |
* an 'andalso' term. Default true. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6074 |
* @param int $post_author Optional. Query posts having a single author ID. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6075 |
* @param bool $public_only Optional. Only return public posts. Skips cap checks for |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6076 |
* $current_user. Default false. |
0 | 6077 |
* @return string SQL WHERE code that can be added to a query. |
6078 |
*/ |
|
6079 |
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { |
|
6080 |
global $wpdb; |
|
6081 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6082 |
if ( is_array( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6083 |
$post_types = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6084 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6085 |
$post_types = array( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6086 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6087 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6088 |
$post_type_clauses = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6089 |
foreach ( $post_types as $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6090 |
$post_type_obj = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6091 |
if ( ! $post_type_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6092 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6093 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6094 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6095 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6096 |
* 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
|
6097 |
* when generating SQL for getting posts by author. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6098 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6099 |
* @since 2.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6100 |
* @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
|
6101 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6102 |
* @param string $cap Capability. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6103 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6104 |
if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6105 |
$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
|
6106 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6107 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6108 |
// 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
|
6109 |
$post_status_sql = "post_status = 'publish'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6110 |
if ( false === $public_only ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6111 |
if ( $cap ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6112 |
// 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
|
6113 |
$post_status_sql .= " OR post_status = 'private'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6114 |
} elseif ( is_user_logged_in() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6115 |
// Users can view their own private posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6116 |
$id = get_current_user_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6117 |
if ( null === $post_author || ! $full ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6118 |
$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
|
6119 |
} elseif ( $id == (int) $post_author ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6120 |
$post_status_sql .= " OR post_status = 'private'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6121 |
} // else none |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6122 |
} // else none |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6123 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6124 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6125 |
$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
|
6126 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6127 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6128 |
if ( empty( $post_type_clauses ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6129 |
return $full ? 'WHERE 1 = 0' : '1 = 0'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6130 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6131 |
|
9 | 6132 |
$sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )'; |
5 | 6133 |
|
6134 |
if ( null !== $post_author ) { |
|
6135 |
$sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); |
|
6136 |
} |
|
6137 |
||
6138 |
if ( $full ) { |
|
6139 |
$sql = 'WHERE ' . $sql; |
|
6140 |
} |
|
0 | 6141 |
|
6142 |
return $sql; |
|
6143 |
} |
|
6144 |
||
6145 |
/** |
|
6146 |
* Retrieve the date that the last post was published. |
|
6147 |
* |
|
6148 |
* The server timezone is the default and is the difference between GMT and |
|
6149 |
* server time. The 'blog' value is the date when the last post was posted. The |
|
6150 |
* 'gmt' is when the last post was posted in GMT formatted date. |
|
6151 |
* |
|
6152 |
* @since 0.71 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6153 |
* @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
|
6154 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6155 |
* @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
|
6156 |
* 'server' uses the server's internal timezone. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6157 |
* 'blog' uses the `post_modified` field, which proxies to the timezone set for the site. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6158 |
* 'gmt' uses the `post_modified_gmt` field. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6159 |
* Default 'server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6160 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
0 | 6161 |
* @return string The date of the last post. |
6162 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6163 |
function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { |
5 | 6164 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6165 |
* Filters the date the last post was published. |
5 | 6166 |
* |
6167 |
* @since 2.3.0 |
|
6168 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6169 |
* @param string $date Date the last post was published. |
5 | 6170 |
* @param string $timezone Location to use for getting the post published date. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6171 |
* See get_lastpostdate() for accepted `$timezone` values. |
5 | 6172 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6173 |
return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone ); |
0 | 6174 |
} |
6175 |
||
6176 |
/** |
|
5 | 6177 |
* Get the timestamp of the last time any post was modified. |
0 | 6178 |
* |
6179 |
* The server timezone is the default and is the difference between GMT and |
|
6180 |
* server time. The 'blog' value is just when the last post was modified. The |
|
6181 |
* 'gmt' is when the last post was modified in GMT time. |
|
6182 |
* |
|
6183 |
* @since 1.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6184 |
* @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
|
6185 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6186 |
* @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
|
6187 |
* for information on accepted values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6188 |
* Default 'server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6189 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
5 | 6190 |
* @return string The timestamp. |
0 | 6191 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6192 |
function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6193 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6194 |
* 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
|
6195 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6196 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6197 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6198 |
* @param string $lastpostmodified Date the last post was modified. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6199 |
* Returning anything other than false will short-circuit the function. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6200 |
* @param string $timezone Location to use for getting the post modified date. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6201 |
* See get_lastpostdate() for accepted `$timezone` values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6202 |
* @param string $post_type The post type to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6203 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6204 |
$lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6205 |
if ( false !== $lastpostmodified ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6206 |
return $lastpostmodified; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6207 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6208 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6209 |
$lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); |
0 | 6210 |
|
9 | 6211 |
$lastpostdate = get_lastpostdate( $timezone ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6212 |
if ( $lastpostdate > $lastpostmodified ) { |
0 | 6213 |
$lastpostmodified = $lastpostdate; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6214 |
} |
0 | 6215 |
|
5 | 6216 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6217 |
* Filters the date the last post was modified. |
5 | 6218 |
* |
6219 |
* @since 2.3.0 |
|
6220 |
* |
|
6221 |
* @param string $lastpostmodified Date the last post was modified. |
|
6222 |
* @param string $timezone Location to use for getting the post modified date. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6223 |
* See get_lastpostdate() for accepted `$timezone` values. |
5 | 6224 |
*/ |
0 | 6225 |
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); |
6226 |
} |
|
6227 |
||
6228 |
/** |
|
5 | 6229 |
* Get the timestamp of the last time any post was modified or published. |
6230 |
* |
|
0 | 6231 |
* @since 3.1.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6232 |
* @since 4.4.0 The `$post_type` argument was added. |
5 | 6233 |
* @access private |
6234 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6235 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6236 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6237 |
* @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
|
6238 |
* for information on accepted values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6239 |
* @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
|
6240 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6241 |
* @return string|false The timestamp. |
0 | 6242 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6243 |
function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { |
0 | 6244 |
global $wpdb; |
6245 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6246 |
if ( ! in_array( $field, array( 'date', 'modified' ) ) ) { |
0 | 6247 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6248 |
} |
0 | 6249 |
|
6250 |
$timezone = strtolower( $timezone ); |
|
6251 |
||
6252 |
$key = "lastpost{$field}:$timezone"; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6253 |
if ( 'any' !== $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6254 |
$key .= ':' . sanitize_key( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6255 |
} |
0 | 6256 |
|
6257 |
$date = wp_cache_get( $key, 'timeinfo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6258 |
if ( false !== $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6259 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6260 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6261 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6262 |
if ( 'any' === $post_type ) { |
0 | 6263 |
$post_types = get_post_types( array( 'public' => true ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6264 |
array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); |
0 | 6265 |
$post_types = "'" . implode( "', '", $post_types ) . "'"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6266 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6267 |
$post_types = "'" . sanitize_key( $post_type ) . "'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6268 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6269 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6270 |
switch ( $timezone ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6271 |
case 'gmt': |
9 | 6272 |
$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
|
6273 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6274 |
case 'blog': |
9 | 6275 |
$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
|
6276 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6277 |
case 'server': |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6278 |
$add_seconds_server = date( 'Z' ); |
9 | 6279 |
$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
|
6280 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6281 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6282 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6283 |
if ( $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6284 |
wp_cache_set( $key, $date, 'timeinfo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6285 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6286 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6287 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6288 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6289 |
return false; |
0 | 6290 |
} |
6291 |
||
6292 |
/** |
|
6293 |
* Updates posts in cache. |
|
6294 |
* |
|
6295 |
* @since 1.5.1 |
|
6296 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6297 |
* @param array $posts Array of post objects (passed by reference). |
0 | 6298 |
*/ |
6299 |
function update_post_cache( &$posts ) { |
|
9 | 6300 |
if ( ! $posts ) { |
0 | 6301 |
return; |
9 | 6302 |
} |
6303 |
||
6304 |
foreach ( $posts as $post ) { |
|
0 | 6305 |
wp_cache_add( $post->ID, $post, 'posts' ); |
9 | 6306 |
} |
0 | 6307 |
} |
6308 |
||
6309 |
/** |
|
6310 |
* Will clean the post in the cache. |
|
6311 |
* |
|
6312 |
* Cleaning means delete from the cache of the post. Will call to clean the term |
|
6313 |
* object cache associated with the post ID. |
|
6314 |
* |
|
6315 |
* This function not run if $_wp_suspend_cache_invalidation is not empty. See |
|
6316 |
* wp_suspend_cache_invalidation(). |
|
6317 |
* |
|
6318 |
* @since 2.0.0 |
|
6319 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6320 |
* @global bool $_wp_suspend_cache_invalidation |
5 | 6321 |
* |
6322 |
* @param int|WP_Post $post Post ID or post object to remove from the cache. |
|
0 | 6323 |
*/ |
6324 |
function clean_post_cache( $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6325 |
global $_wp_suspend_cache_invalidation; |
0 | 6326 |
|
9 | 6327 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
0 | 6328 |
return; |
9 | 6329 |
} |
0 | 6330 |
|
6331 |
$post = get_post( $post ); |
|
9 | 6332 |
if ( empty( $post ) ) { |
0 | 6333 |
return; |
9 | 6334 |
} |
0 | 6335 |
|
6336 |
wp_cache_delete( $post->ID, 'posts' ); |
|
6337 |
wp_cache_delete( $post->ID, 'post_meta' ); |
|
6338 |
||
6339 |
clean_object_term_cache( $post->ID, $post->post_type ); |
|
6340 |
||
6341 |
wp_cache_delete( 'wp_get_archives', 'general' ); |
|
6342 |
||
5 | 6343 |
/** |
6344 |
* Fires immediately after the given post's cache is cleaned. |
|
6345 |
* |
|
6346 |
* @since 2.5.0 |
|
6347 |
* |
|
6348 |
* @param int $post_id Post ID. |
|
6349 |
* @param WP_Post $post Post object. |
|
6350 |
*/ |
|
0 | 6351 |
do_action( 'clean_post_cache', $post->ID, $post ); |
6352 |
||
6353 |
if ( 'page' == $post->post_type ) { |
|
6354 |
wp_cache_delete( 'all_page_ids', 'posts' ); |
|
5 | 6355 |
|
6356 |
/** |
|
6357 |
* Fires immediately after the given page's cache is cleaned. |
|
6358 |
* |
|
6359 |
* @since 2.5.0 |
|
6360 |
* |
|
6361 |
* @param int $post_id Post ID. |
|
6362 |
*/ |
|
0 | 6363 |
do_action( 'clean_page_cache', $post->ID ); |
6364 |
} |
|
6365 |
||
6366 |
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
6367 |
} |
|
6368 |
||
6369 |
/** |
|
6370 |
* Call major cache updating functions for list of Post objects. |
|
6371 |
* |
|
6372 |
* @since 1.5.0 |
|
6373 |
* |
|
5 | 6374 |
* @param array $posts Array of Post objects |
6375 |
* @param string $post_type Optional. Post type. Default 'post'. |
|
6376 |
* @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
|
6377 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|
0 | 6378 |
*/ |
5 | 6379 |
function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { |
0 | 6380 |
// No point in doing all this work if we didn't match any posts. |
9 | 6381 |
if ( ! $posts ) { |
0 | 6382 |
return; |
9 | 6383 |
} |
6384 |
||
6385 |
update_post_cache( $posts ); |
|
0 | 6386 |
|
6387 |
$post_ids = array(); |
|
9 | 6388 |
foreach ( $posts as $post ) { |
0 | 6389 |
$post_ids[] = $post->ID; |
9 | 6390 |
} |
6391 |
||
6392 |
if ( ! $post_type ) { |
|
0 | 6393 |
$post_type = 'any'; |
9 | 6394 |
} |
0 | 6395 |
|
6396 |
if ( $update_term_cache ) { |
|
9 | 6397 |
if ( is_array( $post_type ) ) { |
0 | 6398 |
$ptypes = $post_type; |
6399 |
} elseif ( 'any' == $post_type ) { |
|
5 | 6400 |
$ptypes = array(); |
0 | 6401 |
// Just use the post_types in the supplied posts. |
5 | 6402 |
foreach ( $posts as $post ) { |
0 | 6403 |
$ptypes[] = $post->post_type; |
5 | 6404 |
} |
9 | 6405 |
$ptypes = array_unique( $ptypes ); |
0 | 6406 |
} else { |
9 | 6407 |
$ptypes = array( $post_type ); |
0 | 6408 |
} |
6409 |
||
9 | 6410 |
if ( ! empty( $ptypes ) ) { |
6411 |
update_object_term_cache( $post_ids, $ptypes ); |
|
6412 |
} |
|
6413 |
} |
|
6414 |
||
6415 |
if ( $update_meta_cache ) { |
|
6416 |
update_postmeta_cache( $post_ids ); |
|
6417 |
} |
|
0 | 6418 |
} |
6419 |
||
6420 |
/** |
|
6421 |
* Updates metadata cache for list of post IDs. |
|
6422 |
* |
|
6423 |
* Performs SQL query to retrieve the metadata for the post IDs and updates the |
|
6424 |
* metadata cache for the posts. Therefore, the functions, which call this |
|
6425 |
* function, do not need to perform SQL queries on their own. |
|
6426 |
* |
|
6427 |
* @since 2.1.0 |
|
6428 |
* |
|
6429 |
* @param array $post_ids List of post IDs. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6430 |
* @return array|false Returns false if there is nothing to update or an array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6431 |
* of metadata. |
0 | 6432 |
*/ |
5 | 6433 |
function update_postmeta_cache( $post_ids ) { |
9 | 6434 |
return update_meta_cache( 'post', $post_ids ); |
0 | 6435 |
} |
6436 |
||
6437 |
/** |
|
6438 |
* Will clean the attachment in the cache. |
|
6439 |
* |
|
6440 |
* Cleaning means delete from the cache. Optionally will clean the term |
|
6441 |
* object cache associated with the attachment ID. |
|
6442 |
* |
|
5 | 6443 |
* This function will not run if $_wp_suspend_cache_invalidation is not empty. |
6444 |
* |
|
0 | 6445 |
* @since 3.0.0 |
6446 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6447 |
* @global bool $_wp_suspend_cache_invalidation |
5 | 6448 |
* |
6449 |
* @param int $id The attachment ID in the cache to clean. |
|
6450 |
* @param bool $clean_terms Optional. Whether to clean terms cache. Default false. |
|
0 | 6451 |
*/ |
5 | 6452 |
function clean_attachment_cache( $id, $clean_terms = false ) { |
0 | 6453 |
global $_wp_suspend_cache_invalidation; |
6454 |
||
9 | 6455 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
0 | 6456 |
return; |
9 | 6457 |
} |
0 | 6458 |
|
6459 |
$id = (int) $id; |
|
6460 |
||
9 | 6461 |
wp_cache_delete( $id, 'posts' ); |
6462 |
wp_cache_delete( $id, 'post_meta' ); |
|
6463 |
||
6464 |
if ( $clean_terms ) { |
|
6465 |
clean_object_term_cache( $id, 'attachment' ); |
|
6466 |
} |
|
0 | 6467 |
|
5 | 6468 |
/** |
6469 |
* Fires after the given attachment's cache is cleaned. |
|
6470 |
* |
|
6471 |
* @since 3.0.0 |
|
6472 |
* |
|
6473 |
* @param int $id Attachment ID. |
|
6474 |
*/ |
|
6475 |
do_action( 'clean_attachment_cache', $id ); |
|
0 | 6476 |
} |
6477 |
||
6478 |
// |
|
6479 |
// Hooks |
|
6480 |
// |
|
6481 |
||
6482 |
/** |
|
6483 |
* Hook for managing future post transitions to published. |
|
6484 |
* |
|
6485 |
* @since 2.3.0 |
|
6486 |
* @access private |
|
5 | 6487 |
* |
6488 |
* @see wp_clear_scheduled_hook() |
|
6489 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
6490 |
* |
|
6491 |
* @param string $new_status New post status. |
|
6492 |
* @param string $old_status Previous post status. |
|
6493 |
* @param WP_Post $post Post object. |
|
0 | 6494 |
*/ |
5 | 6495 |
function _transition_post_status( $new_status, $old_status, $post ) { |
0 | 6496 |
global $wpdb; |
6497 |
||
6498 |
if ( $old_status != 'publish' && $new_status == 'publish' ) { |
|
5 | 6499 |
// Reset GUID if transitioning to publish and it is empty. |
9 | 6500 |
if ( '' == get_the_guid( $post->ID ) ) { |
0 | 6501 |
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); |
9 | 6502 |
} |
5 | 6503 |
|
6504 |
/** |
|
6505 |
* Fires when a post's status is transitioned from private to published. |
|
6506 |
* |
|
6507 |
* @since 1.5.0 |
|
6508 |
* @deprecated 2.3.0 Use 'private_to_publish' instead. |
|
6509 |
* |
|
6510 |
* @param int $post_id Post ID. |
|
6511 |
*/ |
|
9 | 6512 |
do_action( 'private_to_published', $post->ID ); |
0 | 6513 |
} |
6514 |
||
5 | 6515 |
// If published posts changed clear the lastpostmodified cache. |
9 | 6516 |
if ( 'publish' == $new_status || 'publish' == $old_status ) { |
0 | 6517 |
foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { |
6518 |
wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); |
|
6519 |
wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6520 |
wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); |
0 | 6521 |
} |
6522 |
} |
|
6523 |
||
5 | 6524 |
if ( $new_status !== $old_status ) { |
6525 |
wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); |
|
6526 |
wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); |
|
6527 |
} |
|
6528 |
||
0 | 6529 |
// Always clears the hook in case the post status bounced from future to draft. |
9 | 6530 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
0 | 6531 |
} |
6532 |
||
6533 |
/** |
|
6534 |
* Hook used to schedule publication for a post marked for the future. |
|
6535 |
* |
|
6536 |
* The $post properties used and must exist are 'ID' and 'post_date_gmt'. |
|
6537 |
* |
|
6538 |
* @since 2.3.0 |
|
6539 |
* @access private |
|
6540 |
* |
|
5 | 6541 |
* @param int $deprecated Not used. Can be set to null. Never implemented. Not marked |
6542 |
* 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
|
6543 |
* wp_transition_post_status() and the default filter for _future_post_hook(). |
5 | 6544 |
* @param WP_Post $post Post object. |
0 | 6545 |
*/ |
6546 |
function _future_post_hook( $deprecated, $post ) { |
|
6547 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
|
9 | 6548 |
wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) ); |
0 | 6549 |
} |
6550 |
||
6551 |
/** |
|
6552 |
* Hook to schedule pings and enclosures when a post is published. |
|
6553 |
* |
|
5 | 6554 |
* Uses XMLRPC_REQUEST and WP_IMPORTING constants. |
6555 |
* |
|
0 | 6556 |
* @since 2.3.0 |
6557 |
* @access private |
|
5 | 6558 |
* |
6559 |
* @param int $post_id The ID in the database table of the post being published. |
|
0 | 6560 |
*/ |
5 | 6561 |
function _publish_post_hook( $post_id ) { |
6562 |
if ( defined( 'XMLRPC_REQUEST' ) ) { |
|
6563 |
/** |
|
6564 |
* Fires when _publish_post_hook() is called during an XML-RPC request. |
|
6565 |
* |
|
6566 |
* @since 2.1.0 |
|
6567 |
* |
|
6568 |
* @param int $post_id Post ID. |
|
6569 |
*/ |
|
6570 |
do_action( 'xmlrpc_publish_post', $post_id ); |
|
6571 |
} |
|
0 | 6572 |
|
9 | 6573 |
if ( defined( 'WP_IMPORTING' ) ) { |
0 | 6574 |
return; |
9 | 6575 |
} |
6576 |
||
6577 |
if ( get_option( 'default_pingback_flag' ) ) { |
|
0 | 6578 |
add_post_meta( $post_id, '_pingme', '1' ); |
9 | 6579 |
} |
0 | 6580 |
add_post_meta( $post_id, '_encloseme', '1' ); |
6581 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6582 |
if ( ! wp_next_scheduled( 'do_pings' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6583 |
wp_schedule_single_event( time(), 'do_pings' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6584 |
} |
0 | 6585 |
} |
6586 |
||
6587 |
/** |
|
9 | 6588 |
* Returns the ID of the post's parent. |
0 | 6589 |
* |
6590 |
* @since 3.1.0 |
|
6591 |
* |
|
9 | 6592 |
* @param int|WP_Post $post Post ID or post object. Defaults to global $post. |
6593 |
* @return int|false Post parent ID (which can be 0 if there is no parent), or false if the post does not exist. |
|
0 | 6594 |
*/ |
9 | 6595 |
function wp_get_post_parent_id( $post ) { |
6596 |
$post = get_post( $post ); |
|
6597 |
if ( ! $post || is_wp_error( $post ) ) { |
|
0 | 6598 |
return false; |
9 | 6599 |
} |
0 | 6600 |
return (int) $post->post_parent; |
6601 |
} |
|
6602 |
||
6603 |
/** |
|
5 | 6604 |
* Check the given subset of the post hierarchy for hierarchy loops. |
6605 |
* |
|
6606 |
* 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
|
6607 |
* to the {@see 'wp_insert_post_parent'} filter. |
0 | 6608 |
* |
6609 |
* @since 3.1.0 |
|
5 | 6610 |
* |
6611 |
* @see wp_find_hierarchy_loop() |
|
0 | 6612 |
* |
6613 |
* @param int $post_parent ID of the parent for the post we're checking. |
|
5 | 6614 |
* @param int $post_ID ID of the post we're checking. |
6615 |
* @return int The new post_parent for the post, 0 otherwise. |
|
0 | 6616 |
*/ |
6617 |
function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { |
|
5 | 6618 |
// Nothing fancy here - bail. |
9 | 6619 |
if ( ! $post_parent ) { |
0 | 6620 |
return 0; |
9 | 6621 |
} |
0 | 6622 |
|
5 | 6623 |
// New post can't cause a loop. |
9 | 6624 |
if ( empty( $post_ID ) ) { |
0 | 6625 |
return $post_parent; |
9 | 6626 |
} |
0 | 6627 |
|
5 | 6628 |
// Can't be its own parent. |
9 | 6629 |
if ( $post_parent == $post_ID ) { |
0 | 6630 |
return 0; |
9 | 6631 |
} |
0 | 6632 |
|
5 | 6633 |
// Now look for larger loops. |
9 | 6634 |
if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) ) { |
0 | 6635 |
return $post_parent; // No loop |
9 | 6636 |
} |
0 | 6637 |
|
5 | 6638 |
// Setting $post_parent to the given value causes a loop. |
9 | 6639 |
if ( isset( $loop[ $post_ID ] ) ) { |
0 | 6640 |
return 0; |
9 | 6641 |
} |
0 | 6642 |
|
6643 |
// There's a loop, but it doesn't contain $post_ID. Break the loop. |
|
9 | 6644 |
foreach ( array_keys( $loop ) as $loop_member ) { |
6645 |
wp_update_post( |
|
6646 |
array( |
|
6647 |
'ID' => $loop_member, |
|
6648 |
'post_parent' => 0, |
|
6649 |
) |
|
6650 |
); |
|
6651 |
} |
|
0 | 6652 |
|
6653 |
return $post_parent; |
|
6654 |
} |
|
6655 |
||
6656 |
/** |
|
9 | 6657 |
* Sets the post thumbnail (featured image) for the given post. |
0 | 6658 |
* |
6659 |
* @since 3.1.0 |
|
6660 |
* |
|
5 | 6661 |
* @param int|WP_Post $post Post ID or post object where thumbnail should be attached. |
6662 |
* @param int $thumbnail_id Thumbnail to attach. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6663 |
* @return int|bool True on success, false on failure. |
0 | 6664 |
*/ |
6665 |
function set_post_thumbnail( $post, $thumbnail_id ) { |
|
9 | 6666 |
$post = get_post( $post ); |
0 | 6667 |
$thumbnail_id = absint( $thumbnail_id ); |
6668 |
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { |
|
9 | 6669 |
if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) { |
0 | 6670 |
return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); |
9 | 6671 |
} else { |
0 | 6672 |
return delete_post_meta( $post->ID, '_thumbnail_id' ); |
9 | 6673 |
} |
0 | 6674 |
} |
6675 |
return false; |
|
6676 |
} |
|
6677 |
||
6678 |
/** |
|
9 | 6679 |
* Removes the thumbnail (featured image) from the given post. |
0 | 6680 |
* |
6681 |
* @since 3.3.0 |
|
6682 |
* |
|
9 | 6683 |
* @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed. |
0 | 6684 |
* @return bool True on success, false on failure. |
6685 |
*/ |
|
6686 |
function delete_post_thumbnail( $post ) { |
|
6687 |
$post = get_post( $post ); |
|
9 | 6688 |
if ( $post ) { |
0 | 6689 |
return delete_post_meta( $post->ID, '_thumbnail_id' ); |
9 | 6690 |
} |
0 | 6691 |
return false; |
6692 |
} |
|
6693 |
||
6694 |
/** |
|
5 | 6695 |
* Delete auto-drafts for new posts that are > 7 days old. |
0 | 6696 |
* |
6697 |
* @since 3.4.0 |
|
5 | 6698 |
* |
6699 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 6700 |
*/ |
6701 |
function wp_delete_auto_drafts() { |
|
6702 |
global $wpdb; |
|
6703 |
||
5 | 6704 |
// Cleanup old auto-drafts more than 7 days old. |
0 | 6705 |
$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 | 6706 |
foreach ( (array) $old_posts as $delete ) { |
6707 |
// Force delete. |
|
6708 |
wp_delete_post( $delete, true ); |
|
6709 |
} |
|
0 | 6710 |
} |
6711 |
||
6712 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6713 |
* Queues posts for lazy-loading of term meta. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6714 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6715 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6716 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6717 |
* @param array $posts Array of WP_Post objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6718 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6719 |
function wp_queue_posts_for_term_meta_lazyload( $posts ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6720 |
$post_type_taxonomies = $term_ids = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6721 |
foreach ( $posts as $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6722 |
if ( ! ( $post instanceof WP_Post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6723 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6724 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6725 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6726 |
if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6727 |
$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
|
6728 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6729 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6730 |
foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6731 |
// 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
|
6732 |
$terms = get_object_term_cache( $post->ID, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6733 |
if ( false !== $terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6734 |
foreach ( $terms as $term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6735 |
if ( ! isset( $term_ids[ $term->term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6736 |
$term_ids[] = $term->term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6737 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6738 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6739 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6740 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6741 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6742 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6743 |
if ( $term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6744 |
$lazyloader = wp_metadata_lazyloader(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6745 |
$lazyloader->queue_objects( 'term', $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6746 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6747 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6748 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6749 |
/** |
5 | 6750 |
* Update the custom taxonomies' term counts when a post's status is changed. |
6751 |
* |
|
6752 |
* For example, default posts term counts (for custom taxonomies) don't include |
|
6753 |
* private / draft posts. |
|
6754 |
* |
|
6755 |
* @since 3.3.0 |
|
0 | 6756 |
* @access private |
5 | 6757 |
* |
6758 |
* @param string $new_status New post status. |
|
6759 |
* @param string $old_status Old post status. |
|
6760 |
* @param WP_Post $post Post object. |
|
0 | 6761 |
*/ |
6762 |
function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { |
|
6763 |
// Update counts for the post's terms. |
|
6764 |
foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { |
|
6765 |
$tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); |
|
6766 |
wp_update_term_count( $tt_ids, $taxonomy ); |
|
6767 |
} |
|
6768 |
} |
|
6769 |
||
6770 |
/** |
|
6771 |
* Adds any posts from the given ids to the cache that do not already exist in cache |
|
6772 |
* |
|
6773 |
* @since 3.4.0 |
|
6774 |
* @access private |
|
6775 |
* |
|
5 | 6776 |
* @see update_post_caches() |
6777 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6778 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6779 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6780 |
* @param array $ids ID list. |
5 | 6781 |
* @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
6782 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|
0 | 6783 |
*/ |
6784 |
function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { |
|
6785 |
global $wpdb; |
|
6786 |
||
6787 |
$non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); |
|
9 | 6788 |
if ( ! empty( $non_cached_ids ) ) { |
6789 |
$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ',', $non_cached_ids ) ) ); |
|
0 | 6790 |
|
6791 |
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); |
|
6792 |
} |
|
6793 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6794 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6795 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6796 |
* 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
|
6797 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6798 |
* 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
|
6799 |
* if the post is untrashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6800 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6801 |
* For internal use. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6802 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6803 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6804 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6805 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6806 |
* @param string $post_name Slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6807 |
* @param string $post_ID Optional. Post ID that should be ignored. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6808 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6809 |
function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { |
9 | 6810 |
$trashed_posts_with_desired_slug = get_posts( |
6811 |
array( |
|
6812 |
'name' => $post_name, |
|
6813 |
'post_status' => 'trash', |
|
6814 |
'post_type' => 'any', |
|
6815 |
'nopaging' => true, |
|
6816 |
'post__not_in' => array( $post_ID ), |
|
6817 |
) |
|
6818 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6819 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6820 |
if ( ! empty( $trashed_posts_with_desired_slug ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6821 |
foreach ( $trashed_posts_with_desired_slug as $_post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6822 |
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
|
6823 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6824 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6825 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6826 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6827 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6828 |
* Adds a trashed suffix for a given post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6829 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6830 |
* 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
|
6831 |
* if the post is untrashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6832 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6833 |
* For internal use. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6834 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6835 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6836 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6837 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6838 |
* @param WP_Post $post The post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6839 |
* @return string New slug for the post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6840 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6841 |
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
|
6842 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6843 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6844 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6845 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6846 |
if ( '__trashed' === substr( $post->post_name, -9 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6847 |
return $post->post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6848 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6849 |
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
|
6850 |
$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
|
6851 |
$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
|
6852 |
clean_post_cache( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6853 |
return $post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6854 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6855 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6856 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6857 |
* Filter the SQL clauses of an attachment query to include filenames. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6858 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6859 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6860 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6861 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6862 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6863 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6864 |
* @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6865 |
* DISTINCT, fields (SELECT), and LIMITS clauses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6866 |
* @return array The modified clauses. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6867 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6868 |
function _filter_query_attachment_filenames( $clauses ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6869 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6870 |
remove_filter( 'posts_clauses', __FUNCTION__ ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6871 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6872 |
// 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
|
6873 |
$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
|
6874 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6875 |
$clauses['groupby'] = "{$wpdb->posts}.ID"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6876 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6877 |
$clauses['where'] = preg_replace( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6878 |
"/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
9 | 6879 |
'$0 OR ( sq1.meta_value $1 $2 )', |
6880 |
$clauses['where'] |
|
6881 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6882 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6883 |
return $clauses; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6884 |
} |
9 | 6885 |
|
6886 |
/** |
|
6887 |
* Sets the last changed time for the 'posts' cache group. |
|
6888 |
* |
|
6889 |
* @since 5.0.0 |
|
6890 |
*/ |
|
6891 |
function wp_cache_set_posts_last_changed() { |
|
6892 |
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
6893 |
} |
|
6894 |
||
6895 |
/** |
|
6896 |
* Get all available post MIME types for a given post type. |
|
6897 |
* |
|
6898 |
* @since 2.5.0 |
|
6899 |
* |
|
6900 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
6901 |
* |
|
6902 |
* @param string $type |
|
6903 |
* @return mixed |
|
6904 |
*/ |
|
6905 |
function get_available_post_mime_types( $type = 'attachment' ) { |
|
6906 |
global $wpdb; |
|
6907 |
||
6908 |
$types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) ); |
|
6909 |
return $types; |
|
6910 |
} |