← All changes
|
json-endpoints/class.wpcom-json-api-update-post-v1-1-endpoint.php
+29
-19
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_v1_1_Endpoint( |
| 13 | 17 | array( |
| 14 | 18 | 'description' => 'Create a post.', |
| 15 | 19 | 'group' => 'posts', |
| @@ -204,8 +208,10 @@ | ||
| 204 | 208 | |
| 205 | 209 | // phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid |
| 206 | 210 | /** |
| 207 | 211 | * Update post v1.1 endpoint class. |
| 212 | + * | |
| 213 | + * @phan-constructor-used-for-side-effects | |
| 208 | 214 | */ |
| 209 | 215 | class WPCOM_JSON_API_Update_Post_v1_1_Endpoint extends WPCOM_JSON_API_Post_v1_1_Endpoint { |
| 210 | 216 | /** |
| 211 | 217 | * WPCOM_JSON_API_Update_Post_v1_1_Endpoint constructor. |
| @@ -268,9 +274,9 @@ | ||
| 268 | 274 | $args = $this->query_args(); |
| 269 | 275 | |
| 270 | 276 | // unhook publicize, it's hooked again later -- without this, skipping services is impossible. |
| 271 | 277 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 272 | - remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 ); | |
| 278 | + remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100 ); | |
| 273 | 279 | add_action( 'rest_api_inserted_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ) ); |
| 274 | 280 | |
| 275 | 281 | if ( $this->should_load_theme_functions( $post_id ) ) { |
| 276 | 282 | $this->load_theme_functions(); |
| @@ -364,9 +370,9 @@ | ||
| 364 | 370 | if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && ! current_user_can( 'publish_post', $post->ID ) ) { |
| 365 | 371 | $input['status'] = 'pending'; |
| 366 | 372 | } |
| 367 | 373 | $last_status = $post->post_status; |
| 368 | - $new_status = isset( $input['status'] ) ? $input['status'] : $last_status; | |
| 374 | + $new_status = $input['status'] ?? $last_status; | |
| 369 | 375 | |
| 370 | 376 | // Make sure that drafts get the current date when transitioning to publish if not supplied in the post. |
| 371 | 377 | // Similarly, scheduled posts that are manually published before their scheduled date should have the date reset. |
| 372 | 378 | $date_in_past = ( strtotime( $post->post_date_gmt ) < time() ); |
| @@ -521,9 +527,10 @@ | ||
| 521 | 527 | $is_open = WPCOM_JSON_API::is_truthy( $discussion[ $discussion_open ] ); |
| 522 | 528 | $discussion[ $discussion_status ] = $is_open ? 'open' : 'closed'; |
| 523 | 529 | } |
| 524 | 530 | |
| 525 | - if ( in_array( $discussion[ $discussion_status ], array( 'open', 'closed' ), true ) ) { | |
| 531 | + if ( isset( $discussion[ $discussion_status ] ) && | |
| 532 | + in_array( $discussion[ $discussion_status ], array( 'open', 'closed' ), true ) ) { | |
| 526 | 533 | $insert[ $discussion_status ] = $discussion[ $discussion_status ]; |
| 527 | 534 | } |
| 528 | 535 | } |
| 529 | 536 | } |
| @@ -534,12 +541,12 @@ | ||
| 534 | 541 | $insert['menu_order'] = $input['menu_order']; |
| 535 | 542 | unset( $input['menu_order'] ); |
| 536 | 543 | } |
| 537 | 544 | |
| 538 | - $publicize = isset( $input['publicize'] ) ? $input['publicize'] : null; | |
| 545 | + $publicize = $input['publicize'] ?? null; | |
| 539 | 546 | unset( $input['publicize'] ); |
| 540 | 547 | |
| 541 | - $publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null; | |
| 548 | + $publicize_custom_message = $input['publicize_message'] ?? null; | |
| 542 | 549 | unset( $input['publicize_message'] ); |
| 543 | 550 | |
| 544 | 551 | if ( isset( $input['featured_image'] ) ) { |
| 545 | 552 | $featured_image = trim( $input['featured_image'] ); |
| @@ -546,18 +553,18 @@ | ||
| 546 | 553 | $delete_featured_image = empty( $featured_image ); |
| 547 | 554 | unset( $input['featured_image'] ); |
| 548 | 555 | } |
| 549 | 556 | |
| 550 | - $metadata = isset( $input['metadata'] ) ? $input['metadata'] : null; | |
| 557 | + $metadata = $input['metadata'] ?? null; | |
| 551 | 558 | unset( $input['metadata'] ); |
| 552 | 559 | |
| 553 | - $likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null; | |
| 560 | + $likes = $input['likes_enabled'] ?? null; | |
| 554 | 561 | unset( $input['likes_enabled'] ); |
| 555 | 562 | |
| 556 | - $sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null; | |
| 563 | + $sharing = $input['sharing_enabled'] ?? null; | |
| 557 | 564 | unset( $input['sharing_enabled'] ); |
| 558 | 565 | |
| 559 | - $sticky = isset( $input['sticky'] ) ? $input['sticky'] : null; | |
| 566 | + $sticky = $input['sticky'] ?? null; | |
| 560 | 567 | unset( $input['sticky'] ); |
| 561 | 568 | |
| 562 | 569 | foreach ( $input as $key => $value ) { |
| 563 | 570 | $insert[ "post_$key" ] = $value; |
| @@ -695,9 +702,9 @@ | ||
| 695 | 702 | |
| 696 | 703 | // Set sharing status of the post. |
| 697 | 704 | if ( $new ) { |
| 698 | 705 | $sharing_enabled = isset( $sharing ) ? (bool) $sharing : true; |
| 699 | - if ( false === $sharing_enabled ) { | |
| 706 | + if ( ! $sharing_enabled ) { | |
| 700 | 707 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
| 701 | 708 | } |
| 702 | 709 | } elseif ( isset( $sharing ) && true === $sharing ) { |
| 703 | 710 | delete_post_meta( $post_id, 'sharing_disabled' ); |
| @@ -828,9 +835,9 @@ | ||
| 828 | 835 | |
| 829 | 836 | $meta = (object) $meta; |
| 830 | 837 | |
| 831 | 838 | if ( |
| 832 | - in_array( $meta->key, Jetpack_SEO_Posts::POST_META_KEYS_ARRAY, true ) && | |
| 839 | + in_array( $meta->key ?? null, Jetpack_SEO_Posts::POST_META_KEYS_ARRAY, true ) && | |
| 833 | 840 | ! Jetpack_SEO_Utils::is_enabled_jetpack_seo() |
| 834 | 841 | ) { |
| 835 | 842 | return new WP_Error( 'unauthorized', __( 'SEO tools are not enabled for this site.', 'jetpack' ), 403 ); |
| 836 | 843 | } |
| @@ -858,12 +865,12 @@ | ||
| 858 | 865 | continue; |
| 859 | 866 | } |
| 860 | 867 | } |
| 861 | 868 | |
| 862 | - $unslashed_meta_key = wp_unslash( $meta->key ); // should match what the final key will be. | |
| 863 | - $meta->key = wp_slash( $meta->key ); | |
| 864 | - $unslashed_existing_meta_key = wp_unslash( $existing_meta_item->meta_key ); | |
| 865 | - $existing_meta_item->meta_key = wp_slash( $existing_meta_item->meta_key ); | |
| 869 | + $unslashed_meta_key = isset( $meta->key ) ? wp_unslash( $meta->key ) : null; // should match what the final key will be. | |
| 870 | + $meta->key = isset( $meta->key ) ? wp_slash( $meta->key ) : null; | |
| 871 | + $unslashed_existing_meta_key = isset( $existing_meta_item->meta_key ) ? wp_unslash( $existing_meta_item->meta_key ) : null; | |
| 872 | + $existing_meta_item->meta_key = isset( $existing_meta_item->meta_key ) ? wp_slash( $existing_meta_item->meta_key ) : null; | |
| 866 | 873 | |
| 867 | 874 | // make sure that the meta id passed matches the existing meta key. |
| 868 | 875 | if ( ! empty( $meta->id ) && ! empty( $meta->key ) ) { |
| 869 | 876 | $meta_by_id = get_metadata_by_mid( 'post', $meta->id ); |
| @@ -927,11 +934,13 @@ | ||
| 927 | 934 | if ( ! empty( $media_results['errors'] ) ) { |
| 928 | 935 | $return['media_errors'] = $media_results['errors']; |
| 929 | 936 | } |
| 930 | 937 | |
| 931 | - if ( 'publish' !== $post->post_status ) { | |
| 938 | + // Generate suggestions for new posts or non-published posts | |
| 939 | + if ( $new || ( isset( $return['status'] ) && 'publish' !== $return['status'] ) ) { | |
| 932 | 940 | $sal_site = $this->get_sal_post_by( 'ID', $post_id, $args['context'] ); |
| 933 | - $return['other_URLs'] = (object) $sal_site->get_permalink_suggestions( $input['title'] ); | |
| 941 | + $title = $input['title'] ?? ''; | |
| 942 | + $return['other_URLs'] = (object) $sal_site->get_permalink_suggestions( $title ); | |
| 934 | 943 | } |
| 935 | 944 | |
| 936 | 945 | /** This action is documented in json-endpoints/class.wpcom-json-api-site-settings-endpoint.php */ |
| 937 | 946 | do_action( 'wpcom_json_api_objects', 'posts' ); |
| @@ -1106,9 +1115,10 @@ | ||
| 1106 | 1115 | protected function untrash_post( $post, $input ) { |
| 1107 | 1116 | wp_untrash_post( $post->ID ); |
| 1108 | 1117 | $untrashed_post = get_post( $post->ID ); |
| 1109 | 1118 | // Lets make sure that we use the reverted the slug. |
| 1110 | - if ( isset( $untrashed_post->post_name ) && $untrashed_post->post_name . '__trashed' === $input['slug'] ) { | |
| 1119 | + if ( isset( $input['slug'] ) && isset( $untrashed_post->post_name ) && | |
| 1120 | + $untrashed_post->post_name . '__trashed' === $input['slug'] ) { | |
| 1111 | 1121 | unset( $input['slug'] ); |
| 1112 | 1122 | } |
| 1113 | 1123 | return $input; |
| 1114 | 1124 | } |
| @@ -1122,9 +1132,9 @@ | ||
| 1122 | 1132 | */ |
| 1123 | 1133 | protected function should_load_theme_functions( $post_id = null ) { |
| 1124 | 1134 | if ( empty( $post_id ) ) { |
| 1125 | 1135 | $input = $this->input( true ); |
| 1126 | - $type = $input['type']; | |
| 1136 | + $type = $input['type'] ?? null; | |
| 1127 | 1137 | } else { |
| 1128 | 1138 | $type = get_post_type( $post_id ); |
| 1129 | 1139 | } |
| 1130 | 1140 | |