post-fields-publicize-connections.php
431 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Handle Publicize connection information for each post. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | /** |
| 9 | * Add per-post Publicize Connection data. |
| 10 | * |
| 11 | * { # Post Object |
| 12 | * ... |
| 13 | * jetpack_publicize_connections: { # Defined below in this file. See schema for more detail. |
| 14 | * id: (string) Connection unique_id |
| 15 | * service_name: (string) Service slug |
| 16 | * display_name: (string) User name/display name of user/connection on Service |
| 17 | * profile_picture: (string) Profile picture of user/connection on Service |
| 18 | * enabled: (boolean) Is this connection slated to be shared to? context=edit only |
| 19 | * done: (boolean) Is this post (or connection) done sharing? context=edit only |
| 20 | * toggleable: (boolean) Can the current user change the `enabled` setting for this Connection+Post? context=edit only |
| 21 | * } |
| 22 | * ... |
| 23 | * meta: { # Not defined in this file. Handled in modules/publicize/publicize.php via `register_meta()` |
| 24 | * jetpack_publicize_feature_enabled: (boolean) Is this publicize feature enabled? |
| 25 | * jetpack_publicize_message: (string) The message to use instead of the post's title when sharing. |
| 26 | * jetpack_social_options: { |
| 27 | * attached_media: (array) List of media that will be attached to the social media post. |
| 28 | * image_generator_settings: (array) List of settings related to the generated image. |
| 29 | * } |
| 30 | * ... |
| 31 | * } |
| 32 | * |
| 33 | * @since 6.8.0 |
| 34 | */ |
| 35 | class WPCOM_REST_API_V2_Post_Publicize_Connections_Field extends WPCOM_REST_API_V2_Field_Controller { |
| 36 | /** |
| 37 | * Array of post types that can handle Publicize. |
| 38 | * |
| 39 | * @var array |
| 40 | */ |
| 41 | protected $object_type = array( 'post' ); |
| 42 | |
| 43 | /** |
| 44 | * Field name |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $field_name = 'jetpack_publicize_connections'; |
| 49 | |
| 50 | /** |
| 51 | * Array of post IDs that have been updated. |
| 52 | * |
| 53 | * @var array |
| 54 | */ |
| 55 | private $meta_saved = array(); |
| 56 | |
| 57 | /** |
| 58 | * Used to memoize the updates for a given post. |
| 59 | * |
| 60 | * @var array |
| 61 | */ |
| 62 | public $memoized_updates = array(); |
| 63 | |
| 64 | /** |
| 65 | * Registers the jetpack_publicize_connections field. Called |
| 66 | * automatically on `rest_api_init()`. |
| 67 | */ |
| 68 | public function register_fields() { |
| 69 | $this->object_type = get_post_types_by_support( 'publicize' ); |
| 70 | foreach ( $this->object_type as $post_type ) { |
| 71 | if ( $this->is_registered( $post_type ) ) { |
| 72 | continue; |
| 73 | } |
| 74 | // Adds meta support for those post types that don't already have it. |
| 75 | // Only runs during REST API requests, so it doesn't impact UI. |
| 76 | if ( ! post_type_supports( $post_type, 'custom-fields' ) ) { |
| 77 | add_post_type_support( $post_type, 'custom-fields' ); |
| 78 | } |
| 79 | |
| 80 | add_filter( 'rest_pre_insert_' . $post_type, array( $this, 'rest_pre_insert' ), 10, 2 ); |
| 81 | add_action( 'rest_insert_' . $post_type, array( $this, 'rest_insert' ), 10, 3 ); |
| 82 | } |
| 83 | |
| 84 | parent::register_fields(); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Defines data structure and what elements are visible in which contexts |
| 89 | */ |
| 90 | public function get_schema() { |
| 91 | return array( |
| 92 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 93 | 'title' => 'jetpack-publicize-post-connections', |
| 94 | 'type' => 'array', |
| 95 | 'context' => array( 'view', 'edit' ), |
| 96 | 'items' => $this->post_connection_schema(), |
| 97 | 'default' => array(), |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Schema for the endpoint. |
| 103 | */ |
| 104 | private function post_connection_schema() { |
| 105 | return array( |
| 106 | '$schema' => 'http://json-schema.org/draft-04/schema#', |
| 107 | 'title' => 'jetpack-publicize-post-connection', |
| 108 | 'type' => 'object', |
| 109 | 'properties' => array( |
| 110 | 'id' => array( |
| 111 | 'description' => __( 'Unique identifier for the Jetpack Social connection', 'jetpack' ), |
| 112 | 'type' => 'string', |
| 113 | 'context' => array( 'view', 'edit' ), |
| 114 | 'readonly' => true, |
| 115 | ), |
| 116 | 'service_name' => array( |
| 117 | 'description' => __( 'Alphanumeric identifier for the Jetpack Social service', 'jetpack' ), |
| 118 | 'type' => 'string', |
| 119 | 'context' => array( 'view', 'edit' ), |
| 120 | 'readonly' => true, |
| 121 | ), |
| 122 | 'display_name' => array( |
| 123 | 'description' => __( 'Display name of the connected account', 'jetpack' ), |
| 124 | 'type' => 'string', |
| 125 | 'context' => array( 'view', 'edit' ), |
| 126 | 'readonly' => true, |
| 127 | ), |
| 128 | 'username' => array( |
| 129 | 'description' => __( 'Username of the connected account', 'jetpack' ), |
| 130 | 'type' => 'string', |
| 131 | 'context' => array( 'view', 'edit' ), |
| 132 | 'readonly' => true, |
| 133 | ), |
| 134 | 'profile_picture' => array( |
| 135 | 'description' => __( 'Profile picture of the connected account', 'jetpack' ), |
| 136 | 'type' => 'string', |
| 137 | 'context' => array( 'view', 'edit' ), |
| 138 | 'readonly' => true, |
| 139 | ), |
| 140 | 'enabled' => array( |
| 141 | 'description' => __( 'Whether to share to this connection', 'jetpack' ), |
| 142 | 'type' => 'boolean', |
| 143 | 'context' => array( 'edit' ), |
| 144 | ), |
| 145 | 'done' => array( |
| 146 | 'description' => __( 'Whether Jetpack Social has already finished sharing for this post', 'jetpack' ), |
| 147 | 'type' => 'boolean', |
| 148 | 'context' => array( 'edit' ), |
| 149 | 'readonly' => true, |
| 150 | ), |
| 151 | 'toggleable' => array( |
| 152 | 'description' => __( 'Whether `enable` can be changed for this post/connection', 'jetpack' ), |
| 153 | 'type' => 'boolean', |
| 154 | 'context' => array( 'edit' ), |
| 155 | 'readonly' => true, |
| 156 | ), |
| 157 | ), |
| 158 | ); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Permission check, based on module availability and user capabilities. |
| 163 | * |
| 164 | * @param int $post_id Post ID. |
| 165 | * |
| 166 | * @return true|WP_Error |
| 167 | */ |
| 168 | public function permission_check( $post_id ) { |
| 169 | global $publicize; |
| 170 | |
| 171 | if ( ! $publicize ) { |
| 172 | return new WP_Error( |
| 173 | 'publicize_not_available', |
| 174 | __( 'Sorry, Jetpack Social is not available on your site right now.', 'jetpack' ), |
| 175 | array( 'status' => rest_authorization_required_code() ) |
| 176 | ); |
| 177 | } |
| 178 | |
| 179 | if ( $publicize->current_user_can_access_publicize_data( $post_id ) ) { |
| 180 | return true; |
| 181 | } |
| 182 | |
| 183 | return new WP_Error( |
| 184 | 'invalid_user_permission_publicize', |
| 185 | __( 'Sorry, you are not allowed to access Jetpack Social data for this post.', 'jetpack' ), |
| 186 | array( 'status' => rest_authorization_required_code() ) |
| 187 | ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Getter permission check |
| 192 | * |
| 193 | * @param mixed $post_array Response from the post endpoint. |
| 194 | * @param WP_REST_Request $request API request. |
| 195 | * |
| 196 | * @return true|WP_Error |
| 197 | */ |
| 198 | public function get_permission_check( $post_array, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 199 | return $this->permission_check( isset( $post_array['id'] ) ? $post_array['id'] : 0 ); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Setter permission check. |
| 204 | * |
| 205 | * @param mixed $value The new value for the field. |
| 206 | * @param WP_Post $post The post object. |
| 207 | * @param WP_REST_Request $request API request. |
| 208 | * |
| 209 | * @return true|WP_Error |
| 210 | */ |
| 211 | public function update_permission_check( $value, $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 212 | return $this->permission_check( isset( $post->ID ) ? $post->ID : 0 ); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Getter: Retrieve current list of connected social accounts for a given post. |
| 217 | * |
| 218 | * @see Publicize::get_filtered_connection_data() |
| 219 | * |
| 220 | * @param array $post_array Response from Post Endpoint. |
| 221 | * @param WP_REST_Request $request API request. |
| 222 | * |
| 223 | * @return array List of connections |
| 224 | */ |
| 225 | public function get( $post_array, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 226 | global $publicize; |
| 227 | |
| 228 | if ( ! $publicize ) { |
| 229 | return array(); |
| 230 | } |
| 231 | |
| 232 | $schema = $this->post_connection_schema(); |
| 233 | $properties = array_keys( $schema['properties'] ); |
| 234 | |
| 235 | $connections = $publicize->get_filtered_connection_data( $post_array['id'] ); |
| 236 | |
| 237 | $output_connections = array(); |
| 238 | foreach ( $connections as $connection ) { |
| 239 | $output_connection = array(); |
| 240 | foreach ( $properties as $property ) { |
| 241 | if ( isset( $connection[ $property ] ) ) { |
| 242 | $output_connection[ $property ] = $connection[ $property ]; |
| 243 | } |
| 244 | } |
| 245 | $output_connection['id'] = (string) $connection['unique_id']; |
| 246 | $output_connection['connection_id'] = (string) $connection['id']; |
| 247 | |
| 248 | $output_connections[] = $output_connection; |
| 249 | } |
| 250 | |
| 251 | return $output_connections; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Prior to updating the post, first calculate which Services to |
| 256 | * Publicize to and which to skip. |
| 257 | * |
| 258 | * @param object $post Post data to insert/update. |
| 259 | * @param WP_REST_Request $request API request. |
| 260 | * |
| 261 | * @return Filtered $post |
| 262 | */ |
| 263 | public function rest_pre_insert( $post, $request ) { |
| 264 | if ( ! isset( $request['jetpack_publicize_connections'] ) ) { |
| 265 | return $post; |
| 266 | } |
| 267 | |
| 268 | $permission_check = $this->update_permission_check( $request['jetpack_publicize_connections'], $post, $request ); |
| 269 | if ( is_wp_error( $permission_check ) ) { |
| 270 | return $permission_check; |
| 271 | } |
| 272 | // memoize. |
| 273 | $this->get_meta_to_update( $request['jetpack_publicize_connections'], isset( $post->ID ) ? $post->ID : 0 ); |
| 274 | |
| 275 | if ( isset( $post->ID ) ) { |
| 276 | // Set the meta before we mark the post as published so that publicize works as expected. |
| 277 | // If this is not the case post end up on social media when they are marked as skipped. |
| 278 | $this->update( $request['jetpack_publicize_connections'], $post, $request ); |
| 279 | } |
| 280 | |
| 281 | return $post; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * After creating a new post, update our cached data to reflect |
| 286 | * the new post ID. |
| 287 | * |
| 288 | * @param WP_Post $post Post data to update. |
| 289 | * @param WP_REST_Request $request API request. |
| 290 | * @param bool $is_new Is this a new post. |
| 291 | */ |
| 292 | public function rest_insert( $post, $request, $is_new ) { |
| 293 | if ( ! $is_new ) { |
| 294 | // An existing post was edited - no need to update |
| 295 | // our cache - we started out knowing the correct |
| 296 | // post ID. |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | if ( ! isset( $request['jetpack_publicize_connections'] ) ) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | if ( ! isset( $this->memoized_updates[0] ) ) { |
| 305 | return; |
| 306 | } |
| 307 | |
| 308 | $this->memoized_updates[ $post->ID ] = $this->memoized_updates[0]; |
| 309 | unset( $this->memoized_updates[0] ); |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * Get list of meta data to update per post ID. |
| 314 | * |
| 315 | * @param array $requested_connections Publicize connections to update. |
| 316 | * Items are either `{ id: (string) }` or `{ service_name: (string) }`. |
| 317 | * @param int $post_id Post ID. |
| 318 | */ |
| 319 | protected function get_meta_to_update( $requested_connections, $post_id = 0 ) { |
| 320 | global $publicize; |
| 321 | |
| 322 | if ( ! $publicize ) { |
| 323 | return array(); |
| 324 | } |
| 325 | |
| 326 | if ( isset( $this->memoized_updates[ $post_id ] ) ) { |
| 327 | return $this->memoized_updates[ $post_id ]; |
| 328 | } |
| 329 | |
| 330 | $available_connections = $publicize->get_filtered_connection_data( $post_id ); |
| 331 | |
| 332 | $changed_connections = array(); |
| 333 | |
| 334 | // Build lookup mappings. |
| 335 | $available_connections_by_connection_id = array(); |
| 336 | $available_connections_by_service_name = array(); |
| 337 | foreach ( $available_connections as $available_connection ) { |
| 338 | $available_connections_by_connection_id[ $available_connection['id'] ] = $available_connection; |
| 339 | |
| 340 | if ( ! isset( $available_connections_by_service_name[ $available_connection['service_name'] ] ) ) { |
| 341 | $available_connections_by_service_name[ $available_connection['service_name'] ] = array(); |
| 342 | } |
| 343 | $available_connections_by_service_name[ $available_connection['service_name'] ][] = $available_connection; |
| 344 | } |
| 345 | |
| 346 | // Handle { service_name: $service_name, enabled: (bool) }. |
| 347 | // If the service is not available, it will be skipped. |
| 348 | foreach ( $requested_connections as $requested_connection ) { |
| 349 | if ( ! isset( $requested_connection['service_name'] ) ) { |
| 350 | continue; |
| 351 | } |
| 352 | |
| 353 | if ( ! isset( $available_connections_by_service_name[ $requested_connection['service_name'] ] ) ) { |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | foreach ( $available_connections_by_service_name[ $requested_connection['service_name'] ] as $available_connection ) { |
| 358 | if ( $requested_connection['connection_id'] === $available_connection['id'] ) { |
| 359 | $changed_connections[ $available_connection['id'] ] = $requested_connection['enabled']; |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Handle { id: $id, enabled: (bool) } |
| 366 | // These override the service_name settings. |
| 367 | foreach ( $requested_connections as $requested_connection ) { |
| 368 | if ( ! isset( $requested_connection['connection_id'] ) ) { |
| 369 | continue; |
| 370 | } |
| 371 | |
| 372 | if ( ! isset( $available_connections_by_connection_id[ $requested_connection['connection_id'] ] ) ) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | $changed_connections[ $requested_connection['connection_id'] ] = $requested_connection['enabled']; |
| 377 | } |
| 378 | |
| 379 | // Set all changed connections to their new value. |
| 380 | foreach ( $changed_connections as $id => $enabled ) { |
| 381 | $connection = $available_connections_by_connection_id[ $id ]; |
| 382 | |
| 383 | if ( $connection['done'] || ! $connection['toggleable'] ) { |
| 384 | continue; |
| 385 | } |
| 386 | |
| 387 | $available_connections_by_connection_id[ $id ]['enabled'] = $enabled; |
| 388 | } |
| 389 | |
| 390 | $meta_to_update = array(); |
| 391 | // For all connections, ensure correct post_meta. |
| 392 | foreach ( $available_connections_by_connection_id as $connection_id => $available_connection ) { |
| 393 | if ( $available_connection['enabled'] ) { |
| 394 | $meta_to_update[ $publicize->POST_SKIP_PUBLICIZE . $connection_id ] = null; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 395 | } else { |
| 396 | $meta_to_update[ $publicize->POST_SKIP_PUBLICIZE . $connection_id ] = 1; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | $this->memoized_updates[ $post_id ] = $meta_to_update; |
| 401 | |
| 402 | return $meta_to_update; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * Update the connections slated to be shared to. |
| 407 | * |
| 408 | * @param array $requested_connections Publicize conenctions to update. |
| 409 | * Items are either `{ id: (string) }` or `{ service_name: (string) }`. |
| 410 | * @param WP_Post $post Post data. |
| 411 | * @param WP_REST_Request $request API request. |
| 412 | */ |
| 413 | public function update( $requested_connections, $post, $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 414 | if ( isset( $this->meta_saved[ $post->ID ] ) ) { // Make sure we only save it once - per request. |
| 415 | return; |
| 416 | } |
| 417 | foreach ( $this->get_meta_to_update( $requested_connections, $post->ID ) as $meta_key => $meta_value ) { |
| 418 | if ( $meta_value === null ) { |
| 419 | delete_post_meta( $post->ID, $meta_key ); |
| 420 | } else { |
| 421 | update_post_meta( $post->ID, $meta_key, $meta_value ); |
| 422 | } |
| 423 | } |
| 424 | $this->meta_saved[ $post->ID ] = true; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | if ( Jetpack::is_module_active( 'publicize' ) ) { |
| 429 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Post_Publicize_Connections_Field' ); |
| 430 | } |
| 431 |