diff options
Diffstat (limited to 'plugins/jetpack/_inc/lib/core-api')
7 files changed, 386 insertions, 46 deletions
diff --git a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php index 96a47a08..3ade1c34 100644 --- a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php +++ b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-module-endpoints.php @@ -1,4 +1,7 @@ <?php + +use Automattic\Jetpack\Status; + /** * This is the base class for every Core API endpoint Jetpack uses. * @@ -196,7 +199,7 @@ class Jetpack_Core_API_Module_List_Endpoint { if ( isset( $modules[ $slug ]['requires_connection'] ) && $modules[ $slug ]['requires_connection'] - && Jetpack::is_development_mode() + && ( new Status() )->is_development_mode() ) { $modules[ $slug ]['activated'] = false; } @@ -363,7 +366,7 @@ class Jetpack_Core_API_Data extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint { if ( isset( $module['requires_connection'] ) && $module['requires_connection'] - && Jetpack::is_development_mode() + && ( new Status() )->is_development_mode() ) { $module['activated'] = false; } @@ -420,19 +423,16 @@ class Jetpack_Core_API_Data extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint { foreach ( $settings as $setting => $properties ) { switch ( $setting ) { case 'lang_id': - if ( defined( 'WPLANG' ) ) { - // We can't affect this setting, so warn the client - $response[ $setting ] = 'error_const'; - break; - } - if ( ! current_user_can( 'install_languages' ) ) { // The user doesn't have caps to install language packs, so warn the client $response[ $setting ] = 'error_cap'; break; } - $value = get_option( 'WPLANG' ); + $value = get_option( 'WPLANG', '' ); + if ( empty( $value ) && defined( 'WPLANG' ) ) { + $value = WPLANG; + } $response[ $setting ] = empty( $value ) ? 'en_US' : $value; break; @@ -645,7 +645,7 @@ class Jetpack_Core_API_Data extends Jetpack_Core_API_XMLRPC_Consumer_Endpoint { switch ( $option ) { case 'lang_id': - if ( defined( 'WPLANG' ) || ! current_user_can( 'install_languages' ) ) { + if ( ! current_user_can( 'install_languages' ) ) { // We can't affect this setting $updated = false; break; @@ -1646,7 +1646,8 @@ class Jetpack_Core_API_Module_Data_Endpoint { 'code' => 'success', 'message' => esc_html( sprintf( - __( 'Your site was successfully backed-up %s ago.', 'jetpack' ), + /* translators: placeholder is a unit of time (1 hour, 5 days, ...) */ + esc_html__( 'Your site was successfully backed up %s ago.', 'jetpack' ), human_time_diff( $data->backups->last_backup, current_time( 'timestamp' ) diff --git a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php index 68327f51..c321f6ae 100644 --- a/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php +++ b/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php @@ -1,12 +1,21 @@ -<?php +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName /** - * This is the endpoint class for `/site` endpoints. + * List of /site core REST API endpoints used in Jetpack's dashboard. * + * @package Jetpack + */ + +use Automattic\Jetpack\Connection\Client; + +/** + * This is the endpoint class for `/site` endpoints. */ class Jetpack_Core_API_Site_Endpoint { + /** * Returns the result of `/sites/%s/features` endpoint call. + * * @return object $features has 'active' and 'available' properties each of which contain feature slugs. * 'active' is a simple array of slugs that are active on the current plan. * 'available' is an object with keys that represent feature slugs and values are arrays @@ -14,11 +23,11 @@ class Jetpack_Core_API_Site_Endpoint { */ public static function get_features() { - // Make the API request - $request = sprintf( '/sites/%d/features', Jetpack_Options::get_option( 'id' ) ); - $response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' ); + // Make the API request. + $request = sprintf( '/sites/%d/features', Jetpack_Options::get_option( 'id' ) ); + $response = Client::wpcom_json_api_request_as_blog( $request, '1.1' ); - // Bail if there was an error or malformed response + // Bail if there was an error or malformed response. if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { return new WP_Error( 'failed_to_fetch_data', @@ -27,10 +36,10 @@ class Jetpack_Core_API_Site_Endpoint { ); } - // Decode the results + // Decode the results. $results = json_decode( $response['body'], true ); - // Bail if there were no results or plan details returned + // Bail if there were no results or plan details returned. if ( ! is_array( $results ) ) { return new WP_Error( 'failed_to_fetch_data', @@ -39,10 +48,52 @@ class Jetpack_Core_API_Site_Endpoint { ); } - return rest_ensure_response( array( - 'code' => 'success', + return rest_ensure_response( + array( + 'code' => 'success', 'message' => esc_html__( 'Site features correctly received.', 'jetpack' ), - 'data' => wp_remote_retrieve_body( $response ), + 'data' => wp_remote_retrieve_body( $response ), + ) + ); + } + + + /** + * Returns the result of `/sites/%s/purchases` endpoint call. + * + * @return array of site purchases. + */ + public static function get_purchases() { + // Make the API request. + $request = sprintf( '/sites/%d/purchases', Jetpack_Options::get_option( 'id' ) ); + $response = Client::wpcom_json_api_request_as_blog( $request, '1.1' ); + + // Bail if there was an error or malformed response. + if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) { + return new WP_Error( + 'failed_to_fetch_data', + esc_html__( 'Unable to fetch the requested data.', 'jetpack' ), + array( 'status' => 500 ) + ); + } + + // Decode the results. + $results = json_decode( $response['body'], true ); + + // Bail if there were no results or purchase details returned. + if ( ! is_array( $results ) ) { + return new WP_Error( + 'failed_to_fetch_data', + esc_html__( 'Unable to fetch the requested data.', 'jetpack' ), + array( 'status' => 500 ) + ); + } + + return rest_ensure_response( + array( + 'code' => 'success', + 'message' => esc_html__( 'Site purchases correctly received.', 'jetpack' ), + 'data' => wp_remote_retrieve_body( $response ), ) ); } @@ -57,4 +108,159 @@ class Jetpack_Core_API_Site_Endpoint { public static function can_request() { return current_user_can( 'jetpack_manage_modules' ); } + + /** + * Gets an array of data that show how Jetpack is currently being used to benefit the site. + * + * @since 7.7 + * + * @return WP_REST_Response + */ + public static function get_benefits() { + $benefits = array(); + + /* + * We get different benefits from Stats: + * - this year's visitors + * - Followers (only if subs module is active) + * - Sharing counts (not currently supported in Jetpack -- https://github.com/Automattic/jetpack/issues/844 ) + */ + $stats = null; + if ( function_exists( 'stats_get_from_restapi' ) ) { + $stats = stats_get_from_restapi( array( 'fields' => 'stats' ) ); + } + + // Yearly visitors. + if ( null !== $stats && $stats->stats->visitors > 0 ) { + $benefits[] = array( + 'name' => 'jetpack-stats', + 'title' => esc_html__( 'Site Stats', 'jetpack' ), + 'description' => esc_html__( 'Visitors tracked by Jetpack', 'jetpack' ), + 'value' => absint( $stats->stats->visitors ), + ); + } + + // Protect blocked logins. + if ( Jetpack::is_module_active( 'protect' ) ) { + $protect = get_site_option( 'jetpack_protect_blocked_attempts' ); + if ( $protect > 0 ) { + $benefits[] = array( + 'name' => 'protect', + 'title' => esc_html__( 'Brute force protection', 'jetpack' ), + 'description' => esc_html__( 'The number of malicious login attempts blocked by Jetpack', 'jetpack' ), + 'value' => absint( $protect ), + ); + } + } + + // Number of followers. + if ( null !== $stats && $stats->stats->followers_blog > 0 && Jetpack::is_module_active( 'subscriptions' ) ) { + $benefits[] = array( + 'name' => 'subscribers', + 'title' => esc_html__( 'Subscribers', 'jetpack' ), + 'description' => esc_html__( 'People subscribed to your updates through Jetpack', 'jetpack' ), + 'value' => absint( $stats->stats->followers_blog ), + ); + } + + // VaultPress backups. + if ( Jetpack::is_plugin_active( 'vaultpress/vaultpress.php' ) && class_exists( 'VaultPress' ) ) { + $vaultpress = new VaultPress(); + if ( $vaultpress->is_registered() ) { + $data = json_decode( base64_decode( $vaultpress->contact_service( 'plugin_data' ) ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode + if ( $data && $data->features->backups && ! empty( $data->backups->stats ) && $data->backups->stats->revisions > 0 ) { + $benefits[] = array( + 'name' => 'jetpack-backup', + 'title' => esc_html__( 'Jetpack Backup', 'jetpack' ), + 'description' => esc_html__( 'The number of times Jetpack has backed up your site and kept it safe', 'jetpack' ), + 'value' => absint( $data->backups->stats->revisions ), + ); + } + } + } + + // Number of forms sent via a Jetpack contact form. + if ( Jetpack::is_module_active( 'contact-form' ) ) { + $contact_form_count = array_sum( get_object_vars( wp_count_posts( 'feedback' ) ) ); + if ( $contact_form_count > 0 ) { + $benefits[] = array( + 'name' => 'contact-form-feedback', + 'title' => esc_html__( 'Contact Form Feedback', 'jetpack' ), + 'description' => esc_html__( 'Form submissions stored by Jetpack', 'jetpack' ), + 'value' => absint( $contact_form_count ), + ); + } + } + + // Number of images in the library if Photon is active. + if ( Jetpack::is_module_active( 'photon' ) ) { + $photon_count = array_reduce( + get_object_vars( wp_count_attachments( array( 'image/jpeg', 'image/png', 'image/gif', 'image/bmp' ) ) ), + function ( $i, $j ) { + return $i + $j; + } + ); + if ( $photon_count > 0 ) { + $benefits[] = array( + 'name' => 'image-hosting', + 'title' => esc_html__( 'Image Hosting', 'jetpack' ), + 'description' => esc_html__( 'Super-fast, mobile-ready images served by Jetpack', 'jetpack' ), + 'value' => absint( $photon_count ), + ); + } + } + + // Number of VideoPress videos on the site. + $videopress_attachments = wp_count_attachments( 'video/videopress' ); + if ( + isset( $videopress_attachments->{'video/videopress'} ) + && $videopress_attachments->{'video/videopress'} > 0 + ) { + $benefits[] = array( + 'name' => 'video-hosting', + 'title' => esc_html__( 'Video Hosting', 'jetpack' ), + 'description' => esc_html__( 'Ad-free, lightning-fast videos delivered by Jetpack', 'jetpack' ), + 'value' => absint( $videopress_attachments->{'video/videopress'} ), + ); + } + + // Number of active Publicize connections. + if ( Jetpack::is_module_active( 'publicize' ) && class_exists( 'Publicize' ) ) { + $publicize = new Publicize(); + $connections = $publicize->get_all_connections(); + + $number_of_connections = 0; + if ( is_array( $connections ) && ! empty( $connections ) ) { + $number_of_connections = count( $connections ); + } + + if ( $number_of_connections > 0 ) { + $benefits[] = array( + 'name' => 'publicize', + 'title' => esc_html__( 'Publicize', 'jetpack' ), + 'description' => esc_html__( 'Live social media site connections, powered by Jetpack', 'jetpack' ), + 'value' => absint( $number_of_connections ), + ); + } + } + + // Total number of shares. + if ( null !== $stats && $stats->stats->shares > 0 ) { + $benefits[] = array( + 'name' => 'sharing', + 'title' => esc_html__( 'Sharing', 'jetpack' ), + 'description' => esc_html__( 'The number of times visitors have shared your posts with the world using Jetpack', 'jetpack' ), + 'value' => absint( $stats->stats->shares ), + ); + } + + // Finally, return the whole list of benefits. + return rest_ensure_response( + array( + 'code' => 'success', + 'message' => esc_html__( 'Site benefits correctly received.', 'jetpack' ), + 'data' => wp_json_encode( $benefits ), + ) + ); + } } diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php index 354880ed..a6612b37 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-mailchimp.php @@ -1,5 +1,7 @@ <?php +use Automattic\Jetpack\Connection\Client; + /** * Mailchimp: Get Mailchimp Status. * API to determine if current site has linked Mailchimp account and mailing list selected. @@ -14,6 +16,7 @@ class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller { $this->wpcom_is_wpcom_only_endpoint = true; add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } /** @@ -30,6 +33,16 @@ class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller { ), ) ); + register_rest_route( + $this->namespace, + $this->rest_base . '/groups', + array( + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'get_mailchimp_groups' ), + ), + ) + ); } /** @@ -67,13 +80,36 @@ class WPCOM_REST_API_V2_Endpoint_Mailchimp extends WP_REST_Controller { 403 ); } - $connect_url = sprintf( 'https://wordpress.com/marketing/connections/%s', rawurlencode( $site_id ) ); + $connect_url = sprintf( 'https://wordpress.com/marketing/connections/%s?mailchimp', rawurlencode( $site_id ) ); return array( 'code' => $this->is_connected() ? 'connected' : 'not_connected', 'connect_url' => $connect_url, 'site_id' => $site_id, ); } + + /** + * Get all Mailchimp groups for the accounted connected to the current blog + * + * @return mixed + * groups:array + * site_id:int + */ + public function get_mailchimp_groups() { + $is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM ); + $site_id = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' ); + if ( ! $site_id ) { + return new WP_Error( + 'unavailable_site_id', + __( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ), + 403 + ); + } + $path = sprintf( '/sites/%d/mailchimp/groups', absint( $site_id ) ); + $request = Client::wpcom_json_api_request_as_blog( $path ); + $body = wp_remote_retrieve_body( $request ); + return json_decode( $body ); + } } wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Mailchimp' ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-resolve-redirect.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-resolve-redirect.php new file mode 100644 index 00000000..442a2efa --- /dev/null +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-resolve-redirect.php @@ -0,0 +1,94 @@ +<?php +/** + * REST API endpoint for resolving URL redirects. + * + * @package Jetpack + * @since 8.0.0 + */ + +/** + * Resolve URL redirects. + * + * @since 8.0.0 + */ +class WPCOM_REST_API_V2_Endpoint_Resolve_Redirect extends WP_REST_Controller { + /** + * Constructor. + */ + public function __construct() { + $this->namespace = 'wpcom/v2'; + $this->rest_base = 'resolve-redirect'; + // This endpoint *does not* need to connect directly to Jetpack sites. + add_action( 'rest_api_init', array( $this, 'register_routes' ) ); + } + + /** + * Register the route. + */ + public function register_routes() { + // GET /sites/<blog_id>/resolve-redirect/<url> - Follow 301/302 redirects on a URL, and return the final destination. + register_rest_route( + $this->namespace, + '/' . $this->rest_base . '/(?P<url>.+)', + array( + 'args' => array( + 'url' => array( + 'description' => __( 'The URL to check for redirects.', 'jetpack' ), + 'type' => 'string', + 'required' => 'true', + 'validate_callback' => 'wp_http_validate_url', + ), + ), + array( + 'methods' => WP_REST_Server::READABLE, + 'callback' => array( $this, 'follow_redirect' ), + 'permission_callback' => 'is_user_logged_in', + ), + 'schema' => array( $this, 'get_public_item_schema' ), + ) + ); + } + + /** + * Follows 301/302 redirect for the passed URL, and returns the final destination. + * + * @param WP_REST_Request $request The REST API request data. + * @return WP_REST_Response The REST API response. + */ + public function follow_redirect( $request ) { + $response = wp_safe_remote_get( $request['url'] ); + if ( is_wp_error( $response ) ) { + return rest_ensure_response( '' ); + } + + $history = $response['http_response']->get_response_object()->history; + if ( ! $history ) { + return response_ensure_response( $request['url'] ); + } + + $location = $history[0]->headers->getValues( 'location' ); + if ( ! $location ) { + return response_ensure_response( $request['url'] ); + } + + return rest_ensure_response( $location[0] ); + } + + /** + * Retrieves the comment's schema, conforming to JSON Schema. + * + * @return array + */ + public function get_item_schema() { + $schema = array( + '$schema' => 'http://json-schema.org/draft-04/schema#', + 'title' => 'resolve-redirect', + 'type' => 'string', + 'description' => __( 'The final destination of the URL being checked for redirects.', 'jetpack' ), + ); + + return $schema; + } +} + +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Resolve_Redirect' ); diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php index ec997739..028568db 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/memberships.php @@ -6,6 +6,8 @@ * @since 7.3.0 */ +use Automattic\Jetpack\Connection\Client; + /** * Class WPCOM_REST_API_V2_Endpoint_Memberships * This introduces V2 endpoints. @@ -108,7 +110,7 @@ class WPCOM_REST_API_V2_Endpoint_Memberships extends WP_REST_Controller { return $product->to_array(); } else { $blog_id = Jetpack_Options::get_option( 'id' ); - $response = Jetpack_Client::wpcom_json_api_request_as_user( + $response = Client::wpcom_json_api_request_as_user( "/sites/$blog_id/{$this->rest_base}/product", 'v2', array( @@ -141,21 +143,16 @@ class WPCOM_REST_API_V2_Endpoint_Memberships extends WP_REST_Controller { /** * Get a status of connection for the site. If this is Jetpack, pass the request to wpcom. * - * @return array|WP_Error + * @return WP_Error|array ['products','connected_account_id','connect_url','should_upgrade_to_access_memberships','upgrade_url'] */ public function get_status() { - $connected_account_id = Jetpack_Memberships::get_connected_account_id(); - $connect_url = ''; if ( ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { require_lib( 'memberships' ); $blog_id = get_current_blog_id(); - if ( ! $connected_account_id ) { - $connect_url = get_memberships_connected_account_redirect( get_current_user_id(), $blog_id ); - } - $products = get_memberships_plans( $blog_id ); + return (array) get_memberships_settings_for_site( $blog_id ); } else { $blog_id = Jetpack_Options::get_option( 'id' ); - $response = Jetpack_Client::wpcom_json_api_request_as_user( + $response = Client::wpcom_json_api_request_as_user( "/sites/$blog_id/{$this->rest_base}/status", 'v2', array(), @@ -168,16 +165,11 @@ class WPCOM_REST_API_V2_Endpoint_Memberships extends WP_REST_Controller { return new WP_Error( 'wpcom_connection_error', __( 'Could not connect to WordPress.com', 'jetpack' ), 404 ); } $data = isset( $response['body'] ) ? json_decode( $response['body'], true ) : null; - if ( ! $connected_account_id ) { - $connect_url = empty( $data['connect_url'] ) ? '' : $data['connect_url']; + if ( 200 !== $response['response']['code'] && $data['code'] && $data['message'] ) { + return new WP_Error( $data['code'], $data['message'], 401 ); } - $products = empty( $data['products'] ) ? array() : $data['products']; + return $data; } - return array( - 'connected_account_id' => $connected_account_id, - 'connect_url' => $connect_url, - 'products' => $products, - ); } } diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php index c1a712bd..47c95b26 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-endpoints/subscribers.php @@ -1,5 +1,7 @@ <?php +use Automattic\Jetpack\Constants; + /** * Subscribers: Get subscriber count * @@ -41,7 +43,7 @@ class WPCOM_REST_API_V2_Endpoint_Subscribers extends WP_REST_Controller { */ public function get_subscriber_count( $request ) { // Get the most up to date subscriber count when request is not a test - if ( ! Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' ) ) { + if ( ! Constants::is_defined( 'TESTING_IN_JETPACK' ) ) { delete_transient( 'wpcom_subscribers_total' ); } @@ -56,7 +58,7 @@ class WPCOM_REST_API_V2_Endpoint_Subscribers extends WP_REST_Controller { if ( Jetpack::is_module_active( 'subscriptions' ) || - ( Jetpack_Constants::is_defined( 'TESTING_IN_JETPACK' ) && Jetpack_Constants::get_constant( 'TESTING_IN_JETPACK' ) ) + ( Constants::is_defined( 'TESTING_IN_JETPACK' ) && Constants::get_constant( 'TESTING_IN_JETPACK' ) ) ) { wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Subscribers' ); } diff --git a/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php b/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php index c4254a9d..21181d2c 100644 --- a/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php +++ b/plugins/jetpack/_inc/lib/core-api/wpcom-fields/post-fields-publicize-connections.php @@ -23,9 +23,11 @@ * @since 6.8.0 */ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_V2_Field_Controller { - protected $object_type = 'post'; + protected $object_type = array( 'post' ); protected $field_name = 'jetpack_publicize_connections'; + private $_meta_saved = array(); + public $memoized_updates = array(); /** @@ -34,7 +36,6 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ */ public function register_fields() { $this->object_type = get_post_types_by_support( 'publicize' ); - foreach ( $this->object_type as $post_type ) { // Adds meta support for those post types that don't already have it. // Only runs during REST API requests, so it doesn't impact UI. @@ -208,14 +209,18 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ } $permission_check = $this->update_permission_check( $request['jetpack_publicize_connections'], $post, $request ); - if ( is_wp_error( $permission_check ) ) { return $permission_check; } - // memoize $this->get_meta_to_update( $request['jetpack_publicize_connections'], isset( $post->ID ) ? $post->ID : 0 ); + if ( isset( $post->ID ) ) { + // Set the meta before we mark the post as published so that publicize works as expected. + // If this is not the case post end up on social media when they are marked as skipped. + $this->update( $request['jetpack_publicize_connections'], $post, $request ); + } + return $post; } @@ -338,6 +343,9 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ * @param WP_REST_Request */ public function update( $requested_connections, $post, $request ) { + if ( isset( $this->_meta_saved[ $post->ID ] ) ) { // Make sure we only save it once - per request. + return; + } foreach ( $this->get_meta_to_update( $requested_connections, $post->ID ) as $meta_key => $meta_value ) { if ( is_null( $meta_value ) ) { delete_post_meta( $post->ID, $meta_key ); @@ -345,6 +353,7 @@ class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_ update_post_meta( $post->ID, $meta_key, $meta_value ); } } + $this->_meta_saved[ $post->ID ] = true; } } |