Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
97.45% |
268 / 275 |
|
73.33% |
11 / 15 |
CRAP | |
0.00% |
0 / 1 |
| Social_Abilities | |
98.17% |
268 / 273 |
|
73.33% |
11 / 15 |
57 | |
0.00% |
0 / 1 |
| enabled_by_default | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| get_category_slug | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_category_definition | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| register_category | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| register_abilities | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| social_is_on | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_abilities | |
100.00% |
142 / 142 |
|
100.00% |
1 / 1 |
1 | |||
| connection_schema | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
1 | |||
| can_view_connections | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get_connections | |
96.00% |
24 / 25 |
|
0.00% |
0 / 1 |
11 | |||
| share_post | |
98.04% |
50 / 51 |
|
0.00% |
0 / 1 |
19 | |||
| delete_connection | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
5 | |||
| post_type_is_shareable | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| connection_is_listed | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 | |||
| dispatch | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jetpack Social Abilities Registration. |
| 4 | * |
| 5 | * Registers Jetpack Social abilities with the WordPress Abilities API so AI |
| 6 | * agents can list connections, share a published post and remove a connection. |
| 7 | * |
| 8 | * @package automattic/jetpack-publicize |
| 9 | */ |
| 10 | |
| 11 | namespace Automattic\Jetpack\Publicize\Abilities; |
| 12 | |
| 13 | use Automattic\Jetpack\Publicize\Publicize_Utils; |
| 14 | use Automattic\Jetpack\WP_Abilities\Registrar; |
| 15 | use WP_Error; |
| 16 | use WP_REST_Request; |
| 17 | use WP_REST_Response; |
| 18 | |
| 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 20 | exit( 0 ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Registers Jetpack Social abilities with the WordPress Abilities API. |
| 25 | * |
| 26 | * Each ability calls the matching `wpcom/v2/publicize` REST route in-process, so |
| 27 | * the route's own permission and ownership checks run and Jetpack and WordPress.com |
| 28 | * sites share one code path. Creating a connection needs OAuth and stays in the UI. |
| 29 | * |
| 30 | * - `jetpack-social/get-connections` — list the connected social accounts. |
| 31 | * - `jetpack-social/share-post` — share a published post to its connections now. |
| 32 | * - `jetpack-social/delete-connection` — remove one connection. |
| 33 | */ |
| 34 | class Social_Abilities extends Registrar { |
| 35 | |
| 36 | const CONNECTIONS_ROUTE = '/wpcom/v2/publicize/connections'; |
| 37 | const SHARE_POST_ROUTE = '/wpcom/v2/publicize/share-post'; |
| 38 | const MAX_SKIPPED = 100; |
| 39 | |
| 40 | /** |
| 41 | * {@inheritDoc} |
| 42 | */ |
| 43 | protected static function enabled_by_default(): bool { |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Return the ability category slug. |
| 49 | * |
| 50 | * @return string |
| 51 | */ |
| 52 | public static function get_category_slug(): string { |
| 53 | return 'jetpack-social'; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Return the ability category definition. |
| 58 | * |
| 59 | * @return array |
| 60 | */ |
| 61 | public static function get_category_definition(): array { |
| 62 | return array( |
| 63 | 'label' => __( 'Jetpack Social', 'jetpack-publicize-pkg' ), |
| 64 | 'description' => __( 'Abilities for the social networks connected to the site and for sharing posts to them.', 'jetpack-publicize-pkg' ), |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Register the category only while Jetpack Social sharing is on. |
| 70 | * |
| 71 | * @return void |
| 72 | */ |
| 73 | public static function register_category() { |
| 74 | if ( self::social_is_on() ) { |
| 75 | parent::register_category(); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Register the abilities only while Jetpack Social sharing is on. |
| 81 | * |
| 82 | * With the Publicize module off, its REST routes do not exist and every call would fail. |
| 83 | * |
| 84 | * @return void |
| 85 | */ |
| 86 | public static function register_abilities() { |
| 87 | if ( self::social_is_on() ) { |
| 88 | parent::register_abilities(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Is Jetpack Social sharing turned on for this site? |
| 94 | * |
| 95 | * @return bool |
| 96 | */ |
| 97 | private static function social_is_on(): bool { |
| 98 | /** |
| 99 | * Filters whether the Jetpack Social abilities register. Defaults to whether the Publicize module is active. |
| 100 | * |
| 101 | * @since 0.90.0 |
| 102 | * |
| 103 | * @param bool $should_load Whether to register the Jetpack Social abilities. |
| 104 | */ |
| 105 | return (bool) apply_filters( 'jetpack_social_abilities_should_load', Publicize_Utils::is_publicize_active() ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Return the abilities this Registrar exposes, keyed by slug. |
| 110 | * |
| 111 | * @return array<string, array<string, mixed>> |
| 112 | */ |
| 113 | public static function get_abilities(): array { |
| 114 | $connection_id_schema = array( |
| 115 | 'type' => 'integer', |
| 116 | 'minimum' => 1, |
| 117 | 'description' => __( 'Connection ID from the list of the site\'s social connections.', 'jetpack-publicize-pkg' ), |
| 118 | ); |
| 119 | |
| 120 | $mcp = array( |
| 121 | 'public' => true, |
| 122 | 'type' => 'tool', |
| 123 | ); |
| 124 | |
| 125 | return array( |
| 126 | 'jetpack-social/get-connections' => array( |
| 127 | 'label' => __( 'Get social connections', 'jetpack-publicize-pkg' ), |
| 128 | 'description' => __( |
| 129 | 'List the social network accounts connected to this site through Jetpack Social. `shared` is true when every author on the site can use the connection. Filter by `service_name` (for example facebook or linkedin). Set `test_connections` to check each connection against its network, which is slower; `status` is then ok, broken or must_reauth, and is null without it. A connection that is not ok cannot publish until it is reconnected in the Jetpack Social settings. Returns an empty list when nothing is connected. On a Jetpack site the current user must be connected to WordPress.com.', |
| 130 | 'jetpack-publicize-pkg' |
| 131 | ), |
| 132 | 'input_schema' => array( |
| 133 | 'type' => 'object', |
| 134 | 'default' => array(), |
| 135 | 'properties' => array( |
| 136 | 'service_name' => array( |
| 137 | 'type' => 'string', |
| 138 | 'description' => __( 'Only return connections to this network, for example facebook.', 'jetpack-publicize-pkg' ), |
| 139 | 'pattern' => '^[a-z0-9_-]+$', |
| 140 | ), |
| 141 | 'test_connections' => array( |
| 142 | 'type' => 'boolean', |
| 143 | 'description' => __( 'Check each connection against its network before answering.', 'jetpack-publicize-pkg' ), |
| 144 | 'default' => false, |
| 145 | ), |
| 146 | ), |
| 147 | 'additionalProperties' => false, |
| 148 | ), |
| 149 | 'output_schema' => array( |
| 150 | 'type' => 'object', |
| 151 | 'properties' => array( |
| 152 | 'total' => array( 'type' => 'integer' ), |
| 153 | 'connections' => array( |
| 154 | 'type' => 'array', |
| 155 | 'items' => self::connection_schema(), |
| 156 | ), |
| 157 | ), |
| 158 | ), |
| 159 | 'execute_callback' => array( __CLASS__, 'get_connections' ), |
| 160 | 'permission_callback' => array( __CLASS__, 'can_view_connections' ), |
| 161 | 'meta' => array( |
| 162 | 'annotations' => array( |
| 163 | 'readonly' => true, |
| 164 | 'destructive' => false, |
| 165 | 'idempotent' => true, |
| 166 | ), |
| 167 | 'public' => true, |
| 168 | 'show_in_rest' => true, |
| 169 | 'mcp' => $mcp, |
| 170 | ), |
| 171 | ), |
| 172 | 'jetpack-social/share-post' => array( |
| 173 | 'label' => __( 'Share a post to social networks', 'jetpack-publicize-pkg' ), |
| 174 | 'description' => __( |
| 175 | 'Share one published post to the site\'s connected social networks right now, as a new public post on each network. `connections` in the result counts the working accounts the share goes to. Each connection is checked against its network first, and the call returns an error when no working connection would receive the share. The shares are queued and go out in the background, so the call returns before they are posted. Each call publishes again, so repeating it posts a duplicate. Only published posts without a password can be shared, and only post types that Jetpack Social supports (posts by default). `message` is optional text posted with the link. Connections listed in `skipped_connections` are left out. Requires at least one working connection. Works on posts the user can edit. On a Jetpack site the current user must be connected to WordPress.com.', |
| 176 | 'jetpack-publicize-pkg' |
| 177 | ), |
| 178 | 'input_schema' => array( |
| 179 | 'type' => 'object', |
| 180 | 'required' => array( 'post_id' ), |
| 181 | 'properties' => array( |
| 182 | 'post_id' => array( |
| 183 | 'type' => 'integer', |
| 184 | 'minimum' => 1, |
| 185 | 'description' => __( 'ID of the published post to share.', 'jetpack-publicize-pkg' ), |
| 186 | ), |
| 187 | 'message' => array( |
| 188 | 'type' => 'string', |
| 189 | 'maxLength' => 1000, |
| 190 | 'description' => __( 'Optional text to post with the link. When omitted or empty, a message saved on a connection is used, then the custom message saved on the post, then each network\'s default text built from the post title and link.', 'jetpack-publicize-pkg' ), |
| 191 | ), |
| 192 | 'skipped_connections' => array( |
| 193 | 'type' => 'array', |
| 194 | 'description' => __( 'Connection IDs to leave out of this share.', 'jetpack-publicize-pkg' ), |
| 195 | 'items' => $connection_id_schema, |
| 196 | 'maxItems' => self::MAX_SKIPPED, |
| 197 | ), |
| 198 | ), |
| 199 | 'additionalProperties' => false, |
| 200 | ), |
| 201 | 'output_schema' => array( |
| 202 | 'type' => 'object', |
| 203 | 'properties' => array( |
| 204 | 'post_id' => array( 'type' => 'integer' ), |
| 205 | 'queued' => array( 'type' => 'boolean' ), |
| 206 | 'connections' => array( 'type' => 'integer' ), |
| 207 | ), |
| 208 | ), |
| 209 | 'execute_callback' => array( __CLASS__, 'share_post' ), |
| 210 | 'permission_callback' => array( __CLASS__, 'can_view_connections' ), |
| 211 | 'meta' => array( |
| 212 | 'annotations' => array( |
| 213 | 'readonly' => false, |
| 214 | 'destructive' => true, |
| 215 | 'idempotent' => false, |
| 216 | 'openWorldHint' => true, |
| 217 | ), |
| 218 | 'public' => true, |
| 219 | 'show_in_rest' => true, |
| 220 | 'mcp' => $mcp, |
| 221 | ), |
| 222 | ), |
| 223 | 'jetpack-social/delete-connection' => array( |
| 224 | 'label' => __( 'Delete a social connection', 'jetpack-publicize-pkg' ), |
| 225 | 'description' => __( |
| 226 | 'Remove one connected social account from the site. Posts already shared stay on the network; future shares skip this account. A connection cannot be restored through abilities, because connecting an account needs a sign-in on the network in the Jetpack Social settings. Editors can delete any connection; other users can delete their own. An unknown connection_id returns an error. On a Jetpack site the current user must be connected to WordPress.com.', |
| 227 | 'jetpack-publicize-pkg' |
| 228 | ), |
| 229 | 'input_schema' => array( |
| 230 | 'type' => 'object', |
| 231 | 'required' => array( 'connection_id' ), |
| 232 | 'properties' => array( |
| 233 | 'connection_id' => $connection_id_schema, |
| 234 | ), |
| 235 | 'additionalProperties' => false, |
| 236 | ), |
| 237 | 'output_schema' => array( |
| 238 | 'type' => 'object', |
| 239 | 'properties' => array( |
| 240 | 'deleted' => array( 'type' => 'boolean' ), |
| 241 | 'connection_id' => array( 'type' => 'integer' ), |
| 242 | ), |
| 243 | ), |
| 244 | 'execute_callback' => array( __CLASS__, 'delete_connection' ), |
| 245 | 'permission_callback' => array( __CLASS__, 'can_view_connections' ), |
| 246 | 'meta' => array( |
| 247 | 'annotations' => array( |
| 248 | 'readonly' => false, |
| 249 | 'destructive' => true, |
| 250 | 'idempotent' => true, |
| 251 | ), |
| 252 | 'public' => true, |
| 253 | 'show_in_rest' => true, |
| 254 | 'mcp' => $mcp, |
| 255 | ), |
| 256 | ), |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Output schema for one connection. |
| 262 | * |
| 263 | * @return array |
| 264 | */ |
| 265 | private static function connection_schema(): array { |
| 266 | return array( |
| 267 | 'type' => 'object', |
| 268 | 'properties' => array( |
| 269 | 'connection_id' => array( 'type' => 'integer' ), |
| 270 | 'service_name' => array( 'type' => 'string' ), |
| 271 | 'service_label' => array( 'type' => 'string' ), |
| 272 | 'display_name' => array( 'type' => 'string' ), |
| 273 | 'external_handle' => array( 'type' => array( 'string', 'null' ) ), |
| 274 | 'profile_link' => array( 'type' => 'string' ), |
| 275 | 'shared' => array( 'type' => 'boolean' ), |
| 276 | 'status' => array( 'type' => array( 'string', 'null' ) ), |
| 277 | ), |
| 278 | ); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Can the current user see and manage Jetpack Social connections? |
| 283 | * |
| 284 | * @return bool |
| 285 | */ |
| 286 | public static function can_view_connections(): bool { |
| 287 | return Publicize_Utils::current_user_can_access_publicize_data(); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Execute jetpack-social/get-connections. |
| 292 | * |
| 293 | * @param mixed $input Ability input. |
| 294 | * @return array|WP_Error |
| 295 | */ |
| 296 | public static function get_connections( $input = null ) { |
| 297 | $input = is_array( $input ) ? $input : array(); |
| 298 | $request = new WP_REST_Request( 'GET', self::CONNECTIONS_ROUTE ); |
| 299 | $request->set_param( 'test_connections', rest_sanitize_boolean( $input['test_connections'] ?? false ) ); |
| 300 | |
| 301 | $items = self::dispatch( $request ); |
| 302 | if ( is_wp_error( $items ) ) { |
| 303 | return $items; |
| 304 | } |
| 305 | |
| 306 | $service = isset( $input['service_name'] ) && is_string( $input['service_name'] ) ? $input['service_name'] : ''; |
| 307 | $connections = array(); |
| 308 | foreach ( (array) $items as $item ) { |
| 309 | if ( ! is_array( $item ) || ( '' !== $service && ( $item['service_name'] ?? '' ) !== $service ) ) { |
| 310 | continue; |
| 311 | } |
| 312 | $connections[] = array( |
| 313 | 'connection_id' => (int) ( $item['connection_id'] ?? 0 ), |
| 314 | 'service_name' => (string) ( $item['service_name'] ?? '' ), |
| 315 | 'service_label' => (string) ( $item['service_label'] ?? '' ), |
| 316 | 'display_name' => (string) ( $item['display_name'] ?? '' ), |
| 317 | 'external_handle' => isset( $item['external_handle'] ) ? (string) $item['external_handle'] : null, |
| 318 | 'profile_link' => (string) ( $item['profile_link'] ?? '' ), |
| 319 | 'shared' => ! empty( $item['shared'] ), |
| 320 | 'status' => isset( $item['status'] ) ? (string) $item['status'] : null, |
| 321 | ); |
| 322 | } |
| 323 | |
| 324 | return array( |
| 325 | 'total' => count( $connections ), |
| 326 | 'connections' => $connections, |
| 327 | ); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * Execute jetpack-social/share-post. |
| 332 | * |
| 333 | * @param mixed $input Ability input. |
| 334 | * @return array|WP_Error |
| 335 | */ |
| 336 | public static function share_post( $input = null ) { |
| 337 | $input = is_array( $input ) ? $input : array(); |
| 338 | $post_id = (int) ( $input['post_id'] ?? 0 ); |
| 339 | $message = trim( (string) ( $input['message'] ?? '' ) ); |
| 340 | |
| 341 | $post = get_post( $post_id ); |
| 342 | if ( ! $post ) { |
| 343 | return new WP_Error( 'jetpack_social_post_not_found', __( 'No post with that id.', 'jetpack-publicize-pkg' ), array( 'status' => 404 ) ); |
| 344 | } |
| 345 | |
| 346 | if ( ! current_user_can( 'publish_post', $post_id ) || ! current_user_can( 'edit_post', $post_id ) ) { |
| 347 | return new WP_Error( 'jetpack_social_cannot_share_post', __( 'You can only share posts you are allowed to edit.', 'jetpack-publicize-pkg' ), array( 'status' => 403 ) ); |
| 348 | } |
| 349 | if ( 'publish' !== $post->post_status ) { |
| 350 | return new WP_Error( 'jetpack_social_not_published', __( 'Only published posts can be shared.', 'jetpack-publicize-pkg' ), array( 'status' => 400 ) ); |
| 351 | } |
| 352 | if ( '' !== $post->post_password ) { |
| 353 | return new WP_Error( 'jetpack_social_password_protected', __( 'Password-protected posts cannot be shared.', 'jetpack-publicize-pkg' ), array( 'status' => 400 ) ); |
| 354 | } |
| 355 | if ( ! self::post_type_is_shareable( $post->post_type ) ) { |
| 356 | return new WP_Error( 'jetpack_social_post_type_not_shareable', __( 'Jetpack Social does not share this post type.', 'jetpack-publicize-pkg' ), array( 'status' => 400 ) ); |
| 357 | } |
| 358 | |
| 359 | $body = array( |
| 360 | 'message' => $message, |
| 361 | 'async' => true, |
| 362 | ); |
| 363 | if ( ! empty( $input['skipped_connections'] ) && is_array( $input['skipped_connections'] ) ) { |
| 364 | $body['skipped_connections'] = array_map( 'absint', $input['skipped_connections'] ); |
| 365 | } |
| 366 | |
| 367 | // Without test_connections the route reports every status as null, so broken accounts would count as working. |
| 368 | $skipped = $body['skipped_connections'] ?? array(); |
| 369 | $list = new WP_REST_Request( 'GET', self::CONNECTIONS_ROUTE ); |
| 370 | $list->set_param( 'test_connections', true ); |
| 371 | $items = self::dispatch( $list ); |
| 372 | if ( is_wp_error( $items ) ) { |
| 373 | return $items; |
| 374 | } |
| 375 | $reachable = 0; |
| 376 | foreach ( (array) $items as $item ) { |
| 377 | $working = is_array( $item ) && ! in_array( $item['status'] ?? null, array( 'broken', 'must_reauth' ), true ); |
| 378 | if ( $working && ! in_array( (int) ( $item['connection_id'] ?? 0 ), $skipped, true ) ) { |
| 379 | ++$reachable; |
| 380 | } |
| 381 | } |
| 382 | if ( 0 === $reachable ) { |
| 383 | return new WP_Error( |
| 384 | 'jetpack_social_no_connections', |
| 385 | __( 'No working social connection would receive this share. Call jetpack-social/get-connections to see the accounts and their status; connecting an account is done in the Jetpack Social settings.', 'jetpack-publicize-pkg' ), |
| 386 | array( 'status' => 400 ) |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | // A JSON body is what the Jetpack-site proxy forwards to WordPress.com. |
| 391 | $request = new WP_REST_Request( 'POST', self::SHARE_POST_ROUTE . '/' . $post_id ); |
| 392 | $request->set_header( 'Content-Type', 'application/json' ); |
| 393 | $request->set_body( (string) wp_json_encode( $body, JSON_UNESCAPED_SLASHES ) ); |
| 394 | |
| 395 | $result = self::dispatch( $request ); |
| 396 | if ( is_wp_error( $result ) ) { |
| 397 | return $result; |
| 398 | } |
| 399 | |
| 400 | $data = (array) $result; |
| 401 | if ( ! empty( $data['errors'] ) && empty( $data['results'] ) ) { |
| 402 | return new WP_Error( 'jetpack_social_share_failed', __( 'No connection accepted the share. Call jetpack-social/get-connections to check their status.', 'jetpack-publicize-pkg' ) ); |
| 403 | } |
| 404 | |
| 405 | return array( |
| 406 | 'post_id' => $post_id, |
| 407 | 'queued' => true, |
| 408 | 'connections' => $reachable, |
| 409 | ); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Execute jetpack-social/delete-connection. |
| 414 | * |
| 415 | * @param mixed $input Ability input. |
| 416 | * @return array|WP_Error |
| 417 | */ |
| 418 | public static function delete_connection( $input = null ) { |
| 419 | $input = is_array( $input ) ? $input : array(); |
| 420 | $connection_id = (int) ( $input['connection_id'] ?? 0 ); |
| 421 | |
| 422 | $result = self::dispatch( new WP_REST_Request( 'DELETE', self::CONNECTIONS_ROUTE . '/' . $connection_id ) ); |
| 423 | if ( is_wp_error( $result ) ) { |
| 424 | // The route answers an unknown id with a generic failure; confirm against the list. |
| 425 | if ( empty( $result->get_error_data()['status'] ) && ! self::connection_is_listed( $connection_id ) ) { |
| 426 | return new WP_Error( |
| 427 | 'jetpack_social_connection_not_found', |
| 428 | __( 'No connection with that id. Call jetpack-social/get-connections to list the ids.', 'jetpack-publicize-pkg' ), |
| 429 | array( 'status' => 404 ) |
| 430 | ); |
| 431 | } |
| 432 | return $result; |
| 433 | } |
| 434 | |
| 435 | return array( |
| 436 | 'deleted' => true, |
| 437 | 'connection_id' => $connection_id, |
| 438 | ); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * Does Jetpack Social share posts of this type? |
| 443 | * |
| 444 | * @param string $post_type Post type. |
| 445 | * @return bool |
| 446 | */ |
| 447 | private static function post_type_is_shareable( string $post_type ): bool { |
| 448 | // Same rule as Publicize_Base::post_type_is_publicizeable(); reading the global $publicize here widens Phan's type for it. |
| 449 | return 'post' === $post_type || post_type_supports( $post_type, 'publicize' ); |
| 450 | } |
| 451 | |
| 452 | /** |
| 453 | * Is this connection id in the list the current user can see? |
| 454 | * |
| 455 | * @param int $connection_id Connection id. |
| 456 | * @return bool |
| 457 | */ |
| 458 | private static function connection_is_listed( int $connection_id ): bool { |
| 459 | $items = self::dispatch( new WP_REST_Request( 'GET', self::CONNECTIONS_ROUTE ) ); |
| 460 | |
| 461 | foreach ( is_wp_error( $items ) ? array() : (array) $items as $item ) { |
| 462 | if ( is_array( $item ) && (int) ( $item['connection_id'] ?? 0 ) === $connection_id ) { |
| 463 | return true; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | return false; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Run a request against the REST server and return its data, or a WP_Error. |
| 472 | * |
| 473 | * @param WP_REST_Request $request The request. |
| 474 | * @return mixed|WP_Error |
| 475 | */ |
| 476 | private static function dispatch( WP_REST_Request $request ) { |
| 477 | $response = rest_do_request( $request ); |
| 478 | |
| 479 | if ( $response instanceof WP_REST_Response && $response->is_error() ) { |
| 480 | $error = $response->as_error(); |
| 481 | return new WP_Error( |
| 482 | 'jetpack_social_' . $error->get_error_code(), |
| 483 | $error->get_error_message(), |
| 484 | $error->get_error_data() |
| 485 | ); |
| 486 | } |
| 487 | |
| 488 | return $response instanceof WP_REST_Response ? $response->get_data() : $response; |
| 489 | } |
| 490 | } |