← All changes
|
json-endpoints/class.wpcom-json-api-update-post-v1-2-endpoint.php
+29
-14
13.7.2
→
16.3-a.7
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_2_Endpoint( |
| 13 | 17 | array( |
| 14 | 18 | 'description' => 'Create a post.', |
| 15 | 19 | 'group' => 'posts', |
| @@ -169,8 +173,10 @@ | ||
| 169 | 173 | |
| 170 | 174 | // phpcs:disable PEAR.NamingConventions.ValidClassName.Invalid |
| 171 | 175 | /** |
| 172 | 176 | * Update post v1.2 endpoint class. |
| 177 | + * | |
| 178 | + * @phan-constructor-used-for-side-effects | |
| 173 | 179 | */ |
| 174 | 180 | class WPCOM_JSON_API_Update_Post_v1_2_Endpoint extends WPCOM_JSON_API_Update_Post_v1_1_Endpoint { |
| 175 | 181 | /** |
| 176 | 182 | * Create or update a post. |
| @@ -195,9 +201,9 @@ | ||
| 195 | 201 | } |
| 196 | 202 | |
| 197 | 203 | // unhook publicize, it's hooked again later -- without this, skipping services is impossible. |
| 198 | 204 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 199 | - remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100, 2 ); | |
| 205 | + remove_action( 'save_post', array( $GLOBALS['publicize_ui']->publicize, 'async_publicize_post' ), 100 ); | |
| 200 | 206 | |
| 201 | 207 | if ( $this->should_load_theme_functions( $post_id ) ) { |
| 202 | 208 | $this->load_theme_functions(); |
| 203 | 209 | } |
| @@ -296,9 +302,9 @@ | ||
| 296 | 302 | if ( ( isset( $input['status'] ) && 'publish' === $input['status'] ) && 'publish' !== $post->post_status && ! current_user_can( 'publish_post', $post->ID ) ) { |
| 297 | 303 | $input['status'] = 'pending'; |
| 298 | 304 | } |
| 299 | 305 | $last_status = $post->post_status; |
| 300 | - $new_status = isset( $input['status'] ) ? $input['status'] : $last_status; | |
| 306 | + $new_status = $input['status'] ?? $last_status; | |
| 301 | 307 | |
| 302 | 308 | // Make sure that drafts get the current date when transitioning to publish if not supplied in the post. |
| 303 | 309 | // Similarly, scheduled posts that are manually published before their scheduled date should have the date reset. |
| 304 | 310 | $date_in_past = ( strtotime( $post->post_date_gmt ) < time() ); |
| @@ -321,11 +327,16 @@ | ||
| 321 | 327 | } |
| 322 | 328 | |
| 323 | 329 | // If date is set, $this->input will set date_gmt, date still needs to be adjusted. |
| 324 | 330 | if ( isset( $input['date_gmt'] ) ) { |
| 325 | - $gmt_offset = get_option( 'gmt_offset' ); | |
| 326 | - $time_with_offset = strtotime( $input['date_gmt'] ) + $gmt_offset * HOUR_IN_SECONDS; | |
| 327 | - $input['date'] = gmdate( 'Y-m-d H:i:s', $time_with_offset ); | |
| 331 | + $date_gmt_timestamp = strtotime( $input['date_gmt'] ); | |
| 332 | + if ( $date_gmt_timestamp ) { | |
| 333 | + $gmt_offset = (int) get_option( 'gmt_offset' ); | |
| 334 | + $time_with_offset = $date_gmt_timestamp + $gmt_offset * HOUR_IN_SECONDS; | |
| 335 | + $input['date'] = gmdate( 'Y-m-d H:i:s', $time_with_offset ); | |
| 336 | + } else { // Invalid input. | |
| 337 | + unset( $input['date_gmt'] ); | |
| 338 | + } | |
| 328 | 339 | } |
| 329 | 340 | |
| 330 | 341 | if ( ! empty( $author_id ) && get_current_user_id() !== $author_id ) { |
| 331 | 342 | if ( ! current_user_can( $post_type->cap->edit_others_posts ) ) { |
| @@ -496,12 +507,12 @@ | ||
| 496 | 507 | $insert['menu_order'] = $input['menu_order']; |
| 497 | 508 | unset( $input['menu_order'] ); |
| 498 | 509 | } |
| 499 | 510 | |
| 500 | - $publicize = isset( $input['publicize'] ) ? $input['publicize'] : null; | |
| 511 | + $publicize = $input['publicize'] ?? null; | |
| 501 | 512 | unset( $input['publicize'] ); |
| 502 | 513 | |
| 503 | - $publicize_custom_message = isset( $input['publicize_message'] ) ? $input['publicize_message'] : null; | |
| 514 | + $publicize_custom_message = $input['publicize_message'] ?? null; | |
| 504 | 515 | unset( $input['publicize_message'] ); |
| 505 | 516 | |
| 506 | 517 | if ( isset( $input['featured_image'] ) ) { |
| 507 | 518 | $featured_image = trim( $input['featured_image'] ); |
| @@ -508,18 +519,18 @@ | ||
| 508 | 519 | $delete_featured_image = empty( $featured_image ); |
| 509 | 520 | unset( $input['featured_image'] ); |
| 510 | 521 | } |
| 511 | 522 | |
| 512 | - $metadata = isset( $input['metadata'] ) ? $input['metadata'] : null; | |
| 523 | + $metadata = $input['metadata'] ?? null; | |
| 513 | 524 | unset( $input['metadata'] ); |
| 514 | 525 | |
| 515 | - $likes = isset( $input['likes_enabled'] ) ? $input['likes_enabled'] : null; | |
| 526 | + $likes = $input['likes_enabled'] ?? null; | |
| 516 | 527 | unset( $input['likes_enabled'] ); |
| 517 | 528 | |
| 518 | - $sharing = isset( $input['sharing_enabled'] ) ? $input['sharing_enabled'] : null; | |
| 529 | + $sharing = $input['sharing_enabled'] ?? null; | |
| 519 | 530 | unset( $input['sharing_enabled'] ); |
| 520 | 531 | |
| 521 | - $sticky = isset( $input['sticky'] ) ? $input['sticky'] : null; | |
| 532 | + $sticky = $input['sticky'] ?? null; | |
| 522 | 533 | unset( $input['sticky'] ); |
| 523 | 534 | |
| 524 | 535 | foreach ( $input as $key => $value ) { |
| 525 | 536 | $insert[ "post_$key" ] = $value; |
| @@ -598,9 +609,9 @@ | ||
| 598 | 609 | break; |
| 599 | 610 | } |
| 600 | 611 | } |
| 601 | 612 | |
| 602 | - $insert['post_date'] = isset( $insert['post_date'] ) ? $insert['post_date'] : ''; | |
| 613 | + $insert['post_date'] ??= ''; | |
| 603 | 614 | |
| 604 | 615 | if ( $is_dtp_fb_post ) { |
| 605 | 616 | $insert = $this->dtp_fb_preprocess_post( $insert, $metadata ); |
| 606 | 617 | } |
| @@ -609,8 +620,9 @@ | ||
| 609 | 620 | if ( 0 === $post_id ) { |
| 610 | 621 | $post_id = wp_insert_post( add_magic_quotes( $insert ), true ); |
| 611 | 622 | } |
| 612 | 623 | } else { |
| 624 | + // @phan-suppress-next-line PhanPossiblyUndeclaredVariable -- $post is set and validated several blocks earlier if $new (only set once) is falsy. | |
| 613 | 625 | $insert['ID'] = $post->ID; |
| 614 | 626 | |
| 615 | 627 | // wp_update_post ignores date unless edit_date is set |
| 616 | 628 | // See: https://codex.wordpress.org/Function_Reference/wp_update_post#Scheduling_posts |
| @@ -695,9 +707,9 @@ | ||
| 695 | 707 | |
| 696 | 708 | // Set sharing status of the post. |
| 697 | 709 | if ( $new ) { |
| 698 | 710 | $sharing_enabled = isset( $sharing ) ? (bool) $sharing : true; |
| 699 | - if ( false === $sharing_enabled ) { | |
| 711 | + if ( ! $sharing_enabled ) { | |
| 700 | 712 | update_post_meta( $post_id, 'sharing_disabled', 1 ); |
| 701 | 713 | } |
| 702 | 714 | } elseif ( isset( $sharing ) && true === $sharing ) { |
| 703 | 715 | delete_post_meta( $post_id, 'sharing_disabled' ); |
| @@ -954,9 +966,9 @@ | ||
| 954 | 966 | */ |
| 955 | 967 | protected function should_load_theme_functions( $post_id = null ) { |
| 956 | 968 | if ( empty( $post_id ) ) { |
| 957 | 969 | $input = $this->input( true ); |
| 958 | - $type = $input['type']; | |
| 970 | + $type = $input['type'] ?? null; | |
| 959 | 971 | } else { |
| 960 | 972 | $type = get_post_type( $post_id ); |
| 961 | 973 | } |
| 962 | 974 | |
| @@ -1000,8 +1012,11 @@ | ||
| 1000 | 1012 | 'latitude' => $fb_point['latitude'], |
| 1001 | 1013 | ), |
| 1002 | 1014 | 'title' => $fb_point['name'], |
| 1003 | 1015 | ); |
| 1016 | + } | |
| 1017 | + if ( ! function_exists( 'map_block_from_geo_points' ) ) { | |
| 1018 | + require_once JETPACK__PLUGIN_DIR . 'extensions/blocks/map/map.php'; | |
| 1004 | 1019 | } |
| 1005 | 1020 | $map_block = map_block_from_geo_points( $geo_points ); |
| 1006 | 1021 | |
| 1007 | 1022 | $post['post_content'] = $map_block . $post['post_content']; |