author | ymh <ymh.work@gmail.com> |
Tue, 15 Dec 2020 13:49:49 +0100 | |
changeset 16 | a86126ab1dd4 |
parent 9 | 177826044cd9 |
child 18 | be944660c56a |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3 |
* Core Post API |
0 | 4 |
* |
5 |
* @package WordPress |
|
6 |
* @subpackage Post |
|
7 |
*/ |
|
8 |
||
9 |
// |
|
16 | 10 |
// Post Type registration. |
0 | 11 |
// |
12 |
||
13 |
/** |
|
14 |
* Creates the initial post types when 'init' action is fired. |
|
15 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
16 |
* See {@see 'init'}. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
17 |
* |
0 | 18 |
* @since 2.9.0 |
19 |
*/ |
|
20 |
function create_initial_post_types() { |
|
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, |
|
16 | 33 |
'menu_icon' => 'dashicons-admin-post', |
9 | 34 |
'hierarchical' => false, |
35 |
'rewrite' => false, |
|
36 |
'query_var' => false, |
|
37 |
'delete_with_user' => true, |
|
38 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), |
|
39 |
'show_in_rest' => true, |
|
40 |
'rest_base' => 'posts', |
|
41 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
42 |
) |
|
43 |
); |
|
44 |
||
45 |
register_post_type( |
|
46 |
'page', |
|
47 |
array( |
|
48 |
'labels' => array( |
|
49 |
'name_admin_bar' => _x( 'Page', 'add new from admin bar' ), |
|
50 |
), |
|
51 |
'public' => true, |
|
52 |
'publicly_queryable' => false, |
|
53 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
54 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
55 |
'capability_type' => 'page', |
|
56 |
'map_meta_cap' => true, |
|
57 |
'menu_position' => 20, |
|
16 | 58 |
'menu_icon' => 'dashicons-admin-page', |
9 | 59 |
'hierarchical' => true, |
60 |
'rewrite' => false, |
|
61 |
'query_var' => false, |
|
62 |
'delete_with_user' => true, |
|
63 |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), |
|
64 |
'show_in_rest' => true, |
|
65 |
'rest_base' => 'pages', |
|
66 |
'rest_controller_class' => 'WP_REST_Posts_Controller', |
|
67 |
) |
|
68 |
); |
|
69 |
||
70 |
register_post_type( |
|
71 |
'attachment', |
|
72 |
array( |
|
73 |
'labels' => array( |
|
74 |
'name' => _x( 'Media', 'post type general name' ), |
|
75 |
'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), |
|
76 |
'add_new' => _x( 'Add New', 'add new media' ), |
|
77 |
'edit_item' => __( 'Edit Media' ), |
|
78 |
'view_item' => __( 'View Attachment Page' ), |
|
79 |
'attributes' => __( 'Attachment Attributes' ), |
|
80 |
), |
|
81 |
'public' => true, |
|
82 |
'show_ui' => true, |
|
83 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
84 |
'_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|
85 |
'capability_type' => 'post', |
|
86 |
'capabilities' => array( |
|
87 |
'create_posts' => 'upload_files', |
|
88 |
), |
|
89 |
'map_meta_cap' => true, |
|
16 | 90 |
'menu_icon' => 'dashicons-admin-media', |
9 | 91 |
'hierarchical' => false, |
92 |
'rewrite' => false, |
|
93 |
'query_var' => false, |
|
94 |
'show_in_nav_menus' => false, |
|
95 |
'delete_with_user' => true, |
|
96 |
'supports' => array( 'title', 'author', 'comments' ), |
|
97 |
'show_in_rest' => true, |
|
98 |
'rest_base' => 'media', |
|
99 |
'rest_controller_class' => 'WP_REST_Attachments_Controller', |
|
100 |
) |
|
101 |
); |
|
5 | 102 |
add_post_type_support( 'attachment:audio', 'thumbnail' ); |
103 |
add_post_type_support( 'attachment:video', 'thumbnail' ); |
|
0 | 104 |
|
9 | 105 |
register_post_type( |
106 |
'revision', |
|
107 |
array( |
|
108 |
'labels' => array( |
|
109 |
'name' => __( 'Revisions' ), |
|
110 |
'singular_name' => __( 'Revision' ), |
|
111 |
), |
|
112 |
'public' => false, |
|
113 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
114 |
'_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ |
|
115 |
'capability_type' => 'post', |
|
116 |
'map_meta_cap' => true, |
|
117 |
'hierarchical' => false, |
|
118 |
'rewrite' => false, |
|
119 |
'query_var' => false, |
|
120 |
'can_export' => false, |
|
121 |
'delete_with_user' => true, |
|
122 |
'supports' => array( 'author' ), |
|
123 |
) |
|
124 |
); |
|
125 |
||
126 |
register_post_type( |
|
127 |
'nav_menu_item', |
|
128 |
array( |
|
129 |
'labels' => array( |
|
130 |
'name' => __( 'Navigation Menu Items' ), |
|
131 |
'singular_name' => __( 'Navigation Menu Item' ), |
|
132 |
), |
|
133 |
'public' => false, |
|
134 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
135 |
'hierarchical' => false, |
|
136 |
'rewrite' => false, |
|
137 |
'delete_with_user' => false, |
|
138 |
'query_var' => false, |
|
139 |
) |
|
140 |
); |
|
141 |
||
142 |
register_post_type( |
|
143 |
'custom_css', |
|
144 |
array( |
|
145 |
'labels' => array( |
|
146 |
'name' => __( 'Custom CSS' ), |
|
147 |
'singular_name' => __( 'Custom CSS' ), |
|
148 |
), |
|
149 |
'public' => false, |
|
150 |
'hierarchical' => false, |
|
151 |
'rewrite' => false, |
|
152 |
'query_var' => false, |
|
153 |
'delete_with_user' => false, |
|
154 |
'can_export' => true, |
|
155 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
156 |
'supports' => array( 'title', 'revisions' ), |
|
157 |
'capabilities' => array( |
|
158 |
'delete_posts' => 'edit_theme_options', |
|
159 |
'delete_post' => 'edit_theme_options', |
|
160 |
'delete_published_posts' => 'edit_theme_options', |
|
161 |
'delete_private_posts' => 'edit_theme_options', |
|
162 |
'delete_others_posts' => 'edit_theme_options', |
|
163 |
'edit_post' => 'edit_css', |
|
164 |
'edit_posts' => 'edit_css', |
|
165 |
'edit_others_posts' => 'edit_css', |
|
166 |
'edit_published_posts' => 'edit_css', |
|
167 |
'read_post' => 'read', |
|
168 |
'read_private_posts' => 'read', |
|
169 |
'publish_posts' => 'edit_theme_options', |
|
170 |
), |
|
171 |
) |
|
172 |
); |
|
173 |
||
174 |
register_post_type( |
|
175 |
'customize_changeset', |
|
176 |
array( |
|
177 |
'labels' => array( |
|
178 |
'name' => _x( 'Changesets', 'post type general name' ), |
|
179 |
'singular_name' => _x( 'Changeset', 'post type singular name' ), |
|
180 |
'menu_name' => _x( 'Changesets', 'admin menu' ), |
|
181 |
'name_admin_bar' => _x( 'Changeset', 'add new on admin bar' ), |
|
182 |
'add_new' => _x( 'Add New', 'Customize Changeset' ), |
|
183 |
'add_new_item' => __( 'Add New Changeset' ), |
|
184 |
'new_item' => __( 'New Changeset' ), |
|
185 |
'edit_item' => __( 'Edit Changeset' ), |
|
186 |
'view_item' => __( 'View Changeset' ), |
|
187 |
'all_items' => __( 'All Changesets' ), |
|
188 |
'search_items' => __( 'Search Changesets' ), |
|
189 |
'not_found' => __( 'No changesets found.' ), |
|
190 |
'not_found_in_trash' => __( 'No changesets found in Trash.' ), |
|
191 |
), |
|
192 |
'public' => false, |
|
193 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
194 |
'map_meta_cap' => true, |
|
195 |
'hierarchical' => false, |
|
196 |
'rewrite' => false, |
|
197 |
'query_var' => false, |
|
198 |
'can_export' => false, |
|
199 |
'delete_with_user' => false, |
|
200 |
'supports' => array( 'title', 'author' ), |
|
201 |
'capability_type' => 'customize_changeset', |
|
202 |
'capabilities' => array( |
|
203 |
'create_posts' => 'customize', |
|
204 |
'delete_others_posts' => 'customize', |
|
205 |
'delete_post' => 'customize', |
|
206 |
'delete_posts' => 'customize', |
|
207 |
'delete_private_posts' => 'customize', |
|
208 |
'delete_published_posts' => 'customize', |
|
209 |
'edit_others_posts' => 'customize', |
|
210 |
'edit_post' => 'customize', |
|
211 |
'edit_posts' => 'customize', |
|
212 |
'edit_private_posts' => 'customize', |
|
213 |
'edit_published_posts' => 'do_not_allow', |
|
214 |
'publish_posts' => 'customize', |
|
215 |
'read' => 'read', |
|
216 |
'read_post' => 'customize', |
|
217 |
'read_private_posts' => 'customize', |
|
218 |
), |
|
219 |
) |
|
220 |
); |
|
221 |
||
222 |
register_post_type( |
|
223 |
'oembed_cache', |
|
224 |
array( |
|
225 |
'labels' => array( |
|
226 |
'name' => __( 'oEmbed Responses' ), |
|
227 |
'singular_name' => __( 'oEmbed Response' ), |
|
228 |
), |
|
229 |
'public' => false, |
|
230 |
'hierarchical' => false, |
|
231 |
'rewrite' => false, |
|
232 |
'query_var' => false, |
|
233 |
'delete_with_user' => false, |
|
234 |
'can_export' => false, |
|
235 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
236 |
'supports' => array(), |
|
237 |
) |
|
238 |
); |
|
239 |
||
240 |
register_post_type( |
|
241 |
'user_request', |
|
242 |
array( |
|
243 |
'labels' => array( |
|
244 |
'name' => __( 'User Requests' ), |
|
245 |
'singular_name' => __( 'User Request' ), |
|
246 |
), |
|
247 |
'public' => false, |
|
248 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
249 |
'hierarchical' => false, |
|
250 |
'rewrite' => false, |
|
251 |
'query_var' => false, |
|
252 |
'can_export' => false, |
|
253 |
'delete_with_user' => false, |
|
254 |
'supports' => array(), |
|
255 |
) |
|
256 |
); |
|
257 |
||
258 |
register_post_type( |
|
259 |
'wp_block', |
|
260 |
array( |
|
261 |
'labels' => array( |
|
262 |
'name' => _x( 'Blocks', 'post type general name' ), |
|
263 |
'singular_name' => _x( 'Block', 'post type singular name' ), |
|
264 |
'menu_name' => _x( 'Blocks', 'admin menu' ), |
|
265 |
'name_admin_bar' => _x( 'Block', 'add new on admin bar' ), |
|
266 |
'add_new' => _x( 'Add New', 'Block' ), |
|
267 |
'add_new_item' => __( 'Add New Block' ), |
|
268 |
'new_item' => __( 'New Block' ), |
|
269 |
'edit_item' => __( 'Edit Block' ), |
|
270 |
'view_item' => __( 'View Block' ), |
|
271 |
'all_items' => __( 'All Blocks' ), |
|
272 |
'search_items' => __( 'Search Blocks' ), |
|
273 |
'not_found' => __( 'No blocks found.' ), |
|
274 |
'not_found_in_trash' => __( 'No blocks found in Trash.' ), |
|
275 |
'filter_items_list' => __( 'Filter blocks list' ), |
|
276 |
'items_list_navigation' => __( 'Blocks list navigation' ), |
|
277 |
'items_list' => __( 'Blocks list' ), |
|
278 |
'item_published' => __( 'Block published.' ), |
|
279 |
'item_published_privately' => __( 'Block published privately.' ), |
|
280 |
'item_reverted_to_draft' => __( 'Block reverted to draft.' ), |
|
281 |
'item_scheduled' => __( 'Block scheduled.' ), |
|
282 |
'item_updated' => __( 'Block updated.' ), |
|
283 |
), |
|
284 |
'public' => false, |
|
285 |
'_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|
286 |
'show_ui' => true, |
|
287 |
'show_in_menu' => false, |
|
288 |
'rewrite' => false, |
|
289 |
'show_in_rest' => true, |
|
290 |
'rest_base' => 'blocks', |
|
291 |
'rest_controller_class' => 'WP_REST_Blocks_Controller', |
|
292 |
'capability_type' => 'block', |
|
293 |
'capabilities' => array( |
|
294 |
// You need to be able to edit posts, in order to read blocks in their raw form. |
|
295 |
'read' => 'edit_posts', |
|
296 |
// You need to be able to publish posts, in order to create blocks. |
|
297 |
'create_posts' => 'publish_posts', |
|
298 |
'edit_posts' => 'edit_posts', |
|
299 |
'edit_published_posts' => 'edit_published_posts', |
|
300 |
'delete_published_posts' => 'delete_published_posts', |
|
301 |
'edit_others_posts' => 'edit_others_posts', |
|
302 |
'delete_others_posts' => 'delete_others_posts', |
|
303 |
), |
|
304 |
'map_meta_cap' => true, |
|
305 |
'supports' => array( |
|
306 |
'title', |
|
307 |
'editor', |
|
308 |
), |
|
309 |
) |
|
310 |
); |
|
311 |
||
312 |
register_post_status( |
|
313 |
'publish', |
|
314 |
array( |
|
315 |
'label' => _x( 'Published', 'post status' ), |
|
316 |
'public' => true, |
|
317 |
'_builtin' => true, /* internal use only. */ |
|
16 | 318 |
/* translators: %s: Number of published posts. */ |
319 |
'label_count' => _n_noop( |
|
320 |
'Published <span class="count">(%s)</span>', |
|
321 |
'Published <span class="count">(%s)</span>' |
|
322 |
), |
|
9 | 323 |
) |
324 |
); |
|
325 |
||
326 |
register_post_status( |
|
327 |
'future', |
|
328 |
array( |
|
329 |
'label' => _x( 'Scheduled', 'post status' ), |
|
330 |
'protected' => true, |
|
331 |
'_builtin' => true, /* internal use only. */ |
|
16 | 332 |
/* translators: %s: Number of scheduled posts. */ |
333 |
'label_count' => _n_noop( |
|
334 |
'Scheduled <span class="count">(%s)</span>', |
|
335 |
'Scheduled <span class="count">(%s)</span>' |
|
336 |
), |
|
9 | 337 |
) |
338 |
); |
|
339 |
||
340 |
register_post_status( |
|
341 |
'draft', |
|
342 |
array( |
|
16 | 343 |
'label' => _x( 'Draft', 'post status' ), |
344 |
'protected' => true, |
|
345 |
'_builtin' => true, /* internal use only. */ |
|
346 |
/* translators: %s: Number of draft posts. */ |
|
347 |
'label_count' => _n_noop( |
|
348 |
'Draft <span class="count">(%s)</span>', |
|
349 |
'Drafts <span class="count">(%s)</span>' |
|
350 |
), |
|
351 |
'date_floating' => true, |
|
9 | 352 |
) |
353 |
); |
|
354 |
||
355 |
register_post_status( |
|
356 |
'pending', |
|
357 |
array( |
|
16 | 358 |
'label' => _x( 'Pending', 'post status' ), |
359 |
'protected' => true, |
|
360 |
'_builtin' => true, /* internal use only. */ |
|
361 |
/* translators: %s: Number of pending posts. */ |
|
362 |
'label_count' => _n_noop( |
|
363 |
'Pending <span class="count">(%s)</span>', |
|
364 |
'Pending <span class="count">(%s)</span>' |
|
365 |
), |
|
366 |
'date_floating' => true, |
|
9 | 367 |
) |
368 |
); |
|
369 |
||
370 |
register_post_status( |
|
371 |
'private', |
|
372 |
array( |
|
373 |
'label' => _x( 'Private', 'post status' ), |
|
374 |
'private' => true, |
|
375 |
'_builtin' => true, /* internal use only. */ |
|
16 | 376 |
/* translators: %s: Number of private posts. */ |
377 |
'label_count' => _n_noop( |
|
378 |
'Private <span class="count">(%s)</span>', |
|
379 |
'Private <span class="count">(%s)</span>' |
|
380 |
), |
|
9 | 381 |
) |
382 |
); |
|
383 |
||
384 |
register_post_status( |
|
385 |
'trash', |
|
386 |
array( |
|
387 |
'label' => _x( 'Trash', 'post status' ), |
|
388 |
'internal' => true, |
|
389 |
'_builtin' => true, /* internal use only. */ |
|
16 | 390 |
/* translators: %s: Number of trashed posts. */ |
391 |
'label_count' => _n_noop( |
|
392 |
'Trash <span class="count">(%s)</span>', |
|
393 |
'Trash <span class="count">(%s)</span>' |
|
394 |
), |
|
9 | 395 |
'show_in_admin_status_list' => true, |
396 |
) |
|
397 |
); |
|
398 |
||
399 |
register_post_status( |
|
400 |
'auto-draft', |
|
401 |
array( |
|
16 | 402 |
'label' => 'auto-draft', |
403 |
'internal' => true, |
|
404 |
'_builtin' => true, /* internal use only. */ |
|
405 |
'date_floating' => true, |
|
9 | 406 |
) |
407 |
); |
|
408 |
||
409 |
register_post_status( |
|
410 |
'inherit', |
|
411 |
array( |
|
412 |
'label' => 'inherit', |
|
413 |
'internal' => true, |
|
414 |
'_builtin' => true, /* internal use only. */ |
|
415 |
'exclude_from_search' => false, |
|
416 |
) |
|
417 |
); |
|
418 |
||
419 |
register_post_status( |
|
420 |
'request-pending', |
|
421 |
array( |
|
422 |
'label' => _x( 'Pending', 'request status' ), |
|
423 |
'internal' => true, |
|
424 |
'_builtin' => true, /* internal use only. */ |
|
16 | 425 |
/* translators: %s: Number of pending requests. */ |
426 |
'label_count' => _n_noop( |
|
427 |
'Pending <span class="count">(%s)</span>', |
|
428 |
'Pending <span class="count">(%s)</span>' |
|
429 |
), |
|
9 | 430 |
'exclude_from_search' => false, |
431 |
) |
|
432 |
); |
|
433 |
||
434 |
register_post_status( |
|
435 |
'request-confirmed', |
|
436 |
array( |
|
437 |
'label' => _x( 'Confirmed', 'request status' ), |
|
438 |
'internal' => true, |
|
439 |
'_builtin' => true, /* internal use only. */ |
|
16 | 440 |
/* translators: %s: Number of confirmed requests. */ |
441 |
'label_count' => _n_noop( |
|
442 |
'Confirmed <span class="count">(%s)</span>', |
|
443 |
'Confirmed <span class="count">(%s)</span>' |
|
444 |
), |
|
9 | 445 |
'exclude_from_search' => false, |
446 |
) |
|
447 |
); |
|
448 |
||
449 |
register_post_status( |
|
450 |
'request-failed', |
|
451 |
array( |
|
452 |
'label' => _x( 'Failed', 'request status' ), |
|
453 |
'internal' => true, |
|
454 |
'_builtin' => true, /* internal use only. */ |
|
16 | 455 |
/* translators: %s: Number of failed requests. */ |
456 |
'label_count' => _n_noop( |
|
457 |
'Failed <span class="count">(%s)</span>', |
|
458 |
'Failed <span class="count">(%s)</span>' |
|
459 |
), |
|
9 | 460 |
'exclude_from_search' => false, |
461 |
) |
|
462 |
); |
|
463 |
||
464 |
register_post_status( |
|
465 |
'request-completed', |
|
466 |
array( |
|
467 |
'label' => _x( 'Completed', 'request status' ), |
|
468 |
'internal' => true, |
|
469 |
'_builtin' => true, /* internal use only. */ |
|
16 | 470 |
/* translators: %s: Number of completed requests. */ |
471 |
'label_count' => _n_noop( |
|
472 |
'Completed <span class="count">(%s)</span>', |
|
473 |
'Completed <span class="count">(%s)</span>' |
|
474 |
), |
|
9 | 475 |
'exclude_from_search' => false, |
476 |
) |
|
477 |
); |
|
0 | 478 |
} |
479 |
||
480 |
/** |
|
481 |
* Retrieve attached file path based on attachment ID. |
|
482 |
* |
|
483 |
* By default the path will go through the 'get_attached_file' filter, but |
|
484 |
* passing a true to the $unfiltered argument of get_attached_file() will |
|
485 |
* return the file path unfiltered. |
|
486 |
* |
|
487 |
* The function works by getting the single post meta name, named |
|
488 |
* '_wp_attached_file' and returning it. This is a convenience function to |
|
489 |
* prevent looking up the meta name and provide a mechanism for sending the |
|
490 |
* attached filename through a filter. |
|
491 |
* |
|
492 |
* @since 2.0.0 |
|
493 |
* |
|
5 | 494 |
* @param int $attachment_id Attachment ID. |
495 |
* @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
|
496 |
* @return string|false The file path to where the attached file should be, false otherwise. |
0 | 497 |
*/ |
498 |
function get_attached_file( $attachment_id, $unfiltered = false ) { |
|
499 |
$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
|
500 |
|
5 | 501 |
// If the file is relative, prepend upload dir. |
16 | 502 |
if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) { |
503 |
$uploads = wp_get_upload_dir(); |
|
504 |
if ( false === $uploads['error'] ) { |
|
505 |
$file = $uploads['basedir'] . "/$file"; |
|
506 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
507 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
508 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
509 |
if ( $unfiltered ) { |
0 | 510 |
return $file; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
511 |
} |
5 | 512 |
|
513 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
514 |
* Filters the attached file based on the given ID. |
5 | 515 |
* |
516 |
* @since 2.1.0 |
|
517 |
* |
|
16 | 518 |
* @param string|false $file The file path to where the attached file should be, false otherwise. |
519 |
* @param int $attachment_id Attachment ID. |
|
5 | 520 |
*/ |
0 | 521 |
return apply_filters( 'get_attached_file', $file, $attachment_id ); |
522 |
} |
|
523 |
||
524 |
/** |
|
525 |
* Update attachment file path based on attachment ID. |
|
526 |
* |
|
527 |
* Used to update the file path of the attachment, which uses post meta name |
|
528 |
* '_wp_attached_file' to store the path of the attachment. |
|
529 |
* |
|
530 |
* @since 2.1.0 |
|
5 | 531 |
* |
532 |
* @param int $attachment_id Attachment ID. |
|
533 |
* @param string $file File path for the attachment. |
|
0 | 534 |
* @return bool True on success, false on failure. |
535 |
*/ |
|
536 |
function update_attached_file( $attachment_id, $file ) { |
|
9 | 537 |
if ( ! get_post( $attachment_id ) ) { |
0 | 538 |
return false; |
9 | 539 |
} |
0 | 540 |
|
5 | 541 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
542 |
* Filters the path to the attached file to update. |
5 | 543 |
* |
544 |
* @since 2.1.0 |
|
545 |
* |
|
546 |
* @param string $file Path to the attached file to update. |
|
547 |
* @param int $attachment_id Attachment ID. |
|
548 |
*/ |
|
0 | 549 |
$file = apply_filters( 'update_attached_file', $file, $attachment_id ); |
5 | 550 |
|
16 | 551 |
$file = _wp_relative_upload_path( $file ); |
552 |
if ( $file ) { |
|
0 | 553 |
return update_post_meta( $attachment_id, '_wp_attached_file', $file ); |
9 | 554 |
} else { |
0 | 555 |
return delete_post_meta( $attachment_id, '_wp_attached_file' ); |
9 | 556 |
} |
0 | 557 |
} |
558 |
||
559 |
/** |
|
560 |
* Return relative path to an uploaded file. |
|
561 |
* |
|
562 |
* The path is relative to the current upload dir. |
|
563 |
* |
|
564 |
* @since 2.9.0 |
|
9 | 565 |
* @access private |
5 | 566 |
* |
567 |
* @param string $path Full path to the file. |
|
568 |
* @return string Relative path on success, unchanged path on failure. |
|
0 | 569 |
*/ |
570 |
function _wp_relative_upload_path( $path ) { |
|
571 |
$new_path = $path; |
|
572 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
573 |
$uploads = wp_get_upload_dir(); |
0 | 574 |
if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { |
575 |
$new_path = str_replace( $uploads['basedir'], '', $new_path ); |
|
576 |
$new_path = ltrim( $new_path, '/' ); |
|
577 |
} |
|
578 |
||
5 | 579 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
580 |
* Filters the relative path to an uploaded file. |
5 | 581 |
* |
582 |
* @since 2.9.0 |
|
583 |
* |
|
584 |
* @param string $new_path Relative path to the file. |
|
585 |
* @param string $path Full path to the file. |
|
586 |
*/ |
|
0 | 587 |
return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
588 |
} |
|
589 |
||
590 |
/** |
|
591 |
* Retrieve all children of the post parent ID. |
|
592 |
* |
|
593 |
* Normally, without any enhancements, the children would apply to pages. In the |
|
594 |
* context of the inner workings of WordPress, pages, posts, and attachments |
|
595 |
* share the same table, so therefore the functionality could apply to any one |
|
596 |
* of them. It is then noted that while this function does not work on posts, it |
|
597 |
* does not mean that it won't work on posts. It is recommended that you know |
|
598 |
* what context you wish to retrieve the children of. |
|
599 |
* |
|
600 |
* Attachments may also be made the child of a post, so if that is an accurate |
|
601 |
* statement (which needs to be verified), it would then be possible to get |
|
602 |
* 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
|
603 |
* version 2.5, so this is most likely inaccurate, but serves generally as an |
0 | 604 |
* example of what is possible. |
605 |
* |
|
606 |
* 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
|
607 |
* 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
|
608 |
* 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
|
609 |
* 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
|
610 |
* arguments listed in the get_posts() function. |
0 | 611 |
* |
612 |
* The 'post_parent' is the most important argument and important attention |
|
613 |
* needs to be paid to the $args parameter. If you pass either an object or an |
|
614 |
* integer (number), then just the 'post_parent' is grabbed and everything else |
|
615 |
* is lost. If you don't specify any arguments, then it is assumed that you are |
|
616 |
* in The Loop and the post parent will be grabbed for from the current post. |
|
617 |
* |
|
618 |
* The 'post_parent' argument is the ID to get the children. The 'numberposts' |
|
619 |
* is the amount of posts to retrieve that has a default of '-1', which is |
|
620 |
* used to get all of the posts. Giving a number higher than 0 will only |
|
621 |
* retrieve that amount of posts. |
|
622 |
* |
|
623 |
* The 'post_type' and 'post_status' arguments can be used to choose what |
|
624 |
* criteria of posts to retrieve. The 'post_type' can be anything, but WordPress |
|
625 |
* post types are 'post', 'pages', and 'attachments'. The 'post_status' |
|
626 |
* argument will accept any post status within the write administration panels. |
|
627 |
* |
|
628 |
* @since 2.0.0 |
|
629 |
* |
|
5 | 630 |
* @see get_posts() |
631 |
* @todo Check validity of description. |
|
632 |
* |
|
16 | 633 |
* @global WP_Post $post Global post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
634 |
* |
5 | 635 |
* @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. |
16 | 636 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
637 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
638 |
* respectively. Default OBJECT. |
|
639 |
* @return WP_Post[]|int[] Array of post objects or post IDs. |
|
0 | 640 |
*/ |
5 | 641 |
function get_children( $args = '', $output = OBJECT ) { |
0 | 642 |
$kids = array(); |
643 |
if ( empty( $args ) ) { |
|
644 |
if ( isset( $GLOBALS['post'] ) ) { |
|
9 | 645 |
$args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent ); |
0 | 646 |
} else { |
647 |
return $kids; |
|
648 |
} |
|
649 |
} elseif ( is_object( $args ) ) { |
|
9 | 650 |
$args = array( 'post_parent' => (int) $args->post_parent ); |
0 | 651 |
} elseif ( is_numeric( $args ) ) { |
9 | 652 |
$args = array( 'post_parent' => (int) $args ); |
0 | 653 |
} |
654 |
||
655 |
$defaults = array( |
|
9 | 656 |
'numberposts' => -1, |
657 |
'post_type' => 'any', |
|
658 |
'post_status' => 'any', |
|
659 |
'post_parent' => 0, |
|
0 | 660 |
); |
661 |
||
16 | 662 |
$parsed_args = wp_parse_args( $args, $defaults ); |
663 |
||
664 |
$children = get_posts( $parsed_args ); |
|
0 | 665 |
|
9 | 666 |
if ( ! $children ) { |
0 | 667 |
return $kids; |
9 | 668 |
} |
669 |
||
16 | 670 |
if ( ! empty( $parsed_args['fields'] ) ) { |
0 | 671 |
return $children; |
9 | 672 |
} |
673 |
||
674 |
update_post_cache( $children ); |
|
675 |
||
676 |
foreach ( $children as $key => $child ) { |
|
677 |
$kids[ $child->ID ] = $children[ $key ]; |
|
678 |
} |
|
0 | 679 |
|
16 | 680 |
if ( OBJECT == $output ) { |
0 | 681 |
return $kids; |
16 | 682 |
} elseif ( ARRAY_A == $output ) { |
5 | 683 |
$weeuns = array(); |
684 |
foreach ( (array) $kids as $kid ) { |
|
9 | 685 |
$weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] ); |
5 | 686 |
} |
0 | 687 |
return $weeuns; |
16 | 688 |
} elseif ( ARRAY_N == $output ) { |
5 | 689 |
$babes = array(); |
690 |
foreach ( (array) $kids as $kid ) { |
|
9 | 691 |
$babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) ); |
5 | 692 |
} |
0 | 693 |
return $babes; |
694 |
} else { |
|
695 |
return $kids; |
|
696 |
} |
|
697 |
} |
|
698 |
||
699 |
/** |
|
700 |
* Get extended entry info (<!--more-->). |
|
701 |
* |
|
702 |
* There should not be any space after the second dash and before the word |
|
703 |
* 'more'. There can be text or space(s) after the word 'more', but won't be |
|
704 |
* referenced. |
|
705 |
* |
|
706 |
* The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before |
|
5 | 707 |
* the `<!--more-->`. The 'extended' key has the content after the |
708 |
* `<!--more-->` comment. The 'more_text' key has the custom "Read More" text. |
|
0 | 709 |
* |
710 |
* @since 1.0.0 |
|
711 |
* |
|
712 |
* @param string $post Post content. |
|
16 | 713 |
* @return string[] { |
714 |
* Extended entry info. |
|
715 |
* |
|
716 |
* @type string $main Content before the more tag. |
|
717 |
* @type string $extended Content after the more tag. |
|
718 |
* @type string $more_text Custom read more text, or empty string. |
|
719 |
* } |
|
0 | 720 |
*/ |
5 | 721 |
function get_extended( $post ) { |
16 | 722 |
// Match the new style more links. |
9 | 723 |
if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) { |
724 |
list($main, $extended) = explode( $matches[0], $post, 2 ); |
|
725 |
$more_text = $matches[1]; |
|
0 | 726 |
} else { |
9 | 727 |
$main = $post; |
728 |
$extended = ''; |
|
0 | 729 |
$more_text = ''; |
730 |
} |
|
731 |
||
16 | 732 |
// Leading and trailing whitespace. |
9 | 733 |
$main = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main ); |
734 |
$extended = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended ); |
|
735 |
$more_text = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $more_text ); |
|
736 |
||
737 |
return array( |
|
738 |
'main' => $main, |
|
739 |
'extended' => $extended, |
|
740 |
'more_text' => $more_text, |
|
741 |
); |
|
0 | 742 |
} |
743 |
||
744 |
/** |
|
745 |
* Retrieves post data given a post ID or post object. |
|
746 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
747 |
* 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
|
748 |
* `$post`, must be given as a variable, since it is passed by reference. |
0 | 749 |
* |
750 |
* @since 1.5.1 |
|
5 | 751 |
* |
16 | 752 |
* @global WP_Post $post Global post object. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
753 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
754 |
* @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
16 | 755 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
756 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
757 |
* respectively. Default OBJECT. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
758 |
* @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
|
759 |
* or 'display'. Default 'raw'. |
5 | 760 |
* @return WP_Post|array|null Type corresponding to $output on success or null on failure. |
761 |
* When $output is OBJECT, a `WP_Post` instance is returned. |
|
0 | 762 |
*/ |
763 |
function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { |
|
9 | 764 |
if ( empty( $post ) && isset( $GLOBALS['post'] ) ) { |
0 | 765 |
$post = $GLOBALS['post']; |
9 | 766 |
} |
0 | 767 |
|
5 | 768 |
if ( $post instanceof WP_Post ) { |
0 | 769 |
$_post = $post; |
770 |
} elseif ( is_object( $post ) ) { |
|
771 |
if ( empty( $post->filter ) ) { |
|
772 |
$_post = sanitize_post( $post, 'raw' ); |
|
773 |
$_post = new WP_Post( $_post ); |
|
16 | 774 |
} elseif ( 'raw' === $post->filter ) { |
0 | 775 |
$_post = new WP_Post( $post ); |
776 |
} else { |
|
777 |
$_post = WP_Post::get_instance( $post->ID ); |
|
778 |
} |
|
779 |
} else { |
|
780 |
$_post = WP_Post::get_instance( $post ); |
|
781 |
} |
|
782 |
||
9 | 783 |
if ( ! $_post ) { |
0 | 784 |
return null; |
9 | 785 |
} |
0 | 786 |
|
787 |
$_post = $_post->filter( $filter ); |
|
788 |
||
16 | 789 |
if ( ARRAY_A == $output ) { |
0 | 790 |
return $_post->to_array(); |
16 | 791 |
} elseif ( ARRAY_N == $output ) { |
0 | 792 |
return array_values( $_post->to_array() ); |
9 | 793 |
} |
0 | 794 |
|
795 |
return $_post; |
|
796 |
} |
|
797 |
||
798 |
/** |
|
799 |
* Retrieve ancestors of a post. |
|
800 |
* |
|
801 |
* @since 2.5.0 |
|
802 |
* |
|
5 | 803 |
* @param int|WP_Post $post Post ID or post object. |
16 | 804 |
* @return int[] Ancestor IDs or empty array if none are found. |
0 | 805 |
*/ |
806 |
function get_post_ancestors( $post ) { |
|
807 |
$post = get_post( $post ); |
|
808 |
||
9 | 809 |
if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) { |
0 | 810 |
return array(); |
9 | 811 |
} |
0 | 812 |
|
813 |
$ancestors = array(); |
|
814 |
||
16 | 815 |
$id = $post->post_parent; |
816 |
$ancestors[] = $id; |
|
0 | 817 |
|
818 |
while ( $ancestor = get_post( $id ) ) { |
|
819 |
// Loop detection: If the ancestor has been seen before, break. |
|
16 | 820 |
if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors, true ) ) { |
0 | 821 |
break; |
9 | 822 |
} |
0 | 823 |
|
16 | 824 |
$id = $ancestor->post_parent; |
825 |
$ancestors[] = $id; |
|
0 | 826 |
} |
827 |
||
828 |
return $ancestors; |
|
829 |
} |
|
830 |
||
831 |
/** |
|
832 |
* Retrieve data from a post field based on Post ID. |
|
833 |
* |
|
834 |
* Examples of the post field will be, 'post_type', 'post_status', 'post_content', |
|
835 |
* etc and based off of the post object property or key names. |
|
836 |
* |
|
837 |
* The context values are based off of the taxonomy filter functions and |
|
838 |
* supported values are found within those functions. |
|
839 |
* |
|
840 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
841 |
* @since 4.5.0 The `$post` parameter was made optional. |
5 | 842 |
* |
843 |
* @see sanitize_post_field() |
|
844 |
* |
|
845 |
* @param string $field Post field name. |
|
9 | 846 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 847 |
* @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db', |
848 |
* or 'display'. Default 'display'. |
|
0 | 849 |
* @return string The value of the post field on success, empty string on failure. |
850 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
851 |
function get_post_field( $field, $post = null, $context = 'display' ) { |
0 | 852 |
$post = get_post( $post ); |
853 |
||
9 | 854 |
if ( ! $post ) { |
0 | 855 |
return ''; |
9 | 856 |
} |
857 |
||
858 |
if ( ! isset( $post->$field ) ) { |
|
0 | 859 |
return ''; |
9 | 860 |
} |
861 |
||
862 |
return sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|
0 | 863 |
} |
864 |
||
865 |
/** |
|
866 |
* Retrieve the mime type of an attachment based on the ID. |
|
867 |
* |
|
868 |
* This function can be used with any post type, but it makes more sense with |
|
869 |
* attachments. |
|
870 |
* |
|
871 |
* @since 2.0.0 |
|
872 |
* |
|
9 | 873 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
5 | 874 |
* @return string|false The mime type on success, false on failure. |
0 | 875 |
*/ |
9 | 876 |
function get_post_mime_type( $post = null ) { |
877 |
$post = get_post( $post ); |
|
878 |
||
879 |
if ( is_object( $post ) ) { |
|
0 | 880 |
return $post->post_mime_type; |
9 | 881 |
} |
0 | 882 |
|
883 |
return false; |
|
884 |
} |
|
885 |
||
886 |
/** |
|
9 | 887 |
* Retrieve the post status based on the post ID. |
0 | 888 |
* |
889 |
* If the post ID is of an attachment, then the parent post status will be given |
|
890 |
* instead. |
|
891 |
* |
|
892 |
* @since 2.0.0 |
|
893 |
* |
|
9 | 894 |
* @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.. |
5 | 895 |
* @return string|false Post status on success, false on failure. |
0 | 896 |
*/ |
9 | 897 |
function get_post_status( $post = null ) { |
898 |
$post = get_post( $post ); |
|
899 |
||
900 |
if ( ! is_object( $post ) ) { |
|
0 | 901 |
return false; |
9 | 902 |
} |
0 | 903 |
|
16 | 904 |
if ( 'attachment' === $post->post_type ) { |
905 |
if ( 'private' === $post->post_status ) { |
|
0 | 906 |
return 'private'; |
9 | 907 |
} |
0 | 908 |
|
5 | 909 |
// Unattached attachments are assumed to be published. |
16 | 910 |
if ( ( 'inherit' === $post->post_status ) && ( 0 == $post->post_parent ) ) { |
0 | 911 |
return 'publish'; |
9 | 912 |
} |
0 | 913 |
|
5 | 914 |
// Inherit status from the parent. |
915 |
if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) { |
|
916 |
$parent_post_status = get_post_status( $post->post_parent ); |
|
16 | 917 |
if ( 'trash' === $parent_post_status ) { |
5 | 918 |
return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); |
919 |
} else { |
|
920 |
return $parent_post_status; |
|
921 |
} |
|
922 |
} |
|
0 | 923 |
} |
924 |
||
7
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 |
* Filters the post status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
927 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
928 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
929 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
930 |
* @param string $post_status The post status. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
931 |
* @param WP_Post $post The post object. |
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 |
return apply_filters( 'get_post_status', $post->post_status, $post ); |
0 | 934 |
} |
935 |
||
936 |
/** |
|
937 |
* Retrieve all of the WordPress supported post statuses. |
|
938 |
* |
|
939 |
* Posts have a limited set of valid status values, this provides the |
|
940 |
* post_status values and descriptions. |
|
941 |
* |
|
942 |
* @since 2.5.0 |
|
943 |
* |
|
16 | 944 |
* @return string[] Array of post status labels keyed by their status. |
0 | 945 |
*/ |
946 |
function get_post_statuses() { |
|
947 |
$status = array( |
|
5 | 948 |
'draft' => __( 'Draft' ), |
949 |
'pending' => __( 'Pending Review' ), |
|
950 |
'private' => __( 'Private' ), |
|
9 | 951 |
'publish' => __( 'Published' ), |
0 | 952 |
); |
953 |
||
954 |
return $status; |
|
955 |
} |
|
956 |
||
957 |
/** |
|
958 |
* Retrieve all of the WordPress support page statuses. |
|
959 |
* |
|
960 |
* Pages have a limited set of valid status values, this provides the |
|
961 |
* post_status values and descriptions. |
|
962 |
* |
|
963 |
* @since 2.5.0 |
|
964 |
* |
|
16 | 965 |
* @return string[] Array of page status labels keyed by their status. |
0 | 966 |
*/ |
967 |
function get_page_statuses() { |
|
968 |
$status = array( |
|
5 | 969 |
'draft' => __( 'Draft' ), |
970 |
'private' => __( 'Private' ), |
|
9 | 971 |
'publish' => __( 'Published' ), |
0 | 972 |
); |
973 |
||
974 |
return $status; |
|
975 |
} |
|
976 |
||
977 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
978 |
* Return statuses for privacy requests. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
979 |
* |
9 | 980 |
* @since 4.9.6 |
981 |
* @access private |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
982 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
983 |
* @return array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
984 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
985 |
function _wp_privacy_statuses() { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
986 |
return array( |
16 | 987 |
'request-pending' => _x( 'Pending', 'request status' ), // Pending confirmation from user. |
988 |
'request-confirmed' => _x( 'Confirmed', 'request status' ), // User has confirmed the action. |
|
989 |
'request-failed' => _x( 'Failed', 'request status' ), // User failed to confirm the action. |
|
990 |
'request-completed' => _x( 'Completed', 'request status' ), // Admin has handled the request. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
991 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
992 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
993 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
994 |
/** |
0 | 995 |
* Register a post status. Do not use before init. |
996 |
* |
|
997 |
* A simple function for creating or modifying a post status based on the |
|
998 |
* parameters given. The function will accept an array (second optional |
|
999 |
* parameter), along with a string for the post status name. |
|
1000 |
* |
|
1001 |
* Arguments prefixed with an _underscore shouldn't be used by plugins and themes. |
|
1002 |
* |
|
1003 |
* @since 3.0.0 |
|
16 | 1004 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1005 |
* @global array $wp_post_statuses Inserts new post status object into the list |
0 | 1006 |
* |
16 | 1007 |
* @param string $post_status Name of the post status. |
5 | 1008 |
* @param array|string $args { |
1009 |
* Optional. Array or string of post status arguments. |
|
1010 |
* |
|
1011 |
* @type bool|string $label A descriptive name for the post status marked |
|
1012 |
* for translation. Defaults to value of $post_status. |
|
1013 |
* @type bool|array $label_count Descriptive text to use for nooped plurals. |
|
16 | 1014 |
* Default array of $label, twice. |
5 | 1015 |
* @type bool $exclude_from_search Whether to exclude posts with this post status |
1016 |
* from search results. Default is value of $internal. |
|
1017 |
* @type bool $_builtin Whether the status is built-in. Core-use only. |
|
1018 |
* Default false. |
|
1019 |
* @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
|
1020 |
* in the front end of the site. Default false. |
5 | 1021 |
* @type bool $internal Whether the status is for internal use only. |
1022 |
* Default false. |
|
1023 |
* @type bool $protected Whether posts with this status should be protected. |
|
1024 |
* Default false. |
|
1025 |
* @type bool $private Whether posts with this status should be private. |
|
1026 |
* Default false. |
|
1027 |
* @type bool $publicly_queryable Whether posts with this status should be publicly- |
|
1028 |
* queryable. Default is value of $public. |
|
1029 |
* @type bool $show_in_admin_all_list Whether to include posts in the edit listing for |
|
16 | 1030 |
* their post type. Default is the opposite value |
1031 |
* of $internal. |
|
5 | 1032 |
* @type bool $show_in_admin_status_list Show in the list of statuses with post counts at |
1033 |
* the top of the edit listings, |
|
1034 |
* e.g. All (12) | Published (9) | My Custom Status (2) |
|
16 | 1035 |
* Default is the opposite value of $internal. |
1036 |
* @type bool $date_floating Whether the post has a floating creation date. |
|
1037 |
* Default to false. |
|
5 | 1038 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1039 |
* @return object |
0 | 1040 |
*/ |
5 | 1041 |
function register_post_status( $post_status, $args = array() ) { |
0 | 1042 |
global $wp_post_statuses; |
1043 |
||
9 | 1044 |
if ( ! is_array( $wp_post_statuses ) ) { |
0 | 1045 |
$wp_post_statuses = array(); |
9 | 1046 |
} |
0 | 1047 |
|
1048 |
// Args prefixed with an underscore are reserved for internal use. |
|
1049 |
$defaults = array( |
|
9 | 1050 |
'label' => false, |
1051 |
'label_count' => false, |
|
1052 |
'exclude_from_search' => null, |
|
1053 |
'_builtin' => false, |
|
1054 |
'public' => null, |
|
1055 |
'internal' => null, |
|
1056 |
'protected' => null, |
|
1057 |
'private' => null, |
|
1058 |
'publicly_queryable' => null, |
|
0 | 1059 |
'show_in_admin_status_list' => null, |
9 | 1060 |
'show_in_admin_all_list' => null, |
16 | 1061 |
'date_floating' => null, |
0 | 1062 |
); |
9 | 1063 |
$args = wp_parse_args( $args, $defaults ); |
1064 |
$args = (object) $args; |
|
1065 |
||
1066 |
$post_status = sanitize_key( $post_status ); |
|
1067 |
$args->name = $post_status; |
|
0 | 1068 |
|
5 | 1069 |
// Set various defaults. |
9 | 1070 |
if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) { |
0 | 1071 |
$args->internal = true; |
9 | 1072 |
} |
1073 |
||
1074 |
if ( null === $args->public ) { |
|
0 | 1075 |
$args->public = false; |
9 | 1076 |
} |
1077 |
||
1078 |
if ( null === $args->private ) { |
|
0 | 1079 |
$args->private = false; |
9 | 1080 |
} |
1081 |
||
1082 |
if ( null === $args->protected ) { |
|
0 | 1083 |
$args->protected = false; |
9 | 1084 |
} |
1085 |
||
1086 |
if ( null === $args->internal ) { |
|
0 | 1087 |
$args->internal = false; |
9 | 1088 |
} |
1089 |
||
1090 |
if ( null === $args->publicly_queryable ) { |
|
0 | 1091 |
$args->publicly_queryable = $args->public; |
9 | 1092 |
} |
1093 |
||
1094 |
if ( null === $args->exclude_from_search ) { |
|
0 | 1095 |
$args->exclude_from_search = $args->internal; |
9 | 1096 |
} |
1097 |
||
1098 |
if ( null === $args->show_in_admin_all_list ) { |
|
1099 |
$args->show_in_admin_all_list = ! $args->internal; |
|
1100 |
} |
|
1101 |
||
1102 |
if ( null === $args->show_in_admin_status_list ) { |
|
1103 |
$args->show_in_admin_status_list = ! $args->internal; |
|
1104 |
} |
|
1105 |
||
16 | 1106 |
if ( null === $args->date_floating ) { |
1107 |
$args->date_floating = false; |
|
1108 |
} |
|
1109 |
||
9 | 1110 |
if ( false === $args->label ) { |
0 | 1111 |
$args->label = $post_status; |
9 | 1112 |
} |
1113 |
||
1114 |
if ( false === $args->label_count ) { |
|
1115 |
// 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
|
1116 |
$args->label_count = _n_noop( $args->label, $args->label ); |
9 | 1117 |
} |
1118 |
||
1119 |
$wp_post_statuses[ $post_status ] = $args; |
|
0 | 1120 |
|
1121 |
return $args; |
|
1122 |
} |
|
1123 |
||
1124 |
/** |
|
5 | 1125 |
* Retrieve a post status object by name. |
1126 |
* |
|
0 | 1127 |
* @since 3.0.0 |
5 | 1128 |
* |
1129 |
* @global array $wp_post_statuses List of post statuses. |
|
1130 |
* |
|
1131 |
* @see register_post_status() |
|
1132 |
* |
|
1133 |
* @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
|
1134 |
* @return object|null A post status object. |
0 | 1135 |
*/ |
1136 |
function get_post_status_object( $post_status ) { |
|
1137 |
global $wp_post_statuses; |
|
1138 |
||
9 | 1139 |
if ( empty( $wp_post_statuses[ $post_status ] ) ) { |
0 | 1140 |
return null; |
9 | 1141 |
} |
1142 |
||
1143 |
return $wp_post_statuses[ $post_status ]; |
|
0 | 1144 |
} |
1145 |
||
1146 |
/** |
|
5 | 1147 |
* Get a list of post statuses. |
1148 |
* |
|
0 | 1149 |
* @since 3.0.0 |
5 | 1150 |
* |
1151 |
* @global array $wp_post_statuses List of post statuses. |
|
1152 |
* |
|
1153 |
* @see register_post_status() |
|
1154 |
* |
|
1155 |
* @param array|string $args Optional. Array or string of post status arguments to compare against |
|
1156 |
* properties of the global `$wp_post_statuses objects`. Default empty array. |
|
1157 |
* @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'. |
|
1158 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one element |
|
1159 |
* from the array needs to match; 'and' means all elements must match. |
|
1160 |
* Default 'and'. |
|
1161 |
* @return array A list of post status names or objects. |
|
0 | 1162 |
*/ |
1163 |
function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { |
|
1164 |
global $wp_post_statuses; |
|
1165 |
||
16 | 1166 |
$field = ( 'names' === $output ) ? 'name' : false; |
9 | 1167 |
|
1168 |
return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field ); |
|
0 | 1169 |
} |
1170 |
||
1171 |
/** |
|
1172 |
* Whether the post type is hierarchical. |
|
1173 |
* |
|
1174 |
* A false return value might also mean that the post type does not exist. |
|
1175 |
* |
|
1176 |
* @since 3.0.0 |
|
5 | 1177 |
* |
1178 |
* @see get_post_type_object() |
|
0 | 1179 |
* |
1180 |
* @param string $post_type Post type name |
|
1181 |
* @return bool Whether post type is hierarchical. |
|
1182 |
*/ |
|
1183 |
function is_post_type_hierarchical( $post_type ) { |
|
9 | 1184 |
if ( ! post_type_exists( $post_type ) ) { |
0 | 1185 |
return false; |
9 | 1186 |
} |
0 | 1187 |
|
1188 |
$post_type = get_post_type_object( $post_type ); |
|
1189 |
return $post_type->hierarchical; |
|
1190 |
} |
|
1191 |
||
1192 |
/** |
|
9 | 1193 |
* Determines whether a post type is registered. |
1194 |
* |
|
1195 |
* For more information on this and similar theme functions, check out |
|
1196 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
1197 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 1198 |
* |
1199 |
* @since 3.0.0 |
|
5 | 1200 |
* |
1201 |
* @see get_post_type_object() |
|
1202 |
* |
|
1203 |
* @param string $post_type Post type name. |
|
0 | 1204 |
* @return bool Whether post type is registered. |
1205 |
*/ |
|
1206 |
function post_type_exists( $post_type ) { |
|
1207 |
return (bool) get_post_type_object( $post_type ); |
|
1208 |
} |
|
1209 |
||
1210 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1211 |
* Retrieves the post type of the current post or of a given post. |
0 | 1212 |
* |
1213 |
* @since 2.1.0 |
|
1214 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1215 |
* @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
|
1216 |
* @return string|false Post type on success, false on failure. |
0 | 1217 |
*/ |
1218 |
function get_post_type( $post = null ) { |
|
16 | 1219 |
$post = get_post( $post ); |
1220 |
if ( $post ) { |
|
0 | 1221 |
return $post->post_type; |
9 | 1222 |
} |
0 | 1223 |
|
1224 |
return false; |
|
1225 |
} |
|
1226 |
||
1227 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1228 |
* Retrieves a post type object by name. |
5 | 1229 |
* |
0 | 1230 |
* @since 3.0.0 |
9 | 1231 |
* @since 4.6.0 Object returned is now an instance of `WP_Post_Type`. |
5 | 1232 |
* |
1233 |
* @global array $wp_post_types List of post types. |
|
1234 |
* |
|
1235 |
* @see register_post_type() |
|
1236 |
* |
|
1237 |
* @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
|
1238 |
* @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. |
0 | 1239 |
*/ |
1240 |
function get_post_type_object( $post_type ) { |
|
1241 |
global $wp_post_types; |
|
1242 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1243 |
if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { |
0 | 1244 |
return null; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1245 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1246 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1247 |
return $wp_post_types[ $post_type ]; |
0 | 1248 |
} |
1249 |
||
1250 |
/** |
|
1251 |
* Get a list of all registered post type objects. |
|
1252 |
* |
|
1253 |
* @since 2.9.0 |
|
5 | 1254 |
* |
1255 |
* @global array $wp_post_types List of post types. |
|
1256 |
* |
|
1257 |
* @see register_post_type() for accepted arguments. |
|
1258 |
* |
|
1259 |
* @param array|string $args Optional. An array of key => value arguments to match against |
|
1260 |
* the post type objects. Default empty array. |
|
1261 |
* @param string $output Optional. The type of output to return. Accepts post type 'names' |
|
1262 |
* or 'objects'. Default 'names'. |
|
1263 |
* @param string $operator Optional. The logical operation to perform. 'or' means only one |
|
1264 |
* 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
|
1265 |
* must match; 'not' means no elements may match. Default 'and'. |
9 | 1266 |
* @return string[]|WP_Post_Type[] An array of post type names or objects. |
0 | 1267 |
*/ |
1268 |
function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { |
|
1269 |
global $wp_post_types; |
|
1270 |
||
16 | 1271 |
$field = ( 'names' === $output ) ? 'name' : false; |
9 | 1272 |
|
1273 |
return wp_filter_object_list( $wp_post_types, $args, $operator, $field ); |
|
0 | 1274 |
} |
1275 |
||
1276 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1277 |
* Registers a post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1278 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1279 |
* 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
|
1280 |
* {@see 'init'} action. Also, any taxonomy connections should be |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1281 |
* registered via the `$taxonomies` argument to ensure consistency |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1282 |
* 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
|
1283 |
* are used. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1284 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1285 |
* 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
|
1286 |
* as meta boxes, custom fields, post thumbnails, post statuses, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1287 |
* 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
|
1288 |
* list of supported features. |
0 | 1289 |
* |
1290 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1291 |
* @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
|
1292 |
* @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
|
1293 |
* screen and post editing screen. |
9 | 1294 |
* @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`. |
1295 |
* @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
|
1296 |
* arguments to register the post type in REST API. |
16 | 1297 |
* @since 5.3.0 The `supports` argument will now accept an array of arguments for a feature. |
1298 |
* . |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1299 |
* @global array $wp_post_types List of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1300 |
* |
16 | 1301 |
* @param string $post_type Post type key. Must not exceed 20 characters and may |
1302 |
* only contain lowercase alphanumeric characters, dashes, |
|
1303 |
* and underscores. See sanitize_key(). |
|
5 | 1304 |
* @param array|string $args { |
1305 |
* Array or string of arguments for registering a post type. |
|
1306 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1307 |
* @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
|
1308 |
* Default is value of $labels['name']. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1309 |
* @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
|
1310 |
* labels are inherited for non-hierarchical types and page |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1311 |
* 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
|
1312 |
* list of supported labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1313 |
* @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
|
1314 |
* Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1315 |
* @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
|
1316 |
* 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
|
1317 |
* settings of $exclude_from_search, $publicly_queryable, $show_ui, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1318 |
* 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
|
1319 |
* 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
|
1320 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1321 |
* @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
|
1322 |
* @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
|
1323 |
* results. Default is the opposite value of $public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1324 |
* @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
|
1325 |
* as part of parse_request(). Endpoints would include: |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1326 |
* * ?post_type={post_type_key} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1327 |
* * ?{post_type_key}={single_post_slug} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1328 |
* * ?{post_type_query_var}={single_post_slug} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1329 |
* If not set, the default is inherited from $public. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1330 |
* @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
|
1331 |
* admin. Default is value of $public. |
16 | 1332 |
* @type bool|string $show_in_menu Where to show the post type in the admin menu. To work, $show_ui |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1333 |
* 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
|
1334 |
* 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
|
1335 |
* 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
|
1336 |
* 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
|
1337 |
* Default is value of $show_ui. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1338 |
* @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. |
16 | 1339 |
* Default is value of $public. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1340 |
* @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
|
1341 |
* of $show_in_menu. |
16 | 1342 |
* @type bool $show_in_rest Whether to include the post type in the REST API. Set this to true |
1343 |
* for the post type to be available in the block editor. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1344 |
* @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
|
1345 |
* @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
|
1346 |
* @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
|
1347 |
* $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
|
1348 |
* @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
|
1349 |
* 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
|
1350 |
* -- 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
|
1351 |
* 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
|
1352 |
* '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
|
1353 |
* 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
|
1354 |
* @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
|
1355 |
* 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
|
1356 |
* 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
|
1357 |
* array('story', 'stories'). Default 'post'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1358 |
* @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
|
1359 |
* as a base to construct capabilities by default. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1360 |
* See get_post_type_capabilities(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1361 |
* @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
|
1362 |
* Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1363 |
* @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
|
1364 |
* add_post_type_support() directly. Core features include 'title', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1365 |
* 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1366 |
* 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1367 |
* Additionally, the 'revisions' feature dictates whether the post type |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1368 |
* will store revisions, and the 'comments' feature dictates whether the |
16 | 1369 |
* comments count will show on the edit screen. A feature can also be |
1370 |
* specified as an array of arguments to provide additional information |
|
1371 |
* about supporting that feature. |
|
1372 |
* Example: `array( 'my_feature', array( 'field' => 'value' ) )`. |
|
1373 |
* Default is an array containing 'title' and 'editor'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1374 |
* @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
|
1375 |
* 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
|
1376 |
* callback. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1377 |
* @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
|
1378 |
* 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
|
1379 |
* or register_taxonomy_for_object_type(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1380 |
* Default empty array. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1381 |
* @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
|
1382 |
* 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
|
1383 |
* $rewrite is enabled. Default false. |
5 | 1384 |
* @type bool|array $rewrite { |
1385 |
* Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
|
1386 |
* Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be |
|
1387 |
* passed with any of these keys: |
|
1388 |
* |
|
1389 |
* @type string $slug Customize the permastruct slug. Defaults to $post_type key. |
|
1390 |
* @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. |
|
1391 |
* Default true. |
|
1392 |
* @type bool $feeds Whether the feed permastruct should be built for this post type. |
|
1393 |
* Default is value of $has_archive. |
|
1394 |
* @type bool $pages Whether the permastruct should provide for pagination. Default true. |
|
1395 |
* @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, |
|
1396 |
* inherits from $permalink_epmask. If not specified and permalink_epmask |
|
1397 |
* is not set, defaults to EP_PERMALINK. |
|
1398 |
* } |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1399 |
* @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
|
1400 |
* 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
|
1401 |
* ?{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
|
1402 |
* ?{query_var_string}={post_slug} will be valid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1403 |
* @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
|
1404 |
* @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true, |
16 | 1405 |
* posts of this type belonging to the user will be moved to Trash |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1406 |
* 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
|
1407 |
* 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
|
1408 |
* 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
|
1409 |
* are not trashed or deleted. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1410 |
* @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
|
1411 |
* "built-in" post_type. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1412 |
* @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
|
1413 |
* this post type. Default 'post.php?post=%d'. |
5 | 1414 |
* } |
16 | 1415 |
* @return WP_Post_Type|WP_Error The registered post type object on success, |
1416 |
* WP_Error object on failure. |
|
0 | 1417 |
*/ |
1418 |
function register_post_type( $post_type, $args = array() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1419 |
global $wp_post_types; |
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 |
if ( ! is_array( $wp_post_types ) ) { |
0 | 1422 |
$wp_post_types = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1423 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1424 |
|
16 | 1425 |
// Sanitize post type name. |
0 | 1426 |
$post_type = sanitize_key( $post_type ); |
1427 |
||
5 | 1428 |
if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1429 |
_doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' ); |
5 | 1430 |
return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); |
1431 |
} |
|
0 | 1432 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1433 |
$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
|
1434 |
$post_type_object->add_supports(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1435 |
$post_type_object->add_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1436 |
$post_type_object->register_meta_boxes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1437 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1438 |
$wp_post_types[ $post_type ] = $post_type_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1439 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1440 |
$post_type_object->add_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1441 |
$post_type_object->register_taxonomies(); |
0 | 1442 |
|
5 | 1443 |
/** |
1444 |
* Fires after a post type is registered. |
|
1445 |
* |
|
1446 |
* @since 3.3.0 |
|
9 | 1447 |
* @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
5 | 1448 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1449 |
* @param string $post_type Post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1450 |
* @param WP_Post_Type $post_type_object Arguments used to register the post type. |
5 | 1451 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1452 |
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
|
1453 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1454 |
return $post_type_object; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1455 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1456 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1457 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1458 |
* Unregisters a post type. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1459 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1460 |
* 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
|
1461 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1462 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1463 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1464 |
* @global array $wp_post_types List of post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1465 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1466 |
* @param string $post_type Post type to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1467 |
* @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
|
1468 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1469 |
function unregister_post_type( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1470 |
global $wp_post_types; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1471 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1472 |
if ( ! post_type_exists( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1473 |
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
|
1474 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1475 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1476 |
$post_type_object = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1477 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1478 |
// Do not allow unregistering internal post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1479 |
if ( $post_type_object->_builtin ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1480 |
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
|
1481 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1482 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1483 |
$post_type_object->remove_supports(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1484 |
$post_type_object->remove_rewrite_rules(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1485 |
$post_type_object->unregister_meta_boxes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1486 |
$post_type_object->remove_hooks(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1487 |
$post_type_object->unregister_taxonomies(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1488 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1489 |
unset( $wp_post_types[ $post_type ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1490 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1491 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1492 |
* Fires after a post type was unregistered. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1493 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1494 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1495 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1496 |
* @param string $post_type Post type key. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1497 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1498 |
do_action( 'unregistered_post_type', $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1499 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1500 |
return true; |
0 | 1501 |
} |
1502 |
||
1503 |
/** |
|
5 | 1504 |
* Build an object with all post type capabilities out of a post type object |
0 | 1505 |
* |
1506 |
* Post type capabilities use the 'capability_type' argument as a base, if the |
|
1507 |
* capability is not set in the 'capabilities' argument array or if the |
|
1508 |
* 'capabilities' argument is not supplied. |
|
1509 |
* |
|
1510 |
* The capability_type argument can optionally be registered as an array, with |
|
1511 |
* the first value being singular and the second plural, e.g. array('story, 'stories') |
|
1512 |
* Otherwise, an 's' will be added to the value for the plural form. After |
|
1513 |
* registration, capability_type will always be a string of the singular value. |
|
1514 |
* |
|
16 | 1515 |
* By default, eight keys are accepted as part of the capabilities array: |
0 | 1516 |
* |
1517 |
* - edit_post, read_post, and delete_post are meta capabilities, which are then |
|
1518 |
* generally mapped to corresponding primitive capabilities depending on the |
|
1519 |
* context, which would be the post being edited/read/deleted and the user or |
|
1520 |
* role being checked. Thus these capabilities would generally not be granted |
|
1521 |
* directly to users or roles. |
|
1522 |
* |
|
1523 |
* - edit_posts - Controls whether objects of this post type can be edited. |
|
1524 |
* - edit_others_posts - Controls whether objects of this type owned by other users |
|
1525 |
* can be edited. If the post type does not support an author, then this will |
|
1526 |
* behave like edit_posts. |
|
16 | 1527 |
* - delete_posts - Controls whether objects of this post type can be deleted. |
0 | 1528 |
* - publish_posts - Controls publishing objects of this post type. |
1529 |
* - read_private_posts - Controls whether private objects can be read. |
|
1530 |
* |
|
16 | 1531 |
* These five primitive capabilities are checked in core in various locations. |
1532 |
* There are also six other primitive capabilities which are not referenced |
|
0 | 1533 |
* directly in core, except in map_meta_cap(), which takes the three aforementioned |
1534 |
* meta capabilities and translates them into one or more primitive capabilities |
|
1535 |
* that must then be checked against the user or role, depending on the context. |
|
1536 |
* |
|
1537 |
* - read - Controls whether objects of this post type can be read. |
|
1538 |
* - delete_private_posts - Controls whether private objects can be deleted. |
|
1539 |
* - delete_published_posts - Controls whether published objects can be deleted. |
|
1540 |
* - delete_others_posts - Controls whether objects owned by other users can be |
|
1541 |
* can be deleted. If the post type does not support an author, then this will |
|
1542 |
* behave like delete_posts. |
|
1543 |
* - edit_private_posts - Controls whether private objects can be edited. |
|
1544 |
* - edit_published_posts - Controls whether published objects can be edited. |
|
1545 |
* |
|
1546 |
* These additional capabilities are only used in map_meta_cap(). Thus, they are |
|
1547 |
* only assigned by default if the post type is registered with the 'map_meta_cap' |
|
1548 |
* argument set to true (default is false). |
|
1549 |
* |
|
5 | 1550 |
* @since 3.0.0 |
16 | 1551 |
* @since 5.4.0 'delete_posts' is included in default capabilities. |
5 | 1552 |
* |
1553 |
* @see register_post_type() |
|
0 | 1554 |
* @see map_meta_cap() |
5 | 1555 |
* |
1556 |
* @param object $args Post type registration arguments. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1557 |
* @return object Object with all the capabilities as member variables. |
0 | 1558 |
*/ |
1559 |
function get_post_type_capabilities( $args ) { |
|
9 | 1560 |
if ( ! is_array( $args->capability_type ) ) { |
0 | 1561 |
$args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); |
9 | 1562 |
} |
0 | 1563 |
|
1564 |
// Singular base for meta capabilities, plural base for primitive capabilities. |
|
1565 |
list( $singular_base, $plural_base ) = $args->capability_type; |
|
1566 |
||
1567 |
$default_capabilities = array( |
|
16 | 1568 |
// Meta capabilities. |
9 | 1569 |
'edit_post' => 'edit_' . $singular_base, |
1570 |
'read_post' => 'read_' . $singular_base, |
|
1571 |
'delete_post' => 'delete_' . $singular_base, |
|
0 | 1572 |
// Primitive capabilities used outside of map_meta_cap(): |
9 | 1573 |
'edit_posts' => 'edit_' . $plural_base, |
1574 |
'edit_others_posts' => 'edit_others_' . $plural_base, |
|
16 | 1575 |
'delete_posts' => 'delete_' . $plural_base, |
9 | 1576 |
'publish_posts' => 'publish_' . $plural_base, |
0 | 1577 |
'read_private_posts' => 'read_private_' . $plural_base, |
1578 |
); |
|
1579 |
||
1580 |
// Primitive capabilities used within map_meta_cap(): |
|
1581 |
if ( $args->map_meta_cap ) { |
|
1582 |
$default_capabilities_for_mapping = array( |
|
1583 |
'read' => 'read', |
|
9 | 1584 |
'delete_private_posts' => 'delete_private_' . $plural_base, |
0 | 1585 |
'delete_published_posts' => 'delete_published_' . $plural_base, |
9 | 1586 |
'delete_others_posts' => 'delete_others_' . $plural_base, |
1587 |
'edit_private_posts' => 'edit_private_' . $plural_base, |
|
1588 |
'edit_published_posts' => 'edit_published_' . $plural_base, |
|
0 | 1589 |
); |
9 | 1590 |
$default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); |
0 | 1591 |
} |
1592 |
||
1593 |
$capabilities = array_merge( $default_capabilities, $args->capabilities ); |
|
1594 |
||
1595 |
// Post creation capability simply maps to edit_posts by default: |
|
9 | 1596 |
if ( ! isset( $capabilities['create_posts'] ) ) { |
0 | 1597 |
$capabilities['create_posts'] = $capabilities['edit_posts']; |
9 | 1598 |
} |
0 | 1599 |
|
1600 |
// Remember meta capabilities for future reference. |
|
9 | 1601 |
if ( $args->map_meta_cap ) { |
0 | 1602 |
_post_type_meta_capabilities( $capabilities ); |
9 | 1603 |
} |
0 | 1604 |
|
1605 |
return (object) $capabilities; |
|
1606 |
} |
|
1607 |
||
1608 |
/** |
|
5 | 1609 |
* Store or return a list of post type meta caps for map_meta_cap(). |
0 | 1610 |
* |
1611 |
* @since 3.1.0 |
|
1612 |
* @access private |
|
5 | 1613 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1614 |
* @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
|
1615 |
* |
16 | 1616 |
* @param string[] $capabilities Post type meta capabilities. |
0 | 1617 |
*/ |
1618 |
function _post_type_meta_capabilities( $capabilities = null ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1619 |
global $post_type_meta_caps; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1620 |
|
0 | 1621 |
foreach ( $capabilities as $core => $custom ) { |
16 | 1622 |
if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ), true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1623 |
$post_type_meta_caps[ $custom ] = $core; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1624 |
} |
0 | 1625 |
} |
1626 |
} |
|
1627 |
||
1628 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1629 |
* Builds an object with all post type labels out of a post type object. |
0 | 1630 |
* |
1631 |
* Accepted keys of the label array in the post type object: |
|
5 | 1632 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1633 |
* - `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
|
1634 |
* by `$post_type_object->label`. Default is 'Posts' / 'Pages'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1635 |
* - `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
|
1636 |
* - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. |
16 | 1637 |
* When internationalizing this string, please use a {@link https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#disambiguation-by-context gettext context} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1638 |
* 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
|
1639 |
* - `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
|
1640 |
* - `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
|
1641 |
* - `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
|
1642 |
* - `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
|
1643 |
* - `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
|
1644 |
* - `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
|
1645 |
* - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. |
16 | 1646 |
* - `not_found_in_trash` - Label used when no items are in the Trash. Default is 'No posts found in Trash' / |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1647 |
* 'No pages found in Trash'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1648 |
* - `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
|
1649 |
* post types. Default is 'Parent Page:'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1650 |
* - `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
|
1651 |
* - `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
|
1652 |
* - `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
|
1653 |
* - `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
|
1654 |
* - `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
|
1655 |
* 'Uploaded to this page'. |
16 | 1656 |
* - `featured_image` - Label for the featured image meta box title. Default is 'Featured image'. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1657 |
* - `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
|
1658 |
* - `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
|
1659 |
* - `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
|
1660 |
* - `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
|
1661 |
* - `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
|
1662 |
* 'Filter pages list'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1663 |
* - `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
|
1664 |
* 'Pages list navigation'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1665 |
* - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. |
9 | 1666 |
* - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' |
1667 |
* - `item_published_privately` - Label used when an item is published with private visibility. |
|
1668 |
* Default is 'Post published privately.' / 'Page published privately.' |
|
1669 |
* - `item_reverted_to_draft` - Label used when an item is switched to a draft. |
|
1670 |
* Default is 'Post reverted to draft.' / 'Page reverted to draft.' |
|
1671 |
* - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / |
|
1672 |
* 'Page scheduled.' |
|
1673 |
* - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' |
|
5 | 1674 |
* |
1675 |
* Above, the first default value is for non-hierarchical post types (like posts) |
|
1676 |
* and the second one is for hierarchical post types (like pages). |
|
0 | 1677 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1678 |
* 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
|
1679 |
* |
0 | 1680 |
* @since 3.0.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1681 |
* @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
|
1682 |
* and `use_featured_image` labels. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1683 |
* @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
|
1684 |
* `items_list_navigation`, and `items_list` labels. |
9 | 1685 |
* @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
|
1686 |
* @since 4.7.0 Added the `view_items` and `attributes` labels. |
9 | 1687 |
* @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, |
1688 |
* `item_scheduled`, and `item_updated` labels. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1689 |
* |
0 | 1690 |
* @access private |
1691 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1692 |
* @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
|
1693 |
* @return object Object with all the labels as member variables. |
0 | 1694 |
*/ |
1695 |
function get_post_type_labels( $post_type_object ) { |
|
9 | 1696 |
$nohier_vs_hier_defaults = array( |
1697 |
'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ), |
|
1698 |
'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ), |
|
1699 |
'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ), |
|
1700 |
'add_new_item' => array( __( 'Add New Post' ), __( 'Add New Page' ) ), |
|
1701 |
'edit_item' => array( __( 'Edit Post' ), __( 'Edit Page' ) ), |
|
1702 |
'new_item' => array( __( 'New Post' ), __( 'New Page' ) ), |
|
1703 |
'view_item' => array( __( 'View Post' ), __( 'View Page' ) ), |
|
1704 |
'view_items' => array( __( 'View Posts' ), __( 'View Pages' ) ), |
|
1705 |
'search_items' => array( __( 'Search Posts' ), __( 'Search Pages' ) ), |
|
1706 |
'not_found' => array( __( 'No posts found.' ), __( 'No pages found.' ) ), |
|
1707 |
'not_found_in_trash' => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ), |
|
1708 |
'parent_item_colon' => array( null, __( 'Parent Page:' ) ), |
|
1709 |
'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ), |
|
1710 |
'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ), |
|
1711 |
'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ), |
|
1712 |
'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ), |
|
1713 |
'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ), |
|
16 | 1714 |
'featured_image' => array( _x( 'Featured image', 'post' ), _x( 'Featured image', 'page' ) ), |
9 | 1715 |
'set_featured_image' => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ), |
1716 |
'remove_featured_image' => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ), |
|
1717 |
'use_featured_image' => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ), |
|
1718 |
'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ), |
|
1719 |
'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ), |
|
1720 |
'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ), |
|
1721 |
'item_published' => array( __( 'Post published.' ), __( 'Page published.' ) ), |
|
1722 |
'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ), |
|
1723 |
'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ), |
|
1724 |
'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ), |
|
1725 |
'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ), |
|
0 | 1726 |
); |
1727 |
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
|
1728 |
||
1729 |
$labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
|
1730 |
||
1731 |
$post_type = $post_type_object->name; |
|
5 | 1732 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1733 |
$default_labels = clone $labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1734 |
|
5 | 1735 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1736 |
* Filters the labels of a specific post type. |
5 | 1737 |
* |
1738 |
* The dynamic portion of the hook name, `$post_type`, refers to |
|
1739 |
* the post type slug. |
|
1740 |
* |
|
1741 |
* @since 3.5.0 |
|
1742 |
* |
|
1743 |
* @see get_post_type_labels() for the full list of labels. |
|
1744 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1745 |
* @param object $labels Object with labels for the post type as member variables. |
5 | 1746 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1747 |
$labels = apply_filters( "post_type_labels_{$post_type}", $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1748 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1749 |
// 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
|
1750 |
$labels = (object) array_merge( (array) $default_labels, (array) $labels ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1751 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1752 |
return $labels; |
0 | 1753 |
} |
1754 |
||
1755 |
/** |
|
5 | 1756 |
* Build an object with custom-something object (post type, taxonomy) labels |
1757 |
* out of a custom-something object |
|
1758 |
* |
|
1759 |
* @since 3.0.0 |
|
0 | 1760 |
* @access private |
5 | 1761 |
* |
1762 |
* @param object $object A custom-something object. |
|
1763 |
* @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
|
1764 |
* @return object Object containing labels for the given custom-something object. |
0 | 1765 |
*/ |
1766 |
function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { |
|
1767 |
$object->labels = (array) $object->labels; |
|
1768 |
||
9 | 1769 |
if ( isset( $object->label ) && empty( $object->labels['name'] ) ) { |
0 | 1770 |
$object->labels['name'] = $object->label; |
9 | 1771 |
} |
1772 |
||
1773 |
if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) { |
|
0 | 1774 |
$object->labels['singular_name'] = $object->labels['name']; |
9 | 1775 |
} |
1776 |
||
1777 |
if ( ! isset( $object->labels['name_admin_bar'] ) ) { |
|
0 | 1778 |
$object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; |
9 | 1779 |
} |
1780 |
||
1781 |
if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) { |
|
0 | 1782 |
$object->labels['menu_name'] = $object->labels['name']; |
9 | 1783 |
} |
1784 |
||
1785 |
if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) { |
|
0 | 1786 |
$object->labels['all_items'] = $object->labels['menu_name']; |
9 | 1787 |
} |
1788 |
||
1789 |
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
|
1790 |
$object->labels['archives'] = $object->labels['all_items']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1791 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1792 |
|
5 | 1793 |
$defaults = array(); |
1794 |
foreach ( $nohier_vs_hier_defaults as $key => $value ) { |
|
9 | 1795 |
$defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0]; |
1796 |
} |
|
1797 |
$labels = array_merge( $defaults, $object->labels ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1798 |
$object->labels = (object) $object->labels; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1799 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1800 |
return (object) $labels; |
0 | 1801 |
} |
1802 |
||
1803 |
/** |
|
5 | 1804 |
* Add submenus for post types. |
0 | 1805 |
* |
1806 |
* @access private |
|
1807 |
* @since 3.1.0 |
|
1808 |
*/ |
|
1809 |
function _add_post_type_submenus() { |
|
1810 |
foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { |
|
1811 |
$ptype_obj = get_post_type_object( $ptype ); |
|
5 | 1812 |
// Sub-menus only. |
16 | 1813 |
if ( ! $ptype_obj->show_in_menu || true === $ptype_obj->show_in_menu ) { |
0 | 1814 |
continue; |
9 | 1815 |
} |
0 | 1816 |
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" ); |
1817 |
} |
|
1818 |
} |
|
1819 |
||
1820 |
/** |
|
16 | 1821 |
* Registers support of certain features for a post type. |
0 | 1822 |
* |
5 | 1823 |
* All core features are directly associated with a functional area of the edit |
1824 |
* screen, such as the editor or a meta box. Features include: 'title', 'editor', |
|
1825 |
* 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', |
|
1826 |
* 'thumbnail', 'custom-fields', and 'post-formats'. |
|
1827 |
* |
|
1828 |
* Additionally, the 'revisions' feature dictates whether the post type will |
|
1829 |
* store revisions, and the 'comments' feature dictates whether the comments |
|
1830 |
* count will show on the edit screen. |
|
0 | 1831 |
* |
16 | 1832 |
* A third, optional parameter can also be passed along with a feature to provide |
1833 |
* additional information about supporting that feature. |
|
1834 |
* |
|
1835 |
* Example usage: |
|
1836 |
* |
|
1837 |
* add_post_type_support( 'my_post_type', 'comments' ); |
|
1838 |
* add_post_type_support( 'my_post_type', array( |
|
1839 |
* 'author', 'excerpt', |
|
1840 |
* ) ); |
|
1841 |
* add_post_type_support( 'my_post_type', 'my_feature', array( |
|
1842 |
* 'field' => 'value', |
|
1843 |
* ) ); |
|
1844 |
* |
|
0 | 1845 |
* @since 3.0.0 |
16 | 1846 |
* @since 5.3.0 Formalized the existing and already documented `...$args` parameter |
1847 |
* by adding it to the function signature. |
|
5 | 1848 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1849 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1850 |
* |
5 | 1851 |
* @param string $post_type The post type for which to add the feature. |
1852 |
* @param string|array $feature The feature being added, accepts an array of |
|
1853 |
* feature strings or a single string. |
|
16 | 1854 |
* @param mixed ...$args Optional extra arguments to pass along with certain features. |
1855 |
*/ |
|
1856 |
function add_post_type_support( $post_type, $feature, ...$args ) { |
|
0 | 1857 |
global $_wp_post_type_features; |
1858 |
||
1859 |
$features = (array) $feature; |
|
9 | 1860 |
foreach ( $features as $feature ) { |
16 | 1861 |
if ( $args ) { |
1862 |
$_wp_post_type_features[ $post_type ][ $feature ] = $args; |
|
1863 |
} else { |
|
9 | 1864 |
$_wp_post_type_features[ $post_type ][ $feature ] = true; |
1865 |
} |
|
0 | 1866 |
} |
1867 |
} |
|
1868 |
||
1869 |
/** |
|
1870 |
* Remove support for a feature from a post type. |
|
1871 |
* |
|
1872 |
* @since 3.0.0 |
|
5 | 1873 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1874 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1875 |
* |
5 | 1876 |
* @param string $post_type The post type for which to remove the feature. |
1877 |
* @param string $feature The feature being removed. |
|
0 | 1878 |
*/ |
1879 |
function remove_post_type_support( $post_type, $feature ) { |
|
1880 |
global $_wp_post_type_features; |
|
1881 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1882 |
unset( $_wp_post_type_features[ $post_type ][ $feature ] ); |
0 | 1883 |
} |
1884 |
||
1885 |
/** |
|
1886 |
* Get all the post type features |
|
1887 |
* |
|
1888 |
* @since 3.4.0 |
|
5 | 1889 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1890 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1891 |
* |
5 | 1892 |
* @param string $post_type The post type. |
1893 |
* @return array Post type supports list. |
|
0 | 1894 |
*/ |
1895 |
function get_all_post_type_supports( $post_type ) { |
|
1896 |
global $_wp_post_type_features; |
|
1897 |
||
9 | 1898 |
if ( isset( $_wp_post_type_features[ $post_type ] ) ) { |
1899 |
return $_wp_post_type_features[ $post_type ]; |
|
1900 |
} |
|
0 | 1901 |
|
1902 |
return array(); |
|
1903 |
} |
|
1904 |
||
1905 |
/** |
|
5 | 1906 |
* Check a post type's support for a given feature. |
0 | 1907 |
* |
1908 |
* @since 3.0.0 |
|
5 | 1909 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1910 |
* @global array $_wp_post_type_features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1911 |
* |
5 | 1912 |
* @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
|
1913 |
* @param string $feature The feature being checked. |
5 | 1914 |
* @return bool Whether the post type supports the given feature. |
0 | 1915 |
*/ |
1916 |
function post_type_supports( $post_type, $feature ) { |
|
1917 |
global $_wp_post_type_features; |
|
1918 |
||
9 | 1919 |
return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) ); |
0 | 1920 |
} |
1921 |
||
1922 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1923 |
* 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
|
1924 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1925 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1926 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1927 |
* @global array $_wp_post_type_features Post type features |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1928 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1929 |
* @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
|
1930 |
* @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
|
1931 |
* 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
|
1932 |
* 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
|
1933 |
* match. Default 'and'. |
16 | 1934 |
* @return string[] A list of post type names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1935 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1936 |
function get_post_types_by_support( $feature, $operator = 'and' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1937 |
global $_wp_post_type_features; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1938 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1939 |
$features = array_fill_keys( (array) $feature, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1940 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1941 |
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
|
1942 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1943 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1944 |
/** |
5 | 1945 |
* Update the post type for the post ID. |
0 | 1946 |
* |
1947 |
* The page or post cache will be cleaned for the post ID. |
|
1948 |
* |
|
1949 |
* @since 2.5.0 |
|
1950 |
* |
|
5 | 1951 |
* @global wpdb $wpdb WordPress database abstraction object. |
1952 |
* |
|
1953 |
* @param int $post_id Optional. Post ID to change post type. Default 0. |
|
1954 |
* @param string $post_type Optional. Post type. Accepts 'post' or 'page' to |
|
1955 |
* name a few. Default 'post'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1956 |
* @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. |
0 | 1957 |
*/ |
1958 |
function set_post_type( $post_id = 0, $post_type = 'post' ) { |
|
1959 |
global $wpdb; |
|
1960 |
||
9 | 1961 |
$post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' ); |
1962 |
$return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) ); |
|
0 | 1963 |
|
1964 |
clean_post_cache( $post_id ); |
|
1965 |
||
1966 |
return $return; |
|
1967 |
} |
|
1968 |
||
1969 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1970 |
* Determines whether a post type is considered "viewable". |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1971 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1972 |
* 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
|
1973 |
* 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
|
1974 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1975 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1976 |
* @since 4.5.0 Added the ability to pass a post type name in addition to object. |
9 | 1977 |
* @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
|
1978 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1979 |
* @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
|
1980 |
* @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
|
1981 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1982 |
function is_post_type_viewable( $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1983 |
if ( is_scalar( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1984 |
$post_type = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1985 |
if ( ! $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1986 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1987 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1988 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1989 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1990 |
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
|
1991 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1992 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1993 |
/** |
9 | 1994 |
* Retrieves an array of the latest posts, or posts matching the given criteria. |
0 | 1995 |
* |
1996 |
* The defaults are as follows: |
|
1997 |
* |
|
1998 |
* @since 1.2.0 |
|
5 | 1999 |
* |
2000 |
* @see WP_Query::parse_query() |
|
2001 |
* |
|
2002 |
* @param array $args { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2003 |
* Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all |
5 | 2004 |
* available arguments. |
2005 |
* |
|
2006 |
* @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
|
2007 |
* in WP_Query. Accepts -1 for all. Default 5. |
5 | 2008 |
* @type int|string $category Category ID or comma-separated list of IDs (this or any children). |
2009 |
* Is an alias of $cat in WP_Query. Default 0. |
|
2010 |
* @type array $include An array of post IDs to retrieve, sticky posts will be included. |
|
2011 |
* Is an alias of $post__in in WP_Query. Default empty array. |
|
2012 |
* @type array $exclude An array of post IDs not to retrieve. Default empty array. |
|
2013 |
* @type bool $suppress_filters Whether to suppress filters. Default true. |
|
2014 |
* } |
|
9 | 2015 |
* @return WP_Post[]|int[] Array of post objects or post IDs. |
0 | 2016 |
*/ |
5 | 2017 |
function get_posts( $args = null ) { |
0 | 2018 |
$defaults = array( |
9 | 2019 |
'numberposts' => 5, |
2020 |
'category' => 0, |
|
2021 |
'orderby' => 'date', |
|
2022 |
'order' => 'DESC', |
|
2023 |
'include' => array(), |
|
2024 |
'exclude' => array(), |
|
2025 |
'meta_key' => '', |
|
2026 |
'meta_value' => '', |
|
2027 |
'post_type' => 'post', |
|
2028 |
'suppress_filters' => true, |
|
0 | 2029 |
); |
2030 |
||
16 | 2031 |
$parsed_args = wp_parse_args( $args, $defaults ); |
2032 |
if ( empty( $parsed_args['post_status'] ) ) { |
|
2033 |
$parsed_args['post_status'] = ( 'attachment' === $parsed_args['post_type'] ) ? 'inherit' : 'publish'; |
|
2034 |
} |
|
2035 |
if ( ! empty( $parsed_args['numberposts'] ) && empty( $parsed_args['posts_per_page'] ) ) { |
|
2036 |
$parsed_args['posts_per_page'] = $parsed_args['numberposts']; |
|
2037 |
} |
|
2038 |
if ( ! empty( $parsed_args['category'] ) ) { |
|
2039 |
$parsed_args['cat'] = $parsed_args['category']; |
|
2040 |
} |
|
2041 |
if ( ! empty( $parsed_args['include'] ) ) { |
|
2042 |
$incposts = wp_parse_id_list( $parsed_args['include'] ); |
|
2043 |
$parsed_args['posts_per_page'] = count( $incposts ); // Only the number of posts included. |
|
2044 |
$parsed_args['post__in'] = $incposts; |
|
2045 |
} elseif ( ! empty( $parsed_args['exclude'] ) ) { |
|
2046 |
$parsed_args['post__not_in'] = wp_parse_id_list( $parsed_args['exclude'] ); |
|
2047 |
} |
|
2048 |
||
2049 |
$parsed_args['ignore_sticky_posts'] = true; |
|
2050 |
$parsed_args['no_found_rows'] = true; |
|
0 | 2051 |
|
2052 |
$get_posts = new WP_Query; |
|
16 | 2053 |
return $get_posts->query( $parsed_args ); |
0 | 2054 |
|
2055 |
} |
|
2056 |
||
2057 |
// |
|
16 | 2058 |
// Post meta functions. |
0 | 2059 |
// |
2060 |
||
2061 |
/** |
|
9 | 2062 |
* Adds a meta field to the given post. |
0 | 2063 |
* |
2064 |
* Post meta data is called "Custom Fields" on the Administration Screen. |
|
2065 |
* |
|
2066 |
* @since 1.5.0 |
|
5 | 2067 |
* |
2068 |
* @param int $post_id Post ID. |
|
2069 |
* @param string $meta_key Metadata name. |
|
2070 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
2071 |
* @param bool $unique Optional. Whether the same key should not be added. |
|
2072 |
* Default false. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2073 |
* @return int|false Meta ID on success, false on failure. |
0 | 2074 |
*/ |
5 | 2075 |
function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { |
2076 |
// Make sure meta is added to the post, not a revision. |
|
9 | 2077 |
$the_post = wp_is_post_revision( $post_id ); |
2078 |
if ( $the_post ) { |
|
0 | 2079 |
$post_id = $the_post; |
9 | 2080 |
} |
2081 |
||
2082 |
return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); |
|
0 | 2083 |
} |
2084 |
||
2085 |
/** |
|
9 | 2086 |
* Deletes a post meta field for the given post ID. |
0 | 2087 |
* |
2088 |
* You can match based on the key, or key and value. Removing based on key and |
|
2089 |
* value, will keep from removing duplicate metadata with the same key. It also |
|
9 | 2090 |
* allows removing all metadata matching the key, if needed. |
0 | 2091 |
* |
2092 |
* @since 1.5.0 |
|
5 | 2093 |
* |
2094 |
* @param int $post_id Post ID. |
|
2095 |
* @param string $meta_key Metadata name. |
|
16 | 2096 |
* @param mixed $meta_value Optional. Metadata value. If provided, |
2097 |
* rows will only be removed that match the value. |
|
2098 |
* Must be serializable if non-scalar. Default empty. |
|
0 | 2099 |
* @return bool True on success, false on failure. |
2100 |
*/ |
|
5 | 2101 |
function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { |
2102 |
// Make sure meta is added to the post, not a revision. |
|
9 | 2103 |
$the_post = wp_is_post_revision( $post_id ); |
2104 |
if ( $the_post ) { |
|
0 | 2105 |
$post_id = $the_post; |
9 | 2106 |
} |
2107 |
||
2108 |
return delete_metadata( 'post', $post_id, $meta_key, $meta_value ); |
|
0 | 2109 |
} |
2110 |
||
2111 |
/** |
|
9 | 2112 |
* Retrieves a post meta field for the given post ID. |
0 | 2113 |
* |
2114 |
* @since 1.5.0 |
|
5 | 2115 |
* |
2116 |
* @param int $post_id Post ID. |
|
16 | 2117 |
* @param string $key Optional. The meta key to retrieve. By default, |
2118 |
* returns data for all keys. Default empty. |
|
2119 |
* @param bool $single Optional. Whether to return a single value. |
|
2120 |
* This parameter has no effect if $key is not specified. |
|
2121 |
* Default false. |
|
2122 |
* @return mixed An array if $single is false. The value of the meta field |
|
2123 |
* if $single is true. False for an invalid $post_id. |
|
0 | 2124 |
*/ |
5 | 2125 |
function get_post_meta( $post_id, $key = '', $single = false ) { |
9 | 2126 |
return get_metadata( 'post', $post_id, $key, $single ); |
0 | 2127 |
} |
2128 |
||
2129 |
/** |
|
9 | 2130 |
* Updates a post meta field based on the given post ID. |
2131 |
* |
|
2132 |
* Use the `$prev_value` parameter to differentiate between meta fields with the |
|
0 | 2133 |
* same key and post ID. |
2134 |
* |
|
9 | 2135 |
* If the meta field for the post does not exist, it will be added and its ID returned. |
2136 |
* |
|
2137 |
* Can be used in place of add_post_meta(). |
|
0 | 2138 |
* |
2139 |
* @since 1.5.0 |
|
5 | 2140 |
* |
2141 |
* @param int $post_id Post ID. |
|
2142 |
* @param string $meta_key Metadata key. |
|
2143 |
* @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|
9 | 2144 |
* @param mixed $prev_value Optional. Previous value to check before updating. |
16 | 2145 |
* If specified, only update existing metadata entries with |
2146 |
* this value. Otherwise, update all entries. Default empty. |
|
2147 |
* @return int|bool Meta ID if the key didn't exist, true on successful update, |
|
2148 |
* false on failure or if the value passed to the function |
|
2149 |
* is the same as the one that is already in the database. |
|
0 | 2150 |
*/ |
5 | 2151 |
function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { |
2152 |
// Make sure meta is added to the post, not a revision. |
|
9 | 2153 |
$the_post = wp_is_post_revision( $post_id ); |
2154 |
if ( $the_post ) { |
|
0 | 2155 |
$post_id = $the_post; |
9 | 2156 |
} |
2157 |
||
2158 |
return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); |
|
0 | 2159 |
} |
2160 |
||
2161 |
/** |
|
9 | 2162 |
* Deletes everything from post meta matching the given meta key. |
0 | 2163 |
* |
2164 |
* @since 2.3.0 |
|
2165 |
* |
|
2166 |
* @param string $post_meta_key Key to search for when deleting. |
|
5 | 2167 |
* @return bool Whether the post meta key was deleted from the database. |
0 | 2168 |
*/ |
5 | 2169 |
function delete_post_meta_by_key( $post_meta_key ) { |
9 | 2170 |
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
|
2171 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2172 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2173 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2174 |
* Registers a meta key for posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2175 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2176 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2177 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2178 |
* @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
|
2179 |
* 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
|
2180 |
* @param string $meta_key The meta key to register. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2181 |
* @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
|
2182 |
* {@see register_meta()} for a list of supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2183 |
* @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
|
2184 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2185 |
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
|
2186 |
$args['object_subtype'] = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2187 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2188 |
return register_meta( 'post', $meta_key, $args ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2189 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2190 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2191 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2192 |
* Unregisters a meta key for posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2193 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2194 |
* @since 4.9.8 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2195 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2196 |
* @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
|
2197 |
* 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
|
2198 |
* existing post types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2199 |
* @param string $meta_key The meta key to unregister. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2200 |
* @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
|
2201 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2202 |
function unregister_post_meta( $post_type, $meta_key ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2203 |
return unregister_meta_key( 'post', $meta_key, $post_type ); |
0 | 2204 |
} |
2205 |
||
2206 |
/** |
|
2207 |
* Retrieve post meta fields, based on post ID. |
|
2208 |
* |
|
2209 |
* The post meta fields are retrieved from the cache where possible, |
|
2210 |
* so the function is optimized to be called more than once. |
|
2211 |
* |
|
2212 |
* @since 1.2.0 |
|
5 | 2213 |
* |
2214 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|
2215 |
* @return array Post meta for the given post. |
|
0 | 2216 |
*/ |
2217 |
function get_post_custom( $post_id = 0 ) { |
|
2218 |
$post_id = absint( $post_id ); |
|
9 | 2219 |
if ( ! $post_id ) { |
0 | 2220 |
$post_id = get_the_ID(); |
9 | 2221 |
} |
0 | 2222 |
|
2223 |
return get_post_meta( $post_id ); |
|
2224 |
} |
|
2225 |
||
2226 |
/** |
|
2227 |
* Retrieve meta field names for a post. |
|
2228 |
* |
|
2229 |
* If there are no meta fields, then nothing (null) will be returned. |
|
2230 |
* |
|
2231 |
* @since 1.2.0 |
|
5 | 2232 |
* |
2233 |
* @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
|
2234 |
* @return array|void Array of the keys, if retrieved. |
0 | 2235 |
*/ |
2236 |
function get_post_custom_keys( $post_id = 0 ) { |
|
2237 |
$custom = get_post_custom( $post_id ); |
|
2238 |
||
9 | 2239 |
if ( ! is_array( $custom ) ) { |
0 | 2240 |
return; |
9 | 2241 |
} |
2242 |
||
16 | 2243 |
$keys = array_keys( $custom ); |
2244 |
if ( $keys ) { |
|
0 | 2245 |
return $keys; |
9 | 2246 |
} |
0 | 2247 |
} |
2248 |
||
2249 |
/** |
|
2250 |
* Retrieve values for a custom post field. |
|
2251 |
* |
|
2252 |
* The parameters must not be considered optional. All of the post meta fields |
|
2253 |
* will be retrieved and only the meta field key values returned. |
|
2254 |
* |
|
2255 |
* @since 1.2.0 |
|
5 | 2256 |
* |
2257 |
* @param string $key Optional. Meta field key. Default empty. |
|
2258 |
* @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
|
2259 |
* @return array|null Meta field values. |
0 | 2260 |
*/ |
2261 |
function get_post_custom_values( $key = '', $post_id = 0 ) { |
|
9 | 2262 |
if ( ! $key ) { |
0 | 2263 |
return null; |
9 | 2264 |
} |
2265 |
||
2266 |
$custom = get_post_custom( $post_id ); |
|
2267 |
||
2268 |
return isset( $custom[ $key ] ) ? $custom[ $key ] : null; |
|
0 | 2269 |
} |
2270 |
||
2271 |
/** |
|
9 | 2272 |
* Determines whether a post is sticky. |
0 | 2273 |
* |
2274 |
* Sticky posts should remain at the top of The Loop. If the post ID is not |
|
2275 |
* given, then The Loop ID for the current post will be used. |
|
2276 |
* |
|
9 | 2277 |
* For more information on this and similar theme functions, check out |
2278 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
2279 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
2280 |
* |
|
0 | 2281 |
* @since 2.7.0 |
2282 |
* |
|
5 | 2283 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post. |
0 | 2284 |
* @return bool Whether post is sticky. |
2285 |
*/ |
|
2286 |
function is_sticky( $post_id = 0 ) { |
|
2287 |
$post_id = absint( $post_id ); |
|
2288 |
||
9 | 2289 |
if ( ! $post_id ) { |
0 | 2290 |
$post_id = get_the_ID(); |
9 | 2291 |
} |
0 | 2292 |
|
2293 |
$stickies = get_option( 'sticky_posts' ); |
|
2294 |
||
16 | 2295 |
if ( is_array( $stickies ) ) { |
2296 |
$stickies = array_map( 'intval', $stickies ); |
|
2297 |
$is_sticky = in_array( $post_id, $stickies, true ); |
|
2298 |
} else { |
|
2299 |
$is_sticky = false; |
|
2300 |
} |
|
2301 |
||
2302 |
/** |
|
2303 |
* Filters whether a post is sticky. |
|
2304 |
* |
|
2305 |
* @since 5.3.0 |
|
2306 |
* |
|
2307 |
* @param bool $is_sticky Whether a post is sticky. |
|
2308 |
* @param int $post_id Post ID. |
|
2309 |
*/ |
|
2310 |
return apply_filters( 'is_sticky', $is_sticky, $post_id ); |
|
0 | 2311 |
} |
2312 |
||
2313 |
/** |
|
2314 |
* Sanitize every post field. |
|
2315 |
* |
|
5 | 2316 |
* If the context is 'raw', then the post object or array will get minimal |
2317 |
* sanitization of the integer fields. |
|
0 | 2318 |
* |
2319 |
* @since 2.3.0 |
|
5 | 2320 |
* |
2321 |
* @see sanitize_post_field() |
|
2322 |
* |
|
2323 |
* @param object|WP_Post|array $post The Post Object or Array |
|
2324 |
* @param string $context Optional. How to sanitize post fields. |
|
2325 |
* Accepts 'raw', 'edit', 'db', or 'display'. |
|
2326 |
* Default 'display'. |
|
2327 |
* @return object|WP_Post|array The now sanitized Post Object or Array (will be the |
|
2328 |
* same type as $post). |
|
0 | 2329 |
*/ |
5 | 2330 |
function sanitize_post( $post, $context = 'display' ) { |
9 | 2331 |
if ( is_object( $post ) ) { |
5 | 2332 |
// Check if post already filtered for this context. |
9 | 2333 |
if ( isset( $post->filter ) && $context == $post->filter ) { |
0 | 2334 |
return $post; |
9 | 2335 |
} |
2336 |
if ( ! isset( $post->ID ) ) { |
|
0 | 2337 |
$post->ID = 0; |
9 | 2338 |
} |
2339 |
foreach ( array_keys( get_object_vars( $post ) ) as $field ) { |
|
2340 |
$post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|
2341 |
} |
|
0 | 2342 |
$post->filter = $context; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2343 |
} elseif ( is_array( $post ) ) { |
5 | 2344 |
// Check if post already filtered for this context. |
9 | 2345 |
if ( isset( $post['filter'] ) && $context == $post['filter'] ) { |
0 | 2346 |
return $post; |
9 | 2347 |
} |
2348 |
if ( ! isset( $post['ID'] ) ) { |
|
0 | 2349 |
$post['ID'] = 0; |
9 | 2350 |
} |
2351 |
foreach ( array_keys( $post ) as $field ) { |
|
2352 |
$post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context ); |
|
2353 |
} |
|
0 | 2354 |
$post['filter'] = $context; |
2355 |
} |
|
2356 |
return $post; |
|
2357 |
} |
|
2358 |
||
2359 |
/** |
|
16 | 2360 |
* Sanitizes a post field based on context. |
0 | 2361 |
* |
5 | 2362 |
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and |
2363 |
* 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts |
|
2364 |
* are treated like 'display' when calling filters. |
|
0 | 2365 |
* |
2366 |
* @since 2.3.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2367 |
* @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. |
5 | 2368 |
* |
2369 |
* @param string $field The Post Object field name. |
|
2370 |
* @param mixed $value The Post Object value. |
|
2371 |
* @param int $post_id Post ID. |
|
16 | 2372 |
* @param string $context Optional. How to sanitize the field. Possible values are 'raw', 'edit', |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2373 |
* 'db', 'display', 'attribute' and 'js'. Default 'display'. |
0 | 2374 |
* @return mixed Sanitized value. |
2375 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2376 |
function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { |
9 | 2377 |
$int_fields = array( 'ID', 'post_parent', 'menu_order' ); |
16 | 2378 |
if ( in_array( $field, $int_fields, true ) ) { |
0 | 2379 |
$value = (int) $value; |
9 | 2380 |
} |
0 | 2381 |
|
5 | 2382 |
// Fields which contain arrays of integers. |
0 | 2383 |
$array_int_fields = array( 'ancestors' ); |
16 | 2384 |
if ( in_array( $field, $array_int_fields, true ) ) { |
9 | 2385 |
$value = array_map( 'absint', $value ); |
0 | 2386 |
return $value; |
2387 |
} |
|
2388 |
||
16 | 2389 |
if ( 'raw' === $context ) { |
0 | 2390 |
return $value; |
9 | 2391 |
} |
0 | 2392 |
|
2393 |
$prefixed = false; |
|
9 | 2394 |
if ( false !== strpos( $field, 'post_' ) ) { |
2395 |
$prefixed = true; |
|
2396 |
$field_no_prefix = str_replace( 'post_', '', $field ); |
|
0 | 2397 |
} |
2398 |
||
16 | 2399 |
if ( 'edit' === $context ) { |
9 | 2400 |
$format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' ); |
0 | 2401 |
|
2402 |
if ( $prefixed ) { |
|
5 | 2403 |
|
2404 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2405 |
* Filters the value of a specific post field to edit. |
5 | 2406 |
* |
2407 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2408 |
* field name. |
|
2409 |
* |
|
2410 |
* @since 2.3.0 |
|
2411 |
* |
|
2412 |
* @param mixed $value Value of the post field. |
|
2413 |
* @param int $post_id Post ID. |
|
2414 |
*/ |
|
2415 |
$value = apply_filters( "edit_{$field}", $value, $post_id ); |
|
2416 |
||
2417 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2418 |
* Filters the value of a specific post field to edit. |
5 | 2419 |
* |
2420 |
* The dynamic portion of the hook name, `$field_no_prefix`, refers to |
|
2421 |
* the post field name. |
|
2422 |
* |
|
2423 |
* @since 2.3.0 |
|
2424 |
* |
|
2425 |
* @param mixed $value Value of the post field. |
|
2426 |
* @param int $post_id Post ID. |
|
2427 |
*/ |
|
2428 |
$value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); |
|
0 | 2429 |
} else { |
5 | 2430 |
$value = apply_filters( "edit_post_{$field}", $value, $post_id ); |
0 | 2431 |
} |
2432 |
||
16 | 2433 |
if ( in_array( $field, $format_to_edit, true ) ) { |
2434 |
if ( 'post_content' === $field ) { |
|
9 | 2435 |
$value = format_to_edit( $value, user_can_richedit() ); |
2436 |
} else { |
|
2437 |
$value = format_to_edit( $value ); |
|
2438 |
} |
|
0 | 2439 |
} else { |
9 | 2440 |
$value = esc_attr( $value ); |
0 | 2441 |
} |
16 | 2442 |
} elseif ( 'db' === $context ) { |
0 | 2443 |
if ( $prefixed ) { |
5 | 2444 |
|
2445 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2446 |
* Filters the value of a specific post field before saving. |
5 | 2447 |
* |
2448 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2449 |
* field name. |
|
2450 |
* |
|
2451 |
* @since 2.3.0 |
|
2452 |
* |
|
2453 |
* @param mixed $value Value of the post field. |
|
2454 |
*/ |
|
2455 |
$value = apply_filters( "pre_{$field}", $value ); |
|
2456 |
||
2457 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2458 |
* Filters the value of a specific field before saving. |
5 | 2459 |
* |
2460 |
* The dynamic portion of the hook name, `$field_no_prefix`, refers |
|
2461 |
* to the post field name. |
|
2462 |
* |
|
2463 |
* @since 2.3.0 |
|
2464 |
* |
|
2465 |
* @param mixed $value Value of the post field. |
|
2466 |
*/ |
|
2467 |
$value = apply_filters( "{$field_no_prefix}_save_pre", $value ); |
|
0 | 2468 |
} else { |
5 | 2469 |
$value = apply_filters( "pre_post_{$field}", $value ); |
2470 |
||
2471 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2472 |
* Filters the value of a specific post field before saving. |
5 | 2473 |
* |
2474 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2475 |
* field name. |
|
2476 |
* |
|
2477 |
* @since 2.3.0 |
|
2478 |
* |
|
2479 |
* @param mixed $value Value of the post field. |
|
2480 |
*/ |
|
2481 |
$value = apply_filters( "{$field}_pre", $value ); |
|
0 | 2482 |
} |
2483 |
} else { |
|
5 | 2484 |
|
0 | 2485 |
// Use display filters by default. |
5 | 2486 |
if ( $prefixed ) { |
2487 |
||
2488 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2489 |
* Filters the value of a specific post field for display. |
5 | 2490 |
* |
2491 |
* The dynamic portion of the hook name, `$field`, refers to the post |
|
2492 |
* field name. |
|
2493 |
* |
|
2494 |
* @since 2.3.0 |
|
2495 |
* |
|
2496 |
* @param mixed $value Value of the prefixed post field. |
|
2497 |
* @param int $post_id Post ID. |
|
2498 |
* @param string $context Context for how to sanitize the field. Possible |
|
16 | 2499 |
* values include 'edit', 'display', |
5 | 2500 |
* 'attribute' and 'js'. |
2501 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2502 |
$value = apply_filters( "{$field}", $value, $post_id, $context ); |
5 | 2503 |
} else { |
2504 |
$value = apply_filters( "post_{$field}", $value, $post_id, $context ); |
|
2505 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2506 |
|
16 | 2507 |
if ( 'attribute' === $context ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2508 |
$value = esc_attr( $value ); |
16 | 2509 |
} elseif ( 'js' === $context ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2510 |
$value = esc_js( $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2511 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2512 |
} |
0 | 2513 |
|
2514 |
return $value; |
|
2515 |
} |
|
2516 |
||
2517 |
/** |
|
2518 |
* Make a post sticky. |
|
2519 |
* |
|
2520 |
* Sticky posts should be displayed at the top of the front page. |
|
2521 |
* |
|
2522 |
* @since 2.7.0 |
|
2523 |
* |
|
2524 |
* @param int $post_id Post ID. |
|
2525 |
*/ |
|
5 | 2526 |
function stick_post( $post_id ) { |
16 | 2527 |
$post_id = (int) $post_id; |
9 | 2528 |
$stickies = get_option( 'sticky_posts' ); |
2529 |
||
2530 |
if ( ! is_array( $stickies ) ) { |
|
16 | 2531 |
$stickies = array(); |
2532 |
} |
|
2533 |
||
2534 |
$stickies = array_map( 'intval', $stickies ); |
|
2535 |
||
2536 |
if ( ! in_array( $post_id, $stickies, true ) ) { |
|
0 | 2537 |
$stickies[] = $post_id; |
9 | 2538 |
} |
0 | 2539 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2540 |
$updated = update_option( 'sticky_posts', $stickies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2541 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2542 |
if ( $updated ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2543 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2544 |
* 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
|
2545 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2546 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2547 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2548 |
* @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
|
2549 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2550 |
do_action( 'post_stuck', $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2551 |
} |
0 | 2552 |
} |
2553 |
||
2554 |
/** |
|
5 | 2555 |
* Un-stick a post. |
0 | 2556 |
* |
2557 |
* Sticky posts should be displayed at the top of the front page. |
|
2558 |
* |
|
2559 |
* @since 2.7.0 |
|
2560 |
* |
|
2561 |
* @param int $post_id Post ID. |
|
2562 |
*/ |
|
5 | 2563 |
function unstick_post( $post_id ) { |
16 | 2564 |
$post_id = (int) $post_id; |
9 | 2565 |
$stickies = get_option( 'sticky_posts' ); |
2566 |
||
2567 |
if ( ! is_array( $stickies ) ) { |
|
0 | 2568 |
return; |
9 | 2569 |
} |
2570 |
||
16 | 2571 |
$stickies = array_map( 'intval', $stickies ); |
2572 |
||
2573 |
if ( ! in_array( $post_id, $stickies, true ) ) { |
|
0 | 2574 |
return; |
9 | 2575 |
} |
2576 |
||
16 | 2577 |
$offset = array_search( $post_id, $stickies, true ); |
9 | 2578 |
if ( false === $offset ) { |
0 | 2579 |
return; |
9 | 2580 |
} |
2581 |
||
2582 |
array_splice( $stickies, $offset, 1 ); |
|
0 | 2583 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2584 |
$updated = update_option( 'sticky_posts', $stickies ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2585 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2586 |
if ( $updated ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2587 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2588 |
* 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
|
2589 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2590 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2591 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2592 |
* @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
|
2593 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2594 |
do_action( 'post_unstuck', $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2595 |
} |
0 | 2596 |
} |
2597 |
||
2598 |
/** |
|
5 | 2599 |
* Return the cache key for wp_count_posts() based on the passed arguments. |
2600 |
* |
|
2601 |
* @since 3.9.0 |
|
9 | 2602 |
* @access private |
5 | 2603 |
* |
2604 |
* @param string $type Optional. Post type to retrieve count Default 'post'. |
|
2605 |
* @param string $perm Optional. 'readable' or empty. Default empty. |
|
2606 |
* @return string The cache key. |
|
2607 |
*/ |
|
2608 |
function _count_posts_cache_key( $type = 'post', $perm = '' ) { |
|
2609 |
$cache_key = 'posts-' . $type; |
|
16 | 2610 |
|
2611 |
if ( 'readable' === $perm && is_user_logged_in() ) { |
|
5 | 2612 |
$post_type_object = get_post_type_object( $type ); |
16 | 2613 |
|
5 | 2614 |
if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
2615 |
$cache_key .= '_' . $perm . '_' . get_current_user_id(); |
|
2616 |
} |
|
2617 |
} |
|
16 | 2618 |
|
5 | 2619 |
return $cache_key; |
2620 |
} |
|
2621 |
||
2622 |
/** |
|
0 | 2623 |
* Count number of posts of a post type and if user has permissions to view. |
2624 |
* |
|
2625 |
* This function provides an efficient method of finding the amount of post's |
|
2626 |
* type a blog has. Another method is to count the amount of items in |
|
2627 |
* get_posts(), but that method has a lot of overhead with doing so. Therefore, |
|
2628 |
* when developing for 2.5+, use this function instead. |
|
2629 |
* |
|
2630 |
* The $perm parameter checks for 'readable' value and if the user can read |
|
2631 |
* private posts, it will display that for the user that is signed in. |
|
2632 |
* |
|
2633 |
* @since 2.5.0 |
|
2634 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2635 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2636 |
* |
5 | 2637 |
* @param string $type Optional. Post type to retrieve count. Default 'post'. |
2638 |
* @param string $perm Optional. 'readable' or empty. Default empty. |
|
2639 |
* @return object Number of posts for each status. |
|
0 | 2640 |
*/ |
2641 |
function wp_count_posts( $type = 'post', $perm = '' ) { |
|
2642 |
global $wpdb; |
|
2643 |
||
9 | 2644 |
if ( ! post_type_exists( $type ) ) { |
0 | 2645 |
return new stdClass; |
9 | 2646 |
} |
0 | 2647 |
|
5 | 2648 |
$cache_key = _count_posts_cache_key( $type, $perm ); |
2649 |
||
2650 |
$counts = wp_cache_get( $cache_key, 'counts' ); |
|
2651 |
if ( false !== $counts ) { |
|
16 | 2652 |
// We may have cached this before every status was registered. |
2653 |
foreach ( get_post_stati() as $status ) { |
|
2654 |
if ( ! isset( $counts->{$status} ) ) { |
|
2655 |
$counts->{$status} = 0; |
|
2656 |
} |
|
2657 |
} |
|
2658 |
||
5 | 2659 |
/** This filter is documented in wp-includes/post.php */ |
2660 |
return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|
2661 |
} |
|
0 | 2662 |
|
2663 |
$query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; |
|
16 | 2664 |
|
2665 |
if ( 'readable' === $perm && is_user_logged_in() ) { |
|
9 | 2666 |
$post_type_object = get_post_type_object( $type ); |
5 | 2667 |
if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
9 | 2668 |
$query .= $wpdb->prepare( |
2669 |
" AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", |
|
5 | 2670 |
get_current_user_id() |
2671 |
); |
|
0 | 2672 |
} |
2673 |
} |
|
16 | 2674 |
|
0 | 2675 |
$query .= ' GROUP BY post_status'; |
2676 |
||
5 | 2677 |
$results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); |
9 | 2678 |
$counts = array_fill_keys( get_post_stati(), 0 ); |
5 | 2679 |
|
2680 |
foreach ( $results as $row ) { |
|
2681 |
$counts[ $row['post_status'] ] = $row['num_posts']; |
|
0 | 2682 |
} |
2683 |
||
5 | 2684 |
$counts = (object) $counts; |
2685 |
wp_cache_set( $cache_key, $counts, 'counts' ); |
|
2686 |
||
0 | 2687 |
/** |
2688 |
* Modify returned post counts by status for the current post type. |
|
2689 |
* |
|
2690 |
* @since 3.7.0 |
|
2691 |
* |
|
5 | 2692 |
* @param object $counts An object containing the current post_type's post |
2693 |
* counts by status. |
|
2694 |
* @param string $type Post type. |
|
2695 |
* @param string $perm The permission to determine if the posts are 'readable' |
|
2696 |
* by the current user. |
|
0 | 2697 |
*/ |
2698 |
return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|
2699 |
} |
|
2700 |
||
2701 |
/** |
|
2702 |
* Count number of attachments for the mime type(s). |
|
2703 |
* |
|
2704 |
* If you set the optional mime_type parameter, then an array will still be |
|
2705 |
* returned, but will only have the item you are looking for. It does not give |
|
2706 |
* you the number of attachments that are children of a post. You can get that |
|
2707 |
* by counting the number of children that post has. |
|
2708 |
* |
|
2709 |
* @since 2.5.0 |
|
2710 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2711 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2712 |
* |
5 | 2713 |
* @param string|array $mime_type Optional. Array or comma-separated list of |
2714 |
* MIME patterns. Default empty. |
|
2715 |
* @return object An object containing the attachment counts by mime type. |
|
0 | 2716 |
*/ |
2717 |
function wp_count_attachments( $mime_type = '' ) { |
|
2718 |
global $wpdb; |
|
2719 |
||
9 | 2720 |
$and = wp_post_mime_type_where( $mime_type ); |
0 | 2721 |
$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 ); |
2722 |
||
2723 |
$counts = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2724 |
foreach ( (array) $count as $row ) { |
0 | 2725 |
$counts[ $row['post_mime_type'] ] = $row['num_posts']; |
2726 |
} |
|
9 | 2727 |
$counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); |
0 | 2728 |
|
2729 |
/** |
|
2730 |
* Modify returned attachment counts by mime type. |
|
2731 |
* |
|
2732 |
* @since 3.7.0 |
|
2733 |
* |
|
5 | 2734 |
* @param object $counts An object containing the attachment counts by |
2735 |
* mime type. |
|
2736 |
* @param string $mime_type The mime type pattern used to filter the attachments |
|
2737 |
* counted. |
|
0 | 2738 |
*/ |
2739 |
return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); |
|
2740 |
} |
|
2741 |
||
2742 |
/** |
|
5 | 2743 |
* Get default post mime types. |
0 | 2744 |
* |
2745 |
* @since 2.9.0 |
|
16 | 2746 |
* @since 5.3.0 Added the 'Documents', 'Spreadsheets', and 'Archives' mime type groups. |
0 | 2747 |
* |
5 | 2748 |
* @return array List of post mime types. |
0 | 2749 |
*/ |
2750 |
function get_post_mime_types() { |
|
16 | 2751 |
$post_mime_types = array( // array( adj, noun ) |
2752 |
'image' => array( |
|
2753 |
__( 'Images' ), |
|
2754 |
__( 'Manage Images' ), |
|
2755 |
/* translators: %s: Number of images. */ |
|
2756 |
_n_noop( |
|
2757 |
'Image <span class="count">(%s)</span>', |
|
2758 |
'Images <span class="count">(%s)</span>' |
|
2759 |
), |
|
2760 |
), |
|
2761 |
'audio' => array( |
|
2762 |
__( 'Audio' ), |
|
2763 |
__( 'Manage Audio' ), |
|
2764 |
/* translators: %s: Number of audio files. */ |
|
2765 |
_n_noop( |
|
2766 |
'Audio <span class="count">(%s)</span>', |
|
2767 |
'Audio <span class="count">(%s)</span>' |
|
2768 |
), |
|
2769 |
), |
|
2770 |
'video' => array( |
|
2771 |
__( 'Video' ), |
|
2772 |
__( 'Manage Video' ), |
|
2773 |
/* translators: %s: Number of video files. */ |
|
2774 |
_n_noop( |
|
2775 |
'Video <span class="count">(%s)</span>', |
|
2776 |
'Video <span class="count">(%s)</span>' |
|
2777 |
), |
|
2778 |
), |
|
2779 |
'document' => array( |
|
2780 |
__( 'Documents' ), |
|
2781 |
__( 'Manage Documents' ), |
|
2782 |
/* translators: %s: Number of documents. */ |
|
2783 |
_n_noop( |
|
2784 |
'Document <span class="count">(%s)</span>', |
|
2785 |
'Documents <span class="count">(%s)</span>' |
|
2786 |
), |
|
2787 |
), |
|
2788 |
'spreadsheet' => array( |
|
2789 |
__( 'Spreadsheets' ), |
|
2790 |
__( 'Manage Spreadsheets' ), |
|
2791 |
/* translators: %s: Number of spreadsheets. */ |
|
2792 |
_n_noop( |
|
2793 |
'Spreadsheet <span class="count">(%s)</span>', |
|
2794 |
'Spreadsheets <span class="count">(%s)</span>' |
|
2795 |
), |
|
2796 |
), |
|
2797 |
'archive' => array( |
|
2798 |
_x( 'Archives', 'file type group' ), |
|
2799 |
__( 'Manage Archives' ), |
|
2800 |
/* translators: %s: Number of archives. */ |
|
2801 |
_n_noop( |
|
2802 |
'Archive <span class="count">(%s)</span>', |
|
2803 |
'Archives <span class="count">(%s)</span>' |
|
2804 |
), |
|
2805 |
), |
|
0 | 2806 |
); |
2807 |
||
16 | 2808 |
$ext_types = wp_get_ext_types(); |
2809 |
$mime_types = wp_get_mime_types(); |
|
2810 |
||
2811 |
foreach ( $post_mime_types as $group => $labels ) { |
|
2812 |
if ( in_array( $group, array( 'image', 'audio', 'video' ), true ) ) { |
|
2813 |
continue; |
|
2814 |
} |
|
2815 |
||
2816 |
if ( ! isset( $ext_types[ $group ] ) ) { |
|
2817 |
unset( $post_mime_types[ $group ] ); |
|
2818 |
continue; |
|
2819 |
} |
|
2820 |
||
2821 |
$group_mime_types = array(); |
|
2822 |
foreach ( $ext_types[ $group ] as $extension ) { |
|
2823 |
foreach ( $mime_types as $exts => $mime ) { |
|
2824 |
if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { |
|
2825 |
$group_mime_types[] = $mime; |
|
2826 |
break; |
|
2827 |
} |
|
2828 |
} |
|
2829 |
} |
|
2830 |
$group_mime_types = implode( ',', array_unique( $group_mime_types ) ); |
|
2831 |
||
2832 |
$post_mime_types[ $group_mime_types ] = $labels; |
|
2833 |
unset( $post_mime_types[ $group ] ); |
|
2834 |
} |
|
2835 |
||
5 | 2836 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2837 |
* Filters the default list of post mime types. |
5 | 2838 |
* |
2839 |
* @since 2.5.0 |
|
2840 |
* |
|
2841 |
* @param array $post_mime_types Default list of post mime types. |
|
2842 |
*/ |
|
2843 |
return apply_filters( 'post_mime_types', $post_mime_types ); |
|
0 | 2844 |
} |
2845 |
||
2846 |
/** |
|
2847 |
* Check a MIME-Type against a list. |
|
2848 |
* |
|
2849 |
* If the wildcard_mime_types parameter is a string, it must be comma separated |
|
2850 |
* list. If the real_mime_types is a string, it is also comma separated to |
|
2851 |
* create the list. |
|
2852 |
* |
|
2853 |
* @since 2.5.0 |
|
2854 |
* |
|
5 | 2855 |
* @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*) |
2856 |
* or flash (same as *flash*). |
|
2857 |
* @param string|array $real_mime_types Real post mime type values. |
|
2858 |
* @return array array(wildcard=>array(real types)). |
|
0 | 2859 |
*/ |
5 | 2860 |
function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { |
0 | 2861 |
$matches = array(); |
5 | 2862 |
if ( is_string( $wildcard_mime_types ) ) { |
2863 |
$wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) ); |
|
2864 |
} |
|
2865 |
if ( is_string( $real_mime_types ) ) { |
|
2866 |
$real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) ); |
|
2867 |
} |
|
2868 |
||
2869 |
$patternses = array(); |
|
9 | 2870 |
$wild = '[-._a-z0-9]*'; |
5 | 2871 |
|
0 | 2872 |
foreach ( (array) $wildcard_mime_types as $type ) { |
5 | 2873 |
$mimes = array_map( 'trim', explode( ',', $type ) ); |
2874 |
foreach ( $mimes as $mime ) { |
|
16 | 2875 |
$regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); |
2876 |
||
9 | 2877 |
$patternses[][ $type ] = "^$regex$"; |
16 | 2878 |
|
5 | 2879 |
if ( false === strpos( $mime, '/' ) ) { |
9 | 2880 |
$patternses[][ $type ] = "^$regex/"; |
2881 |
$patternses[][ $type ] = $regex; |
|
5 | 2882 |
} |
0 | 2883 |
} |
2884 |
} |
|
5 | 2885 |
asort( $patternses ); |
2886 |
||
2887 |
foreach ( $patternses as $patterns ) { |
|
2888 |
foreach ( $patterns as $type => $pattern ) { |
|
2889 |
foreach ( (array) $real_mime_types as $real ) { |
|
16 | 2890 |
if ( preg_match( "#$pattern#", $real ) |
2891 |
&& ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ], true ) ) |
|
2892 |
) { |
|
9 | 2893 |
$matches[ $type ][] = $real; |
5 | 2894 |
} |
2895 |
} |
|
2896 |
} |
|
2897 |
} |
|
16 | 2898 |
|
0 | 2899 |
return $matches; |
2900 |
} |
|
2901 |
||
2902 |
/** |
|
2903 |
* Convert MIME types into SQL. |
|
2904 |
* |
|
2905 |
* @since 2.5.0 |
|
2906 |
* |
|
5 | 2907 |
* @param string|array $post_mime_types List of mime types or comma separated string |
2908 |
* of mime types. |
|
2909 |
* @param string $table_alias Optional. Specify a table alias, if needed. |
|
2910 |
* Default empty. |
|
0 | 2911 |
* @return string The SQL AND clause for mime searching. |
2912 |
*/ |
|
5 | 2913 |
function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { |
9 | 2914 |
$where = ''; |
2915 |
$wildcards = array( '', '%', '%/%' ); |
|
2916 |
if ( is_string( $post_mime_types ) ) { |
|
2917 |
$post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); |
|
2918 |
} |
|
5 | 2919 |
|
2920 |
$wheres = array(); |
|
2921 |
||
0 | 2922 |
foreach ( (array) $post_mime_types as $mime_type ) { |
9 | 2923 |
$mime_type = preg_replace( '/\s/', '', $mime_type ); |
2924 |
$slashpos = strpos( $mime_type, '/' ); |
|
0 | 2925 |
if ( false !== $slashpos ) { |
9 | 2926 |
$mime_group = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) ); |
2927 |
$mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) ); |
|
2928 |
if ( empty( $mime_subgroup ) ) { |
|
0 | 2929 |
$mime_subgroup = '*'; |
9 | 2930 |
} else { |
2931 |
$mime_subgroup = str_replace( '/', '', $mime_subgroup ); |
|
2932 |
} |
|
0 | 2933 |
$mime_pattern = "$mime_group/$mime_subgroup"; |
2934 |
} else { |
|
9 | 2935 |
$mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); |
2936 |
if ( false === strpos( $mime_pattern, '*' ) ) { |
|
0 | 2937 |
$mime_pattern .= '/*'; |
9 | 2938 |
} |
0 | 2939 |
} |
2940 |
||
9 | 2941 |
$mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); |
2942 |
||
16 | 2943 |
if ( in_array( $mime_type, $wildcards, true ) ) { |
0 | 2944 |
return ''; |
9 | 2945 |
} |
2946 |
||
2947 |
if ( false !== strpos( $mime_pattern, '%' ) ) { |
|
2948 |
$wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
|
2949 |
} else { |
|
2950 |
$wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
|
2951 |
} |
|
2952 |
} |
|
16 | 2953 |
|
9 | 2954 |
if ( ! empty( $wheres ) ) { |
2955 |
$where = ' AND (' . join( ' OR ', $wheres ) . ') '; |
|
2956 |
} |
|
16 | 2957 |
|
0 | 2958 |
return $where; |
2959 |
} |
|
2960 |
||
2961 |
/** |
|
5 | 2962 |
* Trash or delete a post or page. |
2963 |
* |
|
2964 |
* When the post and page is permanently deleted, everything that is tied to |
|
2965 |
* it is deleted also. This includes comments, post meta fields, and terms |
|
2966 |
* associated with the post. |
|
2967 |
* |
|
16 | 2968 |
* The post or page is moved to Trash instead of permanently deleted unless |
2969 |
* Trash is disabled, item is already in the Trash, or $force_delete is true. |
|
0 | 2970 |
* |
2971 |
* @since 1.0.0 |
|
5 | 2972 |
* |
2973 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
2974 |
* @see wp_delete_attachment() |
|
2975 |
* @see wp_trash_post() |
|
2976 |
* |
|
2977 |
* @param int $postid Optional. Post ID. Default 0. |
|
16 | 2978 |
* @param bool $force_delete Optional. Whether to bypass Trash and force deletion. |
5 | 2979 |
* Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2980 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 2981 |
*/ |
2982 |
function wp_delete_post( $postid = 0, $force_delete = false ) { |
|
2983 |
global $wpdb; |
|
2984 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2985 |
$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
|
2986 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2987 |
if ( ! $post ) { |
0 | 2988 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2989 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2990 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2991 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2992 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2993 |
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
|
2994 |
return wp_trash_post( $postid ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2995 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2996 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2997 |
if ( 'attachment' === $post->post_type ) { |
0 | 2998 |
return wp_delete_attachment( $postid, $force_delete ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
2999 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3000 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3001 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3002 |
* Filters whether a post deletion should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3003 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3004 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3005 |
* |
16 | 3006 |
* @param bool|null $delete Whether to go forward with deletion. |
3007 |
* @param WP_Post $post Post object. |
|
3008 |
* @param bool $force_delete Whether to bypass the Trash. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3009 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3010 |
$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
|
3011 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3012 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3013 |
} |
0 | 3014 |
|
5 | 3015 |
/** |
3016 |
* Fires before a post is deleted, at the start of wp_delete_post(). |
|
3017 |
* |
|
3018 |
* @since 3.2.0 |
|
16 | 3019 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3020 |
* |
3021 |
* @see wp_delete_post() |
|
3022 |
* |
|
16 | 3023 |
* @param int $postid Post ID. |
3024 |
* @param WP_Post $post Post object. |
|
5 | 3025 |
*/ |
16 | 3026 |
do_action( 'before_delete_post', $postid, $post ); |
0 | 3027 |
|
9 | 3028 |
delete_post_meta( $postid, '_wp_trash_meta_status' ); |
3029 |
delete_post_meta( $postid, '_wp_trash_meta_time' ); |
|
3030 |
||
3031 |
wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) ); |
|
3032 |
||
3033 |
$parent_data = array( 'post_parent' => $post->post_parent ); |
|
0 | 3034 |
$parent_where = array( 'post_parent' => $postid ); |
3035 |
||
3036 |
if ( is_post_type_hierarchical( $post->post_type ) ) { |
|
5 | 3037 |
// Point children of this page to its parent, also clean the cache of affected children. |
0 | 3038 |
$children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); |
9 | 3039 |
$children = $wpdb->get_results( $children_query ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3040 |
if ( $children ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3041 |
$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
|
3042 |
} |
0 | 3043 |
} |
3044 |
||
5 | 3045 |
// Do raw query. wp_get_post_revisions() is filtered. |
0 | 3046 |
$revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
3047 |
// Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
|
9 | 3048 |
foreach ( $revision_ids as $revision_id ) { |
0 | 3049 |
wp_delete_post_revision( $revision_id ); |
9 | 3050 |
} |
0 | 3051 |
|
5 | 3052 |
// Point all attachments to this post up one level. |
0 | 3053 |
$wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
3054 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3055 |
wp_defer_comment_counting( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3056 |
|
9 | 3057 |
$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
|
3058 |
foreach ( $comment_ids as $comment_id ) { |
0 | 3059 |
wp_delete_comment( $comment_id, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3060 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3061 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3062 |
wp_defer_comment_counting( false ); |
0 | 3063 |
|
9 | 3064 |
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) ); |
3065 |
foreach ( $post_meta_ids as $mid ) { |
|
0 | 3066 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 3067 |
} |
0 | 3068 |
|
5 | 3069 |
/** |
3070 |
* Fires immediately before a post is deleted from the database. |
|
3071 |
* |
|
3072 |
* @since 1.2.0 |
|
16 | 3073 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3074 |
* |
16 | 3075 |
* @param int $postid Post ID. |
3076 |
* @param WP_Post $post Post object. |
|
5 | 3077 |
*/ |
16 | 3078 |
do_action( 'delete_post', $postid, $post ); |
3079 |
||
5 | 3080 |
$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); |
3081 |
if ( ! $result ) { |
|
3082 |
return false; |
|
3083 |
} |
|
3084 |
||
3085 |
/** |
|
3086 |
* Fires immediately after a post is deleted from the database. |
|
3087 |
* |
|
3088 |
* @since 2.2.0 |
|
16 | 3089 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3090 |
* |
16 | 3091 |
* @param int $postid Post ID. |
3092 |
* @param WP_Post $post Post object. |
|
5 | 3093 |
*/ |
16 | 3094 |
do_action( 'deleted_post', $postid, $post ); |
0 | 3095 |
|
3096 |
clean_post_cache( $post ); |
|
3097 |
||
3098 |
if ( is_post_type_hierarchical( $post->post_type ) && $children ) { |
|
9 | 3099 |
foreach ( $children as $child ) { |
0 | 3100 |
clean_post_cache( $child ); |
9 | 3101 |
} |
3102 |
} |
|
3103 |
||
3104 |
wp_clear_scheduled_hook( 'publish_future_post', array( $postid ) ); |
|
0 | 3105 |
|
5 | 3106 |
/** |
3107 |
* Fires after a post is deleted, at the conclusion of wp_delete_post(). |
|
3108 |
* |
|
3109 |
* @since 3.2.0 |
|
16 | 3110 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 3111 |
* |
3112 |
* @see wp_delete_post() |
|
3113 |
* |
|
16 | 3114 |
* @param int $postid Post ID. |
3115 |
* @param WP_Post $post Post object. |
|
5 | 3116 |
*/ |
16 | 3117 |
do_action( 'after_delete_post', $postid, $post ); |
0 | 3118 |
|
3119 |
return $post; |
|
3120 |
} |
|
3121 |
||
3122 |
/** |
|
5 | 3123 |
* Reset the page_on_front, show_on_front, and page_for_post settings when |
3124 |
* a linked page is deleted or trashed. |
|
0 | 3125 |
* |
3126 |
* Also ensures the post is no longer sticky. |
|
3127 |
* |
|
5 | 3128 |
* @since 3.7.0 |
0 | 3129 |
* @access private |
5 | 3130 |
* |
3131 |
* @param int $post_id Post ID. |
|
0 | 3132 |
*/ |
3133 |
function _reset_front_page_settings_for_post( $post_id ) { |
|
3134 |
$post = get_post( $post_id ); |
|
16 | 3135 |
|
3136 |
if ( 'page' === $post->post_type ) { |
|
9 | 3137 |
/* |
16 | 3138 |
* If the page is defined in option page_on_front or post_for_posts, |
3139 |
* adjust the corresponding options. |
|
3140 |
*/ |
|
0 | 3141 |
if ( get_option( 'page_on_front' ) == $post->ID ) { |
3142 |
update_option( 'show_on_front', 'posts' ); |
|
3143 |
update_option( 'page_on_front', 0 ); |
|
3144 |
} |
|
3145 |
if ( get_option( 'page_for_posts' ) == $post->ID ) { |
|
16 | 3146 |
update_option( 'page_for_posts', 0 ); |
0 | 3147 |
} |
3148 |
} |
|
16 | 3149 |
|
0 | 3150 |
unstick_post( $post->ID ); |
3151 |
} |
|
3152 |
||
3153 |
/** |
|
5 | 3154 |
* Move a post or page to the Trash |
0 | 3155 |
* |
16 | 3156 |
* If Trash is disabled, the post or page is permanently deleted. |
0 | 3157 |
* |
3158 |
* @since 2.9.0 |
|
5 | 3159 |
* |
3160 |
* @see wp_delete_post() |
|
3161 |
* |
|
3162 |
* @param int $post_id Optional. Post ID. Default is ID of the global $post |
|
3163 |
* if EMPTY_TRASH_DAYS equals true. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3164 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 3165 |
*/ |
5 | 3166 |
function wp_trash_post( $post_id = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3167 |
if ( ! EMPTY_TRASH_DAYS ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3168 |
return wp_delete_post( $post_id, true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3169 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3170 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3171 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3172 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3173 |
if ( ! $post ) { |
0 | 3174 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3175 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3176 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3177 |
if ( 'trash' === $post->post_status ) { |
0 | 3178 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3179 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3180 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3181 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3182 |
* Filters whether a post trashing should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3183 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3184 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3185 |
* |
16 | 3186 |
* @param bool|null $trash Whether to go forward with trashing. |
3187 |
* @param WP_Post $post Post object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3188 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3189 |
$check = apply_filters( 'pre_trash_post', null, $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3190 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3191 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3192 |
} |
0 | 3193 |
|
5 | 3194 |
/** |
16 | 3195 |
* Fires before a post is sent to the Trash. |
5 | 3196 |
* |
3197 |
* @since 3.3.0 |
|
3198 |
* |
|
3199 |
* @param int $post_id Post ID. |
|
3200 |
*/ |
|
3201 |
do_action( 'wp_trash_post', $post_id ); |
|
0 | 3202 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3203 |
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
|
3204 |
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
|
3205 |
|
16 | 3206 |
$post_updated = wp_update_post( |
9 | 3207 |
array( |
3208 |
'ID' => $post_id, |
|
3209 |
'post_status' => 'trash', |
|
3210 |
) |
|
3211 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3212 |
|
16 | 3213 |
if ( ! $post_updated ) { |
3214 |
return false; |
|
3215 |
} |
|
3216 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3217 |
wp_trash_post_comments( $post_id ); |
0 | 3218 |
|
5 | 3219 |
/** |
16 | 3220 |
* Fires after a post is sent to the Trash. |
5 | 3221 |
* |
3222 |
* @since 2.9.0 |
|
3223 |
* |
|
3224 |
* @param int $post_id Post ID. |
|
3225 |
*/ |
|
3226 |
do_action( 'trashed_post', $post_id ); |
|
0 | 3227 |
|
3228 |
return $post; |
|
3229 |
} |
|
3230 |
||
3231 |
/** |
|
5 | 3232 |
* Restore a post or page from the Trash. |
0 | 3233 |
* |
3234 |
* @since 2.9.0 |
|
5 | 3235 |
* |
3236 |
* @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
|
3237 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 3238 |
*/ |
5 | 3239 |
function wp_untrash_post( $post_id = 0 ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3240 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3241 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3242 |
if ( ! $post ) { |
0 | 3243 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3244 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3245 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3246 |
if ( 'trash' !== $post->post_status ) { |
0 | 3247 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3248 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3249 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3250 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3251 |
* Filters whether a post untrashing should take place. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3252 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3253 |
* @since 4.9.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3254 |
* |
16 | 3255 |
* @param bool|null $untrash Whether to go forward with untrashing. |
3256 |
* @param WP_Post $post Post object. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3257 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3258 |
$check = apply_filters( 'pre_untrash_post', null, $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3259 |
if ( null !== $check ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3260 |
return $check; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3261 |
} |
0 | 3262 |
|
5 | 3263 |
/** |
16 | 3264 |
* Fires before a post is restored from the Trash. |
5 | 3265 |
* |
3266 |
* @since 2.9.0 |
|
3267 |
* |
|
3268 |
* @param int $post_id Post ID. |
|
3269 |
*/ |
|
3270 |
do_action( 'untrash_post', $post_id ); |
|
0 | 3271 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3272 |
$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
|
3273 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3274 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3275 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3276 |
|
16 | 3277 |
$post_updated = wp_update_post( |
9 | 3278 |
array( |
3279 |
'ID' => $post_id, |
|
3280 |
'post_status' => $post_status, |
|
3281 |
) |
|
3282 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3283 |
|
16 | 3284 |
if ( ! $post_updated ) { |
3285 |
return false; |
|
3286 |
} |
|
3287 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3288 |
wp_untrash_post_comments( $post_id ); |
0 | 3289 |
|
5 | 3290 |
/** |
16 | 3291 |
* Fires after a post is restored from the Trash. |
5 | 3292 |
* |
3293 |
* @since 2.9.0 |
|
3294 |
* |
|
3295 |
* @param int $post_id Post ID. |
|
3296 |
*/ |
|
3297 |
do_action( 'untrashed_post', $post_id ); |
|
0 | 3298 |
|
3299 |
return $post; |
|
3300 |
} |
|
3301 |
||
3302 |
/** |
|
16 | 3303 |
* Moves comments for a post to the Trash. |
0 | 3304 |
* |
3305 |
* @since 2.9.0 |
|
5 | 3306 |
* |
3307 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
3308 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3309 |
* @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
|
3310 |
* @return mixed|void False on failure. |
0 | 3311 |
*/ |
5 | 3312 |
function wp_trash_post_comments( $post = null ) { |
0 | 3313 |
global $wpdb; |
3314 |
||
9 | 3315 |
$post = get_post( $post ); |
3316 |
if ( empty( $post ) ) { |
|
0 | 3317 |
return; |
9 | 3318 |
} |
0 | 3319 |
|
3320 |
$post_id = $post->ID; |
|
3321 |
||
5 | 3322 |
/** |
16 | 3323 |
* Fires before comments are sent to the Trash. |
5 | 3324 |
* |
3325 |
* @since 2.9.0 |
|
3326 |
* |
|
3327 |
* @param int $post_id Post ID. |
|
3328 |
*/ |
|
3329 |
do_action( 'trash_post_comments', $post_id ); |
|
0 | 3330 |
|
9 | 3331 |
$comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); |
3332 |
if ( empty( $comments ) ) { |
|
0 | 3333 |
return; |
9 | 3334 |
} |
0 | 3335 |
|
5 | 3336 |
// Cache current status for each comment. |
0 | 3337 |
$statuses = array(); |
9 | 3338 |
foreach ( $comments as $comment ) { |
3339 |
$statuses[ $comment->comment_ID ] = $comment->comment_approved; |
|
3340 |
} |
|
3341 |
add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses ); |
|
0 | 3342 |
|
5 | 3343 |
// Set status for all comments to post-trashed. |
9 | 3344 |
$result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) ); |
3345 |
||
3346 |
clean_comment_cache( array_keys( $statuses ) ); |
|
0 | 3347 |
|
5 | 3348 |
/** |
16 | 3349 |
* Fires after comments are sent to the Trash. |
5 | 3350 |
* |
3351 |
* @since 2.9.0 |
|
3352 |
* |
|
3353 |
* @param int $post_id Post ID. |
|
3354 |
* @param array $statuses Array of comment statuses. |
|
3355 |
*/ |
|
3356 |
do_action( 'trashed_post_comments', $post_id, $statuses ); |
|
0 | 3357 |
|
3358 |
return $result; |
|
3359 |
} |
|
3360 |
||
3361 |
/** |
|
16 | 3362 |
* Restore comments for a post from the Trash. |
0 | 3363 |
* |
3364 |
* @since 2.9.0 |
|
5 | 3365 |
* |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3366 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3367 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3368 |
* @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
|
3369 |
* @return true|void |
0 | 3370 |
*/ |
5 | 3371 |
function wp_untrash_post_comments( $post = null ) { |
0 | 3372 |
global $wpdb; |
3373 |
||
9 | 3374 |
$post = get_post( $post ); |
3375 |
if ( empty( $post ) ) { |
|
0 | 3376 |
return; |
9 | 3377 |
} |
0 | 3378 |
|
3379 |
$post_id = $post->ID; |
|
3380 |
||
9 | 3381 |
$statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true ); |
3382 |
||
3383 |
if ( empty( $statuses ) ) { |
|
0 | 3384 |
return true; |
9 | 3385 |
} |
0 | 3386 |
|
5 | 3387 |
/** |
16 | 3388 |
* Fires before comments are restored for a post from the Trash. |
5 | 3389 |
* |
3390 |
* @since 2.9.0 |
|
3391 |
* |
|
3392 |
* @param int $post_id Post ID. |
|
3393 |
*/ |
|
3394 |
do_action( 'untrash_post_comments', $post_id ); |
|
3395 |
||
3396 |
// Restore each comment to its original status. |
|
0 | 3397 |
$group_by_status = array(); |
9 | 3398 |
foreach ( $statuses as $comment_id => $comment_status ) { |
3399 |
$group_by_status[ $comment_status ][] = $comment_id; |
|
3400 |
} |
|
0 | 3401 |
|
3402 |
foreach ( $group_by_status as $status => $comments ) { |
|
3403 |
// Sanity check. This shouldn't happen. |
|
16 | 3404 |
if ( 'post-trashed' === $status ) { |
0 | 3405 |
$status = '0'; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3406 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3407 |
$comments_in = implode( ', ', array_map( 'intval', $comments ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3408 |
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); |
0 | 3409 |
} |
3410 |
||
9 | 3411 |
clean_comment_cache( array_keys( $statuses ) ); |
3412 |
||
3413 |
delete_post_meta( $post_id, '_wp_trash_meta_comments_status' ); |
|
0 | 3414 |
|
5 | 3415 |
/** |
16 | 3416 |
* Fires after comments are restored for a post from the Trash. |
5 | 3417 |
* |
3418 |
* @since 2.9.0 |
|
3419 |
* |
|
3420 |
* @param int $post_id Post ID. |
|
3421 |
*/ |
|
3422 |
do_action( 'untrashed_post_comments', $post_id ); |
|
0 | 3423 |
} |
3424 |
||
3425 |
/** |
|
3426 |
* Retrieve the list of categories for a post. |
|
3427 |
* |
|
3428 |
* Compatibility layer for themes and plugins. Also an easy layer of abstraction |
|
3429 |
* away from the complexity of the taxonomy layer. |
|
3430 |
* |
|
3431 |
* @since 2.1.0 |
|
3432 |
* |
|
5 | 3433 |
* @see wp_get_object_terms() |
3434 |
* |
|
3435 |
* @param int $post_id Optional. The Post ID. Does not default to the ID of the |
|
3436 |
* global $post. Default 0. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3437 |
* @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
|
3438 |
* See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3439 |
* @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
|
3440 |
* 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` |
16 | 3441 |
* is 'ids', an array of category IDs. If `$fields` is 'names', an array of category names. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3442 |
* WP_Error object if 'category' taxonomy doesn't exist. |
0 | 3443 |
*/ |
3444 |
function wp_get_post_categories( $post_id = 0, $args = array() ) { |
|
3445 |
$post_id = (int) $post_id; |
|
3446 |
||
9 | 3447 |
$defaults = array( 'fields' => 'ids' ); |
3448 |
$args = wp_parse_args( $args, $defaults ); |
|
3449 |
||
3450 |
$cats = wp_get_object_terms( $post_id, 'category', $args ); |
|
0 | 3451 |
return $cats; |
3452 |
} |
|
3453 |
||
3454 |
/** |
|
3455 |
* Retrieve the tags for a post. |
|
3456 |
* |
|
3457 |
* There is only one default for this function, called 'fields' and by default |
|
3458 |
* 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
|
3459 |
* wp_get_object_terms(). |
0 | 3460 |
* |
3461 |
* @since 2.3.0 |
|
3462 |
* |
|
5 | 3463 |
* @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
|
3464 |
* global $post. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3465 |
* @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
|
3466 |
* See WP_Term_Query::__construct() for supported arguments. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3467 |
* @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
|
3468 |
* WP_Error object if 'post_tag' taxonomy doesn't exist. |
0 | 3469 |
*/ |
3470 |
function wp_get_post_tags( $post_id = 0, $args = array() ) { |
|
9 | 3471 |
return wp_get_post_terms( $post_id, 'post_tag', $args ); |
0 | 3472 |
} |
3473 |
||
3474 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3475 |
* Retrieves the terms for a post. |
0 | 3476 |
* |
3477 |
* @since 2.8.0 |
|
3478 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3479 |
* @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
|
3480 |
* global $post. Default 0. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3481 |
* @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
|
3482 |
* to retrieve terms. Default 'post_tag'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3483 |
* @param array $args { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3484 |
* 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
|
3485 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3486 |
* @type string $fields Term fields to retrieve. Default 'all'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3487 |
* } |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3488 |
* @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
|
3489 |
* WP_Error object if `$taxonomy` doesn't exist. |
0 | 3490 |
*/ |
3491 |
function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { |
|
3492 |
$post_id = (int) $post_id; |
|
3493 |
||
9 | 3494 |
$defaults = array( 'fields' => 'all' ); |
3495 |
$args = wp_parse_args( $args, $defaults ); |
|
3496 |
||
3497 |
$tags = wp_get_object_terms( $post_id, $taxonomy, $args ); |
|
0 | 3498 |
|
3499 |
return $tags; |
|
3500 |
} |
|
3501 |
||
3502 |
/** |
|
5 | 3503 |
* Retrieve a number of recent posts. |
0 | 3504 |
* |
3505 |
* @since 1.0.0 |
|
5 | 3506 |
* |
3507 |
* @see get_posts() |
|
3508 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3509 |
* @param array $args Optional. Arguments to retrieve posts. Default empty array. |
16 | 3510 |
* @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which |
3511 |
* correspond to a WP_Post object or an associative array, respectively. |
|
3512 |
* Default ARRAY_A. |
|
3513 |
* @return array|false Array of recent posts, where the type of each element is determined |
|
3514 |
* by the `$output` parameter. Empty array on failure. |
|
0 | 3515 |
*/ |
3516 |
function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { |
|
3517 |
||
3518 |
if ( is_numeric( $args ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3519 |
_deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); |
0 | 3520 |
$args = array( 'numberposts' => absint( $args ) ); |
3521 |
} |
|
3522 |
||
5 | 3523 |
// Set default arguments. |
0 | 3524 |
$defaults = array( |
9 | 3525 |
'numberposts' => 10, |
3526 |
'offset' => 0, |
|
3527 |
'category' => 0, |
|
3528 |
'orderby' => 'post_date', |
|
3529 |
'order' => 'DESC', |
|
3530 |
'include' => '', |
|
3531 |
'exclude' => '', |
|
3532 |
'meta_key' => '', |
|
3533 |
'meta_value' => '', |
|
3534 |
'post_type' => 'post', |
|
3535 |
'post_status' => 'draft, publish, future, pending, private', |
|
3536 |
'suppress_filters' => true, |
|
0 | 3537 |
); |
3538 |
||
16 | 3539 |
$parsed_args = wp_parse_args( $args, $defaults ); |
3540 |
||
3541 |
$results = get_posts( $parsed_args ); |
|
0 | 3542 |
|
5 | 3543 |
// Backward compatibility. Prior to 3.1 expected posts to be returned in array. |
9 | 3544 |
if ( ARRAY_A == $output ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3545 |
foreach ( $results as $key => $result ) { |
9 | 3546 |
$results[ $key ] = get_object_vars( $result ); |
0 | 3547 |
} |
3548 |
return $results ? $results : array(); |
|
3549 |
} |
|
3550 |
||
3551 |
return $results ? $results : false; |
|
3552 |
||
3553 |
} |
|
3554 |
||
3555 |
/** |
|
3556 |
* Insert or update a post. |
|
3557 |
* |
|
3558 |
* If the $postarr parameter has 'ID' set to a value, then post will be updated. |
|
3559 |
* |
|
3560 |
* You can set the post date manually, by setting the values for 'post_date' |
|
3561 |
* and 'post_date_gmt' keys. You can close the comments or open the comments by |
|
3562 |
* setting the value for 'comment_status' key. |
|
3563 |
* |
|
3564 |
* @since 1.0.0 |
|
5 | 3565 |
* @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
|
3566 |
* @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. |
5 | 3567 |
* |
3568 |
* @see sanitize_post() |
|
3569 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 3570 |
* |
3571 |
* @param array $postarr { |
|
3572 |
* An array of elements that make up a post to update or insert. |
|
3573 |
* |
|
5 | 3574 |
* @type int $ID The post ID. If equal to something other than 0, |
3575 |
* the post with that ID will be updated. Default 0. |
|
3576 |
* @type int $post_author The ID of the user who added the post. Default is |
|
3577 |
* the current user ID. |
|
3578 |
* @type string $post_date The date of the post. Default is the current time. |
|
3579 |
* @type string $post_date_gmt The date of the post in the GMT timezone. Default is |
|
3580 |
* the value of `$post_date`. |
|
3581 |
* @type mixed $post_content The post content. Default empty. |
|
3582 |
* @type string $post_content_filtered The filtered post content. Default empty. |
|
3583 |
* @type string $post_title The post title. Default empty. |
|
3584 |
* @type string $post_excerpt The post excerpt. Default empty. |
|
3585 |
* @type string $post_status The post status. Default 'draft'. |
|
3586 |
* @type string $post_type The post type. Default 'post'. |
|
3587 |
* @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. |
|
3588 |
* Default is the value of 'default_comment_status' option. |
|
3589 |
* @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. |
|
3590 |
* Default is the value of 'default_ping_status' option. |
|
3591 |
* @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
|
3592 |
* @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
|
3593 |
* when creating a new post. |
5 | 3594 |
* @type string $to_ping Space or carriage return-separated list of URLs to ping. |
3595 |
* Default empty. |
|
3596 |
* @type string $pinged Space or carriage return-separated list of URLs that have |
|
3597 |
* been pinged. Default empty. |
|
3598 |
* @type string $post_modified The date when the post was last modified. Default is |
|
3599 |
* the current time. |
|
3600 |
* @type string $post_modified_gmt The date when the post was last modified in the GMT |
|
3601 |
* timezone. Default is the current time. |
|
3602 |
* @type int $post_parent Set this for the post it belongs to, if any. Default 0. |
|
3603 |
* @type int $menu_order The order the post should be displayed in. Default 0. |
|
3604 |
* @type string $post_mime_type The mime type of the post. Default empty. |
|
3605 |
* @type string $guid Global Unique ID for referencing the post. Default empty. |
|
9 | 3606 |
* @type array $post_category Array of category IDs. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3607 |
* Defaults to value of the 'default_category' option. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3608 |
* @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
|
3609 |
* @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
|
3610 |
* @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. |
0 | 3611 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3612 |
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
0 | 3613 |
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
3614 |
*/ |
|
3615 |
function wp_insert_post( $postarr, $wp_error = false ) { |
|
3616 |
global $wpdb; |
|
3617 |
||
16 | 3618 |
// Capture original pre-sanitized array for passing into filters. |
3619 |
$unsanitized_postarr = $postarr; |
|
3620 |
||
0 | 3621 |
$user_id = get_current_user_id(); |
3622 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3623 |
$defaults = array( |
9 | 3624 |
'post_author' => $user_id, |
3625 |
'post_content' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3626 |
'post_content_filtered' => '', |
9 | 3627 |
'post_title' => '', |
3628 |
'post_excerpt' => '', |
|
3629 |
'post_status' => 'draft', |
|
3630 |
'post_type' => 'post', |
|
3631 |
'comment_status' => '', |
|
3632 |
'ping_status' => '', |
|
3633 |
'post_password' => '', |
|
3634 |
'to_ping' => '', |
|
3635 |
'pinged' => '', |
|
3636 |
'post_parent' => 0, |
|
3637 |
'menu_order' => 0, |
|
3638 |
'guid' => '', |
|
3639 |
'import_id' => 0, |
|
3640 |
'context' => '', |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3641 |
); |
0 | 3642 |
|
9 | 3643 |
$postarr = wp_parse_args( $postarr, $defaults ); |
3644 |
||
3645 |
unset( $postarr['filter'] ); |
|
3646 |
||
3647 |
$postarr = sanitize_post( $postarr, 'db' ); |
|
0 | 3648 |
|
3649 |
// Are we updating or creating? |
|
3650 |
$post_ID = 0; |
|
9 | 3651 |
$update = false; |
3652 |
$guid = $postarr['guid']; |
|
5 | 3653 |
|
3654 |
if ( ! empty( $postarr['ID'] ) ) { |
|
0 | 3655 |
$update = true; |
3656 |
||
5 | 3657 |
// Get the post ID and GUID. |
9 | 3658 |
$post_ID = $postarr['ID']; |
0 | 3659 |
$post_before = get_post( $post_ID ); |
16 | 3660 |
|
0 | 3661 |
if ( is_null( $post_before ) ) { |
5 | 3662 |
if ( $wp_error ) { |
0 | 3663 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
5 | 3664 |
} |
0 | 3665 |
return 0; |
3666 |
} |
|
3667 |
||
9 | 3668 |
$guid = get_post_field( 'guid', $post_ID ); |
3669 |
$previous_status = get_post_field( 'post_status', $post_ID ); |
|
0 | 3670 |
} else { |
3671 |
$previous_status = 'new'; |
|
3672 |
} |
|
3673 |
||
5 | 3674 |
$post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; |
3675 |
||
9 | 3676 |
$post_title = $postarr['post_title']; |
5 | 3677 |
$post_content = $postarr['post_content']; |
3678 |
$post_excerpt = $postarr['post_excerpt']; |
|
16 | 3679 |
|
5 | 3680 |
if ( isset( $postarr['post_name'] ) ) { |
3681 |
$post_name = $postarr['post_name']; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3682 |
} elseif ( $update ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3683 |
// 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
|
3684 |
$post_name = $post_before->post_name; |
0 | 3685 |
} |
3686 |
||
5 | 3687 |
$maybe_empty = 'attachment' !== $post_type |
3688 |
&& ! $post_content && ! $post_title && ! $post_excerpt |
|
3689 |
&& post_type_supports( $post_type, 'editor' ) |
|
3690 |
&& post_type_supports( $post_type, 'title' ) |
|
3691 |
&& post_type_supports( $post_type, 'excerpt' ); |
|
3692 |
||
3693 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3694 |
* Filters whether the post should be considered "empty". |
5 | 3695 |
* |
3696 |
* The post is considered "empty" if both: |
|
3697 |
* 1. The post type supports the title, editor, and excerpt fields |
|
3698 |
* 2. The title, editor, and excerpt fields are all empty |
|
3699 |
* |
|
16 | 3700 |
* Returning a truthy value from the filter will effectively short-circuit |
3701 |
* the new post being inserted and return 0. If $wp_error is true, a WP_Error |
|
5 | 3702 |
* will be returned instead. |
3703 |
* |
|
3704 |
* @since 3.3.0 |
|
3705 |
* |
|
3706 |
* @param bool $maybe_empty Whether the post should be considered "empty". |
|
3707 |
* @param array $postarr Array of post data. |
|
3708 |
*/ |
|
3709 |
if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { |
|
3710 |
if ( $wp_error ) { |
|
3711 |
return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); |
|
3712 |
} else { |
|
3713 |
return 0; |
|
3714 |
} |
|
3715 |
} |
|
3716 |
||
3717 |
$post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; |
|
16 | 3718 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3719 |
if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) { |
5 | 3720 |
$post_status = 'inherit'; |
3721 |
} |
|
3722 |
||
3723 |
if ( ! empty( $postarr['post_category'] ) ) { |
|
3724 |
// Filter out empty terms. |
|
3725 |
$post_category = array_filter( $postarr['post_category'] ); |
|
3726 |
} |
|
0 | 3727 |
|
3728 |
// Make sure we set a valid category. |
|
16 | 3729 |
if ( empty( $post_category ) || 0 === count( $post_category ) || ! is_array( $post_category ) ) { |
0 | 3730 |
// 'post' requires at least one category. |
16 | 3731 |
if ( 'post' === $post_type && 'auto-draft' !== $post_status ) { |
9 | 3732 |
$post_category = array( get_option( 'default_category' ) ); |
5 | 3733 |
} else { |
0 | 3734 |
$post_category = array(); |
5 | 3735 |
} |
0 | 3736 |
} |
3737 |
||
9 | 3738 |
/* |
3739 |
* Don't allow contributors to set the post slug for pending review posts. |
|
3740 |
* |
|
3741 |
* For new posts check the primitive capability, for updates check the meta capability. |
|
3742 |
*/ |
|
3743 |
$post_type_object = get_post_type_object( $post_type ); |
|
3744 |
||
3745 |
if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
|
3746 |
$post_name = ''; |
|
3747 |
} elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) { |
|
0 | 3748 |
$post_name = ''; |
5 | 3749 |
} |
3750 |
||
3751 |
/* |
|
3752 |
* Create a valid post name. Drafts and pending posts are allowed to have |
|
3753 |
* an empty post name. |
|
3754 |
*/ |
|
9 | 3755 |
if ( empty( $post_name ) ) { |
16 | 3756 |
if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) ) { |
9 | 3757 |
$post_name = sanitize_title( $post_title ); |
5 | 3758 |
} else { |
0 | 3759 |
$post_name = ''; |
5 | 3760 |
} |
0 | 3761 |
} else { |
3762 |
// On updates, we need to check to see if it's using the old, fixed sanitization context. |
|
3763 |
$check_name = sanitize_title( $post_name, '', 'old-save' ); |
|
16 | 3764 |
|
5 | 3765 |
if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { |
0 | 3766 |
$post_name = $check_name; |
5 | 3767 |
} else { // new post, or slug has changed. |
9 | 3768 |
$post_name = sanitize_title( $post_name ); |
5 | 3769 |
} |
3770 |
} |
|
3771 |
||
3772 |
/* |
|
3773 |
* If the post date is empty (due to having been new or a draft) and status |
|
3774 |
* is not 'draft' or 'pending', set date to now. |
|
3775 |
*/ |
|
16 | 3776 |
if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' === $postarr['post_date'] ) { |
3777 |
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3778 |
$post_date = current_time( 'mysql' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3779 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3780 |
$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
|
3781 |
} |
5 | 3782 |
} else { |
3783 |
$post_date = $postarr['post_date']; |
|
0 | 3784 |
} |
3785 |
||
5 | 3786 |
// Validate the date. |
9 | 3787 |
$mm = substr( $post_date, 5, 2 ); |
3788 |
$jj = substr( $post_date, 8, 2 ); |
|
3789 |
$aa = substr( $post_date, 0, 4 ); |
|
5 | 3790 |
$valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); |
3791 |
if ( ! $valid_date ) { |
|
3792 |
if ( $wp_error ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3793 |
return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
5 | 3794 |
} else { |
3795 |
return 0; |
|
0 | 3796 |
} |
5 | 3797 |
} |
3798 |
||
16 | 3799 |
if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' === $postarr['post_date_gmt'] ) { |
3800 |
if ( ! in_array( $post_status, get_post_stati( array( 'date_floating' => true ) ), true ) ) { |
|
5 | 3801 |
$post_date_gmt = get_gmt_from_date( $post_date ); |
3802 |
} else { |
|
0 | 3803 |
$post_date_gmt = '0000-00-00 00:00:00'; |
5 | 3804 |
} |
3805 |
} else { |
|
3806 |
$post_date_gmt = $postarr['post_date_gmt']; |
|
0 | 3807 |
} |
3808 |
||
16 | 3809 |
if ( $update || '0000-00-00 00:00:00' === $post_date ) { |
0 | 3810 |
$post_modified = current_time( 'mysql' ); |
3811 |
$post_modified_gmt = current_time( 'mysql', 1 ); |
|
3812 |
} else { |
|
3813 |
$post_modified = $post_date; |
|
3814 |
$post_modified_gmt = $post_date_gmt; |
|
3815 |
} |
|
3816 |
||
5 | 3817 |
if ( 'attachment' !== $post_type ) { |
16 | 3818 |
$now = gmdate( 'Y-m-d H:i:s' ); |
3819 |
||
3820 |
if ( 'publish' === $post_status ) { |
|
3821 |
if ( strtotime( $post_date_gmt ) - strtotime( $now ) >= MINUTE_IN_SECONDS ) { |
|
5 | 3822 |
$post_status = 'future'; |
3823 |
} |
|
16 | 3824 |
} elseif ( 'future' === $post_status ) { |
3825 |
if ( strtotime( $post_date_gmt ) - strtotime( $now ) < MINUTE_IN_SECONDS ) { |
|
5 | 3826 |
$post_status = 'publish'; |
3827 |
} |
|
3828 |
} |
|
3829 |
} |
|
3830 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3831 |
// Comment status. |
5 | 3832 |
if ( empty( $postarr['comment_status'] ) ) { |
3833 |
if ( $update ) { |
|
3834 |
$comment_status = 'closed'; |
|
3835 |
} else { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3836 |
$comment_status = get_default_comment_status( $post_type ); |
5 | 3837 |
} |
3838 |
} else { |
|
3839 |
$comment_status = $postarr['comment_status']; |
|
0 | 3840 |
} |
3841 |
||
5 | 3842 |
// These variables are needed by compact() later. |
3843 |
$post_content_filtered = $postarr['post_content_filtered']; |
|
9 | 3844 |
$post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; |
3845 |
$ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; |
|
3846 |
$to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; |
|
3847 |
$pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; |
|
3848 |
$import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; |
|
5 | 3849 |
|
3850 |
/* |
|
3851 |
* The 'wp_insert_post_parent' filter expects all variables to be present. |
|
3852 |
* Previously, these variables would have already been extracted |
|
3853 |
*/ |
|
3854 |
if ( isset( $postarr['menu_order'] ) ) { |
|
3855 |
$menu_order = (int) $postarr['menu_order']; |
|
3856 |
} else { |
|
3857 |
$menu_order = 0; |
|
3858 |
} |
|
3859 |
||
3860 |
$post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; |
|
16 | 3861 |
if ( 'private' === $post_status ) { |
5 | 3862 |
$post_password = ''; |
3863 |
} |
|
3864 |
||
3865 |
if ( isset( $postarr['post_parent'] ) ) { |
|
3866 |
$post_parent = (int) $postarr['post_parent']; |
|
3867 |
} else { |
|
3868 |
$post_parent = 0; |
|
0 | 3869 |
} |
5 | 3870 |
|
9 | 3871 |
$new_postarr = array_merge( |
3872 |
array( |
|
3873 |
'ID' => $post_ID, |
|
3874 |
), |
|
3875 |
compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) |
|
3876 |
); |
|
3877 |
||
5 | 3878 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3879 |
* Filters the post parent -- used to check for and prevent hierarchy loops. |
5 | 3880 |
* |
3881 |
* @since 3.1.0 |
|
3882 |
* |
|
3883 |
* @param int $post_parent Post parent ID. |
|
3884 |
* @param int $post_ID Post ID. |
|
3885 |
* @param array $new_postarr Array of parsed post data. |
|
3886 |
* @param array $postarr Array of sanitized, but otherwise unmodified post data. |
|
3887 |
*/ |
|
9 | 3888 |
$post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr ); |
0 | 3889 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3890 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3891 |
* 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
|
3892 |
* reassign it. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3893 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3894 |
if ( 'trash' === $previous_status && 'trash' !== $post_status ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3895 |
$desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); |
16 | 3896 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3897 |
if ( $desired_post_slug ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3898 |
delete_post_meta( $post_ID, '_wp_desired_post_slug' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3899 |
$post_name = $desired_post_slug; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3900 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3901 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3902 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3903 |
// 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
|
3904 |
if ( 'trash' !== $post_status && $post_name ) { |
16 | 3905 |
/** |
3906 |
* Filters whether or not to add a `__trashed` suffix to trashed posts that match the name of the updated post. |
|
3907 |
* |
|
3908 |
* @since 5.4.0 |
|
3909 |
* |
|
3910 |
* @param bool $add_trashed_suffix Whether to attempt to add the suffix. |
|
3911 |
* @param string $post_name The name of the post being updated. |
|
3912 |
* @param int $post_ID Post ID. |
|
3913 |
*/ |
|
3914 |
$add_trashed_suffix = apply_filters( 'add_trashed_suffix_to_trashed_posts', true, $post_name, $post_ID ); |
|
3915 |
||
3916 |
if ( $add_trashed_suffix ) { |
|
3917 |
wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); |
|
3918 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3919 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3920 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3921 |
// 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
|
3922 |
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
|
3923 |
$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
|
3924 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3925 |
|
5 | 3926 |
$post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); |
3927 |
||
3928 |
// Don't unslash. |
|
3929 |
$post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; |
|
3930 |
||
3931 |
// Expected_slashed (everything!). |
|
3932 |
$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' ); |
|
3933 |
||
3934 |
$emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
|
3935 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3936 |
foreach ( $emoji_fields as $emoji_field ) { |
5 | 3937 |
if ( isset( $data[ $emoji_field ] ) ) { |
3938 |
$charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); |
|
16 | 3939 |
|
5 | 3940 |
if ( 'utf8' === $charset ) { |
3941 |
$data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); |
|
3942 |
} |
|
3943 |
} |
|
3944 |
} |
|
3945 |
||
3946 |
if ( 'attachment' === $post_type ) { |
|
3947 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3948 |
* Filters attachment post data before it is updated in or added to the database. |
5 | 3949 |
* |
3950 |
* @since 3.9.0 |
|
16 | 3951 |
* @since 5.4.1 `$unsanitized_postarr` argument added. |
5 | 3952 |
* |
16 | 3953 |
* @param array $data An array of slashed, sanitized, and processed attachment post data. |
3954 |
* @param array $postarr An array of slashed and sanitized attachment post data, but not processed. |
|
3955 |
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed attachment post data |
|
3956 |
* as originally passed to wp_insert_post(). |
|
5 | 3957 |
*/ |
16 | 3958 |
$data = apply_filters( 'wp_insert_attachment_data', $data, $postarr, $unsanitized_postarr ); |
5 | 3959 |
} else { |
3960 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
3961 |
* Filters slashed post data just before it is inserted into the database. |
5 | 3962 |
* |
3963 |
* @since 2.7.0 |
|
16 | 3964 |
* @since 5.4.1 `$unsanitized_postarr` argument added. |
5 | 3965 |
* |
16 | 3966 |
* @param array $data An array of slashed, sanitized, and processed post data. |
3967 |
* @param array $postarr An array of sanitized (and slashed) but otherwise unmodified post data. |
|
3968 |
* @param array $unsanitized_postarr An array of slashed yet *unsanitized* and unprocessed post data as |
|
3969 |
* originally passed to wp_insert_post(). |
|
5 | 3970 |
*/ |
16 | 3971 |
$data = apply_filters( 'wp_insert_post_data', $data, $postarr, $unsanitized_postarr ); |
3972 |
} |
|
3973 |
||
9 | 3974 |
$data = wp_unslash( $data ); |
0 | 3975 |
$where = array( 'ID' => $post_ID ); |
3976 |
||
3977 |
if ( $update ) { |
|
5 | 3978 |
/** |
3979 |
* Fires immediately before an existing post is updated in the database. |
|
3980 |
* |
|
3981 |
* @since 2.5.0 |
|
3982 |
* |
|
3983 |
* @param int $post_ID Post ID. |
|
3984 |
* @param array $data Array of unslashed post data. |
|
3985 |
*/ |
|
0 | 3986 |
do_action( 'pre_post_update', $post_ID, $data ); |
16 | 3987 |
|
0 | 3988 |
if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
5 | 3989 |
if ( $wp_error ) { |
16 | 3990 |
if ( 'attachment' === $post_type ) { |
3991 |
$message = __( 'Could not update attachment in the database.' ); |
|
3992 |
} else { |
|
3993 |
$message = __( 'Could not update post in the database.' ); |
|
3994 |
} |
|
3995 |
||
3996 |
return new WP_Error( 'db_update_error', $message, $wpdb->last_error ); |
|
5 | 3997 |
} else { |
0 | 3998 |
return 0; |
5 | 3999 |
} |
0 | 4000 |
} |
4001 |
} else { |
|
5 | 4002 |
// If there is a suggested ID, use it if not already present. |
4003 |
if ( ! empty( $import_id ) ) { |
|
0 | 4004 |
$import_id = (int) $import_id; |
16 | 4005 |
|
9 | 4006 |
if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { |
0 | 4007 |
$data['ID'] = $import_id; |
4008 |
} |
|
4009 |
} |
|
16 | 4010 |
|
0 | 4011 |
if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { |
5 | 4012 |
if ( $wp_error ) { |
16 | 4013 |
if ( 'attachment' === $post_type ) { |
4014 |
$message = __( 'Could not insert attachment into the database.' ); |
|
4015 |
} else { |
|
4016 |
$message = __( 'Could not insert post into the database.' ); |
|
4017 |
} |
|
4018 |
||
4019 |
return new WP_Error( 'db_insert_error', $message, $wpdb->last_error ); |
|
5 | 4020 |
} else { |
0 | 4021 |
return 0; |
5 | 4022 |
} |
0 | 4023 |
} |
16 | 4024 |
|
0 | 4025 |
$post_ID = (int) $wpdb->insert_id; |
4026 |
||
5 | 4027 |
// Use the newly generated $post_ID. |
0 | 4028 |
$where = array( 'ID' => $post_ID ); |
4029 |
} |
|
4030 |
||
16 | 4031 |
if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4032 |
$data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); |
16 | 4033 |
|
0 | 4034 |
$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
|
4035 |
clean_post_cache( $post_ID ); |
0 | 4036 |
} |
4037 |
||
5 | 4038 |
if ( is_object_in_taxonomy( $post_type, 'category' ) ) { |
0 | 4039 |
wp_set_post_categories( $post_ID, $post_category ); |
5 | 4040 |
} |
4041 |
||
4042 |
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { |
|
4043 |
wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
|
4044 |
} |
|
4045 |
||
16 | 4046 |
// Add default term for all associated custom taxonomies. |
4047 |
if ( 'auto-draft' !== $post_status ) { |
|
4048 |
foreach ( get_object_taxonomies( $post_type, 'object' ) as $taxonomy => $tax_object ) { |
|
4049 |
||
4050 |
if ( ! empty( $tax_object->default_term ) ) { |
|
4051 |
||
4052 |
// Filter out empty terms. |
|
4053 |
if ( isset( $postarr['tax_input'] ) && is_array( $postarr['tax_input'][ $taxonomy ] ) ) { |
|
4054 |
$postarr['tax_input'][ $taxonomy ] = array_filter( $postarr['tax_input'][ $taxonomy ] ); |
|
4055 |
} |
|
4056 |
||
4057 |
// Passed custom taxonomy list overwrites the existing list if not empty. |
|
4058 |
$terms = wp_get_object_terms( $post_ID, $taxonomy, array( 'fields' => 'ids' ) ); |
|
4059 |
if ( ! empty( $terms ) && empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
|
4060 |
$postarr['tax_input'][ $taxonomy ] = $terms; |
|
4061 |
} |
|
4062 |
||
4063 |
if ( empty( $postarr['tax_input'][ $taxonomy ] ) ) { |
|
4064 |
$default_term_id = get_option( 'default_term_' . $taxonomy ); |
|
4065 |
if ( ! empty( $default_term_id ) ) { |
|
4066 |
$postarr['tax_input'][ $taxonomy ] = array( (int) $default_term_id ); |
|
4067 |
} |
|
4068 |
} |
|
4069 |
} |
|
4070 |
} |
|
4071 |
} |
|
4072 |
||
5 | 4073 |
// New-style support for all custom taxonomies. |
4074 |
if ( ! empty( $postarr['tax_input'] ) ) { |
|
4075 |
foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { |
|
9 | 4076 |
$taxonomy_obj = get_taxonomy( $taxonomy ); |
16 | 4077 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4078 |
if ( ! $taxonomy_obj ) { |
16 | 4079 |
/* translators: %s: Taxonomy name. */ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4080 |
_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
|
4081 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4082 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4083 |
|
5 | 4084 |
// array = hierarchical, string = non-hierarchical. |
4085 |
if ( is_array( $tags ) ) { |
|
9 | 4086 |
$tags = array_filter( $tags ); |
5 | 4087 |
} |
16 | 4088 |
|
5 | 4089 |
if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
0 | 4090 |
wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
5 | 4091 |
} |
0 | 4092 |
} |
4093 |
} |
|
4094 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4095 |
if ( ! empty( $postarr['meta_input'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4096 |
foreach ( $postarr['meta_input'] as $field => $value ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4097 |
update_post_meta( $post_ID, $field, $value ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4098 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4099 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4100 |
|
0 | 4101 |
$current_guid = get_post_field( 'guid', $post_ID ); |
4102 |
||
5 | 4103 |
// Set GUID. |
16 | 4104 |
if ( ! $update && '' === $current_guid ) { |
0 | 4105 |
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
5 | 4106 |
} |
4107 |
||
4108 |
if ( 'attachment' === $postarr['post_type'] ) { |
|
4109 |
if ( ! empty( $postarr['file'] ) ) { |
|
4110 |
update_attached_file( $post_ID, $postarr['file'] ); |
|
4111 |
} |
|
4112 |
||
4113 |
if ( ! empty( $postarr['context'] ) ) { |
|
4114 |
add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); |
|
4115 |
} |
|
4116 |
} |
|
0 | 4117 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4118 |
// Set or remove featured image. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4119 |
if ( isset( $postarr['_thumbnail_id'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4120 |
$thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; |
16 | 4121 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4122 |
if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4123 |
if ( wp_attachment_is( 'audio', $post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4124 |
$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
|
4125 |
} elseif ( wp_attachment_is( 'video', $post_ID ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4126 |
$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
|
4127 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4128 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4129 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4130 |
if ( $thumbnail_support ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4131 |
$thumbnail_id = intval( $postarr['_thumbnail_id'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4132 |
if ( -1 === $thumbnail_id ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4133 |
delete_post_thumbnail( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4134 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4135 |
set_post_thumbnail( $post_ID, $thumbnail_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4136 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4137 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4138 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4139 |
|
0 | 4140 |
clean_post_cache( $post_ID ); |
4141 |
||
5 | 4142 |
$post = get_post( $post_ID ); |
4143 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4144 |
if ( ! empty( $postarr['page_template'] ) ) { |
5 | 4145 |
$post->page_template = $postarr['page_template']; |
9 | 4146 |
$page_templates = wp_get_theme()->get_page_templates( $post ); |
16 | 4147 |
|
4148 |
if ( 'default' !== $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { |
|
5 | 4149 |
if ( $wp_error ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4150 |
return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); |
5 | 4151 |
} |
16 | 4152 |
|
5 | 4153 |
update_post_meta( $post_ID, '_wp_page_template', 'default' ); |
4154 |
} else { |
|
4155 |
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); |
|
0 | 4156 |
} |
4157 |
} |
|
4158 |
||
5 | 4159 |
if ( 'attachment' !== $postarr['post_type'] ) { |
4160 |
wp_transition_post_status( $data['post_status'], $previous_status, $post ); |
|
4161 |
} else { |
|
4162 |
if ( $update ) { |
|
4163 |
/** |
|
4164 |
* Fires once an existing attachment has been updated. |
|
4165 |
* |
|
4166 |
* @since 2.0.0 |
|
4167 |
* |
|
4168 |
* @param int $post_ID Attachment ID. |
|
4169 |
*/ |
|
4170 |
do_action( 'edit_attachment', $post_ID ); |
|
16 | 4171 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4172 |
$post_after = get_post( $post_ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4173 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4174 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4175 |
* Fires once an existing attachment has been updated. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4176 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4177 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4178 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4179 |
* @param int $post_ID Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4180 |
* @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
|
4181 |
* @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
|
4182 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4183 |
do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); |
5 | 4184 |
} else { |
4185 |
||
4186 |
/** |
|
4187 |
* Fires once an attachment has been added. |
|
4188 |
* |
|
4189 |
* @since 2.0.0 |
|
4190 |
* |
|
4191 |
* @param int $post_ID Attachment ID. |
|
4192 |
*/ |
|
4193 |
do_action( 'add_attachment', $post_ID ); |
|
4194 |
} |
|
4195 |
||
4196 |
return $post_ID; |
|
4197 |
} |
|
0 | 4198 |
|
4199 |
if ( $update ) { |
|
5 | 4200 |
/** |
4201 |
* Fires once an existing post has been updated. |
|
4202 |
* |
|
9 | 4203 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
4204 |
* the post type slug. |
|
4205 |
* |
|
4206 |
* @since 5.1.0 |
|
4207 |
* |
|
4208 |
* @param int $post_ID Post ID. |
|
4209 |
* @param WP_Post $post Post object. |
|
4210 |
*/ |
|
4211 |
do_action( "edit_post_{$post->post_type}", $post_ID, $post ); |
|
4212 |
||
4213 |
/** |
|
4214 |
* Fires once an existing post has been updated. |
|
4215 |
* |
|
5 | 4216 |
* @since 1.2.0 |
4217 |
* |
|
4218 |
* @param int $post_ID Post ID. |
|
4219 |
* @param WP_Post $post Post object. |
|
4220 |
*/ |
|
4221 |
do_action( 'edit_post', $post_ID, $post ); |
|
9 | 4222 |
|
4223 |
$post_after = get_post( $post_ID ); |
|
5 | 4224 |
|
4225 |
/** |
|
4226 |
* Fires once an existing post has been updated. |
|
4227 |
* |
|
4228 |
* @since 3.0.0 |
|
4229 |
* |
|
4230 |
* @param int $post_ID Post ID. |
|
4231 |
* @param WP_Post $post_after Post object following the update. |
|
4232 |
* @param WP_Post $post_before Post object before the update. |
|
4233 |
*/ |
|
9 | 4234 |
do_action( 'post_updated', $post_ID, $post_after, $post_before ); |
0 | 4235 |
} |
4236 |
||
5 | 4237 |
/** |
4238 |
* Fires once a post has been saved. |
|
4239 |
* |
|
4240 |
* The dynamic portion of the hook name, `$post->post_type`, refers to |
|
4241 |
* the post type slug. |
|
4242 |
* |
|
4243 |
* @since 3.7.0 |
|
4244 |
* |
|
4245 |
* @param int $post_ID Post ID. |
|
4246 |
* @param WP_Post $post Post object. |
|
16 | 4247 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4248 |
*/ |
0 | 4249 |
do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); |
5 | 4250 |
|
4251 |
/** |
|
4252 |
* Fires once a post has been saved. |
|
4253 |
* |
|
4254 |
* @since 1.5.0 |
|
4255 |
* |
|
4256 |
* @param int $post_ID Post ID. |
|
4257 |
* @param WP_Post $post Post object. |
|
16 | 4258 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4259 |
*/ |
0 | 4260 |
do_action( 'save_post', $post_ID, $post, $update ); |
5 | 4261 |
|
4262 |
/** |
|
4263 |
* Fires once a post has been saved. |
|
4264 |
* |
|
4265 |
* @since 2.0.0 |
|
4266 |
* |
|
4267 |
* @param int $post_ID Post ID. |
|
4268 |
* @param WP_Post $post Post object. |
|
16 | 4269 |
* @param bool $update Whether this is an existing post being updated. |
5 | 4270 |
*/ |
0 | 4271 |
do_action( 'wp_insert_post', $post_ID, $post, $update ); |
4272 |
||
4273 |
return $post_ID; |
|
4274 |
} |
|
4275 |
||
4276 |
/** |
|
4277 |
* Update a post with new post data. |
|
4278 |
* |
|
4279 |
* The date does not have to be set for drafts. You can set the date and it will |
|
4280 |
* not be overridden. |
|
4281 |
* |
|
4282 |
* @since 1.0.0 |
|
4283 |
* |
|
5 | 4284 |
* @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, |
4285 |
* objects are not. Default array. |
|
4286 |
* @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. |
|
16 | 4287 |
* @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
0 | 4288 |
*/ |
4289 |
function wp_update_post( $postarr = array(), $wp_error = false ) { |
|
9 | 4290 |
if ( is_object( $postarr ) ) { |
5 | 4291 |
// Non-escaped post was passed. |
9 | 4292 |
$postarr = get_object_vars( $postarr ); |
4293 |
$postarr = wp_slash( $postarr ); |
|
0 | 4294 |
} |
4295 |
||
5 | 4296 |
// First, get all of the original fields. |
9 | 4297 |
$post = get_post( $postarr['ID'], ARRAY_A ); |
0 | 4298 |
|
4299 |
if ( is_null( $post ) ) { |
|
9 | 4300 |
if ( $wp_error ) { |
0 | 4301 |
return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
9 | 4302 |
} |
0 | 4303 |
return 0; |
4304 |
} |
|
4305 |
||
4306 |
// Escape data pulled from DB. |
|
9 | 4307 |
$post = wp_slash( $post ); |
0 | 4308 |
|
4309 |
// Passed post category list overwrites existing category list if not empty. |
|
9 | 4310 |
if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] ) |
16 | 4311 |
&& count( $postarr['post_category'] ) > 0 |
4312 |
) { |
|
0 | 4313 |
$post_cats = $postarr['post_category']; |
9 | 4314 |
} else { |
0 | 4315 |
$post_cats = $post['post_category']; |
9 | 4316 |
} |
0 | 4317 |
|
5 | 4318 |
// Drafts shouldn't be assigned a date unless explicitly done so by the user. |
16 | 4319 |
if ( isset( $post['post_status'] ) |
4320 |
&& in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ), true ) |
|
4321 |
&& empty( $postarr['edit_date'] ) && ( '0000-00-00 00:00:00' === $post['post_date_gmt'] ) |
|
4322 |
) { |
|
0 | 4323 |
$clear_date = true; |
9 | 4324 |
} else { |
0 | 4325 |
$clear_date = false; |
9 | 4326 |
} |
0 | 4327 |
|
4328 |
// Merge old and new fields with new fields overwriting old ones. |
|
9 | 4329 |
$postarr = array_merge( $post, $postarr ); |
0 | 4330 |
$postarr['post_category'] = $post_cats; |
4331 |
if ( $clear_date ) { |
|
9 | 4332 |
$postarr['post_date'] = current_time( 'mysql' ); |
0 | 4333 |
$postarr['post_date_gmt'] = ''; |
4334 |
} |
|
4335 |
||
16 | 4336 |
if ( 'attachment' === $postarr['post_type'] ) { |
9 | 4337 |
return wp_insert_attachment( $postarr, false, 0, $wp_error ); |
4338 |
} |
|
0 | 4339 |
|
16 | 4340 |
// Discard 'tags_input' parameter if it's the same as existing post tags. |
4341 |
if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $postarr['post_type'], 'post_tag' ) ) { |
|
4342 |
$tags = get_the_terms( $postarr['ID'], 'post_tag' ); |
|
4343 |
$tag_names = array(); |
|
4344 |
||
4345 |
if ( $tags && ! is_wp_error( $tags ) ) { |
|
4346 |
$tag_names = wp_list_pluck( $tags, 'name' ); |
|
4347 |
} |
|
4348 |
||
4349 |
if ( $postarr['tags_input'] === $tag_names ) { |
|
4350 |
unset( $postarr['tags_input'] ); |
|
4351 |
} |
|
4352 |
} |
|
4353 |
||
0 | 4354 |
return wp_insert_post( $postarr, $wp_error ); |
4355 |
} |
|
4356 |
||
4357 |
/** |
|
4358 |
* Publish a post by transitioning the post status. |
|
4359 |
* |
|
4360 |
* @since 2.1.0 |
|
5 | 4361 |
* |
4362 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4363 |
* |
|
4364 |
* @param int|WP_Post $post Post ID or post object. |
|
0 | 4365 |
*/ |
4366 |
function wp_publish_post( $post ) { |
|
4367 |
global $wpdb; |
|
4368 |
||
16 | 4369 |
$post = get_post( $post ); |
4370 |
if ( ! $post ) { |
|
0 | 4371 |
return; |
9 | 4372 |
} |
4373 |
||
16 | 4374 |
if ( 'publish' === $post->post_status ) { |
0 | 4375 |
return; |
9 | 4376 |
} |
0 | 4377 |
|
4378 |
$wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); |
|
4379 |
||
4380 |
clean_post_cache( $post->ID ); |
|
4381 |
||
9 | 4382 |
$old_status = $post->post_status; |
0 | 4383 |
$post->post_status = 'publish'; |
4384 |
wp_transition_post_status( 'publish', $old_status, $post ); |
|
4385 |
||
5 | 4386 |
/** This action is documented in wp-includes/post.php */ |
9 | 4387 |
do_action( "edit_post_{$post->post_type}", $post->ID, $post ); |
4388 |
||
4389 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4390 |
do_action( 'edit_post', $post->ID, $post ); |
5 | 4391 |
|
4392 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4393 |
do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
5 | 4394 |
|
4395 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4396 |
do_action( 'save_post', $post->ID, $post, true ); |
5 | 4397 |
|
4398 |
/** This action is documented in wp-includes/post.php */ |
|
0 | 4399 |
do_action( 'wp_insert_post', $post->ID, $post, true ); |
4400 |
} |
|
4401 |
||
4402 |
/** |
|
4403 |
* Publish future post and make sure post ID has future post status. |
|
4404 |
* |
|
4405 |
* Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
|
4406 |
* from publishing drafts, etc. |
|
4407 |
* |
|
4408 |
* @since 2.5.0 |
|
4409 |
* |
|
5 | 4410 |
* @param int|WP_Post $post_id Post ID or post object. |
0 | 4411 |
*/ |
5 | 4412 |
function check_and_publish_future_post( $post_id ) { |
9 | 4413 |
$post = get_post( $post_id ); |
4414 |
||
4415 |
if ( empty( $post ) ) { |
|
0 | 4416 |
return; |
9 | 4417 |
} |
4418 |
||
16 | 4419 |
if ( 'future' !== $post->post_status ) { |
0 | 4420 |
return; |
9 | 4421 |
} |
0 | 4422 |
|
4423 |
$time = strtotime( $post->post_date_gmt . ' GMT' ); |
|
4424 |
||
5 | 4425 |
// Uh oh, someone jumped the gun! |
4426 |
if ( $time > time() ) { |
|
16 | 4427 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // Clear anything else in the system. |
0 | 4428 |
wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); |
4429 |
return; |
|
4430 |
} |
|
4431 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4432 |
// wp_publish_post() returns no meaningful value. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4433 |
wp_publish_post( $post_id ); |
0 | 4434 |
} |
4435 |
||
4436 |
/** |
|
4437 |
* Computes a unique slug for the post, when given the desired slug and some post details. |
|
4438 |
* |
|
4439 |
* @since 2.8.0 |
|
4440 |
* |
|
16 | 4441 |
* @global wpdb $wpdb WordPress database abstraction object. |
4442 |
* @global WP_Rewrite $wp_rewrite WordPress rewrite component. |
|
5 | 4443 |
* |
4444 |
* @param string $slug The desired slug (post_name). |
|
4445 |
* @param int $post_ID Post ID. |
|
4446 |
* @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
|
4447 |
* @param string $post_type Post type. |
|
4448 |
* @param int $post_parent Post parent ID. |
|
4449 |
* @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
|
0 | 4450 |
*/ |
4451 |
function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
|
16 | 4452 |
if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ), true ) |
4453 |
|| ( 'inherit' === $post_status && 'revision' === $post_type ) || 'user_request' === $post_type |
|
4454 |
) { |
|
0 | 4455 |
return $slug; |
9 | 4456 |
} |
4457 |
||
4458 |
/** |
|
4459 |
* Filters the post slug before it is generated to be unique. |
|
4460 |
* |
|
4461 |
* Returning a non-null value will short-circuit the |
|
4462 |
* unique slug generation, returning the passed value instead. |
|
4463 |
* |
|
4464 |
* @since 5.1.0 |
|
4465 |
* |
|
16 | 4466 |
* @param string|null $override_slug Short-circuit return value. |
4467 |
* @param string $slug The desired slug (post_name). |
|
4468 |
* @param int $post_ID Post ID. |
|
4469 |
* @param string $post_status The post status. |
|
4470 |
* @param string $post_type Post type. |
|
4471 |
* @param int $post_parent Post parent ID. |
|
9 | 4472 |
*/ |
4473 |
$override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent ); |
|
4474 |
if ( null !== $override_slug ) { |
|
4475 |
return $override_slug; |
|
4476 |
} |
|
0 | 4477 |
|
4478 |
global $wpdb, $wp_rewrite; |
|
4479 |
||
4480 |
$original_slug = $slug; |
|
4481 |
||
4482 |
$feeds = $wp_rewrite->feeds; |
|
9 | 4483 |
if ( ! is_array( $feeds ) ) { |
0 | 4484 |
$feeds = array(); |
9 | 4485 |
} |
0 | 4486 |
|
16 | 4487 |
if ( 'attachment' === $post_type ) { |
0 | 4488 |
// Attachment slugs must be unique across all types. |
9 | 4489 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
0 | 4490 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); |
4491 |
||
5 | 4492 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4493 |
* Filters whether the post slug would make a bad attachment slug. |
5 | 4494 |
* |
4495 |
* @since 3.1.0 |
|
4496 |
* |
|
4497 |
* @param bool $bad_slug Whether the slug would be bad as an attachment slug. |
|
4498 |
* @param string $slug The post slug. |
|
4499 |
*/ |
|
16 | 4500 |
$is_bad_attachment_slug = apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ); |
4501 |
||
4502 |
if ( $post_name_check |
|
4503 |
|| in_array( $slug, $feeds, true ) || 'embed' === $slug |
|
4504 |
|| $is_bad_attachment_slug |
|
4505 |
) { |
|
0 | 4506 |
$suffix = 2; |
4507 |
do { |
|
9 | 4508 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
0 | 4509 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); |
4510 |
$suffix++; |
|
4511 |
} while ( $post_name_check ); |
|
4512 |
$slug = $alt_post_name; |
|
4513 |
} |
|
5 | 4514 |
} elseif ( is_post_type_hierarchical( $post_type ) ) { |
16 | 4515 |
if ( 'nav_menu_item' === $post_type ) { |
0 | 4516 |
return $slug; |
9 | 4517 |
} |
5 | 4518 |
|
4519 |
/* |
|
4520 |
* Page slugs must be unique within their own trees. Pages are in a separate |
|
4521 |
* namespace than posts so page slugs are allowed to overlap post slugs. |
|
4522 |
*/ |
|
9 | 4523 |
$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 | 4524 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); |
4525 |
||
4526 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4527 |
* Filters whether the post slug would make a bad hierarchical post slug. |
5 | 4528 |
* |
4529 |
* @since 3.1.0 |
|
4530 |
* |
|
4531 |
* @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. |
|
4532 |
* @param string $slug The post slug. |
|
4533 |
* @param string $post_type Post type. |
|
4534 |
* @param int $post_parent Post parent ID. |
|
4535 |
*/ |
|
16 | 4536 |
$is_bad_hierarchical_slug = apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ); |
4537 |
||
4538 |
if ( $post_name_check |
|
4539 |
|| in_array( $slug, $feeds, true ) || 'embed' === $slug |
|
4540 |
|| preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) |
|
4541 |
|| $is_bad_hierarchical_slug |
|
4542 |
) { |
|
0 | 4543 |
$suffix = 2; |
4544 |
do { |
|
9 | 4545 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
5 | 4546 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); |
0 | 4547 |
$suffix++; |
4548 |
} while ( $post_name_check ); |
|
4549 |
$slug = $alt_post_name; |
|
4550 |
} |
|
4551 |
} else { |
|
4552 |
// Post slugs must be unique across all posts. |
|
9 | 4553 |
$check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
0 | 4554 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
4555 |
||
16 | 4556 |
$post = get_post( $post_ID ); |
4557 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4558 |
// Prevent new post slugs that could result in URLs that conflict with date archives. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4559 |
$conflicts_with_date_archive = false; |
16 | 4560 |
if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) ) { |
4561 |
$slug_num = intval( $slug ); |
|
4562 |
||
4563 |
if ( $slug_num ) { |
|
4564 |
$permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); |
|
4565 |
$postname_index = array_search( '%postname%', $permastructs, true ); |
|
4566 |
||
4567 |
/* |
|
4568 |
* Potential date clashes are as follows: |
|
4569 |
* |
|
4570 |
* - Any integer in the first permastruct position could be a year. |
|
4571 |
* - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. |
|
4572 |
* - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. |
|
4573 |
*/ |
|
4574 |
if ( 0 === $postname_index || |
|
4575 |
( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || |
|
4576 |
( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) |
|
4577 |
) { |
|
4578 |
$conflicts_with_date_archive = true; |
|
4579 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4580 |
} |
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 |
|
5 | 4583 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4584 |
* Filters whether the post slug would be bad as a flat slug. |
5 | 4585 |
* |
4586 |
* @since 3.1.0 |
|
4587 |
* |
|
4588 |
* @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|
4589 |
* @param string $slug The post slug. |
|
4590 |
* @param string $post_type Post type. |
|
4591 |
*/ |
|
16 | 4592 |
$is_bad_flat_slug = apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ); |
4593 |
||
4594 |
if ( $post_name_check |
|
4595 |
|| in_array( $slug, $feeds, true ) || 'embed' === $slug |
|
4596 |
|| $conflicts_with_date_archive |
|
4597 |
|| $is_bad_flat_slug |
|
4598 |
) { |
|
0 | 4599 |
$suffix = 2; |
4600 |
do { |
|
9 | 4601 |
$alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
0 | 4602 |
$post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
4603 |
$suffix++; |
|
4604 |
} while ( $post_name_check ); |
|
4605 |
$slug = $alt_post_name; |
|
4606 |
} |
|
4607 |
} |
|
4608 |
||
5 | 4609 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4610 |
* Filters the unique post slug. |
5 | 4611 |
* |
4612 |
* @since 3.3.0 |
|
4613 |
* |
|
4614 |
* @param string $slug The post slug. |
|
4615 |
* @param int $post_ID Post ID. |
|
4616 |
* @param string $post_status The post status. |
|
4617 |
* @param string $post_type Post type. |
|
4618 |
* @param int $post_parent Post parent ID |
|
4619 |
* @param string $original_slug The original post slug. |
|
4620 |
*/ |
|
0 | 4621 |
return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); |
4622 |
} |
|
4623 |
||
4624 |
/** |
|
5 | 4625 |
* Truncate a post slug. |
0 | 4626 |
* |
4627 |
* @since 3.6.0 |
|
4628 |
* @access private |
|
5 | 4629 |
* |
4630 |
* @see utf8_uri_encode() |
|
4631 |
* |
|
4632 |
* @param string $slug The slug to truncate. |
|
4633 |
* @param int $length Optional. Max length of the slug. Default 200 (characters). |
|
0 | 4634 |
* @return string The truncated slug. |
4635 |
*/ |
|
4636 |
function _truncate_post_slug( $slug, $length = 200 ) { |
|
4637 |
if ( strlen( $slug ) > $length ) { |
|
4638 |
$decoded_slug = urldecode( $slug ); |
|
9 | 4639 |
if ( $decoded_slug === $slug ) { |
0 | 4640 |
$slug = substr( $slug, 0, $length ); |
9 | 4641 |
} else { |
0 | 4642 |
$slug = utf8_uri_encode( $decoded_slug, $length ); |
9 | 4643 |
} |
0 | 4644 |
} |
4645 |
||
4646 |
return rtrim( $slug, '-' ); |
|
4647 |
} |
|
4648 |
||
4649 |
/** |
|
5 | 4650 |
* Add tags to a post. |
4651 |
* |
|
4652 |
* @see wp_set_post_tags() |
|
4653 |
* |
|
0 | 4654 |
* @since 2.3.0 |
4655 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4656 |
* @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
|
4657 |
* @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
|
4658 |
* separated by commas. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4659 |
* @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. |
0 | 4660 |
*/ |
5 | 4661 |
function wp_add_post_tags( $post_id = 0, $tags = '' ) { |
9 | 4662 |
return wp_set_post_tags( $post_id, $tags, true ); |
0 | 4663 |
} |
4664 |
||
4665 |
/** |
|
4666 |
* Set the tags for a post. |
|
4667 |
* |
|
4668 |
* @since 2.3.0 |
|
5 | 4669 |
* |
4670 |
* @see wp_set_object_terms() |
|
4671 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4672 |
* @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
|
4673 |
* @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
|
4674 |
* separated by commas. Default empty. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4675 |
* @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
|
4676 |
* replace the tags with the new tags. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4677 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
0 | 4678 |
*/ |
4679 |
function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
|
9 | 4680 |
return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); |
0 | 4681 |
} |
4682 |
||
4683 |
/** |
|
4684 |
* Set the terms for a post. |
|
4685 |
* |
|
4686 |
* @since 2.8.0 |
|
5 | 4687 |
* |
4688 |
* @see wp_set_object_terms() |
|
4689 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4690 |
* @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
|
4691 |
* @param string|array $tags Optional. An array of terms to set for the post, or a string of terms |
9 | 4692 |
* separated by commas. Hierarchical taxonomies must always pass IDs rather |
4693 |
* than names so that children with the same names but different parents |
|
4694 |
* aren't confused. Default empty. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4695 |
* @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4696 |
* @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
|
4697 |
* replace the terms with the new terms. Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4698 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
0 | 4699 |
*/ |
4700 |
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { |
|
4701 |
$post_id = (int) $post_id; |
|
4702 |
||
9 | 4703 |
if ( ! $post_id ) { |
0 | 4704 |
return false; |
9 | 4705 |
} |
4706 |
||
4707 |
if ( empty( $tags ) ) { |
|
0 | 4708 |
$tags = array(); |
9 | 4709 |
} |
0 | 4710 |
|
4711 |
if ( ! is_array( $tags ) ) { |
|
4712 |
$comma = _x( ',', 'tag delimiter' ); |
|
9 | 4713 |
if ( ',' !== $comma ) { |
0 | 4714 |
$tags = str_replace( $comma, ',', $tags ); |
9 | 4715 |
} |
0 | 4716 |
$tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) ); |
4717 |
} |
|
4718 |
||
5 | 4719 |
/* |
4720 |
* Hierarchical taxonomies must always pass IDs rather than names so that |
|
4721 |
* children with the same names but different parents aren't confused. |
|
4722 |
*/ |
|
0 | 4723 |
if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
4724 |
$tags = array_unique( array_map( 'intval', $tags ) ); |
|
4725 |
} |
|
4726 |
||
4727 |
return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); |
|
4728 |
} |
|
4729 |
||
4730 |
/** |
|
4731 |
* Set categories for a post. |
|
4732 |
* |
|
16 | 4733 |
* If no categories are provided, the default category is used. |
0 | 4734 |
* |
4735 |
* @since 2.1.0 |
|
4736 |
* |
|
5 | 4737 |
* @param int $post_ID Optional. The Post ID. Does not default to the ID |
4738 |
* of the global $post. Default 0. |
|
9 | 4739 |
* @param array|int $post_categories Optional. List of category IDs, or the ID of a single category. |
5 | 4740 |
* Default empty array. |
9 | 4741 |
* @param bool $append If true, don't delete existing categories, just add on. |
4742 |
* 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
|
4743 |
* @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. |
0 | 4744 |
*/ |
4745 |
function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { |
|
9 | 4746 |
$post_ID = (int) $post_ID; |
4747 |
$post_type = get_post_type( $post_ID ); |
|
0 | 4748 |
$post_status = get_post_status( $post_ID ); |
16 | 4749 |
|
4750 |
// If $post_categories isn't already an array, make it one. |
|
0 | 4751 |
$post_categories = (array) $post_categories; |
16 | 4752 |
|
0 | 4753 |
if ( empty( $post_categories ) ) { |
16 | 4754 |
/** |
4755 |
* Filters post types (in addition to 'post') that require a default category. |
|
4756 |
* |
|
4757 |
* @since 5.5.0 |
|
4758 |
* |
|
4759 |
* @param string[] $post_types An array of post type names. Default empty array. |
|
4760 |
*/ |
|
4761 |
$default_category_post_types = apply_filters( 'default_category_post_types', array() ); |
|
4762 |
||
4763 |
// Regular posts always require a default category. |
|
4764 |
$default_category_post_types = array_merge( $default_category_post_types, array( 'post' ) ); |
|
4765 |
||
4766 |
if ( in_array( $post_type, $default_category_post_types, true ) |
|
4767 |
&& is_object_in_taxonomy( $post_type, 'category' ) |
|
4768 |
&& 'auto-draft' !== $post_status |
|
4769 |
) { |
|
9 | 4770 |
$post_categories = array( get_option( 'default_category' ) ); |
4771 |
$append = false; |
|
0 | 4772 |
} else { |
4773 |
$post_categories = array(); |
|
4774 |
} |
|
16 | 4775 |
} elseif ( 1 === count( $post_categories ) && '' === reset( $post_categories ) ) { |
0 | 4776 |
return true; |
4777 |
} |
|
4778 |
||
4779 |
return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); |
|
4780 |
} |
|
4781 |
||
4782 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4783 |
* Fires actions related to the transitioning of a post's status. |
0 | 4784 |
* |
5 | 4785 |
* When a post is saved, the post status is "transitioned" from one status to another, |
4786 |
* 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
|
4787 |
* 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
|
4788 |
* 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
|
4789 |
* {@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
|
4790 |
* that the function does not transition the post object in the database. |
5 | 4791 |
* |
4792 |
* For instance: When publishing a post for the first time, the post status may transition |
|
4793 |
* from 'draft' – or some other status – to 'publish'. However, if a post is already |
|
4794 |
* published and is simply being updated, the "old" and "new" statuses may both be 'publish' |
|
4795 |
* before and after the transition. |
|
0 | 4796 |
* |
4797 |
* @since 2.3.0 |
|
5 | 4798 |
* |
4799 |
* @param string $new_status Transition to this post status. |
|
4800 |
* @param string $old_status Previous post status. |
|
4801 |
* @param WP_Post $post Post data. |
|
0 | 4802 |
*/ |
5 | 4803 |
function wp_transition_post_status( $new_status, $old_status, $post ) { |
4804 |
/** |
|
4805 |
* Fires when a post is transitioned from one status to another. |
|
4806 |
* |
|
4807 |
* @since 2.3.0 |
|
4808 |
* |
|
4809 |
* @param string $new_status New post status. |
|
4810 |
* @param string $old_status Old post status. |
|
4811 |
* @param WP_Post $post Post object. |
|
4812 |
*/ |
|
4813 |
do_action( 'transition_post_status', $new_status, $old_status, $post ); |
|
4814 |
||
4815 |
/** |
|
4816 |
* Fires when a post is transitioned from one status to another. |
|
4817 |
* |
|
4818 |
* The dynamic portions of the hook name, `$new_status` and `$old status`, |
|
4819 |
* refer to the old and new post statuses, respectively. |
|
4820 |
* |
|
4821 |
* @since 2.3.0 |
|
4822 |
* |
|
4823 |
* @param WP_Post $post Post object. |
|
4824 |
*/ |
|
4825 |
do_action( "{$old_status}_to_{$new_status}", $post ); |
|
4826 |
||
4827 |
/** |
|
4828 |
* Fires when a post is transitioned from one status to another. |
|
4829 |
* |
|
4830 |
* The dynamic portions of the hook name, `$new_status` and `$post->post_type`, |
|
4831 |
* refer to the new post status and post type, respectively. |
|
4832 |
* |
|
4833 |
* Please note: When this action is hooked using a particular post status (like |
|
4834 |
* 'publish', as `publish_{$post->post_type}`), it will fire both when a post is |
|
4835 |
* first transitioned to that status from something else, as well as upon |
|
4836 |
* subsequent post updates (old and new status are both the same). |
|
4837 |
* |
|
4838 |
* Therefore, if you are looking to only fire a callback when a post is first |
|
4839 |
* transitioned to a status, use the {@see 'transition_post_status'} hook instead. |
|
4840 |
* |
|
4841 |
* @since 2.3.0 |
|
4842 |
* |
|
4843 |
* @param int $post_id Post ID. |
|
4844 |
* @param WP_Post $post Post object. |
|
4845 |
*/ |
|
4846 |
do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); |
|
0 | 4847 |
} |
4848 |
||
4849 |
// |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4850 |
// Comment, trackback, and pingback functions. |
0 | 4851 |
// |
4852 |
||
4853 |
/** |
|
5 | 4854 |
* Add a URL to those already pinged. |
0 | 4855 |
* |
4856 |
* @since 1.5.0 |
|
9 | 4857 |
* @since 4.7.0 `$post_id` can be a WP_Post object. |
4858 |
* @since 4.7.0 `$uri` can be an array of URIs. |
|
5 | 4859 |
* |
4860 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
4861 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4862 |
* @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
|
4863 |
* @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
|
4864 |
* @return int|false How many rows were updated. |
0 | 4865 |
*/ |
5 | 4866 |
function add_ping( $post_id, $uri ) { |
0 | 4867 |
global $wpdb; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4868 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4869 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4870 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4871 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4872 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4873 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4874 |
$pung = trim( $post->pinged ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4875 |
$pung = preg_split( '/\s/', $pung ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4876 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4877 |
if ( is_array( $uri ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4878 |
$pung = array_merge( $pung, $uri ); |
9 | 4879 |
} else { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4880 |
$pung[] = $uri; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4881 |
} |
9 | 4882 |
$new = implode( "\n", $pung ); |
5 | 4883 |
|
4884 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4885 |
* Filters the new ping URL to add for the given post. |
5 | 4886 |
* |
4887 |
* @since 2.0.0 |
|
4888 |
* |
|
4889 |
* @param string $new New ping URL to add. |
|
4890 |
*/ |
|
4891 |
$new = apply_filters( 'add_ping', $new ); |
|
4892 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4893 |
$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
|
4894 |
clean_post_cache( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4895 |
return $return; |
0 | 4896 |
} |
4897 |
||
4898 |
/** |
|
4899 |
* Retrieve enclosures already enclosed for a post. |
|
4900 |
* |
|
4901 |
* @since 1.5.0 |
|
4902 |
* |
|
4903 |
* @param int $post_id Post ID. |
|
16 | 4904 |
* @return string[] Array of enclosures for the given post. |
0 | 4905 |
*/ |
5 | 4906 |
function get_enclosed( $post_id ) { |
0 | 4907 |
$custom_fields = get_post_custom( $post_id ); |
9 | 4908 |
$pung = array(); |
4909 |
if ( ! is_array( $custom_fields ) ) { |
|
0 | 4910 |
return $pung; |
9 | 4911 |
} |
0 | 4912 |
|
4913 |
foreach ( $custom_fields as $key => $val ) { |
|
16 | 4914 |
if ( 'enclosure' !== $key || ! is_array( $val ) ) { |
0 | 4915 |
continue; |
9 | 4916 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4917 |
foreach ( $val as $enc ) { |
0 | 4918 |
$enclosure = explode( "\n", $enc ); |
9 | 4919 |
$pung[] = trim( $enclosure[0] ); |
0 | 4920 |
} |
4921 |
} |
|
5 | 4922 |
|
4923 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4924 |
* Filters the list of enclosures already enclosed for the given post. |
5 | 4925 |
* |
4926 |
* @since 2.0.0 |
|
4927 |
* |
|
16 | 4928 |
* @param string[] $pung Array of enclosures for the given post. |
4929 |
* @param int $post_id Post ID. |
|
5 | 4930 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4931 |
return apply_filters( 'get_enclosed', $pung, $post_id ); |
0 | 4932 |
} |
4933 |
||
4934 |
/** |
|
4935 |
* Retrieve URLs already pinged for a post. |
|
4936 |
* |
|
4937 |
* @since 1.5.0 |
|
5 | 4938 |
* |
9 | 4939 |
* @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
|
4940 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4941 |
* @param int|WP_Post $post_id Post ID or object. |
9 | 4942 |
* @return bool|string[] Array of URLs already pinged for the given post, false if the post is not found. |
0 | 4943 |
*/ |
5 | 4944 |
function get_pung( $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4945 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4946 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4947 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4948 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4949 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4950 |
$pung = trim( $post->pinged ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4951 |
$pung = preg_split( '/\s/', $pung ); |
5 | 4952 |
|
4953 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4954 |
* Filters the list of already-pinged URLs for the given post. |
5 | 4955 |
* |
4956 |
* @since 2.0.0 |
|
4957 |
* |
|
9 | 4958 |
* @param string[] $pung Array of URLs already pinged for the given post. |
5 | 4959 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4960 |
return apply_filters( 'get_pung', $pung ); |
0 | 4961 |
} |
4962 |
||
4963 |
/** |
|
4964 |
* Retrieve URLs that need to be pinged. |
|
4965 |
* |
|
4966 |
* @since 1.5.0 |
|
9 | 4967 |
* @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
|
4968 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4969 |
* @param int|WP_Post $post_id Post Object or ID |
16 | 4970 |
* @return string[]|false List of URLs yet to ping. |
0 | 4971 |
*/ |
5 | 4972 |
function get_to_ping( $post_id ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4973 |
$post = get_post( $post_id ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4974 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4975 |
if ( ! $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4976 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4977 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4978 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4979 |
$to_ping = sanitize_trackback_urls( $post->to_ping ); |
9 | 4980 |
$to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY ); |
5 | 4981 |
|
4982 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4983 |
* Filters the list of URLs yet to ping for the given post. |
5 | 4984 |
* |
4985 |
* @since 2.0.0 |
|
4986 |
* |
|
16 | 4987 |
* @param string[] $to_ping List of URLs yet to ping. |
5 | 4988 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
4989 |
return apply_filters( 'get_to_ping', $to_ping ); |
0 | 4990 |
} |
4991 |
||
4992 |
/** |
|
4993 |
* Do trackbacks for a list of URLs. |
|
4994 |
* |
|
4995 |
* @since 1.0.0 |
|
4996 |
* |
|
5 | 4997 |
* @param string $tb_list Comma separated list of URLs. |
4998 |
* @param int $post_id Post ID. |
|
0 | 4999 |
*/ |
5 | 5000 |
function trackback_url_list( $tb_list, $post_id ) { |
0 | 5001 |
if ( ! empty( $tb_list ) ) { |
5 | 5002 |
// Get post data. |
5003 |
$postdata = get_post( $post_id, ARRAY_A ); |
|
5004 |
||
5005 |
// Form an excerpt. |
|
5006 |
$excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); |
|
5007 |
||
5008 |
if ( strlen( $excerpt ) > 255 ) { |
|
5009 |
$excerpt = substr( $excerpt, 0, 252 ) . '…'; |
|
0 | 5010 |
} |
5011 |
||
5 | 5012 |
$trackback_urls = explode( ',', $tb_list ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5013 |
foreach ( (array) $trackback_urls as $tb_url ) { |
5 | 5014 |
$tb_url = trim( $tb_url ); |
5015 |
trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); |
|
0 | 5016 |
} |
5017 |
} |
|
5018 |
} |
|
5019 |
||
5020 |
// |
|
16 | 5021 |
// Page functions. |
0 | 5022 |
// |
5023 |
||
5024 |
/** |
|
5025 |
* Get a list of page IDs. |
|
5026 |
* |
|
5027 |
* @since 2.0.0 |
|
5 | 5028 |
* |
5029 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 5030 |
* |
16 | 5031 |
* @return string[] List of page IDs as strings. |
0 | 5032 |
*/ |
5033 |
function get_all_page_ids() { |
|
5034 |
global $wpdb; |
|
5035 |
||
9 | 5036 |
$page_ids = wp_cache_get( 'all_page_ids', 'posts' ); |
0 | 5037 |
if ( ! is_array( $page_ids ) ) { |
9 | 5038 |
$page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" ); |
5039 |
wp_cache_add( 'all_page_ids', $page_ids, 'posts' ); |
|
0 | 5040 |
} |
5041 |
||
5042 |
return $page_ids; |
|
5043 |
} |
|
5044 |
||
5045 |
/** |
|
5046 |
* Retrieves page data given a page ID or page object. |
|
5047 |
* |
|
5048 |
* Use get_post() instead of get_page(). |
|
5049 |
* |
|
5050 |
* @since 1.5.1 |
|
5 | 5051 |
* @deprecated 3.5.0 Use get_post() |
5052 |
* |
|
16 | 5053 |
* @param int|WP_Post $page Page object or page ID. Passed by reference. |
5054 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
5055 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
5056 |
* respectively. Default OBJECT. |
|
5057 |
* @param string $filter Optional. How the return value should be filtered. Accepts 'raw', |
|
5058 |
* 'edit', 'db', 'display'. Default 'raw'. |
|
5059 |
* @return WP_Post|array|null WP_Post or array on success, null on failure. |
|
0 | 5060 |
*/ |
9 | 5061 |
function get_page( $page, $output = OBJECT, $filter = 'raw' ) { |
0 | 5062 |
return get_post( $page, $output, $filter ); |
5063 |
} |
|
5064 |
||
5065 |
/** |
|
5066 |
* Retrieves a page given its path. |
|
5067 |
* |
|
5068 |
* @since 2.1.0 |
|
5 | 5069 |
* |
5070 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5071 |
* |
|
5072 |
* @param string $page_path Page path. |
|
16 | 5073 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
5074 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
5075 |
* respectively. Default OBJECT. |
|
5 | 5076 |
* @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
|
5077 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 5078 |
*/ |
5 | 5079 |
function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { |
0 | 5080 |
global $wpdb; |
5081 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5082 |
$last_changed = wp_cache_get_last_changed( 'posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5083 |
|
9 | 5084 |
$hash = md5( $page_path . serialize( $post_type ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5085 |
$cache_key = "get_page_by_path:$hash:$last_changed"; |
9 | 5086 |
$cached = wp_cache_get( $cache_key, 'posts' ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5087 |
if ( false !== $cached ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5088 |
// Special case: '0' is a bad `$page_path`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5089 |
if ( '0' === $cached || 0 === $cached ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5090 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5091 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5092 |
return get_post( $cached, $output ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5093 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5094 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5095 |
|
9 | 5096 |
$page_path = rawurlencode( urldecode( $page_path ) ); |
5097 |
$page_path = str_replace( '%2F', '/', $page_path ); |
|
5098 |
$page_path = str_replace( '%20', ' ', $page_path ); |
|
5099 |
$parts = explode( '/', trim( $page_path, '/' ) ); |
|
5100 |
$parts = array_map( 'sanitize_title_for_query', $parts ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5101 |
$escaped_parts = esc_sql( $parts ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5102 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5103 |
$in_string = "'" . implode( "','", $escaped_parts ) . "'"; |
5 | 5104 |
|
5105 |
if ( is_array( $post_type ) ) { |
|
5106 |
$post_types = $post_type; |
|
5107 |
} else { |
|
5108 |
$post_types = array( $post_type, 'attachment' ); |
|
5109 |
} |
|
5110 |
||
9 | 5111 |
$post_types = esc_sql( $post_types ); |
5 | 5112 |
$post_type_in_string = "'" . implode( "','", $post_types ) . "'"; |
9 | 5113 |
$sql = " |
5 | 5114 |
SELECT ID, post_name, post_parent, post_type |
5115 |
FROM $wpdb->posts |
|
5116 |
WHERE post_name IN ($in_string) |
|
5117 |
AND post_type IN ($post_type_in_string) |
|
5118 |
"; |
|
5119 |
||
5120 |
$pages = $wpdb->get_results( $sql, OBJECT_K ); |
|
0 | 5121 |
|
5122 |
$revparts = array_reverse( $parts ); |
|
5123 |
||
5124 |
$foundid = 0; |
|
5125 |
foreach ( (array) $pages as $page ) { |
|
5126 |
if ( $page->post_name == $revparts[0] ) { |
|
5127 |
$count = 0; |
|
9 | 5128 |
$p = $page; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5129 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5130 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5131 |
* 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
|
5132 |
* ensuring each matches the post ancestry. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5133 |
*/ |
16 | 5134 |
while ( 0 != $p->post_parent && isset( $pages[ $p->post_parent ] ) ) { |
0 | 5135 |
$count++; |
5136 |
$parent = $pages[ $p->post_parent ]; |
|
9 | 5137 |
if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) { |
0 | 5138 |
break; |
9 | 5139 |
} |
0 | 5140 |
$p = $parent; |
5141 |
} |
|
5142 |
||
16 | 5143 |
if ( 0 == $p->post_parent && count( $revparts ) == $count + 1 && $p->post_name == $revparts[ $count ] ) { |
0 | 5144 |
$foundid = $page->ID; |
9 | 5145 |
if ( $page->post_type == $post_type ) { |
0 | 5146 |
break; |
9 | 5147 |
} |
0 | 5148 |
} |
5149 |
} |
|
5150 |
} |
|
5151 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5152 |
// We cache misses as well as hits. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5153 |
wp_cache_set( $cache_key, $foundid, 'posts' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5154 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5155 |
if ( $foundid ) { |
0 | 5156 |
return get_post( $foundid, $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5157 |
} |
0 | 5158 |
} |
5159 |
||
5160 |
/** |
|
5161 |
* Retrieve a page given its title. |
|
5162 |
* |
|
16 | 5163 |
* If more than one post uses the same title, the post with the smallest ID will be returned. |
5164 |
* Be careful: in case of more than one post having the same title, it will check the oldest |
|
5165 |
* publication date, not the smallest ID. |
|
5166 |
* |
|
5167 |
* Because this function uses the MySQL '=' comparison, $page_title will usually be matched |
|
5168 |
* as case-insensitive with default collation. |
|
5169 |
* |
|
0 | 5170 |
* @since 2.1.0 |
16 | 5171 |
* @since 3.0.0 The `$post_type` parameter was added. |
5 | 5172 |
* |
5173 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5174 |
* |
|
16 | 5175 |
* @param string $page_title Page title. |
5176 |
* @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which |
|
5177 |
* correspond to a WP_Post object, an associative array, or a numeric array, |
|
5178 |
* respectively. Default OBJECT. |
|
5 | 5179 |
* @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
|
5180 |
* @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
0 | 5181 |
*/ |
5 | 5182 |
function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { |
0 | 5183 |
global $wpdb; |
5 | 5184 |
|
5185 |
if ( is_array( $post_type ) ) { |
|
9 | 5186 |
$post_type = esc_sql( $post_type ); |
5 | 5187 |
$post_type_in_string = "'" . implode( "','", $post_type ) . "'"; |
9 | 5188 |
$sql = $wpdb->prepare( |
5189 |
" |
|
5 | 5190 |
SELECT ID |
5191 |
FROM $wpdb->posts |
|
5192 |
WHERE post_title = %s |
|
5193 |
AND post_type IN ($post_type_in_string) |
|
9 | 5194 |
", |
5195 |
$page_title |
|
5196 |
); |
|
5 | 5197 |
} else { |
9 | 5198 |
$sql = $wpdb->prepare( |
5199 |
" |
|
5 | 5200 |
SELECT ID |
5201 |
FROM $wpdb->posts |
|
5202 |
WHERE post_title = %s |
|
5203 |
AND post_type = %s |
|
9 | 5204 |
", |
5205 |
$page_title, |
|
5206 |
$post_type |
|
5207 |
); |
|
5 | 5208 |
} |
5209 |
||
5210 |
$page = $wpdb->get_var( $sql ); |
|
5211 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5212 |
if ( $page ) { |
0 | 5213 |
return get_post( $page, $output ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5214 |
} |
0 | 5215 |
} |
5216 |
||
5217 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5218 |
* 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
|
5219 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5220 |
* Descendants are identified from the `$pages` array passed to the function. No database queries are performed. |
0 | 5221 |
* |
5222 |
* @since 1.5.1 |
|
5223 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5224 |
* @param int $page_id Page ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5225 |
* @param array $pages List of page objects from which descendants should be identified. |
5 | 5226 |
* @return array List of page children. |
0 | 5227 |
*/ |
5 | 5228 |
function get_page_children( $page_id, $pages ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5229 |
// Build a hash of ID -> children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5230 |
$children = array(); |
0 | 5231 |
foreach ( (array) $pages as $page ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5232 |
$children[ intval( $page->post_parent ) ][] = $page; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5233 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5234 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5235 |
$page_list = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5236 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5237 |
// Start the search by looking at immediate children. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5238 |
if ( isset( $children[ $page_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5239 |
// 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
|
5240 |
$to_look = array_reverse( $children[ $page_id ] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5241 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5242 |
while ( $to_look ) { |
9 | 5243 |
$p = array_pop( $to_look ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5244 |
$page_list[] = $p; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5245 |
if ( isset( $children[ $p->ID ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5246 |
foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5247 |
// 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
|
5248 |
$to_look[] = $child; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5249 |
} |
5 | 5250 |
} |
0 | 5251 |
} |
5252 |
} |
|
5 | 5253 |
|
0 | 5254 |
return $page_list; |
5255 |
} |
|
5256 |
||
5257 |
/** |
|
5258 |
* Order the pages with children under parents in a flat list. |
|
5259 |
* |
|
5260 |
* It uses auxiliary structure to hold parent-children relationships and |
|
5261 |
* runs in O(N) complexity |
|
5262 |
* |
|
5263 |
* @since 2.0.0 |
|
5264 |
* |
|
16 | 5265 |
* @param WP_Post[] $pages Posts array (passed by reference). |
5266 |
* @param int $page_id Optional. Parent page ID. Default 0. |
|
5267 |
* @return string[] Array of post names keyed by ID and arranged by hierarchy. Children immediately follow their parents. |
|
0 | 5268 |
*/ |
5269 |
function get_page_hierarchy( &$pages, $page_id = 0 ) { |
|
5270 |
if ( empty( $pages ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5271 |
return array(); |
0 | 5272 |
} |
5273 |
||
5274 |
$children = array(); |
|
5275 |
foreach ( (array) $pages as $p ) { |
|
9 | 5276 |
$parent_id = intval( $p->post_parent ); |
0 | 5277 |
$children[ $parent_id ][] = $p; |
5278 |
} |
|
5279 |
||
5280 |
$result = array(); |
|
5281 |
_page_traverse_name( $page_id, $children, $result ); |
|
5282 |
||
5283 |
return $result; |
|
5284 |
} |
|
5285 |
||
5286 |
/** |
|
5 | 5287 |
* Traverse and return all the nested children post names of a root page. |
5288 |
* |
|
0 | 5289 |
* $children contains parent-children relations |
5290 |
* |
|
5291 |
* @since 2.9.0 |
|
9 | 5292 |
* @access private |
5 | 5293 |
* |
5294 |
* @see _page_traverse_name() |
|
5295 |
* |
|
16 | 5296 |
* @param int $page_id Page ID. |
5297 |
* @param array $children Parent-children relations (passed by reference). |
|
5298 |
* @param string[] $result Array of page names keyed by ID (passed by reference). |
|
0 | 5299 |
*/ |
9 | 5300 |
function _page_traverse_name( $page_id, &$children, &$result ) { |
5301 |
if ( isset( $children[ $page_id ] ) ) { |
|
5302 |
foreach ( (array) $children[ $page_id ] as $child ) { |
|
0 | 5303 |
$result[ $child->ID ] = $child->post_name; |
5304 |
_page_traverse_name( $child->ID, $children, $result ); |
|
5305 |
} |
|
5306 |
} |
|
5307 |
} |
|
5308 |
||
5309 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5310 |
* Build the URI path for a page. |
0 | 5311 |
* |
5312 |
* Sub pages will be in the "directory" under the parent page post name. |
|
5313 |
* |
|
5314 |
* @since 1.5.0 |
|
16 | 5315 |
* @since 4.6.0 The `$page` parameter was made optional. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5316 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5317 |
* @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. |
0 | 5318 |
* @return string|false Page URI, false on error. |
5319 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5320 |
function get_page_uri( $page = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5321 |
if ( ! $page instanceof WP_Post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5322 |
$page = get_post( $page ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5323 |
} |
0 | 5324 |
|
9 | 5325 |
if ( ! $page ) { |
0 | 5326 |
return false; |
9 | 5327 |
} |
0 | 5328 |
|
5329 |
$uri = $page->post_name; |
|
5330 |
||
5331 |
foreach ( $page->ancestors as $parent ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5332 |
$parent = get_post( $parent ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5333 |
if ( $parent && $parent->post_name ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5334 |
$uri = $parent->post_name . '/' . $uri; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5335 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5336 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5337 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5338 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5339 |
* Filters the URI for a page. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5340 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5341 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5342 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5343 |
* @param string $uri Page URI. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5344 |
* @param WP_Post $page Page object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5345 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5346 |
return apply_filters( 'get_page_uri', $uri, $page ); |
0 | 5347 |
} |
5348 |
||
5349 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5350 |
* Retrieve a list of pages (or hierarchical post type items). |
0 | 5351 |
* |
5 | 5352 |
* @global wpdb $wpdb WordPress database abstraction object. |
0 | 5353 |
* |
5354 |
* @since 1.5.0 |
|
5355 |
* |
|
5 | 5356 |
* @param array|string $args { |
5357 |
* Optional. Array or string of arguments to retrieve pages. |
|
5358 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5359 |
* @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
|
5360 |
* 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
|
5361 |
* hierarchical results. Default 0, or no restriction. |
5 | 5362 |
* @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. |
5363 |
* @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', |
|
5364 |
* 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', |
|
5365 |
* 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. |
|
5366 |
* 'post_' can be omitted for any values that start with it. |
|
5367 |
* Default 'post_title'. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5368 |
* @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
|
5369 |
* `$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
|
5370 |
* Default true. |
5 | 5371 |
* @type array $exclude Array of page IDs to exclude. Default empty array. |
5372 |
* @type array $include Array of page IDs to include. Cannot be used with `$child_of`, |
|
5373 |
* `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. |
|
5374 |
* Default empty array. |
|
5375 |
* @type string $meta_key Only include pages with this meta key. Default empty. |
|
5376 |
* @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. |
|
5377 |
* Default empty. |
|
5378 |
* @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
|
5379 |
* @type int $parent Page ID to return direct children of. Default -1, or no restriction. |
5 | 5380 |
* @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. |
5381 |
* Default empty array. |
|
5382 |
* @type int $number The number of pages to return. Default 0, or all pages. |
|
5383 |
* @type int $offset The number of pages to skip before returning. Requires `$number`. |
|
5384 |
* Default 0. |
|
5385 |
* @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
|
5386 |
* @type string|array $post_status A comma-separated list or array of post statuses to include. |
5 | 5387 |
* Default 'publish'. |
0 | 5388 |
* } |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5389 |
* @return array|false List of pages matching defaults or `$args`. |
0 | 5390 |
*/ |
5391 |
function get_pages( $args = array() ) { |
|
5392 |
global $wpdb; |
|
5393 |
||
5394 |
$defaults = array( |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5395 |
'child_of' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5396 |
'sort_order' => 'ASC', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5397 |
'sort_column' => 'post_title', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5398 |
'hierarchical' => 1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5399 |
'exclude' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5400 |
'include' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5401 |
'meta_key' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5402 |
'meta_value' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5403 |
'authors' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5404 |
'parent' => -1, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5405 |
'exclude_tree' => array(), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5406 |
'number' => '', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5407 |
'offset' => 0, |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5408 |
'post_type' => 'page', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5409 |
'post_status' => 'publish', |
0 | 5410 |
); |
5411 |
||
16 | 5412 |
$parsed_args = wp_parse_args( $args, $defaults ); |
5413 |
||
5414 |
$number = (int) $parsed_args['number']; |
|
5415 |
$offset = (int) $parsed_args['offset']; |
|
5416 |
$child_of = (int) $parsed_args['child_of']; |
|
5417 |
$hierarchical = $parsed_args['hierarchical']; |
|
5418 |
$exclude = $parsed_args['exclude']; |
|
5419 |
$meta_key = $parsed_args['meta_key']; |
|
5420 |
$meta_value = $parsed_args['meta_value']; |
|
5421 |
$parent = $parsed_args['parent']; |
|
5422 |
$post_status = $parsed_args['post_status']; |
|
5 | 5423 |
|
5424 |
// Make sure the post type is hierarchical. |
|
0 | 5425 |
$hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); |
16 | 5426 |
if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) { |
5 | 5427 |
return false; |
5428 |
} |
|
5429 |
||
5430 |
if ( $parent > 0 && ! $child_of ) { |
|
0 | 5431 |
$hierarchical = false; |
5 | 5432 |
} |
5433 |
||
5434 |
// Make sure we have a valid post status. |
|
5435 |
if ( ! is_array( $post_status ) ) { |
|
0 | 5436 |
$post_status = explode( ',', $post_status ); |
5 | 5437 |
} |
5438 |
if ( array_diff( $post_status, get_post_stati() ) ) { |
|
5439 |
return false; |
|
5440 |
} |
|
5441 |
||
5442 |
// $args can be whatever, only use the args defined in defaults to compute the key. |
|
16 | 5443 |
$key = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5444 |
$last_changed = wp_cache_get_last_changed( 'posts' ); |
0 | 5445 |
|
5446 |
$cache_key = "get_pages:$key:$last_changed"; |
|
9 | 5447 |
$cache = wp_cache_get( $cache_key, 'posts' ); |
5448 |
if ( false !== $cache ) { |
|
5 | 5449 |
// Convert to WP_Post instances. |
0 | 5450 |
$pages = array_map( 'get_post', $cache ); |
5 | 5451 |
/** This filter is documented in wp-includes/post.php */ |
16 | 5452 |
$pages = apply_filters( 'get_pages', $pages, $parsed_args ); |
0 | 5453 |
return $pages; |
5454 |
} |
|
5455 |
||
5456 |
$inclusions = ''; |
|
16 | 5457 |
if ( ! empty( $parsed_args['include'] ) ) { |
5458 |
$child_of = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include. |
|
9 | 5459 |
$parent = -1; |
5460 |
$exclude = ''; |
|
5461 |
$meta_key = ''; |
|
5462 |
$meta_value = ''; |
|
0 | 5463 |
$hierarchical = false; |
16 | 5464 |
$incpages = wp_parse_id_list( $parsed_args['include'] ); |
5 | 5465 |
if ( ! empty( $incpages ) ) { |
9 | 5466 |
$inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; |
5 | 5467 |
} |
0 | 5468 |
} |
5469 |
||
5470 |
$exclusions = ''; |
|
5471 |
if ( ! empty( $exclude ) ) { |
|
5472 |
$expages = wp_parse_id_list( $exclude ); |
|
5 | 5473 |
if ( ! empty( $expages ) ) { |
9 | 5474 |
$exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; |
5 | 5475 |
} |
0 | 5476 |
} |
5477 |
||
5478 |
$author_query = ''; |
|
16 | 5479 |
if ( ! empty( $parsed_args['authors'] ) ) { |
5480 |
$post_authors = wp_parse_list( $parsed_args['authors'] ); |
|
0 | 5481 |
|
5482 |
if ( ! empty( $post_authors ) ) { |
|
5483 |
foreach ( $post_authors as $post_author ) { |
|
16 | 5484 |
// Do we have an author id or an author login? |
9 | 5485 |
if ( 0 == intval( $post_author ) ) { |
5486 |
$post_author = get_user_by( 'login', $post_author ); |
|
5 | 5487 |
if ( empty( $post_author ) ) { |
0 | 5488 |
continue; |
5 | 5489 |
} |
5490 |
if ( empty( $post_author->ID ) ) { |
|
0 | 5491 |
continue; |
5 | 5492 |
} |
0 | 5493 |
$post_author = $post_author->ID; |
5494 |
} |
|
5495 |
||
16 | 5496 |
if ( '' === $author_query ) { |
9 | 5497 |
$author_query = $wpdb->prepare( ' post_author = %d ', $post_author ); |
5 | 5498 |
} else { |
9 | 5499 |
$author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author ); |
5 | 5500 |
} |
0 | 5501 |
} |
16 | 5502 |
if ( '' !== $author_query ) { |
0 | 5503 |
$author_query = " AND ($author_query)"; |
5 | 5504 |
} |
0 | 5505 |
} |
5506 |
} |
|
5507 |
||
9 | 5508 |
$join = ''; |
0 | 5509 |
$where = "$exclusions $inclusions "; |
5510 |
if ( '' !== $meta_key || '' !== $meta_value ) { |
|
5511 |
$join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
|
5512 |
||
16 | 5513 |
// meta_key and meta_value might be slashed. |
9 | 5514 |
$meta_key = wp_unslash( $meta_key ); |
5515 |
$meta_value = wp_unslash( $meta_value ); |
|
5 | 5516 |
if ( '' !== $meta_key ) { |
9 | 5517 |
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key ); |
5 | 5518 |
} |
5519 |
if ( '' !== $meta_value ) { |
|
9 | 5520 |
$where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value ); |
5 | 5521 |
} |
0 | 5522 |
} |
5523 |
||
5524 |
if ( is_array( $parent ) ) { |
|
5525 |
$post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); |
|
5 | 5526 |
if ( ! empty( $post_parent__in ) ) { |
0 | 5527 |
$where .= " AND post_parent IN ($post_parent__in)"; |
5 | 5528 |
} |
0 | 5529 |
} elseif ( $parent >= 0 ) { |
9 | 5530 |
$where .= $wpdb->prepare( ' AND post_parent = %d ', $parent ); |
0 | 5531 |
} |
5532 |
||
16 | 5533 |
if ( 1 === count( $post_status ) ) { |
5534 |
$where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) ); |
|
0 | 5535 |
} else { |
9 | 5536 |
$post_status = implode( "', '", $post_status ); |
16 | 5537 |
$where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] ); |
0 | 5538 |
} |
5539 |
||
5540 |
$orderby_array = array(); |
|
9 | 5541 |
$allowed_keys = array( |
5542 |
'author', |
|
5543 |
'post_author', |
|
5544 |
'date', |
|
5545 |
'post_date', |
|
5546 |
'title', |
|
5547 |
'post_title', |
|
5548 |
'name', |
|
5549 |
'post_name', |
|
5550 |
'modified', |
|
5551 |
'post_modified', |
|
5552 |
'modified_gmt', |
|
5553 |
'post_modified_gmt', |
|
5554 |
'menu_order', |
|
5555 |
'parent', |
|
5556 |
'post_parent', |
|
5557 |
'ID', |
|
5558 |
'rand', |
|
5559 |
'comment_count', |
|
5560 |
); |
|
5 | 5561 |
|
16 | 5562 |
foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) { |
0 | 5563 |
$orderby = trim( $orderby ); |
16 | 5564 |
if ( ! in_array( $orderby, $allowed_keys, true ) ) { |
0 | 5565 |
continue; |
5 | 5566 |
} |
0 | 5567 |
|
5568 |
switch ( $orderby ) { |
|
5569 |
case 'menu_order': |
|
5570 |
break; |
|
5571 |
case 'ID': |
|
5572 |
$orderby = "$wpdb->posts.ID"; |
|
5573 |
break; |
|
5574 |
case 'rand': |
|
5575 |
$orderby = 'RAND()'; |
|
5576 |
break; |
|
5577 |
case 'comment_count': |
|
5578 |
$orderby = "$wpdb->posts.comment_count"; |
|
5579 |
break; |
|
5580 |
default: |
|
5 | 5581 |
if ( 0 === strpos( $orderby, 'post_' ) ) { |
0 | 5582 |
$orderby = "$wpdb->posts." . $orderby; |
5 | 5583 |
} else { |
0 | 5584 |
$orderby = "$wpdb->posts.post_" . $orderby; |
5 | 5585 |
} |
0 | 5586 |
} |
5587 |
||
5588 |
$orderby_array[] = $orderby; |
|
5589 |
||
5590 |
} |
|
5591 |
$sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; |
|
5592 |
||
16 | 5593 |
$sort_order = strtoupper( $parsed_args['sort_order'] ); |
5594 |
if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) { |
|
0 | 5595 |
$sort_order = 'ASC'; |
5 | 5596 |
} |
0 | 5597 |
|
9 | 5598 |
$query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
0 | 5599 |
$query .= $author_query; |
9 | 5600 |
$query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order; |
0 | 5601 |
|
5 | 5602 |
if ( ! empty( $number ) ) { |
0 | 5603 |
$query .= ' LIMIT ' . $offset . ',' . $number; |
5 | 5604 |
} |
0 | 5605 |
|
9 | 5606 |
$pages = $wpdb->get_results( $query ); |
5607 |
||
5608 |
if ( empty( $pages ) ) { |
|
5609 |
wp_cache_set( $cache_key, array(), 'posts' ); |
|
5610 |
||
5 | 5611 |
/** This filter is documented in wp-includes/post.php */ |
16 | 5612 |
$pages = apply_filters( 'get_pages', array(), $parsed_args ); |
0 | 5613 |
return $pages; |
5614 |
} |
|
5615 |
||
5 | 5616 |
// Sanitize before caching so it'll only get done once. |
9 | 5617 |
$num_pages = count( $pages ); |
5618 |
for ( $i = 0; $i < $num_pages; $i++ ) { |
|
5619 |
$pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); |
|
0 | 5620 |
} |
5621 |
||
5622 |
// Update cache. |
|
5623 |
update_post_cache( $pages ); |
|
5624 |
||
5 | 5625 |
if ( $child_of || $hierarchical ) { |
9 | 5626 |
$pages = get_page_children( $child_of, $pages ); |
5 | 5627 |
} |
5628 |
||
16 | 5629 |
if ( ! empty( $parsed_args['exclude_tree'] ) ) { |
5630 |
$exclude = wp_parse_id_list( $parsed_args['exclude_tree'] ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5631 |
foreach ( $exclude as $id ) { |
5 | 5632 |
$children = get_page_children( $id, $pages ); |
5633 |
foreach ( $children as $child ) { |
|
5634 |
$exclude[] = $child->ID; |
|
5635 |
} |
|
5636 |
} |
|
5637 |
||
5638 |
$num_pages = count( $pages ); |
|
0 | 5639 |
for ( $i = 0; $i < $num_pages; $i++ ) { |
16 | 5640 |
if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) { |
9 | 5641 |
unset( $pages[ $i ] ); |
5 | 5642 |
} |
0 | 5643 |
} |
5644 |
} |
|
5645 |
||
5646 |
$page_structure = array(); |
|
5 | 5647 |
foreach ( $pages as $page ) { |
0 | 5648 |
$page_structure[] = $page->ID; |
5 | 5649 |
} |
0 | 5650 |
|
5651 |
wp_cache_set( $cache_key, $page_structure, 'posts' ); |
|
5652 |
||
16 | 5653 |
// Convert to WP_Post instances. |
0 | 5654 |
$pages = array_map( 'get_post', $pages ); |
5655 |
||
5 | 5656 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5657 |
* Filters the retrieved list of pages. |
5 | 5658 |
* |
5659 |
* @since 2.1.0 |
|
5660 |
* |
|
16 | 5661 |
* @param WP_Post[] $pages Array of page objects. |
5662 |
* @param array $parsed_args Array of get_pages() arguments. |
|
5 | 5663 |
*/ |
16 | 5664 |
return apply_filters( 'get_pages', $pages, $parsed_args ); |
0 | 5665 |
} |
5666 |
||
5667 |
// |
|
16 | 5668 |
// Attachment functions. |
0 | 5669 |
// |
5670 |
||
5671 |
/** |
|
9 | 5672 |
* Determines whether an attachment URI is local and really an attachment. |
5673 |
* |
|
5674 |
* For more information on this and similar theme functions, check out |
|
5675 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
5676 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 5677 |
* |
5678 |
* @since 2.0.0 |
|
5679 |
* |
|
5680 |
* @param string $url URL to check |
|
5681 |
* @return bool True on success, false on failure. |
|
5682 |
*/ |
|
9 | 5683 |
function is_local_attachment( $url ) { |
5684 |
if ( strpos( $url, home_url() ) === false ) { |
|
0 | 5685 |
return false; |
9 | 5686 |
} |
5687 |
if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) { |
|
0 | 5688 |
return true; |
9 | 5689 |
} |
16 | 5690 |
|
5691 |
$id = url_to_postid( $url ); |
|
5692 |
if ( $id ) { |
|
9 | 5693 |
$post = get_post( $id ); |
16 | 5694 |
if ( 'attachment' === $post->post_type ) { |
0 | 5695 |
return true; |
9 | 5696 |
} |
0 | 5697 |
} |
5698 |
return false; |
|
5699 |
} |
|
5700 |
||
5701 |
/** |
|
5702 |
* Insert an attachment. |
|
5703 |
* |
|
5 | 5704 |
* If you set the 'ID' in the $args parameter, it will mean that you are |
0 | 5705 |
* updating and attempt to update the attachment. You can also set the |
5706 |
* attachment name or title by setting the key 'post_name' or 'post_title'. |
|
5707 |
* |
|
5708 |
* You can set the dates for the attachment manually by setting the 'post_date' |
|
5709 |
* and 'post_date_gmt' keys' values. |
|
5710 |
* |
|
5711 |
* By default, the comments will use the default settings for whether the |
|
5712 |
* comments are allowed. You can close them manually or keep them open by |
|
5713 |
* setting the value for the 'comment_status' key. |
|
5714 |
* |
|
5715 |
* @since 2.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5716 |
* @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. |
5 | 5717 |
* |
5718 |
* @see wp_insert_post() |
|
5719 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5720 |
* @param string|array $args Arguments for inserting an attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5721 |
* @param string $file Optional. Filename. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5722 |
* @param int $parent Optional. Parent post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5723 |
* @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
|
5724 |
* @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. |
0 | 5725 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5726 |
function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) { |
5 | 5727 |
$defaults = array( |
5728 |
'file' => $file, |
|
9 | 5729 |
'post_parent' => 0, |
5 | 5730 |
); |
5731 |
||
5732 |
$data = wp_parse_args( $args, $defaults ); |
|
5733 |
||
5734 |
if ( ! empty( $parent ) ) { |
|
5735 |
$data['post_parent'] = $parent; |
|
0 | 5736 |
} |
5 | 5737 |
|
5738 |
$data['post_type'] = 'attachment'; |
|
5739 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5740 |
return wp_insert_post( $data, $wp_error ); |
0 | 5741 |
} |
5742 |
||
5743 |
/** |
|
5 | 5744 |
* Trash or delete an attachment. |
0 | 5745 |
* |
5746 |
* When an attachment is permanently deleted, the file will also be removed. |
|
5747 |
* Deletion removes all post meta fields, taxonomy, comments, etc. associated |
|
5748 |
* with the attachment (except the main post). |
|
5749 |
* |
|
16 | 5750 |
* The attachment is moved to the Trash instead of permanently deleted unless Trash |
5751 |
* for media is disabled, item is already in the Trash, or $force_delete is true. |
|
0 | 5752 |
* |
5753 |
* @since 2.0.0 |
|
5 | 5754 |
* |
5755 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
5756 |
* |
|
5757 |
* @param int $post_id Attachment ID. |
|
16 | 5758 |
* @param bool $force_delete Optional. Whether to bypass Trash and force deletion. |
5 | 5759 |
* Default false. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5760 |
* @return WP_Post|false|null Post data on success, false or null on failure. |
0 | 5761 |
*/ |
5762 |
function wp_delete_attachment( $post_id, $force_delete = false ) { |
|
5763 |
global $wpdb; |
|
5764 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5765 |
$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
|
5766 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5767 |
if ( ! $post ) { |
0 | 5768 |
return $post; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5769 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5770 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5771 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5772 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5773 |
if ( 'attachment' !== $post->post_type ) { |
0 | 5774 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5775 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5776 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5777 |
if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) { |
0 | 5778 |
return wp_trash_post( $post_id ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5779 |
} |
0 | 5780 |
|
16 | 5781 |
/** |
5782 |
* Filters whether an attachment deletion should take place. |
|
5783 |
* |
|
5784 |
* @since 5.5.0 |
|
5785 |
* |
|
5786 |
* @param bool|null $delete Whether to go forward with deletion. |
|
5787 |
* @param WP_Post $post Post object. |
|
5788 |
* @param bool $force_delete Whether to bypass the Trash. |
|
5789 |
*/ |
|
5790 |
$check = apply_filters( 'pre_delete_attachment', null, $post, $force_delete ); |
|
5791 |
if ( null !== $check ) { |
|
5792 |
return $check; |
|
5793 |
} |
|
5794 |
||
9 | 5795 |
delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
5796 |
delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|
5797 |
||
5798 |
$meta = wp_get_attachment_metadata( $post_id ); |
|
0 | 5799 |
$backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
9 | 5800 |
$file = get_attached_file( $post_id ); |
5801 |
||
5802 |
if ( is_multisite() ) { |
|
0 | 5803 |
delete_transient( 'dirsize_cache' ); |
9 | 5804 |
} |
0 | 5805 |
|
5 | 5806 |
/** |
5807 |
* Fires before an attachment is deleted, at the start of wp_delete_attachment(). |
|
5808 |
* |
|
5809 |
* @since 2.0.0 |
|
16 | 5810 |
* @since 5.5.0 Added the `$post` parameter. |
5 | 5811 |
* |
16 | 5812 |
* @param int $post_id Attachment ID. |
5813 |
* @param WP_Post $post Post object. |
|
5 | 5814 |
*/ |
16 | 5815 |
do_action( 'delete_attachment', $post_id, $post ); |
0 | 5816 |
|
9 | 5817 |
wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) ); |
5818 |
wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); |
|
0 | 5819 |
|
5 | 5820 |
// Delete all for any posts. |
5821 |
delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); |
|
0 | 5822 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5823 |
wp_defer_comment_counting( true ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5824 |
|
9 | 5825 |
$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
|
5826 |
foreach ( $comment_ids as $comment_id ) { |
0 | 5827 |
wp_delete_comment( $comment_id, true ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5828 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5829 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5830 |
wp_defer_comment_counting( false ); |
0 | 5831 |
|
9 | 5832 |
$post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) ); |
5833 |
foreach ( $post_meta_ids as $mid ) { |
|
0 | 5834 |
delete_metadata_by_mid( 'post', $mid ); |
9 | 5835 |
} |
0 | 5836 |
|
5 | 5837 |
/** This action is documented in wp-includes/post.php */ |
16 | 5838 |
do_action( 'delete_post', $post_id, $post ); |
5 | 5839 |
$result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); |
5840 |
if ( ! $result ) { |
|
5841 |
return false; |
|
5842 |
} |
|
5843 |
/** This action is documented in wp-includes/post.php */ |
|
16 | 5844 |
do_action( 'deleted_post', $post_id, $post ); |
0 | 5845 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5846 |
wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); |
0 | 5847 |
|
5848 |
clean_post_cache( $post ); |
|
5849 |
||
5850 |
return $post; |
|
5851 |
} |
|
5852 |
||
5853 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5854 |
* Deletes all files that belong to the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5855 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5856 |
* @since 4.9.7 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5857 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5858 |
* @param int $post_id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5859 |
* @param array $meta The attachment's meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5860 |
* @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
|
5861 |
* @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
|
5862 |
* @return bool True on success, false on failure. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5863 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5864 |
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
|
5865 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5866 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5867 |
$uploadpath = wp_get_upload_dir(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5868 |
$deleted = true; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5869 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5870 |
if ( ! empty( $meta['thumb'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5871 |
// 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
|
5872 |
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 | 5873 |
$thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file ); |
16 | 5874 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5875 |
if ( ! empty( $thumbfile ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5876 |
$thumbfile = path_join( $uploadpath['basedir'], $thumbfile ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5877 |
$thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5878 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5879 |
if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5880 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5881 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5882 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5883 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5884 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5885 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5886 |
// Remove intermediate and backup images if there are any. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5887 |
if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5888 |
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
16 | 5889 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5890 |
foreach ( $meta['sizes'] as $size => $sizeinfo ) { |
9 | 5891 |
$intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file ); |
16 | 5892 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5893 |
if ( ! empty( $intermediate_file ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5894 |
$intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5895 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5896 |
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
|
5897 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5898 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5899 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5900 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5901 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5902 |
|
16 | 5903 |
if ( ! empty( $meta['original_image'] ) ) { |
5904 |
if ( empty( $intermediate_dir ) ) { |
|
5905 |
$intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
|
5906 |
} |
|
5907 |
||
5908 |
$original_image = str_replace( wp_basename( $file ), $meta['original_image'], $file ); |
|
5909 |
||
5910 |
if ( ! empty( $original_image ) ) { |
|
5911 |
$original_image = path_join( $uploadpath['basedir'], $original_image ); |
|
5912 |
||
5913 |
if ( ! wp_delete_file_from_directory( $original_image, $intermediate_dir ) ) { |
|
5914 |
$deleted = false; |
|
5915 |
} |
|
5916 |
} |
|
5917 |
} |
|
5918 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5919 |
if ( is_array( $backup_sizes ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5920 |
$del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) ); |
16 | 5921 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5922 |
foreach ( $backup_sizes as $size ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5923 |
$del_file = path_join( dirname( $meta['file'] ), $size['file'] ); |
16 | 5924 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5925 |
if ( ! empty( $del_file ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5926 |
$del_file = path_join( $uploadpath['basedir'], $del_file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5927 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5928 |
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
|
5929 |
$deleted = false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5930 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5931 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5932 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5933 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5934 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5935 |
if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5936 |
$deleted = false; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5939 |
return $deleted; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5940 |
} |
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 |
/** |
16 | 5943 |
* Retrieves attachment metadata for attachment ID. |
0 | 5944 |
* |
5945 |
* @since 2.1.0 |
|
5946 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5947 |
* @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
|
5948 |
* @param bool $unfiltered Optional. If true, filters are not run. Default false. |
16 | 5949 |
* @return array|false { |
5950 |
* Attachment metadata. False on failure. |
|
5951 |
* |
|
5952 |
* @type int $width The width of the attachment. |
|
5953 |
* @type int $height The height of the attachment. |
|
5954 |
* @type string $file The file path relative to `wp-content/uploads`. |
|
5955 |
* @type array $sizes Keys are size slugs, each value is an array containing |
|
5956 |
* 'file', 'width', 'height', and 'mime-type'. |
|
5957 |
* @type array $image_meta Image metadata. |
|
5958 |
* } |
|
0 | 5959 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5960 |
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
|
5961 |
$attachment_id = (int) $attachment_id; |
16 | 5962 |
|
5963 |
$post = get_post( $attachment_id ); |
|
5964 |
if ( ! $post ) { |
|
0 | 5965 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5966 |
} |
0 | 5967 |
|
5968 |
$data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); |
|
5969 |
||
9 | 5970 |
if ( $unfiltered ) { |
0 | 5971 |
return $data; |
9 | 5972 |
} |
0 | 5973 |
|
5 | 5974 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5975 |
* Filters the attachment meta data. |
5 | 5976 |
* |
5977 |
* @since 2.1.0 |
|
5978 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5979 |
* @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
|
5980 |
* if the object does not exist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5981 |
* @param int $attachment_id Attachment post ID. |
5 | 5982 |
*/ |
0 | 5983 |
return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); |
5984 |
} |
|
5985 |
||
5986 |
/** |
|
16 | 5987 |
* Updates metadata for an attachment. |
0 | 5988 |
* |
5989 |
* @since 2.1.0 |
|
5990 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5991 |
* @param int $attachment_id Attachment post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5992 |
* @param array $data Attachment meta data. |
5 | 5993 |
* @return int|bool False if $post is invalid. |
0 | 5994 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5995 |
function wp_update_attachment_metadata( $attachment_id, $data ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5996 |
$attachment_id = (int) $attachment_id; |
16 | 5997 |
|
5998 |
$post = get_post( $attachment_id ); |
|
5999 |
if ( ! $post ) { |
|
0 | 6000 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6001 |
} |
0 | 6002 |
|
5 | 6003 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6004 |
* Filters the updated attachment meta data. |
5 | 6005 |
* |
6006 |
* @since 2.1.0 |
|
6007 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6008 |
* @param array $data Array of updated attachment meta data. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6009 |
* @param int $attachment_id Attachment post ID. |
5 | 6010 |
*/ |
16 | 6011 |
$data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ); |
6012 |
if ( $data ) { |
|
0 | 6013 |
return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); |
9 | 6014 |
} else { |
0 | 6015 |
return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
9 | 6016 |
} |
0 | 6017 |
} |
6018 |
||
6019 |
/** |
|
6020 |
* Retrieve the URL for an attachment. |
|
6021 |
* |
|
6022 |
* @since 2.1.0 |
|
6023 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6024 |
* @global string $pagenow |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6025 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6026 |
* @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
|
6027 |
* @return string|false Attachment URL, otherwise false. |
0 | 6028 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6029 |
function wp_get_attachment_url( $attachment_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6030 |
$attachment_id = (int) $attachment_id; |
16 | 6031 |
|
6032 |
$post = get_post( $attachment_id ); |
|
6033 |
if ( ! $post ) { |
|
0 | 6034 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6035 |
} |
0 | 6036 |
|
16 | 6037 |
if ( 'attachment' !== $post->post_type ) { |
0 | 6038 |
return false; |
9 | 6039 |
} |
0 | 6040 |
|
6041 |
$url = ''; |
|
5 | 6042 |
// Get attached file. |
16 | 6043 |
$file = get_post_meta( $post->ID, '_wp_attached_file', true ); |
6044 |
if ( $file ) { |
|
5 | 6045 |
// Get upload directory. |
16 | 6046 |
$uploads = wp_get_upload_dir(); |
6047 |
if ( $uploads && false === $uploads['error'] ) { |
|
5 | 6048 |
// Check that the upload base exists in the file location. |
6049 |
if ( 0 === strpos( $file, $uploads['basedir'] ) ) { |
|
6050 |
// Replace file location with url location. |
|
9 | 6051 |
$url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); |
6052 |
} elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) { |
|
16 | 6053 |
// Get the directory name relative to the basedir (back compat for pre-2.7 uploads). |
9 | 6054 |
$url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); |
5 | 6055 |
} else { |
6056 |
// It's a newly-uploaded file, therefore $file is relative to the basedir. |
|
6057 |
$url = $uploads['baseurl'] . "/$file"; |
|
6058 |
} |
|
0 | 6059 |
} |
6060 |
} |
|
6061 |
||
5 | 6062 |
/* |
6063 |
* If any of the above options failed, Fallback on the GUID as used pre-2.7, |
|
6064 |
* not recommended to rely upon this. |
|
6065 |
*/ |
|
9 | 6066 |
if ( empty( $url ) ) { |
0 | 6067 |
$url = get_the_guid( $post->ID ); |
5 | 6068 |
} |
6069 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6070 |
// On SSL front end, URLs should be HTTPS. |
5 | 6071 |
if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) { |
6072 |
$url = set_url_scheme( $url ); |
|
6073 |
} |
|
6074 |
||
6075 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6076 |
* Filters the attachment URL. |
5 | 6077 |
* |
6078 |
* @since 2.1.0 |
|
6079 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6080 |
* @param string $url URL for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6081 |
* @param int $attachment_id Attachment post ID. |
5 | 6082 |
*/ |
0 | 6083 |
$url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
6084 |
||
9 | 6085 |
if ( empty( $url ) ) { |
0 | 6086 |
return false; |
9 | 6087 |
} |
0 | 6088 |
|
6089 |
return $url; |
|
6090 |
} |
|
6091 |
||
6092 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6093 |
* Retrieves the caption for an attachment. |
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 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6096 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6097 |
* @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
|
6098 |
* @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
|
6099 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6100 |
function wp_get_attachment_caption( $post_id = 0 ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6101 |
$post_id = (int) $post_id; |
16 | 6102 |
$post = get_post( $post_id ); |
6103 |
if ( ! $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6104 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6105 |
} |
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 |
if ( 'attachment' !== $post->post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6108 |
return false; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6109 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6110 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6111 |
$caption = $post->post_excerpt; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6112 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6113 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6114 |
* Filters the attachment caption. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6115 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6116 |
* @since 4.6.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6117 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6118 |
* @param string $caption Caption for the given attachment. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6119 |
* @param int $post_id Attachment ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6120 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6121 |
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
|
6122 |
} |
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 |
/** |
0 | 6125 |
* Retrieve thumbnail for an attachment. |
6126 |
* |
|
6127 |
* @since 2.1.0 |
|
6128 |
* |
|
5 | 6129 |
* @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
|
6130 |
* @return string|false False on failure. Thumbnail file path on success. |
0 | 6131 |
*/ |
6132 |
function wp_get_attachment_thumb_file( $post_id = 0 ) { |
|
6133 |
$post_id = (int) $post_id; |
|
16 | 6134 |
$post = get_post( $post_id ); |
6135 |
if ( ! $post ) { |
|
0 | 6136 |
return false; |
9 | 6137 |
} |
16 | 6138 |
|
6139 |
$imagedata = wp_get_attachment_metadata( $post->ID ); |
|
6140 |
if ( ! is_array( $imagedata ) ) { |
|
0 | 6141 |
return false; |
9 | 6142 |
} |
0 | 6143 |
|
6144 |
$file = get_attached_file( $post->ID ); |
|
6145 |
||
16 | 6146 |
if ( ! empty( $imagedata['thumb'] ) ) { |
6147 |
$thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ); |
|
6148 |
if ( file_exists( $thumbfile ) ) { |
|
6149 |
/** |
|
6150 |
* Filters the attachment thumbnail file path. |
|
6151 |
* |
|
6152 |
* @since 2.1.0 |
|
6153 |
* |
|
6154 |
* @param string $thumbfile File path to the attachment thumbnail. |
|
6155 |
* @param int $post_id Attachment ID. |
|
6156 |
*/ |
|
6157 |
return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
|
6158 |
} |
|
5 | 6159 |
} |
0 | 6160 |
return false; |
6161 |
} |
|
6162 |
||
6163 |
/** |
|
6164 |
* Retrieve URL for an attachment thumbnail. |
|
6165 |
* |
|
6166 |
* @since 2.1.0 |
|
6167 |
* |
|
5 | 6168 |
* @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
|
6169 |
* @return string|false False on failure. Thumbnail URL on success. |
0 | 6170 |
*/ |
6171 |
function wp_get_attachment_thumb_url( $post_id = 0 ) { |
|
6172 |
$post_id = (int) $post_id; |
|
16 | 6173 |
$post = get_post( $post_id ); |
6174 |
if ( ! $post ) { |
|
0 | 6175 |
return false; |
9 | 6176 |
} |
16 | 6177 |
|
6178 |
$url = wp_get_attachment_url( $post->ID ); |
|
6179 |
if ( ! $url ) { |
|
0 | 6180 |
return false; |
9 | 6181 |
} |
0 | 6182 |
|
6183 |
$sized = image_downsize( $post_id, 'thumbnail' ); |
|
9 | 6184 |
if ( $sized ) { |
0 | 6185 |
return $sized[0]; |
9 | 6186 |
} |
6187 |
||
16 | 6188 |
$thumb = wp_get_attachment_thumb_file( $post->ID ); |
6189 |
if ( ! $thumb ) { |
|
0 | 6190 |
return false; |
9 | 6191 |
} |
6192 |
||
6193 |
$url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url ); |
|
0 | 6194 |
|
5 | 6195 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6196 |
* Filters the attachment thumbnail URL. |
5 | 6197 |
* |
6198 |
* @since 2.1.0 |
|
6199 |
* |
|
6200 |
* @param string $url URL for the attachment thumbnail. |
|
6201 |
* @param int $post_id Attachment ID. |
|
6202 |
*/ |
|
0 | 6203 |
return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
6204 |
} |
|
6205 |
||
6206 |
/** |
|
5 | 6207 |
* Verifies an attachment is of a given type. |
6208 |
* |
|
6209 |
* @since 4.2.0 |
|
6210 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6211 |
* @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
|
6212 |
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
5 | 6213 |
* @return bool True if one of the accepted types, false otherwise. |
6214 |
*/ |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6215 |
function wp_attachment_is( $type, $post = null ) { |
16 | 6216 |
$post = get_post( $post ); |
6217 |
if ( ! $post ) { |
|
5 | 6218 |
return false; |
6219 |
} |
|
6220 |
||
16 | 6221 |
$file = get_attached_file( $post->ID ); |
6222 |
if ( ! $file ) { |
|
5 | 6223 |
return false; |
6224 |
} |
|
6225 |
||
6226 |
if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { |
|
6227 |
return true; |
|
6228 |
} |
|
6229 |
||
6230 |
$check = wp_check_filetype( $file ); |
|
6231 |
if ( empty( $check['ext'] ) ) { |
|
6232 |
return false; |
|
6233 |
} |
|
6234 |
||
6235 |
$ext = $check['ext']; |
|
6236 |
||
6237 |
if ( 'import' !== $post->post_mime_type ) { |
|
6238 |
return $type === $ext; |
|
6239 |
} |
|
6240 |
||
6241 |
switch ( $type ) { |
|
9 | 6242 |
case 'image': |
6243 |
$image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); |
|
16 | 6244 |
return in_array( $ext, $image_exts, true ); |
9 | 6245 |
|
6246 |
case 'audio': |
|
16 | 6247 |
return in_array( $ext, wp_get_audio_extensions(), true ); |
9 | 6248 |
|
6249 |
case 'video': |
|
16 | 6250 |
return in_array( $ext, wp_get_video_extensions(), true ); |
9 | 6251 |
|
6252 |
default: |
|
6253 |
return $type === $ext; |
|
5 | 6254 |
} |
6255 |
} |
|
6256 |
||
6257 |
/** |
|
9 | 6258 |
* Determines whether an attachment is an image. |
6259 |
* |
|
6260 |
* For more information on this and similar theme functions, check out |
|
6261 |
* the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|
6262 |
* Conditional Tags} article in the Theme Developer Handbook. |
|
0 | 6263 |
* |
6264 |
* @since 2.1.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6265 |
* @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
|
6266 |
* allowed WP_Post object to be passed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6267 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6268 |
* @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
5 | 6269 |
* @return bool Whether the attachment is an image. |
0 | 6270 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6271 |
function wp_attachment_is_image( $post = null ) { |
5 | 6272 |
return wp_attachment_is( 'image', $post ); |
0 | 6273 |
} |
6274 |
||
6275 |
/** |
|
16 | 6276 |
* Retrieve the icon for a MIME type or attachment. |
0 | 6277 |
* |
6278 |
* @since 2.1.0 |
|
6279 |
* |
|
6280 |
* @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
|
6281 |
* @return string|false Icon, false otherwise. |
0 | 6282 |
*/ |
6283 |
function wp_mime_type_icon( $mime = 0 ) { |
|
9 | 6284 |
if ( ! is_numeric( $mime ) ) { |
6285 |
$icon = wp_cache_get( "mime_type_icon_$mime" ); |
|
6286 |
} |
|
0 | 6287 |
|
6288 |
$post_id = 0; |
|
9 | 6289 |
if ( empty( $icon ) ) { |
0 | 6290 |
$post_mimes = array(); |
9 | 6291 |
if ( is_numeric( $mime ) ) { |
0 | 6292 |
$mime = (int) $mime; |
16 | 6293 |
$post = get_post( $mime ); |
6294 |
if ( $post ) { |
|
0 | 6295 |
$post_id = (int) $post->ID; |
9 | 6296 |
$file = get_attached_file( $post_id ); |
6297 |
$ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file ); |
|
6298 |
if ( ! empty( $ext ) ) { |
|
0 | 6299 |
$post_mimes[] = $ext; |
16 | 6300 |
$ext_type = wp_ext2type( $ext ); |
6301 |
if ( $ext_type ) { |
|
0 | 6302 |
$post_mimes[] = $ext_type; |
9 | 6303 |
} |
0 | 6304 |
} |
6305 |
$mime = $post->post_mime_type; |
|
6306 |
} else { |
|
6307 |
$mime = 0; |
|
6308 |
} |
|
6309 |
} else { |
|
6310 |
$post_mimes[] = $mime; |
|
6311 |
} |
|
6312 |
||
9 | 6313 |
$icon_files = wp_cache_get( 'icon_files' ); |
6314 |
||
6315 |
if ( ! is_array( $icon_files ) ) { |
|
5 | 6316 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6317 |
* Filters the icon directory path. |
5 | 6318 |
* |
6319 |
* @since 2.0.0 |
|
6320 |
* |
|
6321 |
* @param string $path Icon directory absolute path. |
|
6322 |
*/ |
|
6323 |
$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); |
|
6324 |
||
6325 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6326 |
* Filters the icon directory URI. |
5 | 6327 |
* |
6328 |
* @since 2.0.0 |
|
6329 |
* |
|
6330 |
* @param string $uri Icon directory URI. |
|
6331 |
*/ |
|
6332 |
$icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); |
|
6333 |
||
6334 |
/** |
|
16 | 6335 |
* Filters the array of icon directory URIs. |
5 | 6336 |
* |
6337 |
* @since 2.5.0 |
|
6338 |
* |
|
16 | 6339 |
* @param string[] $uris Array of icon directory URIs keyed by directory absolute path. |
5 | 6340 |
*/ |
9 | 6341 |
$dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); |
0 | 6342 |
$icon_files = array(); |
6343 |
while ( $dirs ) { |
|
6344 |
$keys = array_keys( $dirs ); |
|
9 | 6345 |
$dir = array_shift( $keys ); |
6346 |
$uri = array_shift( $dirs ); |
|
16 | 6347 |
$dh = opendir( $dir ); |
6348 |
if ( $dh ) { |
|
9 | 6349 |
while ( false !== $file = readdir( $dh ) ) { |
6350 |
$file = wp_basename( $file ); |
|
16 | 6351 |
if ( '.' === substr( $file, 0, 1 ) ) { |
0 | 6352 |
continue; |
6353 |
} |
|
16 | 6354 |
|
6355 |
$ext = strtolower( substr( $file, -4 ) ); |
|
6356 |
if ( ! in_array( $ext, array( '.png', '.gif', '.jpg' ), true ) ) { |
|
9 | 6357 |
if ( is_dir( "$dir/$file" ) ) { |
6358 |
$dirs[ "$dir/$file" ] = "$uri/$file"; |
|
6359 |
} |
|
6360 |
continue; |
|
6361 |
} |
|
6362 |
$icon_files[ "$dir/$file" ] = "$uri/$file"; |
|
0 | 6363 |
} |
9 | 6364 |
closedir( $dh ); |
0 | 6365 |
} |
6366 |
} |
|
6367 |
wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); |
|
6368 |
} |
|
6369 |
||
5 | 6370 |
$types = array(); |
9 | 6371 |
// Icon wp_basename - extension = MIME wildcard. |
6372 |
foreach ( $icon_files as $file => $uri ) { |
|
6373 |
$types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ]; |
|
0 | 6374 |
} |
6375 |
||
9 | 6376 |
if ( ! empty( $mime ) ) { |
6377 |
$post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) ); |
|
6378 |
$post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 ); |
|
6379 |
$post_mimes[] = str_replace( '/', '_', $mime ); |
|
6380 |
} |
|
6381 |
||
6382 |
$matches = wp_match_mime_types( array_keys( $types ), $post_mimes ); |
|
6383 |
$matches['default'] = array( 'default' ); |
|
0 | 6384 |
|
6385 |
foreach ( $matches as $match => $wilds ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6386 |
foreach ( $wilds as $wild ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6387 |
if ( ! isset( $types[ $wild ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6388 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6389 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6390 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6391 |
$icon = $types[ $wild ]; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6392 |
if ( ! is_numeric( $mime ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6393 |
wp_cache_add( "mime_type_icon_$mime", $icon ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6394 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6395 |
break 2; |
0 | 6396 |
} |
6397 |
} |
|
6398 |
} |
|
6399 |
||
5 | 6400 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6401 |
* Filters the mime type icon. |
5 | 6402 |
* |
6403 |
* @since 2.1.0 |
|
6404 |
* |
|
6405 |
* @param string $icon Path to the mime type icon. |
|
6406 |
* @param string $mime Mime type. |
|
6407 |
* @param int $post_id Attachment ID. Will equal 0 if the function passed |
|
6408 |
* the mime type. |
|
6409 |
*/ |
|
6410 |
return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); |
|
0 | 6411 |
} |
6412 |
||
6413 |
/** |
|
5 | 6414 |
* Check for changed slugs for published post objects and save the old slug. |
0 | 6415 |
* |
6416 |
* The function is used when a post object of any type is updated, |
|
6417 |
* by comparing the current and previous post objects. |
|
6418 |
* |
|
6419 |
* If the slug was changed and not already part of the old slugs then it will be |
|
6420 |
* added to the post meta field ('_wp_old_slug') for storing old slugs for that |
|
6421 |
* post. |
|
6422 |
* |
|
6423 |
* The most logically usage of this function is redirecting changed post objects, so |
|
6424 |
* that those that linked to an changed post will be redirected to the new post. |
|
6425 |
* |
|
6426 |
* @since 2.1.0 |
|
6427 |
* |
|
5 | 6428 |
* @param int $post_id Post ID. |
6429 |
* @param WP_Post $post The Post Object |
|
6430 |
* @param WP_Post $post_before The Previous Post Object |
|
0 | 6431 |
*/ |
5 | 6432 |
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
|
6433 |
// Don't bother if it hasn't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6434 |
if ( $post->post_name == $post_before->post_name ) { |
0 | 6435 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6436 |
} |
0 | 6437 |
|
5 | 6438 |
// 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
|
6439 |
if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { |
0 | 6440 |
return; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6441 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6442 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6443 |
$old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); |
0 | 6444 |
|
5 | 6445 |
// If we haven't added this old slug before, add it now. |
16 | 6446 |
if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6447 |
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
|
6448 |
} |
0 | 6449 |
|
5 | 6450 |
// If the new slug was used previously, delete it from the list. |
16 | 6451 |
if ( in_array( $post->post_name, $old_slugs, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6452 |
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
|
6453 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6454 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6455 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6456 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6457 |
* 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
|
6458 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6459 |
* 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
|
6460 |
* by comparing the current and previous post objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6461 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6462 |
* 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
|
6463 |
* 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
|
6464 |
* post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6465 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6466 |
* 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
|
6467 |
* 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
|
6468 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6469 |
* @since 4.9.3 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6470 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6471 |
* @param int $post_id Post ID. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6472 |
* @param WP_Post $post The Post Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6473 |
* @param WP_Post $post_before The Previous Post Object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6474 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6475 |
function wp_check_for_changed_dates( $post_id, $post, $post_before ) { |
16 | 6476 |
$previous_date = gmdate( 'Y-m-d', strtotime( $post_before->post_date ) ); |
6477 |
$new_date = gmdate( 'Y-m-d', strtotime( $post->post_date ) ); |
|
6478 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6479 |
// Don't bother if it hasn't changed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6480 |
if ( $new_date == $previous_date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6481 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6482 |
} |
16 | 6483 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6484 |
// We're only concerned with published, non-hierarchical objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6485 |
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
|
6486 |
return; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6487 |
} |
16 | 6488 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6489 |
$old_dates = (array) get_post_meta( $post_id, '_wp_old_date' ); |
16 | 6490 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6491 |
// If we haven't added this old date before, add it now. |
16 | 6492 |
if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6493 |
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
|
6494 |
} |
16 | 6495 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6496 |
// If the new slug was used previously, delete it from the list. |
16 | 6497 |
if ( in_array( $new_date, $old_dates, true ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6498 |
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
|
6499 |
} |
0 | 6500 |
} |
6501 |
||
6502 |
/** |
|
6503 |
* Retrieve the private post SQL based on capability. |
|
6504 |
* |
|
6505 |
* This function provides a standardized way to appropriately select on the |
|
6506 |
* post_status of a post type. The function will return a piece of SQL code |
|
6507 |
* that can be added to a WHERE clause; this SQL is constructed to allow all |
|
6508 |
* published posts, and all private posts to which the user has access. |
|
6509 |
* |
|
6510 |
* @since 2.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6511 |
* @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
|
6512 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6513 |
* @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. |
0 | 6514 |
* @return string SQL code that can be added to a where clause. |
6515 |
*/ |
|
6516 |
function get_private_posts_cap_sql( $post_type ) { |
|
6517 |
return get_posts_by_author_sql( $post_type, false ); |
|
6518 |
} |
|
6519 |
||
6520 |
/** |
|
6521 |
* Retrieve the post SQL based on capability, author, and type. |
|
6522 |
* |
|
6523 |
* @since 3.0.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6524 |
* @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. |
5 | 6525 |
* |
6526 |
* @see get_private_posts_cap_sql() |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6527 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6528 |
* |
16 | 6529 |
* @param string|string[] $post_type Single post type or an array of post types. |
6530 |
* @param bool $full Optional. Returns a full WHERE statement instead of just |
|
6531 |
* an 'andalso' term. Default true. |
|
6532 |
* @param int $post_author Optional. Query posts having a single author ID. Default null. |
|
6533 |
* @param bool $public_only Optional. Only return public posts. Skips cap checks for |
|
6534 |
* $current_user. Default false. |
|
0 | 6535 |
* @return string SQL WHERE code that can be added to a query. |
6536 |
*/ |
|
6537 |
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { |
|
6538 |
global $wpdb; |
|
6539 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6540 |
if ( is_array( $post_type ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6541 |
$post_types = $post_type; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6542 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6543 |
$post_types = array( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6544 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6545 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6546 |
$post_type_clauses = array(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6547 |
foreach ( $post_types as $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6548 |
$post_type_obj = get_post_type_object( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6549 |
if ( ! $post_type_obj ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6550 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6551 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6552 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6553 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6554 |
* 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
|
6555 |
* when generating SQL for getting posts by author. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6556 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6557 |
* @since 2.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6558 |
* @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
|
6559 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6560 |
* @param string $cap Capability. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6561 |
*/ |
16 | 6562 |
$cap = apply_filters_deprecated( 'pub_priv_sql_capability', array( '' ), '3.2.0' ); |
6563 |
if ( ! $cap ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6564 |
$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
|
6565 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6566 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6567 |
// 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
|
6568 |
$post_status_sql = "post_status = 'publish'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6569 |
if ( false === $public_only ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6570 |
if ( $cap ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6571 |
// 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
|
6572 |
$post_status_sql .= " OR post_status = 'private'"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6573 |
} elseif ( is_user_logged_in() ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6574 |
// Users can view their own private posts. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6575 |
$id = get_current_user_id(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6576 |
if ( null === $post_author || ! $full ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6577 |
$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
|
6578 |
} elseif ( $id == (int) $post_author ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6579 |
$post_status_sql .= " OR post_status = 'private'"; |
16 | 6580 |
} // Else none. |
6581 |
} // Else none. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6582 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6583 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6584 |
$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
|
6585 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6586 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6587 |
if ( empty( $post_type_clauses ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6588 |
return $full ? 'WHERE 1 = 0' : '1 = 0'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6589 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6590 |
|
9 | 6591 |
$sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )'; |
5 | 6592 |
|
6593 |
if ( null !== $post_author ) { |
|
6594 |
$sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); |
|
6595 |
} |
|
6596 |
||
6597 |
if ( $full ) { |
|
6598 |
$sql = 'WHERE ' . $sql; |
|
6599 |
} |
|
0 | 6600 |
|
6601 |
return $sql; |
|
6602 |
} |
|
6603 |
||
6604 |
/** |
|
16 | 6605 |
* Retrieves the most recent time that a post on the site was published. |
0 | 6606 |
* |
6607 |
* The server timezone is the default and is the difference between GMT and |
|
16 | 6608 |
* server time. The 'blog' value is the date when the last post was posted. |
6609 |
* The 'gmt' is when the last post was posted in GMT formatted date. |
|
0 | 6610 |
* |
6611 |
* @since 0.71 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6612 |
* @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
|
6613 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6614 |
* @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
|
6615 |
* 'server' uses the server's internal timezone. |
16 | 6616 |
* 'blog' uses the `post_date` field, which proxies to the timezone set for the site. |
6617 |
* 'gmt' uses the `post_date_gmt` field. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6618 |
* Default 'server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6619 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
16 | 6620 |
* @return string The date of the last post, or false on failure. |
0 | 6621 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6622 |
function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { |
16 | 6623 |
$lastpostdate = _get_last_post_time( $timezone, 'date', $post_type ); |
6624 |
||
5 | 6625 |
/** |
16 | 6626 |
* Filters the most recent time that a post on the site was published. |
5 | 6627 |
* |
6628 |
* @since 2.3.0 |
|
16 | 6629 |
* @since 5.5.0 Added the `$post_type` parameter. |
5 | 6630 |
* |
16 | 6631 |
* @param string|false $lastpostdate The most recent time that a post was published, |
6632 |
* in 'Y-m-d H:i:s' format. False on failure. |
|
6633 |
* @param string $timezone Location to use for getting the post published date. |
|
6634 |
* See get_lastpostdate() for accepted `$timezone` values. |
|
6635 |
* @param string $post_type The post type to check. |
|
5 | 6636 |
*/ |
16 | 6637 |
return apply_filters( 'get_lastpostdate', $lastpostdate, $timezone, $post_type ); |
6638 |
} |
|
6639 |
||
6640 |
/** |
|
6641 |
* Get the most recent time that a post on the site was modified. |
|
0 | 6642 |
* |
6643 |
* The server timezone is the default and is the difference between GMT and |
|
16 | 6644 |
* server time. The 'blog' value is just when the last post was modified. |
6645 |
* The 'gmt' is when the last post was modified in GMT time. |
|
0 | 6646 |
* |
6647 |
* @since 1.2.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6648 |
* @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
|
6649 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6650 |
* @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
|
6651 |
* for information on accepted values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6652 |
* Default 'server'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6653 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
16 | 6654 |
* @return string The timestamp in 'Y-m-d H:i:s' format, or false on failure. |
0 | 6655 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6656 |
function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6657 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6658 |
* 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
|
6659 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6660 |
* @since 4.4.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6661 |
* |
16 | 6662 |
* @param string|false $lastpostmodified The most recent time that a post was modified, |
6663 |
* in 'Y-m-d H:i:s' format, or false. Returning anything |
|
6664 |
* other than false will short-circuit the function. |
|
6665 |
* @param string $timezone Location to use for getting the post modified date. |
|
6666 |
* See get_lastpostdate() for accepted `$timezone` values. |
|
6667 |
* @param string $post_type The post type to check. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6668 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6669 |
$lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); |
16 | 6670 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6671 |
if ( false !== $lastpostmodified ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6672 |
return $lastpostmodified; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6673 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6674 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6675 |
$lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); |
16 | 6676 |
$lastpostdate = get_lastpostdate( $timezone, $post_type ); |
6677 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6678 |
if ( $lastpostdate > $lastpostmodified ) { |
0 | 6679 |
$lastpostmodified = $lastpostdate; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6680 |
} |
0 | 6681 |
|
5 | 6682 |
/** |
16 | 6683 |
* Filters the most recent time that a post on the site was modified. |
5 | 6684 |
* |
6685 |
* @since 2.3.0 |
|
16 | 6686 |
* @since 5.5.0 Added the `$post_type` parameter. |
5 | 6687 |
* |
16 | 6688 |
* @param string|false $lastpostmodified The most recent time that a post was modified, |
6689 |
* in 'Y-m-d H:i:s' format. False on failure. |
|
6690 |
* @param string $timezone Location to use for getting the post modified date. |
|
6691 |
* See get_lastpostdate() for accepted `$timezone` values. |
|
6692 |
* @param string $post_type The post type to check. |
|
5 | 6693 |
*/ |
16 | 6694 |
return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone, $post_type ); |
6695 |
} |
|
6696 |
||
6697 |
/** |
|
6698 |
* Gets the timestamp of the last time any post was modified or published. |
|
5 | 6699 |
* |
0 | 6700 |
* @since 3.1.0 |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6701 |
* @since 4.4.0 The `$post_type` argument was added. |
5 | 6702 |
* @access private |
6703 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6704 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6705 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6706 |
* @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
|
6707 |
* for information on accepted values. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6708 |
* @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
|
6709 |
* @param string $post_type Optional. The post type to check. Default 'any'. |
16 | 6710 |
* @return string|false The timestamp in 'Y-m-d H:i:s' format, or false on failure. |
0 | 6711 |
*/ |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6712 |
function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { |
0 | 6713 |
global $wpdb; |
6714 |
||
16 | 6715 |
if ( ! in_array( $field, array( 'date', 'modified' ), true ) ) { |
0 | 6716 |
return false; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6717 |
} |
0 | 6718 |
|
6719 |
$timezone = strtolower( $timezone ); |
|
6720 |
||
6721 |
$key = "lastpost{$field}:$timezone"; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6722 |
if ( 'any' !== $post_type ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6723 |
$key .= ':' . sanitize_key( $post_type ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6724 |
} |
0 | 6725 |
|
6726 |
$date = wp_cache_get( $key, 'timeinfo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6727 |
if ( false !== $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6728 |
return $date; |
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 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6731 |
if ( 'any' === $post_type ) { |
0 | 6732 |
$post_types = get_post_types( array( 'public' => true ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6733 |
array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); |
0 | 6734 |
$post_types = "'" . implode( "', '", $post_types ) . "'"; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6735 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6736 |
$post_types = "'" . sanitize_key( $post_type ) . "'"; |
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 |
switch ( $timezone ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6740 |
case 'gmt': |
9 | 6741 |
$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
|
6742 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6743 |
case 'blog': |
9 | 6744 |
$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
|
6745 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6746 |
case 'server': |
16 | 6747 |
$add_seconds_server = gmdate( 'Z' ); |
9 | 6748 |
$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
|
6749 |
break; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6750 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6751 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6752 |
if ( $date ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6753 |
wp_cache_set( $key, $date, 'timeinfo' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6754 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6755 |
return $date; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6756 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6757 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6758 |
return false; |
0 | 6759 |
} |
6760 |
||
6761 |
/** |
|
6762 |
* Updates posts in cache. |
|
6763 |
* |
|
6764 |
* @since 1.5.1 |
|
6765 |
* |
|
16 | 6766 |
* @param WP_Post[] $posts Array of post objects (passed by reference). |
0 | 6767 |
*/ |
6768 |
function update_post_cache( &$posts ) { |
|
9 | 6769 |
if ( ! $posts ) { |
0 | 6770 |
return; |
9 | 6771 |
} |
6772 |
||
6773 |
foreach ( $posts as $post ) { |
|
0 | 6774 |
wp_cache_add( $post->ID, $post, 'posts' ); |
9 | 6775 |
} |
0 | 6776 |
} |
6777 |
||
6778 |
/** |
|
6779 |
* Will clean the post in the cache. |
|
6780 |
* |
|
6781 |
* Cleaning means delete from the cache of the post. Will call to clean the term |
|
6782 |
* object cache associated with the post ID. |
|
6783 |
* |
|
6784 |
* This function not run if $_wp_suspend_cache_invalidation is not empty. See |
|
6785 |
* wp_suspend_cache_invalidation(). |
|
6786 |
* |
|
6787 |
* @since 2.0.0 |
|
6788 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6789 |
* @global bool $_wp_suspend_cache_invalidation |
5 | 6790 |
* |
6791 |
* @param int|WP_Post $post Post ID or post object to remove from the cache. |
|
0 | 6792 |
*/ |
6793 |
function clean_post_cache( $post ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6794 |
global $_wp_suspend_cache_invalidation; |
0 | 6795 |
|
9 | 6796 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
0 | 6797 |
return; |
9 | 6798 |
} |
0 | 6799 |
|
6800 |
$post = get_post( $post ); |
|
9 | 6801 |
if ( empty( $post ) ) { |
0 | 6802 |
return; |
9 | 6803 |
} |
0 | 6804 |
|
6805 |
wp_cache_delete( $post->ID, 'posts' ); |
|
6806 |
wp_cache_delete( $post->ID, 'post_meta' ); |
|
6807 |
||
6808 |
clean_object_term_cache( $post->ID, $post->post_type ); |
|
6809 |
||
6810 |
wp_cache_delete( 'wp_get_archives', 'general' ); |
|
6811 |
||
5 | 6812 |
/** |
6813 |
* Fires immediately after the given post's cache is cleaned. |
|
6814 |
* |
|
6815 |
* @since 2.5.0 |
|
6816 |
* |
|
6817 |
* @param int $post_id Post ID. |
|
6818 |
* @param WP_Post $post Post object. |
|
6819 |
*/ |
|
0 | 6820 |
do_action( 'clean_post_cache', $post->ID, $post ); |
6821 |
||
16 | 6822 |
if ( 'page' === $post->post_type ) { |
0 | 6823 |
wp_cache_delete( 'all_page_ids', 'posts' ); |
5 | 6824 |
|
6825 |
/** |
|
6826 |
* Fires immediately after the given page's cache is cleaned. |
|
6827 |
* |
|
6828 |
* @since 2.5.0 |
|
6829 |
* |
|
6830 |
* @param int $post_id Post ID. |
|
6831 |
*/ |
|
0 | 6832 |
do_action( 'clean_page_cache', $post->ID ); |
6833 |
} |
|
6834 |
||
6835 |
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
6836 |
} |
|
6837 |
||
6838 |
/** |
|
6839 |
* Call major cache updating functions for list of Post objects. |
|
6840 |
* |
|
6841 |
* @since 1.5.0 |
|
6842 |
* |
|
16 | 6843 |
* @param WP_Post[] $posts Array of Post objects |
6844 |
* @param string $post_type Optional. Post type. Default 'post'. |
|
6845 |
* @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
|
6846 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|
0 | 6847 |
*/ |
5 | 6848 |
function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { |
0 | 6849 |
// No point in doing all this work if we didn't match any posts. |
9 | 6850 |
if ( ! $posts ) { |
0 | 6851 |
return; |
9 | 6852 |
} |
6853 |
||
6854 |
update_post_cache( $posts ); |
|
0 | 6855 |
|
6856 |
$post_ids = array(); |
|
9 | 6857 |
foreach ( $posts as $post ) { |
0 | 6858 |
$post_ids[] = $post->ID; |
9 | 6859 |
} |
6860 |
||
6861 |
if ( ! $post_type ) { |
|
0 | 6862 |
$post_type = 'any'; |
9 | 6863 |
} |
0 | 6864 |
|
6865 |
if ( $update_term_cache ) { |
|
9 | 6866 |
if ( is_array( $post_type ) ) { |
0 | 6867 |
$ptypes = $post_type; |
16 | 6868 |
} elseif ( 'any' === $post_type ) { |
5 | 6869 |
$ptypes = array(); |
0 | 6870 |
// Just use the post_types in the supplied posts. |
5 | 6871 |
foreach ( $posts as $post ) { |
0 | 6872 |
$ptypes[] = $post->post_type; |
5 | 6873 |
} |
9 | 6874 |
$ptypes = array_unique( $ptypes ); |
0 | 6875 |
} else { |
9 | 6876 |
$ptypes = array( $post_type ); |
0 | 6877 |
} |
6878 |
||
9 | 6879 |
if ( ! empty( $ptypes ) ) { |
6880 |
update_object_term_cache( $post_ids, $ptypes ); |
|
6881 |
} |
|
6882 |
} |
|
6883 |
||
6884 |
if ( $update_meta_cache ) { |
|
6885 |
update_postmeta_cache( $post_ids ); |
|
6886 |
} |
|
0 | 6887 |
} |
6888 |
||
6889 |
/** |
|
6890 |
* Updates metadata cache for list of post IDs. |
|
6891 |
* |
|
6892 |
* Performs SQL query to retrieve the metadata for the post IDs and updates the |
|
6893 |
* metadata cache for the posts. Therefore, the functions, which call this |
|
6894 |
* function, do not need to perform SQL queries on their own. |
|
6895 |
* |
|
6896 |
* @since 2.1.0 |
|
6897 |
* |
|
16 | 6898 |
* @param int[] $post_ids Array of post IDs. |
6899 |
* @return array|false An array of metadata on success, false if there is nothing to update. |
|
0 | 6900 |
*/ |
5 | 6901 |
function update_postmeta_cache( $post_ids ) { |
9 | 6902 |
return update_meta_cache( 'post', $post_ids ); |
0 | 6903 |
} |
6904 |
||
6905 |
/** |
|
6906 |
* Will clean the attachment in the cache. |
|
6907 |
* |
|
6908 |
* Cleaning means delete from the cache. Optionally will clean the term |
|
6909 |
* object cache associated with the attachment ID. |
|
6910 |
* |
|
5 | 6911 |
* This function will not run if $_wp_suspend_cache_invalidation is not empty. |
6912 |
* |
|
0 | 6913 |
* @since 3.0.0 |
6914 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6915 |
* @global bool $_wp_suspend_cache_invalidation |
5 | 6916 |
* |
6917 |
* @param int $id The attachment ID in the cache to clean. |
|
6918 |
* @param bool $clean_terms Optional. Whether to clean terms cache. Default false. |
|
0 | 6919 |
*/ |
5 | 6920 |
function clean_attachment_cache( $id, $clean_terms = false ) { |
0 | 6921 |
global $_wp_suspend_cache_invalidation; |
6922 |
||
9 | 6923 |
if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
0 | 6924 |
return; |
9 | 6925 |
} |
0 | 6926 |
|
6927 |
$id = (int) $id; |
|
6928 |
||
9 | 6929 |
wp_cache_delete( $id, 'posts' ); |
6930 |
wp_cache_delete( $id, 'post_meta' ); |
|
6931 |
||
6932 |
if ( $clean_terms ) { |
|
6933 |
clean_object_term_cache( $id, 'attachment' ); |
|
6934 |
} |
|
0 | 6935 |
|
5 | 6936 |
/** |
6937 |
* Fires after the given attachment's cache is cleaned. |
|
6938 |
* |
|
6939 |
* @since 3.0.0 |
|
6940 |
* |
|
6941 |
* @param int $id Attachment ID. |
|
6942 |
*/ |
|
6943 |
do_action( 'clean_attachment_cache', $id ); |
|
0 | 6944 |
} |
6945 |
||
6946 |
// |
|
16 | 6947 |
// Hooks. |
0 | 6948 |
// |
6949 |
||
6950 |
/** |
|
6951 |
* Hook for managing future post transitions to published. |
|
6952 |
* |
|
6953 |
* @since 2.3.0 |
|
6954 |
* @access private |
|
5 | 6955 |
* |
6956 |
* @see wp_clear_scheduled_hook() |
|
6957 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
6958 |
* |
|
6959 |
* @param string $new_status New post status. |
|
6960 |
* @param string $old_status Previous post status. |
|
6961 |
* @param WP_Post $post Post object. |
|
0 | 6962 |
*/ |
5 | 6963 |
function _transition_post_status( $new_status, $old_status, $post ) { |
0 | 6964 |
global $wpdb; |
6965 |
||
16 | 6966 |
if ( 'publish' !== $old_status && 'publish' === $new_status ) { |
5 | 6967 |
// Reset GUID if transitioning to publish and it is empty. |
16 | 6968 |
if ( '' === get_the_guid( $post->ID ) ) { |
0 | 6969 |
$wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); |
9 | 6970 |
} |
5 | 6971 |
|
6972 |
/** |
|
6973 |
* Fires when a post's status is transitioned from private to published. |
|
6974 |
* |
|
6975 |
* @since 1.5.0 |
|
16 | 6976 |
* @deprecated 2.3.0 Use {@see 'private_to_publish'} instead. |
5 | 6977 |
* |
6978 |
* @param int $post_id Post ID. |
|
6979 |
*/ |
|
16 | 6980 |
do_action_deprecated( 'private_to_published', array( $post->ID ), '2.3.0', 'private_to_publish' ); |
0 | 6981 |
} |
6982 |
||
5 | 6983 |
// If published posts changed clear the lastpostmodified cache. |
16 | 6984 |
if ( 'publish' === $new_status || 'publish' === $old_status ) { |
0 | 6985 |
foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { |
6986 |
wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); |
|
6987 |
wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6988 |
wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); |
0 | 6989 |
} |
6990 |
} |
|
6991 |
||
5 | 6992 |
if ( $new_status !== $old_status ) { |
6993 |
wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); |
|
6994 |
wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); |
|
6995 |
} |
|
6996 |
||
0 | 6997 |
// Always clears the hook in case the post status bounced from future to draft. |
9 | 6998 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
0 | 6999 |
} |
7000 |
||
7001 |
/** |
|
7002 |
* Hook used to schedule publication for a post marked for the future. |
|
7003 |
* |
|
7004 |
* The $post properties used and must exist are 'ID' and 'post_date_gmt'. |
|
7005 |
* |
|
7006 |
* @since 2.3.0 |
|
7007 |
* @access private |
|
7008 |
* |
|
5 | 7009 |
* @param int $deprecated Not used. Can be set to null. Never implemented. Not marked |
7010 |
* 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
|
7011 |
* wp_transition_post_status() and the default filter for _future_post_hook(). |
5 | 7012 |
* @param WP_Post $post Post object. |
0 | 7013 |
*/ |
7014 |
function _future_post_hook( $deprecated, $post ) { |
|
7015 |
wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
|
9 | 7016 |
wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) ); |
0 | 7017 |
} |
7018 |
||
7019 |
/** |
|
7020 |
* Hook to schedule pings and enclosures when a post is published. |
|
7021 |
* |
|
5 | 7022 |
* Uses XMLRPC_REQUEST and WP_IMPORTING constants. |
7023 |
* |
|
0 | 7024 |
* @since 2.3.0 |
7025 |
* @access private |
|
5 | 7026 |
* |
7027 |
* @param int $post_id The ID in the database table of the post being published. |
|
0 | 7028 |
*/ |
5 | 7029 |
function _publish_post_hook( $post_id ) { |
7030 |
if ( defined( 'XMLRPC_REQUEST' ) ) { |
|
7031 |
/** |
|
7032 |
* Fires when _publish_post_hook() is called during an XML-RPC request. |
|
7033 |
* |
|
7034 |
* @since 2.1.0 |
|
7035 |
* |
|
7036 |
* @param int $post_id Post ID. |
|
7037 |
*/ |
|
7038 |
do_action( 'xmlrpc_publish_post', $post_id ); |
|
7039 |
} |
|
0 | 7040 |
|
9 | 7041 |
if ( defined( 'WP_IMPORTING' ) ) { |
0 | 7042 |
return; |
9 | 7043 |
} |
7044 |
||
7045 |
if ( get_option( 'default_pingback_flag' ) ) { |
|
16 | 7046 |
add_post_meta( $post_id, '_pingme', '1', true ); |
7047 |
} |
|
7048 |
add_post_meta( $post_id, '_encloseme', '1', true ); |
|
7049 |
||
7050 |
$to_ping = get_to_ping( $post_id ); |
|
7051 |
if ( ! empty( $to_ping ) ) { |
|
7052 |
add_post_meta( $post_id, '_trackbackme', '1' ); |
|
7053 |
} |
|
0 | 7054 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7055 |
if ( ! wp_next_scheduled( 'do_pings' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7056 |
wp_schedule_single_event( time(), 'do_pings' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7057 |
} |
0 | 7058 |
} |
7059 |
||
7060 |
/** |
|
9 | 7061 |
* Returns the ID of the post's parent. |
0 | 7062 |
* |
7063 |
* @since 3.1.0 |
|
7064 |
* |
|
9 | 7065 |
* @param int|WP_Post $post Post ID or post object. Defaults to global $post. |
16 | 7066 |
* @return int|false Post parent ID (which can be 0 if there is no parent), |
7067 |
* or false if the post does not exist. |
|
0 | 7068 |
*/ |
9 | 7069 |
function wp_get_post_parent_id( $post ) { |
7070 |
$post = get_post( $post ); |
|
7071 |
if ( ! $post || is_wp_error( $post ) ) { |
|
0 | 7072 |
return false; |
9 | 7073 |
} |
0 | 7074 |
return (int) $post->post_parent; |
7075 |
} |
|
7076 |
||
7077 |
/** |
|
5 | 7078 |
* Check the given subset of the post hierarchy for hierarchy loops. |
7079 |
* |
|
7080 |
* 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
|
7081 |
* to the {@see 'wp_insert_post_parent'} filter. |
0 | 7082 |
* |
7083 |
* @since 3.1.0 |
|
5 | 7084 |
* |
7085 |
* @see wp_find_hierarchy_loop() |
|
0 | 7086 |
* |
7087 |
* @param int $post_parent ID of the parent for the post we're checking. |
|
5 | 7088 |
* @param int $post_ID ID of the post we're checking. |
7089 |
* @return int The new post_parent for the post, 0 otherwise. |
|
0 | 7090 |
*/ |
7091 |
function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { |
|
5 | 7092 |
// Nothing fancy here - bail. |
9 | 7093 |
if ( ! $post_parent ) { |
0 | 7094 |
return 0; |
9 | 7095 |
} |
0 | 7096 |
|
5 | 7097 |
// New post can't cause a loop. |
9 | 7098 |
if ( empty( $post_ID ) ) { |
0 | 7099 |
return $post_parent; |
9 | 7100 |
} |
0 | 7101 |
|
5 | 7102 |
// Can't be its own parent. |
9 | 7103 |
if ( $post_parent == $post_ID ) { |
0 | 7104 |
return 0; |
9 | 7105 |
} |
0 | 7106 |
|
5 | 7107 |
// Now look for larger loops. |
16 | 7108 |
$loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ); |
7109 |
if ( ! $loop ) { |
|
7110 |
return $post_parent; // No loop. |
|
9 | 7111 |
} |
0 | 7112 |
|
5 | 7113 |
// Setting $post_parent to the given value causes a loop. |
9 | 7114 |
if ( isset( $loop[ $post_ID ] ) ) { |
0 | 7115 |
return 0; |
9 | 7116 |
} |
0 | 7117 |
|
7118 |
// There's a loop, but it doesn't contain $post_ID. Break the loop. |
|
9 | 7119 |
foreach ( array_keys( $loop ) as $loop_member ) { |
7120 |
wp_update_post( |
|
7121 |
array( |
|
7122 |
'ID' => $loop_member, |
|
7123 |
'post_parent' => 0, |
|
7124 |
) |
|
7125 |
); |
|
7126 |
} |
|
0 | 7127 |
|
7128 |
return $post_parent; |
|
7129 |
} |
|
7130 |
||
7131 |
/** |
|
9 | 7132 |
* Sets the post thumbnail (featured image) for the given post. |
0 | 7133 |
* |
7134 |
* @since 3.1.0 |
|
7135 |
* |
|
5 | 7136 |
* @param int|WP_Post $post Post ID or post object where thumbnail should be attached. |
7137 |
* @param int $thumbnail_id Thumbnail to attach. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7138 |
* @return int|bool True on success, false on failure. |
0 | 7139 |
*/ |
7140 |
function set_post_thumbnail( $post, $thumbnail_id ) { |
|
9 | 7141 |
$post = get_post( $post ); |
0 | 7142 |
$thumbnail_id = absint( $thumbnail_id ); |
7143 |
if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { |
|
9 | 7144 |
if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) { |
0 | 7145 |
return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); |
9 | 7146 |
} else { |
0 | 7147 |
return delete_post_meta( $post->ID, '_thumbnail_id' ); |
9 | 7148 |
} |
0 | 7149 |
} |
7150 |
return false; |
|
7151 |
} |
|
7152 |
||
7153 |
/** |
|
9 | 7154 |
* Removes the thumbnail (featured image) from the given post. |
0 | 7155 |
* |
7156 |
* @since 3.3.0 |
|
7157 |
* |
|
9 | 7158 |
* @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed. |
0 | 7159 |
* @return bool True on success, false on failure. |
7160 |
*/ |
|
7161 |
function delete_post_thumbnail( $post ) { |
|
7162 |
$post = get_post( $post ); |
|
9 | 7163 |
if ( $post ) { |
0 | 7164 |
return delete_post_meta( $post->ID, '_thumbnail_id' ); |
9 | 7165 |
} |
0 | 7166 |
return false; |
7167 |
} |
|
7168 |
||
7169 |
/** |
|
5 | 7170 |
* Delete auto-drafts for new posts that are > 7 days old. |
0 | 7171 |
* |
7172 |
* @since 3.4.0 |
|
5 | 7173 |
* |
7174 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
0 | 7175 |
*/ |
7176 |
function wp_delete_auto_drafts() { |
|
7177 |
global $wpdb; |
|
7178 |
||
5 | 7179 |
// Cleanup old auto-drafts more than 7 days old. |
0 | 7180 |
$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 | 7181 |
foreach ( (array) $old_posts as $delete ) { |
7182 |
// Force delete. |
|
7183 |
wp_delete_post( $delete, true ); |
|
7184 |
} |
|
0 | 7185 |
} |
7186 |
||
7187 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7188 |
* Queues posts for lazy-loading of term meta. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7189 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7190 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7191 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7192 |
* @param array $posts Array of WP_Post objects. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7193 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7194 |
function wp_queue_posts_for_term_meta_lazyload( $posts ) { |
16 | 7195 |
$post_type_taxonomies = array(); |
7196 |
$term_ids = array(); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7197 |
foreach ( $posts as $post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7198 |
if ( ! ( $post instanceof WP_Post ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7199 |
continue; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7200 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7201 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7202 |
if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7203 |
$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
|
7204 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7205 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7206 |
foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7207 |
// 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
|
7208 |
$terms = get_object_term_cache( $post->ID, $taxonomy ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7209 |
if ( false !== $terms ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7210 |
foreach ( $terms as $term ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7211 |
if ( ! isset( $term_ids[ $term->term_id ] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7212 |
$term_ids[] = $term->term_id; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7213 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7214 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7215 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7216 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7217 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7218 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7219 |
if ( $term_ids ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7220 |
$lazyloader = wp_metadata_lazyloader(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7221 |
$lazyloader->queue_objects( 'term', $term_ids ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7222 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7223 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7224 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7225 |
/** |
5 | 7226 |
* Update the custom taxonomies' term counts when a post's status is changed. |
7227 |
* |
|
7228 |
* For example, default posts term counts (for custom taxonomies) don't include |
|
7229 |
* private / draft posts. |
|
7230 |
* |
|
7231 |
* @since 3.3.0 |
|
0 | 7232 |
* @access private |
5 | 7233 |
* |
7234 |
* @param string $new_status New post status. |
|
7235 |
* @param string $old_status Old post status. |
|
7236 |
* @param WP_Post $post Post object. |
|
0 | 7237 |
*/ |
7238 |
function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { |
|
7239 |
// Update counts for the post's terms. |
|
7240 |
foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { |
|
7241 |
$tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); |
|
7242 |
wp_update_term_count( $tt_ids, $taxonomy ); |
|
7243 |
} |
|
7244 |
} |
|
7245 |
||
7246 |
/** |
|
16 | 7247 |
* Adds any posts from the given IDs to the cache that do not already exist in cache |
0 | 7248 |
* |
7249 |
* @since 3.4.0 |
|
7250 |
* @access private |
|
7251 |
* |
|
5 | 7252 |
* @see update_post_caches() |
7253 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7254 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7255 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7256 |
* @param array $ids ID list. |
5 | 7257 |
* @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
7258 |
* @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|
0 | 7259 |
*/ |
7260 |
function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { |
|
7261 |
global $wpdb; |
|
7262 |
||
7263 |
$non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); |
|
9 | 7264 |
if ( ! empty( $non_cached_ids ) ) { |
7265 |
$fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ',', $non_cached_ids ) ) ); |
|
0 | 7266 |
|
7267 |
update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); |
|
7268 |
} |
|
7269 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7270 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7271 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7272 |
* 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
|
7273 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7274 |
* 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
|
7275 |
* if the post is untrashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7276 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7277 |
* For internal use. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7278 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7279 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7280 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7281 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7282 |
* @param string $post_name Slug. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7283 |
* @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
|
7284 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7285 |
function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { |
9 | 7286 |
$trashed_posts_with_desired_slug = get_posts( |
7287 |
array( |
|
7288 |
'name' => $post_name, |
|
7289 |
'post_status' => 'trash', |
|
7290 |
'post_type' => 'any', |
|
7291 |
'nopaging' => true, |
|
7292 |
'post__not_in' => array( $post_ID ), |
|
7293 |
) |
|
7294 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7295 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7296 |
if ( ! empty( $trashed_posts_with_desired_slug ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7297 |
foreach ( $trashed_posts_with_desired_slug as $_post ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7298 |
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
|
7299 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7300 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7301 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7302 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7303 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7304 |
* Adds a trashed suffix for a given post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7305 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7306 |
* 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
|
7307 |
* if the post is untrashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7308 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7309 |
* For internal use. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7310 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7311 |
* @since 4.5.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7312 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7313 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7314 |
* @param WP_Post $post The post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7315 |
* @return string New slug for the post. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7316 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7317 |
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
|
7318 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7319 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7320 |
$post = get_post( $post ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7321 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7322 |
if ( '__trashed' === substr( $post->post_name, -9 ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7323 |
return $post->post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7324 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7325 |
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
|
7326 |
$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
|
7327 |
$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
|
7328 |
clean_post_cache( $post->ID ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7329 |
return $post_name; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7330 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7331 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7332 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7333 |
* 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
|
7334 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7335 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7336 |
* @access private |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7337 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7338 |
* @global wpdb $wpdb WordPress database abstraction object. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7339 |
* |
16 | 7340 |
* @param string[] $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
7341 |
* DISTINCT, fields (SELECT), and LIMITS clauses. |
|
7342 |
* @return string[] The modified array of clauses. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7343 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7344 |
function _filter_query_attachment_filenames( $clauses ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7345 |
global $wpdb; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7346 |
remove_filter( 'posts_clauses', __FUNCTION__ ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7347 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7348 |
// 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
|
7349 |
$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
|
7350 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7351 |
$clauses['groupby'] = "{$wpdb->posts}.ID"; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7352 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7353 |
$clauses['where'] = preg_replace( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7354 |
"/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
9 | 7355 |
'$0 OR ( sq1.meta_value $1 $2 )', |
7356 |
$clauses['where'] |
|
7357 |
); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7358 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7359 |
return $clauses; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7360 |
} |
9 | 7361 |
|
7362 |
/** |
|
7363 |
* Sets the last changed time for the 'posts' cache group. |
|
7364 |
* |
|
7365 |
* @since 5.0.0 |
|
7366 |
*/ |
|
7367 |
function wp_cache_set_posts_last_changed() { |
|
7368 |
wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|
7369 |
} |
|
7370 |
||
7371 |
/** |
|
7372 |
* Get all available post MIME types for a given post type. |
|
7373 |
* |
|
7374 |
* @since 2.5.0 |
|
7375 |
* |
|
7376 |
* @global wpdb $wpdb WordPress database abstraction object. |
|
7377 |
* |
|
7378 |
* @param string $type |
|
7379 |
* @return mixed |
|
7380 |
*/ |
|
7381 |
function get_available_post_mime_types( $type = 'attachment' ) { |
|
7382 |
global $wpdb; |
|
7383 |
||
7384 |
$types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) ); |
|
7385 |
return $types; |
|
7386 |
} |
|
16 | 7387 |
|
7388 |
/** |
|
7389 |
* Retrieves the path to an uploaded image file. |
|
7390 |
* |
|
7391 |
* Similar to `get_attached_file()` however some images may have been processed after uploading |
|
7392 |
* to make them suitable for web use. In this case the attached "full" size file is usually replaced |
|
7393 |
* with a scaled down version of the original image. This function always returns the path |
|
7394 |
* to the originally uploaded image file. |
|
7395 |
* |
|
7396 |
* @since 5.3.0 |
|
7397 |
* @since 5.4.0 Added the `$unfiltered` parameter. |
|
7398 |
* |
|
7399 |
* @param int $attachment_id Attachment ID. |
|
7400 |
* @param bool $unfiltered Optional. Passed through to `get_attached_file()`. Default false. |
|
7401 |
* @return string|false Path to the original image file or false if the attachment is not an image. |
|
7402 |
*/ |
|
7403 |
function wp_get_original_image_path( $attachment_id, $unfiltered = false ) { |
|
7404 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
|
7405 |
return false; |
|
7406 |
} |
|
7407 |
||
7408 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
7409 |
$image_file = get_attached_file( $attachment_id, $unfiltered ); |
|
7410 |
||
7411 |
if ( empty( $image_meta['original_image'] ) ) { |
|
7412 |
$original_image = $image_file; |
|
7413 |
} else { |
|
7414 |
$original_image = path_join( dirname( $image_file ), $image_meta['original_image'] ); |
|
7415 |
} |
|
7416 |
||
7417 |
/** |
|
7418 |
* Filters the path to the original image. |
|
7419 |
* |
|
7420 |
* @since 5.3.0 |
|
7421 |
* |
|
7422 |
* @param string $original_image Path to original image file. |
|
7423 |
* @param int $attachment_id Attachment ID. |
|
7424 |
*/ |
|
7425 |
return apply_filters( 'wp_get_original_image_path', $original_image, $attachment_id ); |
|
7426 |
} |
|
7427 |
||
7428 |
/** |
|
7429 |
* Retrieve the URL to an original attachment image. |
|
7430 |
* |
|
7431 |
* Similar to `wp_get_attachment_url()` however some images may have been |
|
7432 |
* processed after uploading. In this case this function returns the URL |
|
7433 |
* to the originally uploaded image file. |
|
7434 |
* |
|
7435 |
* @since 5.3.0 |
|
7436 |
* |
|
7437 |
* @param int $attachment_id Attachment post ID. |
|
7438 |
* @return string|false Attachment image URL, false on error or if the attachment is not an image. |
|
7439 |
*/ |
|
7440 |
function wp_get_original_image_url( $attachment_id ) { |
|
7441 |
if ( ! wp_attachment_is_image( $attachment_id ) ) { |
|
7442 |
return false; |
|
7443 |
} |
|
7444 |
||
7445 |
$image_url = wp_get_attachment_url( $attachment_id ); |
|
7446 |
||
7447 |
if ( empty( $image_url ) ) { |
|
7448 |
return false; |
|
7449 |
} |
|
7450 |
||
7451 |
$image_meta = wp_get_attachment_metadata( $attachment_id ); |
|
7452 |
||
7453 |
if ( empty( $image_meta['original_image'] ) ) { |
|
7454 |
$original_image_url = $image_url; |
|
7455 |
} else { |
|
7456 |
$original_image_url = path_join( dirname( $image_url ), $image_meta['original_image'] ); |
|
7457 |
} |
|
7458 |
||
7459 |
/** |
|
7460 |
* Filters the URL to the original attachment image. |
|
7461 |
* |
|
7462 |
* @since 5.3.0 |
|
7463 |
* |
|
7464 |
* @param string $original_image_url URL to original image. |
|
7465 |
* @param int $attachment_id Attachment ID. |
|
7466 |
*/ |
|
7467 |
return apply_filters( 'wp_get_original_image_url', $original_image_url, $attachment_id ); |
|
7468 |
} |