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