← All changes
|
json-endpoints/class.wpcom-json-api-update-post-endpoint.php
+21
-14
13.7.2
→
16.3-a.1
View file →
| @@ -8,8 +8,12 @@ | ||
| 8 | 8 | * Delete a post: /sites/%s/posts/%d/delete |
| 9 | 9 | * Restore a post: /sites/%s/posts/%d/restore |
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | + exit( 0 ); | |
| 14 | +} | |
| 15 | + | |
| 12 | 16 | new WPCOM_JSON_API_Update_Post_Endpoint( |
| 13 | 17 | array( |
| 14 | 18 | 'description' => 'Create a post.', |
| 15 | 19 | 'group' => 'posts', |
| @@ -196,8 +200,10 @@ | ||
| 196 | 200 | ); |
| 197 | 201 | |
| 198 | 202 | /** |
| 199 | 203 | * Update post endpoint class. |
| 204 | + * | |
| 205 | + * @phan-constructor-used-for-side-effects | |
| 200 | 206 | */ |
| 201 | 207 | class WPCOM_JSON_API_Update_Post_Endpoint extends WPCOM_JSON_API_Post_Endpoint { |
| 202 | 208 | /** |
| 203 | 209 | * WPCOM_JSON_API_Update_Post_Endpoint constructor. |
| @@ -256,9 +262,9 @@ | ||
| 256 | 262 | $args = $this->query_args(); |
| 257 | 263 | |
| 258 | 264 | // unhook publicize, it's hooked again later -- without this, skipping services is impossible. |
| 259 | 265 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 260 | - remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 ); | |
| 266 | + remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100 ); | |
| 261 | 267 | add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) ); |
| 262 | 268 | } |
| 263 | 269 | |
| 264 | 270 | if ( $new ) { |
| @@ -337,9 +343,9 @@ | ||
| 337 | 343 | if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && ! current_user_can( 'publish_post', $post->ID ) ) { |
| 338 | 344 | $input['status'] = 'pending'; |
| 339 | 345 | } |
| 340 | 346 | $last_status = $post->post_status; |
| 341 | - $new_status = isset( $input['status'] ) ? $input['status'] : $last_status; | |
| 347 | + $new_status = $input['status'] ?? $last_status; | |
| 342 | 348 | |
| 343 | 349 | // Make sure that drafts get the current date when transitioning to publish if not supplied in the post. |
| 344 | 350 | $date_in_past = ( strtotime( $post->post_date_gmt ) < time() ); |
| 345 | 351 | if ( 'publish' === $new_status && 'draft' === $last_status && ! isset( $input['date_gmt'] ) && $date_in_past ) { |
| @@ -475,12 +481,12 @@ | ||
| 475 | 481 | $insert['menu_order'] = $input['menu_order']; |
| 476 | 482 | unset( $input['menu_order'] ); |
| 477 | 483 | } |
| 478 | 484 | |
| 479 | - $publicize = isset( $input['publicize'] ) ? $input['publicize'] : null; | |
| 485 | + $publicize = $input['publicize'] ?? null; | |
| 480 | 486 | unset( $input['publicize'] ); |
| 481 | 487 | |
| 482 | - $publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null; | |
| 488 | + $publicize_custom_message = $input['publicize_message'] ?? null; | |
| 483 | 489 | unset( $input['publicize_message'] ); |
| 484 | 490 | |
| 485 | 491 | if ( isset( $input['featured_image'] ) ) { |
| 486 | 492 | $featured_image = trim( $input['featured_image'] ); |
| @@ -487,18 +493,18 @@ | ||
| 487 | 493 | $delete_featured_image = empty( $featured_image ); |
| 488 | 494 | unset( $input['featured_image'] ); |
| 489 | 495 | } |
| 490 | 496 | |
| 491 | - $metadata = isset( $input['metadata'] ) ? $input['metadata'] : null; | |
| 497 | + $metadata = $input['metadata'] ?? null; | |
| 492 | 498 | unset( $input['metadata'] ); |
| 493 | 499 | |
| 494 | - $likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null; | |
| 500 | + $likes = $input['likes_enabled'] ?? null; | |
| 495 | 501 | unset( $input['likes_enabled'] ); |
| 496 | 502 | |
| 497 | - $sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null; | |
| 503 | + $sharing = $input['sharing_enabled'] ?? null; | |
| 498 | 504 | unset( $input['sharing_enabled'] ); |
| 499 | 505 | |
| 500 | - $sticky = isset( $input['sticky'] ) ? $input['sticky'] : null; | |
| 506 | + $sticky = $input['sticky'] ?? null; | |
| 501 | 507 | unset( $input['sticky'] ); |
| 502 | 508 | |
| 503 | 509 | foreach ( $input as $key => $value ) { |
| 504 | 510 | $insert[ "post_$key" ] = $value; |
| @@ -536,8 +542,9 @@ | ||
| 536 | 542 | } |
| 537 | 543 | |
| 538 | 544 | $post_id = wp_insert_post( add_magic_quotes( $insert ), true ); |
| 539 | 545 | } else { |
| 546 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $post is set and validated several blocks earlier if $new (only set once) is falsy. | |
| 540 | 547 | $insert['ID'] = $post->ID; |
| 541 | 548 | |
| 542 | 549 | // wp_update_post ignores date unless edit_date is set |
| 543 | 550 | // See: https://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts |
| @@ -617,9 +624,9 @@ | ||
| 617 | 624 | |
| 618 | 625 | // Set sharing status of the post. |
| 619 | 626 | if ( $new ) { |
| 620 | 627 | $sharing_enabled = isset( $sharing ) ? (bool) $sharing : true; |
| 621 | - if ( false === $sharing_enabled ) { | |
| 628 | + if ( ! $sharing_enabled ) { | |
| 622 | 629 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
| 623 | 630 | } |
| 624 | 631 | } elseif ( isset( $sharing ) && true === $sharing ) { |
| 625 | 632 | delete_post_meta( $post_id, 'sharing_disabled' ); |
| @@ -749,9 +756,9 @@ | ||
| 749 | 756 | |
| 750 | 757 | $meta = (object) $meta; |
| 751 | 758 | |
| 752 | 759 | if ( |
| 753 | - in_array( $meta->key, Jetpack_SEO_Posts::POST_META_KEYS_ARRAY, true ) && | |
| 760 | + in_array( $meta->key ?? null, Jetpack_SEO_Posts::POST_META_KEYS_ARRAY, true ) && | |
| 754 | 761 | ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() |
| 755 | 762 | ) { |
| 756 | 763 | return new WP_Error( 'unauthorized', __( 'SEO tools are not enabled for this site.', 'jetpack' ), 403 ); |
| 757 | 764 | } |
| @@ -779,12 +786,12 @@ | ||
| 779 | 786 | continue; |
| 780 | 787 | } |
| 781 | 788 | } |
| 782 | 789 | |
| 783 | - $unslashed_meta_key = wp_unslash( $meta->key ); // should match what the final key will be. | |
| 784 | - $meta->key = wp_slash( $meta->key ); | |
| 785 | - $unslashed_existing_meta_key = wp_unslash( $existing_meta_item->meta_key ); | |
| 786 | - $existing_meta_item->meta_key = wp_slash( $existing_meta_item->meta_key ); | |
| 790 | + $unslashed_meta_key = isset( $meta->key ) ? wp_unslash( $meta->key ) : null; // should match what the final key will be. | |
| 791 | + $meta->key = isset( $meta->key ) ? wp_slash( $meta->key ) : null; | |
| 792 | + $unslashed_existing_meta_key = isset( $existing_meta_item->meta_key ) ? wp_unslash( $existing_meta_item->meta_key ) : null; | |
| 793 | + $existing_meta_item->meta_key = isset( $existing_meta_item->meta_key ) ? wp_slash( $existing_meta_item->meta_key ) : null; | |
| 787 | 794 | |
| 788 | 795 | // make sure that the meta id passed matches the existing meta key. |
| 789 | 796 | if ( ! empty( $meta->id ) && ! empty( $meta->key ) ) { |
| 790 | 797 | $meta_by_id = get_metadata_by_mid( 'post', $meta->id ); |