← All changes
|
jetpack_vendor/automattic/jetpack-publicize/src/rest-api/class-connections-controller.php
+60
-1
16.2
→
16.3
View file →
| @@ -6,8 +6,9 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace Automattic\Jetpack\Publicize\REST_API; |
| 9 | 9 | |
| 10 | +use Automattic\Jetpack\Connection\Rest_Authentication; | |
| 10 | 11 | use Automattic\Jetpack\Connection\Traits\WPCOM_REST_API_Proxy_Request; |
| 11 | 12 | use Automattic\Jetpack\Publicize\Connections; |
| 12 | 13 | use Automattic\Jetpack\Publicize\Jetpack_Social_Settings\Settings; |
| 13 | 14 | use Automattic\Jetpack\Publicize\Publicize_Utils; |
| @@ -104,9 +105,13 @@ | ||
| 104 | 105 | 'methods' => WP_REST_Server::EDITABLE, |
| 105 | 106 | 'callback' => array( $this, 'update_item' ), |
| 106 | 107 | 'permission_callback' => array( $this, 'update_item_permissions_check' ), |
| 107 | 108 | 'args' => array( |
| 108 | - 'shared' => array( | |
| 109 | + 'external_user_ID' => array( | |
| 110 | + 'description' => __( 'External User Id - in case of services like Facebook.', 'jetpack-publicize-pkg' ), | |
| 111 | + 'type' => 'string', | |
| 112 | + ), | |
| 113 | + 'shared' => array( | |
| 109 | 114 | 'description' => __( 'Whether the connection is shared with other users.', 'jetpack-publicize-pkg' ), |
| 110 | 115 | 'type' => 'boolean', |
| 111 | 116 | ), |
| 112 | 117 | ), |
| @@ -119,11 +124,60 @@ | ||
| 119 | 124 | ), |
| 120 | 125 | 'schema' => array( $this, 'get_public_item_schema' ), |
| 121 | 126 | ) |
| 122 | 127 | ); |
| 128 | + | |
| 129 | + // This route receives pushes from WPCOM, so it is registered under the | |
| 130 | + // site-local jetpack/v4 namespace and never on WPCOM itself. | |
| 131 | + if ( ! Publicize_Utils::is_wpcom() ) { | |
| 132 | + register_rest_route( | |
| 133 | + 'jetpack/v4', | |
| 134 | + '/publicize/connections/sync', | |
| 135 | + array( | |
| 136 | + array( | |
| 137 | + 'methods' => WP_REST_Server::CREATABLE, | |
| 138 | + 'callback' => array( $this, 'receive_updated_connections' ), | |
| 139 | + 'permission_callback' => array( Rest_Authentication::class, 'is_signed_with_user_token' ), | |
| 140 | + 'args' => array( | |
| 141 | + // An empty value is accepted on purpose: a site with no connections left | |
| 142 | + // syncs an empty payload, which arrives here as an empty object. | |
| 143 | + 'connections' => array( | |
| 144 | + 'type' => 'object', | |
| 145 | + 'required' => true, | |
| 146 | + 'description' => __( 'The updated Publicize connections, keyed by service name.', 'jetpack-publicize-pkg' ), | |
| 147 | + ), | |
| 148 | + ), | |
| 149 | + ), | |
| 150 | + ) | |
| 151 | + ); | |
| 152 | + } | |
| 123 | 153 | } |
| 124 | 154 | |
| 125 | 155 | /** |
| 156 | + * Receive updated Publicize connections from WPCOM. | |
| 157 | + * | |
| 158 | + * REST replacement for the jetpack.updatePublicizeConnections XML-RPC method. | |
| 159 | + * | |
| 160 | + * Unusable connections are dropped rather than rejected: an error response would send | |
| 161 | + * WPCOM down its XML-RPC fallback, which stores the same payload without the check. | |
| 162 | + * | |
| 163 | + * @param WP_REST_Request $request Full details about the request. | |
| 164 | + * @return WP_REST_Response | |
| 165 | + */ | |
| 166 | + public function receive_updated_connections( $request ) { | |
| 167 | + /** | |
| 168 | + * The route only registers on Jetpack sites, where the global is this package's Publicize. | |
| 169 | + * | |
| 170 | + * @var \Automattic\Jetpack\Publicize\Publicize $publicize | |
| 171 | + */ | |
| 172 | + global $publicize; | |
| 173 | + | |
| 174 | + return rest_ensure_response( | |
| 175 | + $publicize->receive_updated_publicize_connections( $request->get_param( 'connections' ) ) | |
| 176 | + ); | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 126 | 180 | * Schema for the endpoint. |
| 127 | 181 | * |
| 128 | 182 | * @return array |
| 129 | 183 | */ |
| @@ -457,8 +511,13 @@ | ||
| 457 | 511 | |
| 458 | 512 | $input = array( |
| 459 | 513 | 'shared' => $request->get_param( 'shared' ), |
| 460 | 514 | ); |
| 515 | + | |
| 516 | + $external_user_id = $request->get_param( 'external_user_ID' ); | |
| 517 | + if ( ! empty( $external_user_id ) ) { | |
| 518 | + $input['external_user_ID'] = $external_user_id; | |
| 519 | + } | |
| 461 | 520 | |
| 462 | 521 | if ( $request->has_param( 'template' ) ) { |
| 463 | 522 | require_lib( 'publicize/util/message-templates' ); |
| 464 | 523 | |