[ Index ] |
PHP Cross Reference of WordPress |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WordPress Post Administration API. 4 * 5 * @package WordPress 6 * @subpackage Administration 7 */ 8 9 /** 10 * Rename $_POST data from form names to DB post columns. 11 * 12 * Manipulates $_POST directly. 13 * 14 * @since 2.6.0 15 * 16 * @param bool $update Are we updating a pre-existing post? 17 * @param array $post_data Array of post data. Defaults to the contents of $_POST. 18 * @return array|WP_Error Array of post data on success, WP_Error on failure. 19 */ 20 function _wp_translate_postdata( $update = false, $post_data = null ) { 21 22 if ( empty( $post_data ) ) { 23 $post_data = &$_POST; 24 } 25 26 if ( $update ) { 27 $post_data['ID'] = (int) $post_data['post_ID']; 28 } 29 30 $ptype = get_post_type_object( $post_data['post_type'] ); 31 32 if ( $update && ! current_user_can( 'edit_post', $post_data['ID'] ) ) { 33 if ( 'page' === $post_data['post_type'] ) { 34 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); 35 } else { 36 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); 37 } 38 } elseif ( ! $update && ! current_user_can( $ptype->cap->create_posts ) ) { 39 if ( 'page' === $post_data['post_type'] ) { 40 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); 41 } else { 42 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); 43 } 44 } 45 46 if ( isset( $post_data['content'] ) ) { 47 $post_data['post_content'] = $post_data['content']; 48 } 49 50 if ( isset( $post_data['excerpt'] ) ) { 51 $post_data['post_excerpt'] = $post_data['excerpt']; 52 } 53 54 if ( isset( $post_data['parent_id'] ) ) { 55 $post_data['post_parent'] = (int) $post_data['parent_id']; 56 } 57 58 if ( isset( $post_data['trackback_url'] ) ) { 59 $post_data['to_ping'] = $post_data['trackback_url']; 60 } 61 62 $post_data['user_ID'] = get_current_user_id(); 63 64 if ( ! empty( $post_data['post_author_override'] ) ) { 65 $post_data['post_author'] = (int) $post_data['post_author_override']; 66 } else { 67 if ( ! empty( $post_data['post_author'] ) ) { 68 $post_data['post_author'] = (int) $post_data['post_author']; 69 } else { 70 $post_data['post_author'] = (int) $post_data['user_ID']; 71 } 72 } 73 74 if ( isset( $post_data['user_ID'] ) && ( $post_data['post_author'] != $post_data['user_ID'] ) 75 && ! current_user_can( $ptype->cap->edit_others_posts ) ) { 76 77 if ( $update ) { 78 if ( 'page' === $post_data['post_type'] ) { 79 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to edit pages as this user.' ) ); 80 } else { 81 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to edit posts as this user.' ) ); 82 } 83 } else { 84 if ( 'page' === $post_data['post_type'] ) { 85 return new WP_Error( 'edit_others_pages', __( 'Sorry, you are not allowed to create pages as this user.' ) ); 86 } else { 87 return new WP_Error( 'edit_others_posts', __( 'Sorry, you are not allowed to create posts as this user.' ) ); 88 } 89 } 90 } 91 92 if ( ! empty( $post_data['post_status'] ) ) { 93 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 94 95 // No longer an auto-draft. 96 if ( 'auto-draft' === $post_data['post_status'] ) { 97 $post_data['post_status'] = 'draft'; 98 } 99 100 if ( ! get_post_status_object( $post_data['post_status'] ) ) { 101 unset( $post_data['post_status'] ); 102 } 103 } 104 105 // What to do based on which button they pressed. 106 if ( isset( $post_data['saveasdraft'] ) && '' !== $post_data['saveasdraft'] ) { 107 $post_data['post_status'] = 'draft'; 108 } 109 if ( isset( $post_data['saveasprivate'] ) && '' !== $post_data['saveasprivate'] ) { 110 $post_data['post_status'] = 'private'; 111 } 112 if ( isset( $post_data['publish'] ) && ( '' !== $post_data['publish'] ) 113 && ( ! isset( $post_data['post_status'] ) || 'private' !== $post_data['post_status'] ) 114 ) { 115 $post_data['post_status'] = 'publish'; 116 } 117 if ( isset( $post_data['advanced'] ) && '' !== $post_data['advanced'] ) { 118 $post_data['post_status'] = 'draft'; 119 } 120 if ( isset( $post_data['pending'] ) && '' !== $post_data['pending'] ) { 121 $post_data['post_status'] = 'pending'; 122 } 123 124 if ( isset( $post_data['ID'] ) ) { 125 $post_id = $post_data['ID']; 126 } else { 127 $post_id = false; 128 } 129 $previous_status = $post_id ? get_post_field( 'post_status', $post_id ) : false; 130 131 if ( isset( $post_data['post_status'] ) && 'private' === $post_data['post_status'] && ! current_user_can( $ptype->cap->publish_posts ) ) { 132 $post_data['post_status'] = $previous_status ? $previous_status : 'pending'; 133 } 134 135 $published_statuses = array( 'publish', 'future' ); 136 137 // Posts 'submitted for approval' are submitted to $_POST the same as if they were being published. 138 // Change status from 'publish' to 'pending' if user lacks permissions to publish or to resave published posts. 139 if ( isset( $post_data['post_status'] ) 140 && ( in_array( $post_data['post_status'], $published_statuses, true ) 141 && ! current_user_can( $ptype->cap->publish_posts ) ) 142 ) { 143 if ( ! in_array( $previous_status, $published_statuses, true ) || ! current_user_can( 'edit_post', $post_id ) ) { 144 $post_data['post_status'] = 'pending'; 145 } 146 } 147 148 if ( ! isset( $post_data['post_status'] ) ) { 149 $post_data['post_status'] = 'auto-draft' === $previous_status ? 'draft' : $previous_status; 150 } 151 152 if ( isset( $post_data['post_password'] ) && ! current_user_can( $ptype->cap->publish_posts ) ) { 153 unset( $post_data['post_password'] ); 154 } 155 156 if ( ! isset( $post_data['comment_status'] ) ) { 157 $post_data['comment_status'] = 'closed'; 158 } 159 160 if ( ! isset( $post_data['ping_status'] ) ) { 161 $post_data['ping_status'] = 'closed'; 162 } 163 164 foreach ( array( 'aa', 'mm', 'jj', 'hh', 'mn' ) as $timeunit ) { 165 if ( ! empty( $post_data[ 'hidden_' . $timeunit ] ) && $post_data[ 'hidden_' . $timeunit ] != $post_data[ $timeunit ] ) { 166 $post_data['edit_date'] = '1'; 167 break; 168 } 169 } 170 171 if ( ! empty( $post_data['edit_date'] ) ) { 172 $aa = $post_data['aa']; 173 $mm = $post_data['mm']; 174 $jj = $post_data['jj']; 175 $hh = $post_data['hh']; 176 $mn = $post_data['mn']; 177 $ss = $post_data['ss']; 178 $aa = ( $aa <= 0 ) ? gmdate( 'Y' ) : $aa; 179 $mm = ( $mm <= 0 ) ? gmdate( 'n' ) : $mm; 180 $jj = ( $jj > 31 ) ? 31 : $jj; 181 $jj = ( $jj <= 0 ) ? gmdate( 'j' ) : $jj; 182 $hh = ( $hh > 23 ) ? $hh - 24 : $hh; 183 $mn = ( $mn > 59 ) ? $mn - 60 : $mn; 184 $ss = ( $ss > 59 ) ? $ss - 60 : $ss; 185 $post_data['post_date'] = sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $aa, $mm, $jj, $hh, $mn, $ss ); 186 $valid_date = wp_checkdate( $mm, $jj, $aa, $post_data['post_date'] ); 187 if ( ! $valid_date ) { 188 return new WP_Error( 'invalid_date', __( 'Invalid date.' ) ); 189 } 190 $post_data['post_date_gmt'] = get_gmt_from_date( $post_data['post_date'] ); 191 } 192 193 if ( isset( $post_data['post_category'] ) ) { 194 $category_object = get_taxonomy( 'category' ); 195 if ( ! current_user_can( $category_object->cap->assign_terms ) ) { 196 unset( $post_data['post_category'] ); 197 } 198 } 199 200 return $post_data; 201 } 202 203 /** 204 * Returns only allowed post data fields 205 * 206 * @since 5.0.1 207 * 208 * @param array $post_data Array of post data. Defaults to the contents of $_POST. 209 * @return array|WP_Error Array of post data on success, WP_Error on failure. 210 */ 211 function _wp_get_allowed_postdata( $post_data = null ) { 212 if ( empty( $post_data ) ) { 213 $post_data = $_POST; 214 } 215 216 // Pass through errors. 217 if ( is_wp_error( $post_data ) ) { 218 return $post_data; 219 } 220 221 return array_diff_key( $post_data, array_flip( array( 'meta_input', 'file', 'guid' ) ) ); 222 } 223 224 /** 225 * Update an existing post with values provided in $_POST. 226 * 227 * If post data is passed as an argument, it is treated as an array of data 228 * keyed appropriately for turning into a post object. 229 * 230 * If post data is not passed, the $_POST global variable is used instead. 231 * 232 * @since 1.5.0 233 * 234 * @global wpdb $wpdb WordPress database abstraction object. 235 * 236 * @param array $post_data Optional. Defaults to the $_POST global. 237 * @return int Post ID. 238 */ 239 function edit_post( $post_data = null ) { 240 global $wpdb; 241 242 if ( empty( $post_data ) ) { 243 $post_data = &$_POST; 244 } 245 246 // Clear out any data in internal vars. 247 unset( $post_data['filter'] ); 248 249 $post_ID = (int) $post_data['post_ID']; 250 $post = get_post( $post_ID ); 251 $post_data['post_type'] = $post->post_type; 252 $post_data['post_mime_type'] = $post->post_mime_type; 253 254 if ( ! empty( $post_data['post_status'] ) ) { 255 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 256 257 if ( 'inherit' === $post_data['post_status'] ) { 258 unset( $post_data['post_status'] ); 259 } 260 } 261 262 $ptype = get_post_type_object( $post_data['post_type'] ); 263 if ( ! current_user_can( 'edit_post', $post_ID ) ) { 264 if ( 'page' === $post_data['post_type'] ) { 265 wp_die( __( 'Sorry, you are not allowed to edit this page.' ) ); 266 } else { 267 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 268 } 269 } 270 271 if ( post_type_supports( $ptype->name, 'revisions' ) ) { 272 $revisions = wp_get_post_revisions( 273 $post_ID, 274 array( 275 'order' => 'ASC', 276 'posts_per_page' => 1, 277 ) 278 ); 279 $revision = current( $revisions ); 280 281 // Check if the revisions have been upgraded. 282 if ( $revisions && _wp_get_post_revision_version( $revision ) < 1 ) { 283 _wp_upgrade_revisions_of_post( $post, wp_get_post_revisions( $post_ID ) ); 284 } 285 } 286 287 if ( isset( $post_data['visibility'] ) ) { 288 switch ( $post_data['visibility'] ) { 289 case 'public': 290 $post_data['post_password'] = ''; 291 break; 292 case 'password': 293 unset( $post_data['sticky'] ); 294 break; 295 case 'private': 296 $post_data['post_status'] = 'private'; 297 $post_data['post_password'] = ''; 298 unset( $post_data['sticky'] ); 299 break; 300 } 301 } 302 303 $post_data = _wp_translate_postdata( true, $post_data ); 304 if ( is_wp_error( $post_data ) ) { 305 wp_die( $post_data->get_error_message() ); 306 } 307 $translated = _wp_get_allowed_postdata( $post_data ); 308 309 // Post formats. 310 if ( isset( $post_data['post_format'] ) ) { 311 set_post_format( $post_ID, $post_data['post_format'] ); 312 } 313 314 $format_meta_urls = array( 'url', 'link_url', 'quote_source_url' ); 315 foreach ( $format_meta_urls as $format_meta_url ) { 316 $keyed = '_format_' . $format_meta_url; 317 if ( isset( $post_data[ $keyed ] ) ) { 318 update_post_meta( $post_ID, $keyed, wp_slash( esc_url_raw( wp_unslash( $post_data[ $keyed ] ) ) ) ); 319 } 320 } 321 322 $format_keys = array( 'quote', 'quote_source_name', 'image', 'gallery', 'audio_embed', 'video_embed' ); 323 324 foreach ( $format_keys as $key ) { 325 $keyed = '_format_' . $key; 326 if ( isset( $post_data[ $keyed ] ) ) { 327 if ( current_user_can( 'unfiltered_html' ) ) { 328 update_post_meta( $post_ID, $keyed, $post_data[ $keyed ] ); 329 } else { 330 update_post_meta( $post_ID, $keyed, wp_filter_post_kses( $post_data[ $keyed ] ) ); 331 } 332 } 333 } 334 335 if ( 'attachment' === $post_data['post_type'] && preg_match( '#^(audio|video)/#', $post_data['post_mime_type'] ) ) { 336 $id3data = wp_get_attachment_metadata( $post_ID ); 337 if ( ! is_array( $id3data ) ) { 338 $id3data = array(); 339 } 340 341 foreach ( wp_get_attachment_id3_keys( $post, 'edit' ) as $key => $label ) { 342 if ( isset( $post_data[ 'id3_' . $key ] ) ) { 343 $id3data[ $key ] = sanitize_text_field( wp_unslash( $post_data[ 'id3_' . $key ] ) ); 344 } 345 } 346 wp_update_attachment_metadata( $post_ID, $id3data ); 347 } 348 349 // Meta stuff. 350 if ( isset( $post_data['meta'] ) && $post_data['meta'] ) { 351 foreach ( $post_data['meta'] as $key => $value ) { 352 $meta = get_post_meta_by_id( $key ); 353 if ( ! $meta ) { 354 continue; 355 } 356 if ( $meta->post_id != $post_ID ) { 357 continue; 358 } 359 if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $meta->meta_key ) ) { 360 continue; 361 } 362 if ( is_protected_meta( $value['key'], 'post' ) || ! current_user_can( 'edit_post_meta', $post_ID, $value['key'] ) ) { 363 continue; 364 } 365 update_meta( $key, $value['key'], $value['value'] ); 366 } 367 } 368 369 if ( isset( $post_data['deletemeta'] ) && $post_data['deletemeta'] ) { 370 foreach ( $post_data['deletemeta'] as $key => $value ) { 371 $meta = get_post_meta_by_id( $key ); 372 if ( ! $meta ) { 373 continue; 374 } 375 if ( $meta->post_id != $post_ID ) { 376 continue; 377 } 378 if ( is_protected_meta( $meta->meta_key, 'post' ) || ! current_user_can( 'delete_post_meta', $post_ID, $meta->meta_key ) ) { 379 continue; 380 } 381 delete_meta( $key ); 382 } 383 } 384 385 // Attachment stuff. 386 if ( 'attachment' === $post_data['post_type'] ) { 387 if ( isset( $post_data['_wp_attachment_image_alt'] ) ) { 388 $image_alt = wp_unslash( $post_data['_wp_attachment_image_alt'] ); 389 390 if ( get_post_meta( $post_ID, '_wp_attachment_image_alt', true ) !== $image_alt ) { 391 $image_alt = wp_strip_all_tags( $image_alt, true ); 392 393 // update_post_meta() expects slashed. 394 update_post_meta( $post_ID, '_wp_attachment_image_alt', wp_slash( $image_alt ) ); 395 } 396 } 397 398 $attachment_data = isset( $post_data['attachments'][ $post_ID ] ) ? $post_data['attachments'][ $post_ID ] : array(); 399 400 /** This filter is documented in wp-admin/includes/media.php */ 401 $translated = apply_filters( 'attachment_fields_to_save', $translated, $attachment_data ); 402 } 403 404 // Convert taxonomy input to term IDs, to avoid ambiguity. 405 if ( isset( $post_data['tax_input'] ) ) { 406 foreach ( (array) $post_data['tax_input'] as $taxonomy => $terms ) { 407 $tax_object = get_taxonomy( $taxonomy ); 408 409 if ( $tax_object && isset( $tax_object->meta_box_sanitize_cb ) ) { 410 $translated['tax_input'][ $taxonomy ] = call_user_func_array( $tax_object->meta_box_sanitize_cb, array( $taxonomy, $terms ) ); 411 } 412 } 413 } 414 415 add_meta( $post_ID ); 416 417 update_post_meta( $post_ID, '_edit_last', get_current_user_id() ); 418 419 $success = wp_update_post( $translated ); 420 421 // If the save failed, see if we can sanity check the main fields and try again. 422 if ( ! $success && is_callable( array( $wpdb, 'strip_invalid_text_for_column' ) ) ) { 423 $fields = array( 'post_title', 'post_content', 'post_excerpt' ); 424 425 foreach ( $fields as $field ) { 426 if ( isset( $translated[ $field ] ) ) { 427 $translated[ $field ] = $wpdb->strip_invalid_text_for_column( $wpdb->posts, $field, $translated[ $field ] ); 428 } 429 } 430 431 wp_update_post( $translated ); 432 } 433 434 // Now that we have an ID we can fix any attachment anchor hrefs. 435 _fix_attachment_links( $post_ID ); 436 437 wp_set_post_lock( $post_ID ); 438 439 if ( current_user_can( $ptype->cap->edit_others_posts ) && current_user_can( $ptype->cap->publish_posts ) ) { 440 if ( ! empty( $post_data['sticky'] ) ) { 441 stick_post( $post_ID ); 442 } else { 443 unstick_post( $post_ID ); 444 } 445 } 446 447 return $post_ID; 448 } 449 450 /** 451 * Process the post data for the bulk editing of posts. 452 * 453 * Updates all bulk edited posts/pages, adding (but not removing) tags and 454 * categories. Skips pages when they would be their own parent or child. 455 * 456 * @since 2.7.0 457 * 458 * @global wpdb $wpdb WordPress database abstraction object. 459 * 460 * @param array $post_data Optional, the array of post data to process if not provided will use $_POST superglobal. 461 * @return array 462 */ 463 function bulk_edit_posts( $post_data = null ) { 464 global $wpdb; 465 466 if ( empty( $post_data ) ) { 467 $post_data = &$_POST; 468 } 469 470 if ( isset( $post_data['post_type'] ) ) { 471 $ptype = get_post_type_object( $post_data['post_type'] ); 472 } else { 473 $ptype = get_post_type_object( 'post' ); 474 } 475 476 if ( ! current_user_can( $ptype->cap->edit_posts ) ) { 477 if ( 'page' === $ptype->name ) { 478 wp_die( __( 'Sorry, you are not allowed to edit pages.' ) ); 479 } else { 480 wp_die( __( 'Sorry, you are not allowed to edit posts.' ) ); 481 } 482 } 483 484 if ( -1 == $post_data['_status'] ) { 485 $post_data['post_status'] = null; 486 unset( $post_data['post_status'] ); 487 } else { 488 $post_data['post_status'] = $post_data['_status']; 489 } 490 unset( $post_data['_status'] ); 491 492 if ( ! empty( $post_data['post_status'] ) ) { 493 $post_data['post_status'] = sanitize_key( $post_data['post_status'] ); 494 495 if ( 'inherit' === $post_data['post_status'] ) { 496 unset( $post_data['post_status'] ); 497 } 498 } 499 500 $post_IDs = array_map( 'intval', (array) $post_data['post'] ); 501 502 $reset = array( 503 'post_author', 504 'post_status', 505 'post_password', 506 'post_parent', 507 'page_template', 508 'comment_status', 509 'ping_status', 510 'keep_private', 511 'tax_input', 512 'post_category', 513 'sticky', 514 'post_format', 515 ); 516 517 foreach ( $reset as $field ) { 518 if ( isset( $post_data[ $field ] ) && ( '' === $post_data[ $field ] || -1 == $post_data[ $field ] ) ) { 519 unset( $post_data[ $field ] ); 520 } 521 } 522 523 if ( isset( $post_data['post_category'] ) ) { 524 if ( is_array( $post_data['post_category'] ) && ! empty( $post_data['post_category'] ) ) { 525 $new_cats = array_map( 'absint', $post_data['post_category'] ); 526 } else { 527 unset( $post_data['post_category'] ); 528 } 529 } 530 531 $tax_input = array(); 532 if ( isset( $post_data['tax_input'] ) ) { 533 foreach ( $post_data['tax_input'] as $tax_name => $terms ) { 534 if ( empty( $terms ) ) { 535 continue; 536 } 537 if ( is_taxonomy_hierarchical( $tax_name ) ) { 538 $tax_input[ $tax_name ] = array_map( 'absint', $terms ); 539 } else { 540 $comma = _x( ',', 'tag delimiter' ); 541 if ( ',' !== $comma ) { 542 $terms = str_replace( $comma, ',', $terms ); 543 } 544 $tax_input[ $tax_name ] = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); 545 } 546 } 547 } 548 549 if ( isset( $post_data['post_parent'] ) && (int) $post_data['post_parent'] ) { 550 $parent = (int) $post_data['post_parent']; 551 $pages = $wpdb->get_results( "SELECT ID, post_parent FROM $wpdb->posts WHERE post_type = 'page'" ); 552 $children = array(); 553 554 for ( $i = 0; $i < 50 && $parent > 0; $i++ ) { 555 $children[] = $parent; 556 557 foreach ( $pages as $page ) { 558 if ( (int) $page->ID === $parent ) { 559 $parent = (int) $page->post_parent; 560 break; 561 } 562 } 563 } 564 } 565 566 $updated = array(); 567 $skipped = array(); 568 $locked = array(); 569 $shared_post_data = $post_data; 570 571 foreach ( $post_IDs as $post_ID ) { 572 // Start with fresh post data with each iteration. 573 $post_data = $shared_post_data; 574 575 $post_type_object = get_post_type_object( get_post_type( $post_ID ) ); 576 577 if ( ! isset( $post_type_object ) 578 || ( isset( $children ) && in_array( $post_ID, $children, true ) ) 579 || ! current_user_can( 'edit_post', $post_ID ) 580 ) { 581 $skipped[] = $post_ID; 582 continue; 583 } 584 585 if ( wp_check_post_lock( $post_ID ) ) { 586 $locked[] = $post_ID; 587 continue; 588 } 589 590 $post = get_post( $post_ID ); 591 $tax_names = get_object_taxonomies( $post ); 592 foreach ( $tax_names as $tax_name ) { 593 $taxonomy_obj = get_taxonomy( $tax_name ); 594 if ( isset( $tax_input[ $tax_name ] ) && current_user_can( $taxonomy_obj->cap->assign_terms ) ) { 595 $new_terms = $tax_input[ $tax_name ]; 596 } else { 597 $new_terms = array(); 598 } 599 600 if ( $taxonomy_obj->hierarchical ) { 601 $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'ids' ) ); 602 } else { 603 $current_terms = (array) wp_get_object_terms( $post_ID, $tax_name, array( 'fields' => 'names' ) ); 604 } 605 606 $post_data['tax_input'][ $tax_name ] = array_merge( $current_terms, $new_terms ); 607 } 608 609 if ( isset( $new_cats ) && in_array( 'category', $tax_names, true ) ) { 610 $cats = (array) wp_get_post_categories( $post_ID ); 611 $post_data['post_category'] = array_unique( array_merge( $cats, $new_cats ) ); 612 unset( $post_data['tax_input']['category'] ); 613 } 614 615 $post_data['post_ID'] = $post_ID; 616 $post_data['post_type'] = $post->post_type; 617 $post_data['post_mime_type'] = $post->post_mime_type; 618 619 foreach ( array( 'comment_status', 'ping_status', 'post_author' ) as $field ) { 620 if ( ! isset( $post_data[ $field ] ) ) { 621 $post_data[ $field ] = $post->$field; 622 } 623 } 624 625 $post_data = _wp_translate_postdata( true, $post_data ); 626 if ( is_wp_error( $post_data ) ) { 627 $skipped[] = $post_ID; 628 continue; 629 } 630 $post_data = _wp_get_allowed_postdata( $post_data ); 631 632 if ( isset( $shared_post_data['post_format'] ) ) { 633 set_post_format( $post_ID, $shared_post_data['post_format'] ); 634 } 635 636 // Prevent wp_insert_post() from overwriting post format with the old data. 637 unset( $post_data['tax_input']['post_format'] ); 638 639 $updated[] = wp_update_post( $post_data ); 640 641 if ( isset( $post_data['sticky'] ) && current_user_can( $ptype->cap->edit_others_posts ) ) { 642 if ( 'sticky' === $post_data['sticky'] ) { 643 stick_post( $post_ID ); 644 } else { 645 unstick_post( $post_ID ); 646 } 647 } 648 } 649 650 return array( 651 'updated' => $updated, 652 'skipped' => $skipped, 653 'locked' => $locked, 654 ); 655 } 656 657 /** 658 * Default post information to use when populating the "Write Post" form. 659 * 660 * @since 2.0.0 661 * 662 * @param string $post_type Optional. A post type string. Default 'post'. 663 * @param bool $create_in_db Optional. Whether to insert the post into database. Default false. 664 * @return WP_Post Post object containing all the default post data as attributes 665 */ 666 function get_default_post_to_edit( $post_type = 'post', $create_in_db = false ) { 667 $post_title = ''; 668 if ( ! empty( $_REQUEST['post_title'] ) ) { 669 $post_title = esc_html( wp_unslash( $_REQUEST['post_title'] ) ); 670 } 671 672 $post_content = ''; 673 if ( ! empty( $_REQUEST['content'] ) ) { 674 $post_content = esc_html( wp_unslash( $_REQUEST['content'] ) ); 675 } 676 677 $post_excerpt = ''; 678 if ( ! empty( $_REQUEST['excerpt'] ) ) { 679 $post_excerpt = esc_html( wp_unslash( $_REQUEST['excerpt'] ) ); 680 } 681 682 if ( $create_in_db ) { 683 $post_id = wp_insert_post( 684 array( 685 'post_title' => __( 'Auto Draft' ), 686 'post_type' => $post_type, 687 'post_status' => 'auto-draft', 688 ), 689 false, 690 false 691 ); 692 $post = get_post( $post_id ); 693 if ( current_theme_supports( 'post-formats' ) && post_type_supports( $post->post_type, 'post-formats' ) && get_option( 'default_post_format' ) ) { 694 set_post_format( $post, get_option( 'default_post_format' ) ); 695 } 696 wp_after_insert_post( $post, false, null ); 697 698 // Schedule auto-draft cleanup. 699 if ( ! wp_next_scheduled( 'wp_scheduled_auto_draft_delete' ) ) { 700 wp_schedule_event( time(), 'daily', 'wp_scheduled_auto_draft_delete' ); 701 } 702 } else { 703 $post = new stdClass; 704 $post->ID = 0; 705 $post->post_author = ''; 706 $post->post_date = ''; 707 $post->post_date_gmt = ''; 708 $post->post_password = ''; 709 $post->post_name = ''; 710 $post->post_type = $post_type; 711 $post->post_status = 'draft'; 712 $post->to_ping = ''; 713 $post->pinged = ''; 714 $post->comment_status = get_default_comment_status( $post_type ); 715 $post->ping_status = get_default_comment_status( $post_type, 'pingback' ); 716 $post->post_pingback = get_option( 'default_pingback_flag' ); 717 $post->post_category = get_option( 'default_category' ); 718 $post->page_template = 'default'; 719 $post->post_parent = 0; 720 $post->menu_order = 0; 721 $post = new WP_Post( $post ); 722 } 723 724 /** 725 * Filters the default post content initially used in the "Write Post" form. 726 * 727 * @since 1.5.0 728 * 729 * @param string $post_content Default post content. 730 * @param WP_Post $post Post object. 731 */ 732 $post->post_content = (string) apply_filters( 'default_content', $post_content, $post ); 733 734 /** 735 * Filters the default post title initially used in the "Write Post" form. 736 * 737 * @since 1.5.0 738 * 739 * @param string $post_title Default post title. 740 * @param WP_Post $post Post object. 741 */ 742 $post->post_title = (string) apply_filters( 'default_title', $post_title, $post ); 743 744 /** 745 * Filters the default post excerpt initially used in the "Write Post" form. 746 * 747 * @since 1.5.0 748 * 749 * @param string $post_excerpt Default post excerpt. 750 * @param WP_Post $post Post object. 751 */ 752 $post->post_excerpt = (string) apply_filters( 'default_excerpt', $post_excerpt, $post ); 753 754 return $post; 755 } 756 757 /** 758 * Determines if a post exists based on title, content, date and type. 759 * 760 * @since 2.0.0 761 * @since 5.2.0 Added the `$type` parameter. 762 * 763 * @global wpdb $wpdb WordPress database abstraction object. 764 * 765 * @param string $title Post title. 766 * @param string $content Optional post content. 767 * @param string $date Optional post date. 768 * @param string $type Optional post type. 769 * @return int Post ID if post exists, 0 otherwise. 770 */ 771 function post_exists( $title, $content = '', $date = '', $type = '' ) { 772 global $wpdb; 773 774 $post_title = wp_unslash( sanitize_post_field( 'post_title', $title, 0, 'db' ) ); 775 $post_content = wp_unslash( sanitize_post_field( 'post_content', $content, 0, 'db' ) ); 776 $post_date = wp_unslash( sanitize_post_field( 'post_date', $date, 0, 'db' ) ); 777 $post_type = wp_unslash( sanitize_post_field( 'post_type', $type, 0, 'db' ) ); 778 779 $query = "SELECT ID FROM $wpdb->posts WHERE 1=1"; 780 $args = array(); 781 782 if ( ! empty( $date ) ) { 783 $query .= ' AND post_date = %s'; 784 $args[] = $post_date; 785 } 786 787 if ( ! empty( $title ) ) { 788 $query .= ' AND post_title = %s'; 789 $args[] = $post_title; 790 } 791 792 if ( ! empty( $content ) ) { 793 $query .= ' AND post_content = %s'; 794 $args[] = $post_content; 795 } 796 797 if ( ! empty( $type ) ) { 798 $query .= ' AND post_type = %s'; 799 $args[] = $post_type; 800 } 801 802 if ( ! empty( $args ) ) { 803 return (int) $wpdb->get_var( $wpdb->prepare( $query, $args ) ); 804 } 805 806 return 0; 807 } 808 809 /** 810 * Creates a new post from the "Write Post" form using $_POST information. 811 * 812 * @since 2.1.0 813 * 814 * @global WP_User $current_user 815 * 816 * @return int|WP_Error Post ID on success, WP_Error on failure. 817 */ 818 function wp_write_post() { 819 if ( isset( $_POST['post_type'] ) ) { 820 $ptype = get_post_type_object( $_POST['post_type'] ); 821 } else { 822 $ptype = get_post_type_object( 'post' ); 823 } 824 825 if ( ! current_user_can( $ptype->cap->edit_posts ) ) { 826 if ( 'page' === $ptype->name ) { 827 return new WP_Error( 'edit_pages', __( 'Sorry, you are not allowed to create pages on this site.' ) ); 828 } else { 829 return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to create posts or drafts on this site.' ) ); 830 } 831 } 832 833 $_POST['post_mime_type'] = ''; 834 835 // Clear out any data in internal vars. 836 unset( $_POST['filter'] ); 837 838 // Edit, don't write, if we have a post ID. 839 if ( isset( $_POST['post_ID'] ) ) { 840 return edit_post(); 841 } 842 843 if ( isset( $_POST['visibility'] ) ) { 844 switch ( $_POST['visibility'] ) { 845 case 'public': 846 $_POST['post_password'] = ''; 847 break; 848 case 'password': 849 unset( $_POST['sticky'] ); 850 break; 851 case 'private': 852 $_POST['post_status'] = 'private'; 853 $_POST['post_password'] = ''; 854 unset( $_POST['sticky'] ); 855 break; 856 } 857 } 858 859 $translated = _wp_translate_postdata( false ); 860 if ( is_wp_error( $translated ) ) { 861 return $translated; 862 } 863 $translated = _wp_get_allowed_postdata( $translated ); 864 865 // Create the post. 866 $post_ID = wp_insert_post( $translated ); 867 if ( is_wp_error( $post_ID ) ) { 868 return $post_ID; 869 } 870 871 if ( empty( $post_ID ) ) { 872 return 0; 873 } 874 875 add_meta( $post_ID ); 876 877 add_post_meta( $post_ID, '_edit_last', $GLOBALS['current_user']->ID ); 878 879 // Now that we have an ID we can fix any attachment anchor hrefs. 880 _fix_attachment_links( $post_ID ); 881 882 wp_set_post_lock( $post_ID ); 883 884 return $post_ID; 885 } 886 887 /** 888 * Calls wp_write_post() and handles the errors. 889 * 890 * @since 2.0.0 891 * 892 * @return int|void Post ID on success, void on failure. 893 */ 894 function write_post() { 895 $result = wp_write_post(); 896 if ( is_wp_error( $result ) ) { 897 wp_die( $result->get_error_message() ); 898 } else { 899 return $result; 900 } 901 } 902 903 // 904 // Post Meta. 905 // 906 907 /** 908 * Add post meta data defined in $_POST superglobal for post with given ID. 909 * 910 * @since 1.2.0 911 * 912 * @param int $post_ID 913 * @return int|bool 914 */ 915 function add_meta( $post_ID ) { 916 $post_ID = (int) $post_ID; 917 918 $metakeyselect = isset( $_POST['metakeyselect'] ) ? wp_unslash( trim( $_POST['metakeyselect'] ) ) : ''; 919 $metakeyinput = isset( $_POST['metakeyinput'] ) ? wp_unslash( trim( $_POST['metakeyinput'] ) ) : ''; 920 $metavalue = isset( $_POST['metavalue'] ) ? $_POST['metavalue'] : ''; 921 if ( is_string( $metavalue ) ) { 922 $metavalue = trim( $metavalue ); 923 } 924 925 if ( ( ( '#NONE#' !== $metakeyselect ) && ! empty( $metakeyselect ) ) || ! empty( $metakeyinput ) ) { 926 /* 927 * We have a key/value pair. If both the select and the input 928 * for the key have data, the input takes precedence. 929 */ 930 if ( '#NONE#' !== $metakeyselect ) { 931 $metakey = $metakeyselect; 932 } 933 934 if ( $metakeyinput ) { 935 $metakey = $metakeyinput; // Default. 936 } 937 938 if ( is_protected_meta( $metakey, 'post' ) || ! current_user_can( 'add_post_meta', $post_ID, $metakey ) ) { 939 return false; 940 } 941 942 $metakey = wp_slash( $metakey ); 943 944 return add_post_meta( $post_ID, $metakey, $metavalue ); 945 } 946 947 return false; 948 } 949 950 /** 951 * Delete post meta data by meta ID. 952 * 953 * @since 1.2.0 954 * 955 * @param int $mid 956 * @return bool 957 */ 958 function delete_meta( $mid ) { 959 return delete_metadata_by_mid( 'post', $mid ); 960 } 961 962 /** 963 * Get a list of previously defined keys. 964 * 965 * @since 1.2.0 966 * 967 * @global wpdb $wpdb WordPress database abstraction object. 968 * 969 * @return mixed 970 */ 971 function get_meta_keys() { 972 global $wpdb; 973 974 $keys = $wpdb->get_col( 975 " 976 SELECT meta_key 977 FROM $wpdb->postmeta 978 GROUP BY meta_key 979 ORDER BY meta_key" 980 ); 981 982 return $keys; 983 } 984 985 /** 986 * Get post meta data by meta ID. 987 * 988 * @since 2.1.0 989 * 990 * @param int $mid 991 * @return object|bool 992 */ 993 function get_post_meta_by_id( $mid ) { 994 return get_metadata_by_mid( 'post', $mid ); 995 } 996 997 /** 998 * Get meta data for the given post ID. 999 * 1000 * @since 1.2.0 1001 * 1002 * @global wpdb $wpdb WordPress database abstraction object. 1003 * 1004 * @param int $postid 1005 * @return mixed 1006 */ 1007 function has_meta( $postid ) { 1008 global $wpdb; 1009 1010 return $wpdb->get_results( 1011 $wpdb->prepare( 1012 "SELECT meta_key, meta_value, meta_id, post_id 1013 FROM $wpdb->postmeta WHERE post_id = %d 1014 ORDER BY meta_key,meta_id", 1015 $postid 1016 ), 1017 ARRAY_A 1018 ); 1019 } 1020 1021 /** 1022 * Update post meta data by meta ID. 1023 * 1024 * @since 1.2.0 1025 * 1026 * @param int $meta_id 1027 * @param string $meta_key Expect Slashed 1028 * @param string $meta_value Expect Slashed 1029 * @return bool 1030 */ 1031 function update_meta( $meta_id, $meta_key, $meta_value ) { 1032 $meta_key = wp_unslash( $meta_key ); 1033 $meta_value = wp_unslash( $meta_value ); 1034 1035 return update_metadata_by_mid( 'post', $meta_id, $meta_value, $meta_key ); 1036 } 1037 1038 // 1039 // Private. 1040 // 1041 1042 /** 1043 * Replace hrefs of attachment anchors with up-to-date permalinks. 1044 * 1045 * @since 2.3.0 1046 * @access private 1047 * 1048 * @param int|object $post Post ID or post object. 1049 * @return void|int|WP_Error Void if nothing fixed. 0 or WP_Error on update failure. The post ID on update success. 1050 */ 1051 function _fix_attachment_links( $post ) { 1052 $post = get_post( $post, ARRAY_A ); 1053 $content = $post['post_content']; 1054 1055 // Don't run if no pretty permalinks or post is not published, scheduled, or privately published. 1056 if ( ! get_option( 'permalink_structure' ) || ! in_array( $post['post_status'], array( 'publish', 'future', 'private' ), true ) ) { 1057 return; 1058 } 1059 1060 // Short if there aren't any links or no '?attachment_id=' strings (strpos cannot be zero). 1061 if ( ! strpos( $content, '?attachment_id=' ) || ! preg_match_all( '/<a ([^>]+)>[\s\S]+?<\/a>/', $content, $link_matches ) ) { 1062 return; 1063 } 1064 1065 $site_url = get_bloginfo( 'url' ); 1066 $site_url = substr( $site_url, (int) strpos( $site_url, '://' ) ); // Remove the http(s). 1067 $replace = ''; 1068 1069 foreach ( $link_matches[1] as $key => $value ) { 1070 if ( ! strpos( $value, '?attachment_id=' ) || ! strpos( $value, 'wp-att-' ) 1071 || ! preg_match( '/href=(["\'])[^"\']*\?attachment_id=(\d+)[^"\']*\\1/', $value, $url_match ) 1072 || ! preg_match( '/rel=["\'][^"\']*wp-att-(\d+)/', $value, $rel_match ) ) { 1073 continue; 1074 } 1075 1076 $quote = $url_match[1]; // The quote (single or double). 1077 $url_id = (int) $url_match[2]; 1078 $rel_id = (int) $rel_match[1]; 1079 1080 if ( ! $url_id || ! $rel_id || $url_id != $rel_id || strpos( $url_match[0], $site_url ) === false ) { 1081 continue; 1082 } 1083 1084 $link = $link_matches[0][ $key ]; 1085 $replace = str_replace( $url_match[0], 'href=' . $quote . get_attachment_link( $url_id ) . $quote, $link ); 1086 1087 $content = str_replace( $link, $replace, $content ); 1088 } 1089 1090 if ( $replace ) { 1091 $post['post_content'] = $content; 1092 // Escape data pulled from DB. 1093 $post = add_magic_quotes( $post ); 1094 1095 return wp_update_post( $post ); 1096 } 1097 } 1098 1099 /** 1100 * Get all the possible statuses for a post_type 1101 * 1102 * @since 2.5.0 1103 * 1104 * @param string $type The post_type you want the statuses for. Default 'post'. 1105 * @return string[] An array of all the statuses for the supplied post type. 1106 */ 1107 function get_available_post_statuses( $type = 'post' ) { 1108 $stati = wp_count_posts( $type ); 1109 1110 return array_keys( get_object_vars( $stati ) ); 1111 } 1112 1113 /** 1114 * Run the wp query to fetch the posts for listing on the edit posts page 1115 * 1116 * @since 2.5.0 1117 * 1118 * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal. 1119 * @return array 1120 */ 1121 function wp_edit_posts_query( $q = false ) { 1122 if ( false === $q ) { 1123 $q = $_GET; 1124 } 1125 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; 1126 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; 1127 $post_stati = get_post_stati(); 1128 1129 if ( isset( $q['post_type'] ) && in_array( $q['post_type'], get_post_types(), true ) ) { 1130 $post_type = $q['post_type']; 1131 } else { 1132 $post_type = 'post'; 1133 } 1134 1135 $avail_post_stati = get_available_post_statuses( $post_type ); 1136 $post_status = ''; 1137 $perm = ''; 1138 1139 if ( isset( $q['post_status'] ) && in_array( $q['post_status'], $post_stati, true ) ) { 1140 $post_status = $q['post_status']; 1141 $perm = 'readable'; 1142 } 1143 1144 $orderby = ''; 1145 1146 if ( isset( $q['orderby'] ) ) { 1147 $orderby = $q['orderby']; 1148 } elseif ( isset( $q['post_status'] ) && in_array( $q['post_status'], array( 'pending', 'draft' ), true ) ) { 1149 $orderby = 'modified'; 1150 } 1151 1152 $order = ''; 1153 1154 if ( isset( $q['order'] ) ) { 1155 $order = $q['order']; 1156 } elseif ( isset( $q['post_status'] ) && 'pending' === $q['post_status'] ) { 1157 $order = 'ASC'; 1158 } 1159 1160 $per_page = "edit_{$post_type}_per_page"; 1161 $posts_per_page = (int) get_user_option( $per_page ); 1162 if ( empty( $posts_per_page ) || $posts_per_page < 1 ) { 1163 $posts_per_page = 20; 1164 } 1165 1166 /** 1167 * Filters the number of items per page to show for a specific 'per_page' type. 1168 * 1169 * The dynamic portion of the hook name, `$post_type`, refers to the post type. 1170 * 1171 * Some examples of filter hooks generated here include: 'edit_attachment_per_page', 1172 * 'edit_post_per_page', 'edit_page_per_page', etc. 1173 * 1174 * @since 3.0.0 1175 * 1176 * @param int $posts_per_page Number of posts to display per page for the given post 1177 * type. Default 20. 1178 */ 1179 $posts_per_page = apply_filters( "edit_{$post_type}_per_page", $posts_per_page ); 1180 1181 /** 1182 * Filters the number of posts displayed per page when specifically listing "posts". 1183 * 1184 * @since 2.8.0 1185 * 1186 * @param int $posts_per_page Number of posts to be displayed. Default 20. 1187 * @param string $post_type The post type. 1188 */ 1189 $posts_per_page = apply_filters( 'edit_posts_per_page', $posts_per_page, $post_type ); 1190 1191 $query = compact( 'post_type', 'post_status', 'perm', 'order', 'orderby', 'posts_per_page' ); 1192 1193 // Hierarchical types require special args. 1194 if ( is_post_type_hierarchical( $post_type ) && empty( $orderby ) ) { 1195 $query['orderby'] = 'menu_order title'; 1196 $query['order'] = 'asc'; 1197 $query['posts_per_page'] = -1; 1198 $query['posts_per_archive_page'] = -1; 1199 $query['fields'] = 'id=>parent'; 1200 } 1201 1202 if ( ! empty( $q['show_sticky'] ) ) { 1203 $query['post__in'] = (array) get_option( 'sticky_posts' ); 1204 } 1205 1206 wp( $query ); 1207 1208 return $avail_post_stati; 1209 } 1210 1211 /** 1212 * Get the query variables for the current attachments request. 1213 * 1214 * @since 4.2.0 1215 * 1216 * @param array|false $q Optional. Array of query variables to use to build the query or false 1217 * to use $_GET superglobal. Default false. 1218 * @return array The parsed query vars. 1219 */ 1220 function wp_edit_attachments_query_vars( $q = false ) { 1221 if ( false === $q ) { 1222 $q = $_GET; 1223 } 1224 $q['m'] = isset( $q['m'] ) ? (int) $q['m'] : 0; 1225 $q['cat'] = isset( $q['cat'] ) ? (int) $q['cat'] : 0; 1226 $q['post_type'] = 'attachment'; 1227 $post_type = get_post_type_object( 'attachment' ); 1228 $states = 'inherit'; 1229 if ( current_user_can( $post_type->cap->read_private_posts ) ) { 1230 $states .= ',private'; 1231 } 1232 1233 $q['post_status'] = isset( $q['status'] ) && 'trash' === $q['status'] ? 'trash' : $states; 1234 $q['post_status'] = isset( $q['attachment-filter'] ) && 'trash' === $q['attachment-filter'] ? 'trash' : $states; 1235 1236 $media_per_page = (int) get_user_option( 'upload_per_page' ); 1237 if ( empty( $media_per_page ) || $media_per_page < 1 ) { 1238 $media_per_page = 20; 1239 } 1240 1241 /** 1242 * Filters the number of items to list per page when listing media items. 1243 * 1244 * @since 2.9.0 1245 * 1246 * @param int $media_per_page Number of media to list. Default 20. 1247 */ 1248 $q['posts_per_page'] = apply_filters( 'upload_per_page', $media_per_page ); 1249 1250 $post_mime_types = get_post_mime_types(); 1251 if ( isset( $q['post_mime_type'] ) && ! array_intersect( (array) $q['post_mime_type'], array_keys( $post_mime_types ) ) ) { 1252 unset( $q['post_mime_type'] ); 1253 } 1254 1255 foreach ( array_keys( $post_mime_types ) as $type ) { 1256 if ( isset( $q['attachment-filter'] ) && "post_mime_type:$type" === $q['attachment-filter'] ) { 1257 $q['post_mime_type'] = $type; 1258 break; 1259 } 1260 } 1261 1262 if ( isset( $q['detached'] ) || ( isset( $q['attachment-filter'] ) && 'detached' === $q['attachment-filter'] ) ) { 1263 $q['post_parent'] = 0; 1264 } 1265 1266 if ( isset( $q['mine'] ) || ( isset( $q['attachment-filter'] ) && 'mine' === $q['attachment-filter'] ) ) { 1267 $q['author'] = get_current_user_id(); 1268 } 1269 1270 // Filter query clauses to include filenames. 1271 if ( isset( $q['s'] ) ) { 1272 add_filter( 'posts_clauses', '_filter_query_attachment_filenames' ); 1273 } 1274 1275 return $q; 1276 } 1277 1278 /** 1279 * Executes a query for attachments. An array of WP_Query arguments 1280 * can be passed in, which will override the arguments set by this function. 1281 * 1282 * @since 2.5.0 1283 * 1284 * @param array|false $q Array of query variables to use to build the query or false to use $_GET superglobal. 1285 * @return array 1286 */ 1287 function wp_edit_attachments_query( $q = false ) { 1288 wp( wp_edit_attachments_query_vars( $q ) ); 1289 1290 $post_mime_types = get_post_mime_types(); 1291 $avail_post_mime_types = get_available_post_mime_types( 'attachment' ); 1292 1293 return array( $post_mime_types, $avail_post_mime_types ); 1294 } 1295 1296 /** 1297 * Returns the list of classes to be used by a meta box. 1298 * 1299 * @since 2.5.0 1300 * 1301 * @param string $box_id Meta box ID (used in the 'id' attribute for the meta box). 1302 * @param string $screen_id The screen on which the meta box is shown. 1303 * @return string Space-separated string of class names. 1304 */ 1305 function postbox_classes( $box_id, $screen_id ) { 1306 if ( isset( $_GET['edit'] ) && $_GET['edit'] == $box_id ) { 1307 $classes = array( '' ); 1308 } elseif ( get_user_option( 'closedpostboxes_' . $screen_id ) ) { 1309 $closed = get_user_option( 'closedpostboxes_' . $screen_id ); 1310 if ( ! is_array( $closed ) ) { 1311 $classes = array( '' ); 1312 } else { 1313 $classes = in_array( $box_id, $closed, true ) ? array( 'closed' ) : array( '' ); 1314 } 1315 } else { 1316 $classes = array( '' ); 1317 } 1318 1319 /** 1320 * Filters the postbox classes for a specific screen and box ID combo. 1321 * 1322 * The dynamic portions of the hook name, `$screen_id` and `$box_id`, refer to 1323 * the screen ID and meta box ID, respectively. 1324 * 1325 * @since 3.2.0 1326 * 1327 * @param string[] $classes An array of postbox classes. 1328 */ 1329 $classes = apply_filters( "postbox_classes_{$screen_id}_{$box_id}", $classes ); 1330 return implode( ' ', $classes ); 1331 } 1332 1333 /** 1334 * Get a sample permalink based off of the post name. 1335 * 1336 * @since 2.5.0 1337 * 1338 * @param int|WP_Post $id Post ID or post object. 1339 * @param string $title Optional. Title to override the post's current title when generating the post name. Default null. 1340 * @param string $name Optional. Name to override the post name. Default null. 1341 * @return array { 1342 * Array containing the sample permalink with placeholder for the post name, and the post name. 1343 * 1344 * @type string $0 The permalink with placeholder for the post name. 1345 * @type string $1 The post name. 1346 * } 1347 */ 1348 function get_sample_permalink( $id, $title = null, $name = null ) { 1349 $post = get_post( $id ); 1350 if ( ! $post ) { 1351 return array( '', '' ); 1352 } 1353 1354 $ptype = get_post_type_object( $post->post_type ); 1355 1356 $original_status = $post->post_status; 1357 $original_date = $post->post_date; 1358 $original_name = $post->post_name; 1359 1360 // Hack: get_permalink() would return ugly permalink for drafts, so we will fake that our post is published. 1361 if ( in_array( $post->post_status, array( 'draft', 'pending', 'future' ), true ) ) { 1362 $post->post_status = 'publish'; 1363 $post->post_name = sanitize_title( $post->post_name ? $post->post_name : $post->post_title, $post->ID ); 1364 } 1365 1366 // If the user wants to set a new name -- override the current one. 1367 // Note: if empty name is supplied -- use the title instead, see #6072. 1368 if ( ! is_null( $name ) ) { 1369 $post->post_name = sanitize_title( $name ? $name : $title, $post->ID ); 1370 } 1371 1372 $post->post_name = wp_unique_post_slug( $post->post_name, $post->ID, $post->post_status, $post->post_type, $post->post_parent ); 1373 1374 $post->filter = 'sample'; 1375 1376 $permalink = get_permalink( $post, true ); 1377 1378 // Replace custom post_type token with generic pagename token for ease of use. 1379 $permalink = str_replace( "%$post->post_type%", '%pagename%', $permalink ); 1380 1381 // Handle page hierarchy. 1382 if ( $ptype->hierarchical ) { 1383 $uri = get_page_uri( $post ); 1384 if ( $uri ) { 1385 $uri = untrailingslashit( $uri ); 1386 $uri = strrev( stristr( strrev( $uri ), '/' ) ); 1387 $uri = untrailingslashit( $uri ); 1388 } 1389 1390 /** This filter is documented in wp-admin/edit-tag-form.php */ 1391 $uri = apply_filters( 'editable_slug', $uri, $post ); 1392 if ( ! empty( $uri ) ) { 1393 $uri .= '/'; 1394 } 1395 $permalink = str_replace( '%pagename%', "{$uri}%pagename%", $permalink ); 1396 } 1397 1398 /** This filter is documented in wp-admin/edit-tag-form.php */ 1399 $permalink = array( $permalink, apply_filters( 'editable_slug', $post->post_name, $post ) ); 1400 $post->post_status = $original_status; 1401 $post->post_date = $original_date; 1402 $post->post_name = $original_name; 1403 unset( $post->filter ); 1404 1405 /** 1406 * Filters the sample permalink. 1407 * 1408 * @since 4.4.0 1409 * 1410 * @param array $permalink { 1411 * Array containing the sample permalink with placeholder for the post name, and the post name. 1412 * 1413 * @type string $0 The permalink with placeholder for the post name. 1414 * @type string $1 The post name. 1415 * } 1416 * @param int $post_id Post ID. 1417 * @param string $title Post title. 1418 * @param string $name Post name (slug). 1419 * @param WP_Post $post Post object. 1420 */ 1421 return apply_filters( 'get_sample_permalink', $permalink, $post->ID, $title, $name, $post ); 1422 } 1423 1424 /** 1425 * Returns the HTML of the sample permalink slug editor. 1426 * 1427 * @since 2.5.0 1428 * 1429 * @param int $id Post ID or post object. 1430 * @param string $new_title Optional. New title. Default null. 1431 * @param string $new_slug Optional. New slug. Default null. 1432 * @return string The HTML of the sample permalink slug editor. 1433 */ 1434 function get_sample_permalink_html( $id, $new_title = null, $new_slug = null ) { 1435 $post = get_post( $id ); 1436 if ( ! $post ) { 1437 return ''; 1438 } 1439 1440 list($permalink, $post_name) = get_sample_permalink( $post->ID, $new_title, $new_slug ); 1441 1442 $view_link = false; 1443 $preview_target = ''; 1444 1445 if ( current_user_can( 'read_post', $post->ID ) ) { 1446 if ( 'draft' === $post->post_status || empty( $post->post_name ) ) { 1447 $view_link = get_preview_post_link( $post ); 1448 $preview_target = " target='wp-preview-{$post->ID}'"; 1449 } else { 1450 if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) { 1451 $view_link = get_permalink( $post ); 1452 } else { 1453 // Allow non-published (private, future) to be viewed at a pretty permalink, in case $post->post_name is set. 1454 $view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink ); 1455 } 1456 } 1457 } 1458 1459 // Permalinks without a post/page name placeholder don't have anything to edit. 1460 if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) { 1461 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; 1462 1463 if ( false !== $view_link ) { 1464 $display_link = urldecode( $view_link ); 1465 $return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . esc_html( $display_link ) . "</a>\n"; 1466 } else { 1467 $return .= '<span id="sample-permalink">' . $permalink . "</span>\n"; 1468 } 1469 1470 // Encourage a pretty permalink setting. 1471 if ( ! get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) 1472 && ! ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) == $id ) 1473 ) { 1474 $return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __( 'Change Permalinks' ) . "</a></span>\n"; 1475 } 1476 } else { 1477 if ( mb_strlen( $post_name ) > 34 ) { 1478 $post_name_abridged = mb_substr( $post_name, 0, 16 ) . '…' . mb_substr( $post_name, -16 ); 1479 } else { 1480 $post_name_abridged = $post_name; 1481 } 1482 1483 $post_name_html = '<span id="editable-post-name">' . esc_html( $post_name_abridged ) . '</span>'; 1484 $display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, esc_html( urldecode( $permalink ) ) ); 1485 1486 $return = '<strong>' . __( 'Permalink:' ) . "</strong>\n"; 1487 $return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n"; 1488 $return .= '‎'; // Fix bi-directional text display defect in RTL languages. 1489 $return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n"; 1490 $return .= '<span id="editable-post-name-full">' . esc_html( $post_name ) . "</span>\n"; 1491 } 1492 1493 /** 1494 * Filters the sample permalink HTML markup. 1495 * 1496 * @since 2.9.0 1497 * @since 4.4.0 Added `$post` parameter. 1498 * 1499 * @param string $return Sample permalink HTML markup. 1500 * @param int $post_id Post ID. 1501 * @param string $new_title New sample permalink title. 1502 * @param string $new_slug New sample permalink slug. 1503 * @param WP_Post $post Post object. 1504 */ 1505 $return = apply_filters( 'get_sample_permalink_html', $return, $post->ID, $new_title, $new_slug, $post ); 1506 1507 return $return; 1508 } 1509 1510 /** 1511 * Returns HTML for the post thumbnail meta box. 1512 * 1513 * @since 2.9.0 1514 * 1515 * @param int $thumbnail_id ID of the attachment used for thumbnail 1516 * @param int|WP_Post $post Optional. The post ID or object associated with the thumbnail, defaults to global $post. 1517 * @return string The post thumbnail HTML. 1518 */ 1519 function _wp_post_thumbnail_html( $thumbnail_id = null, $post = null ) { 1520 $_wp_additional_image_sizes = wp_get_additional_image_sizes(); 1521 1522 $post = get_post( $post ); 1523 $post_type_object = get_post_type_object( $post->post_type ); 1524 $set_thumbnail_link = '<p class="hide-if-no-js"><a href="%s" id="set-post-thumbnail"%s class="thickbox">%s</a></p>'; 1525 $upload_iframe_src = get_upload_iframe_src( 'image', $post->ID ); 1526 1527 $content = sprintf( 1528 $set_thumbnail_link, 1529 esc_url( $upload_iframe_src ), 1530 '', // Empty when there's no featured image set, `aria-describedby` attribute otherwise. 1531 esc_html( $post_type_object->labels->set_featured_image ) 1532 ); 1533 1534 if ( $thumbnail_id && get_post( $thumbnail_id ) ) { 1535 $size = isset( $_wp_additional_image_sizes['post-thumbnail'] ) ? 'post-thumbnail' : array( 266, 266 ); 1536 1537 /** 1538 * Filters the size used to display the post thumbnail image in the 'Featured image' meta box. 1539 * 1540 * Note: When a theme adds 'post-thumbnail' support, a special 'post-thumbnail' 1541 * image size is registered, which differs from the 'thumbnail' image size 1542 * managed via the Settings > Media screen. 1543 * 1544 * @since 4.4.0 1545 * 1546 * @param string|int[] $size Requested image size. Can be any registered image size name, or 1547 * an array of width and height values in pixels (in that order). 1548 * @param int $thumbnail_id Post thumbnail attachment ID. 1549 * @param WP_Post $post The post object associated with the thumbnail. 1550 */ 1551 $size = apply_filters( 'admin_post_thumbnail_size', $size, $thumbnail_id, $post ); 1552 1553 $thumbnail_html = wp_get_attachment_image( $thumbnail_id, $size ); 1554 1555 if ( ! empty( $thumbnail_html ) ) { 1556 $content = sprintf( 1557 $set_thumbnail_link, 1558 esc_url( $upload_iframe_src ), 1559 ' aria-describedby="set-post-thumbnail-desc"', 1560 $thumbnail_html 1561 ); 1562 $content .= '<p class="hide-if-no-js howto" id="set-post-thumbnail-desc">' . __( 'Click the image to edit or update' ) . '</p>'; 1563 $content .= '<p class="hide-if-no-js"><a href="#" id="remove-post-thumbnail">' . esc_html( $post_type_object->labels->remove_featured_image ) . '</a></p>'; 1564 } 1565 } 1566 1567 $content .= '<input type="hidden" id="_thumbnail_id" name="_thumbnail_id" value="' . esc_attr( $thumbnail_id ? $thumbnail_id : '-1' ) . '" />'; 1568 1569 /** 1570 * Filters the admin post thumbnail HTML markup to return. 1571 * 1572 * @since 2.9.0 1573 * @since 3.5.0 Added the `$post_id` parameter. 1574 * @since 4.6.0 Added the `$thumbnail_id` parameter. 1575 * 1576 * @param string $content Admin post thumbnail HTML markup. 1577 * @param int $post_id Post ID. 1578 * @param int|null $thumbnail_id Thumbnail attachment ID, or null if there isn't one. 1579 */ 1580 return apply_filters( 'admin_post_thumbnail_html', $content, $post->ID, $thumbnail_id ); 1581 } 1582 1583 /** 1584 * Check to see if the post is currently being edited by another user. 1585 * 1586 * @since 2.5.0 1587 * 1588 * @param int|WP_Post $post_id ID or object of the post to check for editing. 1589 * @return int|false ID of the user with lock. False if the post does not exist, post is not locked, 1590 * the user with lock does not exist, or the post is locked by current user. 1591 */ 1592 function wp_check_post_lock( $post_id ) { 1593 $post = get_post( $post_id ); 1594 if ( ! $post ) { 1595 return false; 1596 } 1597 1598 $lock = get_post_meta( $post->ID, '_edit_lock', true ); 1599 if ( ! $lock ) { 1600 return false; 1601 } 1602 1603 $lock = explode( ':', $lock ); 1604 $time = $lock[0]; 1605 $user = isset( $lock[1] ) ? $lock[1] : get_post_meta( $post->ID, '_edit_last', true ); 1606 1607 if ( ! get_userdata( $user ) ) { 1608 return false; 1609 } 1610 1611 /** This filter is documented in wp-admin/includes/ajax-actions.php */ 1612 $time_window = apply_filters( 'wp_check_post_lock_window', 150 ); 1613 1614 if ( $time && $time > time() - $time_window && get_current_user_id() != $user ) { 1615 return $user; 1616 } 1617 1618 return false; 1619 } 1620 1621 /** 1622 * Mark the post as currently being edited by the current user 1623 * 1624 * @since 2.5.0 1625 * 1626 * @param int|WP_Post $post_id ID or object of the post being edited. 1627 * @return array|false Array of the lock time and user ID. False if the post does not exist, or 1628 * there is no current user. 1629 */ 1630 function wp_set_post_lock( $post_id ) { 1631 $post = get_post( $post_id ); 1632 if ( ! $post ) { 1633 return false; 1634 } 1635 1636 $user_id = get_current_user_id(); 1637 if ( 0 == $user_id ) { 1638 return false; 1639 } 1640 1641 $now = time(); 1642 $lock = "$now:$user_id"; 1643 1644 update_post_meta( $post->ID, '_edit_lock', $lock ); 1645 1646 return array( $now, $user_id ); 1647 } 1648 1649 /** 1650 * Outputs the HTML for the notice to say that someone else is editing or has taken over editing of this post. 1651 * 1652 * @since 2.8.5 1653 */ 1654 function _admin_notice_post_locked() { 1655 $post = get_post(); 1656 if ( ! $post ) { 1657 return; 1658 } 1659 1660 $user = null; 1661 $user_id = wp_check_post_lock( $post->ID ); 1662 if ( $user_id ) { 1663 $user = get_userdata( $user_id ); 1664 } 1665 1666 if ( $user ) { 1667 /** 1668 * Filters whether to show the post locked dialog. 1669 * 1670 * Returning false from the filter will prevent the dialog from being displayed. 1671 * 1672 * @since 3.6.0 1673 * 1674 * @param bool $display Whether to display the dialog. Default true. 1675 * @param WP_Post $post Post object. 1676 * @param WP_User $user The user with the lock for the post. 1677 */ 1678 if ( ! apply_filters( 'show_post_locked_dialog', true, $post, $user ) ) { 1679 return; 1680 } 1681 1682 $locked = true; 1683 } else { 1684 $locked = false; 1685 } 1686 1687 $sendback = wp_get_referer(); 1688 if ( $locked && $sendback && false === strpos( $sendback, 'post.php' ) && false === strpos( $sendback, 'post-new.php' ) ) { 1689 1690 $sendback_text = __( 'Go back' ); 1691 } else { 1692 $sendback = admin_url( 'edit.php' ); 1693 1694 if ( 'post' !== $post->post_type ) { 1695 $sendback = add_query_arg( 'post_type', $post->post_type, $sendback ); 1696 } 1697 1698 $sendback_text = get_post_type_object( $post->post_type )->labels->all_items; 1699 } 1700 1701 $hidden = $locked ? '' : ' hidden'; 1702 1703 ?> 1704 <div id="post-lock-dialog" class="notification-dialog-wrap<?php echo $hidden; ?>"> 1705 <div class="notification-dialog-background"></div> 1706 <div class="notification-dialog"> 1707 <?php 1708 1709 if ( $locked ) { 1710 $query_args = array(); 1711 if ( get_post_type_object( $post->post_type )->public ) { 1712 if ( 'publish' === $post->post_status || $user->ID != $post->post_author ) { 1713 // Latest content is in autosave. 1714 $nonce = wp_create_nonce( 'post_preview_' . $post->ID ); 1715 $query_args['preview_id'] = $post->ID; 1716 $query_args['preview_nonce'] = $nonce; 1717 } 1718 } 1719 1720 $preview_link = get_preview_post_link( $post->ID, $query_args ); 1721 1722 /** 1723 * Filters whether to allow the post lock to be overridden. 1724 * 1725 * Returning false from the filter will disable the ability 1726 * to override the post lock. 1727 * 1728 * @since 3.6.0 1729 * 1730 * @param bool $override Whether to allow the post lock to be overridden. Default true. 1731 * @param WP_Post $post Post object. 1732 * @param WP_User $user The user with the lock for the post. 1733 */ 1734 $override = apply_filters( 'override_post_lock', true, $post, $user ); 1735 $tab_last = $override ? '' : ' wp-tab-last'; 1736 1737 ?> 1738 <div class="post-locked-message"> 1739 <div class="post-locked-avatar"><?php echo get_avatar( $user->ID, 64 ); ?></div> 1740 <p class="currently-editing wp-tab-first" tabindex="0"> 1741 <?php 1742 if ( $override ) { 1743 /* translators: %s: User's display name. */ 1744 printf( __( '%s is currently editing this post. Do you want to take over?' ), esc_html( $user->display_name ) ); 1745 } else { 1746 /* translators: %s: User's display name. */ 1747 printf( __( '%s is currently editing this post.' ), esc_html( $user->display_name ) ); 1748 } 1749 ?> 1750 </p> 1751 <?php 1752 /** 1753 * Fires inside the post locked dialog before the buttons are displayed. 1754 * 1755 * @since 3.6.0 1756 * @since 5.4.0 The $user parameter was added. 1757 * 1758 * @param WP_Post $post Post object. 1759 * @param WP_User $user The user with the lock for the post. 1760 */ 1761 do_action( 'post_locked_dialog', $post, $user ); 1762 ?> 1763 <p> 1764 <a class="button" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a> 1765 <?php if ( $preview_link ) { ?> 1766 <a class="button<?php echo $tab_last; ?>" href="<?php echo esc_url( $preview_link ); ?>"><?php _e( 'Preview' ); ?></a> 1767 <?php 1768 } 1769 1770 // Allow plugins to prevent some users overriding the post lock. 1771 if ( $override ) { 1772 ?> 1773 <a class="button button-primary wp-tab-last" href="<?php echo esc_url( add_query_arg( 'get-post-lock', '1', wp_nonce_url( get_edit_post_link( $post->ID, 'url' ), 'lock-post_' . $post->ID ) ) ); ?>"><?php _e( 'Take over' ); ?></a> 1774 <?php 1775 } 1776 1777 ?> 1778 </p> 1779 </div> 1780 <?php 1781 } else { 1782 ?> 1783 <div class="post-taken-over"> 1784 <div class="post-locked-avatar"></div> 1785 <p class="wp-tab-first" tabindex="0"> 1786 <span class="currently-editing"></span><br /> 1787 <span class="locked-saving hidden"><img src="<?php echo esc_url( admin_url( 'images/spinner-2x.gif' ) ); ?>" width="16" height="16" alt="" /> <?php _e( 'Saving revision…' ); ?></span> 1788 <span class="locked-saved hidden"><?php _e( 'Your latest changes were saved as a revision.' ); ?></span> 1789 </p> 1790 <?php 1791 /** 1792 * Fires inside the dialog displayed when a user has lost the post lock. 1793 * 1794 * @since 3.6.0 1795 * 1796 * @param WP_Post $post Post object. 1797 */ 1798 do_action( 'post_lock_lost_dialog', $post ); 1799 ?> 1800 <p><a class="button button-primary wp-tab-last" href="<?php echo esc_url( $sendback ); ?>"><?php echo $sendback_text; ?></a></p> 1801 </div> 1802 <?php 1803 } 1804 1805 ?> 1806 </div> 1807 </div> 1808 <?php 1809 } 1810 1811 /** 1812 * Creates autosave data for the specified post from $_POST data. 1813 * 1814 * @since 2.6.0 1815 * 1816 * @param array|int $post_data Associative array containing the post data or int post ID. 1817 * @return int|WP_Error The autosave revision ID. WP_Error or 0 on error. 1818 */ 1819 function wp_create_post_autosave( $post_data ) { 1820 if ( is_numeric( $post_data ) ) { 1821 $post_id = $post_data; 1822 $post_data = $_POST; 1823 } else { 1824 $post_id = (int) $post_data['post_ID']; 1825 } 1826 1827 $post_data = _wp_translate_postdata( true, $post_data ); 1828 if ( is_wp_error( $post_data ) ) { 1829 return $post_data; 1830 } 1831 $post_data = _wp_get_allowed_postdata( $post_data ); 1832 1833 $post_author = get_current_user_id(); 1834 1835 // Store one autosave per author. If there is already an autosave, overwrite it. 1836 $old_autosave = wp_get_post_autosave( $post_id, $post_author ); 1837 if ( $old_autosave ) { 1838 $new_autosave = _wp_post_revision_data( $post_data, true ); 1839 $new_autosave['ID'] = $old_autosave->ID; 1840 $new_autosave['post_author'] = $post_author; 1841 1842 $post = get_post( $post_id ); 1843 1844 // If the new autosave has the same content as the post, delete the autosave. 1845 $autosave_is_different = false; 1846 foreach ( array_intersect( array_keys( $new_autosave ), array_keys( _wp_post_revision_fields( $post ) ) ) as $field ) { 1847 if ( normalize_whitespace( $new_autosave[ $field ] ) !== normalize_whitespace( $post->$field ) ) { 1848 $autosave_is_different = true; 1849 break; 1850 } 1851 } 1852 1853 if ( ! $autosave_is_different ) { 1854 wp_delete_post_revision( $old_autosave->ID ); 1855 return 0; 1856 } 1857 1858 /** 1859 * Fires before an autosave is stored. 1860 * 1861 * @since 4.1.0 1862 * 1863 * @param array $new_autosave Post array - the autosave that is about to be saved. 1864 */ 1865 do_action( 'wp_creating_autosave', $new_autosave ); 1866 1867 return wp_update_post( $new_autosave ); 1868 } 1869 1870 // _wp_put_post_revision() expects unescaped. 1871 $post_data = wp_unslash( $post_data ); 1872 1873 // Otherwise create the new autosave as a special post revision. 1874 return _wp_put_post_revision( $post_data, true ); 1875 } 1876 1877 /** 1878 * Saves a draft or manually autosaves for the purpose of showing a post preview. 1879 * 1880 * @since 2.7.0 1881 * 1882 * @return string URL to redirect to show the preview. 1883 */ 1884 function post_preview() { 1885 1886 $post_ID = (int) $_POST['post_ID']; 1887 $_POST['ID'] = $post_ID; 1888 1889 $post = get_post( $post_ID ); 1890 if ( ! $post ) { 1891 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 1892 } 1893 1894 if ( ! current_user_can( 'edit_post', $post->ID ) ) { 1895 wp_die( __( 'Sorry, you are not allowed to edit this post.' ) ); 1896 } 1897 1898 $is_autosave = false; 1899 1900 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author 1901 && ( 'draft' === $post->post_status || 'auto-draft' === $post->post_status ) 1902 ) { 1903 $saved_post_id = edit_post(); 1904 } else { 1905 $is_autosave = true; 1906 1907 if ( isset( $_POST['post_status'] ) && 'auto-draft' === $_POST['post_status'] ) { 1908 $_POST['post_status'] = 'draft'; 1909 } 1910 1911 $saved_post_id = wp_create_post_autosave( $post->ID ); 1912 } 1913 1914 if ( is_wp_error( $saved_post_id ) ) { 1915 wp_die( $saved_post_id->get_error_message() ); 1916 } 1917 1918 $query_args = array(); 1919 1920 if ( $is_autosave && $saved_post_id ) { 1921 $query_args['preview_id'] = $post->ID; 1922 $query_args['preview_nonce'] = wp_create_nonce( 'post_preview_' . $post->ID ); 1923 1924 if ( isset( $_POST['post_format'] ) ) { 1925 $query_args['post_format'] = empty( $_POST['post_format'] ) ? 'standard' : sanitize_key( $_POST['post_format'] ); 1926 } 1927 1928 if ( isset( $_POST['_thumbnail_id'] ) ) { 1929 $query_args['_thumbnail_id'] = ( (int) $_POST['_thumbnail_id'] <= 0 ) ? '-1' : (int) $_POST['_thumbnail_id']; 1930 } 1931 } 1932 1933 return get_preview_post_link( $post, $query_args ); 1934 } 1935 1936 /** 1937 * Save a post submitted with XHR 1938 * 1939 * Intended for use with heartbeat and autosave.js 1940 * 1941 * @since 3.9.0 1942 * 1943 * @param array $post_data Associative array of the submitted post data. 1944 * @return mixed The value 0 or WP_Error on failure. The saved post ID on success. 1945 * The ID can be the draft post_id or the autosave revision post_id. 1946 */ 1947 function wp_autosave( $post_data ) { 1948 // Back-compat. 1949 if ( ! defined( 'DOING_AUTOSAVE' ) ) { 1950 define( 'DOING_AUTOSAVE', true ); 1951 } 1952 1953 $post_id = (int) $post_data['post_id']; 1954 $post_data['ID'] = $post_id; 1955 $post_data['post_ID'] = $post_id; 1956 1957 if ( false === wp_verify_nonce( $post_data['_wpnonce'], 'update-post_' . $post_id ) ) { 1958 return new WP_Error( 'invalid_nonce', __( 'Error while saving.' ) ); 1959 } 1960 1961 $post = get_post( $post_id ); 1962 1963 if ( ! current_user_can( 'edit_post', $post->ID ) ) { 1964 return new WP_Error( 'edit_posts', __( 'Sorry, you are not allowed to edit this item.' ) ); 1965 } 1966 1967 if ( 'auto-draft' === $post->post_status ) { 1968 $post_data['post_status'] = 'draft'; 1969 } 1970 1971 if ( 'page' !== $post_data['post_type'] && ! empty( $post_data['catslist'] ) ) { 1972 $post_data['post_category'] = explode( ',', $post_data['catslist'] ); 1973 } 1974 1975 if ( ! wp_check_post_lock( $post->ID ) && get_current_user_id() == $post->post_author 1976 && ( 'auto-draft' === $post->post_status || 'draft' === $post->post_status ) 1977 ) { 1978 // Drafts and auto-drafts are just overwritten by autosave for the same user if the post is not locked. 1979 return edit_post( wp_slash( $post_data ) ); 1980 } else { 1981 // Non-drafts or other users' drafts are not overwritten. 1982 // The autosave is stored in a special post revision for each user. 1983 return wp_create_post_autosave( wp_slash( $post_data ) ); 1984 } 1985 } 1986 1987 /** 1988 * Redirect to previous page. 1989 * 1990 * @since 2.7.0 1991 * 1992 * @param int $post_id Optional. Post ID. 1993 */ 1994 function redirect_post( $post_id = '' ) { 1995 if ( isset( $_POST['save'] ) || isset( $_POST['publish'] ) ) { 1996 $status = get_post_status( $post_id ); 1997 1998 if ( isset( $_POST['publish'] ) ) { 1999 switch ( $status ) { 2000 case 'pending': 2001 $message = 8; 2002 break; 2003 case 'future': 2004 $message = 9; 2005 break; 2006 default: 2007 $message = 6; 2008 } 2009 } else { 2010 $message = 'draft' === $status ? 10 : 1; 2011 } 2012 2013 $location = add_query_arg( 'message', $message, get_edit_post_link( $post_id, 'url' ) ); 2014 } elseif ( isset( $_POST['addmeta'] ) && $_POST['addmeta'] ) { 2015 $location = add_query_arg( 'message', 2, wp_get_referer() ); 2016 $location = explode( '#', $location ); 2017 $location = $location[0] . '#postcustom'; 2018 } elseif ( isset( $_POST['deletemeta'] ) && $_POST['deletemeta'] ) { 2019 $location = add_query_arg( 'message', 3, wp_get_referer() ); 2020 $location = explode( '#', $location ); 2021 $location = $location[0] . '#postcustom'; 2022 } else { 2023 $location = add_query_arg( 'message', 4, get_edit_post_link( $post_id, 'url' ) ); 2024 } 2025 2026 /** 2027 * Filters the post redirect destination URL. 2028 * 2029 * @since 2.9.0 2030 * 2031 * @param string $location The destination URL. 2032 * @param int $post_id The post ID. 2033 */ 2034 wp_redirect( apply_filters( 'redirect_post_location', $location, $post_id ) ); 2035 exit; 2036 } 2037 2038 /** 2039 * Sanitizes POST values from a checkbox taxonomy metabox. 2040 * 2041 * @since 5.1.0 2042 * 2043 * @param string $taxonomy The taxonomy name. 2044 * @param array $terms Raw term data from the 'tax_input' field. 2045 * @return int[] Array of sanitized term IDs. 2046 */ 2047 function taxonomy_meta_box_sanitize_cb_checkboxes( $taxonomy, $terms ) { 2048 return array_map( 'intval', $terms ); 2049 } 2050 2051 /** 2052 * Sanitizes POST values from an input taxonomy metabox. 2053 * 2054 * @since 5.1.0 2055 * 2056 * @param string $taxonomy The taxonomy name. 2057 * @param array|string $terms Raw term data from the 'tax_input' field. 2058 * @return array 2059 */ 2060 function taxonomy_meta_box_sanitize_cb_input( $taxonomy, $terms ) { 2061 /* 2062 * Assume that a 'tax_input' string is a comma-separated list of term names. 2063 * Some languages may use a character other than a comma as a delimiter, so we standardize on 2064 * commas before parsing the list. 2065 */ 2066 if ( ! is_array( $terms ) ) { 2067 $comma = _x( ',', 'tag delimiter' ); 2068 if ( ',' !== $comma ) { 2069 $terms = str_replace( $comma, ',', $terms ); 2070 } 2071 $terms = explode( ',', trim( $terms, " \n\t\r\0\x0B," ) ); 2072 } 2073 2074 $clean_terms = array(); 2075 foreach ( $terms as $term ) { 2076 // Empty terms are invalid input. 2077 if ( empty( $term ) ) { 2078 continue; 2079 } 2080 2081 $_term = get_terms( 2082 array( 2083 'taxonomy' => $taxonomy, 2084 'name' => $term, 2085 'fields' => 'ids', 2086 'hide_empty' => false, 2087 ) 2088 ); 2089 2090 if ( ! empty( $_term ) ) { 2091 $clean_terms[] = (int) $_term[0]; 2092 } else { 2093 // No existing term was found, so pass the string. A new term will be created. 2094 $clean_terms[] = $term; 2095 } 2096 } 2097 2098 return $clean_terms; 2099 } 2100 2101 /** 2102 * Return whether the post can be edited in the block editor. 2103 * 2104 * @since 5.0.0 2105 * 2106 * @param int|WP_Post $post Post ID or WP_Post object. 2107 * @return bool Whether the post can be edited in the block editor. 2108 */ 2109 function use_block_editor_for_post( $post ) { 2110 $post = get_post( $post ); 2111 2112 if ( ! $post ) { 2113 return false; 2114 } 2115 2116 // We're in the meta box loader, so don't use the block editor. 2117 if ( isset( $_GET['meta-box-loader'] ) ) { 2118 check_admin_referer( 'meta-box-loader', 'meta-box-loader-nonce' ); 2119 return false; 2120 } 2121 2122 // The posts page can't be edited in the block editor. 2123 if ( absint( get_option( 'page_for_posts' ) ) === $post->ID && empty( $post->post_content ) ) { 2124 return false; 2125 } 2126 2127 $use_block_editor = use_block_editor_for_post_type( $post->post_type ); 2128 2129 /** 2130 * Filters whether a post is able to be edited in the block editor. 2131 * 2132 * @since 5.0.0 2133 * 2134 * @param bool $use_block_editor Whether the post can be edited or not. 2135 * @param WP_Post $post The post being checked. 2136 */ 2137 return apply_filters( 'use_block_editor_for_post', $use_block_editor, $post ); 2138 } 2139 2140 /** 2141 * Return whether a post type is compatible with the block editor. 2142 * 2143 * The block editor depends on the REST API, and if the post type is not shown in the 2144 * REST API, then it won't work with the block editor. 2145 * 2146 * @since 5.0.0 2147 * 2148 * @param string $post_type The post type. 2149 * @return bool Whether the post type can be edited with the block editor. 2150 */ 2151 function use_block_editor_for_post_type( $post_type ) { 2152 if ( ! post_type_exists( $post_type ) ) { 2153 return false; 2154 } 2155 2156 if ( ! post_type_supports( $post_type, 'editor' ) ) { 2157 return false; 2158 } 2159 2160 $post_type_object = get_post_type_object( $post_type ); 2161 if ( $post_type_object && ! $post_type_object->show_in_rest ) { 2162 return false; 2163 } 2164 2165 /** 2166 * Filters whether a post is able to be edited in the block editor. 2167 * 2168 * @since 5.0.0 2169 * 2170 * @param bool $use_block_editor Whether the post type can be edited or not. Default true. 2171 * @param string $post_type The post type being checked. 2172 */ 2173 return apply_filters( 'use_block_editor_for_post_type', true, $post_type ); 2174 } 2175 2176 /** 2177 * Returns all the block categories that will be shown in the block editor. 2178 * 2179 * @since 5.0.0 2180 * 2181 * @param WP_Post $post Post object. 2182 * @return array[] Array of block categories. 2183 */ 2184 function get_block_categories( $post ) { 2185 $default_categories = array( 2186 array( 2187 'slug' => 'text', 2188 'title' => _x( 'Text', 'block category' ), 2189 'icon' => null, 2190 ), 2191 array( 2192 'slug' => 'media', 2193 'title' => _x( 'Media', 'block category' ), 2194 'icon' => null, 2195 ), 2196 array( 2197 'slug' => 'design', 2198 'title' => _x( 'Design', 'block category' ), 2199 'icon' => null, 2200 ), 2201 array( 2202 'slug' => 'widgets', 2203 'title' => _x( 'Widgets', 'block category' ), 2204 'icon' => null, 2205 ), 2206 array( 2207 'slug' => 'embed', 2208 'title' => _x( 'Embeds', 'block category' ), 2209 'icon' => null, 2210 ), 2211 array( 2212 'slug' => 'reusable', 2213 'title' => _x( 'Reusable Blocks', 'block category' ), 2214 'icon' => null, 2215 ), 2216 ); 2217 2218 /** 2219 * Filters the default array of block categories. 2220 * 2221 * @since 5.0.0 2222 * 2223 * @param array[] $default_categories Array of block categories. 2224 * @param WP_Post $post Post being loaded. 2225 */ 2226 return apply_filters( 'block_categories', $default_categories, $post ); 2227 } 2228 2229 /** 2230 * Prepares server-registered blocks for the block editor. 2231 * 2232 * Returns an associative array of registered block data keyed by block name. Data includes properties 2233 * of a block relevant for client registration. 2234 * 2235 * @since 5.0.0 2236 * 2237 * @return array An associative array of registered block data. 2238 */ 2239 function get_block_editor_server_block_settings() { 2240 $block_registry = WP_Block_Type_Registry::get_instance(); 2241 $blocks = array(); 2242 $fields_to_pick = array( 2243 'title' => 'title', 2244 'description' => 'description', 2245 'icon' => 'icon', 2246 'category' => 'category', 2247 'keywords' => 'keywords', 2248 'parent' => 'parent', 2249 'supports' => 'supports', 2250 'attributes' => 'attributes', 2251 'provides_context' => 'providesContext', 2252 'uses_context' => 'usesContext', 2253 'styles' => 'styles', 2254 'textdomain' => 'textdomain', 2255 'example' => 'example', 2256 ); 2257 2258 foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { 2259 foreach ( $fields_to_pick as $field => $key ) { 2260 if ( ! isset( $block_type->{ $field } ) ) { 2261 continue; 2262 } 2263 2264 if ( ! isset( $blocks[ $block_name ] ) ) { 2265 $blocks[ $block_name ] = array(); 2266 } 2267 2268 $blocks[ $block_name ][ $key ] = $block_type->{ $field }; 2269 } 2270 } 2271 2272 return $blocks; 2273 } 2274 2275 /** 2276 * Renders the meta boxes forms. 2277 * 2278 * @since 5.0.0 2279 */ 2280 function the_block_editor_meta_boxes() { 2281 global $post, $current_screen, $wp_meta_boxes; 2282 2283 // Handle meta box state. 2284 $_original_meta_boxes = $wp_meta_boxes; 2285 2286 /** 2287 * Fires right before the meta boxes are rendered. 2288 * 2289 * This allows for the filtering of meta box data, that should already be 2290 * present by this point. Do not use as a means of adding meta box data. 2291 * 2292 * @since 5.0.0 2293 * 2294 * @param array $wp_meta_boxes Global meta box state. 2295 */ 2296 $wp_meta_boxes = apply_filters( 'filter_block_editor_meta_boxes', $wp_meta_boxes ); 2297 $locations = array( 'side', 'normal', 'advanced' ); 2298 $priorities = array( 'high', 'sorted', 'core', 'default', 'low' ); 2299 2300 // Render meta boxes. 2301 ?> 2302 <form class="metabox-base-form"> 2303 <?php the_block_editor_meta_box_post_form_hidden_fields( $post ); ?> 2304 </form> 2305 <form id="toggle-custom-fields-form" method="post" action="<?php echo esc_attr( admin_url( 'post.php' ) ); ?>"> 2306 <?php wp_nonce_field( 'toggle-custom-fields' ); ?> 2307 <input type="hidden" name="action" value="toggle-custom-fields" /> 2308 </form> 2309 <?php foreach ( $locations as $location ) : ?> 2310 <form class="metabox-location-<?php echo esc_attr( $location ); ?>" onsubmit="return false;"> 2311 <div id="poststuff" class="sidebar-open"> 2312 <div id="postbox-container-2" class="postbox-container"> 2313 <?php 2314 do_meta_boxes( 2315 $current_screen, 2316 $location, 2317 $post 2318 ); 2319 ?> 2320 </div> 2321 </div> 2322 </form> 2323 <?php endforeach; ?> 2324 <?php 2325 2326 $meta_boxes_per_location = array(); 2327 foreach ( $locations as $location ) { 2328 $meta_boxes_per_location[ $location ] = array(); 2329 2330 if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ] ) ) { 2331 continue; 2332 } 2333 2334 foreach ( $priorities as $priority ) { 2335 if ( ! isset( $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ] ) ) { 2336 continue; 2337 } 2338 2339 $meta_boxes = (array) $wp_meta_boxes[ $current_screen->id ][ $location ][ $priority ]; 2340 foreach ( $meta_boxes as $meta_box ) { 2341 if ( false == $meta_box || ! $meta_box['title'] ) { 2342 continue; 2343 } 2344 2345 // If a meta box is just here for back compat, don't show it in the block editor. 2346 if ( isset( $meta_box['args']['__back_compat_meta_box'] ) && $meta_box['args']['__back_compat_meta_box'] ) { 2347 continue; 2348 } 2349 2350 $meta_boxes_per_location[ $location ][] = array( 2351 'id' => $meta_box['id'], 2352 'title' => $meta_box['title'], 2353 ); 2354 } 2355 } 2356 } 2357 2358 /** 2359 * Sadly we probably can not add this data directly into editor settings. 2360 * 2361 * Some meta boxes need admin_head to fire for meta box registry. 2362 * admin_head fires after admin_enqueue_scripts, which is where we create our 2363 * editor instance. 2364 */ 2365 $script = 'window._wpLoadBlockEditor.then( function() { 2366 wp.data.dispatch( \'core/edit-post\' ).setAvailableMetaBoxesPerLocation( ' . wp_json_encode( $meta_boxes_per_location ) . ' ); 2367 } );'; 2368 2369 wp_add_inline_script( 'wp-edit-post', $script ); 2370 2371 /** 2372 * When `wp-edit-post` is output in the `<head>`, the inline script needs to be manually printed. Otherwise, 2373 * meta boxes will not display because inline scripts for `wp-edit-post` will not be printed again after this point. 2374 */ 2375 if ( wp_script_is( 'wp-edit-post', 'done' ) ) { 2376 printf( "<script type='text/javascript'>\n%s\n</script>\n", trim( $script ) ); 2377 } 2378 2379 /** 2380 * If the 'postcustom' meta box is enabled, then we need to perform some 2381 * extra initialization on it. 2382 */ 2383 $enable_custom_fields = (bool) get_user_meta( get_current_user_id(), 'enable_custom_fields', true ); 2384 if ( $enable_custom_fields ) { 2385 $script = "( function( $ ) { 2386 if ( $('#postcustom').length ) { 2387 $( '#the-list' ).wpList( { 2388 addBefore: function( s ) { 2389 s.data += '&post_id=$post->ID'; 2390 return s; 2391 }, 2392 addAfter: function() { 2393 $('table#list-table').show(); 2394 } 2395 }); 2396 } 2397 } )( jQuery );"; 2398 wp_enqueue_script( 'wp-lists' ); 2399 wp_add_inline_script( 'wp-lists', $script ); 2400 } 2401 2402 // Reset meta box data. 2403 $wp_meta_boxes = $_original_meta_boxes; 2404 } 2405 2406 /** 2407 * Renders the hidden form required for the meta boxes form. 2408 * 2409 * @since 5.0.0 2410 * 2411 * @param WP_Post $post Current post object. 2412 */ 2413 function the_block_editor_meta_box_post_form_hidden_fields( $post ) { 2414 $form_extra = ''; 2415 if ( 'auto-draft' === $post->post_status ) { 2416 $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />"; 2417 } 2418 $form_action = 'editpost'; 2419 $nonce_action = 'update-post_' . $post->ID; 2420 $form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr( $post->ID ) . "' />"; 2421 $referer = wp_get_referer(); 2422 $current_user = wp_get_current_user(); 2423 $user_id = $current_user->ID; 2424 wp_nonce_field( $nonce_action ); 2425 2426 /* 2427 * Some meta boxes hook into these actions to add hidden input fields in the classic post form. For backwards 2428 * compatibility, we can capture the output from these actions, and extract the hidden input fields. 2429 */ 2430 ob_start(); 2431 /** This filter is documented in wp-admin/edit-form-advanced.php */ 2432 do_action( 'edit_form_after_title', $post ); 2433 /** This filter is documented in wp-admin/edit-form-advanced.php */ 2434 do_action( 'edit_form_advanced', $post ); 2435 $classic_output = ob_get_clean(); 2436 2437 $classic_elements = wp_html_split( $classic_output ); 2438 $hidden_inputs = ''; 2439 foreach ( $classic_elements as $element ) { 2440 if ( 0 !== strpos( $element, '<input ' ) ) { 2441 continue; 2442 } 2443 2444 if ( preg_match( '/\stype=[\'"]hidden[\'"]\s/', $element ) ) { 2445 echo $element; 2446 } 2447 } 2448 ?> 2449 <input type="hidden" id="user-id" name="user_ID" value="<?php echo (int) $user_id; ?>" /> 2450 <input type="hidden" id="hiddenaction" name="action" value="<?php echo esc_attr( $form_action ); ?>" /> 2451 <input type="hidden" id="originalaction" name="originalaction" value="<?php echo esc_attr( $form_action ); ?>" /> 2452 <input type="hidden" id="post_type" name="post_type" value="<?php echo esc_attr( $post->post_type ); ?>" /> 2453 <input type="hidden" id="original_post_status" name="original_post_status" value="<?php echo esc_attr( $post->post_status ); ?>" /> 2454 <input type="hidden" id="referredby" name="referredby" value="<?php echo $referer ? esc_url( $referer ) : ''; ?>" /> 2455 2456 <?php 2457 if ( 'draft' !== get_post_status( $post ) ) { 2458 wp_original_referer_field( true, 'previous' ); 2459 } 2460 echo $form_extra; 2461 wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); 2462 wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); 2463 // Permalink title nonce. 2464 wp_nonce_field( 'samplepermalink', 'samplepermalinknonce', false ); 2465 2466 /** 2467 * Add hidden input fields to the meta box save form. 2468 * 2469 * Hook into this action to print `<input type="hidden" ... />` fields, which will be POSTed back to 2470 * the server when meta boxes are saved. 2471 * 2472 * @since 5.0.0 2473 * 2474 * @param WP_Post $post The post that is being edited. 2475 */ 2476 do_action( 'block_editor_meta_box_hidden_fields', $post ); 2477 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Jan 28 01:00:03 2021 | Cross-referenced by PHPXref 0.7.1 |