| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * Core Post API |
|---|
| 4 | * |
|---|
| 5 | * @package WordPress |
|---|
| 6 | * @subpackage Post |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | // |
|---|
| 10 | // Post Type Registration |
|---|
| 11 | // |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Creates the initial post types when 'init' action is fired. |
|---|
| 15 | * |
|---|
| 16 | * See {@see 'init'}. |
|---|
| 17 | * |
|---|
| 18 | * @since 2.9.0 |
|---|
| 19 | */ |
|---|
| 20 | function create_initial_post_types() { |
|---|
| 21 | register_post_type( |
|---|
| 22 | 'post', |
|---|
| 23 | array( |
|---|
| 24 | 'labels' => array( |
|---|
| 25 | 'name_admin_bar' => _x( 'Post', 'add new from admin bar' ), |
|---|
| 26 | ), |
|---|
| 27 | 'public' => true, |
|---|
| 28 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 29 | '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|---|
| 30 | 'capability_type' => 'post', |
|---|
| 31 | 'map_meta_cap' => true, |
|---|
| 32 | 'menu_position' => 5, |
|---|
| 33 | 'hierarchical' => false, |
|---|
| 34 | 'rewrite' => false, |
|---|
| 35 | 'query_var' => false, |
|---|
| 36 | 'delete_with_user' => true, |
|---|
| 37 | 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'post-formats' ), |
|---|
| 38 | 'show_in_rest' => true, |
|---|
| 39 | 'rest_base' => 'posts', |
|---|
| 40 | 'rest_controller_class' => 'WP_REST_Posts_Controller', |
|---|
| 41 | ) |
|---|
| 42 | ); |
|---|
| 43 | |
|---|
| 44 | register_post_type( |
|---|
| 45 | 'page', |
|---|
| 46 | array( |
|---|
| 47 | 'labels' => array( |
|---|
| 48 | 'name_admin_bar' => _x( 'Page', 'add new from admin bar' ), |
|---|
| 49 | ), |
|---|
| 50 | 'public' => true, |
|---|
| 51 | 'publicly_queryable' => false, |
|---|
| 52 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 53 | '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|---|
| 54 | 'capability_type' => 'page', |
|---|
| 55 | 'map_meta_cap' => true, |
|---|
| 56 | 'menu_position' => 20, |
|---|
| 57 | 'hierarchical' => true, |
|---|
| 58 | 'rewrite' => false, |
|---|
| 59 | 'query_var' => false, |
|---|
| 60 | 'delete_with_user' => true, |
|---|
| 61 | 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'page-attributes', 'custom-fields', 'comments', 'revisions' ), |
|---|
| 62 | 'show_in_rest' => true, |
|---|
| 63 | 'rest_base' => 'pages', |
|---|
| 64 | 'rest_controller_class' => 'WP_REST_Posts_Controller', |
|---|
| 65 | ) |
|---|
| 66 | ); |
|---|
| 67 | |
|---|
| 68 | register_post_type( |
|---|
| 69 | 'attachment', |
|---|
| 70 | array( |
|---|
| 71 | 'labels' => array( |
|---|
| 72 | 'name' => _x( 'Media', 'post type general name' ), |
|---|
| 73 | 'name_admin_bar' => _x( 'Media', 'add new from admin bar' ), |
|---|
| 74 | 'add_new' => _x( 'Add New', 'add new media' ), |
|---|
| 75 | 'edit_item' => __( 'Edit Media' ), |
|---|
| 76 | 'view_item' => __( 'View Attachment Page' ), |
|---|
| 77 | 'attributes' => __( 'Attachment Attributes' ), |
|---|
| 78 | ), |
|---|
| 79 | 'public' => true, |
|---|
| 80 | 'show_ui' => true, |
|---|
| 81 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 82 | '_edit_link' => 'post.php?post=%d', /* internal use only. don't use this when registering your own post type. */ |
|---|
| 83 | 'capability_type' => 'post', |
|---|
| 84 | 'capabilities' => array( |
|---|
| 85 | 'create_posts' => 'upload_files', |
|---|
| 86 | ), |
|---|
| 87 | 'map_meta_cap' => true, |
|---|
| 88 | 'hierarchical' => false, |
|---|
| 89 | 'rewrite' => false, |
|---|
| 90 | 'query_var' => false, |
|---|
| 91 | 'show_in_nav_menus' => false, |
|---|
| 92 | 'delete_with_user' => true, |
|---|
| 93 | 'supports' => array( 'title', 'author', 'comments' ), |
|---|
| 94 | 'show_in_rest' => true, |
|---|
| 95 | 'rest_base' => 'media', |
|---|
| 96 | 'rest_controller_class' => 'WP_REST_Attachments_Controller', |
|---|
| 97 | ) |
|---|
| 98 | ); |
|---|
| 99 | add_post_type_support( 'attachment:audio', 'thumbnail' ); |
|---|
| 100 | add_post_type_support( 'attachment:video', 'thumbnail' ); |
|---|
| 101 | |
|---|
| 102 | register_post_type( |
|---|
| 103 | 'revision', |
|---|
| 104 | array( |
|---|
| 105 | 'labels' => array( |
|---|
| 106 | 'name' => __( 'Revisions' ), |
|---|
| 107 | 'singular_name' => __( 'Revision' ), |
|---|
| 108 | ), |
|---|
| 109 | 'public' => false, |
|---|
| 110 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 111 | '_edit_link' => 'revision.php?revision=%d', /* internal use only. don't use this when registering your own post type. */ |
|---|
| 112 | 'capability_type' => 'post', |
|---|
| 113 | 'map_meta_cap' => true, |
|---|
| 114 | 'hierarchical' => false, |
|---|
| 115 | 'rewrite' => false, |
|---|
| 116 | 'query_var' => false, |
|---|
| 117 | 'can_export' => false, |
|---|
| 118 | 'delete_with_user' => true, |
|---|
| 119 | 'supports' => array( 'author' ), |
|---|
| 120 | ) |
|---|
| 121 | ); |
|---|
| 122 | |
|---|
| 123 | register_post_type( |
|---|
| 124 | 'nav_menu_item', |
|---|
| 125 | array( |
|---|
| 126 | 'labels' => array( |
|---|
| 127 | 'name' => __( 'Navigation Menu Items' ), |
|---|
| 128 | 'singular_name' => __( 'Navigation Menu Item' ), |
|---|
| 129 | ), |
|---|
| 130 | 'public' => false, |
|---|
| 131 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 132 | 'hierarchical' => false, |
|---|
| 133 | 'rewrite' => false, |
|---|
| 134 | 'delete_with_user' => false, |
|---|
| 135 | 'query_var' => false, |
|---|
| 136 | ) |
|---|
| 137 | ); |
|---|
| 138 | |
|---|
| 139 | register_post_type( |
|---|
| 140 | 'custom_css', |
|---|
| 141 | array( |
|---|
| 142 | 'labels' => array( |
|---|
| 143 | 'name' => __( 'Custom CSS' ), |
|---|
| 144 | 'singular_name' => __( 'Custom CSS' ), |
|---|
| 145 | ), |
|---|
| 146 | 'public' => false, |
|---|
| 147 | 'hierarchical' => false, |
|---|
| 148 | 'rewrite' => false, |
|---|
| 149 | 'query_var' => false, |
|---|
| 150 | 'delete_with_user' => false, |
|---|
| 151 | 'can_export' => true, |
|---|
| 152 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 153 | 'supports' => array( 'title', 'revisions' ), |
|---|
| 154 | 'capabilities' => array( |
|---|
| 155 | 'delete_posts' => 'edit_theme_options', |
|---|
| 156 | 'delete_post' => 'edit_theme_options', |
|---|
| 157 | 'delete_published_posts' => 'edit_theme_options', |
|---|
| 158 | 'delete_private_posts' => 'edit_theme_options', |
|---|
| 159 | 'delete_others_posts' => 'edit_theme_options', |
|---|
| 160 | 'edit_post' => 'edit_css', |
|---|
| 161 | 'edit_posts' => 'edit_css', |
|---|
| 162 | 'edit_others_posts' => 'edit_css', |
|---|
| 163 | 'edit_published_posts' => 'edit_css', |
|---|
| 164 | 'read_post' => 'read', |
|---|
| 165 | 'read_private_posts' => 'read', |
|---|
| 166 | 'publish_posts' => 'edit_theme_options', |
|---|
| 167 | ), |
|---|
| 168 | ) |
|---|
| 169 | ); |
|---|
| 170 | |
|---|
| 171 | register_post_type( |
|---|
| 172 | 'customize_changeset', |
|---|
| 173 | array( |
|---|
| 174 | 'labels' => array( |
|---|
| 175 | 'name' => _x( 'Changesets', 'post type general name' ), |
|---|
| 176 | 'singular_name' => _x( 'Changeset', 'post type singular name' ), |
|---|
| 177 | 'menu_name' => _x( 'Changesets', 'admin menu' ), |
|---|
| 178 | 'name_admin_bar' => _x( 'Changeset', 'add new on admin bar' ), |
|---|
| 179 | 'add_new' => _x( 'Add New', 'Customize Changeset' ), |
|---|
| 180 | 'add_new_item' => __( 'Add New Changeset' ), |
|---|
| 181 | 'new_item' => __( 'New Changeset' ), |
|---|
| 182 | 'edit_item' => __( 'Edit Changeset' ), |
|---|
| 183 | 'view_item' => __( 'View Changeset' ), |
|---|
| 184 | 'all_items' => __( 'All Changesets' ), |
|---|
| 185 | 'search_items' => __( 'Search Changesets' ), |
|---|
| 186 | 'not_found' => __( 'No changesets found.' ), |
|---|
| 187 | 'not_found_in_trash' => __( 'No changesets found in Trash.' ), |
|---|
| 188 | ), |
|---|
| 189 | 'public' => false, |
|---|
| 190 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 191 | 'map_meta_cap' => true, |
|---|
| 192 | 'hierarchical' => false, |
|---|
| 193 | 'rewrite' => false, |
|---|
| 194 | 'query_var' => false, |
|---|
| 195 | 'can_export' => false, |
|---|
| 196 | 'delete_with_user' => false, |
|---|
| 197 | 'supports' => array( 'title', 'author' ), |
|---|
| 198 | 'capability_type' => 'customize_changeset', |
|---|
| 199 | 'capabilities' => array( |
|---|
| 200 | 'create_posts' => 'customize', |
|---|
| 201 | 'delete_others_posts' => 'customize', |
|---|
| 202 | 'delete_post' => 'customize', |
|---|
| 203 | 'delete_posts' => 'customize', |
|---|
| 204 | 'delete_private_posts' => 'customize', |
|---|
| 205 | 'delete_published_posts' => 'customize', |
|---|
| 206 | 'edit_others_posts' => 'customize', |
|---|
| 207 | 'edit_post' => 'customize', |
|---|
| 208 | 'edit_posts' => 'customize', |
|---|
| 209 | 'edit_private_posts' => 'customize', |
|---|
| 210 | 'edit_published_posts' => 'do_not_allow', |
|---|
| 211 | 'publish_posts' => 'customize', |
|---|
| 212 | 'read' => 'read', |
|---|
| 213 | 'read_post' => 'customize', |
|---|
| 214 | 'read_private_posts' => 'customize', |
|---|
| 215 | ), |
|---|
| 216 | ) |
|---|
| 217 | ); |
|---|
| 218 | |
|---|
| 219 | register_post_type( |
|---|
| 220 | 'oembed_cache', |
|---|
| 221 | array( |
|---|
| 222 | 'labels' => array( |
|---|
| 223 | 'name' => __( 'oEmbed Responses' ), |
|---|
| 224 | 'singular_name' => __( 'oEmbed Response' ), |
|---|
| 225 | ), |
|---|
| 226 | 'public' => false, |
|---|
| 227 | 'hierarchical' => false, |
|---|
| 228 | 'rewrite' => false, |
|---|
| 229 | 'query_var' => false, |
|---|
| 230 | 'delete_with_user' => false, |
|---|
| 231 | 'can_export' => false, |
|---|
| 232 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 233 | 'supports' => array(), |
|---|
| 234 | ) |
|---|
| 235 | ); |
|---|
| 236 | |
|---|
| 237 | register_post_type( |
|---|
| 238 | 'user_request', |
|---|
| 239 | array( |
|---|
| 240 | 'labels' => array( |
|---|
| 241 | 'name' => __( 'User Requests' ), |
|---|
| 242 | 'singular_name' => __( 'User Request' ), |
|---|
| 243 | ), |
|---|
| 244 | 'public' => false, |
|---|
| 245 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 246 | 'hierarchical' => false, |
|---|
| 247 | 'rewrite' => false, |
|---|
| 248 | 'query_var' => false, |
|---|
| 249 | 'can_export' => false, |
|---|
| 250 | 'delete_with_user' => false, |
|---|
| 251 | 'supports' => array(), |
|---|
| 252 | ) |
|---|
| 253 | ); |
|---|
| 254 | |
|---|
| 255 | register_post_type( |
|---|
| 256 | 'wp_block', |
|---|
| 257 | array( |
|---|
| 258 | 'labels' => array( |
|---|
| 259 | 'name' => _x( 'Blocks', 'post type general name' ), |
|---|
| 260 | 'singular_name' => _x( 'Block', 'post type singular name' ), |
|---|
| 261 | 'menu_name' => _x( 'Blocks', 'admin menu' ), |
|---|
| 262 | 'name_admin_bar' => _x( 'Block', 'add new on admin bar' ), |
|---|
| 263 | 'add_new' => _x( 'Add New', 'Block' ), |
|---|
| 264 | 'add_new_item' => __( 'Add New Block' ), |
|---|
| 265 | 'new_item' => __( 'New Block' ), |
|---|
| 266 | 'edit_item' => __( 'Edit Block' ), |
|---|
| 267 | 'view_item' => __( 'View Block' ), |
|---|
| 268 | 'all_items' => __( 'All Blocks' ), |
|---|
| 269 | 'search_items' => __( 'Search Blocks' ), |
|---|
| 270 | 'not_found' => __( 'No blocks found.' ), |
|---|
| 271 | 'not_found_in_trash' => __( 'No blocks found in Trash.' ), |
|---|
| 272 | 'filter_items_list' => __( 'Filter blocks list' ), |
|---|
| 273 | 'items_list_navigation' => __( 'Blocks list navigation' ), |
|---|
| 274 | 'items_list' => __( 'Blocks list' ), |
|---|
| 275 | 'item_published' => __( 'Block published.' ), |
|---|
| 276 | 'item_published_privately' => __( 'Block published privately.' ), |
|---|
| 277 | 'item_reverted_to_draft' => __( 'Block reverted to draft.' ), |
|---|
| 278 | 'item_scheduled' => __( 'Block scheduled.' ), |
|---|
| 279 | 'item_updated' => __( 'Block updated.' ), |
|---|
| 280 | ), |
|---|
| 281 | 'public' => false, |
|---|
| 282 | '_builtin' => true, /* internal use only. don't use this when registering your own post type. */ |
|---|
| 283 | 'show_ui' => true, |
|---|
| 284 | 'show_in_menu' => false, |
|---|
| 285 | 'rewrite' => false, |
|---|
| 286 | 'show_in_rest' => true, |
|---|
| 287 | 'rest_base' => 'blocks', |
|---|
| 288 | 'rest_controller_class' => 'WP_REST_Blocks_Controller', |
|---|
| 289 | 'capability_type' => 'block', |
|---|
| 290 | 'capabilities' => array( |
|---|
| 291 | // You need to be able to edit posts, in order to read blocks in their raw form. |
|---|
| 292 | 'read' => 'edit_posts', |
|---|
| 293 | // You need to be able to publish posts, in order to create blocks. |
|---|
| 294 | 'create_posts' => 'publish_posts', |
|---|
| 295 | 'edit_posts' => 'edit_posts', |
|---|
| 296 | 'edit_published_posts' => 'edit_published_posts', |
|---|
| 297 | 'delete_published_posts' => 'delete_published_posts', |
|---|
| 298 | 'edit_others_posts' => 'edit_others_posts', |
|---|
| 299 | 'delete_others_posts' => 'delete_others_posts', |
|---|
| 300 | ), |
|---|
| 301 | 'map_meta_cap' => true, |
|---|
| 302 | 'supports' => array( |
|---|
| 303 | 'title', |
|---|
| 304 | 'editor', |
|---|
| 305 | ), |
|---|
| 306 | ) |
|---|
| 307 | ); |
|---|
| 308 | |
|---|
| 309 | register_post_status( |
|---|
| 310 | 'publish', |
|---|
| 311 | array( |
|---|
| 312 | 'label' => _x( 'Published', 'post status' ), |
|---|
| 313 | 'public' => true, |
|---|
| 314 | '_builtin' => true, /* internal use only. */ |
|---|
| 315 | 'label_count' => _n_noop( 'Published <span class="count">(%s)</span>', 'Published <span class="count">(%s)</span>' ), |
|---|
| 316 | ) |
|---|
| 317 | ); |
|---|
| 318 | |
|---|
| 319 | register_post_status( |
|---|
| 320 | 'future', |
|---|
| 321 | array( |
|---|
| 322 | 'label' => _x( 'Scheduled', 'post status' ), |
|---|
| 323 | 'protected' => true, |
|---|
| 324 | '_builtin' => true, /* internal use only. */ |
|---|
| 325 | 'label_count' => _n_noop( 'Scheduled <span class="count">(%s)</span>', 'Scheduled <span class="count">(%s)</span>' ), |
|---|
| 326 | ) |
|---|
| 327 | ); |
|---|
| 328 | |
|---|
| 329 | register_post_status( |
|---|
| 330 | 'draft', |
|---|
| 331 | array( |
|---|
| 332 | 'label' => _x( 'Draft', 'post status' ), |
|---|
| 333 | 'protected' => true, |
|---|
| 334 | '_builtin' => true, /* internal use only. */ |
|---|
| 335 | 'label_count' => _n_noop( 'Draft <span class="count">(%s)</span>', 'Drafts <span class="count">(%s)</span>' ), |
|---|
| 336 | ) |
|---|
| 337 | ); |
|---|
| 338 | |
|---|
| 339 | register_post_status( |
|---|
| 340 | 'pending', |
|---|
| 341 | array( |
|---|
| 342 | 'label' => _x( 'Pending', 'post status' ), |
|---|
| 343 | 'protected' => true, |
|---|
| 344 | '_builtin' => true, /* internal use only. */ |
|---|
| 345 | 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ), |
|---|
| 346 | ) |
|---|
| 347 | ); |
|---|
| 348 | |
|---|
| 349 | register_post_status( |
|---|
| 350 | 'private', |
|---|
| 351 | array( |
|---|
| 352 | 'label' => _x( 'Private', 'post status' ), |
|---|
| 353 | 'private' => true, |
|---|
| 354 | '_builtin' => true, /* internal use only. */ |
|---|
| 355 | 'label_count' => _n_noop( 'Private <span class="count">(%s)</span>', 'Private <span class="count">(%s)</span>' ), |
|---|
| 356 | ) |
|---|
| 357 | ); |
|---|
| 358 | |
|---|
| 359 | register_post_status( |
|---|
| 360 | 'trash', |
|---|
| 361 | array( |
|---|
| 362 | 'label' => _x( 'Trash', 'post status' ), |
|---|
| 363 | 'internal' => true, |
|---|
| 364 | '_builtin' => true, /* internal use only. */ |
|---|
| 365 | 'label_count' => _n_noop( 'Trash <span class="count">(%s)</span>', 'Trash <span class="count">(%s)</span>' ), |
|---|
| 366 | 'show_in_admin_status_list' => true, |
|---|
| 367 | ) |
|---|
| 368 | ); |
|---|
| 369 | |
|---|
| 370 | register_post_status( |
|---|
| 371 | 'auto-draft', |
|---|
| 372 | array( |
|---|
| 373 | 'label' => 'auto-draft', |
|---|
| 374 | 'internal' => true, |
|---|
| 375 | '_builtin' => true, /* internal use only. */ |
|---|
| 376 | ) |
|---|
| 377 | ); |
|---|
| 378 | |
|---|
| 379 | register_post_status( |
|---|
| 380 | 'inherit', |
|---|
| 381 | array( |
|---|
| 382 | 'label' => 'inherit', |
|---|
| 383 | 'internal' => true, |
|---|
| 384 | '_builtin' => true, /* internal use only. */ |
|---|
| 385 | 'exclude_from_search' => false, |
|---|
| 386 | ) |
|---|
| 387 | ); |
|---|
| 388 | |
|---|
| 389 | register_post_status( |
|---|
| 390 | 'request-pending', |
|---|
| 391 | array( |
|---|
| 392 | 'label' => _x( 'Pending', 'request status' ), |
|---|
| 393 | 'internal' => true, |
|---|
| 394 | '_builtin' => true, /* internal use only. */ |
|---|
| 395 | 'label_count' => _n_noop( 'Pending <span class="count">(%s)</span>', 'Pending <span class="count">(%s)</span>' ), |
|---|
| 396 | 'exclude_from_search' => false, |
|---|
| 397 | ) |
|---|
| 398 | ); |
|---|
| 399 | |
|---|
| 400 | register_post_status( |
|---|
| 401 | 'request-confirmed', |
|---|
| 402 | array( |
|---|
| 403 | 'label' => _x( 'Confirmed', 'request status' ), |
|---|
| 404 | 'internal' => true, |
|---|
| 405 | '_builtin' => true, /* internal use only. */ |
|---|
| 406 | 'label_count' => _n_noop( 'Confirmed <span class="count">(%s)</span>', 'Confirmed <span class="count">(%s)</span>' ), |
|---|
| 407 | 'exclude_from_search' => false, |
|---|
| 408 | ) |
|---|
| 409 | ); |
|---|
| 410 | |
|---|
| 411 | register_post_status( |
|---|
| 412 | 'request-failed', |
|---|
| 413 | array( |
|---|
| 414 | 'label' => _x( 'Failed', 'request status' ), |
|---|
| 415 | 'internal' => true, |
|---|
| 416 | '_builtin' => true, /* internal use only. */ |
|---|
| 417 | 'label_count' => _n_noop( 'Failed <span class="count">(%s)</span>', 'Failed <span class="count">(%s)</span>' ), |
|---|
| 418 | 'exclude_from_search' => false, |
|---|
| 419 | ) |
|---|
| 420 | ); |
|---|
| 421 | |
|---|
| 422 | register_post_status( |
|---|
| 423 | 'request-completed', |
|---|
| 424 | array( |
|---|
| 425 | 'label' => _x( 'Completed', 'request status' ), |
|---|
| 426 | 'internal' => true, |
|---|
| 427 | '_builtin' => true, /* internal use only. */ |
|---|
| 428 | 'label_count' => _n_noop( 'Completed <span class="count">(%s)</span>', 'Completed <span class="count">(%s)</span>' ), |
|---|
| 429 | 'exclude_from_search' => false, |
|---|
| 430 | ) |
|---|
| 431 | ); |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | /** |
|---|
| 435 | * Retrieve attached file path based on attachment ID. |
|---|
| 436 | * |
|---|
| 437 | * By default the path will go through the 'get_attached_file' filter, but |
|---|
| 438 | * passing a true to the $unfiltered argument of get_attached_file() will |
|---|
| 439 | * return the file path unfiltered. |
|---|
| 440 | * |
|---|
| 441 | * The function works by getting the single post meta name, named |
|---|
| 442 | * '_wp_attached_file' and returning it. This is a convenience function to |
|---|
| 443 | * prevent looking up the meta name and provide a mechanism for sending the |
|---|
| 444 | * attached filename through a filter. |
|---|
| 445 | * |
|---|
| 446 | * @since 2.0.0 |
|---|
| 447 | * |
|---|
| 448 | * @param int $attachment_id Attachment ID. |
|---|
| 449 | * @param bool $unfiltered Optional. Whether to apply filters. Default false. |
|---|
| 450 | * @return string|false The file path to where the attached file should be, false otherwise. |
|---|
| 451 | */ |
|---|
| 452 | function get_attached_file( $attachment_id, $unfiltered = false ) { |
|---|
| 453 | $file = get_post_meta( $attachment_id, '_wp_attached_file', true ); |
|---|
| 454 | |
|---|
| 455 | // If the file is relative, prepend upload dir. |
|---|
| 456 | if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) && ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) ) { |
|---|
| 457 | $file = $uploads['basedir'] . "/$file"; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | if ( $unfiltered ) { |
|---|
| 461 | return $file; |
|---|
| 462 | } |
|---|
| 463 | |
|---|
| 464 | /** |
|---|
| 465 | * Filters the attached file based on the given ID. |
|---|
| 466 | * |
|---|
| 467 | * @since 2.1.0 |
|---|
| 468 | * |
|---|
| 469 | * @param string $file Path to attached file. |
|---|
| 470 | * @param int $attachment_id Attachment ID. |
|---|
| 471 | */ |
|---|
| 472 | return apply_filters( 'get_attached_file', $file, $attachment_id ); |
|---|
| 473 | } |
|---|
| 474 | |
|---|
| 475 | /** |
|---|
| 476 | * Update attachment file path based on attachment ID. |
|---|
| 477 | * |
|---|
| 478 | * Used to update the file path of the attachment, which uses post meta name |
|---|
| 479 | * '_wp_attached_file' to store the path of the attachment. |
|---|
| 480 | * |
|---|
| 481 | * @since 2.1.0 |
|---|
| 482 | * |
|---|
| 483 | * @param int $attachment_id Attachment ID. |
|---|
| 484 | * @param string $file File path for the attachment. |
|---|
| 485 | * @return bool True on success, false on failure. |
|---|
| 486 | */ |
|---|
| 487 | function update_attached_file( $attachment_id, $file ) { |
|---|
| 488 | if ( ! get_post( $attachment_id ) ) { |
|---|
| 489 | return false; |
|---|
| 490 | } |
|---|
| 491 | |
|---|
| 492 | /** |
|---|
| 493 | * Filters the path to the attached file to update. |
|---|
| 494 | * |
|---|
| 495 | * @since 2.1.0 |
|---|
| 496 | * |
|---|
| 497 | * @param string $file Path to the attached file to update. |
|---|
| 498 | * @param int $attachment_id Attachment ID. |
|---|
| 499 | */ |
|---|
| 500 | $file = apply_filters( 'update_attached_file', $file, $attachment_id ); |
|---|
| 501 | |
|---|
| 502 | if ( $file = _wp_relative_upload_path( $file ) ) { |
|---|
| 503 | return update_post_meta( $attachment_id, '_wp_attached_file', $file ); |
|---|
| 504 | } else { |
|---|
| 505 | return delete_post_meta( $attachment_id, '_wp_attached_file' ); |
|---|
| 506 | } |
|---|
| 507 | } |
|---|
| 508 | |
|---|
| 509 | /** |
|---|
| 510 | * Return relative path to an uploaded file. |
|---|
| 511 | * |
|---|
| 512 | * The path is relative to the current upload dir. |
|---|
| 513 | * |
|---|
| 514 | * @since 2.9.0 |
|---|
| 515 | * @access private |
|---|
| 516 | * |
|---|
| 517 | * @param string $path Full path to the file. |
|---|
| 518 | * @return string Relative path on success, unchanged path on failure. |
|---|
| 519 | */ |
|---|
| 520 | function _wp_relative_upload_path( $path ) { |
|---|
| 521 | $new_path = $path; |
|---|
| 522 | |
|---|
| 523 | $uploads = wp_get_upload_dir(); |
|---|
| 524 | if ( 0 === strpos( $new_path, $uploads['basedir'] ) ) { |
|---|
| 525 | $new_path = str_replace( $uploads['basedir'], '', $new_path ); |
|---|
| 526 | $new_path = ltrim( $new_path, '/' ); |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | /** |
|---|
| 530 | * Filters the relative path to an uploaded file. |
|---|
| 531 | * |
|---|
| 532 | * @since 2.9.0 |
|---|
| 533 | * |
|---|
| 534 | * @param string $new_path Relative path to the file. |
|---|
| 535 | * @param string $path Full path to the file. |
|---|
| 536 | */ |
|---|
| 537 | return apply_filters( '_wp_relative_upload_path', $new_path, $path ); |
|---|
| 538 | } |
|---|
| 539 | |
|---|
| 540 | /** |
|---|
| 541 | * Retrieve all children of the post parent ID. |
|---|
| 542 | * |
|---|
| 543 | * Normally, without any enhancements, the children would apply to pages. In the |
|---|
| 544 | * context of the inner workings of WordPress, pages, posts, and attachments |
|---|
| 545 | * share the same table, so therefore the functionality could apply to any one |
|---|
| 546 | * of them. It is then noted that while this function does not work on posts, it |
|---|
| 547 | * does not mean that it won't work on posts. It is recommended that you know |
|---|
| 548 | * what context you wish to retrieve the children of. |
|---|
| 549 | * |
|---|
| 550 | * Attachments may also be made the child of a post, so if that is an accurate |
|---|
| 551 | * statement (which needs to be verified), it would then be possible to get |
|---|
| 552 | * all of the attachments for a post. Attachments have since changed since |
|---|
| 553 | * version 2.5, so this is most likely inaccurate, but serves generally as an |
|---|
| 554 | * example of what is possible. |
|---|
| 555 | * |
|---|
| 556 | * The arguments listed as defaults are for this function and also of the |
|---|
| 557 | * get_posts() function. The arguments are combined with the get_children defaults |
|---|
| 558 | * and are then passed to the get_posts() function, which accepts additional arguments. |
|---|
| 559 | * You can replace the defaults in this function, listed below and the additional |
|---|
| 560 | * arguments listed in the get_posts() function. |
|---|
| 561 | * |
|---|
| 562 | * The 'post_parent' is the most important argument and important attention |
|---|
| 563 | * needs to be paid to the $args parameter. If you pass either an object or an |
|---|
| 564 | * integer (number), then just the 'post_parent' is grabbed and everything else |
|---|
| 565 | * is lost. If you don't specify any arguments, then it is assumed that you are |
|---|
| 566 | * in The Loop and the post parent will be grabbed for from the current post. |
|---|
| 567 | * |
|---|
| 568 | * The 'post_parent' argument is the ID to get the children. The 'numberposts' |
|---|
| 569 | * is the amount of posts to retrieve that has a default of '-1', which is |
|---|
| 570 | * used to get all of the posts. Giving a number higher than 0 will only |
|---|
| 571 | * retrieve that amount of posts. |
|---|
| 572 | * |
|---|
| 573 | * The 'post_type' and 'post_status' arguments can be used to choose what |
|---|
| 574 | * criteria of posts to retrieve. The 'post_type' can be anything, but WordPress |
|---|
| 575 | * post types are 'post', 'pages', and 'attachments'. The 'post_status' |
|---|
| 576 | * argument will accept any post status within the write administration panels. |
|---|
| 577 | * |
|---|
| 578 | * @since 2.0.0 |
|---|
| 579 | * |
|---|
| 580 | * @see get_posts() |
|---|
| 581 | * @todo Check validity of description. |
|---|
| 582 | * |
|---|
| 583 | * @global WP_Post $post |
|---|
| 584 | * |
|---|
| 585 | * @param mixed $args Optional. User defined arguments for replacing the defaults. Default empty. |
|---|
| 586 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
|---|
| 587 | * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
|---|
| 588 | * @return array Array of children, where the type of each element is determined by $output parameter. |
|---|
| 589 | * Empty array on failure. |
|---|
| 590 | */ |
|---|
| 591 | function get_children( $args = '', $output = OBJECT ) { |
|---|
| 592 | $kids = array(); |
|---|
| 593 | if ( empty( $args ) ) { |
|---|
| 594 | if ( isset( $GLOBALS['post'] ) ) { |
|---|
| 595 | $args = array( 'post_parent' => (int) $GLOBALS['post']->post_parent ); |
|---|
| 596 | } else { |
|---|
| 597 | return $kids; |
|---|
| 598 | } |
|---|
| 599 | } elseif ( is_object( $args ) ) { |
|---|
| 600 | $args = array( 'post_parent' => (int) $args->post_parent ); |
|---|
| 601 | } elseif ( is_numeric( $args ) ) { |
|---|
| 602 | $args = array( 'post_parent' => (int) $args ); |
|---|
| 603 | } |
|---|
| 604 | |
|---|
| 605 | $defaults = array( |
|---|
| 606 | 'numberposts' => -1, |
|---|
| 607 | 'post_type' => 'any', |
|---|
| 608 | 'post_status' => 'any', |
|---|
| 609 | 'post_parent' => 0, |
|---|
| 610 | ); |
|---|
| 611 | |
|---|
| 612 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 613 | |
|---|
| 614 | $children = get_posts( $r ); |
|---|
| 615 | |
|---|
| 616 | if ( ! $children ) { |
|---|
| 617 | return $kids; |
|---|
| 618 | } |
|---|
| 619 | |
|---|
| 620 | if ( ! empty( $r['fields'] ) ) { |
|---|
| 621 | return $children; |
|---|
| 622 | } |
|---|
| 623 | |
|---|
| 624 | update_post_cache( $children ); |
|---|
| 625 | |
|---|
| 626 | foreach ( $children as $key => $child ) { |
|---|
| 627 | $kids[ $child->ID ] = $children[ $key ]; |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | if ( $output == OBJECT ) { |
|---|
| 631 | return $kids; |
|---|
| 632 | } elseif ( $output == ARRAY_A ) { |
|---|
| 633 | $weeuns = array(); |
|---|
| 634 | foreach ( (array) $kids as $kid ) { |
|---|
| 635 | $weeuns[ $kid->ID ] = get_object_vars( $kids[ $kid->ID ] ); |
|---|
| 636 | } |
|---|
| 637 | return $weeuns; |
|---|
| 638 | } elseif ( $output == ARRAY_N ) { |
|---|
| 639 | $babes = array(); |
|---|
| 640 | foreach ( (array) $kids as $kid ) { |
|---|
| 641 | $babes[ $kid->ID ] = array_values( get_object_vars( $kids[ $kid->ID ] ) ); |
|---|
| 642 | } |
|---|
| 643 | return $babes; |
|---|
| 644 | } else { |
|---|
| 645 | return $kids; |
|---|
| 646 | } |
|---|
| 647 | } |
|---|
| 648 | |
|---|
| 649 | /** |
|---|
| 650 | * Get extended entry info (<!--more-->). |
|---|
| 651 | * |
|---|
| 652 | * There should not be any space after the second dash and before the word |
|---|
| 653 | * 'more'. There can be text or space(s) after the word 'more', but won't be |
|---|
| 654 | * referenced. |
|---|
| 655 | * |
|---|
| 656 | * The returned array has 'main', 'extended', and 'more_text' keys. Main has the text before |
|---|
| 657 | * the `<!--more-->`. The 'extended' key has the content after the |
|---|
| 658 | * `<!--more-->` comment. The 'more_text' key has the custom "Read More" text. |
|---|
| 659 | * |
|---|
| 660 | * @since 1.0.0 |
|---|
| 661 | * |
|---|
| 662 | * @param string $post Post content. |
|---|
| 663 | * @return array Post before ('main'), after ('extended'), and custom read more ('more_text'). |
|---|
| 664 | */ |
|---|
| 665 | function get_extended( $post ) { |
|---|
| 666 | //Match the new style more links. |
|---|
| 667 | if ( preg_match( '/<!--more(.*?)?-->/', $post, $matches ) ) { |
|---|
| 668 | list($main, $extended) = explode( $matches[0], $post, 2 ); |
|---|
| 669 | $more_text = $matches[1]; |
|---|
| 670 | } else { |
|---|
| 671 | $main = $post; |
|---|
| 672 | $extended = ''; |
|---|
| 673 | $more_text = ''; |
|---|
| 674 | } |
|---|
| 675 | |
|---|
| 676 | // leading and trailing whitespace. |
|---|
| 677 | $main = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $main ); |
|---|
| 678 | $extended = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $extended ); |
|---|
| 679 | $more_text = preg_replace( '/^[\s]*(.*)[\s]*$/', '\\1', $more_text ); |
|---|
| 680 | |
|---|
| 681 | return array( |
|---|
| 682 | 'main' => $main, |
|---|
| 683 | 'extended' => $extended, |
|---|
| 684 | 'more_text' => $more_text, |
|---|
| 685 | ); |
|---|
| 686 | } |
|---|
| 687 | |
|---|
| 688 | /** |
|---|
| 689 | * Retrieves post data given a post ID or post object. |
|---|
| 690 | * |
|---|
| 691 | * See sanitize_post() for optional $filter values. Also, the parameter |
|---|
| 692 | * `$post`, must be given as a variable, since it is passed by reference. |
|---|
| 693 | * |
|---|
| 694 | * @since 1.5.1 |
|---|
| 695 | * |
|---|
| 696 | * @global WP_Post $post |
|---|
| 697 | * |
|---|
| 698 | * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
|---|
| 699 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
|---|
| 700 | * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
|---|
| 701 | * @param string $filter Optional. Type of filter to apply. Accepts 'raw', 'edit', 'db', |
|---|
| 702 | * or 'display'. Default 'raw'. |
|---|
| 703 | * @return WP_Post|array|null Type corresponding to $output on success or null on failure. |
|---|
| 704 | * When $output is OBJECT, a `WP_Post` instance is returned. |
|---|
| 705 | */ |
|---|
| 706 | function get_post( $post = null, $output = OBJECT, $filter = 'raw' ) { |
|---|
| 707 | if ( empty( $post ) && isset( $GLOBALS['post'] ) ) { |
|---|
| 708 | $post = $GLOBALS['post']; |
|---|
| 709 | } |
|---|
| 710 | |
|---|
| 711 | if ( $post instanceof WP_Post ) { |
|---|
| 712 | $_post = $post; |
|---|
| 713 | } elseif ( is_object( $post ) ) { |
|---|
| 714 | if ( empty( $post->filter ) ) { |
|---|
| 715 | $_post = sanitize_post( $post, 'raw' ); |
|---|
| 716 | $_post = new WP_Post( $_post ); |
|---|
| 717 | } elseif ( 'raw' == $post->filter ) { |
|---|
| 718 | $_post = new WP_Post( $post ); |
|---|
| 719 | } else { |
|---|
| 720 | $_post = WP_Post::get_instance( $post->ID ); |
|---|
| 721 | } |
|---|
| 722 | } else { |
|---|
| 723 | $_post = WP_Post::get_instance( $post ); |
|---|
| 724 | } |
|---|
| 725 | |
|---|
| 726 | if ( ! $_post ) { |
|---|
| 727 | return null; |
|---|
| 728 | } |
|---|
| 729 | |
|---|
| 730 | $_post = $_post->filter( $filter ); |
|---|
| 731 | |
|---|
| 732 | if ( $output == ARRAY_A ) { |
|---|
| 733 | return $_post->to_array(); |
|---|
| 734 | } elseif ( $output == ARRAY_N ) { |
|---|
| 735 | return array_values( $_post->to_array() ); |
|---|
| 736 | } |
|---|
| 737 | |
|---|
| 738 | return $_post; |
|---|
| 739 | } |
|---|
| 740 | |
|---|
| 741 | /** |
|---|
| 742 | * Retrieve ancestors of a post. |
|---|
| 743 | * |
|---|
| 744 | * @since 2.5.0 |
|---|
| 745 | * |
|---|
| 746 | * @param int|WP_Post $post Post ID or post object. |
|---|
| 747 | * @return array Ancestor IDs or empty array if none are found. |
|---|
| 748 | */ |
|---|
| 749 | function get_post_ancestors( $post ) { |
|---|
| 750 | $post = get_post( $post ); |
|---|
| 751 | |
|---|
| 752 | if ( ! $post || empty( $post->post_parent ) || $post->post_parent == $post->ID ) { |
|---|
| 753 | return array(); |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | $ancestors = array(); |
|---|
| 757 | |
|---|
| 758 | $id = $ancestors[] = $post->post_parent; |
|---|
| 759 | |
|---|
| 760 | while ( $ancestor = get_post( $id ) ) { |
|---|
| 761 | // Loop detection: If the ancestor has been seen before, break. |
|---|
| 762 | if ( empty( $ancestor->post_parent ) || ( $ancestor->post_parent == $post->ID ) || in_array( $ancestor->post_parent, $ancestors ) ) { |
|---|
| 763 | break; |
|---|
| 764 | } |
|---|
| 765 | |
|---|
| 766 | $id = $ancestors[] = $ancestor->post_parent; |
|---|
| 767 | } |
|---|
| 768 | |
|---|
| 769 | return $ancestors; |
|---|
| 770 | } |
|---|
| 771 | |
|---|
| 772 | /** |
|---|
| 773 | * Retrieve data from a post field based on Post ID. |
|---|
| 774 | * |
|---|
| 775 | * Examples of the post field will be, 'post_type', 'post_status', 'post_content', |
|---|
| 776 | * etc and based off of the post object property or key names. |
|---|
| 777 | * |
|---|
| 778 | * The context values are based off of the taxonomy filter functions and |
|---|
| 779 | * supported values are found within those functions. |
|---|
| 780 | * |
|---|
| 781 | * @since 2.3.0 |
|---|
| 782 | * @since 4.5.0 The `$post` parameter was made optional. |
|---|
| 783 | * |
|---|
| 784 | * @see sanitize_post_field() |
|---|
| 785 | * |
|---|
| 786 | * @param string $field Post field name. |
|---|
| 787 | * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
|---|
| 788 | * @param string $context Optional. How to filter the field. Accepts 'raw', 'edit', 'db', |
|---|
| 789 | * or 'display'. Default 'display'. |
|---|
| 790 | * @return string The value of the post field on success, empty string on failure. |
|---|
| 791 | */ |
|---|
| 792 | function get_post_field( $field, $post = null, $context = 'display' ) { |
|---|
| 793 | $post = get_post( $post ); |
|---|
| 794 | |
|---|
| 795 | if ( ! $post ) { |
|---|
| 796 | return ''; |
|---|
| 797 | } |
|---|
| 798 | |
|---|
| 799 | if ( ! isset( $post->$field ) ) { |
|---|
| 800 | return ''; |
|---|
| 801 | } |
|---|
| 802 | |
|---|
| 803 | return sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|---|
| 804 | } |
|---|
| 805 | |
|---|
| 806 | /** |
|---|
| 807 | * Retrieve the mime type of an attachment based on the ID. |
|---|
| 808 | * |
|---|
| 809 | * This function can be used with any post type, but it makes more sense with |
|---|
| 810 | * attachments. |
|---|
| 811 | * |
|---|
| 812 | * @since 2.0.0 |
|---|
| 813 | * |
|---|
| 814 | * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post. |
|---|
| 815 | * @return string|false The mime type on success, false on failure. |
|---|
| 816 | */ |
|---|
| 817 | function get_post_mime_type( $post = null ) { |
|---|
| 818 | $post = get_post( $post ); |
|---|
| 819 | |
|---|
| 820 | if ( is_object( $post ) ) { |
|---|
| 821 | return $post->post_mime_type; |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | return false; |
|---|
| 825 | } |
|---|
| 826 | |
|---|
| 827 | /** |
|---|
| 828 | * Retrieve the post status based on the post ID. |
|---|
| 829 | * |
|---|
| 830 | * If the post ID is of an attachment, then the parent post status will be given |
|---|
| 831 | * instead. |
|---|
| 832 | * |
|---|
| 833 | * @since 2.0.0 |
|---|
| 834 | * |
|---|
| 835 | * @param int|WP_Post $post Optional. Post ID or post object. Defaults to global $post.. |
|---|
| 836 | * @return string|false Post status on success, false on failure. |
|---|
| 837 | */ |
|---|
| 838 | function get_post_status( $post = null ) { |
|---|
| 839 | $post = get_post( $post ); |
|---|
| 840 | |
|---|
| 841 | if ( ! is_object( $post ) ) { |
|---|
| 842 | return false; |
|---|
| 843 | } |
|---|
| 844 | |
|---|
| 845 | if ( 'attachment' == $post->post_type ) { |
|---|
| 846 | if ( 'private' == $post->post_status ) { |
|---|
| 847 | return 'private'; |
|---|
| 848 | } |
|---|
| 849 | |
|---|
| 850 | // Unattached attachments are assumed to be published. |
|---|
| 851 | if ( ( 'inherit' == $post->post_status ) && ( 0 == $post->post_parent ) ) { |
|---|
| 852 | return 'publish'; |
|---|
| 853 | } |
|---|
| 854 | |
|---|
| 855 | // Inherit status from the parent. |
|---|
| 856 | if ( $post->post_parent && ( $post->ID != $post->post_parent ) ) { |
|---|
| 857 | $parent_post_status = get_post_status( $post->post_parent ); |
|---|
| 858 | if ( 'trash' == $parent_post_status ) { |
|---|
| 859 | return get_post_meta( $post->post_parent, '_wp_trash_meta_status', true ); |
|---|
| 860 | } else { |
|---|
| 861 | return $parent_post_status; |
|---|
| 862 | } |
|---|
| 863 | } |
|---|
| 864 | } |
|---|
| 865 | |
|---|
| 866 | /** |
|---|
| 867 | * Filters the post status. |
|---|
| 868 | * |
|---|
| 869 | * @since 4.4.0 |
|---|
| 870 | * |
|---|
| 871 | * @param string $post_status The post status. |
|---|
| 872 | * @param WP_Post $post The post object. |
|---|
| 873 | */ |
|---|
| 874 | return apply_filters( 'get_post_status', $post->post_status, $post ); |
|---|
| 875 | } |
|---|
| 876 | |
|---|
| 877 | /** |
|---|
| 878 | * Retrieve all of the WordPress supported post statuses. |
|---|
| 879 | * |
|---|
| 880 | * Posts have a limited set of valid status values, this provides the |
|---|
| 881 | * post_status values and descriptions. |
|---|
| 882 | * |
|---|
| 883 | * @since 2.5.0 |
|---|
| 884 | * |
|---|
| 885 | * @return array List of post statuses. |
|---|
| 886 | */ |
|---|
| 887 | function get_post_statuses() { |
|---|
| 888 | $status = array( |
|---|
| 889 | 'draft' => __( 'Draft' ), |
|---|
| 890 | 'pending' => __( 'Pending Review' ), |
|---|
| 891 | 'private' => __( 'Private' ), |
|---|
| 892 | 'publish' => __( 'Published' ), |
|---|
| 893 | ); |
|---|
| 894 | |
|---|
| 895 | return $status; |
|---|
| 896 | } |
|---|
| 897 | |
|---|
| 898 | /** |
|---|
| 899 | * Retrieve all of the WordPress support page statuses. |
|---|
| 900 | * |
|---|
| 901 | * Pages have a limited set of valid status values, this provides the |
|---|
| 902 | * post_status values and descriptions. |
|---|
| 903 | * |
|---|
| 904 | * @since 2.5.0 |
|---|
| 905 | * |
|---|
| 906 | * @return array List of page statuses. |
|---|
| 907 | */ |
|---|
| 908 | function get_page_statuses() { |
|---|
| 909 | $status = array( |
|---|
| 910 | 'draft' => __( 'Draft' ), |
|---|
| 911 | 'private' => __( 'Private' ), |
|---|
| 912 | 'publish' => __( 'Published' ), |
|---|
| 913 | ); |
|---|
| 914 | |
|---|
| 915 | return $status; |
|---|
| 916 | } |
|---|
| 917 | |
|---|
| 918 | /** |
|---|
| 919 | * Return statuses for privacy requests. |
|---|
| 920 | * |
|---|
| 921 | * @since 4.9.6 |
|---|
| 922 | * @access private |
|---|
| 923 | * |
|---|
| 924 | * @return array |
|---|
| 925 | */ |
|---|
| 926 | function _wp_privacy_statuses() { |
|---|
| 927 | return array( |
|---|
| 928 | 'request-pending' => __( 'Pending' ), // Pending confirmation from user. |
|---|
| 929 | 'request-confirmed' => __( 'Confirmed' ), // User has confirmed the action. |
|---|
| 930 | 'request-failed' => __( 'Failed' ), // User failed to confirm the action. |
|---|
| 931 | 'request-completed' => __( 'Completed' ), // Admin has handled the request. |
|---|
| 932 | ); |
|---|
| 933 | } |
|---|
| 934 | |
|---|
| 935 | /** |
|---|
| 936 | * Register a post status. Do not use before init. |
|---|
| 937 | * |
|---|
| 938 | * A simple function for creating or modifying a post status based on the |
|---|
| 939 | * parameters given. The function will accept an array (second optional |
|---|
| 940 | * parameter), along with a string for the post status name. |
|---|
| 941 | * |
|---|
| 942 | * Arguments prefixed with an _underscore shouldn't be used by plugins and themes. |
|---|
| 943 | * |
|---|
| 944 | * @since 3.0.0 |
|---|
| 945 | * @global array $wp_post_statuses Inserts new post status object into the list |
|---|
| 946 | * |
|---|
| 947 | * @param string $post_status Name of the post status. |
|---|
| 948 | * @param array|string $args { |
|---|
| 949 | * Optional. Array or string of post status arguments. |
|---|
| 950 | * |
|---|
| 951 | * @type bool|string $label A descriptive name for the post status marked |
|---|
| 952 | * for translation. Defaults to value of $post_status. |
|---|
| 953 | * @type bool|array $label_count Descriptive text to use for nooped plurals. |
|---|
| 954 | * Default array of $label, twice |
|---|
| 955 | * @type bool $exclude_from_search Whether to exclude posts with this post status |
|---|
| 956 | * from search results. Default is value of $internal. |
|---|
| 957 | * @type bool $_builtin Whether the status is built-in. Core-use only. |
|---|
| 958 | * Default false. |
|---|
| 959 | * @type bool $public Whether posts of this status should be shown |
|---|
| 960 | * in the front end of the site. Default false. |
|---|
| 961 | * @type bool $internal Whether the status is for internal use only. |
|---|
| 962 | * Default false. |
|---|
| 963 | * @type bool $protected Whether posts with this status should be protected. |
|---|
| 964 | * Default false. |
|---|
| 965 | * @type bool $private Whether posts with this status should be private. |
|---|
| 966 | * Default false. |
|---|
| 967 | * @type bool $publicly_queryable Whether posts with this status should be publicly- |
|---|
| 968 | * queryable. Default is value of $public. |
|---|
| 969 | * @type bool $show_in_admin_all_list Whether to include posts in the edit listing for |
|---|
| 970 | * their post type. Default is value of $internal. |
|---|
| 971 | * @type bool $show_in_admin_status_list Show in the list of statuses with post counts at |
|---|
| 972 | * the top of the edit listings, |
|---|
| 973 | * e.g. All (12) | Published (9) | My Custom Status (2) |
|---|
| 974 | * Default is value of $internal. |
|---|
| 975 | * } |
|---|
| 976 | * @return object |
|---|
| 977 | */ |
|---|
| 978 | function register_post_status( $post_status, $args = array() ) { |
|---|
| 979 | global $wp_post_statuses; |
|---|
| 980 | |
|---|
| 981 | if ( ! is_array( $wp_post_statuses ) ) { |
|---|
| 982 | $wp_post_statuses = array(); |
|---|
| 983 | } |
|---|
| 984 | |
|---|
| 985 | // Args prefixed with an underscore are reserved for internal use. |
|---|
| 986 | $defaults = array( |
|---|
| 987 | 'label' => false, |
|---|
| 988 | 'label_count' => false, |
|---|
| 989 | 'exclude_from_search' => null, |
|---|
| 990 | '_builtin' => false, |
|---|
| 991 | 'public' => null, |
|---|
| 992 | 'internal' => null, |
|---|
| 993 | 'protected' => null, |
|---|
| 994 | 'private' => null, |
|---|
| 995 | 'publicly_queryable' => null, |
|---|
| 996 | 'show_in_admin_status_list' => null, |
|---|
| 997 | 'show_in_admin_all_list' => null, |
|---|
| 998 | ); |
|---|
| 999 | $args = wp_parse_args( $args, $defaults ); |
|---|
| 1000 | $args = (object) $args; |
|---|
| 1001 | |
|---|
| 1002 | $post_status = sanitize_key( $post_status ); |
|---|
| 1003 | $args->name = $post_status; |
|---|
| 1004 | |
|---|
| 1005 | // Set various defaults. |
|---|
| 1006 | if ( null === $args->public && null === $args->internal && null === $args->protected && null === $args->private ) { |
|---|
| 1007 | $args->internal = true; |
|---|
| 1008 | } |
|---|
| 1009 | |
|---|
| 1010 | if ( null === $args->public ) { |
|---|
| 1011 | $args->public = false; |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | if ( null === $args->private ) { |
|---|
| 1015 | $args->private = false; |
|---|
| 1016 | } |
|---|
| 1017 | |
|---|
| 1018 | if ( null === $args->protected ) { |
|---|
| 1019 | $args->protected = false; |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| 1022 | if ( null === $args->internal ) { |
|---|
| 1023 | $args->internal = false; |
|---|
| 1024 | } |
|---|
| 1025 | |
|---|
| 1026 | if ( null === $args->publicly_queryable ) { |
|---|
| 1027 | $args->publicly_queryable = $args->public; |
|---|
| 1028 | } |
|---|
| 1029 | |
|---|
| 1030 | if ( null === $args->exclude_from_search ) { |
|---|
| 1031 | $args->exclude_from_search = $args->internal; |
|---|
| 1032 | } |
|---|
| 1033 | |
|---|
| 1034 | if ( null === $args->show_in_admin_all_list ) { |
|---|
| 1035 | $args->show_in_admin_all_list = ! $args->internal; |
|---|
| 1036 | } |
|---|
| 1037 | |
|---|
| 1038 | if ( null === $args->show_in_admin_status_list ) { |
|---|
| 1039 | $args->show_in_admin_status_list = ! $args->internal; |
|---|
| 1040 | } |
|---|
| 1041 | |
|---|
| 1042 | if ( false === $args->label ) { |
|---|
| 1043 | $args->label = $post_status; |
|---|
| 1044 | } |
|---|
| 1045 | |
|---|
| 1046 | if ( false === $args->label_count ) { |
|---|
| 1047 | // phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralSingle,WordPress.WP.I18n.NonSingularStringLiteralPlural |
|---|
| 1048 | $args->label_count = _n_noop( $args->label, $args->label ); |
|---|
| 1049 | } |
|---|
| 1050 | |
|---|
| 1051 | $wp_post_statuses[ $post_status ] = $args; |
|---|
| 1052 | |
|---|
| 1053 | return $args; |
|---|
| 1054 | } |
|---|
| 1055 | |
|---|
| 1056 | /** |
|---|
| 1057 | * Retrieve a post status object by name. |
|---|
| 1058 | * |
|---|
| 1059 | * @since 3.0.0 |
|---|
| 1060 | * |
|---|
| 1061 | * @global array $wp_post_statuses List of post statuses. |
|---|
| 1062 | * |
|---|
| 1063 | * @see register_post_status() |
|---|
| 1064 | * |
|---|
| 1065 | * @param string $post_status The name of a registered post status. |
|---|
| 1066 | * @return object|null A post status object. |
|---|
| 1067 | */ |
|---|
| 1068 | function get_post_status_object( $post_status ) { |
|---|
| 1069 | global $wp_post_statuses; |
|---|
| 1070 | |
|---|
| 1071 | if ( empty( $wp_post_statuses[ $post_status ] ) ) { |
|---|
| 1072 | return null; |
|---|
| 1073 | } |
|---|
| 1074 | |
|---|
| 1075 | return $wp_post_statuses[ $post_status ]; |
|---|
| 1076 | } |
|---|
| 1077 | |
|---|
| 1078 | /** |
|---|
| 1079 | * Get a list of post statuses. |
|---|
| 1080 | * |
|---|
| 1081 | * @since 3.0.0 |
|---|
| 1082 | * |
|---|
| 1083 | * @global array $wp_post_statuses List of post statuses. |
|---|
| 1084 | * |
|---|
| 1085 | * @see register_post_status() |
|---|
| 1086 | * |
|---|
| 1087 | * @param array|string $args Optional. Array or string of post status arguments to compare against |
|---|
| 1088 | * properties of the global `$wp_post_statuses objects`. Default empty array. |
|---|
| 1089 | * @param string $output Optional. The type of output to return, either 'names' or 'objects'. Default 'names'. |
|---|
| 1090 | * @param string $operator Optional. The logical operation to perform. 'or' means only one element |
|---|
| 1091 | * from the array needs to match; 'and' means all elements must match. |
|---|
| 1092 | * Default 'and'. |
|---|
| 1093 | * @return array A list of post status names or objects. |
|---|
| 1094 | */ |
|---|
| 1095 | function get_post_stati( $args = array(), $output = 'names', $operator = 'and' ) { |
|---|
| 1096 | global $wp_post_statuses; |
|---|
| 1097 | |
|---|
| 1098 | $field = ( 'names' == $output ) ? 'name' : false; |
|---|
| 1099 | |
|---|
| 1100 | return wp_filter_object_list( $wp_post_statuses, $args, $operator, $field ); |
|---|
| 1101 | } |
|---|
| 1102 | |
|---|
| 1103 | /** |
|---|
| 1104 | * Whether the post type is hierarchical. |
|---|
| 1105 | * |
|---|
| 1106 | * A false return value might also mean that the post type does not exist. |
|---|
| 1107 | * |
|---|
| 1108 | * @since 3.0.0 |
|---|
| 1109 | * |
|---|
| 1110 | * @see get_post_type_object() |
|---|
| 1111 | * |
|---|
| 1112 | * @param string $post_type Post type name |
|---|
| 1113 | * @return bool Whether post type is hierarchical. |
|---|
| 1114 | */ |
|---|
| 1115 | function is_post_type_hierarchical( $post_type ) { |
|---|
| 1116 | if ( ! post_type_exists( $post_type ) ) { |
|---|
| 1117 | return false; |
|---|
| 1118 | } |
|---|
| 1119 | |
|---|
| 1120 | $post_type = get_post_type_object( $post_type ); |
|---|
| 1121 | return $post_type->hierarchical; |
|---|
| 1122 | } |
|---|
| 1123 | |
|---|
| 1124 | /** |
|---|
| 1125 | * Determines whether a post type is registered. |
|---|
| 1126 | * |
|---|
| 1127 | * For more information on this and similar theme functions, check out |
|---|
| 1128 | * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|---|
| 1129 | * Conditional Tags} article in the Theme Developer Handbook. |
|---|
| 1130 | * |
|---|
| 1131 | * @since 3.0.0 |
|---|
| 1132 | * |
|---|
| 1133 | * @see get_post_type_object() |
|---|
| 1134 | * |
|---|
| 1135 | * @param string $post_type Post type name. |
|---|
| 1136 | * @return bool Whether post type is registered. |
|---|
| 1137 | */ |
|---|
| 1138 | function post_type_exists( $post_type ) { |
|---|
| 1139 | return (bool) get_post_type_object( $post_type ); |
|---|
| 1140 | } |
|---|
| 1141 | |
|---|
| 1142 | /** |
|---|
| 1143 | * Retrieves the post type of the current post or of a given post. |
|---|
| 1144 | * |
|---|
| 1145 | * @since 2.1.0 |
|---|
| 1146 | * |
|---|
| 1147 | * @param int|WP_Post|null $post Optional. Post ID or post object. Default is global $post. |
|---|
| 1148 | * @return string|false Post type on success, false on failure. |
|---|
| 1149 | */ |
|---|
| 1150 | function get_post_type( $post = null ) { |
|---|
| 1151 | if ( $post = get_post( $post ) ) { |
|---|
| 1152 | return $post->post_type; |
|---|
| 1153 | } |
|---|
| 1154 | |
|---|
| 1155 | return false; |
|---|
| 1156 | } |
|---|
| 1157 | |
|---|
| 1158 | /** |
|---|
| 1159 | * Retrieves a post type object by name. |
|---|
| 1160 | * |
|---|
| 1161 | * @since 3.0.0 |
|---|
| 1162 | * @since 4.6.0 Object returned is now an instance of `WP_Post_Type`. |
|---|
| 1163 | * |
|---|
| 1164 | * @global array $wp_post_types List of post types. |
|---|
| 1165 | * |
|---|
| 1166 | * @see register_post_type() |
|---|
| 1167 | * |
|---|
| 1168 | * @param string $post_type The name of a registered post type. |
|---|
| 1169 | * @return WP_Post_Type|null WP_Post_Type object if it exists, null otherwise. |
|---|
| 1170 | */ |
|---|
| 1171 | function get_post_type_object( $post_type ) { |
|---|
| 1172 | global $wp_post_types; |
|---|
| 1173 | |
|---|
| 1174 | if ( ! is_scalar( $post_type ) || empty( $wp_post_types[ $post_type ] ) ) { |
|---|
| 1175 | return null; |
|---|
| 1176 | } |
|---|
| 1177 | |
|---|
| 1178 | return $wp_post_types[ $post_type ]; |
|---|
| 1179 | } |
|---|
| 1180 | |
|---|
| 1181 | /** |
|---|
| 1182 | * Get a list of all registered post type objects. |
|---|
| 1183 | * |
|---|
| 1184 | * @since 2.9.0 |
|---|
| 1185 | * |
|---|
| 1186 | * @global array $wp_post_types List of post types. |
|---|
| 1187 | * |
|---|
| 1188 | * @see register_post_type() for accepted arguments. |
|---|
| 1189 | * |
|---|
| 1190 | * @param array|string $args Optional. An array of key => value arguments to match against |
|---|
| 1191 | * the post type objects. Default empty array. |
|---|
| 1192 | * @param string $output Optional. The type of output to return. Accepts post type 'names' |
|---|
| 1193 | * or 'objects'. Default 'names'. |
|---|
| 1194 | * @param string $operator Optional. The logical operation to perform. 'or' means only one |
|---|
| 1195 | * element from the array needs to match; 'and' means all elements |
|---|
| 1196 | * must match; 'not' means no elements may match. Default 'and'. |
|---|
| 1197 | * @return string[]|WP_Post_Type[] An array of post type names or objects. |
|---|
| 1198 | */ |
|---|
| 1199 | function get_post_types( $args = array(), $output = 'names', $operator = 'and' ) { |
|---|
| 1200 | global $wp_post_types; |
|---|
| 1201 | |
|---|
| 1202 | $field = ( 'names' == $output ) ? 'name' : false; |
|---|
| 1203 | |
|---|
| 1204 | return wp_filter_object_list( $wp_post_types, $args, $operator, $field ); |
|---|
| 1205 | } |
|---|
| 1206 | |
|---|
| 1207 | /** |
|---|
| 1208 | * Registers a post type. |
|---|
| 1209 | * |
|---|
| 1210 | * Note: Post type registrations should not be hooked before the |
|---|
| 1211 | * {@see 'init'} action. Also, any taxonomy connections should be |
|---|
| 1212 | * registered via the `$taxonomies` argument to ensure consistency |
|---|
| 1213 | * when hooks such as {@see 'parse_query'} or {@see 'pre_get_posts'} |
|---|
| 1214 | * are used. |
|---|
| 1215 | * |
|---|
| 1216 | * Post types can support any number of built-in core features such |
|---|
| 1217 | * as meta boxes, custom fields, post thumbnails, post statuses, |
|---|
| 1218 | * comments, and more. See the `$supports` argument for a complete |
|---|
| 1219 | * list of supported features. |
|---|
| 1220 | * |
|---|
| 1221 | * @since 2.9.0 |
|---|
| 1222 | * @since 3.0.0 The `show_ui` argument is now enforced on the new post screen. |
|---|
| 1223 | * @since 4.4.0 The `show_ui` argument is now enforced on the post type listing |
|---|
| 1224 | * screen and post editing screen. |
|---|
| 1225 | * @since 4.6.0 Post type object returned is now an instance of `WP_Post_Type`. |
|---|
| 1226 | * @since 4.7.0 Introduced `show_in_rest`, `rest_base` and `rest_controller_class` |
|---|
| 1227 | * arguments to register the post type in REST API. |
|---|
| 1228 | * |
|---|
| 1229 | * @global array $wp_post_types List of post types. |
|---|
| 1230 | * |
|---|
| 1231 | * @param string $post_type Post type key. Must not exceed 20 characters and may |
|---|
| 1232 | * only contain lowercase alphanumeric characters, dashes, |
|---|
| 1233 | * and underscores. See sanitize_key(). |
|---|
| 1234 | * @param array|string $args { |
|---|
| 1235 | * Array or string of arguments for registering a post type. |
|---|
| 1236 | * |
|---|
| 1237 | * @type string $label Name of the post type shown in the menu. Usually plural. |
|---|
| 1238 | * Default is value of $labels['name']. |
|---|
| 1239 | * @type array $labels An array of labels for this post type. If not set, post |
|---|
| 1240 | * labels are inherited for non-hierarchical types and page |
|---|
| 1241 | * labels for hierarchical ones. See get_post_type_labels() for a full |
|---|
| 1242 | * list of supported labels. |
|---|
| 1243 | * @type string $description A short descriptive summary of what the post type is. |
|---|
| 1244 | * Default empty. |
|---|
| 1245 | * @type bool $public Whether a post type is intended for use publicly either via |
|---|
| 1246 | * the admin interface or by front-end users. While the default |
|---|
| 1247 | * settings of $exclude_from_search, $publicly_queryable, $show_ui, |
|---|
| 1248 | * and $show_in_nav_menus are inherited from public, each does not |
|---|
| 1249 | * rely on this relationship and controls a very specific intention. |
|---|
| 1250 | * Default false. |
|---|
| 1251 | * @type bool $hierarchical Whether the post type is hierarchical (e.g. page). Default false. |
|---|
| 1252 | * @type bool $exclude_from_search Whether to exclude posts with this post type from front end search |
|---|
| 1253 | * results. Default is the opposite value of $public. |
|---|
| 1254 | * @type bool $publicly_queryable Whether queries can be performed on the front end for the post type |
|---|
| 1255 | * as part of parse_request(). Endpoints would include: |
|---|
| 1256 | * * ?post_type={post_type_key} |
|---|
| 1257 | * * ?{post_type_key}={single_post_slug} |
|---|
| 1258 | * * ?{post_type_query_var}={single_post_slug} |
|---|
| 1259 | * If not set, the default is inherited from $public. |
|---|
| 1260 | * @type bool $show_ui Whether to generate and allow a UI for managing this post type in the |
|---|
| 1261 | * admin. Default is value of $public. |
|---|
| 1262 | * @type bool $show_in_menu Where to show the post type in the admin menu. To work, $show_ui |
|---|
| 1263 | * must be true. If true, the post type is shown in its own top level |
|---|
| 1264 | * menu. If false, no menu is shown. If a string of an existing top |
|---|
| 1265 | * level menu (eg. 'tools.php' or 'edit.php?post_type=page'), the post |
|---|
| 1266 | * type will be placed as a sub-menu of that. |
|---|
| 1267 | * Default is value of $show_ui. |
|---|
| 1268 | * @type bool $show_in_nav_menus Makes this post type available for selection in navigation menus. |
|---|
| 1269 | * Default is value $public. |
|---|
| 1270 | * @type bool $show_in_admin_bar Makes this post type available via the admin bar. Default is value |
|---|
| 1271 | * of $show_in_menu. |
|---|
| 1272 | * @type bool $show_in_rest Whether to add the post type route in the REST API 'wp/v2' namespace. |
|---|
| 1273 | * @type string $rest_base To change the base url of REST API route. Default is $post_type. |
|---|
| 1274 | * @type string $rest_controller_class REST API Controller class name. Default is 'WP_REST_Posts_Controller'. |
|---|
| 1275 | * @type int $menu_position The position in the menu order the post type should appear. To work, |
|---|
| 1276 | * $show_in_menu must be true. Default null (at the bottom). |
|---|
| 1277 | * @type string $menu_icon The url to the icon to be used for this menu. Pass a base64-encoded |
|---|
| 1278 | * SVG using a data URI, which will be colored to match the color scheme |
|---|
| 1279 | * -- this should begin with 'data:image/svg+xml;base64,'. Pass the name |
|---|
| 1280 | * of a Dashicons helper class to use a font icon, e.g. |
|---|
| 1281 | * 'dashicons-chart-pie'. Pass 'none' to leave div.wp-menu-image empty |
|---|
| 1282 | * so an icon can be added via CSS. Defaults to use the posts icon. |
|---|
| 1283 | * @type string $capability_type The string to use to build the read, edit, and delete capabilities. |
|---|
| 1284 | * May be passed as an array to allow for alternative plurals when using |
|---|
| 1285 | * this argument as a base to construct the capabilities, e.g. |
|---|
| 1286 | * array('story', 'stories'). Default 'post'. |
|---|
| 1287 | * @type array $capabilities Array of capabilities for this post type. $capability_type is used |
|---|
| 1288 | * as a base to construct capabilities by default. |
|---|
| 1289 | * See get_post_type_capabilities(). |
|---|
| 1290 | * @type bool $map_meta_cap Whether to use the internal default meta capability handling. |
|---|
| 1291 | * Default false. |
|---|
| 1292 | * @type array $supports Core feature(s) the post type supports. Serves as an alias for calling |
|---|
| 1293 | * add_post_type_support() directly. Core features include 'title', |
|---|
| 1294 | * 'editor', 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', |
|---|
| 1295 | * 'page-attributes', 'thumbnail', 'custom-fields', and 'post-formats'. |
|---|
| 1296 | * Additionally, the 'revisions' feature dictates whether the post type |
|---|
| 1297 | * will store revisions, and the 'comments' feature dictates whether the |
|---|
| 1298 | * comments count will show on the edit screen. Defaults is an array |
|---|
| 1299 | * containing 'title' and 'editor'. |
|---|
| 1300 | * @type callable $register_meta_box_cb Provide a callback function that sets up the meta boxes for the |
|---|
| 1301 | * edit form. Do remove_meta_box() and add_meta_box() calls in the |
|---|
| 1302 | * callback. Default null. |
|---|
| 1303 | * @type array $taxonomies An array of taxonomy identifiers that will be registered for the |
|---|
| 1304 | * post type. Taxonomies can be registered later with register_taxonomy() |
|---|
| 1305 | * or register_taxonomy_for_object_type(). |
|---|
| 1306 | * Default empty array. |
|---|
| 1307 | * @type bool|string $has_archive Whether there should be post type archives, or if a string, the |
|---|
| 1308 | * archive slug to use. Will generate the proper rewrite rules if |
|---|
| 1309 | * $rewrite is enabled. Default false. |
|---|
| 1310 | * @type bool|array $rewrite { |
|---|
| 1311 | * Triggers the handling of rewrites for this post type. To prevent rewrite, set to false. |
|---|
| 1312 | * Defaults to true, using $post_type as slug. To specify rewrite rules, an array can be |
|---|
| 1313 | * passed with any of these keys: |
|---|
| 1314 | * |
|---|
| 1315 | * @type string $slug Customize the permastruct slug. Defaults to $post_type key. |
|---|
| 1316 | * @type bool $with_front Whether the permastruct should be prepended with WP_Rewrite::$front. |
|---|
| 1317 | * Default true. |
|---|
| 1318 | * @type bool $feeds Whether the feed permastruct should be built for this post type. |
|---|
| 1319 | * Default is value of $has_archive. |
|---|
| 1320 | * @type bool $pages Whether the permastruct should provide for pagination. Default true. |
|---|
| 1321 | * @type const $ep_mask Endpoint mask to assign. If not specified and permalink_epmask is set, |
|---|
| 1322 | * inherits from $permalink_epmask. If not specified and permalink_epmask |
|---|
| 1323 | * is not set, defaults to EP_PERMALINK. |
|---|
| 1324 | * } |
|---|
| 1325 | * @type string|bool $query_var Sets the query_var key for this post type. Defaults to $post_type |
|---|
| 1326 | * key. If false, a post type cannot be loaded at |
|---|
| 1327 | * ?{query_var}={post_slug}. If specified as a string, the query |
|---|
| 1328 | * ?{query_var_string}={post_slug} will be valid. |
|---|
| 1329 | * @type bool $can_export Whether to allow this post type to be exported. Default true. |
|---|
| 1330 | * @type bool $delete_with_user Whether to delete posts of this type when deleting a user. If true, |
|---|
| 1331 | * posts of this type belonging to the user will be moved to trash |
|---|
| 1332 | * when then user is deleted. If false, posts of this type belonging |
|---|
| 1333 | * to the user will *not* be trashed or deleted. If not set (the default), |
|---|
| 1334 | * posts are trashed if post_type_supports('author'). Otherwise posts |
|---|
| 1335 | * are not trashed or deleted. Default null. |
|---|
| 1336 | * @type bool $_builtin FOR INTERNAL USE ONLY! True if this post type is a native or |
|---|
| 1337 | * "built-in" post_type. Default false. |
|---|
| 1338 | * @type string $_edit_link FOR INTERNAL USE ONLY! URL segment to use for edit link of |
|---|
| 1339 | * this post type. Default 'post.php?post=%d'. |
|---|
| 1340 | * } |
|---|
| 1341 | * @return WP_Post_Type|WP_Error The registered post type object, or an error object. |
|---|
| 1342 | */ |
|---|
| 1343 | function register_post_type( $post_type, $args = array() ) { |
|---|
| 1344 | global $wp_post_types; |
|---|
| 1345 | |
|---|
| 1346 | if ( ! is_array( $wp_post_types ) ) { |
|---|
| 1347 | $wp_post_types = array(); |
|---|
| 1348 | } |
|---|
| 1349 | |
|---|
| 1350 | // Sanitize post type name |
|---|
| 1351 | $post_type = sanitize_key( $post_type ); |
|---|
| 1352 | |
|---|
| 1353 | if ( empty( $post_type ) || strlen( $post_type ) > 20 ) { |
|---|
| 1354 | _doing_it_wrong( __FUNCTION__, __( 'Post type names must be between 1 and 20 characters in length.' ), '4.2.0' ); |
|---|
| 1355 | return new WP_Error( 'post_type_length_invalid', __( 'Post type names must be between 1 and 20 characters in length.' ) ); |
|---|
| 1356 | } |
|---|
| 1357 | |
|---|
| 1358 | $post_type_object = new WP_Post_Type( $post_type, $args ); |
|---|
| 1359 | $post_type_object->add_supports(); |
|---|
| 1360 | $post_type_object->add_rewrite_rules(); |
|---|
| 1361 | $post_type_object->register_meta_boxes(); |
|---|
| 1362 | |
|---|
| 1363 | $wp_post_types[ $post_type ] = $post_type_object; |
|---|
| 1364 | |
|---|
| 1365 | $post_type_object->add_hooks(); |
|---|
| 1366 | $post_type_object->register_taxonomies(); |
|---|
| 1367 | |
|---|
| 1368 | /** |
|---|
| 1369 | * Fires after a post type is registered. |
|---|
| 1370 | * |
|---|
| 1371 | * @since 3.3.0 |
|---|
| 1372 | * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
|---|
| 1373 | * |
|---|
| 1374 | * @param string $post_type Post type. |
|---|
| 1375 | * @param WP_Post_Type $post_type_object Arguments used to register the post type. |
|---|
| 1376 | */ |
|---|
| 1377 | do_action( 'registered_post_type', $post_type, $post_type_object ); |
|---|
| 1378 | |
|---|
| 1379 | return $post_type_object; |
|---|
| 1380 | } |
|---|
| 1381 | |
|---|
| 1382 | /** |
|---|
| 1383 | * Unregisters a post type. |
|---|
| 1384 | * |
|---|
| 1385 | * Can not be used to unregister built-in post types. |
|---|
| 1386 | * |
|---|
| 1387 | * @since 4.5.0 |
|---|
| 1388 | * |
|---|
| 1389 | * @global array $wp_post_types List of post types. |
|---|
| 1390 | * |
|---|
| 1391 | * @param string $post_type Post type to unregister. |
|---|
| 1392 | * @return bool|WP_Error True on success, WP_Error on failure or if the post type doesn't exist. |
|---|
| 1393 | */ |
|---|
| 1394 | function unregister_post_type( $post_type ) { |
|---|
| 1395 | global $wp_post_types; |
|---|
| 1396 | |
|---|
| 1397 | if ( ! post_type_exists( $post_type ) ) { |
|---|
| 1398 | return new WP_Error( 'invalid_post_type', __( 'Invalid post type.' ) ); |
|---|
| 1399 | } |
|---|
| 1400 | |
|---|
| 1401 | $post_type_object = get_post_type_object( $post_type ); |
|---|
| 1402 | |
|---|
| 1403 | // Do not allow unregistering internal post types. |
|---|
| 1404 | if ( $post_type_object->_builtin ) { |
|---|
| 1405 | return new WP_Error( 'invalid_post_type', __( 'Unregistering a built-in post type is not allowed' ) ); |
|---|
| 1406 | } |
|---|
| 1407 | |
|---|
| 1408 | $post_type_object->remove_supports(); |
|---|
| 1409 | $post_type_object->remove_rewrite_rules(); |
|---|
| 1410 | $post_type_object->unregister_meta_boxes(); |
|---|
| 1411 | $post_type_object->remove_hooks(); |
|---|
| 1412 | $post_type_object->unregister_taxonomies(); |
|---|
| 1413 | |
|---|
| 1414 | unset( $wp_post_types[ $post_type ] ); |
|---|
| 1415 | |
|---|
| 1416 | /** |
|---|
| 1417 | * Fires after a post type was unregistered. |
|---|
| 1418 | * |
|---|
| 1419 | * @since 4.5.0 |
|---|
| 1420 | * |
|---|
| 1421 | * @param string $post_type Post type key. |
|---|
| 1422 | */ |
|---|
| 1423 | do_action( 'unregistered_post_type', $post_type ); |
|---|
| 1424 | |
|---|
| 1425 | return true; |
|---|
| 1426 | } |
|---|
| 1427 | |
|---|
| 1428 | /** |
|---|
| 1429 | * Build an object with all post type capabilities out of a post type object |
|---|
| 1430 | * |
|---|
| 1431 | * Post type capabilities use the 'capability_type' argument as a base, if the |
|---|
| 1432 | * capability is not set in the 'capabilities' argument array or if the |
|---|
| 1433 | * 'capabilities' argument is not supplied. |
|---|
| 1434 | * |
|---|
| 1435 | * The capability_type argument can optionally be registered as an array, with |
|---|
| 1436 | * the first value being singular and the second plural, e.g. array('story, 'stories') |
|---|
| 1437 | * Otherwise, an 's' will be added to the value for the plural form. After |
|---|
| 1438 | * registration, capability_type will always be a string of the singular value. |
|---|
| 1439 | * |
|---|
| 1440 | * By default, seven keys are accepted as part of the capabilities array: |
|---|
| 1441 | * |
|---|
| 1442 | * - edit_post, read_post, and delete_post are meta capabilities, which are then |
|---|
| 1443 | * generally mapped to corresponding primitive capabilities depending on the |
|---|
| 1444 | * context, which would be the post being edited/read/deleted and the user or |
|---|
| 1445 | * role being checked. Thus these capabilities would generally not be granted |
|---|
| 1446 | * directly to users or roles. |
|---|
| 1447 | * |
|---|
| 1448 | * - edit_posts - Controls whether objects of this post type can be edited. |
|---|
| 1449 | * - edit_others_posts - Controls whether objects of this type owned by other users |
|---|
| 1450 | * can be edited. If the post type does not support an author, then this will |
|---|
| 1451 | * behave like edit_posts. |
|---|
| 1452 | * - publish_posts - Controls publishing objects of this post type. |
|---|
| 1453 | * - read_private_posts - Controls whether private objects can be read. |
|---|
| 1454 | * |
|---|
| 1455 | * These four primitive capabilities are checked in core in various locations. |
|---|
| 1456 | * There are also seven other primitive capabilities which are not referenced |
|---|
| 1457 | * directly in core, except in map_meta_cap(), which takes the three aforementioned |
|---|
| 1458 | * meta capabilities and translates them into one or more primitive capabilities |
|---|
| 1459 | * that must then be checked against the user or role, depending on the context. |
|---|
| 1460 | * |
|---|
| 1461 | * - read - Controls whether objects of this post type can be read. |
|---|
| 1462 | * - delete_posts - Controls whether objects of this post type can be deleted. |
|---|
| 1463 | * - delete_private_posts - Controls whether private objects can be deleted. |
|---|
| 1464 | * - delete_published_posts - Controls whether published objects can be deleted. |
|---|
| 1465 | * - delete_others_posts - Controls whether objects owned by other users can be |
|---|
| 1466 | * can be deleted. If the post type does not support an author, then this will |
|---|
| 1467 | * behave like delete_posts. |
|---|
| 1468 | * - edit_private_posts - Controls whether private objects can be edited. |
|---|
| 1469 | * - edit_published_posts - Controls whether published objects can be edited. |
|---|
| 1470 | * |
|---|
| 1471 | * These additional capabilities are only used in map_meta_cap(). Thus, they are |
|---|
| 1472 | * only assigned by default if the post type is registered with the 'map_meta_cap' |
|---|
| 1473 | * argument set to true (default is false). |
|---|
| 1474 | * |
|---|
| 1475 | * @since 3.0.0 |
|---|
| 1476 | * |
|---|
| 1477 | * @see register_post_type() |
|---|
| 1478 | * @see map_meta_cap() |
|---|
| 1479 | * |
|---|
| 1480 | * @param object $args Post type registration arguments. |
|---|
| 1481 | * @return object Object with all the capabilities as member variables. |
|---|
| 1482 | */ |
|---|
| 1483 | function get_post_type_capabilities( $args ) { |
|---|
| 1484 | if ( ! is_array( $args->capability_type ) ) { |
|---|
| 1485 | $args->capability_type = array( $args->capability_type, $args->capability_type . 's' ); |
|---|
| 1486 | } |
|---|
| 1487 | |
|---|
| 1488 | // Singular base for meta capabilities, plural base for primitive capabilities. |
|---|
| 1489 | list( $singular_base, $plural_base ) = $args->capability_type; |
|---|
| 1490 | |
|---|
| 1491 | $default_capabilities = array( |
|---|
| 1492 | // Meta capabilities |
|---|
| 1493 | 'edit_post' => 'edit_' . $singular_base, |
|---|
| 1494 | 'read_post' => 'read_' . $singular_base, |
|---|
| 1495 | 'delete_post' => 'delete_' . $singular_base, |
|---|
| 1496 | // Primitive capabilities used outside of map_meta_cap(): |
|---|
| 1497 | 'edit_posts' => 'edit_' . $plural_base, |
|---|
| 1498 | 'edit_others_posts' => 'edit_others_' . $plural_base, |
|---|
| 1499 | 'publish_posts' => 'publish_' . $plural_base, |
|---|
| 1500 | 'read_private_posts' => 'read_private_' . $plural_base, |
|---|
| 1501 | ); |
|---|
| 1502 | |
|---|
| 1503 | // Primitive capabilities used within map_meta_cap(): |
|---|
| 1504 | if ( $args->map_meta_cap ) { |
|---|
| 1505 | $default_capabilities_for_mapping = array( |
|---|
| 1506 | 'read' => 'read', |
|---|
| 1507 | 'delete_posts' => 'delete_' . $plural_base, |
|---|
| 1508 | 'delete_private_posts' => 'delete_private_' . $plural_base, |
|---|
| 1509 | 'delete_published_posts' => 'delete_published_' . $plural_base, |
|---|
| 1510 | 'delete_others_posts' => 'delete_others_' . $plural_base, |
|---|
| 1511 | 'edit_private_posts' => 'edit_private_' . $plural_base, |
|---|
| 1512 | 'edit_published_posts' => 'edit_published_' . $plural_base, |
|---|
| 1513 | ); |
|---|
| 1514 | $default_capabilities = array_merge( $default_capabilities, $default_capabilities_for_mapping ); |
|---|
| 1515 | } |
|---|
| 1516 | |
|---|
| 1517 | $capabilities = array_merge( $default_capabilities, $args->capabilities ); |
|---|
| 1518 | |
|---|
| 1519 | // Post creation capability simply maps to edit_posts by default: |
|---|
| 1520 | if ( ! isset( $capabilities['create_posts'] ) ) { |
|---|
| 1521 | $capabilities['create_posts'] = $capabilities['edit_posts']; |
|---|
| 1522 | } |
|---|
| 1523 | |
|---|
| 1524 | // Remember meta capabilities for future reference. |
|---|
| 1525 | if ( $args->map_meta_cap ) { |
|---|
| 1526 | _post_type_meta_capabilities( $capabilities ); |
|---|
| 1527 | } |
|---|
| 1528 | |
|---|
| 1529 | return (object) $capabilities; |
|---|
| 1530 | } |
|---|
| 1531 | |
|---|
| 1532 | /** |
|---|
| 1533 | * Store or return a list of post type meta caps for map_meta_cap(). |
|---|
| 1534 | * |
|---|
| 1535 | * @since 3.1.0 |
|---|
| 1536 | * @access private |
|---|
| 1537 | * |
|---|
| 1538 | * @global array $post_type_meta_caps Used to store meta capabilities. |
|---|
| 1539 | * |
|---|
| 1540 | * @param array $capabilities Post type meta capabilities. |
|---|
| 1541 | */ |
|---|
| 1542 | function _post_type_meta_capabilities( $capabilities = null ) { |
|---|
| 1543 | global $post_type_meta_caps; |
|---|
| 1544 | |
|---|
| 1545 | foreach ( $capabilities as $core => $custom ) { |
|---|
| 1546 | if ( in_array( $core, array( 'read_post', 'delete_post', 'edit_post' ) ) ) { |
|---|
| 1547 | $post_type_meta_caps[ $custom ] = $core; |
|---|
| 1548 | } |
|---|
| 1549 | } |
|---|
| 1550 | } |
|---|
| 1551 | |
|---|
| 1552 | /** |
|---|
| 1553 | * Builds an object with all post type labels out of a post type object. |
|---|
| 1554 | * |
|---|
| 1555 | * Accepted keys of the label array in the post type object: |
|---|
| 1556 | * |
|---|
| 1557 | * - `name` - General name for the post type, usually plural. The same and overridden |
|---|
| 1558 | * by `$post_type_object->label`. Default is 'Posts' / 'Pages'. |
|---|
| 1559 | * - `singular_name` - Name for one object of this post type. Default is 'Post' / 'Page'. |
|---|
| 1560 | * - `add_new` - Default is 'Add New' for both hierarchical and non-hierarchical types. |
|---|
| 1561 | * When internationalizing this string, please use a {@link https://codex.wordpress.org/I18n_for_WordPress_Developers#Disambiguation_by_context gettext context} |
|---|
| 1562 | * matching your post type. Example: `_x( 'Add New', 'product', 'textdomain' );`. |
|---|
| 1563 | * - `add_new_item` - Label for adding a new singular item. Default is 'Add New Post' / 'Add New Page'. |
|---|
| 1564 | * - `edit_item` - Label for editing a singular item. Default is 'Edit Post' / 'Edit Page'. |
|---|
| 1565 | * - `new_item` - Label for the new item page title. Default is 'New Post' / 'New Page'. |
|---|
| 1566 | * - `view_item` - Label for viewing a singular item. Default is 'View Post' / 'View Page'. |
|---|
| 1567 | * - `view_items` - Label for viewing post type archives. Default is 'View Posts' / 'View Pages'. |
|---|
| 1568 | * - `search_items` - Label for searching plural items. Default is 'Search Posts' / 'Search Pages'. |
|---|
| 1569 | * - `not_found` - Label used when no items are found. Default is 'No posts found' / 'No pages found'. |
|---|
| 1570 | * - `not_found_in_trash` - Label used when no items are in the trash. Default is 'No posts found in Trash' / |
|---|
| 1571 | * 'No pages found in Trash'. |
|---|
| 1572 | * - `parent_item_colon` - Label used to prefix parents of hierarchical items. Not used on non-hierarchical |
|---|
| 1573 | * post types. Default is 'Parent Page:'. |
|---|
| 1574 | * - `all_items` - Label to signify all items in a submenu link. Default is 'All Posts' / 'All Pages'. |
|---|
| 1575 | * - `archives` - Label for archives in nav menus. Default is 'Post Archives' / 'Page Archives'. |
|---|
| 1576 | * - `attributes` - Label for the attributes meta box. Default is 'Post Attributes' / 'Page Attributes'. |
|---|
| 1577 | * - `insert_into_item` - Label for the media frame button. Default is 'Insert into post' / 'Insert into page'. |
|---|
| 1578 | * - `uploaded_to_this_item` - Label for the media frame filter. Default is 'Uploaded to this post' / |
|---|
| 1579 | * 'Uploaded to this page'. |
|---|
| 1580 | * - `featured_image` - Label for the Featured Image meta box title. Default is 'Featured Image'. |
|---|
| 1581 | * - `set_featured_image` - Label for setting the featured image. Default is 'Set featured image'. |
|---|
| 1582 | * - `remove_featured_image` - Label for removing the featured image. Default is 'Remove featured image'. |
|---|
| 1583 | * - `use_featured_image` - Label in the media frame for using a featured image. Default is 'Use as featured image'. |
|---|
| 1584 | * - `menu_name` - Label for the menu name. Default is the same as `name`. |
|---|
| 1585 | * - `filter_items_list` - Label for the table views hidden heading. Default is 'Filter posts list' / |
|---|
| 1586 | * 'Filter pages list'. |
|---|
| 1587 | * - `items_list_navigation` - Label for the table pagination hidden heading. Default is 'Posts list navigation' / |
|---|
| 1588 | * 'Pages list navigation'. |
|---|
| 1589 | * - `items_list` - Label for the table hidden heading. Default is 'Posts list' / 'Pages list'. |
|---|
| 1590 | * - `item_published` - Label used when an item is published. Default is 'Post published.' / 'Page published.' |
|---|
| 1591 | * - `item_published_privately` - Label used when an item is published with private visibility. |
|---|
| 1592 | * Default is 'Post published privately.' / 'Page published privately.' |
|---|
| 1593 | * - `item_reverted_to_draft` - Label used when an item is switched to a draft. |
|---|
| 1594 | * Default is 'Post reverted to draft.' / 'Page reverted to draft.' |
|---|
| 1595 | * - `item_scheduled` - Label used when an item is scheduled for publishing. Default is 'Post scheduled.' / |
|---|
| 1596 | * 'Page scheduled.' |
|---|
| 1597 | * - `item_updated` - Label used when an item is updated. Default is 'Post updated.' / 'Page updated.' |
|---|
| 1598 | * |
|---|
| 1599 | * Above, the first default value is for non-hierarchical post types (like posts) |
|---|
| 1600 | * and the second one is for hierarchical post types (like pages). |
|---|
| 1601 | * |
|---|
| 1602 | * Note: To set labels used in post type admin notices, see the {@see 'post_updated_messages'} filter. |
|---|
| 1603 | * |
|---|
| 1604 | * @since 3.0.0 |
|---|
| 1605 | * @since 4.3.0 Added the `featured_image`, `set_featured_image`, `remove_featured_image`, |
|---|
| 1606 | * and `use_featured_image` labels. |
|---|
| 1607 | * @since 4.4.0 Added the `archives`, `insert_into_item`, `uploaded_to_this_item`, `filter_items_list`, |
|---|
| 1608 | * `items_list_navigation`, and `items_list` labels. |
|---|
| 1609 | * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
|---|
| 1610 | * @since 4.7.0 Added the `view_items` and `attributes` labels. |
|---|
| 1611 | * @since 5.0.0 Added the `item_published`, `item_published_privately`, `item_reverted_to_draft`, |
|---|
| 1612 | * `item_scheduled`, and `item_updated` labels. |
|---|
| 1613 | * |
|---|
| 1614 | * @access private |
|---|
| 1615 | * |
|---|
| 1616 | * @param object|WP_Post_Type $post_type_object Post type object. |
|---|
| 1617 | * @return object Object with all the labels as member variables. |
|---|
| 1618 | */ |
|---|
| 1619 | function get_post_type_labels( $post_type_object ) { |
|---|
| 1620 | $nohier_vs_hier_defaults = array( |
|---|
| 1621 | 'name' => array( _x( 'Posts', 'post type general name' ), _x( 'Pages', 'post type general name' ) ), |
|---|
| 1622 | 'singular_name' => array( _x( 'Post', 'post type singular name' ), _x( 'Page', 'post type singular name' ) ), |
|---|
| 1623 | 'add_new' => array( _x( 'Add New', 'post' ), _x( 'Add New', 'page' ) ), |
|---|
| 1624 | 'add_new_item' => array( __( 'Add New Post' ), __( 'Add New Page' ) ), |
|---|
| 1625 | 'edit_item' => array( __( 'Edit Post' ), __( 'Edit Page' ) ), |
|---|
| 1626 | 'new_item' => array( __( 'New Post' ), __( 'New Page' ) ), |
|---|
| 1627 | 'view_item' => array( __( 'View Post' ), __( 'View Page' ) ), |
|---|
| 1628 | 'view_items' => array( __( 'View Posts' ), __( 'View Pages' ) ), |
|---|
| 1629 | 'search_items' => array( __( 'Search Posts' ), __( 'Search Pages' ) ), |
|---|
| 1630 | 'not_found' => array( __( 'No posts found.' ), __( 'No pages found.' ) ), |
|---|
| 1631 | 'not_found_in_trash' => array( __( 'No posts found in Trash.' ), __( 'No pages found in Trash.' ) ), |
|---|
| 1632 | 'parent_item_colon' => array( null, __( 'Parent Page:' ) ), |
|---|
| 1633 | 'all_items' => array( __( 'All Posts' ), __( 'All Pages' ) ), |
|---|
| 1634 | 'archives' => array( __( 'Post Archives' ), __( 'Page Archives' ) ), |
|---|
| 1635 | 'attributes' => array( __( 'Post Attributes' ), __( 'Page Attributes' ) ), |
|---|
| 1636 | 'insert_into_item' => array( __( 'Insert into post' ), __( 'Insert into page' ) ), |
|---|
| 1637 | 'uploaded_to_this_item' => array( __( 'Uploaded to this post' ), __( 'Uploaded to this page' ) ), |
|---|
| 1638 | 'featured_image' => array( _x( 'Featured Image', 'post' ), _x( 'Featured Image', 'page' ) ), |
|---|
| 1639 | 'set_featured_image' => array( _x( 'Set featured image', 'post' ), _x( 'Set featured image', 'page' ) ), |
|---|
| 1640 | 'remove_featured_image' => array( _x( 'Remove featured image', 'post' ), _x( 'Remove featured image', 'page' ) ), |
|---|
| 1641 | 'use_featured_image' => array( _x( 'Use as featured image', 'post' ), _x( 'Use as featured image', 'page' ) ), |
|---|
| 1642 | 'filter_items_list' => array( __( 'Filter posts list' ), __( 'Filter pages list' ) ), |
|---|
| 1643 | 'items_list_navigation' => array( __( 'Posts list navigation' ), __( 'Pages list navigation' ) ), |
|---|
| 1644 | 'items_list' => array( __( 'Posts list' ), __( 'Pages list' ) ), |
|---|
| 1645 | 'item_published' => array( __( 'Post published.' ), __( 'Page published.' ) ), |
|---|
| 1646 | 'item_published_privately' => array( __( 'Post published privately.' ), __( 'Page published privately.' ) ), |
|---|
| 1647 | 'item_reverted_to_draft' => array( __( 'Post reverted to draft.' ), __( 'Page reverted to draft.' ) ), |
|---|
| 1648 | 'item_scheduled' => array( __( 'Post scheduled.' ), __( 'Page scheduled.' ) ), |
|---|
| 1649 | 'item_updated' => array( __( 'Post updated.' ), __( 'Page updated.' ) ), |
|---|
| 1650 | ); |
|---|
| 1651 | $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name']; |
|---|
| 1652 | |
|---|
| 1653 | $labels = _get_custom_object_labels( $post_type_object, $nohier_vs_hier_defaults ); |
|---|
| 1654 | |
|---|
| 1655 | $post_type = $post_type_object->name; |
|---|
| 1656 | |
|---|
| 1657 | $default_labels = clone $labels; |
|---|
| 1658 | |
|---|
| 1659 | /** |
|---|
| 1660 | * Filters the labels of a specific post type. |
|---|
| 1661 | * |
|---|
| 1662 | * The dynamic portion of the hook name, `$post_type`, refers to |
|---|
| 1663 | * the post type slug. |
|---|
| 1664 | * |
|---|
| 1665 | * @since 3.5.0 |
|---|
| 1666 | * |
|---|
| 1667 | * @see get_post_type_labels() for the full list of labels. |
|---|
| 1668 | * |
|---|
| 1669 | * @param object $labels Object with labels for the post type as member variables. |
|---|
| 1670 | */ |
|---|
| 1671 | $labels = apply_filters( "post_type_labels_{$post_type}", $labels ); |
|---|
| 1672 | |
|---|
| 1673 | // Ensure that the filtered labels contain all required default values. |
|---|
| 1674 | $labels = (object) array_merge( (array) $default_labels, (array) $labels ); |
|---|
| 1675 | |
|---|
| 1676 | return $labels; |
|---|
| 1677 | } |
|---|
| 1678 | |
|---|
| 1679 | /** |
|---|
| 1680 | * Build an object with custom-something object (post type, taxonomy) labels |
|---|
| 1681 | * out of a custom-something object |
|---|
| 1682 | * |
|---|
| 1683 | * @since 3.0.0 |
|---|
| 1684 | * @access private |
|---|
| 1685 | * |
|---|
| 1686 | * @param object $object A custom-something object. |
|---|
| 1687 | * @param array $nohier_vs_hier_defaults Hierarchical vs non-hierarchical default labels. |
|---|
| 1688 | * @return object Object containing labels for the given custom-something object. |
|---|
| 1689 | */ |
|---|
| 1690 | function _get_custom_object_labels( $object, $nohier_vs_hier_defaults ) { |
|---|
| 1691 | $object->labels = (array) $object->labels; |
|---|
| 1692 | |
|---|
| 1693 | if ( isset( $object->label ) && empty( $object->labels['name'] ) ) { |
|---|
| 1694 | $object->labels['name'] = $object->label; |
|---|
| 1695 | } |
|---|
| 1696 | |
|---|
| 1697 | if ( ! isset( $object->labels['singular_name'] ) && isset( $object->labels['name'] ) ) { |
|---|
| 1698 | $object->labels['singular_name'] = $object->labels['name']; |
|---|
| 1699 | } |
|---|
| 1700 | |
|---|
| 1701 | if ( ! isset( $object->labels['name_admin_bar'] ) ) { |
|---|
| 1702 | $object->labels['name_admin_bar'] = isset( $object->labels['singular_name'] ) ? $object->labels['singular_name'] : $object->name; |
|---|
| 1703 | } |
|---|
| 1704 | |
|---|
| 1705 | if ( ! isset( $object->labels['menu_name'] ) && isset( $object->labels['name'] ) ) { |
|---|
| 1706 | $object->labels['menu_name'] = $object->labels['name']; |
|---|
| 1707 | } |
|---|
| 1708 | |
|---|
| 1709 | if ( ! isset( $object->labels['all_items'] ) && isset( $object->labels['menu_name'] ) ) { |
|---|
| 1710 | $object->labels['all_items'] = $object->labels['menu_name']; |
|---|
| 1711 | } |
|---|
| 1712 | |
|---|
| 1713 | if ( ! isset( $object->labels['archives'] ) && isset( $object->labels['all_items'] ) ) { |
|---|
| 1714 | $object->labels['archives'] = $object->labels['all_items']; |
|---|
| 1715 | } |
|---|
| 1716 | |
|---|
| 1717 | $defaults = array(); |
|---|
| 1718 | foreach ( $nohier_vs_hier_defaults as $key => $value ) { |
|---|
| 1719 | $defaults[ $key ] = $object->hierarchical ? $value[1] : $value[0]; |
|---|
| 1720 | } |
|---|
| 1721 | $labels = array_merge( $defaults, $object->labels ); |
|---|
| 1722 | $object->labels = (object) $object->labels; |
|---|
| 1723 | |
|---|
| 1724 | return (object) $labels; |
|---|
| 1725 | } |
|---|
| 1726 | |
|---|
| 1727 | /** |
|---|
| 1728 | * Add submenus for post types. |
|---|
| 1729 | * |
|---|
| 1730 | * @access private |
|---|
| 1731 | * @since 3.1.0 |
|---|
| 1732 | */ |
|---|
| 1733 | function _add_post_type_submenus() { |
|---|
| 1734 | foreach ( get_post_types( array( 'show_ui' => true ) ) as $ptype ) { |
|---|
| 1735 | $ptype_obj = get_post_type_object( $ptype ); |
|---|
| 1736 | // Sub-menus only. |
|---|
| 1737 | if ( ! $ptype_obj->show_in_menu || $ptype_obj->show_in_menu === true ) { |
|---|
| 1738 | continue; |
|---|
| 1739 | } |
|---|
| 1740 | add_submenu_page( $ptype_obj->show_in_menu, $ptype_obj->labels->name, $ptype_obj->labels->all_items, $ptype_obj->cap->edit_posts, "edit.php?post_type=$ptype" ); |
|---|
| 1741 | } |
|---|
| 1742 | } |
|---|
| 1743 | |
|---|
| 1744 | /** |
|---|
| 1745 | * Register support of certain features for a post type. |
|---|
| 1746 | * |
|---|
| 1747 | * All core features are directly associated with a functional area of the edit |
|---|
| 1748 | * screen, such as the editor or a meta box. Features include: 'title', 'editor', |
|---|
| 1749 | * 'comments', 'revisions', 'trackbacks', 'author', 'excerpt', 'page-attributes', |
|---|
| 1750 | * 'thumbnail', 'custom-fields', and 'post-formats'. |
|---|
| 1751 | * |
|---|
| 1752 | * Additionally, the 'revisions' feature dictates whether the post type will |
|---|
| 1753 | * store revisions, and the 'comments' feature dictates whether the comments |
|---|
| 1754 | * count will show on the edit screen. |
|---|
| 1755 | * |
|---|
| 1756 | * @since 3.0.0 |
|---|
| 1757 | * |
|---|
| 1758 | * @global array $_wp_post_type_features |
|---|
| 1759 | * |
|---|
| 1760 | * @param string $post_type The post type for which to add the feature. |
|---|
| 1761 | * @param string|array $feature The feature being added, accepts an array of |
|---|
| 1762 | * feature strings or a single string. |
|---|
| 1763 | */ |
|---|
| 1764 | function add_post_type_support( $post_type, $feature ) { |
|---|
| 1765 | global $_wp_post_type_features; |
|---|
| 1766 | |
|---|
| 1767 | $features = (array) $feature; |
|---|
| 1768 | foreach ( $features as $feature ) { |
|---|
| 1769 | if ( func_num_args() == 2 ) { |
|---|
| 1770 | $_wp_post_type_features[ $post_type ][ $feature ] = true; |
|---|
| 1771 | } else { |
|---|
| 1772 | $_wp_post_type_features[ $post_type ][ $feature ] = array_slice( func_get_args(), 2 ); |
|---|
| 1773 | } |
|---|
| 1774 | } |
|---|
| 1775 | } |
|---|
| 1776 | |
|---|
| 1777 | /** |
|---|
| 1778 | * Remove support for a feature from a post type. |
|---|
| 1779 | * |
|---|
| 1780 | * @since 3.0.0 |
|---|
| 1781 | * |
|---|
| 1782 | * @global array $_wp_post_type_features |
|---|
| 1783 | * |
|---|
| 1784 | * @param string $post_type The post type for which to remove the feature. |
|---|
| 1785 | * @param string $feature The feature being removed. |
|---|
| 1786 | */ |
|---|
| 1787 | function remove_post_type_support( $post_type, $feature ) { |
|---|
| 1788 | global $_wp_post_type_features; |
|---|
| 1789 | |
|---|
| 1790 | unset( $_wp_post_type_features[ $post_type ][ $feature ] ); |
|---|
| 1791 | } |
|---|
| 1792 | |
|---|
| 1793 | /** |
|---|
| 1794 | * Get all the post type features |
|---|
| 1795 | * |
|---|
| 1796 | * @since 3.4.0 |
|---|
| 1797 | * |
|---|
| 1798 | * @global array $_wp_post_type_features |
|---|
| 1799 | * |
|---|
| 1800 | * @param string $post_type The post type. |
|---|
| 1801 | * @return array Post type supports list. |
|---|
| 1802 | */ |
|---|
| 1803 | function get_all_post_type_supports( $post_type ) { |
|---|
| 1804 | global $_wp_post_type_features; |
|---|
| 1805 | |
|---|
| 1806 | if ( isset( $_wp_post_type_features[ $post_type ] ) ) { |
|---|
| 1807 | return $_wp_post_type_features[ $post_type ]; |
|---|
| 1808 | } |
|---|
| 1809 | |
|---|
| 1810 | return array(); |
|---|
| 1811 | } |
|---|
| 1812 | |
|---|
| 1813 | /** |
|---|
| 1814 | * Check a post type's support for a given feature. |
|---|
| 1815 | * |
|---|
| 1816 | * @since 3.0.0 |
|---|
| 1817 | * |
|---|
| 1818 | * @global array $_wp_post_type_features |
|---|
| 1819 | * |
|---|
| 1820 | * @param string $post_type The post type being checked. |
|---|
| 1821 | * @param string $feature The feature being checked. |
|---|
| 1822 | * @return bool Whether the post type supports the given feature. |
|---|
| 1823 | */ |
|---|
| 1824 | function post_type_supports( $post_type, $feature ) { |
|---|
| 1825 | global $_wp_post_type_features; |
|---|
| 1826 | |
|---|
| 1827 | return ( isset( $_wp_post_type_features[ $post_type ][ $feature ] ) ); |
|---|
| 1828 | } |
|---|
| 1829 | |
|---|
| 1830 | /** |
|---|
| 1831 | * Retrieves a list of post type names that support a specific feature. |
|---|
| 1832 | * |
|---|
| 1833 | * @since 4.5.0 |
|---|
| 1834 | * |
|---|
| 1835 | * @global array $_wp_post_type_features Post type features |
|---|
| 1836 | * |
|---|
| 1837 | * @param array|string $feature Single feature or an array of features the post types should support. |
|---|
| 1838 | * @param string $operator Optional. The logical operation to perform. 'or' means |
|---|
| 1839 | * only one element from the array needs to match; 'and' |
|---|
| 1840 | * means all elements must match; 'not' means no elements may |
|---|
| 1841 | * match. Default 'and'. |
|---|
| 1842 | * @return array A list of post type names. |
|---|
| 1843 | */ |
|---|
| 1844 | function get_post_types_by_support( $feature, $operator = 'and' ) { |
|---|
| 1845 | global $_wp_post_type_features; |
|---|
| 1846 | |
|---|
| 1847 | $features = array_fill_keys( (array) $feature, true ); |
|---|
| 1848 | |
|---|
| 1849 | return array_keys( wp_filter_object_list( $_wp_post_type_features, $features, $operator ) ); |
|---|
| 1850 | } |
|---|
| 1851 | |
|---|
| 1852 | /** |
|---|
| 1853 | * Update the post type for the post ID. |
|---|
| 1854 | * |
|---|
| 1855 | * The page or post cache will be cleaned for the post ID. |
|---|
| 1856 | * |
|---|
| 1857 | * @since 2.5.0 |
|---|
| 1858 | * |
|---|
| 1859 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 1860 | * |
|---|
| 1861 | * @param int $post_id Optional. Post ID to change post type. Default 0. |
|---|
| 1862 | * @param string $post_type Optional. Post type. Accepts 'post' or 'page' to |
|---|
| 1863 | * name a few. Default 'post'. |
|---|
| 1864 | * @return int|false Amount of rows changed. Should be 1 for success and 0 for failure. |
|---|
| 1865 | */ |
|---|
| 1866 | function set_post_type( $post_id = 0, $post_type = 'post' ) { |
|---|
| 1867 | global $wpdb; |
|---|
| 1868 | |
|---|
| 1869 | $post_type = sanitize_post_field( 'post_type', $post_type, $post_id, 'db' ); |
|---|
| 1870 | $return = $wpdb->update( $wpdb->posts, array( 'post_type' => $post_type ), array( 'ID' => $post_id ) ); |
|---|
| 1871 | |
|---|
| 1872 | clean_post_cache( $post_id ); |
|---|
| 1873 | |
|---|
| 1874 | return $return; |
|---|
| 1875 | } |
|---|
| 1876 | |
|---|
| 1877 | /** |
|---|
| 1878 | * Determines whether a post type is considered "viewable". |
|---|
| 1879 | * |
|---|
| 1880 | * For built-in post types such as posts and pages, the 'public' value will be evaluated. |
|---|
| 1881 | * For all others, the 'publicly_queryable' value will be used. |
|---|
| 1882 | * |
|---|
| 1883 | * @since 4.4.0 |
|---|
| 1884 | * @since 4.5.0 Added the ability to pass a post type name in addition to object. |
|---|
| 1885 | * @since 4.6.0 Converted the `$post_type` parameter to accept a `WP_Post_Type` object. |
|---|
| 1886 | * |
|---|
| 1887 | * @param string|WP_Post_Type $post_type Post type name or object. |
|---|
| 1888 | * @return bool Whether the post type should be considered viewable. |
|---|
| 1889 | */ |
|---|
| 1890 | function is_post_type_viewable( $post_type ) { |
|---|
| 1891 | if ( is_scalar( $post_type ) ) { |
|---|
| 1892 | $post_type = get_post_type_object( $post_type ); |
|---|
| 1893 | if ( ! $post_type ) { |
|---|
| 1894 | return false; |
|---|
| 1895 | } |
|---|
| 1896 | } |
|---|
| 1897 | |
|---|
| 1898 | return $post_type->publicly_queryable || ( $post_type->_builtin && $post_type->public ); |
|---|
| 1899 | } |
|---|
| 1900 | |
|---|
| 1901 | /** |
|---|
| 1902 | * Retrieves an array of the latest posts, or posts matching the given criteria. |
|---|
| 1903 | * |
|---|
| 1904 | * The defaults are as follows: |
|---|
| 1905 | * |
|---|
| 1906 | * @since 1.2.0 |
|---|
| 1907 | * |
|---|
| 1908 | * @see WP_Query::parse_query() |
|---|
| 1909 | * |
|---|
| 1910 | * @param array $args { |
|---|
| 1911 | * Optional. Arguments to retrieve posts. See WP_Query::parse_query() for all |
|---|
| 1912 | * available arguments. |
|---|
| 1913 | * |
|---|
| 1914 | * @type int $numberposts Total number of posts to retrieve. Is an alias of $posts_per_page |
|---|
| 1915 | * in WP_Query. Accepts -1 for all. Default 5. |
|---|
| 1916 | * @type int|string $category Category ID or comma-separated list of IDs (this or any children). |
|---|
| 1917 | * Is an alias of $cat in WP_Query. Default 0. |
|---|
| 1918 | * @type array $include An array of post IDs to retrieve, sticky posts will be included. |
|---|
| 1919 | * Is an alias of $post__in in WP_Query. Default empty array. |
|---|
| 1920 | * @type array $exclude An array of post IDs not to retrieve. Default empty array. |
|---|
| 1921 | * @type bool $suppress_filters Whether to suppress filters. Default true. |
|---|
| 1922 | * } |
|---|
| 1923 | * @return WP_Post[]|int[] Array of post objects or post IDs. |
|---|
| 1924 | */ |
|---|
| 1925 | function get_posts( $args = null ) { |
|---|
| 1926 | $defaults = array( |
|---|
| 1927 | 'numberposts' => 5, |
|---|
| 1928 | 'category' => 0, |
|---|
| 1929 | 'orderby' => 'date', |
|---|
| 1930 | 'order' => 'DESC', |
|---|
| 1931 | 'include' => array(), |
|---|
| 1932 | 'exclude' => array(), |
|---|
| 1933 | 'meta_key' => '', |
|---|
| 1934 | 'meta_value' => '', |
|---|
| 1935 | 'post_type' => 'post', |
|---|
| 1936 | 'suppress_filters' => true, |
|---|
| 1937 | ); |
|---|
| 1938 | |
|---|
| 1939 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 1940 | if ( empty( $r['post_status'] ) ) { |
|---|
| 1941 | $r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish'; |
|---|
| 1942 | } |
|---|
| 1943 | if ( ! empty( $r['numberposts'] ) && empty( $r['posts_per_page'] ) ) { |
|---|
| 1944 | $r['posts_per_page'] = $r['numberposts']; |
|---|
| 1945 | } |
|---|
| 1946 | if ( ! empty( $r['category'] ) ) { |
|---|
| 1947 | $r['cat'] = $r['category']; |
|---|
| 1948 | } |
|---|
| 1949 | if ( ! empty( $r['include'] ) ) { |
|---|
| 1950 | $incposts = wp_parse_id_list( $r['include'] ); |
|---|
| 1951 | $r['posts_per_page'] = count( $incposts ); // only the number of posts included |
|---|
| 1952 | $r['post__in'] = $incposts; |
|---|
| 1953 | } elseif ( ! empty( $r['exclude'] ) ) { |
|---|
| 1954 | $r['post__not_in'] = wp_parse_id_list( $r['exclude'] ); |
|---|
| 1955 | } |
|---|
| 1956 | |
|---|
| 1957 | $r['ignore_sticky_posts'] = true; |
|---|
| 1958 | $r['no_found_rows'] = true; |
|---|
| 1959 | |
|---|
| 1960 | $get_posts = new WP_Query; |
|---|
| 1961 | return $get_posts->query( $r ); |
|---|
| 1962 | |
|---|
| 1963 | } |
|---|
| 1964 | |
|---|
| 1965 | // |
|---|
| 1966 | // Post meta functions |
|---|
| 1967 | // |
|---|
| 1968 | |
|---|
| 1969 | /** |
|---|
| 1970 | * Adds a meta field to the given post. |
|---|
| 1971 | * |
|---|
| 1972 | * Post meta data is called "Custom Fields" on the Administration Screen. |
|---|
| 1973 | * |
|---|
| 1974 | * @since 1.5.0 |
|---|
| 1975 | * |
|---|
| 1976 | * @param int $post_id Post ID. |
|---|
| 1977 | * @param string $meta_key Metadata name. |
|---|
| 1978 | * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|---|
| 1979 | * @param bool $unique Optional. Whether the same key should not be added. |
|---|
| 1980 | * Default false. |
|---|
| 1981 | * @return int|false Meta ID on success, false on failure. |
|---|
| 1982 | */ |
|---|
| 1983 | function add_post_meta( $post_id, $meta_key, $meta_value, $unique = false ) { |
|---|
| 1984 | // Make sure meta is added to the post, not a revision. |
|---|
| 1985 | $the_post = wp_is_post_revision( $post_id ); |
|---|
| 1986 | if ( $the_post ) { |
|---|
| 1987 | $post_id = $the_post; |
|---|
| 1988 | } |
|---|
| 1989 | |
|---|
| 1990 | return add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique ); |
|---|
| 1991 | } |
|---|
| 1992 | |
|---|
| 1993 | /** |
|---|
| 1994 | * Deletes a post meta field for the given post ID. |
|---|
| 1995 | * |
|---|
| 1996 | * You can match based on the key, or key and value. Removing based on key and |
|---|
| 1997 | * value, will keep from removing duplicate metadata with the same key. It also |
|---|
| 1998 | * allows removing all metadata matching the key, if needed. |
|---|
| 1999 | * |
|---|
| 2000 | * @since 1.5.0 |
|---|
| 2001 | * |
|---|
| 2002 | * @param int $post_id Post ID. |
|---|
| 2003 | * @param string $meta_key Metadata name. |
|---|
| 2004 | * @param mixed $meta_value Optional. Metadata value. Must be serializable if |
|---|
| 2005 | * non-scalar. Default empty. |
|---|
| 2006 | * @return bool True on success, false on failure. |
|---|
| 2007 | */ |
|---|
| 2008 | function delete_post_meta( $post_id, $meta_key, $meta_value = '' ) { |
|---|
| 2009 | // Make sure meta is added to the post, not a revision. |
|---|
| 2010 | $the_post = wp_is_post_revision( $post_id ); |
|---|
| 2011 | if ( $the_post ) { |
|---|
| 2012 | $post_id = $the_post; |
|---|
| 2013 | } |
|---|
| 2014 | |
|---|
| 2015 | return delete_metadata( 'post', $post_id, $meta_key, $meta_value ); |
|---|
| 2016 | } |
|---|
| 2017 | |
|---|
| 2018 | /** |
|---|
| 2019 | * Retrieves a post meta field for the given post ID. |
|---|
| 2020 | * |
|---|
| 2021 | * @since 1.5.0 |
|---|
| 2022 | * |
|---|
| 2023 | * @param int $post_id Post ID. |
|---|
| 2024 | * @param string $key Optional. The meta key to retrieve. By default, returns |
|---|
| 2025 | * data for all keys. Default empty. |
|---|
| 2026 | * @param bool $single Optional. If true, returns only the first value for the specified meta key. |
|---|
| 2027 | * This parameter has no effect if $key is not specified. Default false. |
|---|
| 2028 | * @return mixed Will be an array if $single is false. Will be value of the meta |
|---|
| 2029 | * field if $single is true. |
|---|
| 2030 | */ |
|---|
| 2031 | function get_post_meta( $post_id, $key = '', $single = false ) { |
|---|
| 2032 | return get_metadata( 'post', $post_id, $key, $single ); |
|---|
| 2033 | } |
|---|
| 2034 | |
|---|
| 2035 | /** |
|---|
| 2036 | * Updates a post meta field based on the given post ID. |
|---|
| 2037 | * |
|---|
| 2038 | * Use the `$prev_value` parameter to differentiate between meta fields with the |
|---|
| 2039 | * same key and post ID. |
|---|
| 2040 | * |
|---|
| 2041 | * If the meta field for the post does not exist, it will be added and its ID returned. |
|---|
| 2042 | * |
|---|
| 2043 | * Can be used in place of add_post_meta(). |
|---|
| 2044 | * |
|---|
| 2045 | * @since 1.5.0 |
|---|
| 2046 | * |
|---|
| 2047 | * @param int $post_id Post ID. |
|---|
| 2048 | * @param string $meta_key Metadata key. |
|---|
| 2049 | * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. |
|---|
| 2050 | * @param mixed $prev_value Optional. Previous value to check before updating. |
|---|
| 2051 | * @return int|bool The new meta field ID if a field with the given key didn't exist and was |
|---|
| 2052 | * therefore added, true on successful update, false on failure. |
|---|
| 2053 | */ |
|---|
| 2054 | function update_post_meta( $post_id, $meta_key, $meta_value, $prev_value = '' ) { |
|---|
| 2055 | // Make sure meta is added to the post, not a revision. |
|---|
| 2056 | $the_post = wp_is_post_revision( $post_id ); |
|---|
| 2057 | if ( $the_post ) { |
|---|
| 2058 | $post_id = $the_post; |
|---|
| 2059 | } |
|---|
| 2060 | |
|---|
| 2061 | return update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value ); |
|---|
| 2062 | } |
|---|
| 2063 | |
|---|
| 2064 | /** |
|---|
| 2065 | * Deletes everything from post meta matching the given meta key. |
|---|
| 2066 | * |
|---|
| 2067 | * @since 2.3.0 |
|---|
| 2068 | * |
|---|
| 2069 | * @param string $post_meta_key Key to search for when deleting. |
|---|
| 2070 | * @return bool Whether the post meta key was deleted from the database. |
|---|
| 2071 | */ |
|---|
| 2072 | function delete_post_meta_by_key( $post_meta_key ) { |
|---|
| 2073 | return delete_metadata( 'post', null, $post_meta_key, '', true ); |
|---|
| 2074 | } |
|---|
| 2075 | |
|---|
| 2076 | /** |
|---|
| 2077 | * Registers a meta key for posts. |
|---|
| 2078 | * |
|---|
| 2079 | * @since 4.9.8 |
|---|
| 2080 | * |
|---|
| 2081 | * @param string $post_type Post type to register a meta key for. Pass an empty string |
|---|
| 2082 | * to register the meta key across all existing post types. |
|---|
| 2083 | * @param string $meta_key The meta key to register. |
|---|
| 2084 | * @param array $args Data used to describe the meta key when registered. See |
|---|
| 2085 | * {@see register_meta()} for a list of supported arguments. |
|---|
| 2086 | * @return bool True if the meta key was successfully registered, false if not. |
|---|
| 2087 | */ |
|---|
| 2088 | function register_post_meta( $post_type, $meta_key, array $args ) { |
|---|
| 2089 | $args['object_subtype'] = $post_type; |
|---|
| 2090 | |
|---|
| 2091 | return register_meta( 'post', $meta_key, $args ); |
|---|
| 2092 | } |
|---|
| 2093 | |
|---|
| 2094 | /** |
|---|
| 2095 | * Unregisters a meta key for posts. |
|---|
| 2096 | * |
|---|
| 2097 | * @since 4.9.8 |
|---|
| 2098 | * |
|---|
| 2099 | * @param string $post_type Post type the meta key is currently registered for. Pass |
|---|
| 2100 | * an empty string if the meta key is registered across all |
|---|
| 2101 | * existing post types. |
|---|
| 2102 | * @param string $meta_key The meta key to unregister. |
|---|
| 2103 | * @return bool True on success, false if the meta key was not previously registered. |
|---|
| 2104 | */ |
|---|
| 2105 | function unregister_post_meta( $post_type, $meta_key ) { |
|---|
| 2106 | return unregister_meta_key( 'post', $meta_key, $post_type ); |
|---|
| 2107 | } |
|---|
| 2108 | |
|---|
| 2109 | /** |
|---|
| 2110 | * Retrieve post meta fields, based on post ID. |
|---|
| 2111 | * |
|---|
| 2112 | * The post meta fields are retrieved from the cache where possible, |
|---|
| 2113 | * so the function is optimized to be called more than once. |
|---|
| 2114 | * |
|---|
| 2115 | * @since 1.2.0 |
|---|
| 2116 | * |
|---|
| 2117 | * @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|---|
| 2118 | * @return array Post meta for the given post. |
|---|
| 2119 | */ |
|---|
| 2120 | function get_post_custom( $post_id = 0 ) { |
|---|
| 2121 | $post_id = absint( $post_id ); |
|---|
| 2122 | if ( ! $post_id ) { |
|---|
| 2123 | $post_id = get_the_ID(); |
|---|
| 2124 | } |
|---|
| 2125 | |
|---|
| 2126 | return get_post_meta( $post_id ); |
|---|
| 2127 | } |
|---|
| 2128 | |
|---|
| 2129 | /** |
|---|
| 2130 | * Retrieve meta field names for a post. |
|---|
| 2131 | * |
|---|
| 2132 | * If there are no meta fields, then nothing (null) will be returned. |
|---|
| 2133 | * |
|---|
| 2134 | * @since 1.2.0 |
|---|
| 2135 | * |
|---|
| 2136 | * @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|---|
| 2137 | * @return array|void Array of the keys, if retrieved. |
|---|
| 2138 | */ |
|---|
| 2139 | function get_post_custom_keys( $post_id = 0 ) { |
|---|
| 2140 | $custom = get_post_custom( $post_id ); |
|---|
| 2141 | |
|---|
| 2142 | if ( ! is_array( $custom ) ) { |
|---|
| 2143 | return; |
|---|
| 2144 | } |
|---|
| 2145 | |
|---|
| 2146 | if ( $keys = array_keys( $custom ) ) { |
|---|
| 2147 | return $keys; |
|---|
| 2148 | } |
|---|
| 2149 | } |
|---|
| 2150 | |
|---|
| 2151 | /** |
|---|
| 2152 | * Retrieve values for a custom post field. |
|---|
| 2153 | * |
|---|
| 2154 | * The parameters must not be considered optional. All of the post meta fields |
|---|
| 2155 | * will be retrieved and only the meta field key values returned. |
|---|
| 2156 | * |
|---|
| 2157 | * @since 1.2.0 |
|---|
| 2158 | * |
|---|
| 2159 | * @param string $key Optional. Meta field key. Default empty. |
|---|
| 2160 | * @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|---|
| 2161 | * @return array|null Meta field values. |
|---|
| 2162 | */ |
|---|
| 2163 | function get_post_custom_values( $key = '', $post_id = 0 ) { |
|---|
| 2164 | if ( ! $key ) { |
|---|
| 2165 | return null; |
|---|
| 2166 | } |
|---|
| 2167 | |
|---|
| 2168 | $custom = get_post_custom( $post_id ); |
|---|
| 2169 | |
|---|
| 2170 | return isset( $custom[ $key ] ) ? $custom[ $key ] : null; |
|---|
| 2171 | } |
|---|
| 2172 | |
|---|
| 2173 | /** |
|---|
| 2174 | * Determines whether a post is sticky. |
|---|
| 2175 | * |
|---|
| 2176 | * Sticky posts should remain at the top of The Loop. If the post ID is not |
|---|
| 2177 | * given, then The Loop ID for the current post will be used. |
|---|
| 2178 | * |
|---|
| 2179 | * For more information on this and similar theme functions, check out |
|---|
| 2180 | * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|---|
| 2181 | * Conditional Tags} article in the Theme Developer Handbook. |
|---|
| 2182 | * |
|---|
| 2183 | * @since 2.7.0 |
|---|
| 2184 | * |
|---|
| 2185 | * @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|---|
| 2186 | * @return bool Whether post is sticky. |
|---|
| 2187 | */ |
|---|
| 2188 | function is_sticky( $post_id = 0 ) { |
|---|
| 2189 | $post_id = absint( $post_id ); |
|---|
| 2190 | |
|---|
| 2191 | if ( ! $post_id ) { |
|---|
| 2192 | $post_id = get_the_ID(); |
|---|
| 2193 | } |
|---|
| 2194 | |
|---|
| 2195 | $stickies = get_option( 'sticky_posts' ); |
|---|
| 2196 | |
|---|
| 2197 | if ( ! is_array( $stickies ) ) { |
|---|
| 2198 | return false; |
|---|
| 2199 | } |
|---|
| 2200 | |
|---|
| 2201 | if ( in_array( $post_id, $stickies ) ) { |
|---|
| 2202 | return true; |
|---|
| 2203 | } |
|---|
| 2204 | |
|---|
| 2205 | return false; |
|---|
| 2206 | } |
|---|
| 2207 | |
|---|
| 2208 | /** |
|---|
| 2209 | * Sanitize every post field. |
|---|
| 2210 | * |
|---|
| 2211 | * If the context is 'raw', then the post object or array will get minimal |
|---|
| 2212 | * sanitization of the integer fields. |
|---|
| 2213 | * |
|---|
| 2214 | * @since 2.3.0 |
|---|
| 2215 | * |
|---|
| 2216 | * @see sanitize_post_field() |
|---|
| 2217 | * |
|---|
| 2218 | * @param object|WP_Post|array $post The Post Object or Array |
|---|
| 2219 | * @param string $context Optional. How to sanitize post fields. |
|---|
| 2220 | * Accepts 'raw', 'edit', 'db', or 'display'. |
|---|
| 2221 | * Default 'display'. |
|---|
| 2222 | * @return object|WP_Post|array The now sanitized Post Object or Array (will be the |
|---|
| 2223 | * same type as $post). |
|---|
| 2224 | */ |
|---|
| 2225 | function sanitize_post( $post, $context = 'display' ) { |
|---|
| 2226 | if ( is_object( $post ) ) { |
|---|
| 2227 | // Check if post already filtered for this context. |
|---|
| 2228 | if ( isset( $post->filter ) && $context == $post->filter ) { |
|---|
| 2229 | return $post; |
|---|
| 2230 | } |
|---|
| 2231 | if ( ! isset( $post->ID ) ) { |
|---|
| 2232 | $post->ID = 0; |
|---|
| 2233 | } |
|---|
| 2234 | foreach ( array_keys( get_object_vars( $post ) ) as $field ) { |
|---|
| 2235 | $post->$field = sanitize_post_field( $field, $post->$field, $post->ID, $context ); |
|---|
| 2236 | } |
|---|
| 2237 | $post->filter = $context; |
|---|
| 2238 | } elseif ( is_array( $post ) ) { |
|---|
| 2239 | // Check if post already filtered for this context. |
|---|
| 2240 | if ( isset( $post['filter'] ) && $context == $post['filter'] ) { |
|---|
| 2241 | return $post; |
|---|
| 2242 | } |
|---|
| 2243 | if ( ! isset( $post['ID'] ) ) { |
|---|
| 2244 | $post['ID'] = 0; |
|---|
| 2245 | } |
|---|
| 2246 | foreach ( array_keys( $post ) as $field ) { |
|---|
| 2247 | $post[ $field ] = sanitize_post_field( $field, $post[ $field ], $post['ID'], $context ); |
|---|
| 2248 | } |
|---|
| 2249 | $post['filter'] = $context; |
|---|
| 2250 | } |
|---|
| 2251 | return $post; |
|---|
| 2252 | } |
|---|
| 2253 | |
|---|
| 2254 | /** |
|---|
| 2255 | * Sanitize post field based on context. |
|---|
| 2256 | * |
|---|
| 2257 | * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and |
|---|
| 2258 | * 'js'. The 'display' context is used by default. 'attribute' and 'js' contexts |
|---|
| 2259 | * are treated like 'display' when calling filters. |
|---|
| 2260 | * |
|---|
| 2261 | * @since 2.3.0 |
|---|
| 2262 | * @since 4.4.0 Like `sanitize_post()`, `$context` defaults to 'display'. |
|---|
| 2263 | * |
|---|
| 2264 | * @param string $field The Post Object field name. |
|---|
| 2265 | * @param mixed $value The Post Object value. |
|---|
| 2266 | * @param int $post_id Post ID. |
|---|
| 2267 | * @param string $context Optional. How to sanitize post fields. Looks for 'raw', 'edit', |
|---|
| 2268 | * 'db', 'display', 'attribute' and 'js'. Default 'display'. |
|---|
| 2269 | * @return mixed Sanitized value. |
|---|
| 2270 | */ |
|---|
| 2271 | function sanitize_post_field( $field, $value, $post_id, $context = 'display' ) { |
|---|
| 2272 | $int_fields = array( 'ID', 'post_parent', 'menu_order' ); |
|---|
| 2273 | if ( in_array( $field, $int_fields ) ) { |
|---|
| 2274 | $value = (int) $value; |
|---|
| 2275 | } |
|---|
| 2276 | |
|---|
| 2277 | // Fields which contain arrays of integers. |
|---|
| 2278 | $array_int_fields = array( 'ancestors' ); |
|---|
| 2279 | if ( in_array( $field, $array_int_fields ) ) { |
|---|
| 2280 | $value = array_map( 'absint', $value ); |
|---|
| 2281 | return $value; |
|---|
| 2282 | } |
|---|
| 2283 | |
|---|
| 2284 | if ( 'raw' == $context ) { |
|---|
| 2285 | return $value; |
|---|
| 2286 | } |
|---|
| 2287 | |
|---|
| 2288 | $prefixed = false; |
|---|
| 2289 | if ( false !== strpos( $field, 'post_' ) ) { |
|---|
| 2290 | $prefixed = true; |
|---|
| 2291 | $field_no_prefix = str_replace( 'post_', '', $field ); |
|---|
| 2292 | } |
|---|
| 2293 | |
|---|
| 2294 | if ( 'edit' == $context ) { |
|---|
| 2295 | $format_to_edit = array( 'post_content', 'post_excerpt', 'post_title', 'post_password' ); |
|---|
| 2296 | |
|---|
| 2297 | if ( $prefixed ) { |
|---|
| 2298 | |
|---|
| 2299 | /** |
|---|
| 2300 | * Filters the value of a specific post field to edit. |
|---|
| 2301 | * |
|---|
| 2302 | * The dynamic portion of the hook name, `$field`, refers to the post |
|---|
| 2303 | * field name. |
|---|
| 2304 | * |
|---|
| 2305 | * @since 2.3.0 |
|---|
| 2306 | * |
|---|
| 2307 | * @param mixed $value Value of the post field. |
|---|
| 2308 | * @param int $post_id Post ID. |
|---|
| 2309 | */ |
|---|
| 2310 | $value = apply_filters( "edit_{$field}", $value, $post_id ); |
|---|
| 2311 | |
|---|
| 2312 | /** |
|---|
| 2313 | * Filters the value of a specific post field to edit. |
|---|
| 2314 | * |
|---|
| 2315 | * The dynamic portion of the hook name, `$field_no_prefix`, refers to |
|---|
| 2316 | * the post field name. |
|---|
| 2317 | * |
|---|
| 2318 | * @since 2.3.0 |
|---|
| 2319 | * |
|---|
| 2320 | * @param mixed $value Value of the post field. |
|---|
| 2321 | * @param int $post_id Post ID. |
|---|
| 2322 | */ |
|---|
| 2323 | $value = apply_filters( "{$field_no_prefix}_edit_pre", $value, $post_id ); |
|---|
| 2324 | } else { |
|---|
| 2325 | $value = apply_filters( "edit_post_{$field}", $value, $post_id ); |
|---|
| 2326 | } |
|---|
| 2327 | |
|---|
| 2328 | if ( in_array( $field, $format_to_edit ) ) { |
|---|
| 2329 | if ( 'post_content' == $field ) { |
|---|
| 2330 | $value = format_to_edit( $value, user_can_richedit() ); |
|---|
| 2331 | } else { |
|---|
| 2332 | $value = format_to_edit( $value ); |
|---|
| 2333 | } |
|---|
| 2334 | } else { |
|---|
| 2335 | $value = esc_attr( $value ); |
|---|
| 2336 | } |
|---|
| 2337 | } elseif ( 'db' == $context ) { |
|---|
| 2338 | if ( $prefixed ) { |
|---|
| 2339 | |
|---|
| 2340 | /** |
|---|
| 2341 | * Filters the value of a specific post field before saving. |
|---|
| 2342 | * |
|---|
| 2343 | * The dynamic portion of the hook name, `$field`, refers to the post |
|---|
| 2344 | * field name. |
|---|
| 2345 | * |
|---|
| 2346 | * @since 2.3.0 |
|---|
| 2347 | * |
|---|
| 2348 | * @param mixed $value Value of the post field. |
|---|
| 2349 | */ |
|---|
| 2350 | $value = apply_filters( "pre_{$field}", $value ); |
|---|
| 2351 | |
|---|
| 2352 | /** |
|---|
| 2353 | * Filters the value of a specific field before saving. |
|---|
| 2354 | * |
|---|
| 2355 | * The dynamic portion of the hook name, `$field_no_prefix`, refers |
|---|
| 2356 | * to the post field name. |
|---|
| 2357 | * |
|---|
| 2358 | * @since 2.3.0 |
|---|
| 2359 | * |
|---|
| 2360 | * @param mixed $value Value of the post field. |
|---|
| 2361 | */ |
|---|
| 2362 | $value = apply_filters( "{$field_no_prefix}_save_pre", $value ); |
|---|
| 2363 | } else { |
|---|
| 2364 | $value = apply_filters( "pre_post_{$field}", $value ); |
|---|
| 2365 | |
|---|
| 2366 | /** |
|---|
| 2367 | * Filters the value of a specific post field before saving. |
|---|
| 2368 | * |
|---|
| 2369 | * The dynamic portion of the hook name, `$field`, refers to the post |
|---|
| 2370 | * field name. |
|---|
| 2371 | * |
|---|
| 2372 | * @since 2.3.0 |
|---|
| 2373 | * |
|---|
| 2374 | * @param mixed $value Value of the post field. |
|---|
| 2375 | */ |
|---|
| 2376 | $value = apply_filters( "{$field}_pre", $value ); |
|---|
| 2377 | } |
|---|
| 2378 | } else { |
|---|
| 2379 | |
|---|
| 2380 | // Use display filters by default. |
|---|
| 2381 | if ( $prefixed ) { |
|---|
| 2382 | |
|---|
| 2383 | /** |
|---|
| 2384 | * Filters the value of a specific post field for display. |
|---|
| 2385 | * |
|---|
| 2386 | * The dynamic portion of the hook name, `$field`, refers to the post |
|---|
| 2387 | * field name. |
|---|
| 2388 | * |
|---|
| 2389 | * @since 2.3.0 |
|---|
| 2390 | * |
|---|
| 2391 | * @param mixed $value Value of the prefixed post field. |
|---|
| 2392 | * @param int $post_id Post ID. |
|---|
| 2393 | * @param string $context Context for how to sanitize the field. Possible |
|---|
| 2394 | * values include 'raw', 'edit', 'db', 'display', |
|---|
| 2395 | * 'attribute' and 'js'. |
|---|
| 2396 | */ |
|---|
| 2397 | $value = apply_filters( "{$field}", $value, $post_id, $context ); |
|---|
| 2398 | } else { |
|---|
| 2399 | $value = apply_filters( "post_{$field}", $value, $post_id, $context ); |
|---|
| 2400 | } |
|---|
| 2401 | |
|---|
| 2402 | if ( 'attribute' == $context ) { |
|---|
| 2403 | $value = esc_attr( $value ); |
|---|
| 2404 | } elseif ( 'js' == $context ) { |
|---|
| 2405 | $value = esc_js( $value ); |
|---|
| 2406 | } |
|---|
| 2407 | } |
|---|
| 2408 | |
|---|
| 2409 | return $value; |
|---|
| 2410 | } |
|---|
| 2411 | |
|---|
| 2412 | /** |
|---|
| 2413 | * Make a post sticky. |
|---|
| 2414 | * |
|---|
| 2415 | * Sticky posts should be displayed at the top of the front page. |
|---|
| 2416 | * |
|---|
| 2417 | * @since 2.7.0 |
|---|
| 2418 | * |
|---|
| 2419 | * @param int $post_id Post ID. |
|---|
| 2420 | */ |
|---|
| 2421 | function stick_post( $post_id ) { |
|---|
| 2422 | $stickies = get_option( 'sticky_posts' ); |
|---|
| 2423 | |
|---|
| 2424 | if ( ! is_array( $stickies ) ) { |
|---|
| 2425 | $stickies = array( $post_id ); |
|---|
| 2426 | } |
|---|
| 2427 | |
|---|
| 2428 | if ( ! in_array( $post_id, $stickies ) ) { |
|---|
| 2429 | $stickies[] = $post_id; |
|---|
| 2430 | } |
|---|
| 2431 | |
|---|
| 2432 | $updated = update_option( 'sticky_posts', $stickies ); |
|---|
| 2433 | |
|---|
| 2434 | if ( $updated ) { |
|---|
| 2435 | /** |
|---|
| 2436 | * Fires once a post has been added to the sticky list. |
|---|
| 2437 | * |
|---|
| 2438 | * @since 4.6.0 |
|---|
| 2439 | * |
|---|
| 2440 | * @param int $post_id ID of the post that was stuck. |
|---|
| 2441 | */ |
|---|
| 2442 | do_action( 'post_stuck', $post_id ); |
|---|
| 2443 | } |
|---|
| 2444 | } |
|---|
| 2445 | |
|---|
| 2446 | /** |
|---|
| 2447 | * Un-stick a post. |
|---|
| 2448 | * |
|---|
| 2449 | * Sticky posts should be displayed at the top of the front page. |
|---|
| 2450 | * |
|---|
| 2451 | * @since 2.7.0 |
|---|
| 2452 | * |
|---|
| 2453 | * @param int $post_id Post ID. |
|---|
| 2454 | */ |
|---|
| 2455 | function unstick_post( $post_id ) { |
|---|
| 2456 | $stickies = get_option( 'sticky_posts' ); |
|---|
| 2457 | |
|---|
| 2458 | if ( ! is_array( $stickies ) ) { |
|---|
| 2459 | return; |
|---|
| 2460 | } |
|---|
| 2461 | |
|---|
| 2462 | if ( ! in_array( $post_id, $stickies ) ) { |
|---|
| 2463 | return; |
|---|
| 2464 | } |
|---|
| 2465 | |
|---|
| 2466 | $offset = array_search( $post_id, $stickies ); |
|---|
| 2467 | if ( false === $offset ) { |
|---|
| 2468 | return; |
|---|
| 2469 | } |
|---|
| 2470 | |
|---|
| 2471 | array_splice( $stickies, $offset, 1 ); |
|---|
| 2472 | |
|---|
| 2473 | $updated = update_option( 'sticky_posts', $stickies ); |
|---|
| 2474 | |
|---|
| 2475 | if ( $updated ) { |
|---|
| 2476 | /** |
|---|
| 2477 | * Fires once a post has been removed from the sticky list. |
|---|
| 2478 | * |
|---|
| 2479 | * @since 4.6.0 |
|---|
| 2480 | * |
|---|
| 2481 | * @param int $post_id ID of the post that was unstuck. |
|---|
| 2482 | */ |
|---|
| 2483 | do_action( 'post_unstuck', $post_id ); |
|---|
| 2484 | } |
|---|
| 2485 | } |
|---|
| 2486 | |
|---|
| 2487 | /** |
|---|
| 2488 | * Return the cache key for wp_count_posts() based on the passed arguments. |
|---|
| 2489 | * |
|---|
| 2490 | * @since 3.9.0 |
|---|
| 2491 | * @access private |
|---|
| 2492 | * |
|---|
| 2493 | * @param string $type Optional. Post type to retrieve count Default 'post'. |
|---|
| 2494 | * @param string $perm Optional. 'readable' or empty. Default empty. |
|---|
| 2495 | * @return string The cache key. |
|---|
| 2496 | */ |
|---|
| 2497 | function _count_posts_cache_key( $type = 'post', $perm = '' ) { |
|---|
| 2498 | $cache_key = 'posts-' . $type; |
|---|
| 2499 | if ( 'readable' == $perm && is_user_logged_in() ) { |
|---|
| 2500 | $post_type_object = get_post_type_object( $type ); |
|---|
| 2501 | if ( $post_type_object && ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
|---|
| 2502 | $cache_key .= '_' . $perm . '_' . get_current_user_id(); |
|---|
| 2503 | } |
|---|
| 2504 | } |
|---|
| 2505 | return $cache_key; |
|---|
| 2506 | } |
|---|
| 2507 | |
|---|
| 2508 | /** |
|---|
| 2509 | * Count number of posts of a post type and if user has permissions to view. |
|---|
| 2510 | * |
|---|
| 2511 | * This function provides an efficient method of finding the amount of post's |
|---|
| 2512 | * type a blog has. Another method is to count the amount of items in |
|---|
| 2513 | * get_posts(), but that method has a lot of overhead with doing so. Therefore, |
|---|
| 2514 | * when developing for 2.5+, use this function instead. |
|---|
| 2515 | * |
|---|
| 2516 | * The $perm parameter checks for 'readable' value and if the user can read |
|---|
| 2517 | * private posts, it will display that for the user that is signed in. |
|---|
| 2518 | * |
|---|
| 2519 | * @since 2.5.0 |
|---|
| 2520 | * |
|---|
| 2521 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 2522 | * |
|---|
| 2523 | * @param string $type Optional. Post type to retrieve count. Default 'post'. |
|---|
| 2524 | * @param string $perm Optional. 'readable' or empty. Default empty. |
|---|
| 2525 | * @return object Number of posts for each status. |
|---|
| 2526 | */ |
|---|
| 2527 | function wp_count_posts( $type = 'post', $perm = '' ) { |
|---|
| 2528 | global $wpdb; |
|---|
| 2529 | |
|---|
| 2530 | if ( ! post_type_exists( $type ) ) { |
|---|
| 2531 | return new stdClass; |
|---|
| 2532 | } |
|---|
| 2533 | |
|---|
| 2534 | $cache_key = _count_posts_cache_key( $type, $perm ); |
|---|
| 2535 | |
|---|
| 2536 | $counts = wp_cache_get( $cache_key, 'counts' ); |
|---|
| 2537 | if ( false !== $counts ) { |
|---|
| 2538 | /** This filter is documented in wp-includes/post.php */ |
|---|
| 2539 | return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|---|
| 2540 | } |
|---|
| 2541 | |
|---|
| 2542 | $query = "SELECT post_status, COUNT( * ) AS num_posts FROM {$wpdb->posts} WHERE post_type = %s"; |
|---|
| 2543 | if ( 'readable' == $perm && is_user_logged_in() ) { |
|---|
| 2544 | $post_type_object = get_post_type_object( $type ); |
|---|
| 2545 | if ( ! current_user_can( $post_type_object->cap->read_private_posts ) ) { |
|---|
| 2546 | $query .= $wpdb->prepare( |
|---|
| 2547 | " AND (post_status != 'private' OR ( post_author = %d AND post_status = 'private' ))", |
|---|
| 2548 | get_current_user_id() |
|---|
| 2549 | ); |
|---|
| 2550 | } |
|---|
| 2551 | } |
|---|
| 2552 | $query .= ' GROUP BY post_status'; |
|---|
| 2553 | |
|---|
| 2554 | $results = (array) $wpdb->get_results( $wpdb->prepare( $query, $type ), ARRAY_A ); |
|---|
| 2555 | $counts = array_fill_keys( get_post_stati(), 0 ); |
|---|
| 2556 | |
|---|
| 2557 | foreach ( $results as $row ) { |
|---|
| 2558 | $counts[ $row['post_status'] ] = $row['num_posts']; |
|---|
| 2559 | } |
|---|
| 2560 | |
|---|
| 2561 | $counts = (object) $counts; |
|---|
| 2562 | wp_cache_set( $cache_key, $counts, 'counts' ); |
|---|
| 2563 | |
|---|
| 2564 | /** |
|---|
| 2565 | * Modify returned post counts by status for the current post type. |
|---|
| 2566 | * |
|---|
| 2567 | * @since 3.7.0 |
|---|
| 2568 | * |
|---|
| 2569 | * @param object $counts An object containing the current post_type's post |
|---|
| 2570 | * counts by status. |
|---|
| 2571 | * @param string $type Post type. |
|---|
| 2572 | * @param string $perm The permission to determine if the posts are 'readable' |
|---|
| 2573 | * by the current user. |
|---|
| 2574 | */ |
|---|
| 2575 | return apply_filters( 'wp_count_posts', $counts, $type, $perm ); |
|---|
| 2576 | } |
|---|
| 2577 | |
|---|
| 2578 | /** |
|---|
| 2579 | * Count number of attachments for the mime type(s). |
|---|
| 2580 | * |
|---|
| 2581 | * If you set the optional mime_type parameter, then an array will still be |
|---|
| 2582 | * returned, but will only have the item you are looking for. It does not give |
|---|
| 2583 | * you the number of attachments that are children of a post. You can get that |
|---|
| 2584 | * by counting the number of children that post has. |
|---|
| 2585 | * |
|---|
| 2586 | * @since 2.5.0 |
|---|
| 2587 | * |
|---|
| 2588 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 2589 | * |
|---|
| 2590 | * @param string|array $mime_type Optional. Array or comma-separated list of |
|---|
| 2591 | * MIME patterns. Default empty. |
|---|
| 2592 | * @return object An object containing the attachment counts by mime type. |
|---|
| 2593 | */ |
|---|
| 2594 | function wp_count_attachments( $mime_type = '' ) { |
|---|
| 2595 | global $wpdb; |
|---|
| 2596 | |
|---|
| 2597 | $and = wp_post_mime_type_where( $mime_type ); |
|---|
| 2598 | $count = $wpdb->get_results( "SELECT post_mime_type, COUNT( * ) AS num_posts FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' $and GROUP BY post_mime_type", ARRAY_A ); |
|---|
| 2599 | |
|---|
| 2600 | $counts = array(); |
|---|
| 2601 | foreach ( (array) $count as $row ) { |
|---|
| 2602 | $counts[ $row['post_mime_type'] ] = $row['num_posts']; |
|---|
| 2603 | } |
|---|
| 2604 | $counts['trash'] = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status = 'trash' $and" ); |
|---|
| 2605 | |
|---|
| 2606 | /** |
|---|
| 2607 | * Modify returned attachment counts by mime type. |
|---|
| 2608 | * |
|---|
| 2609 | * @since 3.7.0 |
|---|
| 2610 | * |
|---|
| 2611 | * @param object $counts An object containing the attachment counts by |
|---|
| 2612 | * mime type. |
|---|
| 2613 | * @param string $mime_type The mime type pattern used to filter the attachments |
|---|
| 2614 | * counted. |
|---|
| 2615 | */ |
|---|
| 2616 | return apply_filters( 'wp_count_attachments', (object) $counts, $mime_type ); |
|---|
| 2617 | } |
|---|
| 2618 | |
|---|
| 2619 | /** |
|---|
| 2620 | * Get default post mime types. |
|---|
| 2621 | * |
|---|
| 2622 | * @since 2.9.0 |
|---|
| 2623 | * |
|---|
| 2624 | * @return array List of post mime types. |
|---|
| 2625 | */ |
|---|
| 2626 | function get_post_mime_types() { |
|---|
| 2627 | $post_mime_types = array( // array( adj, noun ) |
|---|
| 2628 | 'image' => array( __( 'Images' ), __( 'Manage Images' ), _n_noop( 'Image <span class="count">(%s)</span>', 'Images <span class="count">(%s)</span>' ) ), |
|---|
| 2629 | 'audio' => array( __( 'Audio' ), __( 'Manage Audio' ), _n_noop( 'Audio <span class="count">(%s)</span>', 'Audio <span class="count">(%s)</span>' ) ), |
|---|
| 2630 | 'video' => array( __( 'Video' ), __( 'Manage Video' ), _n_noop( 'Video <span class="count">(%s)</span>', 'Video <span class="count">(%s)</span>' ) ), |
|---|
| 2631 | ); |
|---|
| 2632 | |
|---|
| 2633 | /** |
|---|
| 2634 | * Filters the default list of post mime types. |
|---|
| 2635 | * |
|---|
| 2636 | * @since 2.5.0 |
|---|
| 2637 | * |
|---|
| 2638 | * @param array $post_mime_types Default list of post mime types. |
|---|
| 2639 | */ |
|---|
| 2640 | return apply_filters( 'post_mime_types', $post_mime_types ); |
|---|
| 2641 | } |
|---|
| 2642 | |
|---|
| 2643 | /** |
|---|
| 2644 | * Check a MIME-Type against a list. |
|---|
| 2645 | * |
|---|
| 2646 | * If the wildcard_mime_types parameter is a string, it must be comma separated |
|---|
| 2647 | * list. If the real_mime_types is a string, it is also comma separated to |
|---|
| 2648 | * create the list. |
|---|
| 2649 | * |
|---|
| 2650 | * @since 2.5.0 |
|---|
| 2651 | * |
|---|
| 2652 | * @param string|array $wildcard_mime_types Mime types, e.g. audio/mpeg or image (same as image/*) |
|---|
| 2653 | * or flash (same as *flash*). |
|---|
| 2654 | * @param string|array $real_mime_types Real post mime type values. |
|---|
| 2655 | * @return array array(wildcard=>array(real types)). |
|---|
| 2656 | */ |
|---|
| 2657 | function wp_match_mime_types( $wildcard_mime_types, $real_mime_types ) { |
|---|
| 2658 | $matches = array(); |
|---|
| 2659 | if ( is_string( $wildcard_mime_types ) ) { |
|---|
| 2660 | $wildcard_mime_types = array_map( 'trim', explode( ',', $wildcard_mime_types ) ); |
|---|
| 2661 | } |
|---|
| 2662 | if ( is_string( $real_mime_types ) ) { |
|---|
| 2663 | $real_mime_types = array_map( 'trim', explode( ',', $real_mime_types ) ); |
|---|
| 2664 | } |
|---|
| 2665 | |
|---|
| 2666 | $patternses = array(); |
|---|
| 2667 | $wild = '[-._a-z0-9]*'; |
|---|
| 2668 | |
|---|
| 2669 | foreach ( (array) $wildcard_mime_types as $type ) { |
|---|
| 2670 | $mimes = array_map( 'trim', explode( ',', $type ) ); |
|---|
| 2671 | foreach ( $mimes as $mime ) { |
|---|
| 2672 | $regex = str_replace( '__wildcard__', $wild, preg_quote( str_replace( '*', '__wildcard__', $mime ) ) ); |
|---|
| 2673 | $patternses[][ $type ] = "^$regex$"; |
|---|
| 2674 | if ( false === strpos( $mime, '/' ) ) { |
|---|
| 2675 | $patternses[][ $type ] = "^$regex/"; |
|---|
| 2676 | $patternses[][ $type ] = $regex; |
|---|
| 2677 | } |
|---|
| 2678 | } |
|---|
| 2679 | } |
|---|
| 2680 | asort( $patternses ); |
|---|
| 2681 | |
|---|
| 2682 | foreach ( $patternses as $patterns ) { |
|---|
| 2683 | foreach ( $patterns as $type => $pattern ) { |
|---|
| 2684 | foreach ( (array) $real_mime_types as $real ) { |
|---|
| 2685 | if ( preg_match( "#$pattern#", $real ) && ( empty( $matches[ $type ] ) || false === array_search( $real, $matches[ $type ] ) ) ) { |
|---|
| 2686 | $matches[ $type ][] = $real; |
|---|
| 2687 | } |
|---|
| 2688 | } |
|---|
| 2689 | } |
|---|
| 2690 | } |
|---|
| 2691 | return $matches; |
|---|
| 2692 | } |
|---|
| 2693 | |
|---|
| 2694 | /** |
|---|
| 2695 | * Convert MIME types into SQL. |
|---|
| 2696 | * |
|---|
| 2697 | * @since 2.5.0 |
|---|
| 2698 | * |
|---|
| 2699 | * @param string|array $post_mime_types List of mime types or comma separated string |
|---|
| 2700 | * of mime types. |
|---|
| 2701 | * @param string $table_alias Optional. Specify a table alias, if needed. |
|---|
| 2702 | * Default empty. |
|---|
| 2703 | * @return string The SQL AND clause for mime searching. |
|---|
| 2704 | */ |
|---|
| 2705 | function wp_post_mime_type_where( $post_mime_types, $table_alias = '' ) { |
|---|
| 2706 | $where = ''; |
|---|
| 2707 | $wildcards = array( '', '%', '%/%' ); |
|---|
| 2708 | if ( is_string( $post_mime_types ) ) { |
|---|
| 2709 | $post_mime_types = array_map( 'trim', explode( ',', $post_mime_types ) ); |
|---|
| 2710 | } |
|---|
| 2711 | |
|---|
| 2712 | $wheres = array(); |
|---|
| 2713 | |
|---|
| 2714 | foreach ( (array) $post_mime_types as $mime_type ) { |
|---|
| 2715 | $mime_type = preg_replace( '/\s/', '', $mime_type ); |
|---|
| 2716 | $slashpos = strpos( $mime_type, '/' ); |
|---|
| 2717 | if ( false !== $slashpos ) { |
|---|
| 2718 | $mime_group = preg_replace( '/[^-*.a-zA-Z0-9]/', '', substr( $mime_type, 0, $slashpos ) ); |
|---|
| 2719 | $mime_subgroup = preg_replace( '/[^-*.+a-zA-Z0-9]/', '', substr( $mime_type, $slashpos + 1 ) ); |
|---|
| 2720 | if ( empty( $mime_subgroup ) ) { |
|---|
| 2721 | $mime_subgroup = '*'; |
|---|
| 2722 | } else { |
|---|
| 2723 | $mime_subgroup = str_replace( '/', '', $mime_subgroup ); |
|---|
| 2724 | } |
|---|
| 2725 | $mime_pattern = "$mime_group/$mime_subgroup"; |
|---|
| 2726 | } else { |
|---|
| 2727 | $mime_pattern = preg_replace( '/[^-*.a-zA-Z0-9]/', '', $mime_type ); |
|---|
| 2728 | if ( false === strpos( $mime_pattern, '*' ) ) { |
|---|
| 2729 | $mime_pattern .= '/*'; |
|---|
| 2730 | } |
|---|
| 2731 | } |
|---|
| 2732 | |
|---|
| 2733 | $mime_pattern = preg_replace( '/\*+/', '%', $mime_pattern ); |
|---|
| 2734 | |
|---|
| 2735 | if ( in_array( $mime_type, $wildcards ) ) { |
|---|
| 2736 | return ''; |
|---|
| 2737 | } |
|---|
| 2738 | |
|---|
| 2739 | if ( false !== strpos( $mime_pattern, '%' ) ) { |
|---|
| 2740 | $wheres[] = empty( $table_alias ) ? "post_mime_type LIKE '$mime_pattern'" : "$table_alias.post_mime_type LIKE '$mime_pattern'"; |
|---|
| 2741 | } else { |
|---|
| 2742 | $wheres[] = empty( $table_alias ) ? "post_mime_type = '$mime_pattern'" : "$table_alias.post_mime_type = '$mime_pattern'"; |
|---|
| 2743 | } |
|---|
| 2744 | } |
|---|
| 2745 | if ( ! empty( $wheres ) ) { |
|---|
| 2746 | $where = ' AND (' . join( ' OR ', $wheres ) . ') '; |
|---|
| 2747 | } |
|---|
| 2748 | return $where; |
|---|
| 2749 | } |
|---|
| 2750 | |
|---|
| 2751 | /** |
|---|
| 2752 | * Trash or delete a post or page. |
|---|
| 2753 | * |
|---|
| 2754 | * When the post and page is permanently deleted, everything that is tied to |
|---|
| 2755 | * it is deleted also. This includes comments, post meta fields, and terms |
|---|
| 2756 | * associated with the post. |
|---|
| 2757 | * |
|---|
| 2758 | * The post or page is moved to trash instead of permanently deleted unless |
|---|
| 2759 | * trash is disabled, item is already in the trash, or $force_delete is true. |
|---|
| 2760 | * |
|---|
| 2761 | * @since 1.0.0 |
|---|
| 2762 | * |
|---|
| 2763 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 2764 | * @see wp_delete_attachment() |
|---|
| 2765 | * @see wp_trash_post() |
|---|
| 2766 | * |
|---|
| 2767 | * @param int $postid Optional. Post ID. Default 0. |
|---|
| 2768 | * @param bool $force_delete Optional. Whether to bypass trash and force deletion. |
|---|
| 2769 | * Default false. |
|---|
| 2770 | * @return WP_Post|false|null Post data on success, false or null on failure. |
|---|
| 2771 | */ |
|---|
| 2772 | function wp_delete_post( $postid = 0, $force_delete = false ) { |
|---|
| 2773 | global $wpdb; |
|---|
| 2774 | |
|---|
| 2775 | $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $postid ) ); |
|---|
| 2776 | |
|---|
| 2777 | if ( ! $post ) { |
|---|
| 2778 | return $post; |
|---|
| 2779 | } |
|---|
| 2780 | |
|---|
| 2781 | $post = get_post( $post ); |
|---|
| 2782 | |
|---|
| 2783 | if ( ! $force_delete && ( 'post' === $post->post_type || 'page' === $post->post_type ) && 'trash' !== get_post_status( $postid ) && EMPTY_TRASH_DAYS ) { |
|---|
| 2784 | return wp_trash_post( $postid ); |
|---|
| 2785 | } |
|---|
| 2786 | |
|---|
| 2787 | if ( 'attachment' === $post->post_type ) { |
|---|
| 2788 | return wp_delete_attachment( $postid, $force_delete ); |
|---|
| 2789 | } |
|---|
| 2790 | |
|---|
| 2791 | /** |
|---|
| 2792 | * Filters whether a post deletion should take place. |
|---|
| 2793 | * |
|---|
| 2794 | * @since 4.4.0 |
|---|
| 2795 | * |
|---|
| 2796 | * @param bool $delete Whether to go forward with deletion. |
|---|
| 2797 | * @param WP_Post $post Post object. |
|---|
| 2798 | * @param bool $force_delete Whether to bypass the trash. |
|---|
| 2799 | */ |
|---|
| 2800 | $check = apply_filters( 'pre_delete_post', null, $post, $force_delete ); |
|---|
| 2801 | if ( null !== $check ) { |
|---|
| 2802 | return $check; |
|---|
| 2803 | } |
|---|
| 2804 | |
|---|
| 2805 | /** |
|---|
| 2806 | * Fires before a post is deleted, at the start of wp_delete_post(). |
|---|
| 2807 | * |
|---|
| 2808 | * @since 3.2.0 |
|---|
| 2809 | * |
|---|
| 2810 | * @see wp_delete_post() |
|---|
| 2811 | * |
|---|
| 2812 | * @param int $postid Post ID. |
|---|
| 2813 | */ |
|---|
| 2814 | do_action( 'before_delete_post', $postid ); |
|---|
| 2815 | |
|---|
| 2816 | delete_post_meta( $postid, '_wp_trash_meta_status' ); |
|---|
| 2817 | delete_post_meta( $postid, '_wp_trash_meta_time' ); |
|---|
| 2818 | |
|---|
| 2819 | wp_delete_object_term_relationships( $postid, get_object_taxonomies( $post->post_type ) ); |
|---|
| 2820 | |
|---|
| 2821 | $parent_data = array( 'post_parent' => $post->post_parent ); |
|---|
| 2822 | $parent_where = array( 'post_parent' => $postid ); |
|---|
| 2823 | |
|---|
| 2824 | if ( is_post_type_hierarchical( $post->post_type ) ) { |
|---|
| 2825 | // Point children of this page to its parent, also clean the cache of affected children. |
|---|
| 2826 | $children_query = $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type ); |
|---|
| 2827 | $children = $wpdb->get_results( $children_query ); |
|---|
| 2828 | if ( $children ) { |
|---|
| 2829 | $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => $post->post_type ) ); |
|---|
| 2830 | } |
|---|
| 2831 | } |
|---|
| 2832 | |
|---|
| 2833 | // Do raw query. wp_get_post_revisions() is filtered. |
|---|
| 2834 | $revision_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'revision'", $postid ) ); |
|---|
| 2835 | // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up. |
|---|
| 2836 | foreach ( $revision_ids as $revision_id ) { |
|---|
| 2837 | wp_delete_post_revision( $revision_id ); |
|---|
| 2838 | } |
|---|
| 2839 | |
|---|
| 2840 | // Point all attachments to this post up one level. |
|---|
| 2841 | $wpdb->update( $wpdb->posts, $parent_data, $parent_where + array( 'post_type' => 'attachment' ) ); |
|---|
| 2842 | |
|---|
| 2843 | wp_defer_comment_counting( true ); |
|---|
| 2844 | |
|---|
| 2845 | $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $postid ) ); |
|---|
| 2846 | foreach ( $comment_ids as $comment_id ) { |
|---|
| 2847 | wp_delete_comment( $comment_id, true ); |
|---|
| 2848 | } |
|---|
| 2849 | |
|---|
| 2850 | wp_defer_comment_counting( false ); |
|---|
| 2851 | |
|---|
| 2852 | $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $postid ) ); |
|---|
| 2853 | foreach ( $post_meta_ids as $mid ) { |
|---|
| 2854 | delete_metadata_by_mid( 'post', $mid ); |
|---|
| 2855 | } |
|---|
| 2856 | |
|---|
| 2857 | /** |
|---|
| 2858 | * Fires immediately before a post is deleted from the database. |
|---|
| 2859 | * |
|---|
| 2860 | * @since 1.2.0 |
|---|
| 2861 | * |
|---|
| 2862 | * @param int $postid Post ID. |
|---|
| 2863 | */ |
|---|
| 2864 | do_action( 'delete_post', $postid ); |
|---|
| 2865 | $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $postid ) ); |
|---|
| 2866 | if ( ! $result ) { |
|---|
| 2867 | return false; |
|---|
| 2868 | } |
|---|
| 2869 | |
|---|
| 2870 | /** |
|---|
| 2871 | * Fires immediately after a post is deleted from the database. |
|---|
| 2872 | * |
|---|
| 2873 | * @since 2.2.0 |
|---|
| 2874 | * |
|---|
| 2875 | * @param int $postid Post ID. |
|---|
| 2876 | */ |
|---|
| 2877 | do_action( 'deleted_post', $postid ); |
|---|
| 2878 | |
|---|
| 2879 | clean_post_cache( $post ); |
|---|
| 2880 | |
|---|
| 2881 | if ( is_post_type_hierarchical( $post->post_type ) && $children ) { |
|---|
| 2882 | foreach ( $children as $child ) { |
|---|
| 2883 | clean_post_cache( $child ); |
|---|
| 2884 | } |
|---|
| 2885 | } |
|---|
| 2886 | |
|---|
| 2887 | wp_clear_scheduled_hook( 'publish_future_post', array( $postid ) ); |
|---|
| 2888 | |
|---|
| 2889 | /** |
|---|
| 2890 | * Fires after a post is deleted, at the conclusion of wp_delete_post(). |
|---|
| 2891 | * |
|---|
| 2892 | * @since 3.2.0 |
|---|
| 2893 | * |
|---|
| 2894 | * @see wp_delete_post() |
|---|
| 2895 | * |
|---|
| 2896 | * @param int $postid Post ID. |
|---|
| 2897 | */ |
|---|
| 2898 | do_action( 'after_delete_post', $postid ); |
|---|
| 2899 | |
|---|
| 2900 | return $post; |
|---|
| 2901 | } |
|---|
| 2902 | |
|---|
| 2903 | /** |
|---|
| 2904 | * Reset the page_on_front, show_on_front, and page_for_post settings when |
|---|
| 2905 | * a linked page is deleted or trashed. |
|---|
| 2906 | * |
|---|
| 2907 | * Also ensures the post is no longer sticky. |
|---|
| 2908 | * |
|---|
| 2909 | * @since 3.7.0 |
|---|
| 2910 | * @access private |
|---|
| 2911 | * |
|---|
| 2912 | * @param int $post_id Post ID. |
|---|
| 2913 | */ |
|---|
| 2914 | function _reset_front_page_settings_for_post( $post_id ) { |
|---|
| 2915 | $post = get_post( $post_id ); |
|---|
| 2916 | if ( 'page' == $post->post_type ) { |
|---|
| 2917 | /* |
|---|
| 2918 | * If the page is defined in option page_on_front or post_for_posts, |
|---|
| 2919 | * adjust the corresponding options. |
|---|
| 2920 | */ |
|---|
| 2921 | if ( get_option( 'page_on_front' ) == $post->ID ) { |
|---|
| 2922 | update_option( 'show_on_front', 'posts' ); |
|---|
| 2923 | update_option( 'page_on_front', 0 ); |
|---|
| 2924 | } |
|---|
| 2925 | if ( get_option( 'page_for_posts' ) == $post->ID ) { |
|---|
| 2926 | delete_option( 'page_for_posts', 0 ); |
|---|
| 2927 | } |
|---|
| 2928 | } |
|---|
| 2929 | unstick_post( $post->ID ); |
|---|
| 2930 | } |
|---|
| 2931 | |
|---|
| 2932 | /** |
|---|
| 2933 | * Move a post or page to the Trash |
|---|
| 2934 | * |
|---|
| 2935 | * If trash is disabled, the post or page is permanently deleted. |
|---|
| 2936 | * |
|---|
| 2937 | * @since 2.9.0 |
|---|
| 2938 | * |
|---|
| 2939 | * @see wp_delete_post() |
|---|
| 2940 | * |
|---|
| 2941 | * @param int $post_id Optional. Post ID. Default is ID of the global $post |
|---|
| 2942 | * if EMPTY_TRASH_DAYS equals true. |
|---|
| 2943 | * @return WP_Post|false|null Post data on success, false or null on failure. |
|---|
| 2944 | */ |
|---|
| 2945 | function wp_trash_post( $post_id = 0 ) { |
|---|
| 2946 | if ( ! EMPTY_TRASH_DAYS ) { |
|---|
| 2947 | return wp_delete_post( $post_id, true ); |
|---|
| 2948 | } |
|---|
| 2949 | |
|---|
| 2950 | $post = get_post( $post_id ); |
|---|
| 2951 | |
|---|
| 2952 | if ( ! $post ) { |
|---|
| 2953 | return $post; |
|---|
| 2954 | } |
|---|
| 2955 | |
|---|
| 2956 | if ( 'trash' === $post->post_status ) { |
|---|
| 2957 | return false; |
|---|
| 2958 | } |
|---|
| 2959 | |
|---|
| 2960 | /** |
|---|
| 2961 | * Filters whether a post trashing should take place. |
|---|
| 2962 | * |
|---|
| 2963 | * @since 4.9.0 |
|---|
| 2964 | * |
|---|
| 2965 | * @param bool $trash Whether to go forward with trashing. |
|---|
| 2966 | * @param WP_Post $post Post object. |
|---|
| 2967 | */ |
|---|
| 2968 | $check = apply_filters( 'pre_trash_post', null, $post ); |
|---|
| 2969 | if ( null !== $check ) { |
|---|
| 2970 | return $check; |
|---|
| 2971 | } |
|---|
| 2972 | |
|---|
| 2973 | /** |
|---|
| 2974 | * Fires before a post is sent to the trash. |
|---|
| 2975 | * |
|---|
| 2976 | * @since 3.3.0 |
|---|
| 2977 | * |
|---|
| 2978 | * @param int $post_id Post ID. |
|---|
| 2979 | */ |
|---|
| 2980 | do_action( 'wp_trash_post', $post_id ); |
|---|
| 2981 | |
|---|
| 2982 | add_post_meta( $post_id, '_wp_trash_meta_status', $post->post_status ); |
|---|
| 2983 | add_post_meta( $post_id, '_wp_trash_meta_time', time() ); |
|---|
| 2984 | |
|---|
| 2985 | wp_update_post( |
|---|
| 2986 | array( |
|---|
| 2987 | 'ID' => $post_id, |
|---|
| 2988 | 'post_status' => 'trash', |
|---|
| 2989 | ) |
|---|
| 2990 | ); |
|---|
| 2991 | |
|---|
| 2992 | wp_trash_post_comments( $post_id ); |
|---|
| 2993 | |
|---|
| 2994 | /** |
|---|
| 2995 | * Fires after a post is sent to the trash. |
|---|
| 2996 | * |
|---|
| 2997 | * @since 2.9.0 |
|---|
| 2998 | * |
|---|
| 2999 | * @param int $post_id Post ID. |
|---|
| 3000 | */ |
|---|
| 3001 | do_action( 'trashed_post', $post_id ); |
|---|
| 3002 | |
|---|
| 3003 | return $post; |
|---|
| 3004 | } |
|---|
| 3005 | |
|---|
| 3006 | /** |
|---|
| 3007 | * Restore a post or page from the Trash. |
|---|
| 3008 | * |
|---|
| 3009 | * @since 2.9.0 |
|---|
| 3010 | * |
|---|
| 3011 | * @param int $post_id Optional. Post ID. Default is ID of the global $post. |
|---|
| 3012 | * @return WP_Post|false|null Post data on success, false or null on failure. |
|---|
| 3013 | */ |
|---|
| 3014 | function wp_untrash_post( $post_id = 0 ) { |
|---|
| 3015 | $post = get_post( $post_id ); |
|---|
| 3016 | |
|---|
| 3017 | if ( ! $post ) { |
|---|
| 3018 | return $post; |
|---|
| 3019 | } |
|---|
| 3020 | |
|---|
| 3021 | if ( 'trash' !== $post->post_status ) { |
|---|
| 3022 | return false; |
|---|
| 3023 | } |
|---|
| 3024 | |
|---|
| 3025 | /** |
|---|
| 3026 | * Filters whether a post untrashing should take place. |
|---|
| 3027 | * |
|---|
| 3028 | * @since 4.9.0 |
|---|
| 3029 | * |
|---|
| 3030 | * @param bool $untrash Whether to go forward with untrashing. |
|---|
| 3031 | * @param WP_Post $post Post object. |
|---|
| 3032 | */ |
|---|
| 3033 | $check = apply_filters( 'pre_untrash_post', null, $post ); |
|---|
| 3034 | if ( null !== $check ) { |
|---|
| 3035 | return $check; |
|---|
| 3036 | } |
|---|
| 3037 | |
|---|
| 3038 | /** |
|---|
| 3039 | * Fires before a post is restored from the trash. |
|---|
| 3040 | * |
|---|
| 3041 | * @since 2.9.0 |
|---|
| 3042 | * |
|---|
| 3043 | * @param int $post_id Post ID. |
|---|
| 3044 | */ |
|---|
| 3045 | do_action( 'untrash_post', $post_id ); |
|---|
| 3046 | |
|---|
| 3047 | $post_status = get_post_meta( $post_id, '_wp_trash_meta_status', true ); |
|---|
| 3048 | |
|---|
| 3049 | delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
|---|
| 3050 | delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|---|
| 3051 | |
|---|
| 3052 | wp_update_post( |
|---|
| 3053 | array( |
|---|
| 3054 | 'ID' => $post_id, |
|---|
| 3055 | 'post_status' => $post_status, |
|---|
| 3056 | ) |
|---|
| 3057 | ); |
|---|
| 3058 | |
|---|
| 3059 | wp_untrash_post_comments( $post_id ); |
|---|
| 3060 | |
|---|
| 3061 | /** |
|---|
| 3062 | * Fires after a post is restored from the trash. |
|---|
| 3063 | * |
|---|
| 3064 | * @since 2.9.0 |
|---|
| 3065 | * |
|---|
| 3066 | * @param int $post_id Post ID. |
|---|
| 3067 | */ |
|---|
| 3068 | do_action( 'untrashed_post', $post_id ); |
|---|
| 3069 | |
|---|
| 3070 | return $post; |
|---|
| 3071 | } |
|---|
| 3072 | |
|---|
| 3073 | /** |
|---|
| 3074 | * Moves comments for a post to the trash. |
|---|
| 3075 | * |
|---|
| 3076 | * @since 2.9.0 |
|---|
| 3077 | * |
|---|
| 3078 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 3079 | * |
|---|
| 3080 | * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
|---|
| 3081 | * @return mixed|void False on failure. |
|---|
| 3082 | */ |
|---|
| 3083 | function wp_trash_post_comments( $post = null ) { |
|---|
| 3084 | global $wpdb; |
|---|
| 3085 | |
|---|
| 3086 | $post = get_post( $post ); |
|---|
| 3087 | if ( empty( $post ) ) { |
|---|
| 3088 | return; |
|---|
| 3089 | } |
|---|
| 3090 | |
|---|
| 3091 | $post_id = $post->ID; |
|---|
| 3092 | |
|---|
| 3093 | /** |
|---|
| 3094 | * Fires before comments are sent to the trash. |
|---|
| 3095 | * |
|---|
| 3096 | * @since 2.9.0 |
|---|
| 3097 | * |
|---|
| 3098 | * @param int $post_id Post ID. |
|---|
| 3099 | */ |
|---|
| 3100 | do_action( 'trash_post_comments', $post_id ); |
|---|
| 3101 | |
|---|
| 3102 | $comments = $wpdb->get_results( $wpdb->prepare( "SELECT comment_ID, comment_approved FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); |
|---|
| 3103 | if ( empty( $comments ) ) { |
|---|
| 3104 | return; |
|---|
| 3105 | } |
|---|
| 3106 | |
|---|
| 3107 | // Cache current status for each comment. |
|---|
| 3108 | $statuses = array(); |
|---|
| 3109 | foreach ( $comments as $comment ) { |
|---|
| 3110 | $statuses[ $comment->comment_ID ] = $comment->comment_approved; |
|---|
| 3111 | } |
|---|
| 3112 | add_post_meta( $post_id, '_wp_trash_meta_comments_status', $statuses ); |
|---|
| 3113 | |
|---|
| 3114 | // Set status for all comments to post-trashed. |
|---|
| 3115 | $result = $wpdb->update( $wpdb->comments, array( 'comment_approved' => 'post-trashed' ), array( 'comment_post_ID' => $post_id ) ); |
|---|
| 3116 | |
|---|
| 3117 | clean_comment_cache( array_keys( $statuses ) ); |
|---|
| 3118 | |
|---|
| 3119 | /** |
|---|
| 3120 | * Fires after comments are sent to the trash. |
|---|
| 3121 | * |
|---|
| 3122 | * @since 2.9.0 |
|---|
| 3123 | * |
|---|
| 3124 | * @param int $post_id Post ID. |
|---|
| 3125 | * @param array $statuses Array of comment statuses. |
|---|
| 3126 | */ |
|---|
| 3127 | do_action( 'trashed_post_comments', $post_id, $statuses ); |
|---|
| 3128 | |
|---|
| 3129 | return $result; |
|---|
| 3130 | } |
|---|
| 3131 | |
|---|
| 3132 | /** |
|---|
| 3133 | * Restore comments for a post from the trash. |
|---|
| 3134 | * |
|---|
| 3135 | * @since 2.9.0 |
|---|
| 3136 | * |
|---|
| 3137 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 3138 | * |
|---|
| 3139 | * @param int|WP_Post|null $post Optional. Post ID or post object. Defaults to global $post. |
|---|
| 3140 | * @return true|void |
|---|
| 3141 | */ |
|---|
| 3142 | function wp_untrash_post_comments( $post = null ) { |
|---|
| 3143 | global $wpdb; |
|---|
| 3144 | |
|---|
| 3145 | $post = get_post( $post ); |
|---|
| 3146 | if ( empty( $post ) ) { |
|---|
| 3147 | return; |
|---|
| 3148 | } |
|---|
| 3149 | |
|---|
| 3150 | $post_id = $post->ID; |
|---|
| 3151 | |
|---|
| 3152 | $statuses = get_post_meta( $post_id, '_wp_trash_meta_comments_status', true ); |
|---|
| 3153 | |
|---|
| 3154 | if ( empty( $statuses ) ) { |
|---|
| 3155 | return true; |
|---|
| 3156 | } |
|---|
| 3157 | |
|---|
| 3158 | /** |
|---|
| 3159 | * Fires before comments are restored for a post from the trash. |
|---|
| 3160 | * |
|---|
| 3161 | * @since 2.9.0 |
|---|
| 3162 | * |
|---|
| 3163 | * @param int $post_id Post ID. |
|---|
| 3164 | */ |
|---|
| 3165 | do_action( 'untrash_post_comments', $post_id ); |
|---|
| 3166 | |
|---|
| 3167 | // Restore each comment to its original status. |
|---|
| 3168 | $group_by_status = array(); |
|---|
| 3169 | foreach ( $statuses as $comment_id => $comment_status ) { |
|---|
| 3170 | $group_by_status[ $comment_status ][] = $comment_id; |
|---|
| 3171 | } |
|---|
| 3172 | |
|---|
| 3173 | foreach ( $group_by_status as $status => $comments ) { |
|---|
| 3174 | // Sanity check. This shouldn't happen. |
|---|
| 3175 | if ( 'post-trashed' == $status ) { |
|---|
| 3176 | $status = '0'; |
|---|
| 3177 | } |
|---|
| 3178 | $comments_in = implode( ', ', array_map( 'intval', $comments ) ); |
|---|
| 3179 | $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->comments SET comment_approved = %s WHERE comment_ID IN ($comments_in)", $status ) ); |
|---|
| 3180 | } |
|---|
| 3181 | |
|---|
| 3182 | clean_comment_cache( array_keys( $statuses ) ); |
|---|
| 3183 | |
|---|
| 3184 | delete_post_meta( $post_id, '_wp_trash_meta_comments_status' ); |
|---|
| 3185 | |
|---|
| 3186 | /** |
|---|
| 3187 | * Fires after comments are restored for a post from the trash. |
|---|
| 3188 | * |
|---|
| 3189 | * @since 2.9.0 |
|---|
| 3190 | * |
|---|
| 3191 | * @param int $post_id Post ID. |
|---|
| 3192 | */ |
|---|
| 3193 | do_action( 'untrashed_post_comments', $post_id ); |
|---|
| 3194 | } |
|---|
| 3195 | |
|---|
| 3196 | /** |
|---|
| 3197 | * Retrieve the list of categories for a post. |
|---|
| 3198 | * |
|---|
| 3199 | * Compatibility layer for themes and plugins. Also an easy layer of abstraction |
|---|
| 3200 | * away from the complexity of the taxonomy layer. |
|---|
| 3201 | * |
|---|
| 3202 | * @since 2.1.0 |
|---|
| 3203 | * |
|---|
| 3204 | * @see wp_get_object_terms() |
|---|
| 3205 | * |
|---|
| 3206 | * @param int $post_id Optional. The Post ID. Does not default to the ID of the |
|---|
| 3207 | * global $post. Default 0. |
|---|
| 3208 | * @param array $args Optional. Category query parameters. Default empty array. |
|---|
| 3209 | * See WP_Term_Query::__construct() for supported arguments. |
|---|
| 3210 | * @return array|WP_Error List of categories. If the `$fields` argument passed via `$args` is 'all' or |
|---|
| 3211 | * 'all_with_object_id', an array of WP_Term objects will be returned. If `$fields` |
|---|
| 3212 | * is 'ids', an array of category ids. If `$fields` is 'names', an array of category names. |
|---|
| 3213 | * WP_Error object if 'category' taxonomy doesn't exist. |
|---|
| 3214 | */ |
|---|
| 3215 | function wp_get_post_categories( $post_id = 0, $args = array() ) { |
|---|
| 3216 | $post_id = (int) $post_id; |
|---|
| 3217 | |
|---|
| 3218 | $defaults = array( 'fields' => 'ids' ); |
|---|
| 3219 | $args = wp_parse_args( $args, $defaults ); |
|---|
| 3220 | |
|---|
| 3221 | $cats = wp_get_object_terms( $post_id, 'category', $args ); |
|---|
| 3222 | return $cats; |
|---|
| 3223 | } |
|---|
| 3224 | |
|---|
| 3225 | /** |
|---|
| 3226 | * Retrieve the tags for a post. |
|---|
| 3227 | * |
|---|
| 3228 | * There is only one default for this function, called 'fields' and by default |
|---|
| 3229 | * is set to 'all'. There are other defaults that can be overridden in |
|---|
| 3230 | * wp_get_object_terms(). |
|---|
| 3231 | * |
|---|
| 3232 | * @since 2.3.0 |
|---|
| 3233 | * |
|---|
| 3234 | * @param int $post_id Optional. The Post ID. Does not default to the ID of the |
|---|
| 3235 | * global $post. Default 0. |
|---|
| 3236 | * @param array $args Optional. Tag query parameters. Default empty array. |
|---|
| 3237 | * See WP_Term_Query::__construct() for supported arguments. |
|---|
| 3238 | * @return array|WP_Error Array of WP_Term objects on success or empty array if no tags were found. |
|---|
| 3239 | * WP_Error object if 'post_tag' taxonomy doesn't exist. |
|---|
| 3240 | */ |
|---|
| 3241 | function wp_get_post_tags( $post_id = 0, $args = array() ) { |
|---|
| 3242 | return wp_get_post_terms( $post_id, 'post_tag', $args ); |
|---|
| 3243 | } |
|---|
| 3244 | |
|---|
| 3245 | /** |
|---|
| 3246 | * Retrieves the terms for a post. |
|---|
| 3247 | * |
|---|
| 3248 | * @since 2.8.0 |
|---|
| 3249 | * |
|---|
| 3250 | * @param int $post_id Optional. The Post ID. Does not default to the ID of the |
|---|
| 3251 | * global $post. Default 0. |
|---|
| 3252 | * @param string|array $taxonomy Optional. The taxonomy slug or array of slugs for which |
|---|
| 3253 | * to retrieve terms. Default 'post_tag'. |
|---|
| 3254 | * @param array $args { |
|---|
| 3255 | * Optional. Term query parameters. See WP_Term_Query::__construct() for supported arguments. |
|---|
| 3256 | * |
|---|
| 3257 | * @type string $fields Term fields to retrieve. Default 'all'. |
|---|
| 3258 | * } |
|---|
| 3259 | * @return array|WP_Error Array of WP_Term objects on success or empty array if no terms were found. |
|---|
| 3260 | * WP_Error object if `$taxonomy` doesn't exist. |
|---|
| 3261 | */ |
|---|
| 3262 | function wp_get_post_terms( $post_id = 0, $taxonomy = 'post_tag', $args = array() ) { |
|---|
| 3263 | $post_id = (int) $post_id; |
|---|
| 3264 | |
|---|
| 3265 | $defaults = array( 'fields' => 'all' ); |
|---|
| 3266 | $args = wp_parse_args( $args, $defaults ); |
|---|
| 3267 | |
|---|
| 3268 | $tags = wp_get_object_terms( $post_id, $taxonomy, $args ); |
|---|
| 3269 | |
|---|
| 3270 | return $tags; |
|---|
| 3271 | } |
|---|
| 3272 | |
|---|
| 3273 | /** |
|---|
| 3274 | * Retrieve a number of recent posts. |
|---|
| 3275 | * |
|---|
| 3276 | * @since 1.0.0 |
|---|
| 3277 | * |
|---|
| 3278 | * @see get_posts() |
|---|
| 3279 | * |
|---|
| 3280 | * @param array $args Optional. Arguments to retrieve posts. Default empty array. |
|---|
| 3281 | * @param string $output Optional. The required return type. One of OBJECT or ARRAY_A, which correspond to |
|---|
| 3282 | * a WP_Post object or an associative array, respectively. Default ARRAY_A. |
|---|
| 3283 | * @return array|false Array of recent posts, where the type of each element is determined by $output parameter. |
|---|
| 3284 | * Empty array on failure. |
|---|
| 3285 | */ |
|---|
| 3286 | function wp_get_recent_posts( $args = array(), $output = ARRAY_A ) { |
|---|
| 3287 | |
|---|
| 3288 | if ( is_numeric( $args ) ) { |
|---|
| 3289 | _deprecated_argument( __FUNCTION__, '3.1.0', __( 'Passing an integer number of posts is deprecated. Pass an array of arguments instead.' ) ); |
|---|
| 3290 | $args = array( 'numberposts' => absint( $args ) ); |
|---|
| 3291 | } |
|---|
| 3292 | |
|---|
| 3293 | // Set default arguments. |
|---|
| 3294 | $defaults = array( |
|---|
| 3295 | 'numberposts' => 10, |
|---|
| 3296 | 'offset' => 0, |
|---|
| 3297 | 'category' => 0, |
|---|
| 3298 | 'orderby' => 'post_date', |
|---|
| 3299 | 'order' => 'DESC', |
|---|
| 3300 | 'include' => '', |
|---|
| 3301 | 'exclude' => '', |
|---|
| 3302 | 'meta_key' => '', |
|---|
| 3303 | 'meta_value' => '', |
|---|
| 3304 | 'post_type' => 'post', |
|---|
| 3305 | 'post_status' => 'draft, publish, future, pending, private', |
|---|
| 3306 | 'suppress_filters' => true, |
|---|
| 3307 | ); |
|---|
| 3308 | |
|---|
| 3309 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 3310 | |
|---|
| 3311 | $results = get_posts( $r ); |
|---|
| 3312 | |
|---|
| 3313 | // Backward compatibility. Prior to 3.1 expected posts to be returned in array. |
|---|
| 3314 | if ( ARRAY_A == $output ) { |
|---|
| 3315 | foreach ( $results as $key => $result ) { |
|---|
| 3316 | $results[ $key ] = get_object_vars( $result ); |
|---|
| 3317 | } |
|---|
| 3318 | return $results ? $results : array(); |
|---|
| 3319 | } |
|---|
| 3320 | |
|---|
| 3321 | return $results ? $results : false; |
|---|
| 3322 | |
|---|
| 3323 | } |
|---|
| 3324 | |
|---|
| 3325 | /** |
|---|
| 3326 | * Insert or update a post. |
|---|
| 3327 | * |
|---|
| 3328 | * If the $postarr parameter has 'ID' set to a value, then post will be updated. |
|---|
| 3329 | * |
|---|
| 3330 | * You can set the post date manually, by setting the values for 'post_date' |
|---|
| 3331 | * and 'post_date_gmt' keys. You can close the comments or open the comments by |
|---|
| 3332 | * setting the value for 'comment_status' key. |
|---|
| 3333 | * |
|---|
| 3334 | * @since 1.0.0 |
|---|
| 3335 | * @since 4.2.0 Support was added for encoding emoji in the post title, content, and excerpt. |
|---|
| 3336 | * @since 4.4.0 A 'meta_input' array can now be passed to `$postarr` to add post meta data. |
|---|
| 3337 | * |
|---|
| 3338 | * @see sanitize_post() |
|---|
| 3339 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 3340 | * |
|---|
| 3341 | * @param array $postarr { |
|---|
| 3342 | * An array of elements that make up a post to update or insert. |
|---|
| 3343 | * |
|---|
| 3344 | * @type int $ID The post ID. If equal to something other than 0, |
|---|
| 3345 | * the post with that ID will be updated. Default 0. |
|---|
| 3346 | * @type int $post_author The ID of the user who added the post. Default is |
|---|
| 3347 | * the current user ID. |
|---|
| 3348 | * @type string $post_date The date of the post. Default is the current time. |
|---|
| 3349 | * @type string $post_date_gmt The date of the post in the GMT timezone. Default is |
|---|
| 3350 | * the value of `$post_date`. |
|---|
| 3351 | * @type mixed $post_content The post content. Default empty. |
|---|
| 3352 | * @type string $post_content_filtered The filtered post content. Default empty. |
|---|
| 3353 | * @type string $post_title The post title. Default empty. |
|---|
| 3354 | * @type string $post_excerpt The post excerpt. Default empty. |
|---|
| 3355 | * @type string $post_status The post status. Default 'draft'. |
|---|
| 3356 | * @type string $post_type The post type. Default 'post'. |
|---|
| 3357 | * @type string $comment_status Whether the post can accept comments. Accepts 'open' or 'closed'. |
|---|
| 3358 | * Default is the value of 'default_comment_status' option. |
|---|
| 3359 | * @type string $ping_status Whether the post can accept pings. Accepts 'open' or 'closed'. |
|---|
| 3360 | * Default is the value of 'default_ping_status' option. |
|---|
| 3361 | * @type string $post_password The password to access the post. Default empty. |
|---|
| 3362 | * @type string $post_name The post name. Default is the sanitized post title |
|---|
| 3363 | * when creating a new post. |
|---|
| 3364 | * @type string $to_ping Space or carriage return-separated list of URLs to ping. |
|---|
| 3365 | * Default empty. |
|---|
| 3366 | * @type string $pinged Space or carriage return-separated list of URLs that have |
|---|
| 3367 | * been pinged. Default empty. |
|---|
| 3368 | * @type string $post_modified The date when the post was last modified. Default is |
|---|
| 3369 | * the current time. |
|---|
| 3370 | * @type string $post_modified_gmt The date when the post was last modified in the GMT |
|---|
| 3371 | * timezone. Default is the current time. |
|---|
| 3372 | * @type int $post_parent Set this for the post it belongs to, if any. Default 0. |
|---|
| 3373 | * @type int $menu_order The order the post should be displayed in. Default 0. |
|---|
| 3374 | * @type string $post_mime_type The mime type of the post. Default empty. |
|---|
| 3375 | * @type string $guid Global Unique ID for referencing the post. Default empty. |
|---|
| 3376 | * @type array $post_category Array of category IDs. |
|---|
| 3377 | * Defaults to value of the 'default_category' option. |
|---|
| 3378 | * @type array $tags_input Array of tag names, slugs, or IDs. Default empty. |
|---|
| 3379 | * @type array $tax_input Array of taxonomy terms keyed by their taxonomy name. Default empty. |
|---|
| 3380 | * @type array $meta_input Array of post meta values keyed by their post meta key. Default empty. |
|---|
| 3381 | * } |
|---|
| 3382 | * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
|---|
| 3383 | * @return int|WP_Error The post ID on success. The value 0 or WP_Error on failure. |
|---|
| 3384 | */ |
|---|
| 3385 | function wp_insert_post( $postarr, $wp_error = false ) { |
|---|
| 3386 | global $wpdb; |
|---|
| 3387 | |
|---|
| 3388 | $user_id = get_current_user_id(); |
|---|
| 3389 | |
|---|
| 3390 | $defaults = array( |
|---|
| 3391 | 'post_author' => $user_id, |
|---|
| 3392 | 'post_content' => '', |
|---|
| 3393 | 'post_content_filtered' => '', |
|---|
| 3394 | 'post_title' => '', |
|---|
| 3395 | 'post_excerpt' => '', |
|---|
| 3396 | 'post_status' => 'draft', |
|---|
| 3397 | 'post_type' => 'post', |
|---|
| 3398 | 'comment_status' => '', |
|---|
| 3399 | 'ping_status' => '', |
|---|
| 3400 | 'post_password' => '', |
|---|
| 3401 | 'to_ping' => '', |
|---|
| 3402 | 'pinged' => '', |
|---|
| 3403 | 'post_parent' => 0, |
|---|
| 3404 | 'menu_order' => 0, |
|---|
| 3405 | 'guid' => '', |
|---|
| 3406 | 'import_id' => 0, |
|---|
| 3407 | 'context' => '', |
|---|
| 3408 | ); |
|---|
| 3409 | |
|---|
| 3410 | $postarr = wp_parse_args( $postarr, $defaults ); |
|---|
| 3411 | |
|---|
| 3412 | unset( $postarr['filter'] ); |
|---|
| 3413 | |
|---|
| 3414 | $postarr = sanitize_post( $postarr, 'db' ); |
|---|
| 3415 | |
|---|
| 3416 | // Are we updating or creating? |
|---|
| 3417 | $post_ID = 0; |
|---|
| 3418 | $update = false; |
|---|
| 3419 | $guid = $postarr['guid']; |
|---|
| 3420 | |
|---|
| 3421 | if ( ! empty( $postarr['ID'] ) ) { |
|---|
| 3422 | $update = true; |
|---|
| 3423 | |
|---|
| 3424 | // Get the post ID and GUID. |
|---|
| 3425 | $post_ID = $postarr['ID']; |
|---|
| 3426 | $post_before = get_post( $post_ID ); |
|---|
| 3427 | if ( is_null( $post_before ) ) { |
|---|
| 3428 | if ( $wp_error ) { |
|---|
| 3429 | return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
|---|
| 3430 | } |
|---|
| 3431 | return 0; |
|---|
| 3432 | } |
|---|
| 3433 | |
|---|
| 3434 | $guid = get_post_field( 'guid', $post_ID ); |
|---|
| 3435 | $previous_status = get_post_field( 'post_status', $post_ID ); |
|---|
| 3436 | } else { |
|---|
| 3437 | $previous_status = 'new'; |
|---|
| 3438 | } |
|---|
| 3439 | |
|---|
| 3440 | $post_type = empty( $postarr['post_type'] ) ? 'post' : $postarr['post_type']; |
|---|
| 3441 | |
|---|
| 3442 | $post_title = $postarr['post_title']; |
|---|
| 3443 | $post_content = $postarr['post_content']; |
|---|
| 3444 | $post_excerpt = $postarr['post_excerpt']; |
|---|
| 3445 | if ( isset( $postarr['post_name'] ) ) { |
|---|
| 3446 | $post_name = $postarr['post_name']; |
|---|
| 3447 | } elseif ( $update ) { |
|---|
| 3448 | // For an update, don't modify the post_name if it wasn't supplied as an argument. |
|---|
| 3449 | $post_name = $post_before->post_name; |
|---|
| 3450 | } |
|---|
| 3451 | |
|---|
| 3452 | $maybe_empty = 'attachment' !== $post_type |
|---|
| 3453 | && ! $post_content && ! $post_title && ! $post_excerpt |
|---|
| 3454 | && post_type_supports( $post_type, 'editor' ) |
|---|
| 3455 | && post_type_supports( $post_type, 'title' ) |
|---|
| 3456 | && post_type_supports( $post_type, 'excerpt' ); |
|---|
| 3457 | |
|---|
| 3458 | /** |
|---|
| 3459 | * Filters whether the post should be considered "empty". |
|---|
| 3460 | * |
|---|
| 3461 | * The post is considered "empty" if both: |
|---|
| 3462 | * 1. The post type supports the title, editor, and excerpt fields |
|---|
| 3463 | * 2. The title, editor, and excerpt fields are all empty |
|---|
| 3464 | * |
|---|
| 3465 | * Returning a truthy value to the filter will effectively short-circuit |
|---|
| 3466 | * the new post being inserted, returning 0. If $wp_error is true, a WP_Error |
|---|
| 3467 | * will be returned instead. |
|---|
| 3468 | * |
|---|
| 3469 | * @since 3.3.0 |
|---|
| 3470 | * |
|---|
| 3471 | * @param bool $maybe_empty Whether the post should be considered "empty". |
|---|
| 3472 | * @param array $postarr Array of post data. |
|---|
| 3473 | */ |
|---|
| 3474 | if ( apply_filters( 'wp_insert_post_empty_content', $maybe_empty, $postarr ) ) { |
|---|
| 3475 | if ( $wp_error ) { |
|---|
| 3476 | return new WP_Error( 'empty_content', __( 'Content, title, and excerpt are empty.' ) ); |
|---|
| 3477 | } else { |
|---|
| 3478 | return 0; |
|---|
| 3479 | } |
|---|
| 3480 | } |
|---|
| 3481 | |
|---|
| 3482 | $post_status = empty( $postarr['post_status'] ) ? 'draft' : $postarr['post_status']; |
|---|
| 3483 | if ( 'attachment' === $post_type && ! in_array( $post_status, array( 'inherit', 'private', 'trash', 'auto-draft' ), true ) ) { |
|---|
| 3484 | $post_status = 'inherit'; |
|---|
| 3485 | } |
|---|
| 3486 | |
|---|
| 3487 | if ( ! empty( $postarr['post_category'] ) ) { |
|---|
| 3488 | // Filter out empty terms. |
|---|
| 3489 | $post_category = array_filter( $postarr['post_category'] ); |
|---|
| 3490 | } |
|---|
| 3491 | |
|---|
| 3492 | // Make sure we set a valid category. |
|---|
| 3493 | if ( empty( $post_category ) || 0 == count( $post_category ) || ! is_array( $post_category ) ) { |
|---|
| 3494 | // 'post' requires at least one category. |
|---|
| 3495 | if ( 'post' == $post_type && 'auto-draft' != $post_status ) { |
|---|
| 3496 | $post_category = array( get_option( 'default_category' ) ); |
|---|
| 3497 | } else { |
|---|
| 3498 | $post_category = array(); |
|---|
| 3499 | } |
|---|
| 3500 | } |
|---|
| 3501 | |
|---|
| 3502 | /* |
|---|
| 3503 | * Don't allow contributors to set the post slug for pending review posts. |
|---|
| 3504 | * |
|---|
| 3505 | * For new posts check the primitive capability, for updates check the meta capability. |
|---|
| 3506 | */ |
|---|
| 3507 | $post_type_object = get_post_type_object( $post_type ); |
|---|
| 3508 | |
|---|
| 3509 | if ( ! $update && 'pending' === $post_status && ! current_user_can( $post_type_object->cap->publish_posts ) ) { |
|---|
| 3510 | $post_name = ''; |
|---|
| 3511 | } elseif ( $update && 'pending' === $post_status && ! current_user_can( 'publish_post', $post_ID ) ) { |
|---|
| 3512 | $post_name = ''; |
|---|
| 3513 | } |
|---|
| 3514 | |
|---|
| 3515 | /* |
|---|
| 3516 | * Create a valid post name. Drafts and pending posts are allowed to have |
|---|
| 3517 | * an empty post name. |
|---|
| 3518 | */ |
|---|
| 3519 | if ( empty( $post_name ) ) { |
|---|
| 3520 | if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { |
|---|
| 3521 | $post_name = sanitize_title( $post_title ); |
|---|
| 3522 | } else { |
|---|
| 3523 | $post_name = ''; |
|---|
| 3524 | } |
|---|
| 3525 | } else { |
|---|
| 3526 | // On updates, we need to check to see if it's using the old, fixed sanitization context. |
|---|
| 3527 | $check_name = sanitize_title( $post_name, '', 'old-save' ); |
|---|
| 3528 | if ( $update && strtolower( urlencode( $post_name ) ) == $check_name && get_post_field( 'post_name', $post_ID ) == $check_name ) { |
|---|
| 3529 | $post_name = $check_name; |
|---|
| 3530 | } else { // new post, or slug has changed. |
|---|
| 3531 | $post_name = sanitize_title( $post_name ); |
|---|
| 3532 | } |
|---|
| 3533 | } |
|---|
| 3534 | |
|---|
| 3535 | /* |
|---|
| 3536 | * If the post date is empty (due to having been new or a draft) and status |
|---|
| 3537 | * is not 'draft' or 'pending', set date to now. |
|---|
| 3538 | */ |
|---|
| 3539 | if ( empty( $postarr['post_date'] ) || '0000-00-00 00:00:00' == $postarr['post_date'] ) { |
|---|
| 3540 | if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { |
|---|
| 3541 | $post_date = current_time( 'mysql' ); |
|---|
| 3542 | } else { |
|---|
| 3543 | $post_date = get_date_from_gmt( $postarr['post_date_gmt'] ); |
|---|
| 3544 | } |
|---|
| 3545 | } else { |
|---|
| 3546 | $post_date = $postarr['post_date']; |
|---|
| 3547 | } |
|---|
| 3548 | |
|---|
| 3549 | // Validate the date. |
|---|
| 3550 | $mm = substr( $post_date, 5, 2 ); |
|---|
| 3551 | $jj = substr( $post_date, 8, 2 ); |
|---|
| 3552 | $aa = substr( $post_date, 0, 4 ); |
|---|
| 3553 | $valid_date = wp_checkdate( $mm, $jj, $aa, $post_date ); |
|---|
| 3554 | if ( ! $valid_date ) { |
|---|
| 3555 | if ( $wp_error ) { |
|---|
| 3556 | return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); |
|---|
| 3557 | } else { |
|---|
| 3558 | return 0; |
|---|
| 3559 | } |
|---|
| 3560 | } |
|---|
| 3561 | |
|---|
| 3562 | if ( empty( $postarr['post_date_gmt'] ) || '0000-00-00 00:00:00' == $postarr['post_date_gmt'] ) { |
|---|
| 3563 | if ( ! in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) ) { |
|---|
| 3564 | $post_date_gmt = get_gmt_from_date( $post_date ); |
|---|
| 3565 | } else { |
|---|
| 3566 | $post_date_gmt = '0000-00-00 00:00:00'; |
|---|
| 3567 | } |
|---|
| 3568 | } else { |
|---|
| 3569 | $post_date_gmt = $postarr['post_date_gmt']; |
|---|
| 3570 | } |
|---|
| 3571 | |
|---|
| 3572 | if ( $update || '0000-00-00 00:00:00' == $post_date ) { |
|---|
| 3573 | $post_modified = current_time( 'mysql' ); |
|---|
| 3574 | $post_modified_gmt = current_time( 'mysql', 1 ); |
|---|
| 3575 | } else { |
|---|
| 3576 | $post_modified = $post_date; |
|---|
| 3577 | $post_modified_gmt = $post_date_gmt; |
|---|
| 3578 | } |
|---|
| 3579 | |
|---|
| 3580 | if ( 'attachment' !== $post_type ) { |
|---|
| 3581 | if ( 'publish' == $post_status ) { |
|---|
| 3582 | $now = gmdate( 'Y-m-d H:i:59' ); |
|---|
| 3583 | if ( mysql2date( 'U', $post_date_gmt, false ) > mysql2date( 'U', $now, false ) ) { |
|---|
| 3584 | $post_status = 'future'; |
|---|
| 3585 | } |
|---|
| 3586 | } elseif ( 'future' == $post_status ) { |
|---|
| 3587 | $now = gmdate( 'Y-m-d H:i:59' ); |
|---|
| 3588 | if ( mysql2date( 'U', $post_date_gmt, false ) <= mysql2date( 'U', $now, false ) ) { |
|---|
| 3589 | $post_status = 'publish'; |
|---|
| 3590 | } |
|---|
| 3591 | } |
|---|
| 3592 | } |
|---|
| 3593 | |
|---|
| 3594 | // Comment status. |
|---|
| 3595 | if ( empty( $postarr['comment_status'] ) ) { |
|---|
| 3596 | if ( $update ) { |
|---|
| 3597 | $comment_status = 'closed'; |
|---|
| 3598 | } else { |
|---|
| 3599 | $comment_status = get_default_comment_status( $post_type ); |
|---|
| 3600 | } |
|---|
| 3601 | } else { |
|---|
| 3602 | $comment_status = $postarr['comment_status']; |
|---|
| 3603 | } |
|---|
| 3604 | |
|---|
| 3605 | // These variables are needed by compact() later. |
|---|
| 3606 | $post_content_filtered = $postarr['post_content_filtered']; |
|---|
| 3607 | $post_author = isset( $postarr['post_author'] ) ? $postarr['post_author'] : $user_id; |
|---|
| 3608 | $ping_status = empty( $postarr['ping_status'] ) ? get_default_comment_status( $post_type, 'pingback' ) : $postarr['ping_status']; |
|---|
| 3609 | $to_ping = isset( $postarr['to_ping'] ) ? sanitize_trackback_urls( $postarr['to_ping'] ) : ''; |
|---|
| 3610 | $pinged = isset( $postarr['pinged'] ) ? $postarr['pinged'] : ''; |
|---|
| 3611 | $import_id = isset( $postarr['import_id'] ) ? $postarr['import_id'] : 0; |
|---|
| 3612 | |
|---|
| 3613 | /* |
|---|
| 3614 | * The 'wp_insert_post_parent' filter expects all variables to be present. |
|---|
| 3615 | * Previously, these variables would have already been extracted |
|---|
| 3616 | */ |
|---|
| 3617 | if ( isset( $postarr['menu_order'] ) ) { |
|---|
| 3618 | $menu_order = (int) $postarr['menu_order']; |
|---|
| 3619 | } else { |
|---|
| 3620 | $menu_order = 0; |
|---|
| 3621 | } |
|---|
| 3622 | |
|---|
| 3623 | $post_password = isset( $postarr['post_password'] ) ? $postarr['post_password'] : ''; |
|---|
| 3624 | if ( 'private' == $post_status ) { |
|---|
| 3625 | $post_password = ''; |
|---|
| 3626 | } |
|---|
| 3627 | |
|---|
| 3628 | if ( isset( $postarr['post_parent'] ) ) { |
|---|
| 3629 | $post_parent = (int) $postarr['post_parent']; |
|---|
| 3630 | } else { |
|---|
| 3631 | $post_parent = 0; |
|---|
| 3632 | } |
|---|
| 3633 | |
|---|
| 3634 | $new_postarr = array_merge( |
|---|
| 3635 | array( |
|---|
| 3636 | 'ID' => $post_ID, |
|---|
| 3637 | ), |
|---|
| 3638 | compact( array_diff( array_keys( $defaults ), array( 'context', 'filter' ) ) ) |
|---|
| 3639 | ); |
|---|
| 3640 | |
|---|
| 3641 | /** |
|---|
| 3642 | * Filters the post parent -- used to check for and prevent hierarchy loops. |
|---|
| 3643 | * |
|---|
| 3644 | * @since 3.1.0 |
|---|
| 3645 | * |
|---|
| 3646 | * @param int $post_parent Post parent ID. |
|---|
| 3647 | * @param int $post_ID Post ID. |
|---|
| 3648 | * @param array $new_postarr Array of parsed post data. |
|---|
| 3649 | * @param array $postarr Array of sanitized, but otherwise unmodified post data. |
|---|
| 3650 | */ |
|---|
| 3651 | $post_parent = apply_filters( 'wp_insert_post_parent', $post_parent, $post_ID, $new_postarr, $postarr ); |
|---|
| 3652 | |
|---|
| 3653 | /* |
|---|
| 3654 | * If the post is being untrashed and it has a desired slug stored in post meta, |
|---|
| 3655 | * reassign it. |
|---|
| 3656 | */ |
|---|
| 3657 | if ( 'trash' === $previous_status && 'trash' !== $post_status ) { |
|---|
| 3658 | $desired_post_slug = get_post_meta( $post_ID, '_wp_desired_post_slug', true ); |
|---|
| 3659 | if ( $desired_post_slug ) { |
|---|
| 3660 | delete_post_meta( $post_ID, '_wp_desired_post_slug' ); |
|---|
| 3661 | $post_name = $desired_post_slug; |
|---|
| 3662 | } |
|---|
| 3663 | } |
|---|
| 3664 | |
|---|
| 3665 | // If a trashed post has the desired slug, change it and let this post have it. |
|---|
| 3666 | if ( 'trash' !== $post_status && $post_name ) { |
|---|
| 3667 | wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID ); |
|---|
| 3668 | } |
|---|
| 3669 | |
|---|
| 3670 | // When trashing an existing post, change its slug to allow non-trashed posts to use it. |
|---|
| 3671 | if ( 'trash' === $post_status && 'trash' !== $previous_status && 'new' !== $previous_status ) { |
|---|
| 3672 | $post_name = wp_add_trashed_suffix_to_post_name_for_post( $post_ID ); |
|---|
| 3673 | } |
|---|
| 3674 | |
|---|
| 3675 | $post_name = wp_unique_post_slug( $post_name, $post_ID, $post_status, $post_type, $post_parent ); |
|---|
| 3676 | |
|---|
| 3677 | // Don't unslash. |
|---|
| 3678 | $post_mime_type = isset( $postarr['post_mime_type'] ) ? $postarr['post_mime_type'] : ''; |
|---|
| 3679 | |
|---|
| 3680 | // Expected_slashed (everything!). |
|---|
| 3681 | $data = compact( 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_content_filtered', 'post_title', 'post_excerpt', 'post_status', 'post_type', 'comment_status', 'ping_status', 'post_password', 'post_name', 'to_ping', 'pinged', 'post_modified', 'post_modified_gmt', 'post_parent', 'menu_order', 'post_mime_type', 'guid' ); |
|---|
| 3682 | |
|---|
| 3683 | $emoji_fields = array( 'post_title', 'post_content', 'post_excerpt' ); |
|---|
| 3684 | |
|---|
| 3685 | foreach ( $emoji_fields as $emoji_field ) { |
|---|
| 3686 | if ( isset( $data[ $emoji_field ] ) ) { |
|---|
| 3687 | $charset = $wpdb->get_col_charset( $wpdb->posts, $emoji_field ); |
|---|
| 3688 | if ( 'utf8' === $charset ) { |
|---|
| 3689 | $data[ $emoji_field ] = wp_encode_emoji( $data[ $emoji_field ] ); |
|---|
| 3690 | } |
|---|
| 3691 | } |
|---|
| 3692 | } |
|---|
| 3693 | |
|---|
| 3694 | if ( 'attachment' === $post_type ) { |
|---|
| 3695 | /** |
|---|
| 3696 | * Filters attachment post data before it is updated in or added to the database. |
|---|
| 3697 | * |
|---|
| 3698 | * @since 3.9.0 |
|---|
| 3699 | * |
|---|
| 3700 | * @param array $data An array of sanitized attachment post data. |
|---|
| 3701 | * @param array $postarr An array of unsanitized attachment post data. |
|---|
| 3702 | */ |
|---|
| 3703 | $data = apply_filters( 'wp_insert_attachment_data', $data, $postarr ); |
|---|
| 3704 | } else { |
|---|
| 3705 | /** |
|---|
| 3706 | * Filters slashed post data just before it is inserted into the database. |
|---|
| 3707 | * |
|---|
| 3708 | * @since 2.7.0 |
|---|
| 3709 | * |
|---|
| 3710 | * @param array $data An array of slashed post data. |
|---|
| 3711 | * @param array $postarr An array of sanitized, but otherwise unmodified post data. |
|---|
| 3712 | */ |
|---|
| 3713 | $data = apply_filters( 'wp_insert_post_data', $data, $postarr ); |
|---|
| 3714 | } |
|---|
| 3715 | $data = wp_unslash( $data ); |
|---|
| 3716 | $where = array( 'ID' => $post_ID ); |
|---|
| 3717 | |
|---|
| 3718 | if ( $update ) { |
|---|
| 3719 | /** |
|---|
| 3720 | * Fires immediately before an existing post is updated in the database. |
|---|
| 3721 | * |
|---|
| 3722 | * @since 2.5.0 |
|---|
| 3723 | * |
|---|
| 3724 | * @param int $post_ID Post ID. |
|---|
| 3725 | * @param array $data Array of unslashed post data. |
|---|
| 3726 | */ |
|---|
| 3727 | do_action( 'pre_post_update', $post_ID, $data ); |
|---|
| 3728 | if ( false === $wpdb->update( $wpdb->posts, $data, $where ) ) { |
|---|
| 3729 | if ( $wp_error ) { |
|---|
| 3730 | return new WP_Error( 'db_update_error', __( 'Could not update post in the database' ), $wpdb->last_error ); |
|---|
| 3731 | } else { |
|---|
| 3732 | return 0; |
|---|
| 3733 | } |
|---|
| 3734 | } |
|---|
| 3735 | } else { |
|---|
| 3736 | // If there is a suggested ID, use it if not already present. |
|---|
| 3737 | if ( ! empty( $import_id ) ) { |
|---|
| 3738 | $import_id = (int) $import_id; |
|---|
| 3739 | if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE ID = %d", $import_id ) ) ) { |
|---|
| 3740 | $data['ID'] = $import_id; |
|---|
| 3741 | } |
|---|
| 3742 | } |
|---|
| 3743 | if ( false === $wpdb->insert( $wpdb->posts, $data ) ) { |
|---|
| 3744 | if ( $wp_error ) { |
|---|
| 3745 | return new WP_Error( 'db_insert_error', __( 'Could not insert post into the database' ), $wpdb->last_error ); |
|---|
| 3746 | } else { |
|---|
| 3747 | return 0; |
|---|
| 3748 | } |
|---|
| 3749 | } |
|---|
| 3750 | $post_ID = (int) $wpdb->insert_id; |
|---|
| 3751 | |
|---|
| 3752 | // Use the newly generated $post_ID. |
|---|
| 3753 | $where = array( 'ID' => $post_ID ); |
|---|
| 3754 | } |
|---|
| 3755 | |
|---|
| 3756 | if ( empty( $data['post_name'] ) && ! in_array( $data['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) ) { |
|---|
| 3757 | $data['post_name'] = wp_unique_post_slug( sanitize_title( $data['post_title'], $post_ID ), $post_ID, $data['post_status'], $post_type, $post_parent ); |
|---|
| 3758 | $wpdb->update( $wpdb->posts, array( 'post_name' => $data['post_name'] ), $where ); |
|---|
| 3759 | clean_post_cache( $post_ID ); |
|---|
| 3760 | } |
|---|
| 3761 | |
|---|
| 3762 | if ( is_object_in_taxonomy( $post_type, 'category' ) ) { |
|---|
| 3763 | wp_set_post_categories( $post_ID, $post_category ); |
|---|
| 3764 | } |
|---|
| 3765 | |
|---|
| 3766 | if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) { |
|---|
| 3767 | wp_set_post_tags( $post_ID, $postarr['tags_input'] ); |
|---|
| 3768 | } |
|---|
| 3769 | |
|---|
| 3770 | // New-style support for all custom taxonomies. |
|---|
| 3771 | if ( ! empty( $postarr['tax_input'] ) ) { |
|---|
| 3772 | foreach ( $postarr['tax_input'] as $taxonomy => $tags ) { |
|---|
| 3773 | $taxonomy_obj = get_taxonomy( $taxonomy ); |
|---|
| 3774 | if ( ! $taxonomy_obj ) { |
|---|
| 3775 | /* translators: %s: taxonomy name */ |
|---|
| 3776 | _doing_it_wrong( __FUNCTION__, sprintf( __( 'Invalid taxonomy: %s.' ), $taxonomy ), '4.4.0' ); |
|---|
| 3777 | continue; |
|---|
| 3778 | } |
|---|
| 3779 | |
|---|
| 3780 | // array = hierarchical, string = non-hierarchical. |
|---|
| 3781 | if ( is_array( $tags ) ) { |
|---|
| 3782 | $tags = array_filter( $tags ); |
|---|
| 3783 | } |
|---|
| 3784 | if ( current_user_can( $taxonomy_obj->cap->assign_terms ) ) { |
|---|
| 3785 | wp_set_post_terms( $post_ID, $tags, $taxonomy ); |
|---|
| 3786 | } |
|---|
| 3787 | } |
|---|
| 3788 | } |
|---|
| 3789 | |
|---|
| 3790 | if ( ! empty( $postarr['meta_input'] ) ) { |
|---|
| 3791 | foreach ( $postarr['meta_input'] as $field => $value ) { |
|---|
| 3792 | update_post_meta( $post_ID, $field, $value ); |
|---|
| 3793 | } |
|---|
| 3794 | } |
|---|
| 3795 | |
|---|
| 3796 | $current_guid = get_post_field( 'guid', $post_ID ); |
|---|
| 3797 | |
|---|
| 3798 | // Set GUID. |
|---|
| 3799 | if ( ! $update && '' == $current_guid ) { |
|---|
| 3800 | $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post_ID ) ), $where ); |
|---|
| 3801 | } |
|---|
| 3802 | |
|---|
| 3803 | if ( 'attachment' === $postarr['post_type'] ) { |
|---|
| 3804 | if ( ! empty( $postarr['file'] ) ) { |
|---|
| 3805 | update_attached_file( $post_ID, $postarr['file'] ); |
|---|
| 3806 | } |
|---|
| 3807 | |
|---|
| 3808 | if ( ! empty( $postarr['context'] ) ) { |
|---|
| 3809 | add_post_meta( $post_ID, '_wp_attachment_context', $postarr['context'], true ); |
|---|
| 3810 | } |
|---|
| 3811 | } |
|---|
| 3812 | |
|---|
| 3813 | // Set or remove featured image. |
|---|
| 3814 | if ( isset( $postarr['_thumbnail_id'] ) ) { |
|---|
| 3815 | $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' ) || 'revision' === $post_type; |
|---|
| 3816 | if ( ! $thumbnail_support && 'attachment' === $post_type && $post_mime_type ) { |
|---|
| 3817 | if ( wp_attachment_is( 'audio', $post_ID ) ) { |
|---|
| 3818 | $thumbnail_support = post_type_supports( 'attachment:audio', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:audio' ); |
|---|
| 3819 | } elseif ( wp_attachment_is( 'video', $post_ID ) ) { |
|---|
| 3820 | $thumbnail_support = post_type_supports( 'attachment:video', 'thumbnail' ) || current_theme_supports( 'post-thumbnails', 'attachment:video' ); |
|---|
| 3821 | } |
|---|
| 3822 | } |
|---|
| 3823 | |
|---|
| 3824 | if ( $thumbnail_support ) { |
|---|
| 3825 | $thumbnail_id = intval( $postarr['_thumbnail_id'] ); |
|---|
| 3826 | if ( -1 === $thumbnail_id ) { |
|---|
| 3827 | delete_post_thumbnail( $post_ID ); |
|---|
| 3828 | } else { |
|---|
| 3829 | set_post_thumbnail( $post_ID, $thumbnail_id ); |
|---|
| 3830 | } |
|---|
| 3831 | } |
|---|
| 3832 | } |
|---|
| 3833 | |
|---|
| 3834 | clean_post_cache( $post_ID ); |
|---|
| 3835 | |
|---|
| 3836 | $post = get_post( $post_ID ); |
|---|
| 3837 | |
|---|
| 3838 | if ( ! empty( $postarr['page_template'] ) ) { |
|---|
| 3839 | $post->page_template = $postarr['page_template']; |
|---|
| 3840 | $page_templates = wp_get_theme()->get_page_templates( $post ); |
|---|
| 3841 | if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) { |
|---|
| 3842 | if ( $wp_error ) { |
|---|
| 3843 | return new WP_Error( 'invalid_page_template', __( 'Invalid page template.' ) ); |
|---|
| 3844 | } |
|---|
| 3845 | update_post_meta( $post_ID, '_wp_page_template', 'default' ); |
|---|
| 3846 | } else { |
|---|
| 3847 | update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] ); |
|---|
| 3848 | } |
|---|
| 3849 | } |
|---|
| 3850 | |
|---|
| 3851 | if ( 'attachment' !== $postarr['post_type'] ) { |
|---|
| 3852 | wp_transition_post_status( $data['post_status'], $previous_status, $post ); |
|---|
| 3853 | } else { |
|---|
| 3854 | if ( $update ) { |
|---|
| 3855 | /** |
|---|
| 3856 | * Fires once an existing attachment has been updated. |
|---|
| 3857 | * |
|---|
| 3858 | * @since 2.0.0 |
|---|
| 3859 | * |
|---|
| 3860 | * @param int $post_ID Attachment ID. |
|---|
| 3861 | */ |
|---|
| 3862 | do_action( 'edit_attachment', $post_ID ); |
|---|
| 3863 | $post_after = get_post( $post_ID ); |
|---|
| 3864 | |
|---|
| 3865 | /** |
|---|
| 3866 | * Fires once an existing attachment has been updated. |
|---|
| 3867 | * |
|---|
| 3868 | * @since 4.4.0 |
|---|
| 3869 | * |
|---|
| 3870 | * @param int $post_ID Post ID. |
|---|
| 3871 | * @param WP_Post $post_after Post object following the update. |
|---|
| 3872 | * @param WP_Post $post_before Post object before the update. |
|---|
| 3873 | */ |
|---|
| 3874 | do_action( 'attachment_updated', $post_ID, $post_after, $post_before ); |
|---|
| 3875 | } else { |
|---|
| 3876 | |
|---|
| 3877 | /** |
|---|
| 3878 | * Fires once an attachment has been added. |
|---|
| 3879 | * |
|---|
| 3880 | * @since 2.0.0 |
|---|
| 3881 | * |
|---|
| 3882 | * @param int $post_ID Attachment ID. |
|---|
| 3883 | */ |
|---|
| 3884 | do_action( 'add_attachment', $post_ID ); |
|---|
| 3885 | } |
|---|
| 3886 | |
|---|
| 3887 | return $post_ID; |
|---|
| 3888 | } |
|---|
| 3889 | |
|---|
| 3890 | if ( $update ) { |
|---|
| 3891 | /** |
|---|
| 3892 | * Fires once an existing post has been updated. |
|---|
| 3893 | * |
|---|
| 3894 | * The dynamic portion of the hook name, `$post->post_type`, refers to |
|---|
| 3895 | * the post type slug. |
|---|
| 3896 | * |
|---|
| 3897 | * @since 5.1.0 |
|---|
| 3898 | * |
|---|
| 3899 | * @param int $post_ID Post ID. |
|---|
| 3900 | * @param WP_Post $post Post object. |
|---|
| 3901 | */ |
|---|
| 3902 | do_action( "edit_post_{$post->post_type}", $post_ID, $post ); |
|---|
| 3903 | |
|---|
| 3904 | /** |
|---|
| 3905 | * Fires once an existing post has been updated. |
|---|
| 3906 | * |
|---|
| 3907 | * @since 1.2.0 |
|---|
| 3908 | * |
|---|
| 3909 | * @param int $post_ID Post ID. |
|---|
| 3910 | * @param WP_Post $post Post object. |
|---|
| 3911 | */ |
|---|
| 3912 | do_action( 'edit_post', $post_ID, $post ); |
|---|
| 3913 | |
|---|
| 3914 | $post_after = get_post( $post_ID ); |
|---|
| 3915 | |
|---|
| 3916 | /** |
|---|
| 3917 | * Fires once an existing post has been updated. |
|---|
| 3918 | * |
|---|
| 3919 | * @since 3.0.0 |
|---|
| 3920 | * |
|---|
| 3921 | * @param int $post_ID Post ID. |
|---|
| 3922 | * @param WP_Post $post_after Post object following the update. |
|---|
| 3923 | * @param WP_Post $post_before Post object before the update. |
|---|
| 3924 | */ |
|---|
| 3925 | do_action( 'post_updated', $post_ID, $post_after, $post_before ); |
|---|
| 3926 | } |
|---|
| 3927 | |
|---|
| 3928 | /** |
|---|
| 3929 | * Fires once a post has been saved. |
|---|
| 3930 | * |
|---|
| 3931 | * The dynamic portion of the hook name, `$post->post_type`, refers to |
|---|
| 3932 | * the post type slug. |
|---|
| 3933 | * |
|---|
| 3934 | * @since 3.7.0 |
|---|
| 3935 | * |
|---|
| 3936 | * @param int $post_ID Post ID. |
|---|
| 3937 | * @param WP_Post $post Post object. |
|---|
| 3938 | * @param bool $update Whether this is an existing post being updated or not. |
|---|
| 3939 | */ |
|---|
| 3940 | do_action( "save_post_{$post->post_type}", $post_ID, $post, $update ); |
|---|
| 3941 | |
|---|
| 3942 | /** |
|---|
| 3943 | * Fires once a post has been saved. |
|---|
| 3944 | * |
|---|
| 3945 | * @since 1.5.0 |
|---|
| 3946 | * |
|---|
| 3947 | * @param int $post_ID Post ID. |
|---|
| 3948 | * @param WP_Post $post Post object. |
|---|
| 3949 | * @param bool $update Whether this is an existing post being updated or not. |
|---|
| 3950 | */ |
|---|
| 3951 | do_action( 'save_post', $post_ID, $post, $update ); |
|---|
| 3952 | |
|---|
| 3953 | /** |
|---|
| 3954 | * Fires once a post has been saved. |
|---|
| 3955 | * |
|---|
| 3956 | * @since 2.0.0 |
|---|
| 3957 | * |
|---|
| 3958 | * @param int $post_ID Post ID. |
|---|
| 3959 | * @param WP_Post $post Post object. |
|---|
| 3960 | * @param bool $update Whether this is an existing post being updated or not. |
|---|
| 3961 | */ |
|---|
| 3962 | do_action( 'wp_insert_post', $post_ID, $post, $update ); |
|---|
| 3963 | |
|---|
| 3964 | return $post_ID; |
|---|
| 3965 | } |
|---|
| 3966 | |
|---|
| 3967 | /** |
|---|
| 3968 | * Update a post with new post data. |
|---|
| 3969 | * |
|---|
| 3970 | * The date does not have to be set for drafts. You can set the date and it will |
|---|
| 3971 | * not be overridden. |
|---|
| 3972 | * |
|---|
| 3973 | * @since 1.0.0 |
|---|
| 3974 | * |
|---|
| 3975 | * @param array|object $postarr Optional. Post data. Arrays are expected to be escaped, |
|---|
| 3976 | * objects are not. Default array. |
|---|
| 3977 | * @param bool $wp_error Optional. Allow return of WP_Error on failure. Default false. |
|---|
| 3978 | * @return int|WP_Error The value 0 or WP_Error on failure. The post ID on success. |
|---|
| 3979 | */ |
|---|
| 3980 | function wp_update_post( $postarr = array(), $wp_error = false ) { |
|---|
| 3981 | if ( is_object( $postarr ) ) { |
|---|
| 3982 | // Non-escaped post was passed. |
|---|
| 3983 | $postarr = get_object_vars( $postarr ); |
|---|
| 3984 | $postarr = wp_slash( $postarr ); |
|---|
| 3985 | } |
|---|
| 3986 | |
|---|
| 3987 | // First, get all of the original fields. |
|---|
| 3988 | $post = get_post( $postarr['ID'], ARRAY_A ); |
|---|
| 3989 | |
|---|
| 3990 | if ( is_null( $post ) ) { |
|---|
| 3991 | if ( $wp_error ) { |
|---|
| 3992 | return new WP_Error( 'invalid_post', __( 'Invalid post ID.' ) ); |
|---|
| 3993 | } |
|---|
| 3994 | return 0; |
|---|
| 3995 | } |
|---|
| 3996 | |
|---|
| 3997 | // Escape data pulled from DB. |
|---|
| 3998 | $post = wp_slash( $post ); |
|---|
| 3999 | |
|---|
| 4000 | // Passed post category list overwrites existing category list if not empty. |
|---|
| 4001 | if ( isset( $postarr['post_category'] ) && is_array( $postarr['post_category'] ) |
|---|
| 4002 | && 0 != count( $postarr['post_category'] ) ) { |
|---|
| 4003 | $post_cats = $postarr['post_category']; |
|---|
| 4004 | } else { |
|---|
| 4005 | $post_cats = $post['post_category']; |
|---|
| 4006 | } |
|---|
| 4007 | |
|---|
| 4008 | // Drafts shouldn't be assigned a date unless explicitly done so by the user. |
|---|
| 4009 | if ( isset( $post['post_status'] ) && in_array( $post['post_status'], array( 'draft', 'pending', 'auto-draft' ) ) && empty( $postarr['edit_date'] ) && |
|---|
| 4010 | ( '0000-00-00 00:00:00' == $post['post_date_gmt'] ) ) { |
|---|
| 4011 | $clear_date = true; |
|---|
| 4012 | } else { |
|---|
| 4013 | $clear_date = false; |
|---|
| 4014 | } |
|---|
| 4015 | |
|---|
| 4016 | // Merge old and new fields with new fields overwriting old ones. |
|---|
| 4017 | $postarr = array_merge( $post, $postarr ); |
|---|
| 4018 | $postarr['post_category'] = $post_cats; |
|---|
| 4019 | if ( $clear_date ) { |
|---|
| 4020 | $postarr['post_date'] = current_time( 'mysql' ); |
|---|
| 4021 | $postarr['post_date_gmt'] = ''; |
|---|
| 4022 | } |
|---|
| 4023 | |
|---|
| 4024 | if ( $postarr['post_type'] == 'attachment' ) { |
|---|
| 4025 | return wp_insert_attachment( $postarr, false, 0, $wp_error ); |
|---|
| 4026 | } |
|---|
| 4027 | |
|---|
| 4028 | return wp_insert_post( $postarr, $wp_error ); |
|---|
| 4029 | } |
|---|
| 4030 | |
|---|
| 4031 | /** |
|---|
| 4032 | * Publish a post by transitioning the post status. |
|---|
| 4033 | * |
|---|
| 4034 | * @since 2.1.0 |
|---|
| 4035 | * |
|---|
| 4036 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4037 | * |
|---|
| 4038 | * @param int|WP_Post $post Post ID or post object. |
|---|
| 4039 | */ |
|---|
| 4040 | function wp_publish_post( $post ) { |
|---|
| 4041 | global $wpdb; |
|---|
| 4042 | |
|---|
| 4043 | if ( ! $post = get_post( $post ) ) { |
|---|
| 4044 | return; |
|---|
| 4045 | } |
|---|
| 4046 | |
|---|
| 4047 | if ( 'publish' == $post->post_status ) { |
|---|
| 4048 | return; |
|---|
| 4049 | } |
|---|
| 4050 | |
|---|
| 4051 | $wpdb->update( $wpdb->posts, array( 'post_status' => 'publish' ), array( 'ID' => $post->ID ) ); |
|---|
| 4052 | |
|---|
| 4053 | clean_post_cache( $post->ID ); |
|---|
| 4054 | |
|---|
| 4055 | $old_status = $post->post_status; |
|---|
| 4056 | $post->post_status = 'publish'; |
|---|
| 4057 | wp_transition_post_status( 'publish', $old_status, $post ); |
|---|
| 4058 | |
|---|
| 4059 | /** This action is documented in wp-includes/post.php */ |
|---|
| 4060 | do_action( "edit_post_{$post->post_type}", $post->ID, $post ); |
|---|
| 4061 | |
|---|
| 4062 | /** This action is documented in wp-includes/post.php */ |
|---|
| 4063 | do_action( 'edit_post', $post->ID, $post ); |
|---|
| 4064 | |
|---|
| 4065 | /** This action is documented in wp-includes/post.php */ |
|---|
| 4066 | do_action( "save_post_{$post->post_type}", $post->ID, $post, true ); |
|---|
| 4067 | |
|---|
| 4068 | /** This action is documented in wp-includes/post.php */ |
|---|
| 4069 | do_action( 'save_post', $post->ID, $post, true ); |
|---|
| 4070 | |
|---|
| 4071 | /** This action is documented in wp-includes/post.php */ |
|---|
| 4072 | do_action( 'wp_insert_post', $post->ID, $post, true ); |
|---|
| 4073 | } |
|---|
| 4074 | |
|---|
| 4075 | /** |
|---|
| 4076 | * Publish future post and make sure post ID has future post status. |
|---|
| 4077 | * |
|---|
| 4078 | * Invoked by cron 'publish_future_post' event. This safeguard prevents cron |
|---|
| 4079 | * from publishing drafts, etc. |
|---|
| 4080 | * |
|---|
| 4081 | * @since 2.5.0 |
|---|
| 4082 | * |
|---|
| 4083 | * @param int|WP_Post $post_id Post ID or post object. |
|---|
| 4084 | */ |
|---|
| 4085 | function check_and_publish_future_post( $post_id ) { |
|---|
| 4086 | $post = get_post( $post_id ); |
|---|
| 4087 | |
|---|
| 4088 | if ( empty( $post ) ) { |
|---|
| 4089 | return; |
|---|
| 4090 | } |
|---|
| 4091 | |
|---|
| 4092 | if ( 'future' != $post->post_status ) { |
|---|
| 4093 | return; |
|---|
| 4094 | } |
|---|
| 4095 | |
|---|
| 4096 | $time = strtotime( $post->post_date_gmt . ' GMT' ); |
|---|
| 4097 | |
|---|
| 4098 | // Uh oh, someone jumped the gun! |
|---|
| 4099 | if ( $time > time() ) { |
|---|
| 4100 | wp_clear_scheduled_hook( 'publish_future_post', array( $post_id ) ); // clear anything else in the system |
|---|
| 4101 | wp_schedule_single_event( $time, 'publish_future_post', array( $post_id ) ); |
|---|
| 4102 | return; |
|---|
| 4103 | } |
|---|
| 4104 | |
|---|
| 4105 | // wp_publish_post() returns no meaningful value. |
|---|
| 4106 | wp_publish_post( $post_id ); |
|---|
| 4107 | } |
|---|
| 4108 | |
|---|
| 4109 | /** |
|---|
| 4110 | * Computes a unique slug for the post, when given the desired slug and some post details. |
|---|
| 4111 | * |
|---|
| 4112 | * @since 2.8.0 |
|---|
| 4113 | * |
|---|
| 4114 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4115 | * @global WP_Rewrite $wp_rewrite |
|---|
| 4116 | * |
|---|
| 4117 | * @param string $slug The desired slug (post_name). |
|---|
| 4118 | * @param int $post_ID Post ID. |
|---|
| 4119 | * @param string $post_status No uniqueness checks are made if the post is still draft or pending. |
|---|
| 4120 | * @param string $post_type Post type. |
|---|
| 4121 | * @param int $post_parent Post parent ID. |
|---|
| 4122 | * @return string Unique slug for the post, based on $post_name (with a -1, -2, etc. suffix) |
|---|
| 4123 | */ |
|---|
| 4124 | function wp_unique_post_slug( $slug, $post_ID, $post_status, $post_type, $post_parent ) { |
|---|
| 4125 | if ( in_array( $post_status, array( 'draft', 'pending', 'auto-draft' ) ) || ( 'inherit' == $post_status && 'revision' == $post_type ) || 'user_request' === $post_type ) { |
|---|
| 4126 | return $slug; |
|---|
| 4127 | } |
|---|
| 4128 | |
|---|
| 4129 | /** |
|---|
| 4130 | * Filters the post slug before it is generated to be unique. |
|---|
| 4131 | * |
|---|
| 4132 | * Returning a non-null value will short-circuit the |
|---|
| 4133 | * unique slug generation, returning the passed value instead. |
|---|
| 4134 | * |
|---|
| 4135 | * @since 5.1.0 |
|---|
| 4136 | * |
|---|
| 4137 | * @param string $override_slug Short-circuit return value. |
|---|
| 4138 | * @param string $slug The desired slug (post_name). |
|---|
| 4139 | * @param int $post_ID Post ID. |
|---|
| 4140 | * @param string $post_status The post status. |
|---|
| 4141 | * @param string $post_type Post type. |
|---|
| 4142 | * @param int $post_parent Post parent ID. |
|---|
| 4143 | */ |
|---|
| 4144 | $override_slug = apply_filters( 'pre_wp_unique_post_slug', null, $slug, $post_ID, $post_status, $post_type, $post_parent ); |
|---|
| 4145 | if ( null !== $override_slug ) { |
|---|
| 4146 | return $override_slug; |
|---|
| 4147 | } |
|---|
| 4148 | |
|---|
| 4149 | global $wpdb, $wp_rewrite; |
|---|
| 4150 | |
|---|
| 4151 | $original_slug = $slug; |
|---|
| 4152 | |
|---|
| 4153 | $feeds = $wp_rewrite->feeds; |
|---|
| 4154 | if ( ! is_array( $feeds ) ) { |
|---|
| 4155 | $feeds = array(); |
|---|
| 4156 | } |
|---|
| 4157 | |
|---|
| 4158 | if ( 'attachment' == $post_type ) { |
|---|
| 4159 | // Attachment slugs must be unique across all types. |
|---|
| 4160 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND ID != %d LIMIT 1"; |
|---|
| 4161 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_ID ) ); |
|---|
| 4162 | |
|---|
| 4163 | /** |
|---|
| 4164 | * Filters whether the post slug would make a bad attachment slug. |
|---|
| 4165 | * |
|---|
| 4166 | * @since 3.1.0 |
|---|
| 4167 | * |
|---|
| 4168 | * @param bool $bad_slug Whether the slug would be bad as an attachment slug. |
|---|
| 4169 | * @param string $slug The post slug. |
|---|
| 4170 | */ |
|---|
| 4171 | if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || apply_filters( 'wp_unique_post_slug_is_bad_attachment_slug', false, $slug ) ) { |
|---|
| 4172 | $suffix = 2; |
|---|
| 4173 | do { |
|---|
| 4174 | $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
|---|
| 4175 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_ID ) ); |
|---|
| 4176 | $suffix++; |
|---|
| 4177 | } while ( $post_name_check ); |
|---|
| 4178 | $slug = $alt_post_name; |
|---|
| 4179 | } |
|---|
| 4180 | } elseif ( is_post_type_hierarchical( $post_type ) ) { |
|---|
| 4181 | if ( 'nav_menu_item' == $post_type ) { |
|---|
| 4182 | return $slug; |
|---|
| 4183 | } |
|---|
| 4184 | |
|---|
| 4185 | /* |
|---|
| 4186 | * Page slugs must be unique within their own trees. Pages are in a separate |
|---|
| 4187 | * namespace than posts so page slugs are allowed to overlap post slugs. |
|---|
| 4188 | */ |
|---|
| 4189 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type IN ( %s, 'attachment' ) AND ID != %d AND post_parent = %d LIMIT 1"; |
|---|
| 4190 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID, $post_parent ) ); |
|---|
| 4191 | |
|---|
| 4192 | /** |
|---|
| 4193 | * Filters whether the post slug would make a bad hierarchical post slug. |
|---|
| 4194 | * |
|---|
| 4195 | * @since 3.1.0 |
|---|
| 4196 | * |
|---|
| 4197 | * @param bool $bad_slug Whether the post slug would be bad in a hierarchical post context. |
|---|
| 4198 | * @param string $slug The post slug. |
|---|
| 4199 | * @param string $post_type Post type. |
|---|
| 4200 | * @param int $post_parent Post parent ID. |
|---|
| 4201 | */ |
|---|
| 4202 | if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || preg_match( "@^($wp_rewrite->pagination_base)?\d+$@", $slug ) || apply_filters( 'wp_unique_post_slug_is_bad_hierarchical_slug', false, $slug, $post_type, $post_parent ) ) { |
|---|
| 4203 | $suffix = 2; |
|---|
| 4204 | do { |
|---|
| 4205 | $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
|---|
| 4206 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID, $post_parent ) ); |
|---|
| 4207 | $suffix++; |
|---|
| 4208 | } while ( $post_name_check ); |
|---|
| 4209 | $slug = $alt_post_name; |
|---|
| 4210 | } |
|---|
| 4211 | } else { |
|---|
| 4212 | // Post slugs must be unique across all posts. |
|---|
| 4213 | $check_sql = "SELECT post_name FROM $wpdb->posts WHERE post_name = %s AND post_type = %s AND ID != %d LIMIT 1"; |
|---|
| 4214 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $post_type, $post_ID ) ); |
|---|
| 4215 | |
|---|
| 4216 | // Prevent new post slugs that could result in URLs that conflict with date archives. |
|---|
| 4217 | $post = get_post( $post_ID ); |
|---|
| 4218 | $conflicts_with_date_archive = false; |
|---|
| 4219 | if ( 'post' === $post_type && ( ! $post || $post->post_name !== $slug ) && preg_match( '/^[0-9]+$/', $slug ) && $slug_num = intval( $slug ) ) { |
|---|
| 4220 | $permastructs = array_values( array_filter( explode( '/', get_option( 'permalink_structure' ) ) ) ); |
|---|
| 4221 | $postname_index = array_search( '%postname%', $permastructs ); |
|---|
| 4222 | |
|---|
| 4223 | /* |
|---|
| 4224 | * Potential date clashes are as follows: |
|---|
| 4225 | * |
|---|
| 4226 | * - Any integer in the first permastruct position could be a year. |
|---|
| 4227 | * - An integer between 1 and 12 that follows 'year' conflicts with 'monthnum'. |
|---|
| 4228 | * - An integer between 1 and 31 that follows 'monthnum' conflicts with 'day'. |
|---|
| 4229 | */ |
|---|
| 4230 | if ( 0 === $postname_index || |
|---|
| 4231 | ( $postname_index && '%year%' === $permastructs[ $postname_index - 1 ] && 13 > $slug_num ) || |
|---|
| 4232 | ( $postname_index && '%monthnum%' === $permastructs[ $postname_index - 1 ] && 32 > $slug_num ) |
|---|
| 4233 | ) { |
|---|
| 4234 | $conflicts_with_date_archive = true; |
|---|
| 4235 | } |
|---|
| 4236 | } |
|---|
| 4237 | |
|---|
| 4238 | /** |
|---|
| 4239 | * Filters whether the post slug would be bad as a flat slug. |
|---|
| 4240 | * |
|---|
| 4241 | * @since 3.1.0 |
|---|
| 4242 | * |
|---|
| 4243 | * @param bool $bad_slug Whether the post slug would be bad as a flat slug. |
|---|
| 4244 | * @param string $slug The post slug. |
|---|
| 4245 | * @param string $post_type Post type. |
|---|
| 4246 | */ |
|---|
| 4247 | if ( $post_name_check || in_array( $slug, $feeds ) || 'embed' === $slug || $conflicts_with_date_archive || apply_filters( 'wp_unique_post_slug_is_bad_flat_slug', false, $slug, $post_type ) ) { |
|---|
| 4248 | $suffix = 2; |
|---|
| 4249 | do { |
|---|
| 4250 | $alt_post_name = _truncate_post_slug( $slug, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix"; |
|---|
| 4251 | $post_name_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $alt_post_name, $post_type, $post_ID ) ); |
|---|
| 4252 | $suffix++; |
|---|
| 4253 | } while ( $post_name_check ); |
|---|
| 4254 | $slug = $alt_post_name; |
|---|
| 4255 | } |
|---|
| 4256 | } |
|---|
| 4257 | |
|---|
| 4258 | /** |
|---|
| 4259 | * Filters the unique post slug. |
|---|
| 4260 | * |
|---|
| 4261 | * @since 3.3.0 |
|---|
| 4262 | * |
|---|
| 4263 | * @param string $slug The post slug. |
|---|
| 4264 | * @param int $post_ID Post ID. |
|---|
| 4265 | * @param string $post_status The post status. |
|---|
| 4266 | * @param string $post_type Post type. |
|---|
| 4267 | * @param int $post_parent Post parent ID |
|---|
| 4268 | * @param string $original_slug The original post slug. |
|---|
| 4269 | */ |
|---|
| 4270 | return apply_filters( 'wp_unique_post_slug', $slug, $post_ID, $post_status, $post_type, $post_parent, $original_slug ); |
|---|
| 4271 | } |
|---|
| 4272 | |
|---|
| 4273 | /** |
|---|
| 4274 | * Truncate a post slug. |
|---|
| 4275 | * |
|---|
| 4276 | * @since 3.6.0 |
|---|
| 4277 | * @access private |
|---|
| 4278 | * |
|---|
| 4279 | * @see utf8_uri_encode() |
|---|
| 4280 | * |
|---|
| 4281 | * @param string $slug The slug to truncate. |
|---|
| 4282 | * @param int $length Optional. Max length of the slug. Default 200 (characters). |
|---|
| 4283 | * @return string The truncated slug. |
|---|
| 4284 | */ |
|---|
| 4285 | function _truncate_post_slug( $slug, $length = 200 ) { |
|---|
| 4286 | if ( strlen( $slug ) > $length ) { |
|---|
| 4287 | $decoded_slug = urldecode( $slug ); |
|---|
| 4288 | if ( $decoded_slug === $slug ) { |
|---|
| 4289 | $slug = substr( $slug, 0, $length ); |
|---|
| 4290 | } else { |
|---|
| 4291 | $slug = utf8_uri_encode( $decoded_slug, $length ); |
|---|
| 4292 | } |
|---|
| 4293 | } |
|---|
| 4294 | |
|---|
| 4295 | return rtrim( $slug, '-' ); |
|---|
| 4296 | } |
|---|
| 4297 | |
|---|
| 4298 | /** |
|---|
| 4299 | * Add tags to a post. |
|---|
| 4300 | * |
|---|
| 4301 | * @see wp_set_post_tags() |
|---|
| 4302 | * |
|---|
| 4303 | * @since 2.3.0 |
|---|
| 4304 | * |
|---|
| 4305 | * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
|---|
| 4306 | * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags |
|---|
| 4307 | * separated by commas. Default empty. |
|---|
| 4308 | * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure. |
|---|
| 4309 | */ |
|---|
| 4310 | function wp_add_post_tags( $post_id = 0, $tags = '' ) { |
|---|
| 4311 | return wp_set_post_tags( $post_id, $tags, true ); |
|---|
| 4312 | } |
|---|
| 4313 | |
|---|
| 4314 | /** |
|---|
| 4315 | * Set the tags for a post. |
|---|
| 4316 | * |
|---|
| 4317 | * @since 2.3.0 |
|---|
| 4318 | * |
|---|
| 4319 | * @see wp_set_object_terms() |
|---|
| 4320 | * |
|---|
| 4321 | * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
|---|
| 4322 | * @param string|array $tags Optional. An array of tags to set for the post, or a string of tags |
|---|
| 4323 | * separated by commas. Default empty. |
|---|
| 4324 | * @param bool $append Optional. If true, don't delete existing tags, just add on. If false, |
|---|
| 4325 | * replace the tags with the new tags. Default false. |
|---|
| 4326 | * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
|---|
| 4327 | */ |
|---|
| 4328 | function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { |
|---|
| 4329 | return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); |
|---|
| 4330 | } |
|---|
| 4331 | |
|---|
| 4332 | /** |
|---|
| 4333 | * Set the terms for a post. |
|---|
| 4334 | * |
|---|
| 4335 | * @since 2.8.0 |
|---|
| 4336 | * |
|---|
| 4337 | * @see wp_set_object_terms() |
|---|
| 4338 | * |
|---|
| 4339 | * @param int $post_id Optional. The Post ID. Does not default to the ID of the global $post. |
|---|
| 4340 | * @param string|array $tags Optional. An array of terms to set for the post, or a string of terms |
|---|
| 4341 | * separated by commas. Hierarchical taxonomies must always pass IDs rather |
|---|
| 4342 | * than names so that children with the same names but different parents |
|---|
| 4343 | * aren't confused. Default empty. |
|---|
| 4344 | * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'. |
|---|
| 4345 | * @param bool $append Optional. If true, don't delete existing terms, just add on. If false, |
|---|
| 4346 | * replace the terms with the new terms. Default false. |
|---|
| 4347 | * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure. |
|---|
| 4348 | */ |
|---|
| 4349 | function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) { |
|---|
| 4350 | $post_id = (int) $post_id; |
|---|
| 4351 | |
|---|
| 4352 | if ( ! $post_id ) { |
|---|
| 4353 | return false; |
|---|
| 4354 | } |
|---|
| 4355 | |
|---|
| 4356 | if ( empty( $tags ) ) { |
|---|
| 4357 | $tags = array(); |
|---|
| 4358 | } |
|---|
| 4359 | |
|---|
| 4360 | if ( ! is_array( $tags ) ) { |
|---|
| 4361 | $comma = _x( ',', 'tag delimiter' ); |
|---|
| 4362 | if ( ',' !== $comma ) { |
|---|
| 4363 | $tags = str_replace( $comma, ',', $tags ); |
|---|
| 4364 | } |
|---|
| 4365 | $tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) ); |
|---|
| 4366 | } |
|---|
| 4367 | |
|---|
| 4368 | /* |
|---|
| 4369 | * Hierarchical taxonomies must always pass IDs rather than names so that |
|---|
| 4370 | * children with the same names but different parents aren't confused. |
|---|
| 4371 | */ |
|---|
| 4372 | if ( is_taxonomy_hierarchical( $taxonomy ) ) { |
|---|
| 4373 | $tags = array_unique( array_map( 'intval', $tags ) ); |
|---|
| 4374 | } |
|---|
| 4375 | |
|---|
| 4376 | return wp_set_object_terms( $post_id, $tags, $taxonomy, $append ); |
|---|
| 4377 | } |
|---|
| 4378 | |
|---|
| 4379 | /** |
|---|
| 4380 | * Set categories for a post. |
|---|
| 4381 | * |
|---|
| 4382 | * If the post categories parameter is not set, then the default category is |
|---|
| 4383 | * going used. |
|---|
| 4384 | * |
|---|
| 4385 | * @since 2.1.0 |
|---|
| 4386 | * |
|---|
| 4387 | * @param int $post_ID Optional. The Post ID. Does not default to the ID |
|---|
| 4388 | * of the global $post. Default 0. |
|---|
| 4389 | * @param array|int $post_categories Optional. List of category IDs, or the ID of a single category. |
|---|
| 4390 | * Default empty array. |
|---|
| 4391 | * @param bool $append If true, don't delete existing categories, just add on. |
|---|
| 4392 | * If false, replace the categories with the new categories. |
|---|
| 4393 | * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure. |
|---|
| 4394 | */ |
|---|
| 4395 | function wp_set_post_categories( $post_ID = 0, $post_categories = array(), $append = false ) { |
|---|
| 4396 | $post_ID = (int) $post_ID; |
|---|
| 4397 | $post_type = get_post_type( $post_ID ); |
|---|
| 4398 | $post_status = get_post_status( $post_ID ); |
|---|
| 4399 | // If $post_categories isn't already an array, make it one: |
|---|
| 4400 | $post_categories = (array) $post_categories; |
|---|
| 4401 | if ( empty( $post_categories ) ) { |
|---|
| 4402 | if ( 'post' == $post_type && 'auto-draft' != $post_status ) { |
|---|
| 4403 | $post_categories = array( get_option( 'default_category' ) ); |
|---|
| 4404 | $append = false; |
|---|
| 4405 | } else { |
|---|
| 4406 | $post_categories = array(); |
|---|
| 4407 | } |
|---|
| 4408 | } elseif ( 1 == count( $post_categories ) && '' == reset( $post_categories ) ) { |
|---|
| 4409 | return true; |
|---|
| 4410 | } |
|---|
| 4411 | |
|---|
| 4412 | return wp_set_post_terms( $post_ID, $post_categories, 'category', $append ); |
|---|
| 4413 | } |
|---|
| 4414 | |
|---|
| 4415 | /** |
|---|
| 4416 | * Fires actions related to the transitioning of a post's status. |
|---|
| 4417 | * |
|---|
| 4418 | * When a post is saved, the post status is "transitioned" from one status to another, |
|---|
| 4419 | * though this does not always mean the status has actually changed before and after |
|---|
| 4420 | * the save. This function fires a number of action hooks related to that transition: |
|---|
| 4421 | * the generic {@see 'transition_post_status'} action, as well as the dynamic hooks |
|---|
| 4422 | * {@see '$old_status_to_$new_status'} and {@see '$new_status_$post->post_type'}. Note |
|---|
| 4423 | * that the function does not transition the post object in the database. |
|---|
| 4424 | * |
|---|
| 4425 | * For instance: When publishing a post for the first time, the post status may transition |
|---|
| 4426 | * from 'draft' – or some other status – to 'publish'. However, if a post is already |
|---|
| 4427 | * published and is simply being updated, the "old" and "new" statuses may both be 'publish' |
|---|
| 4428 | * before and after the transition. |
|---|
| 4429 | * |
|---|
| 4430 | * @since 2.3.0 |
|---|
| 4431 | * |
|---|
| 4432 | * @param string $new_status Transition to this post status. |
|---|
| 4433 | * @param string $old_status Previous post status. |
|---|
| 4434 | * @param WP_Post $post Post data. |
|---|
| 4435 | */ |
|---|
| 4436 | function wp_transition_post_status( $new_status, $old_status, $post ) { |
|---|
| 4437 | /** |
|---|
| 4438 | * Fires when a post is transitioned from one status to another. |
|---|
| 4439 | * |
|---|
| 4440 | * @since 2.3.0 |
|---|
| 4441 | * |
|---|
| 4442 | * @param string $new_status New post status. |
|---|
| 4443 | * @param string $old_status Old post status. |
|---|
| 4444 | * @param WP_Post $post Post object. |
|---|
| 4445 | */ |
|---|
| 4446 | do_action( 'transition_post_status', $new_status, $old_status, $post ); |
|---|
| 4447 | |
|---|
| 4448 | /** |
|---|
| 4449 | * Fires when a post is transitioned from one status to another. |
|---|
| 4450 | * |
|---|
| 4451 | * The dynamic portions of the hook name, `$new_status` and `$old status`, |
|---|
| 4452 | * refer to the old and new post statuses, respectively. |
|---|
| 4453 | * |
|---|
| 4454 | * @since 2.3.0 |
|---|
| 4455 | * |
|---|
| 4456 | * @param WP_Post $post Post object. |
|---|
| 4457 | */ |
|---|
| 4458 | do_action( "{$old_status}_to_{$new_status}", $post ); |
|---|
| 4459 | |
|---|
| 4460 | /** |
|---|
| 4461 | * Fires when a post is transitioned from one status to another. |
|---|
| 4462 | * |
|---|
| 4463 | * The dynamic portions of the hook name, `$new_status` and `$post->post_type`, |
|---|
| 4464 | * refer to the new post status and post type, respectively. |
|---|
| 4465 | * |
|---|
| 4466 | * Please note: When this action is hooked using a particular post status (like |
|---|
| 4467 | * 'publish', as `publish_{$post->post_type}`), it will fire both when a post is |
|---|
| 4468 | * first transitioned to that status from something else, as well as upon |
|---|
| 4469 | * subsequent post updates (old and new status are both the same). |
|---|
| 4470 | * |
|---|
| 4471 | * Therefore, if you are looking to only fire a callback when a post is first |
|---|
| 4472 | * transitioned to a status, use the {@see 'transition_post_status'} hook instead. |
|---|
| 4473 | * |
|---|
| 4474 | * @since 2.3.0 |
|---|
| 4475 | * |
|---|
| 4476 | * @param int $post_id Post ID. |
|---|
| 4477 | * @param WP_Post $post Post object. |
|---|
| 4478 | */ |
|---|
| 4479 | do_action( "{$new_status}_{$post->post_type}", $post->ID, $post ); |
|---|
| 4480 | } |
|---|
| 4481 | |
|---|
| 4482 | // |
|---|
| 4483 | // Comment, trackback, and pingback functions. |
|---|
| 4484 | // |
|---|
| 4485 | |
|---|
| 4486 | /** |
|---|
| 4487 | * Add a URL to those already pinged. |
|---|
| 4488 | * |
|---|
| 4489 | * @since 1.5.0 |
|---|
| 4490 | * @since 4.7.0 `$post_id` can be a WP_Post object. |
|---|
| 4491 | * @since 4.7.0 `$uri` can be an array of URIs. |
|---|
| 4492 | * |
|---|
| 4493 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4494 | * |
|---|
| 4495 | * @param int|WP_Post $post_id Post object or ID. |
|---|
| 4496 | * @param string|array $uri Ping URI or array of URIs. |
|---|
| 4497 | * @return int|false How many rows were updated. |
|---|
| 4498 | */ |
|---|
| 4499 | function add_ping( $post_id, $uri ) { |
|---|
| 4500 | global $wpdb; |
|---|
| 4501 | |
|---|
| 4502 | $post = get_post( $post_id ); |
|---|
| 4503 | if ( ! $post ) { |
|---|
| 4504 | return false; |
|---|
| 4505 | } |
|---|
| 4506 | |
|---|
| 4507 | $pung = trim( $post->pinged ); |
|---|
| 4508 | $pung = preg_split( '/\s/', $pung ); |
|---|
| 4509 | |
|---|
| 4510 | if ( is_array( $uri ) ) { |
|---|
| 4511 | $pung = array_merge( $pung, $uri ); |
|---|
| 4512 | } else { |
|---|
| 4513 | $pung[] = $uri; |
|---|
| 4514 | } |
|---|
| 4515 | $new = implode( "\n", $pung ); |
|---|
| 4516 | |
|---|
| 4517 | /** |
|---|
| 4518 | * Filters the new ping URL to add for the given post. |
|---|
| 4519 | * |
|---|
| 4520 | * @since 2.0.0 |
|---|
| 4521 | * |
|---|
| 4522 | * @param string $new New ping URL to add. |
|---|
| 4523 | */ |
|---|
| 4524 | $new = apply_filters( 'add_ping', $new ); |
|---|
| 4525 | |
|---|
| 4526 | $return = $wpdb->update( $wpdb->posts, array( 'pinged' => $new ), array( 'ID' => $post->ID ) ); |
|---|
| 4527 | clean_post_cache( $post->ID ); |
|---|
| 4528 | return $return; |
|---|
| 4529 | } |
|---|
| 4530 | |
|---|
| 4531 | /** |
|---|
| 4532 | * Retrieve enclosures already enclosed for a post. |
|---|
| 4533 | * |
|---|
| 4534 | * @since 1.5.0 |
|---|
| 4535 | * |
|---|
| 4536 | * @param int $post_id Post ID. |
|---|
| 4537 | * @return array List of enclosures. |
|---|
| 4538 | */ |
|---|
| 4539 | function get_enclosed( $post_id ) { |
|---|
| 4540 | $custom_fields = get_post_custom( $post_id ); |
|---|
| 4541 | $pung = array(); |
|---|
| 4542 | if ( ! is_array( $custom_fields ) ) { |
|---|
| 4543 | return $pung; |
|---|
| 4544 | } |
|---|
| 4545 | |
|---|
| 4546 | foreach ( $custom_fields as $key => $val ) { |
|---|
| 4547 | if ( 'enclosure' != $key || ! is_array( $val ) ) { |
|---|
| 4548 | continue; |
|---|
| 4549 | } |
|---|
| 4550 | foreach ( $val as $enc ) { |
|---|
| 4551 | $enclosure = explode( "\n", $enc ); |
|---|
| 4552 | $pung[] = trim( $enclosure[0] ); |
|---|
| 4553 | } |
|---|
| 4554 | } |
|---|
| 4555 | |
|---|
| 4556 | /** |
|---|
| 4557 | * Filters the list of enclosures already enclosed for the given post. |
|---|
| 4558 | * |
|---|
| 4559 | * @since 2.0.0 |
|---|
| 4560 | * |
|---|
| 4561 | * @param array $pung Array of enclosures for the given post. |
|---|
| 4562 | * @param int $post_id Post ID. |
|---|
| 4563 | */ |
|---|
| 4564 | return apply_filters( 'get_enclosed', $pung, $post_id ); |
|---|
| 4565 | } |
|---|
| 4566 | |
|---|
| 4567 | /** |
|---|
| 4568 | * Retrieve URLs already pinged for a post. |
|---|
| 4569 | * |
|---|
| 4570 | * @since 1.5.0 |
|---|
| 4571 | * |
|---|
| 4572 | * @since 4.7.0 `$post_id` can be a WP_Post object. |
|---|
| 4573 | * |
|---|
| 4574 | * @param int|WP_Post $post_id Post ID or object. |
|---|
| 4575 | * @return bool|string[] Array of URLs already pinged for the given post, false if the post is not found. |
|---|
| 4576 | */ |
|---|
| 4577 | function get_pung( $post_id ) { |
|---|
| 4578 | $post = get_post( $post_id ); |
|---|
| 4579 | if ( ! $post ) { |
|---|
| 4580 | return false; |
|---|
| 4581 | } |
|---|
| 4582 | |
|---|
| 4583 | $pung = trim( $post->pinged ); |
|---|
| 4584 | $pung = preg_split( '/\s/', $pung ); |
|---|
| 4585 | |
|---|
| 4586 | /** |
|---|
| 4587 | * Filters the list of already-pinged URLs for the given post. |
|---|
| 4588 | * |
|---|
| 4589 | * @since 2.0.0 |
|---|
| 4590 | * |
|---|
| 4591 | * @param string[] $pung Array of URLs already pinged for the given post. |
|---|
| 4592 | */ |
|---|
| 4593 | return apply_filters( 'get_pung', $pung ); |
|---|
| 4594 | } |
|---|
| 4595 | |
|---|
| 4596 | /** |
|---|
| 4597 | * Retrieve URLs that need to be pinged. |
|---|
| 4598 | * |
|---|
| 4599 | * @since 1.5.0 |
|---|
| 4600 | * @since 4.7.0 `$post_id` can be a WP_Post object. |
|---|
| 4601 | * |
|---|
| 4602 | * @param int|WP_Post $post_id Post Object or ID |
|---|
| 4603 | * @return array |
|---|
| 4604 | */ |
|---|
| 4605 | function get_to_ping( $post_id ) { |
|---|
| 4606 | $post = get_post( $post_id ); |
|---|
| 4607 | |
|---|
| 4608 | if ( ! $post ) { |
|---|
| 4609 | return false; |
|---|
| 4610 | } |
|---|
| 4611 | |
|---|
| 4612 | $to_ping = sanitize_trackback_urls( $post->to_ping ); |
|---|
| 4613 | $to_ping = preg_split( '/\s/', $to_ping, -1, PREG_SPLIT_NO_EMPTY ); |
|---|
| 4614 | |
|---|
| 4615 | /** |
|---|
| 4616 | * Filters the list of URLs yet to ping for the given post. |
|---|
| 4617 | * |
|---|
| 4618 | * @since 2.0.0 |
|---|
| 4619 | * |
|---|
| 4620 | * @param array $to_ping List of URLs yet to ping. |
|---|
| 4621 | */ |
|---|
| 4622 | return apply_filters( 'get_to_ping', $to_ping ); |
|---|
| 4623 | } |
|---|
| 4624 | |
|---|
| 4625 | /** |
|---|
| 4626 | * Do trackbacks for a list of URLs. |
|---|
| 4627 | * |
|---|
| 4628 | * @since 1.0.0 |
|---|
| 4629 | * |
|---|
| 4630 | * @param string $tb_list Comma separated list of URLs. |
|---|
| 4631 | * @param int $post_id Post ID. |
|---|
| 4632 | */ |
|---|
| 4633 | function trackback_url_list( $tb_list, $post_id ) { |
|---|
| 4634 | if ( ! empty( $tb_list ) ) { |
|---|
| 4635 | // Get post data. |
|---|
| 4636 | $postdata = get_post( $post_id, ARRAY_A ); |
|---|
| 4637 | |
|---|
| 4638 | // Form an excerpt. |
|---|
| 4639 | $excerpt = strip_tags( $postdata['post_excerpt'] ? $postdata['post_excerpt'] : $postdata['post_content'] ); |
|---|
| 4640 | |
|---|
| 4641 | if ( strlen( $excerpt ) > 255 ) { |
|---|
| 4642 | $excerpt = substr( $excerpt, 0, 252 ) . '…'; |
|---|
| 4643 | } |
|---|
| 4644 | |
|---|
| 4645 | $trackback_urls = explode( ',', $tb_list ); |
|---|
| 4646 | foreach ( (array) $trackback_urls as $tb_url ) { |
|---|
| 4647 | $tb_url = trim( $tb_url ); |
|---|
| 4648 | trackback( $tb_url, wp_unslash( $postdata['post_title'] ), $excerpt, $post_id ); |
|---|
| 4649 | } |
|---|
| 4650 | } |
|---|
| 4651 | } |
|---|
| 4652 | |
|---|
| 4653 | // |
|---|
| 4654 | // Page functions |
|---|
| 4655 | // |
|---|
| 4656 | |
|---|
| 4657 | /** |
|---|
| 4658 | * Get a list of page IDs. |
|---|
| 4659 | * |
|---|
| 4660 | * @since 2.0.0 |
|---|
| 4661 | * |
|---|
| 4662 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4663 | * |
|---|
| 4664 | * @return array List of page IDs. |
|---|
| 4665 | */ |
|---|
| 4666 | function get_all_page_ids() { |
|---|
| 4667 | global $wpdb; |
|---|
| 4668 | |
|---|
| 4669 | $page_ids = wp_cache_get( 'all_page_ids', 'posts' ); |
|---|
| 4670 | if ( ! is_array( $page_ids ) ) { |
|---|
| 4671 | $page_ids = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_type = 'page'" ); |
|---|
| 4672 | wp_cache_add( 'all_page_ids', $page_ids, 'posts' ); |
|---|
| 4673 | } |
|---|
| 4674 | |
|---|
| 4675 | return $page_ids; |
|---|
| 4676 | } |
|---|
| 4677 | |
|---|
| 4678 | /** |
|---|
| 4679 | * Retrieves page data given a page ID or page object. |
|---|
| 4680 | * |
|---|
| 4681 | * Use get_post() instead of get_page(). |
|---|
| 4682 | * |
|---|
| 4683 | * @since 1.5.1 |
|---|
| 4684 | * @deprecated 3.5.0 Use get_post() |
|---|
| 4685 | * |
|---|
| 4686 | * @param mixed $page Page object or page ID. Passed by reference. |
|---|
| 4687 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
|---|
| 4688 | * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
|---|
| 4689 | * @param string $filter Optional. How the return value should be filtered. Accepts 'raw', |
|---|
| 4690 | * 'edit', 'db', 'display'. Default 'raw'. |
|---|
| 4691 | * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
|---|
| 4692 | */ |
|---|
| 4693 | function get_page( $page, $output = OBJECT, $filter = 'raw' ) { |
|---|
| 4694 | return get_post( $page, $output, $filter ); |
|---|
| 4695 | } |
|---|
| 4696 | |
|---|
| 4697 | /** |
|---|
| 4698 | * Retrieves a page given its path. |
|---|
| 4699 | * |
|---|
| 4700 | * @since 2.1.0 |
|---|
| 4701 | * |
|---|
| 4702 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4703 | * |
|---|
| 4704 | * @param string $page_path Page path. |
|---|
| 4705 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
|---|
| 4706 | * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
|---|
| 4707 | * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. |
|---|
| 4708 | * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
|---|
| 4709 | */ |
|---|
| 4710 | function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { |
|---|
| 4711 | global $wpdb; |
|---|
| 4712 | |
|---|
| 4713 | $last_changed = wp_cache_get_last_changed( 'posts' ); |
|---|
| 4714 | |
|---|
| 4715 | $hash = md5( $page_path . serialize( $post_type ) ); |
|---|
| 4716 | $cache_key = "get_page_by_path:$hash:$last_changed"; |
|---|
| 4717 | $cached = wp_cache_get( $cache_key, 'posts' ); |
|---|
| 4718 | if ( false !== $cached ) { |
|---|
| 4719 | // Special case: '0' is a bad `$page_path`. |
|---|
| 4720 | if ( '0' === $cached || 0 === $cached ) { |
|---|
| 4721 | return; |
|---|
| 4722 | } else { |
|---|
| 4723 | return get_post( $cached, $output ); |
|---|
| 4724 | } |
|---|
| 4725 | } |
|---|
| 4726 | |
|---|
| 4727 | $page_path = rawurlencode( urldecode( $page_path ) ); |
|---|
| 4728 | $page_path = str_replace( '%2F', '/', $page_path ); |
|---|
| 4729 | $page_path = str_replace( '%20', ' ', $page_path ); |
|---|
| 4730 | $parts = explode( '/', trim( $page_path, '/' ) ); |
|---|
| 4731 | $parts = array_map( 'sanitize_title_for_query', $parts ); |
|---|
| 4732 | $escaped_parts = esc_sql( $parts ); |
|---|
| 4733 | |
|---|
| 4734 | $in_string = "'" . implode( "','", $escaped_parts ) . "'"; |
|---|
| 4735 | |
|---|
| 4736 | if ( is_array( $post_type ) ) { |
|---|
| 4737 | $post_types = $post_type; |
|---|
| 4738 | } else { |
|---|
| 4739 | $post_types = array( $post_type, 'attachment' ); |
|---|
| 4740 | } |
|---|
| 4741 | |
|---|
| 4742 | $post_types = esc_sql( $post_types ); |
|---|
| 4743 | $post_type_in_string = "'" . implode( "','", $post_types ) . "'"; |
|---|
| 4744 | $sql = " |
|---|
| 4745 | SELECT ID, post_name, post_parent, post_type |
|---|
| 4746 | FROM $wpdb->posts |
|---|
| 4747 | WHERE post_name IN ($in_string) |
|---|
| 4748 | AND post_type IN ($post_type_in_string) |
|---|
| 4749 | "; |
|---|
| 4750 | |
|---|
| 4751 | $pages = $wpdb->get_results( $sql, OBJECT_K ); |
|---|
| 4752 | |
|---|
| 4753 | $revparts = array_reverse( $parts ); |
|---|
| 4754 | |
|---|
| 4755 | $foundid = 0; |
|---|
| 4756 | foreach ( (array) $pages as $page ) { |
|---|
| 4757 | if ( $page->post_name == $revparts[0] ) { |
|---|
| 4758 | $count = 0; |
|---|
| 4759 | $p = $page; |
|---|
| 4760 | |
|---|
| 4761 | /* |
|---|
| 4762 | * Loop through the given path parts from right to left, |
|---|
| 4763 | * ensuring each matches the post ancestry. |
|---|
| 4764 | */ |
|---|
| 4765 | while ( $p->post_parent != 0 && isset( $pages[ $p->post_parent ] ) ) { |
|---|
| 4766 | $count++; |
|---|
| 4767 | $parent = $pages[ $p->post_parent ]; |
|---|
| 4768 | if ( ! isset( $revparts[ $count ] ) || $parent->post_name != $revparts[ $count ] ) { |
|---|
| 4769 | break; |
|---|
| 4770 | } |
|---|
| 4771 | $p = $parent; |
|---|
| 4772 | } |
|---|
| 4773 | |
|---|
| 4774 | if ( $p->post_parent == 0 && $count + 1 == count( $revparts ) && $p->post_name == $revparts[ $count ] ) { |
|---|
| 4775 | $foundid = $page->ID; |
|---|
| 4776 | if ( $page->post_type == $post_type ) { |
|---|
| 4777 | break; |
|---|
| 4778 | } |
|---|
| 4779 | } |
|---|
| 4780 | } |
|---|
| 4781 | } |
|---|
| 4782 | |
|---|
| 4783 | // We cache misses as well as hits. |
|---|
| 4784 | wp_cache_set( $cache_key, $foundid, 'posts' ); |
|---|
| 4785 | |
|---|
| 4786 | if ( $foundid ) { |
|---|
| 4787 | return get_post( $foundid, $output ); |
|---|
| 4788 | } |
|---|
| 4789 | } |
|---|
| 4790 | |
|---|
| 4791 | /** |
|---|
| 4792 | * Retrieve a page given its title. |
|---|
| 4793 | * |
|---|
| 4794 | * @since 2.1.0 |
|---|
| 4795 | * |
|---|
| 4796 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4797 | * |
|---|
| 4798 | * @param string $page_title Page title |
|---|
| 4799 | * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which correspond to |
|---|
| 4800 | * a WP_Post object, an associative array, or a numeric array, respectively. Default OBJECT. |
|---|
| 4801 | * @param string|array $post_type Optional. Post type or array of post types. Default 'page'. |
|---|
| 4802 | * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. |
|---|
| 4803 | */ |
|---|
| 4804 | function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { |
|---|
| 4805 | global $wpdb; |
|---|
| 4806 | |
|---|
| 4807 | if ( is_array( $post_type ) ) { |
|---|
| 4808 | $post_type = esc_sql( $post_type ); |
|---|
| 4809 | $post_type_in_string = "'" . implode( "','", $post_type ) . "'"; |
|---|
| 4810 | $sql = $wpdb->prepare( |
|---|
| 4811 | " |
|---|
| 4812 | SELECT ID |
|---|
| 4813 | FROM $wpdb->posts |
|---|
| 4814 | WHERE post_title = %s |
|---|
| 4815 | AND post_type IN ($post_type_in_string) |
|---|
| 4816 | ", |
|---|
| 4817 | $page_title |
|---|
| 4818 | ); |
|---|
| 4819 | } else { |
|---|
| 4820 | $sql = $wpdb->prepare( |
|---|
| 4821 | " |
|---|
| 4822 | SELECT ID |
|---|
| 4823 | FROM $wpdb->posts |
|---|
| 4824 | WHERE post_title = %s |
|---|
| 4825 | AND post_type = %s |
|---|
| 4826 | ", |
|---|
| 4827 | $page_title, |
|---|
| 4828 | $post_type |
|---|
| 4829 | ); |
|---|
| 4830 | } |
|---|
| 4831 | |
|---|
| 4832 | $page = $wpdb->get_var( $sql ); |
|---|
| 4833 | |
|---|
| 4834 | if ( $page ) { |
|---|
| 4835 | return get_post( $page, $output ); |
|---|
| 4836 | } |
|---|
| 4837 | } |
|---|
| 4838 | |
|---|
| 4839 | /** |
|---|
| 4840 | * Identify descendants of a given page ID in a list of page objects. |
|---|
| 4841 | * |
|---|
| 4842 | * Descendants are identified from the `$pages` array passed to the function. No database queries are performed. |
|---|
| 4843 | * |
|---|
| 4844 | * @since 1.5.1 |
|---|
| 4845 | * |
|---|
| 4846 | * @param int $page_id Page ID. |
|---|
| 4847 | * @param array $pages List of page objects from which descendants should be identified. |
|---|
| 4848 | * @return array List of page children. |
|---|
| 4849 | */ |
|---|
| 4850 | function get_page_children( $page_id, $pages ) { |
|---|
| 4851 | // Build a hash of ID -> children. |
|---|
| 4852 | $children = array(); |
|---|
| 4853 | foreach ( (array) $pages as $page ) { |
|---|
| 4854 | $children[ intval( $page->post_parent ) ][] = $page; |
|---|
| 4855 | } |
|---|
| 4856 | |
|---|
| 4857 | $page_list = array(); |
|---|
| 4858 | |
|---|
| 4859 | // Start the search by looking at immediate children. |
|---|
| 4860 | if ( isset( $children[ $page_id ] ) ) { |
|---|
| 4861 | // Always start at the end of the stack in order to preserve original `$pages` order. |
|---|
| 4862 | $to_look = array_reverse( $children[ $page_id ] ); |
|---|
| 4863 | |
|---|
| 4864 | while ( $to_look ) { |
|---|
| 4865 | $p = array_pop( $to_look ); |
|---|
| 4866 | $page_list[] = $p; |
|---|
| 4867 | if ( isset( $children[ $p->ID ] ) ) { |
|---|
| 4868 | foreach ( array_reverse( $children[ $p->ID ] ) as $child ) { |
|---|
| 4869 | // Append to the `$to_look` stack to descend the tree. |
|---|
| 4870 | $to_look[] = $child; |
|---|
| 4871 | } |
|---|
| 4872 | } |
|---|
| 4873 | } |
|---|
| 4874 | } |
|---|
| 4875 | |
|---|
| 4876 | return $page_list; |
|---|
| 4877 | } |
|---|
| 4878 | |
|---|
| 4879 | /** |
|---|
| 4880 | * Order the pages with children under parents in a flat list. |
|---|
| 4881 | * |
|---|
| 4882 | * It uses auxiliary structure to hold parent-children relationships and |
|---|
| 4883 | * runs in O(N) complexity |
|---|
| 4884 | * |
|---|
| 4885 | * @since 2.0.0 |
|---|
| 4886 | * |
|---|
| 4887 | * @param array $pages Posts array (passed by reference). |
|---|
| 4888 | * @param int $page_id Optional. Parent page ID. Default 0. |
|---|
| 4889 | * @return array A list arranged by hierarchy. Children immediately follow their parents. |
|---|
| 4890 | */ |
|---|
| 4891 | function get_page_hierarchy( &$pages, $page_id = 0 ) { |
|---|
| 4892 | if ( empty( $pages ) ) { |
|---|
| 4893 | return array(); |
|---|
| 4894 | } |
|---|
| 4895 | |
|---|
| 4896 | $children = array(); |
|---|
| 4897 | foreach ( (array) $pages as $p ) { |
|---|
| 4898 | $parent_id = intval( $p->post_parent ); |
|---|
| 4899 | $children[ $parent_id ][] = $p; |
|---|
| 4900 | } |
|---|
| 4901 | |
|---|
| 4902 | $result = array(); |
|---|
| 4903 | _page_traverse_name( $page_id, $children, $result ); |
|---|
| 4904 | |
|---|
| 4905 | return $result; |
|---|
| 4906 | } |
|---|
| 4907 | |
|---|
| 4908 | /** |
|---|
| 4909 | * Traverse and return all the nested children post names of a root page. |
|---|
| 4910 | * |
|---|
| 4911 | * $children contains parent-children relations |
|---|
| 4912 | * |
|---|
| 4913 | * @since 2.9.0 |
|---|
| 4914 | * @access private |
|---|
| 4915 | * |
|---|
| 4916 | * @see _page_traverse_name() |
|---|
| 4917 | * |
|---|
| 4918 | * @param int $page_id Page ID. |
|---|
| 4919 | * @param array $children Parent-children relations (passed by reference). |
|---|
| 4920 | * @param array $result Result (passed by reference). |
|---|
| 4921 | */ |
|---|
| 4922 | function _page_traverse_name( $page_id, &$children, &$result ) { |
|---|
| 4923 | if ( isset( $children[ $page_id ] ) ) { |
|---|
| 4924 | foreach ( (array) $children[ $page_id ] as $child ) { |
|---|
| 4925 | $result[ $child->ID ] = $child->post_name; |
|---|
| 4926 | _page_traverse_name( $child->ID, $children, $result ); |
|---|
| 4927 | } |
|---|
| 4928 | } |
|---|
| 4929 | } |
|---|
| 4930 | |
|---|
| 4931 | /** |
|---|
| 4932 | * Build the URI path for a page. |
|---|
| 4933 | * |
|---|
| 4934 | * Sub pages will be in the "directory" under the parent page post name. |
|---|
| 4935 | * |
|---|
| 4936 | * @since 1.5.0 |
|---|
| 4937 | * @since 4.6.0 Converted the `$page` parameter to optional. |
|---|
| 4938 | * |
|---|
| 4939 | * @param WP_Post|object|int $page Optional. Page ID or WP_Post object. Default is global $post. |
|---|
| 4940 | * @return string|false Page URI, false on error. |
|---|
| 4941 | */ |
|---|
| 4942 | function get_page_uri( $page = 0 ) { |
|---|
| 4943 | if ( ! $page instanceof WP_Post ) { |
|---|
| 4944 | $page = get_post( $page ); |
|---|
| 4945 | } |
|---|
| 4946 | |
|---|
| 4947 | if ( ! $page ) { |
|---|
| 4948 | return false; |
|---|
| 4949 | } |
|---|
| 4950 | |
|---|
| 4951 | $uri = $page->post_name; |
|---|
| 4952 | |
|---|
| 4953 | foreach ( $page->ancestors as $parent ) { |
|---|
| 4954 | $parent = get_post( $parent ); |
|---|
| 4955 | if ( $parent && $parent->post_name ) { |
|---|
| 4956 | $uri = $parent->post_name . '/' . $uri; |
|---|
| 4957 | } |
|---|
| 4958 | } |
|---|
| 4959 | |
|---|
| 4960 | /** |
|---|
| 4961 | * Filters the URI for a page. |
|---|
| 4962 | * |
|---|
| 4963 | * @since 4.4.0 |
|---|
| 4964 | * |
|---|
| 4965 | * @param string $uri Page URI. |
|---|
| 4966 | * @param WP_Post $page Page object. |
|---|
| 4967 | */ |
|---|
| 4968 | return apply_filters( 'get_page_uri', $uri, $page ); |
|---|
| 4969 | } |
|---|
| 4970 | |
|---|
| 4971 | /** |
|---|
| 4972 | * Retrieve a list of pages (or hierarchical post type items). |
|---|
| 4973 | * |
|---|
| 4974 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 4975 | * |
|---|
| 4976 | * @since 1.5.0 |
|---|
| 4977 | * |
|---|
| 4978 | * @param array|string $args { |
|---|
| 4979 | * Optional. Array or string of arguments to retrieve pages. |
|---|
| 4980 | * |
|---|
| 4981 | * @type int $child_of Page ID to return child and grandchild pages of. Note: The value |
|---|
| 4982 | * of `$hierarchical` has no bearing on whether `$child_of` returns |
|---|
| 4983 | * hierarchical results. Default 0, or no restriction. |
|---|
| 4984 | * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. |
|---|
| 4985 | * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', |
|---|
| 4986 | * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', |
|---|
| 4987 | * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. |
|---|
| 4988 | * 'post_' can be omitted for any values that start with it. |
|---|
| 4989 | * Default 'post_title'. |
|---|
| 4990 | * @type bool $hierarchical Whether to return pages hierarchically. If false in conjunction with |
|---|
| 4991 | * `$child_of` also being false, both arguments will be disregarded. |
|---|
| 4992 | * Default true. |
|---|
| 4993 | * @type array $exclude Array of page IDs to exclude. Default empty array. |
|---|
| 4994 | * @type array $include Array of page IDs to include. Cannot be used with `$child_of`, |
|---|
| 4995 | * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. |
|---|
| 4996 | * Default empty array. |
|---|
| 4997 | * @type string $meta_key Only include pages with this meta key. Default empty. |
|---|
| 4998 | * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. |
|---|
| 4999 | * Default empty. |
|---|
| 5000 | * @type string $authors A comma-separated list of author IDs. Default empty. |
|---|
| 5001 | * @type int $parent Page ID to return direct children of. Default -1, or no restriction. |
|---|
| 5002 | * @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. |
|---|
| 5003 | * Default empty array. |
|---|
| 5004 | * @type int $number The number of pages to return. Default 0, or all pages. |
|---|
| 5005 | * @type int $offset The number of pages to skip before returning. Requires `$number`. |
|---|
| 5006 | * Default 0. |
|---|
| 5007 | * @type string $post_type The post type to query. Default 'page'. |
|---|
| 5008 | * @type string|array $post_status A comma-separated list or array of post statuses to include. |
|---|
| 5009 | * Default 'publish'. |
|---|
| 5010 | * } |
|---|
| 5011 | * @return array|false List of pages matching defaults or `$args`. |
|---|
| 5012 | */ |
|---|
| 5013 | function get_pages( $args = array() ) { |
|---|
| 5014 | global $wpdb; |
|---|
| 5015 | |
|---|
| 5016 | $defaults = array( |
|---|
| 5017 | 'child_of' => 0, |
|---|
| 5018 | 'sort_order' => 'ASC', |
|---|
| 5019 | 'sort_column' => 'post_title', |
|---|
| 5020 | 'hierarchical' => 1, |
|---|
| 5021 | 'exclude' => array(), |
|---|
| 5022 | 'include' => array(), |
|---|
| 5023 | 'meta_key' => '', |
|---|
| 5024 | 'meta_value' => '', |
|---|
| 5025 | 'authors' => '', |
|---|
| 5026 | 'parent' => -1, |
|---|
| 5027 | 'exclude_tree' => array(), |
|---|
| 5028 | 'number' => '', |
|---|
| 5029 | 'offset' => 0, |
|---|
| 5030 | 'post_type' => 'page', |
|---|
| 5031 | 'post_status' => 'publish', |
|---|
| 5032 | ); |
|---|
| 5033 | |
|---|
| 5034 | $r = wp_parse_args( $args, $defaults ); |
|---|
| 5035 | |
|---|
| 5036 | $number = (int) $r['number']; |
|---|
| 5037 | $offset = (int) $r['offset']; |
|---|
| 5038 | $child_of = (int) $r['child_of']; |
|---|
| 5039 | $hierarchical = $r['hierarchical']; |
|---|
| 5040 | $exclude = $r['exclude']; |
|---|
| 5041 | $meta_key = $r['meta_key']; |
|---|
| 5042 | $meta_value = $r['meta_value']; |
|---|
| 5043 | $parent = $r['parent']; |
|---|
| 5044 | $post_status = $r['post_status']; |
|---|
| 5045 | |
|---|
| 5046 | // Make sure the post type is hierarchical. |
|---|
| 5047 | $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); |
|---|
| 5048 | if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) { |
|---|
| 5049 | return false; |
|---|
| 5050 | } |
|---|
| 5051 | |
|---|
| 5052 | if ( $parent > 0 && ! $child_of ) { |
|---|
| 5053 | $hierarchical = false; |
|---|
| 5054 | } |
|---|
| 5055 | |
|---|
| 5056 | // Make sure we have a valid post status. |
|---|
| 5057 | if ( ! is_array( $post_status ) ) { |
|---|
| 5058 | $post_status = explode( ',', $post_status ); |
|---|
| 5059 | } |
|---|
| 5060 | if ( array_diff( $post_status, get_post_stati() ) ) { |
|---|
| 5061 | return false; |
|---|
| 5062 | } |
|---|
| 5063 | |
|---|
| 5064 | // $args can be whatever, only use the args defined in defaults to compute the key. |
|---|
| 5065 | $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); |
|---|
| 5066 | $last_changed = wp_cache_get_last_changed( 'posts' ); |
|---|
| 5067 | |
|---|
| 5068 | $cache_key = "get_pages:$key:$last_changed"; |
|---|
| 5069 | $cache = wp_cache_get( $cache_key, 'posts' ); |
|---|
| 5070 | if ( false !== $cache ) { |
|---|
| 5071 | // Convert to WP_Post instances. |
|---|
| 5072 | $pages = array_map( 'get_post', $cache ); |
|---|
| 5073 | /** This filter is documented in wp-includes/post.php */ |
|---|
| 5074 | $pages = apply_filters( 'get_pages', $pages, $r ); |
|---|
| 5075 | return $pages; |
|---|
| 5076 | } |
|---|
| 5077 | |
|---|
| 5078 | $inclusions = ''; |
|---|
| 5079 | if ( ! empty( $r['include'] ) ) { |
|---|
| 5080 | $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include |
|---|
| 5081 | $parent = -1; |
|---|
| 5082 | $exclude = ''; |
|---|
| 5083 | $meta_key = ''; |
|---|
| 5084 | $meta_value = ''; |
|---|
| 5085 | $hierarchical = false; |
|---|
| 5086 | $incpages = wp_parse_id_list( $r['include'] ); |
|---|
| 5087 | if ( ! empty( $incpages ) ) { |
|---|
| 5088 | $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')'; |
|---|
| 5089 | } |
|---|
| 5090 | } |
|---|
| 5091 | |
|---|
| 5092 | $exclusions = ''; |
|---|
| 5093 | if ( ! empty( $exclude ) ) { |
|---|
| 5094 | $expages = wp_parse_id_list( $exclude ); |
|---|
| 5095 | if ( ! empty( $expages ) ) { |
|---|
| 5096 | $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')'; |
|---|
| 5097 | } |
|---|
| 5098 | } |
|---|
| 5099 | |
|---|
| 5100 | $author_query = ''; |
|---|
| 5101 | if ( ! empty( $r['authors'] ) ) { |
|---|
| 5102 | $post_authors = wp_parse_list( $r['authors'] ); |
|---|
| 5103 | |
|---|
| 5104 | if ( ! empty( $post_authors ) ) { |
|---|
| 5105 | foreach ( $post_authors as $post_author ) { |
|---|
| 5106 | //Do we have an author id or an author login? |
|---|
| 5107 | if ( 0 == intval( $post_author ) ) { |
|---|
| 5108 | $post_author = get_user_by( 'login', $post_author ); |
|---|
| 5109 | if ( empty( $post_author ) ) { |
|---|
| 5110 | continue; |
|---|
| 5111 | } |
|---|
| 5112 | if ( empty( $post_author->ID ) ) { |
|---|
| 5113 | continue; |
|---|
| 5114 | } |
|---|
| 5115 | $post_author = $post_author->ID; |
|---|
| 5116 | } |
|---|
| 5117 | |
|---|
| 5118 | if ( '' == $author_query ) { |
|---|
| 5119 | $author_query = $wpdb->prepare( ' post_author = %d ', $post_author ); |
|---|
| 5120 | } else { |
|---|
| 5121 | $author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author ); |
|---|
| 5122 | } |
|---|
| 5123 | } |
|---|
| 5124 | if ( '' != $author_query ) { |
|---|
| 5125 | $author_query = " AND ($author_query)"; |
|---|
| 5126 | } |
|---|
| 5127 | } |
|---|
| 5128 | } |
|---|
| 5129 | |
|---|
| 5130 | $join = ''; |
|---|
| 5131 | $where = "$exclusions $inclusions "; |
|---|
| 5132 | if ( '' !== $meta_key || '' !== $meta_value ) { |
|---|
| 5133 | $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
|---|
| 5134 | |
|---|
| 5135 | // meta_key and meta_value might be slashed |
|---|
| 5136 | $meta_key = wp_unslash( $meta_key ); |
|---|
| 5137 | $meta_value = wp_unslash( $meta_value ); |
|---|
| 5138 | if ( '' !== $meta_key ) { |
|---|
| 5139 | $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key ); |
|---|
| 5140 | } |
|---|
| 5141 | if ( '' !== $meta_value ) { |
|---|
| 5142 | $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value ); |
|---|
| 5143 | } |
|---|
| 5144 | } |
|---|
| 5145 | |
|---|
| 5146 | if ( is_array( $parent ) ) { |
|---|
| 5147 | $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) ); |
|---|
| 5148 | if ( ! empty( $post_parent__in ) ) { |
|---|
| 5149 | $where .= " AND post_parent IN ($post_parent__in)"; |
|---|
| 5150 | } |
|---|
| 5151 | } elseif ( $parent >= 0 ) { |
|---|
| 5152 | $where .= $wpdb->prepare( ' AND post_parent = %d ', $parent ); |
|---|
| 5153 | } |
|---|
| 5154 | |
|---|
| 5155 | if ( 1 == count( $post_status ) ) { |
|---|
| 5156 | $where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $r['post_type'], reset( $post_status ) ); |
|---|
| 5157 | } else { |
|---|
| 5158 | $post_status = implode( "', '", $post_status ); |
|---|
| 5159 | $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $r['post_type'] ); |
|---|
| 5160 | } |
|---|
| 5161 | |
|---|
| 5162 | $orderby_array = array(); |
|---|
| 5163 | $allowed_keys = array( |
|---|
| 5164 | 'author', |
|---|
| 5165 | 'post_author', |
|---|
| 5166 | 'date', |
|---|
| 5167 | 'post_date', |
|---|
| 5168 | 'title', |
|---|
| 5169 | 'post_title', |
|---|
| 5170 | 'name', |
|---|
| 5171 | 'post_name', |
|---|
| 5172 | 'modified', |
|---|
| 5173 | 'post_modified', |
|---|
| 5174 | 'modified_gmt', |
|---|
| 5175 | 'post_modified_gmt', |
|---|
| 5176 | 'menu_order', |
|---|
| 5177 | 'parent', |
|---|
| 5178 | 'post_parent', |
|---|
| 5179 | 'ID', |
|---|
| 5180 | 'rand', |
|---|
| 5181 | 'comment_count', |
|---|
| 5182 | ); |
|---|
| 5183 | |
|---|
| 5184 | foreach ( explode( ',', $r['sort_column'] ) as $orderby ) { |
|---|
| 5185 | $orderby = trim( $orderby ); |
|---|
| 5186 | if ( ! in_array( $orderby, $allowed_keys ) ) { |
|---|
| 5187 | continue; |
|---|
| 5188 | } |
|---|
| 5189 | |
|---|
| 5190 | switch ( $orderby ) { |
|---|
| 5191 | case 'menu_order': |
|---|
| 5192 | break; |
|---|
| 5193 | case 'ID': |
|---|
| 5194 | $orderby = "$wpdb->posts.ID"; |
|---|
| 5195 | break; |
|---|
| 5196 | case 'rand': |
|---|
| 5197 | $orderby = 'RAND()'; |
|---|
| 5198 | break; |
|---|
| 5199 | case 'comment_count': |
|---|
| 5200 | $orderby = "$wpdb->posts.comment_count"; |
|---|
| 5201 | break; |
|---|
| 5202 | default: |
|---|
| 5203 | if ( 0 === strpos( $orderby, 'post_' ) ) { |
|---|
| 5204 | $orderby = "$wpdb->posts." . $orderby; |
|---|
| 5205 | } else { |
|---|
| 5206 | $orderby = "$wpdb->posts.post_" . $orderby; |
|---|
| 5207 | } |
|---|
| 5208 | } |
|---|
| 5209 | |
|---|
| 5210 | $orderby_array[] = $orderby; |
|---|
| 5211 | |
|---|
| 5212 | } |
|---|
| 5213 | $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; |
|---|
| 5214 | |
|---|
| 5215 | $sort_order = strtoupper( $r['sort_order'] ); |
|---|
| 5216 | if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) { |
|---|
| 5217 | $sort_order = 'ASC'; |
|---|
| 5218 | } |
|---|
| 5219 | |
|---|
| 5220 | $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
|---|
| 5221 | $query .= $author_query; |
|---|
| 5222 | $query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order; |
|---|
| 5223 | |
|---|
| 5224 | if ( ! empty( $number ) ) { |
|---|
| 5225 | $query .= ' LIMIT ' . $offset . ',' . $number; |
|---|
| 5226 | } |
|---|
| 5227 | |
|---|
| 5228 | $pages = $wpdb->get_results( $query ); |
|---|
| 5229 | |
|---|
| 5230 | if ( empty( $pages ) ) { |
|---|
| 5231 | wp_cache_set( $cache_key, array(), 'posts' ); |
|---|
| 5232 | |
|---|
| 5233 | /** This filter is documented in wp-includes/post.php */ |
|---|
| 5234 | $pages = apply_filters( 'get_pages', array(), $r ); |
|---|
| 5235 | return $pages; |
|---|
| 5236 | } |
|---|
| 5237 | |
|---|
| 5238 | // Sanitize before caching so it'll only get done once. |
|---|
| 5239 | $num_pages = count( $pages ); |
|---|
| 5240 | for ( $i = 0; $i < $num_pages; $i++ ) { |
|---|
| 5241 | $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); |
|---|
| 5242 | } |
|---|
| 5243 | |
|---|
| 5244 | // Update cache. |
|---|
| 5245 | update_post_cache( $pages ); |
|---|
| 5246 | |
|---|
| 5247 | if ( $child_of || $hierarchical ) { |
|---|
| 5248 | $pages = get_page_children( $child_of, $pages ); |
|---|
| 5249 | } |
|---|
| 5250 | |
|---|
| 5251 | if ( ! empty( $r['exclude_tree'] ) ) { |
|---|
| 5252 | $exclude = wp_parse_id_list( $r['exclude_tree'] ); |
|---|
| 5253 | foreach ( $exclude as $id ) { |
|---|
| 5254 | $children = get_page_children( $id, $pages ); |
|---|
| 5255 | foreach ( $children as $child ) { |
|---|
| 5256 | $exclude[] = $child->ID; |
|---|
| 5257 | } |
|---|
| 5258 | } |
|---|
| 5259 | |
|---|
| 5260 | $num_pages = count( $pages ); |
|---|
| 5261 | for ( $i = 0; $i < $num_pages; $i++ ) { |
|---|
| 5262 | if ( in_array( $pages[ $i ]->ID, $exclude ) ) { |
|---|
| 5263 | unset( $pages[ $i ] ); |
|---|
| 5264 | } |
|---|
| 5265 | } |
|---|
| 5266 | } |
|---|
| 5267 | |
|---|
| 5268 | $page_structure = array(); |
|---|
| 5269 | foreach ( $pages as $page ) { |
|---|
| 5270 | $page_structure[] = $page->ID; |
|---|
| 5271 | } |
|---|
| 5272 | |
|---|
| 5273 | wp_cache_set( $cache_key, $page_structure, 'posts' ); |
|---|
| 5274 | |
|---|
| 5275 | // Convert to WP_Post instances |
|---|
| 5276 | $pages = array_map( 'get_post', $pages ); |
|---|
| 5277 | |
|---|
| 5278 | /** |
|---|
| 5279 | * Filters the retrieved list of pages. |
|---|
| 5280 | * |
|---|
| 5281 | * @since 2.1.0 |
|---|
| 5282 | * |
|---|
| 5283 | * @param array $pages List of pages to retrieve. |
|---|
| 5284 | * @param array $r Array of get_pages() arguments. |
|---|
| 5285 | */ |
|---|
| 5286 | return apply_filters( 'get_pages', $pages, $r ); |
|---|
| 5287 | } |
|---|
| 5288 | |
|---|
| 5289 | // |
|---|
| 5290 | // Attachment functions |
|---|
| 5291 | // |
|---|
| 5292 | |
|---|
| 5293 | /** |
|---|
| 5294 | * Determines whether an attachment URI is local and really an attachment. |
|---|
| 5295 | * |
|---|
| 5296 | * For more information on this and similar theme functions, check out |
|---|
| 5297 | * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|---|
| 5298 | * Conditional Tags} article in the Theme Developer Handbook. |
|---|
| 5299 | * |
|---|
| 5300 | * @since 2.0.0 |
|---|
| 5301 | * |
|---|
| 5302 | * @param string $url URL to check |
|---|
| 5303 | * @return bool True on success, false on failure. |
|---|
| 5304 | */ |
|---|
| 5305 | function is_local_attachment( $url ) { |
|---|
| 5306 | if ( strpos( $url, home_url() ) === false ) { |
|---|
| 5307 | return false; |
|---|
| 5308 | } |
|---|
| 5309 | if ( strpos( $url, home_url( '/?attachment_id=' ) ) !== false ) { |
|---|
| 5310 | return true; |
|---|
| 5311 | } |
|---|
| 5312 | if ( $id = url_to_postid( $url ) ) { |
|---|
| 5313 | $post = get_post( $id ); |
|---|
| 5314 | if ( 'attachment' == $post->post_type ) { |
|---|
| 5315 | return true; |
|---|
| 5316 | } |
|---|
| 5317 | } |
|---|
| 5318 | return false; |
|---|
| 5319 | } |
|---|
| 5320 | |
|---|
| 5321 | /** |
|---|
| 5322 | * Insert an attachment. |
|---|
| 5323 | * |
|---|
| 5324 | * If you set the 'ID' in the $args parameter, it will mean that you are |
|---|
| 5325 | * updating and attempt to update the attachment. You can also set the |
|---|
| 5326 | * attachment name or title by setting the key 'post_name' or 'post_title'. |
|---|
| 5327 | * |
|---|
| 5328 | * You can set the dates for the attachment manually by setting the 'post_date' |
|---|
| 5329 | * and 'post_date_gmt' keys' values. |
|---|
| 5330 | * |
|---|
| 5331 | * By default, the comments will use the default settings for whether the |
|---|
| 5332 | * comments are allowed. You can close them manually or keep them open by |
|---|
| 5333 | * setting the value for the 'comment_status' key. |
|---|
| 5334 | * |
|---|
| 5335 | * @since 2.0.0 |
|---|
| 5336 | * @since 4.7.0 Added the `$wp_error` parameter to allow a WP_Error to be returned on failure. |
|---|
| 5337 | * |
|---|
| 5338 | * @see wp_insert_post() |
|---|
| 5339 | * |
|---|
| 5340 | * @param string|array $args Arguments for inserting an attachment. |
|---|
| 5341 | * @param string $file Optional. Filename. |
|---|
| 5342 | * @param int $parent Optional. Parent post ID. |
|---|
| 5343 | * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. |
|---|
| 5344 | * @return int|WP_Error The attachment ID on success. The value 0 or WP_Error on failure. |
|---|
| 5345 | */ |
|---|
| 5346 | function wp_insert_attachment( $args, $file = false, $parent = 0, $wp_error = false ) { |
|---|
| 5347 | $defaults = array( |
|---|
| 5348 | 'file' => $file, |
|---|
| 5349 | 'post_parent' => 0, |
|---|
| 5350 | ); |
|---|
| 5351 | |
|---|
| 5352 | $data = wp_parse_args( $args, $defaults ); |
|---|
| 5353 | |
|---|
| 5354 | if ( ! empty( $parent ) ) { |
|---|
| 5355 | $data['post_parent'] = $parent; |
|---|
| 5356 | } |
|---|
| 5357 | |
|---|
| 5358 | $data['post_type'] = 'attachment'; |
|---|
| 5359 | |
|---|
| 5360 | return wp_insert_post( $data, $wp_error ); |
|---|
| 5361 | } |
|---|
| 5362 | |
|---|
| 5363 | /** |
|---|
| 5364 | * Trash or delete an attachment. |
|---|
| 5365 | * |
|---|
| 5366 | * When an attachment is permanently deleted, the file will also be removed. |
|---|
| 5367 | * Deletion removes all post meta fields, taxonomy, comments, etc. associated |
|---|
| 5368 | * with the attachment (except the main post). |
|---|
| 5369 | * |
|---|
| 5370 | * The attachment is moved to the trash instead of permanently deleted unless trash |
|---|
| 5371 | * for media is disabled, item is already in the trash, or $force_delete is true. |
|---|
| 5372 | * |
|---|
| 5373 | * @since 2.0.0 |
|---|
| 5374 | * |
|---|
| 5375 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 5376 | * |
|---|
| 5377 | * @param int $post_id Attachment ID. |
|---|
| 5378 | * @param bool $force_delete Optional. Whether to bypass trash and force deletion. |
|---|
| 5379 | * Default false. |
|---|
| 5380 | * @return WP_Post|false|null Post data on success, false or null on failure. |
|---|
| 5381 | */ |
|---|
| 5382 | function wp_delete_attachment( $post_id, $force_delete = false ) { |
|---|
| 5383 | global $wpdb; |
|---|
| 5384 | |
|---|
| 5385 | $post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE ID = %d", $post_id ) ); |
|---|
| 5386 | |
|---|
| 5387 | if ( ! $post ) { |
|---|
| 5388 | return $post; |
|---|
| 5389 | } |
|---|
| 5390 | |
|---|
| 5391 | $post = get_post( $post ); |
|---|
| 5392 | |
|---|
| 5393 | if ( 'attachment' !== $post->post_type ) { |
|---|
| 5394 | return false; |
|---|
| 5395 | } |
|---|
| 5396 | |
|---|
| 5397 | if ( ! $force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' !== $post->post_status ) { |
|---|
| 5398 | return wp_trash_post( $post_id ); |
|---|
| 5399 | } |
|---|
| 5400 | |
|---|
| 5401 | delete_post_meta( $post_id, '_wp_trash_meta_status' ); |
|---|
| 5402 | delete_post_meta( $post_id, '_wp_trash_meta_time' ); |
|---|
| 5403 | |
|---|
| 5404 | $meta = wp_get_attachment_metadata( $post_id ); |
|---|
| 5405 | $backup_sizes = get_post_meta( $post->ID, '_wp_attachment_backup_sizes', true ); |
|---|
| 5406 | $file = get_attached_file( $post_id ); |
|---|
| 5407 | |
|---|
| 5408 | if ( is_multisite() ) { |
|---|
| 5409 | delete_transient( 'dirsize_cache' ); |
|---|
| 5410 | } |
|---|
| 5411 | |
|---|
| 5412 | /** |
|---|
| 5413 | * Fires before an attachment is deleted, at the start of wp_delete_attachment(). |
|---|
| 5414 | * |
|---|
| 5415 | * @since 2.0.0 |
|---|
| 5416 | * |
|---|
| 5417 | * @param int $post_id Attachment ID. |
|---|
| 5418 | */ |
|---|
| 5419 | do_action( 'delete_attachment', $post_id ); |
|---|
| 5420 | |
|---|
| 5421 | wp_delete_object_term_relationships( $post_id, array( 'category', 'post_tag' ) ); |
|---|
| 5422 | wp_delete_object_term_relationships( $post_id, get_object_taxonomies( $post->post_type ) ); |
|---|
| 5423 | |
|---|
| 5424 | // Delete all for any posts. |
|---|
| 5425 | delete_metadata( 'post', null, '_thumbnail_id', $post_id, true ); |
|---|
| 5426 | |
|---|
| 5427 | wp_defer_comment_counting( true ); |
|---|
| 5428 | |
|---|
| 5429 | $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = %d", $post_id ) ); |
|---|
| 5430 | foreach ( $comment_ids as $comment_id ) { |
|---|
| 5431 | wp_delete_comment( $comment_id, true ); |
|---|
| 5432 | } |
|---|
| 5433 | |
|---|
| 5434 | wp_defer_comment_counting( false ); |
|---|
| 5435 | |
|---|
| 5436 | $post_meta_ids = $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE post_id = %d ", $post_id ) ); |
|---|
| 5437 | foreach ( $post_meta_ids as $mid ) { |
|---|
| 5438 | delete_metadata_by_mid( 'post', $mid ); |
|---|
| 5439 | } |
|---|
| 5440 | |
|---|
| 5441 | /** This action is documented in wp-includes/post.php */ |
|---|
| 5442 | do_action( 'delete_post', $post_id ); |
|---|
| 5443 | $result = $wpdb->delete( $wpdb->posts, array( 'ID' => $post_id ) ); |
|---|
| 5444 | if ( ! $result ) { |
|---|
| 5445 | return false; |
|---|
| 5446 | } |
|---|
| 5447 | /** This action is documented in wp-includes/post.php */ |
|---|
| 5448 | do_action( 'deleted_post', $post_id ); |
|---|
| 5449 | |
|---|
| 5450 | wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ); |
|---|
| 5451 | |
|---|
| 5452 | clean_post_cache( $post ); |
|---|
| 5453 | |
|---|
| 5454 | return $post; |
|---|
| 5455 | } |
|---|
| 5456 | |
|---|
| 5457 | /** |
|---|
| 5458 | * Deletes all files that belong to the given attachment. |
|---|
| 5459 | * |
|---|
| 5460 | * @since 4.9.7 |
|---|
| 5461 | * |
|---|
| 5462 | * @param int $post_id Attachment ID. |
|---|
| 5463 | * @param array $meta The attachment's meta data. |
|---|
| 5464 | * @param array $backup_sizes The meta data for the attachment's backup images. |
|---|
| 5465 | * @param string $file Absolute path to the attachment's file. |
|---|
| 5466 | * @return bool True on success, false on failure. |
|---|
| 5467 | */ |
|---|
| 5468 | function wp_delete_attachment_files( $post_id, $meta, $backup_sizes, $file ) { |
|---|
| 5469 | global $wpdb; |
|---|
| 5470 | |
|---|
| 5471 | $uploadpath = wp_get_upload_dir(); |
|---|
| 5472 | $deleted = true; |
|---|
| 5473 | |
|---|
| 5474 | if ( ! empty( $meta['thumb'] ) ) { |
|---|
| 5475 | // Don't delete the thumb if another attachment uses it. |
|---|
| 5476 | if ( ! $wpdb->get_row( $wpdb->prepare( "SELECT meta_id FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like( $meta['thumb'] ) . '%', $post_id ) ) ) { |
|---|
| 5477 | $thumbfile = str_replace( wp_basename( $file ), $meta['thumb'], $file ); |
|---|
| 5478 | if ( ! empty( $thumbfile ) ) { |
|---|
| 5479 | $thumbfile = path_join( $uploadpath['basedir'], $thumbfile ); |
|---|
| 5480 | $thumbdir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
|---|
| 5481 | |
|---|
| 5482 | if ( ! wp_delete_file_from_directory( $thumbfile, $thumbdir ) ) { |
|---|
| 5483 | $deleted = false; |
|---|
| 5484 | } |
|---|
| 5485 | } |
|---|
| 5486 | } |
|---|
| 5487 | } |
|---|
| 5488 | |
|---|
| 5489 | // Remove intermediate and backup images if there are any. |
|---|
| 5490 | if ( isset( $meta['sizes'] ) && is_array( $meta['sizes'] ) ) { |
|---|
| 5491 | $intermediate_dir = path_join( $uploadpath['basedir'], dirname( $file ) ); |
|---|
| 5492 | foreach ( $meta['sizes'] as $size => $sizeinfo ) { |
|---|
| 5493 | $intermediate_file = str_replace( wp_basename( $file ), $sizeinfo['file'], $file ); |
|---|
| 5494 | if ( ! empty( $intermediate_file ) ) { |
|---|
| 5495 | $intermediate_file = path_join( $uploadpath['basedir'], $intermediate_file ); |
|---|
| 5496 | |
|---|
| 5497 | if ( ! wp_delete_file_from_directory( $intermediate_file, $intermediate_dir ) ) { |
|---|
| 5498 | $deleted = false; |
|---|
| 5499 | } |
|---|
| 5500 | } |
|---|
| 5501 | } |
|---|
| 5502 | } |
|---|
| 5503 | |
|---|
| 5504 | if ( is_array( $backup_sizes ) ) { |
|---|
| 5505 | $del_dir = path_join( $uploadpath['basedir'], dirname( $meta['file'] ) ); |
|---|
| 5506 | foreach ( $backup_sizes as $size ) { |
|---|
| 5507 | $del_file = path_join( dirname( $meta['file'] ), $size['file'] ); |
|---|
| 5508 | if ( ! empty( $del_file ) ) { |
|---|
| 5509 | $del_file = path_join( $uploadpath['basedir'], $del_file ); |
|---|
| 5510 | |
|---|
| 5511 | if ( ! wp_delete_file_from_directory( $del_file, $del_dir ) ) { |
|---|
| 5512 | $deleted = false; |
|---|
| 5513 | } |
|---|
| 5514 | } |
|---|
| 5515 | } |
|---|
| 5516 | } |
|---|
| 5517 | |
|---|
| 5518 | if ( ! wp_delete_file_from_directory( $file, $uploadpath['basedir'] ) ) { |
|---|
| 5519 | $deleted = false; |
|---|
| 5520 | } |
|---|
| 5521 | |
|---|
| 5522 | return $deleted; |
|---|
| 5523 | } |
|---|
| 5524 | |
|---|
| 5525 | /** |
|---|
| 5526 | * Retrieve attachment meta field for attachment ID. |
|---|
| 5527 | * |
|---|
| 5528 | * @since 2.1.0 |
|---|
| 5529 | * |
|---|
| 5530 | * @param int $attachment_id Attachment post ID. Defaults to global $post. |
|---|
| 5531 | * @param bool $unfiltered Optional. If true, filters are not run. Default false. |
|---|
| 5532 | * @return mixed Attachment meta field. False on failure. |
|---|
| 5533 | */ |
|---|
| 5534 | function wp_get_attachment_metadata( $attachment_id = 0, $unfiltered = false ) { |
|---|
| 5535 | $attachment_id = (int) $attachment_id; |
|---|
| 5536 | if ( ! $post = get_post( $attachment_id ) ) { |
|---|
| 5537 | return false; |
|---|
| 5538 | } |
|---|
| 5539 | |
|---|
| 5540 | $data = get_post_meta( $post->ID, '_wp_attachment_metadata', true ); |
|---|
| 5541 | |
|---|
| 5542 | if ( $unfiltered ) { |
|---|
| 5543 | return $data; |
|---|
| 5544 | } |
|---|
| 5545 | |
|---|
| 5546 | /** |
|---|
| 5547 | * Filters the attachment meta data. |
|---|
| 5548 | * |
|---|
| 5549 | * @since 2.1.0 |
|---|
| 5550 | * |
|---|
| 5551 | * @param array|bool $data Array of meta data for the given attachment, or false |
|---|
| 5552 | * if the object does not exist. |
|---|
| 5553 | * @param int $attachment_id Attachment post ID. |
|---|
| 5554 | */ |
|---|
| 5555 | return apply_filters( 'wp_get_attachment_metadata', $data, $post->ID ); |
|---|
| 5556 | } |
|---|
| 5557 | |
|---|
| 5558 | /** |
|---|
| 5559 | * Update metadata for an attachment. |
|---|
| 5560 | * |
|---|
| 5561 | * @since 2.1.0 |
|---|
| 5562 | * |
|---|
| 5563 | * @param int $attachment_id Attachment post ID. |
|---|
| 5564 | * @param array $data Attachment meta data. |
|---|
| 5565 | * @return int|bool False if $post is invalid. |
|---|
| 5566 | */ |
|---|
| 5567 | function wp_update_attachment_metadata( $attachment_id, $data ) { |
|---|
| 5568 | $attachment_id = (int) $attachment_id; |
|---|
| 5569 | if ( ! $post = get_post( $attachment_id ) ) { |
|---|
| 5570 | return false; |
|---|
| 5571 | } |
|---|
| 5572 | |
|---|
| 5573 | /** |
|---|
| 5574 | * Filters the updated attachment meta data. |
|---|
| 5575 | * |
|---|
| 5576 | * @since 2.1.0 |
|---|
| 5577 | * |
|---|
| 5578 | * @param array $data Array of updated attachment meta data. |
|---|
| 5579 | * @param int $attachment_id Attachment post ID. |
|---|
| 5580 | */ |
|---|
| 5581 | if ( $data = apply_filters( 'wp_update_attachment_metadata', $data, $post->ID ) ) { |
|---|
| 5582 | return update_post_meta( $post->ID, '_wp_attachment_metadata', $data ); |
|---|
| 5583 | } else { |
|---|
| 5584 | return delete_post_meta( $post->ID, '_wp_attachment_metadata' ); |
|---|
| 5585 | } |
|---|
| 5586 | } |
|---|
| 5587 | |
|---|
| 5588 | /** |
|---|
| 5589 | * Retrieve the URL for an attachment. |
|---|
| 5590 | * |
|---|
| 5591 | * @since 2.1.0 |
|---|
| 5592 | * |
|---|
| 5593 | * @global string $pagenow |
|---|
| 5594 | * |
|---|
| 5595 | * @param int $attachment_id Optional. Attachment post ID. Defaults to global $post. |
|---|
| 5596 | * @return string|false Attachment URL, otherwise false. |
|---|
| 5597 | */ |
|---|
| 5598 | function wp_get_attachment_url( $attachment_id = 0 ) { |
|---|
| 5599 | $attachment_id = (int) $attachment_id; |
|---|
| 5600 | if ( ! $post = get_post( $attachment_id ) ) { |
|---|
| 5601 | return false; |
|---|
| 5602 | } |
|---|
| 5603 | |
|---|
| 5604 | if ( 'attachment' != $post->post_type ) { |
|---|
| 5605 | return false; |
|---|
| 5606 | } |
|---|
| 5607 | |
|---|
| 5608 | $url = ''; |
|---|
| 5609 | // Get attached file. |
|---|
| 5610 | if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true ) ) { |
|---|
| 5611 | // Get upload directory. |
|---|
| 5612 | if ( ( $uploads = wp_get_upload_dir() ) && false === $uploads['error'] ) { |
|---|
| 5613 | // Check that the upload base exists in the file location. |
|---|
| 5614 | if ( 0 === strpos( $file, $uploads['basedir'] ) ) { |
|---|
| 5615 | // Replace file location with url location. |
|---|
| 5616 | $url = str_replace( $uploads['basedir'], $uploads['baseurl'], $file ); |
|---|
| 5617 | } elseif ( false !== strpos( $file, 'wp-content/uploads' ) ) { |
|---|
| 5618 | // Get the directory name relative to the basedir (back compat for pre-2.7 uploads) |
|---|
| 5619 | $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . wp_basename( $file ); |
|---|
| 5620 | } else { |
|---|
| 5621 | // It's a newly-uploaded file, therefore $file is relative to the basedir. |
|---|
| 5622 | $url = $uploads['baseurl'] . "/$file"; |
|---|
| 5623 | } |
|---|
| 5624 | } |
|---|
| 5625 | } |
|---|
| 5626 | |
|---|
| 5627 | /* |
|---|
| 5628 | * If any of the above options failed, Fallback on the GUID as used pre-2.7, |
|---|
| 5629 | * not recommended to rely upon this. |
|---|
| 5630 | */ |
|---|
| 5631 | if ( empty( $url ) ) { |
|---|
| 5632 | $url = get_the_guid( $post->ID ); |
|---|
| 5633 | } |
|---|
| 5634 | |
|---|
| 5635 | // On SSL front end, URLs should be HTTPS. |
|---|
| 5636 | if ( is_ssl() && ! is_admin() && 'wp-login.php' !== $GLOBALS['pagenow'] ) { |
|---|
| 5637 | $url = set_url_scheme( $url ); |
|---|
| 5638 | } |
|---|
| 5639 | |
|---|
| 5640 | /** |
|---|
| 5641 | * Filters the attachment URL. |
|---|
| 5642 | * |
|---|
| 5643 | * @since 2.1.0 |
|---|
| 5644 | * |
|---|
| 5645 | * @param string $url URL for the given attachment. |
|---|
| 5646 | * @param int $attachment_id Attachment post ID. |
|---|
| 5647 | */ |
|---|
| 5648 | $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID ); |
|---|
| 5649 | |
|---|
| 5650 | if ( empty( $url ) ) { |
|---|
| 5651 | return false; |
|---|
| 5652 | } |
|---|
| 5653 | |
|---|
| 5654 | return $url; |
|---|
| 5655 | } |
|---|
| 5656 | |
|---|
| 5657 | /** |
|---|
| 5658 | * Retrieves the caption for an attachment. |
|---|
| 5659 | * |
|---|
| 5660 | * @since 4.6.0 |
|---|
| 5661 | * |
|---|
| 5662 | * @param int $post_id Optional. Attachment ID. Default is the ID of the global `$post`. |
|---|
| 5663 | * @return string|false False on failure. Attachment caption on success. |
|---|
| 5664 | */ |
|---|
| 5665 | function wp_get_attachment_caption( $post_id = 0 ) { |
|---|
| 5666 | $post_id = (int) $post_id; |
|---|
| 5667 | if ( ! $post = get_post( $post_id ) ) { |
|---|
| 5668 | return false; |
|---|
| 5669 | } |
|---|
| 5670 | |
|---|
| 5671 | if ( 'attachment' !== $post->post_type ) { |
|---|
| 5672 | return false; |
|---|
| 5673 | } |
|---|
| 5674 | |
|---|
| 5675 | $caption = $post->post_excerpt; |
|---|
| 5676 | |
|---|
| 5677 | /** |
|---|
| 5678 | * Filters the attachment caption. |
|---|
| 5679 | * |
|---|
| 5680 | * @since 4.6.0 |
|---|
| 5681 | * |
|---|
| 5682 | * @param string $caption Caption for the given attachment. |
|---|
| 5683 | * @param int $post_id Attachment ID. |
|---|
| 5684 | */ |
|---|
| 5685 | return apply_filters( 'wp_get_attachment_caption', $caption, $post->ID ); |
|---|
| 5686 | } |
|---|
| 5687 | |
|---|
| 5688 | /** |
|---|
| 5689 | * Retrieve thumbnail for an attachment. |
|---|
| 5690 | * |
|---|
| 5691 | * @since 2.1.0 |
|---|
| 5692 | * |
|---|
| 5693 | * @param int $post_id Optional. Attachment ID. Default 0. |
|---|
| 5694 | * @return string|false False on failure. Thumbnail file path on success. |
|---|
| 5695 | */ |
|---|
| 5696 | function wp_get_attachment_thumb_file( $post_id = 0 ) { |
|---|
| 5697 | $post_id = (int) $post_id; |
|---|
| 5698 | if ( ! $post = get_post( $post_id ) ) { |
|---|
| 5699 | return false; |
|---|
| 5700 | } |
|---|
| 5701 | if ( ! is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) ) { |
|---|
| 5702 | return false; |
|---|
| 5703 | } |
|---|
| 5704 | |
|---|
| 5705 | $file = get_attached_file( $post->ID ); |
|---|
| 5706 | |
|---|
| 5707 | if ( ! empty( $imagedata['thumb'] ) && ( $thumbfile = str_replace( wp_basename( $file ), $imagedata['thumb'], $file ) ) && file_exists( $thumbfile ) ) { |
|---|
| 5708 | /** |
|---|
| 5709 | * Filters the attachment thumbnail file path. |
|---|
| 5710 | * |
|---|
| 5711 | * @since 2.1.0 |
|---|
| 5712 | * |
|---|
| 5713 | * @param string $thumbfile File path to the attachment thumbnail. |
|---|
| 5714 | * @param int $post_id Attachment ID. |
|---|
| 5715 | */ |
|---|
| 5716 | return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID ); |
|---|
| 5717 | } |
|---|
| 5718 | return false; |
|---|
| 5719 | } |
|---|
| 5720 | |
|---|
| 5721 | /** |
|---|
| 5722 | * Retrieve URL for an attachment thumbnail. |
|---|
| 5723 | * |
|---|
| 5724 | * @since 2.1.0 |
|---|
| 5725 | * |
|---|
| 5726 | * @param int $post_id Optional. Attachment ID. Default 0. |
|---|
| 5727 | * @return string|false False on failure. Thumbnail URL on success. |
|---|
| 5728 | */ |
|---|
| 5729 | function wp_get_attachment_thumb_url( $post_id = 0 ) { |
|---|
| 5730 | $post_id = (int) $post_id; |
|---|
| 5731 | if ( ! $post = get_post( $post_id ) ) { |
|---|
| 5732 | return false; |
|---|
| 5733 | } |
|---|
| 5734 | if ( ! $url = wp_get_attachment_url( $post->ID ) ) { |
|---|
| 5735 | return false; |
|---|
| 5736 | } |
|---|
| 5737 | |
|---|
| 5738 | $sized = image_downsize( $post_id, 'thumbnail' ); |
|---|
| 5739 | if ( $sized ) { |
|---|
| 5740 | return $sized[0]; |
|---|
| 5741 | } |
|---|
| 5742 | |
|---|
| 5743 | if ( ! $thumb = wp_get_attachment_thumb_file( $post->ID ) ) { |
|---|
| 5744 | return false; |
|---|
| 5745 | } |
|---|
| 5746 | |
|---|
| 5747 | $url = str_replace( wp_basename( $url ), wp_basename( $thumb ), $url ); |
|---|
| 5748 | |
|---|
| 5749 | /** |
|---|
| 5750 | * Filters the attachment thumbnail URL. |
|---|
| 5751 | * |
|---|
| 5752 | * @since 2.1.0 |
|---|
| 5753 | * |
|---|
| 5754 | * @param string $url URL for the attachment thumbnail. |
|---|
| 5755 | * @param int $post_id Attachment ID. |
|---|
| 5756 | */ |
|---|
| 5757 | return apply_filters( 'wp_get_attachment_thumb_url', $url, $post->ID ); |
|---|
| 5758 | } |
|---|
| 5759 | |
|---|
| 5760 | /** |
|---|
| 5761 | * Verifies an attachment is of a given type. |
|---|
| 5762 | * |
|---|
| 5763 | * @since 4.2.0 |
|---|
| 5764 | * |
|---|
| 5765 | * @param string $type Attachment type. Accepts 'image', 'audio', or 'video'. |
|---|
| 5766 | * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
|---|
| 5767 | * @return bool True if one of the accepted types, false otherwise. |
|---|
| 5768 | */ |
|---|
| 5769 | function wp_attachment_is( $type, $post = null ) { |
|---|
| 5770 | if ( ! $post = get_post( $post ) ) { |
|---|
| 5771 | return false; |
|---|
| 5772 | } |
|---|
| 5773 | |
|---|
| 5774 | if ( ! $file = get_attached_file( $post->ID ) ) { |
|---|
| 5775 | return false; |
|---|
| 5776 | } |
|---|
| 5777 | |
|---|
| 5778 | if ( 0 === strpos( $post->post_mime_type, $type . '/' ) ) { |
|---|
| 5779 | return true; |
|---|
| 5780 | } |
|---|
| 5781 | |
|---|
| 5782 | $check = wp_check_filetype( $file ); |
|---|
| 5783 | if ( empty( $check['ext'] ) ) { |
|---|
| 5784 | return false; |
|---|
| 5785 | } |
|---|
| 5786 | |
|---|
| 5787 | $ext = $check['ext']; |
|---|
| 5788 | |
|---|
| 5789 | if ( 'import' !== $post->post_mime_type ) { |
|---|
| 5790 | return $type === $ext; |
|---|
| 5791 | } |
|---|
| 5792 | |
|---|
| 5793 | switch ( $type ) { |
|---|
| 5794 | case 'image': |
|---|
| 5795 | $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' ); |
|---|
| 5796 | return in_array( $ext, $image_exts ); |
|---|
| 5797 | |
|---|
| 5798 | case 'audio': |
|---|
| 5799 | return in_array( $ext, wp_get_audio_extensions() ); |
|---|
| 5800 | |
|---|
| 5801 | case 'video': |
|---|
| 5802 | return in_array( $ext, wp_get_video_extensions() ); |
|---|
| 5803 | |
|---|
| 5804 | default: |
|---|
| 5805 | return $type === $ext; |
|---|
| 5806 | } |
|---|
| 5807 | } |
|---|
| 5808 | |
|---|
| 5809 | /** |
|---|
| 5810 | * Determines whether an attachment is an image. |
|---|
| 5811 | * |
|---|
| 5812 | * For more information on this and similar theme functions, check out |
|---|
| 5813 | * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ |
|---|
| 5814 | * Conditional Tags} article in the Theme Developer Handbook. |
|---|
| 5815 | * |
|---|
| 5816 | * @since 2.1.0 |
|---|
| 5817 | * @since 4.2.0 Modified into wrapper for wp_attachment_is() and |
|---|
| 5818 | * allowed WP_Post object to be passed. |
|---|
| 5819 | * |
|---|
| 5820 | * @param int|WP_Post $post Optional. Attachment ID or object. Default is global $post. |
|---|
| 5821 | * @return bool Whether the attachment is an image. |
|---|
| 5822 | */ |
|---|
| 5823 | function wp_attachment_is_image( $post = null ) { |
|---|
| 5824 | return wp_attachment_is( 'image', $post ); |
|---|
| 5825 | } |
|---|
| 5826 | |
|---|
| 5827 | /** |
|---|
| 5828 | * Retrieve the icon for a MIME type. |
|---|
| 5829 | * |
|---|
| 5830 | * @since 2.1.0 |
|---|
| 5831 | * |
|---|
| 5832 | * @param string|int $mime MIME type or attachment ID. |
|---|
| 5833 | * @return string|false Icon, false otherwise. |
|---|
| 5834 | */ |
|---|
| 5835 | function wp_mime_type_icon( $mime = 0 ) { |
|---|
| 5836 | if ( ! is_numeric( $mime ) ) { |
|---|
| 5837 | $icon = wp_cache_get( "mime_type_icon_$mime" ); |
|---|
| 5838 | } |
|---|
| 5839 | |
|---|
| 5840 | $post_id = 0; |
|---|
| 5841 | if ( empty( $icon ) ) { |
|---|
| 5842 | $post_mimes = array(); |
|---|
| 5843 | if ( is_numeric( $mime ) ) { |
|---|
| 5844 | $mime = (int) $mime; |
|---|
| 5845 | if ( $post = get_post( $mime ) ) { |
|---|
| 5846 | $post_id = (int) $post->ID; |
|---|
| 5847 | $file = get_attached_file( $post_id ); |
|---|
| 5848 | $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $file ); |
|---|
| 5849 | if ( ! empty( $ext ) ) { |
|---|
| 5850 | $post_mimes[] = $ext; |
|---|
| 5851 | if ( $ext_type = wp_ext2type( $ext ) ) { |
|---|
| 5852 | $post_mimes[] = $ext_type; |
|---|
| 5853 | } |
|---|
| 5854 | } |
|---|
| 5855 | $mime = $post->post_mime_type; |
|---|
| 5856 | } else { |
|---|
| 5857 | $mime = 0; |
|---|
| 5858 | } |
|---|
| 5859 | } else { |
|---|
| 5860 | $post_mimes[] = $mime; |
|---|
| 5861 | } |
|---|
| 5862 | |
|---|
| 5863 | $icon_files = wp_cache_get( 'icon_files' ); |
|---|
| 5864 | |
|---|
| 5865 | if ( ! is_array( $icon_files ) ) { |
|---|
| 5866 | /** |
|---|
| 5867 | * Filters the icon directory path. |
|---|
| 5868 | * |
|---|
| 5869 | * @since 2.0.0 |
|---|
| 5870 | * |
|---|
| 5871 | * @param string $path Icon directory absolute path. |
|---|
| 5872 | */ |
|---|
| 5873 | $icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' ); |
|---|
| 5874 | |
|---|
| 5875 | /** |
|---|
| 5876 | * Filters the icon directory URI. |
|---|
| 5877 | * |
|---|
| 5878 | * @since 2.0.0 |
|---|
| 5879 | * |
|---|
| 5880 | * @param string $uri Icon directory URI. |
|---|
| 5881 | */ |
|---|
| 5882 | $icon_dir_uri = apply_filters( 'icon_dir_uri', includes_url( 'images/media' ) ); |
|---|
| 5883 | |
|---|
| 5884 | /** |
|---|
| 5885 | * Filters the list of icon directory URIs. |
|---|
| 5886 | * |
|---|
| 5887 | * @since 2.5.0 |
|---|
| 5888 | * |
|---|
| 5889 | * @param array $uris List of icon directory URIs. |
|---|
| 5890 | */ |
|---|
| 5891 | $dirs = apply_filters( 'icon_dirs', array( $icon_dir => $icon_dir_uri ) ); |
|---|
| 5892 | $icon_files = array(); |
|---|
| 5893 | while ( $dirs ) { |
|---|
| 5894 | $keys = array_keys( $dirs ); |
|---|
| 5895 | $dir = array_shift( $keys ); |
|---|
| 5896 | $uri = array_shift( $dirs ); |
|---|
| 5897 | if ( $dh = opendir( $dir ) ) { |
|---|
| 5898 | while ( false !== $file = readdir( $dh ) ) { |
|---|
| 5899 | $file = wp_basename( $file ); |
|---|
| 5900 | if ( substr( $file, 0, 1 ) == '.' ) { |
|---|
| 5901 | continue; |
|---|
| 5902 | } |
|---|
| 5903 | if ( ! in_array( strtolower( substr( $file, -4 ) ), array( '.png', '.gif', '.jpg' ) ) ) { |
|---|
| 5904 | if ( is_dir( "$dir/$file" ) ) { |
|---|
| 5905 | $dirs[ "$dir/$file" ] = "$uri/$file"; |
|---|
| 5906 | } |
|---|
| 5907 | continue; |
|---|
| 5908 | } |
|---|
| 5909 | $icon_files[ "$dir/$file" ] = "$uri/$file"; |
|---|
| 5910 | } |
|---|
| 5911 | closedir( $dh ); |
|---|
| 5912 | } |
|---|
| 5913 | } |
|---|
| 5914 | wp_cache_add( 'icon_files', $icon_files, 'default', 600 ); |
|---|
| 5915 | } |
|---|
| 5916 | |
|---|
| 5917 | $types = array(); |
|---|
| 5918 | // Icon wp_basename - extension = MIME wildcard. |
|---|
| 5919 | foreach ( $icon_files as $file => $uri ) { |
|---|
| 5920 | $types[ preg_replace( '/^([^.]*).*$/', '$1', wp_basename( $file ) ) ] =& $icon_files[ $file ]; |
|---|
| 5921 | } |
|---|
| 5922 | |
|---|
| 5923 | if ( ! empty( $mime ) ) { |
|---|
| 5924 | $post_mimes[] = substr( $mime, 0, strpos( $mime, '/' ) ); |
|---|
| 5925 | $post_mimes[] = substr( $mime, strpos( $mime, '/' ) + 1 ); |
|---|
| 5926 | $post_mimes[] = str_replace( '/', '_', $mime ); |
|---|
| 5927 | } |
|---|
| 5928 | |
|---|
| 5929 | $matches = wp_match_mime_types( array_keys( $types ), $post_mimes ); |
|---|
| 5930 | $matches['default'] = array( 'default' ); |
|---|
| 5931 | |
|---|
| 5932 | foreach ( $matches as $match => $wilds ) { |
|---|
| 5933 | foreach ( $wilds as $wild ) { |
|---|
| 5934 | if ( ! isset( $types[ $wild ] ) ) { |
|---|
| 5935 | continue; |
|---|
| 5936 | } |
|---|
| 5937 | |
|---|
| 5938 | $icon = $types[ $wild ]; |
|---|
| 5939 | if ( ! is_numeric( $mime ) ) { |
|---|
| 5940 | wp_cache_add( "mime_type_icon_$mime", $icon ); |
|---|
| 5941 | } |
|---|
| 5942 | break 2; |
|---|
| 5943 | } |
|---|
| 5944 | } |
|---|
| 5945 | } |
|---|
| 5946 | |
|---|
| 5947 | /** |
|---|
| 5948 | * Filters the mime type icon. |
|---|
| 5949 | * |
|---|
| 5950 | * @since 2.1.0 |
|---|
| 5951 | * |
|---|
| 5952 | * @param string $icon Path to the mime type icon. |
|---|
| 5953 | * @param string $mime Mime type. |
|---|
| 5954 | * @param int $post_id Attachment ID. Will equal 0 if the function passed |
|---|
| 5955 | * the mime type. |
|---|
| 5956 | */ |
|---|
| 5957 | return apply_filters( 'wp_mime_type_icon', $icon, $mime, $post_id ); |
|---|
| 5958 | } |
|---|
| 5959 | |
|---|
| 5960 | /** |
|---|
| 5961 | * Check for changed slugs for published post objects and save the old slug. |
|---|
| 5962 | * |
|---|
| 5963 | * The function is used when a post object of any type is updated, |
|---|
| 5964 | * by comparing the current and previous post objects. |
|---|
| 5965 | * |
|---|
| 5966 | * If the slug was changed and not already part of the old slugs then it will be |
|---|
| 5967 | * added to the post meta field ('_wp_old_slug') for storing old slugs for that |
|---|
| 5968 | * post. |
|---|
| 5969 | * |
|---|
| 5970 | * The most logically usage of this function is redirecting changed post objects, so |
|---|
| 5971 | * that those that linked to an changed post will be redirected to the new post. |
|---|
| 5972 | * |
|---|
| 5973 | * @since 2.1.0 |
|---|
| 5974 | * |
|---|
| 5975 | * @param int $post_id Post ID. |
|---|
| 5976 | * @param WP_Post $post The Post Object |
|---|
| 5977 | * @param WP_Post $post_before The Previous Post Object |
|---|
| 5978 | */ |
|---|
| 5979 | function wp_check_for_changed_slugs( $post_id, $post, $post_before ) { |
|---|
| 5980 | // Don't bother if it hasn't changed. |
|---|
| 5981 | if ( $post->post_name == $post_before->post_name ) { |
|---|
| 5982 | return; |
|---|
| 5983 | } |
|---|
| 5984 | |
|---|
| 5985 | // We're only concerned with published, non-hierarchical objects. |
|---|
| 5986 | if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { |
|---|
| 5987 | return; |
|---|
| 5988 | } |
|---|
| 5989 | |
|---|
| 5990 | $old_slugs = (array) get_post_meta( $post_id, '_wp_old_slug' ); |
|---|
| 5991 | |
|---|
| 5992 | // If we haven't added this old slug before, add it now. |
|---|
| 5993 | if ( ! empty( $post_before->post_name ) && ! in_array( $post_before->post_name, $old_slugs ) ) { |
|---|
| 5994 | add_post_meta( $post_id, '_wp_old_slug', $post_before->post_name ); |
|---|
| 5995 | } |
|---|
| 5996 | |
|---|
| 5997 | // If the new slug was used previously, delete it from the list. |
|---|
| 5998 | if ( in_array( $post->post_name, $old_slugs ) ) { |
|---|
| 5999 | delete_post_meta( $post_id, '_wp_old_slug', $post->post_name ); |
|---|
| 6000 | } |
|---|
| 6001 | } |
|---|
| 6002 | |
|---|
| 6003 | /** |
|---|
| 6004 | * Check for changed dates for published post objects and save the old date. |
|---|
| 6005 | * |
|---|
| 6006 | * The function is used when a post object of any type is updated, |
|---|
| 6007 | * by comparing the current and previous post objects. |
|---|
| 6008 | * |
|---|
| 6009 | * If the date was changed and not already part of the old dates then it will be |
|---|
| 6010 | * added to the post meta field ('_wp_old_date') for storing old dates for that |
|---|
| 6011 | * post. |
|---|
| 6012 | * |
|---|
| 6013 | * The most logically usage of this function is redirecting changed post objects, so |
|---|
| 6014 | * that those that linked to an changed post will be redirected to the new post. |
|---|
| 6015 | * |
|---|
| 6016 | * @since 4.9.3 |
|---|
| 6017 | * |
|---|
| 6018 | * @param int $post_id Post ID. |
|---|
| 6019 | * @param WP_Post $post The Post Object |
|---|
| 6020 | * @param WP_Post $post_before The Previous Post Object |
|---|
| 6021 | */ |
|---|
| 6022 | function wp_check_for_changed_dates( $post_id, $post, $post_before ) { |
|---|
| 6023 | $previous_date = date( 'Y-m-d', strtotime( $post_before->post_date ) ); |
|---|
| 6024 | $new_date = date( 'Y-m-d', strtotime( $post->post_date ) ); |
|---|
| 6025 | // Don't bother if it hasn't changed. |
|---|
| 6026 | if ( $new_date == $previous_date ) { |
|---|
| 6027 | return; |
|---|
| 6028 | } |
|---|
| 6029 | // We're only concerned with published, non-hierarchical objects. |
|---|
| 6030 | if ( ! ( 'publish' === $post->post_status || ( 'attachment' === get_post_type( $post ) && 'inherit' === $post->post_status ) ) || is_post_type_hierarchical( $post->post_type ) ) { |
|---|
| 6031 | return; |
|---|
| 6032 | } |
|---|
| 6033 | $old_dates = (array) get_post_meta( $post_id, '_wp_old_date' ); |
|---|
| 6034 | // If we haven't added this old date before, add it now. |
|---|
| 6035 | if ( ! empty( $previous_date ) && ! in_array( $previous_date, $old_dates ) ) { |
|---|
| 6036 | add_post_meta( $post_id, '_wp_old_date', $previous_date ); |
|---|
| 6037 | } |
|---|
| 6038 | // If the new slug was used previously, delete it from the list. |
|---|
| 6039 | if ( in_array( $new_date, $old_dates ) ) { |
|---|
| 6040 | delete_post_meta( $post_id, '_wp_old_date', $new_date ); |
|---|
| 6041 | } |
|---|
| 6042 | } |
|---|
| 6043 | |
|---|
| 6044 | /** |
|---|
| 6045 | * Retrieve the private post SQL based on capability. |
|---|
| 6046 | * |
|---|
| 6047 | * This function provides a standardized way to appropriately select on the |
|---|
| 6048 | * post_status of a post type. The function will return a piece of SQL code |
|---|
| 6049 | * that can be added to a WHERE clause; this SQL is constructed to allow all |
|---|
| 6050 | * published posts, and all private posts to which the user has access. |
|---|
| 6051 | * |
|---|
| 6052 | * @since 2.2.0 |
|---|
| 6053 | * @since 4.3.0 Added the ability to pass an array to `$post_type`. |
|---|
| 6054 | * |
|---|
| 6055 | * @param string|array $post_type Single post type or an array of post types. Currently only supports 'post' or 'page'. |
|---|
| 6056 | * @return string SQL code that can be added to a where clause. |
|---|
| 6057 | */ |
|---|
| 6058 | function get_private_posts_cap_sql( $post_type ) { |
|---|
| 6059 | return get_posts_by_author_sql( $post_type, false ); |
|---|
| 6060 | } |
|---|
| 6061 | |
|---|
| 6062 | /** |
|---|
| 6063 | * Retrieve the post SQL based on capability, author, and type. |
|---|
| 6064 | * |
|---|
| 6065 | * @since 3.0.0 |
|---|
| 6066 | * @since 4.3.0 Introduced the ability to pass an array of post types to `$post_type`. |
|---|
| 6067 | * |
|---|
| 6068 | * @see get_private_posts_cap_sql() |
|---|
| 6069 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6070 | * |
|---|
| 6071 | * @param array|string $post_type Single post type or an array of post types. |
|---|
| 6072 | * @param bool $full Optional. Returns a full WHERE statement instead of just |
|---|
| 6073 | * an 'andalso' term. Default true. |
|---|
| 6074 | * @param int $post_author Optional. Query posts having a single author ID. Default null. |
|---|
| 6075 | * @param bool $public_only Optional. Only return public posts. Skips cap checks for |
|---|
| 6076 | * $current_user. Default false. |
|---|
| 6077 | * @return string SQL WHERE code that can be added to a query. |
|---|
| 6078 | */ |
|---|
| 6079 | function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) { |
|---|
| 6080 | global $wpdb; |
|---|
| 6081 | |
|---|
| 6082 | if ( is_array( $post_type ) ) { |
|---|
| 6083 | $post_types = $post_type; |
|---|
| 6084 | } else { |
|---|
| 6085 | $post_types = array( $post_type ); |
|---|
| 6086 | } |
|---|
| 6087 | |
|---|
| 6088 | $post_type_clauses = array(); |
|---|
| 6089 | foreach ( $post_types as $post_type ) { |
|---|
| 6090 | $post_type_obj = get_post_type_object( $post_type ); |
|---|
| 6091 | if ( ! $post_type_obj ) { |
|---|
| 6092 | continue; |
|---|
| 6093 | } |
|---|
| 6094 | |
|---|
| 6095 | /** |
|---|
| 6096 | * Filters the capability to read private posts for a custom post type |
|---|
| 6097 | * when generating SQL for getting posts by author. |
|---|
| 6098 | * |
|---|
| 6099 | * @since 2.2.0 |
|---|
| 6100 | * @deprecated 3.2.0 The hook transitioned from "somewhat useless" to "totally useless". |
|---|
| 6101 | * |
|---|
| 6102 | * @param string $cap Capability. |
|---|
| 6103 | */ |
|---|
| 6104 | if ( ! $cap = apply_filters( 'pub_priv_sql_capability', '' ) ) { |
|---|
| 6105 | $cap = current_user_can( $post_type_obj->cap->read_private_posts ); |
|---|
| 6106 | } |
|---|
| 6107 | |
|---|
| 6108 | // Only need to check the cap if $public_only is false. |
|---|
| 6109 | $post_status_sql = "post_status = 'publish'"; |
|---|
| 6110 | if ( false === $public_only ) { |
|---|
| 6111 | if ( $cap ) { |
|---|
| 6112 | // Does the user have the capability to view private posts? Guess so. |
|---|
| 6113 | $post_status_sql .= " OR post_status = 'private'"; |
|---|
| 6114 | } elseif ( is_user_logged_in() ) { |
|---|
| 6115 | // Users can view their own private posts. |
|---|
| 6116 | $id = get_current_user_id(); |
|---|
| 6117 | if ( null === $post_author || ! $full ) { |
|---|
| 6118 | $post_status_sql .= " OR post_status = 'private' AND post_author = $id"; |
|---|
| 6119 | } elseif ( $id == (int) $post_author ) { |
|---|
| 6120 | $post_status_sql .= " OR post_status = 'private'"; |
|---|
| 6121 | } // else none |
|---|
| 6122 | } // else none |
|---|
| 6123 | } |
|---|
| 6124 | |
|---|
| 6125 | $post_type_clauses[] = "( post_type = '" . $post_type . "' AND ( $post_status_sql ) )"; |
|---|
| 6126 | } |
|---|
| 6127 | |
|---|
| 6128 | if ( empty( $post_type_clauses ) ) { |
|---|
| 6129 | return $full ? 'WHERE 1 = 0' : '1 = 0'; |
|---|
| 6130 | } |
|---|
| 6131 | |
|---|
| 6132 | $sql = '( ' . implode( ' OR ', $post_type_clauses ) . ' )'; |
|---|
| 6133 | |
|---|
| 6134 | if ( null !== $post_author ) { |
|---|
| 6135 | $sql .= $wpdb->prepare( ' AND post_author = %d', $post_author ); |
|---|
| 6136 | } |
|---|
| 6137 | |
|---|
| 6138 | if ( $full ) { |
|---|
| 6139 | $sql = 'WHERE ' . $sql; |
|---|
| 6140 | } |
|---|
| 6141 | |
|---|
| 6142 | return $sql; |
|---|
| 6143 | } |
|---|
| 6144 | |
|---|
| 6145 | /** |
|---|
| 6146 | * Retrieve the date that the last post was published. |
|---|
| 6147 | * |
|---|
| 6148 | * The server timezone is the default and is the difference between GMT and |
|---|
| 6149 | * server time. The 'blog' value is the date when the last post was posted. The |
|---|
| 6150 | * 'gmt' is when the last post was posted in GMT formatted date. |
|---|
| 6151 | * |
|---|
| 6152 | * @since 0.71 |
|---|
| 6153 | * @since 4.4.0 The `$post_type` argument was added. |
|---|
| 6154 | * |
|---|
| 6155 | * @param string $timezone Optional. The timezone for the timestamp. Accepts 'server', 'blog', or 'gmt'. |
|---|
| 6156 | * 'server' uses the server's internal timezone. |
|---|
| 6157 | * 'blog' uses the `post_modified` field, which proxies to the timezone set for the site. |
|---|
| 6158 | * 'gmt' uses the `post_modified_gmt` field. |
|---|
| 6159 | * Default 'server'. |
|---|
| 6160 | * @param string $post_type Optional. The post type to check. Default 'any'. |
|---|
| 6161 | * @return string The date of the last post. |
|---|
| 6162 | */ |
|---|
| 6163 | function get_lastpostdate( $timezone = 'server', $post_type = 'any' ) { |
|---|
| 6164 | /** |
|---|
| 6165 | * Filters the date the last post was published. |
|---|
| 6166 | * |
|---|
| 6167 | * @since 2.3.0 |
|---|
| 6168 | * |
|---|
| 6169 | * @param string $date Date the last post was published. |
|---|
| 6170 | * @param string $timezone Location to use for getting the post published date. |
|---|
| 6171 | * See get_lastpostdate() for accepted `$timezone` values. |
|---|
| 6172 | */ |
|---|
| 6173 | return apply_filters( 'get_lastpostdate', _get_last_post_time( $timezone, 'date', $post_type ), $timezone ); |
|---|
| 6174 | } |
|---|
| 6175 | |
|---|
| 6176 | /** |
|---|
| 6177 | * Get the timestamp of the last time any post was modified. |
|---|
| 6178 | * |
|---|
| 6179 | * The server timezone is the default and is the difference between GMT and |
|---|
| 6180 | * server time. The 'blog' value is just when the last post was modified. The |
|---|
| 6181 | * 'gmt' is when the last post was modified in GMT time. |
|---|
| 6182 | * |
|---|
| 6183 | * @since 1.2.0 |
|---|
| 6184 | * @since 4.4.0 The `$post_type` argument was added. |
|---|
| 6185 | * |
|---|
| 6186 | * @param string $timezone Optional. The timezone for the timestamp. See get_lastpostdate() |
|---|
| 6187 | * for information on accepted values. |
|---|
| 6188 | * Default 'server'. |
|---|
| 6189 | * @param string $post_type Optional. The post type to check. Default 'any'. |
|---|
| 6190 | * @return string The timestamp. |
|---|
| 6191 | */ |
|---|
| 6192 | function get_lastpostmodified( $timezone = 'server', $post_type = 'any' ) { |
|---|
| 6193 | /** |
|---|
| 6194 | * Pre-filter the return value of get_lastpostmodified() before the query is run. |
|---|
| 6195 | * |
|---|
| 6196 | * @since 4.4.0 |
|---|
| 6197 | * |
|---|
| 6198 | * @param string $lastpostmodified Date the last post was modified. |
|---|
| 6199 | * Returning anything other than false will short-circuit the function. |
|---|
| 6200 | * @param string $timezone Location to use for getting the post modified date. |
|---|
| 6201 | * See get_lastpostdate() for accepted `$timezone` values. |
|---|
| 6202 | * @param string $post_type The post type to check. |
|---|
| 6203 | */ |
|---|
| 6204 | $lastpostmodified = apply_filters( 'pre_get_lastpostmodified', false, $timezone, $post_type ); |
|---|
| 6205 | if ( false !== $lastpostmodified ) { |
|---|
| 6206 | return $lastpostmodified; |
|---|
| 6207 | } |
|---|
| 6208 | |
|---|
| 6209 | $lastpostmodified = _get_last_post_time( $timezone, 'modified', $post_type ); |
|---|
| 6210 | |
|---|
| 6211 | $lastpostdate = get_lastpostdate( $timezone ); |
|---|
| 6212 | if ( $lastpostdate > $lastpostmodified ) { |
|---|
| 6213 | $lastpostmodified = $lastpostdate; |
|---|
| 6214 | } |
|---|
| 6215 | |
|---|
| 6216 | /** |
|---|
| 6217 | * Filters the date the last post was modified. |
|---|
| 6218 | * |
|---|
| 6219 | * @since 2.3.0 |
|---|
| 6220 | * |
|---|
| 6221 | * @param string $lastpostmodified Date the last post was modified. |
|---|
| 6222 | * @param string $timezone Location to use for getting the post modified date. |
|---|
| 6223 | * See get_lastpostdate() for accepted `$timezone` values. |
|---|
| 6224 | */ |
|---|
| 6225 | return apply_filters( 'get_lastpostmodified', $lastpostmodified, $timezone ); |
|---|
| 6226 | } |
|---|
| 6227 | |
|---|
| 6228 | /** |
|---|
| 6229 | * Get the timestamp of the last time any post was modified or published. |
|---|
| 6230 | * |
|---|
| 6231 | * @since 3.1.0 |
|---|
| 6232 | * @since 4.4.0 The `$post_type` argument was added. |
|---|
| 6233 | * @access private |
|---|
| 6234 | * |
|---|
| 6235 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6236 | * |
|---|
| 6237 | * @param string $timezone The timezone for the timestamp. See get_lastpostdate(). |
|---|
| 6238 | * for information on accepted values. |
|---|
| 6239 | * @param string $field Post field to check. Accepts 'date' or 'modified'. |
|---|
| 6240 | * @param string $post_type Optional. The post type to check. Default 'any'. |
|---|
| 6241 | * @return string|false The timestamp. |
|---|
| 6242 | */ |
|---|
| 6243 | function _get_last_post_time( $timezone, $field, $post_type = 'any' ) { |
|---|
| 6244 | global $wpdb; |
|---|
| 6245 | |
|---|
| 6246 | if ( ! in_array( $field, array( 'date', 'modified' ) ) ) { |
|---|
| 6247 | return false; |
|---|
| 6248 | } |
|---|
| 6249 | |
|---|
| 6250 | $timezone = strtolower( $timezone ); |
|---|
| 6251 | |
|---|
| 6252 | $key = "lastpost{$field}:$timezone"; |
|---|
| 6253 | if ( 'any' !== $post_type ) { |
|---|
| 6254 | $key .= ':' . sanitize_key( $post_type ); |
|---|
| 6255 | } |
|---|
| 6256 | |
|---|
| 6257 | $date = wp_cache_get( $key, 'timeinfo' ); |
|---|
| 6258 | if ( false !== $date ) { |
|---|
| 6259 | return $date; |
|---|
| 6260 | } |
|---|
| 6261 | |
|---|
| 6262 | if ( 'any' === $post_type ) { |
|---|
| 6263 | $post_types = get_post_types( array( 'public' => true ) ); |
|---|
| 6264 | array_walk( $post_types, array( $wpdb, 'escape_by_ref' ) ); |
|---|
| 6265 | $post_types = "'" . implode( "', '", $post_types ) . "'"; |
|---|
| 6266 | } else { |
|---|
| 6267 | $post_types = "'" . sanitize_key( $post_type ) . "'"; |
|---|
| 6268 | } |
|---|
| 6269 | |
|---|
| 6270 | switch ( $timezone ) { |
|---|
| 6271 | case 'gmt': |
|---|
| 6272 | $date = $wpdb->get_var( "SELECT post_{$field}_gmt FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); |
|---|
| 6273 | break; |
|---|
| 6274 | case 'blog': |
|---|
| 6275 | $date = $wpdb->get_var( "SELECT post_{$field} FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); |
|---|
| 6276 | break; |
|---|
| 6277 | case 'server': |
|---|
| 6278 | $add_seconds_server = date( 'Z' ); |
|---|
| 6279 | $date = $wpdb->get_var( "SELECT DATE_ADD(post_{$field}_gmt, INTERVAL '$add_seconds_server' SECOND) FROM $wpdb->posts WHERE post_status = 'publish' AND post_type IN ({$post_types}) ORDER BY post_{$field}_gmt DESC LIMIT 1" ); |
|---|
| 6280 | break; |
|---|
| 6281 | } |
|---|
| 6282 | |
|---|
| 6283 | if ( $date ) { |
|---|
| 6284 | wp_cache_set( $key, $date, 'timeinfo' ); |
|---|
| 6285 | |
|---|
| 6286 | return $date; |
|---|
| 6287 | } |
|---|
| 6288 | |
|---|
| 6289 | return false; |
|---|
| 6290 | } |
|---|
| 6291 | |
|---|
| 6292 | /** |
|---|
| 6293 | * Updates posts in cache. |
|---|
| 6294 | * |
|---|
| 6295 | * @since 1.5.1 |
|---|
| 6296 | * |
|---|
| 6297 | * @param array $posts Array of post objects (passed by reference). |
|---|
| 6298 | */ |
|---|
| 6299 | function update_post_cache( &$posts ) { |
|---|
| 6300 | if ( ! $posts ) { |
|---|
| 6301 | return; |
|---|
| 6302 | } |
|---|
| 6303 | |
|---|
| 6304 | foreach ( $posts as $post ) { |
|---|
| 6305 | wp_cache_add( $post->ID, $post, 'posts' ); |
|---|
| 6306 | } |
|---|
| 6307 | } |
|---|
| 6308 | |
|---|
| 6309 | /** |
|---|
| 6310 | * Will clean the post in the cache. |
|---|
| 6311 | * |
|---|
| 6312 | * Cleaning means delete from the cache of the post. Will call to clean the term |
|---|
| 6313 | * object cache associated with the post ID. |
|---|
| 6314 | * |
|---|
| 6315 | * This function not run if $_wp_suspend_cache_invalidation is not empty. See |
|---|
| 6316 | * wp_suspend_cache_invalidation(). |
|---|
| 6317 | * |
|---|
| 6318 | * @since 2.0.0 |
|---|
| 6319 | * |
|---|
| 6320 | * @global bool $_wp_suspend_cache_invalidation |
|---|
| 6321 | * |
|---|
| 6322 | * @param int|WP_Post $post Post ID or post object to remove from the cache. |
|---|
| 6323 | */ |
|---|
| 6324 | function clean_post_cache( $post ) { |
|---|
| 6325 | global $_wp_suspend_cache_invalidation; |
|---|
| 6326 | |
|---|
| 6327 | if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
|---|
| 6328 | return; |
|---|
| 6329 | } |
|---|
| 6330 | |
|---|
| 6331 | $post = get_post( $post ); |
|---|
| 6332 | if ( empty( $post ) ) { |
|---|
| 6333 | return; |
|---|
| 6334 | } |
|---|
| 6335 | |
|---|
| 6336 | wp_cache_delete( $post->ID, 'posts' ); |
|---|
| 6337 | wp_cache_delete( $post->ID, 'post_meta' ); |
|---|
| 6338 | |
|---|
| 6339 | clean_object_term_cache( $post->ID, $post->post_type ); |
|---|
| 6340 | |
|---|
| 6341 | wp_cache_delete( 'wp_get_archives', 'general' ); |
|---|
| 6342 | |
|---|
| 6343 | /** |
|---|
| 6344 | * Fires immediately after the given post's cache is cleaned. |
|---|
| 6345 | * |
|---|
| 6346 | * @since 2.5.0 |
|---|
| 6347 | * |
|---|
| 6348 | * @param int $post_id Post ID. |
|---|
| 6349 | * @param WP_Post $post Post object. |
|---|
| 6350 | */ |
|---|
| 6351 | do_action( 'clean_post_cache', $post->ID, $post ); |
|---|
| 6352 | |
|---|
| 6353 | if ( 'page' == $post->post_type ) { |
|---|
| 6354 | wp_cache_delete( 'all_page_ids', 'posts' ); |
|---|
| 6355 | |
|---|
| 6356 | /** |
|---|
| 6357 | * Fires immediately after the given page's cache is cleaned. |
|---|
| 6358 | * |
|---|
| 6359 | * @since 2.5.0 |
|---|
| 6360 | * |
|---|
| 6361 | * @param int $post_id Post ID. |
|---|
| 6362 | */ |
|---|
| 6363 | do_action( 'clean_page_cache', $post->ID ); |
|---|
| 6364 | } |
|---|
| 6365 | |
|---|
| 6366 | wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|---|
| 6367 | } |
|---|
| 6368 | |
|---|
| 6369 | /** |
|---|
| 6370 | * Call major cache updating functions for list of Post objects. |
|---|
| 6371 | * |
|---|
| 6372 | * @since 1.5.0 |
|---|
| 6373 | * |
|---|
| 6374 | * @param array $posts Array of Post objects |
|---|
| 6375 | * @param string $post_type Optional. Post type. Default 'post'. |
|---|
| 6376 | * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
|---|
| 6377 | * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|---|
| 6378 | */ |
|---|
| 6379 | function update_post_caches( &$posts, $post_type = 'post', $update_term_cache = true, $update_meta_cache = true ) { |
|---|
| 6380 | // No point in doing all this work if we didn't match any posts. |
|---|
| 6381 | if ( ! $posts ) { |
|---|
| 6382 | return; |
|---|
| 6383 | } |
|---|
| 6384 | |
|---|
| 6385 | update_post_cache( $posts ); |
|---|
| 6386 | |
|---|
| 6387 | $post_ids = array(); |
|---|
| 6388 | foreach ( $posts as $post ) { |
|---|
| 6389 | $post_ids[] = $post->ID; |
|---|
| 6390 | } |
|---|
| 6391 | |
|---|
| 6392 | if ( ! $post_type ) { |
|---|
| 6393 | $post_type = 'any'; |
|---|
| 6394 | } |
|---|
| 6395 | |
|---|
| 6396 | if ( $update_term_cache ) { |
|---|
| 6397 | if ( is_array( $post_type ) ) { |
|---|
| 6398 | $ptypes = $post_type; |
|---|
| 6399 | } elseif ( 'any' == $post_type ) { |
|---|
| 6400 | $ptypes = array(); |
|---|
| 6401 | // Just use the post_types in the supplied posts. |
|---|
| 6402 | foreach ( $posts as $post ) { |
|---|
| 6403 | $ptypes[] = $post->post_type; |
|---|
| 6404 | } |
|---|
| 6405 | $ptypes = array_unique( $ptypes ); |
|---|
| 6406 | } else { |
|---|
| 6407 | $ptypes = array( $post_type ); |
|---|
| 6408 | } |
|---|
| 6409 | |
|---|
| 6410 | if ( ! empty( $ptypes ) ) { |
|---|
| 6411 | update_object_term_cache( $post_ids, $ptypes ); |
|---|
| 6412 | } |
|---|
| 6413 | } |
|---|
| 6414 | |
|---|
| 6415 | if ( $update_meta_cache ) { |
|---|
| 6416 | update_postmeta_cache( $post_ids ); |
|---|
| 6417 | } |
|---|
| 6418 | } |
|---|
| 6419 | |
|---|
| 6420 | /** |
|---|
| 6421 | * Updates metadata cache for list of post IDs. |
|---|
| 6422 | * |
|---|
| 6423 | * Performs SQL query to retrieve the metadata for the post IDs and updates the |
|---|
| 6424 | * metadata cache for the posts. Therefore, the functions, which call this |
|---|
| 6425 | * function, do not need to perform SQL queries on their own. |
|---|
| 6426 | * |
|---|
| 6427 | * @since 2.1.0 |
|---|
| 6428 | * |
|---|
| 6429 | * @param array $post_ids List of post IDs. |
|---|
| 6430 | * @return array|false Returns false if there is nothing to update or an array |
|---|
| 6431 | * of metadata. |
|---|
| 6432 | */ |
|---|
| 6433 | function update_postmeta_cache( $post_ids ) { |
|---|
| 6434 | return update_meta_cache( 'post', $post_ids ); |
|---|
| 6435 | } |
|---|
| 6436 | |
|---|
| 6437 | /** |
|---|
| 6438 | * Will clean the attachment in the cache. |
|---|
| 6439 | * |
|---|
| 6440 | * Cleaning means delete from the cache. Optionally will clean the term |
|---|
| 6441 | * object cache associated with the attachment ID. |
|---|
| 6442 | * |
|---|
| 6443 | * This function will not run if $_wp_suspend_cache_invalidation is not empty. |
|---|
| 6444 | * |
|---|
| 6445 | * @since 3.0.0 |
|---|
| 6446 | * |
|---|
| 6447 | * @global bool $_wp_suspend_cache_invalidation |
|---|
| 6448 | * |
|---|
| 6449 | * @param int $id The attachment ID in the cache to clean. |
|---|
| 6450 | * @param bool $clean_terms Optional. Whether to clean terms cache. Default false. |
|---|
| 6451 | */ |
|---|
| 6452 | function clean_attachment_cache( $id, $clean_terms = false ) { |
|---|
| 6453 | global $_wp_suspend_cache_invalidation; |
|---|
| 6454 | |
|---|
| 6455 | if ( ! empty( $_wp_suspend_cache_invalidation ) ) { |
|---|
| 6456 | return; |
|---|
| 6457 | } |
|---|
| 6458 | |
|---|
| 6459 | $id = (int) $id; |
|---|
| 6460 | |
|---|
| 6461 | wp_cache_delete( $id, 'posts' ); |
|---|
| 6462 | wp_cache_delete( $id, 'post_meta' ); |
|---|
| 6463 | |
|---|
| 6464 | if ( $clean_terms ) { |
|---|
| 6465 | clean_object_term_cache( $id, 'attachment' ); |
|---|
| 6466 | } |
|---|
| 6467 | |
|---|
| 6468 | /** |
|---|
| 6469 | * Fires after the given attachment's cache is cleaned. |
|---|
| 6470 | * |
|---|
| 6471 | * @since 3.0.0 |
|---|
| 6472 | * |
|---|
| 6473 | * @param int $id Attachment ID. |
|---|
| 6474 | */ |
|---|
| 6475 | do_action( 'clean_attachment_cache', $id ); |
|---|
| 6476 | } |
|---|
| 6477 | |
|---|
| 6478 | // |
|---|
| 6479 | // Hooks |
|---|
| 6480 | // |
|---|
| 6481 | |
|---|
| 6482 | /** |
|---|
| 6483 | * Hook for managing future post transitions to published. |
|---|
| 6484 | * |
|---|
| 6485 | * @since 2.3.0 |
|---|
| 6486 | * @access private |
|---|
| 6487 | * |
|---|
| 6488 | * @see wp_clear_scheduled_hook() |
|---|
| 6489 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6490 | * |
|---|
| 6491 | * @param string $new_status New post status. |
|---|
| 6492 | * @param string $old_status Previous post status. |
|---|
| 6493 | * @param WP_Post $post Post object. |
|---|
| 6494 | */ |
|---|
| 6495 | function _transition_post_status( $new_status, $old_status, $post ) { |
|---|
| 6496 | global $wpdb; |
|---|
| 6497 | |
|---|
| 6498 | if ( $old_status != 'publish' && $new_status == 'publish' ) { |
|---|
| 6499 | // Reset GUID if transitioning to publish and it is empty. |
|---|
| 6500 | if ( '' == get_the_guid( $post->ID ) ) { |
|---|
| 6501 | $wpdb->update( $wpdb->posts, array( 'guid' => get_permalink( $post->ID ) ), array( 'ID' => $post->ID ) ); |
|---|
| 6502 | } |
|---|
| 6503 | |
|---|
| 6504 | /** |
|---|
| 6505 | * Fires when a post's status is transitioned from private to published. |
|---|
| 6506 | * |
|---|
| 6507 | * @since 1.5.0 |
|---|
| 6508 | * @deprecated 2.3.0 Use 'private_to_publish' instead. |
|---|
| 6509 | * |
|---|
| 6510 | * @param int $post_id Post ID. |
|---|
| 6511 | */ |
|---|
| 6512 | do_action( 'private_to_published', $post->ID ); |
|---|
| 6513 | } |
|---|
| 6514 | |
|---|
| 6515 | // If published posts changed clear the lastpostmodified cache. |
|---|
| 6516 | if ( 'publish' == $new_status || 'publish' == $old_status ) { |
|---|
| 6517 | foreach ( array( 'server', 'gmt', 'blog' ) as $timezone ) { |
|---|
| 6518 | wp_cache_delete( "lastpostmodified:$timezone", 'timeinfo' ); |
|---|
| 6519 | wp_cache_delete( "lastpostdate:$timezone", 'timeinfo' ); |
|---|
| 6520 | wp_cache_delete( "lastpostdate:$timezone:{$post->post_type}", 'timeinfo' ); |
|---|
| 6521 | } |
|---|
| 6522 | } |
|---|
| 6523 | |
|---|
| 6524 | if ( $new_status !== $old_status ) { |
|---|
| 6525 | wp_cache_delete( _count_posts_cache_key( $post->post_type ), 'counts' ); |
|---|
| 6526 | wp_cache_delete( _count_posts_cache_key( $post->post_type, 'readable' ), 'counts' ); |
|---|
| 6527 | } |
|---|
| 6528 | |
|---|
| 6529 | // Always clears the hook in case the post status bounced from future to draft. |
|---|
| 6530 | wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
|---|
| 6531 | } |
|---|
| 6532 | |
|---|
| 6533 | /** |
|---|
| 6534 | * Hook used to schedule publication for a post marked for the future. |
|---|
| 6535 | * |
|---|
| 6536 | * The $post properties used and must exist are 'ID' and 'post_date_gmt'. |
|---|
| 6537 | * |
|---|
| 6538 | * @since 2.3.0 |
|---|
| 6539 | * @access private |
|---|
| 6540 | * |
|---|
| 6541 | * @param int $deprecated Not used. Can be set to null. Never implemented. Not marked |
|---|
| 6542 | * as deprecated with _deprecated_argument() as it conflicts with |
|---|
| 6543 | * wp_transition_post_status() and the default filter for _future_post_hook(). |
|---|
| 6544 | * @param WP_Post $post Post object. |
|---|
| 6545 | */ |
|---|
| 6546 | function _future_post_hook( $deprecated, $post ) { |
|---|
| 6547 | wp_clear_scheduled_hook( 'publish_future_post', array( $post->ID ) ); |
|---|
| 6548 | wp_schedule_single_event( strtotime( get_gmt_from_date( $post->post_date ) . ' GMT' ), 'publish_future_post', array( $post->ID ) ); |
|---|
| 6549 | } |
|---|
| 6550 | |
|---|
| 6551 | /** |
|---|
| 6552 | * Hook to schedule pings and enclosures when a post is published. |
|---|
| 6553 | * |
|---|
| 6554 | * Uses XMLRPC_REQUEST and WP_IMPORTING constants. |
|---|
| 6555 | * |
|---|
| 6556 | * @since 2.3.0 |
|---|
| 6557 | * @access private |
|---|
| 6558 | * |
|---|
| 6559 | * @param int $post_id The ID in the database table of the post being published. |
|---|
| 6560 | */ |
|---|
| 6561 | function _publish_post_hook( $post_id ) { |
|---|
| 6562 | if ( defined( 'XMLRPC_REQUEST' ) ) { |
|---|
| 6563 | /** |
|---|
| 6564 | * Fires when _publish_post_hook() is called during an XML-RPC request. |
|---|
| 6565 | * |
|---|
| 6566 | * @since 2.1.0 |
|---|
| 6567 | * |
|---|
| 6568 | * @param int $post_id Post ID. |
|---|
| 6569 | */ |
|---|
| 6570 | do_action( 'xmlrpc_publish_post', $post_id ); |
|---|
| 6571 | } |
|---|
| 6572 | |
|---|
| 6573 | if ( defined( 'WP_IMPORTING' ) ) { |
|---|
| 6574 | return; |
|---|
| 6575 | } |
|---|
| 6576 | |
|---|
| 6577 | if ( get_option( 'default_pingback_flag' ) ) { |
|---|
| 6578 | add_post_meta( $post_id, '_pingme', '1' ); |
|---|
| 6579 | } |
|---|
| 6580 | add_post_meta( $post_id, '_encloseme', '1' ); |
|---|
| 6581 | |
|---|
| 6582 | if ( ! wp_next_scheduled( 'do_pings' ) ) { |
|---|
| 6583 | wp_schedule_single_event( time(), 'do_pings' ); |
|---|
| 6584 | } |
|---|
| 6585 | } |
|---|
| 6586 | |
|---|
| 6587 | /** |
|---|
| 6588 | * Returns the ID of the post's parent. |
|---|
| 6589 | * |
|---|
| 6590 | * @since 3.1.0 |
|---|
| 6591 | * |
|---|
| 6592 | * @param int|WP_Post $post Post ID or post object. Defaults to global $post. |
|---|
| 6593 | * @return int|false Post parent ID (which can be 0 if there is no parent), or false if the post does not exist. |
|---|
| 6594 | */ |
|---|
| 6595 | function wp_get_post_parent_id( $post ) { |
|---|
| 6596 | $post = get_post( $post ); |
|---|
| 6597 | if ( ! $post || is_wp_error( $post ) ) { |
|---|
| 6598 | return false; |
|---|
| 6599 | } |
|---|
| 6600 | return (int) $post->post_parent; |
|---|
| 6601 | } |
|---|
| 6602 | |
|---|
| 6603 | /** |
|---|
| 6604 | * Check the given subset of the post hierarchy for hierarchy loops. |
|---|
| 6605 | * |
|---|
| 6606 | * Prevents loops from forming and breaks those that it finds. Attached |
|---|
| 6607 | * to the {@see 'wp_insert_post_parent'} filter. |
|---|
| 6608 | * |
|---|
| 6609 | * @since 3.1.0 |
|---|
| 6610 | * |
|---|
| 6611 | * @see wp_find_hierarchy_loop() |
|---|
| 6612 | * |
|---|
| 6613 | * @param int $post_parent ID of the parent for the post we're checking. |
|---|
| 6614 | * @param int $post_ID ID of the post we're checking. |
|---|
| 6615 | * @return int The new post_parent for the post, 0 otherwise. |
|---|
| 6616 | */ |
|---|
| 6617 | function wp_check_post_hierarchy_for_loops( $post_parent, $post_ID ) { |
|---|
| 6618 | // Nothing fancy here - bail. |
|---|
| 6619 | if ( ! $post_parent ) { |
|---|
| 6620 | return 0; |
|---|
| 6621 | } |
|---|
| 6622 | |
|---|
| 6623 | // New post can't cause a loop. |
|---|
| 6624 | if ( empty( $post_ID ) ) { |
|---|
| 6625 | return $post_parent; |
|---|
| 6626 | } |
|---|
| 6627 | |
|---|
| 6628 | // Can't be its own parent. |
|---|
| 6629 | if ( $post_parent == $post_ID ) { |
|---|
| 6630 | return 0; |
|---|
| 6631 | } |
|---|
| 6632 | |
|---|
| 6633 | // Now look for larger loops. |
|---|
| 6634 | if ( ! $loop = wp_find_hierarchy_loop( 'wp_get_post_parent_id', $post_ID, $post_parent ) ) { |
|---|
| 6635 | return $post_parent; // No loop |
|---|
| 6636 | } |
|---|
| 6637 | |
|---|
| 6638 | // Setting $post_parent to the given value causes a loop. |
|---|
| 6639 | if ( isset( $loop[ $post_ID ] ) ) { |
|---|
| 6640 | return 0; |
|---|
| 6641 | } |
|---|
| 6642 | |
|---|
| 6643 | // There's a loop, but it doesn't contain $post_ID. Break the loop. |
|---|
| 6644 | foreach ( array_keys( $loop ) as $loop_member ) { |
|---|
| 6645 | wp_update_post( |
|---|
| 6646 | array( |
|---|
| 6647 | 'ID' => $loop_member, |
|---|
| 6648 | 'post_parent' => 0, |
|---|
| 6649 | ) |
|---|
| 6650 | ); |
|---|
| 6651 | } |
|---|
| 6652 | |
|---|
| 6653 | return $post_parent; |
|---|
| 6654 | } |
|---|
| 6655 | |
|---|
| 6656 | /** |
|---|
| 6657 | * Sets the post thumbnail (featured image) for the given post. |
|---|
| 6658 | * |
|---|
| 6659 | * @since 3.1.0 |
|---|
| 6660 | * |
|---|
| 6661 | * @param int|WP_Post $post Post ID or post object where thumbnail should be attached. |
|---|
| 6662 | * @param int $thumbnail_id Thumbnail to attach. |
|---|
| 6663 | * @return int|bool True on success, false on failure. |
|---|
| 6664 | */ |
|---|
| 6665 | function set_post_thumbnail( $post, $thumbnail_id ) { |
|---|
| 6666 | $post = get_post( $post ); |
|---|
| 6667 | $thumbnail_id = absint( $thumbnail_id ); |
|---|
| 6668 | if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) { |
|---|
| 6669 | if ( wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) ) { |
|---|
| 6670 | return update_post_meta( $post->ID, '_thumbnail_id', $thumbnail_id ); |
|---|
| 6671 | } else { |
|---|
| 6672 | return delete_post_meta( $post->ID, '_thumbnail_id' ); |
|---|
| 6673 | } |
|---|
| 6674 | } |
|---|
| 6675 | return false; |
|---|
| 6676 | } |
|---|
| 6677 | |
|---|
| 6678 | /** |
|---|
| 6679 | * Removes the thumbnail (featured image) from the given post. |
|---|
| 6680 | * |
|---|
| 6681 | * @since 3.3.0 |
|---|
| 6682 | * |
|---|
| 6683 | * @param int|WP_Post $post Post ID or post object from which the thumbnail should be removed. |
|---|
| 6684 | * @return bool True on success, false on failure. |
|---|
| 6685 | */ |
|---|
| 6686 | function delete_post_thumbnail( $post ) { |
|---|
| 6687 | $post = get_post( $post ); |
|---|
| 6688 | if ( $post ) { |
|---|
| 6689 | return delete_post_meta( $post->ID, '_thumbnail_id' ); |
|---|
| 6690 | } |
|---|
| 6691 | return false; |
|---|
| 6692 | } |
|---|
| 6693 | |
|---|
| 6694 | /** |
|---|
| 6695 | * Delete auto-drafts for new posts that are > 7 days old. |
|---|
| 6696 | * |
|---|
| 6697 | * @since 3.4.0 |
|---|
| 6698 | * |
|---|
| 6699 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6700 | */ |
|---|
| 6701 | function wp_delete_auto_drafts() { |
|---|
| 6702 | global $wpdb; |
|---|
| 6703 | |
|---|
| 6704 | // Cleanup old auto-drafts more than 7 days old. |
|---|
| 6705 | $old_posts = $wpdb->get_col( "SELECT ID FROM $wpdb->posts WHERE post_status = 'auto-draft' AND DATE_SUB( NOW(), INTERVAL 7 DAY ) > post_date" ); |
|---|
| 6706 | foreach ( (array) $old_posts as $delete ) { |
|---|
| 6707 | // Force delete. |
|---|
| 6708 | wp_delete_post( $delete, true ); |
|---|
| 6709 | } |
|---|
| 6710 | } |
|---|
| 6711 | |
|---|
| 6712 | /** |
|---|
| 6713 | * Queues posts for lazy-loading of term meta. |
|---|
| 6714 | * |
|---|
| 6715 | * @since 4.5.0 |
|---|
| 6716 | * |
|---|
| 6717 | * @param array $posts Array of WP_Post objects. |
|---|
| 6718 | */ |
|---|
| 6719 | function wp_queue_posts_for_term_meta_lazyload( $posts ) { |
|---|
| 6720 | $post_type_taxonomies = $term_ids = array(); |
|---|
| 6721 | foreach ( $posts as $post ) { |
|---|
| 6722 | if ( ! ( $post instanceof WP_Post ) ) { |
|---|
| 6723 | continue; |
|---|
| 6724 | } |
|---|
| 6725 | |
|---|
| 6726 | if ( ! isset( $post_type_taxonomies[ $post->post_type ] ) ) { |
|---|
| 6727 | $post_type_taxonomies[ $post->post_type ] = get_object_taxonomies( $post->post_type ); |
|---|
| 6728 | } |
|---|
| 6729 | |
|---|
| 6730 | foreach ( $post_type_taxonomies[ $post->post_type ] as $taxonomy ) { |
|---|
| 6731 | // Term cache should already be primed by `update_post_term_cache()`. |
|---|
| 6732 | $terms = get_object_term_cache( $post->ID, $taxonomy ); |
|---|
| 6733 | if ( false !== $terms ) { |
|---|
| 6734 | foreach ( $terms as $term ) { |
|---|
| 6735 | if ( ! isset( $term_ids[ $term->term_id ] ) ) { |
|---|
| 6736 | $term_ids[] = $term->term_id; |
|---|
| 6737 | } |
|---|
| 6738 | } |
|---|
| 6739 | } |
|---|
| 6740 | } |
|---|
| 6741 | } |
|---|
| 6742 | |
|---|
| 6743 | if ( $term_ids ) { |
|---|
| 6744 | $lazyloader = wp_metadata_lazyloader(); |
|---|
| 6745 | $lazyloader->queue_objects( 'term', $term_ids ); |
|---|
| 6746 | } |
|---|
| 6747 | } |
|---|
| 6748 | |
|---|
| 6749 | /** |
|---|
| 6750 | * Update the custom taxonomies' term counts when a post's status is changed. |
|---|
| 6751 | * |
|---|
| 6752 | * For example, default posts term counts (for custom taxonomies) don't include |
|---|
| 6753 | * private / draft posts. |
|---|
| 6754 | * |
|---|
| 6755 | * @since 3.3.0 |
|---|
| 6756 | * @access private |
|---|
| 6757 | * |
|---|
| 6758 | * @param string $new_status New post status. |
|---|
| 6759 | * @param string $old_status Old post status. |
|---|
| 6760 | * @param WP_Post $post Post object. |
|---|
| 6761 | */ |
|---|
| 6762 | function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) { |
|---|
| 6763 | // Update counts for the post's terms. |
|---|
| 6764 | foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) { |
|---|
| 6765 | $tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) ); |
|---|
| 6766 | wp_update_term_count( $tt_ids, $taxonomy ); |
|---|
| 6767 | } |
|---|
| 6768 | } |
|---|
| 6769 | |
|---|
| 6770 | /** |
|---|
| 6771 | * Adds any posts from the given ids to the cache that do not already exist in cache |
|---|
| 6772 | * |
|---|
| 6773 | * @since 3.4.0 |
|---|
| 6774 | * @access private |
|---|
| 6775 | * |
|---|
| 6776 | * @see update_post_caches() |
|---|
| 6777 | * |
|---|
| 6778 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6779 | * |
|---|
| 6780 | * @param array $ids ID list. |
|---|
| 6781 | * @param bool $update_term_cache Optional. Whether to update the term cache. Default true. |
|---|
| 6782 | * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
|---|
| 6783 | */ |
|---|
| 6784 | function _prime_post_caches( $ids, $update_term_cache = true, $update_meta_cache = true ) { |
|---|
| 6785 | global $wpdb; |
|---|
| 6786 | |
|---|
| 6787 | $non_cached_ids = _get_non_cached_ids( $ids, 'posts' ); |
|---|
| 6788 | if ( ! empty( $non_cached_ids ) ) { |
|---|
| 6789 | $fresh_posts = $wpdb->get_results( sprintf( "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s)", join( ',', $non_cached_ids ) ) ); |
|---|
| 6790 | |
|---|
| 6791 | update_post_caches( $fresh_posts, 'any', $update_term_cache, $update_meta_cache ); |
|---|
| 6792 | } |
|---|
| 6793 | } |
|---|
| 6794 | |
|---|
| 6795 | /** |
|---|
| 6796 | * Adds a suffix if any trashed posts have a given slug. |
|---|
| 6797 | * |
|---|
| 6798 | * Store its desired (i.e. current) slug so it can try to reclaim it |
|---|
| 6799 | * if the post is untrashed. |
|---|
| 6800 | * |
|---|
| 6801 | * For internal use. |
|---|
| 6802 | * |
|---|
| 6803 | * @since 4.5.0 |
|---|
| 6804 | * @access private |
|---|
| 6805 | * |
|---|
| 6806 | * @param string $post_name Slug. |
|---|
| 6807 | * @param string $post_ID Optional. Post ID that should be ignored. Default 0. |
|---|
| 6808 | */ |
|---|
| 6809 | function wp_add_trashed_suffix_to_post_name_for_trashed_posts( $post_name, $post_ID = 0 ) { |
|---|
| 6810 | $trashed_posts_with_desired_slug = get_posts( |
|---|
| 6811 | array( |
|---|
| 6812 | 'name' => $post_name, |
|---|
| 6813 | 'post_status' => 'trash', |
|---|
| 6814 | 'post_type' => 'any', |
|---|
| 6815 | 'nopaging' => true, |
|---|
| 6816 | 'post__not_in' => array( $post_ID ), |
|---|
| 6817 | ) |
|---|
| 6818 | ); |
|---|
| 6819 | |
|---|
| 6820 | if ( ! empty( $trashed_posts_with_desired_slug ) ) { |
|---|
| 6821 | foreach ( $trashed_posts_with_desired_slug as $_post ) { |
|---|
| 6822 | wp_add_trashed_suffix_to_post_name_for_post( $_post ); |
|---|
| 6823 | } |
|---|
| 6824 | } |
|---|
| 6825 | } |
|---|
| 6826 | |
|---|
| 6827 | /** |
|---|
| 6828 | * Adds a trashed suffix for a given post. |
|---|
| 6829 | * |
|---|
| 6830 | * Store its desired (i.e. current) slug so it can try to reclaim it |
|---|
| 6831 | * if the post is untrashed. |
|---|
| 6832 | * |
|---|
| 6833 | * For internal use. |
|---|
| 6834 | * |
|---|
| 6835 | * @since 4.5.0 |
|---|
| 6836 | * @access private |
|---|
| 6837 | * |
|---|
| 6838 | * @param WP_Post $post The post. |
|---|
| 6839 | * @return string New slug for the post. |
|---|
| 6840 | */ |
|---|
| 6841 | function wp_add_trashed_suffix_to_post_name_for_post( $post ) { |
|---|
| 6842 | global $wpdb; |
|---|
| 6843 | |
|---|
| 6844 | $post = get_post( $post ); |
|---|
| 6845 | |
|---|
| 6846 | if ( '__trashed' === substr( $post->post_name, -9 ) ) { |
|---|
| 6847 | return $post->post_name; |
|---|
| 6848 | } |
|---|
| 6849 | add_post_meta( $post->ID, '_wp_desired_post_slug', $post->post_name ); |
|---|
| 6850 | $post_name = _truncate_post_slug( $post->post_name, 191 ) . '__trashed'; |
|---|
| 6851 | $wpdb->update( $wpdb->posts, array( 'post_name' => $post_name ), array( 'ID' => $post->ID ) ); |
|---|
| 6852 | clean_post_cache( $post->ID ); |
|---|
| 6853 | return $post_name; |
|---|
| 6854 | } |
|---|
| 6855 | |
|---|
| 6856 | /** |
|---|
| 6857 | * Filter the SQL clauses of an attachment query to include filenames. |
|---|
| 6858 | * |
|---|
| 6859 | * @since 4.7.0 |
|---|
| 6860 | * @access private |
|---|
| 6861 | * |
|---|
| 6862 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6863 | * |
|---|
| 6864 | * @param array $clauses An array including WHERE, GROUP BY, JOIN, ORDER BY, |
|---|
| 6865 | * DISTINCT, fields (SELECT), and LIMITS clauses. |
|---|
| 6866 | * @return array The modified clauses. |
|---|
| 6867 | */ |
|---|
| 6868 | function _filter_query_attachment_filenames( $clauses ) { |
|---|
| 6869 | global $wpdb; |
|---|
| 6870 | remove_filter( 'posts_clauses', __FUNCTION__ ); |
|---|
| 6871 | |
|---|
| 6872 | // Add a LEFT JOIN of the postmeta table so we don't trample existing JOINs. |
|---|
| 6873 | $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} AS sq1 ON ( {$wpdb->posts}.ID = sq1.post_id AND sq1.meta_key = '_wp_attached_file' )"; |
|---|
| 6874 | |
|---|
| 6875 | $clauses['groupby'] = "{$wpdb->posts}.ID"; |
|---|
| 6876 | |
|---|
| 6877 | $clauses['where'] = preg_replace( |
|---|
| 6878 | "/\({$wpdb->posts}.post_content (NOT LIKE|LIKE) (\'[^']+\')\)/", |
|---|
| 6879 | '$0 OR ( sq1.meta_value $1 $2 )', |
|---|
| 6880 | $clauses['where'] |
|---|
| 6881 | ); |
|---|
| 6882 | |
|---|
| 6883 | return $clauses; |
|---|
| 6884 | } |
|---|
| 6885 | |
|---|
| 6886 | /** |
|---|
| 6887 | * Sets the last changed time for the 'posts' cache group. |
|---|
| 6888 | * |
|---|
| 6889 | * @since 5.0.0 |
|---|
| 6890 | */ |
|---|
| 6891 | function wp_cache_set_posts_last_changed() { |
|---|
| 6892 | wp_cache_set( 'last_changed', microtime(), 'posts' ); |
|---|
| 6893 | } |
|---|
| 6894 | |
|---|
| 6895 | /** |
|---|
| 6896 | * Get all available post MIME types for a given post type. |
|---|
| 6897 | * |
|---|
| 6898 | * @since 2.5.0 |
|---|
| 6899 | * |
|---|
| 6900 | * @global wpdb $wpdb WordPress database abstraction object. |
|---|
| 6901 | * |
|---|
| 6902 | * @param string $type |
|---|
| 6903 | * @return mixed |
|---|
| 6904 | */ |
|---|
| 6905 | function get_available_post_mime_types( $type = 'attachment' ) { |
|---|
| 6906 | global $wpdb; |
|---|
| 6907 | |
|---|
| 6908 | $types = $wpdb->get_col( $wpdb->prepare( "SELECT DISTINCT post_mime_type FROM $wpdb->posts WHERE post_type = %s", $type ) ); |
|---|
| 6909 | return $types; |
|---|
| 6910 | } |
|---|