jetpack
/
_inc
/
lib
/
core-api
/
wpcom-endpoints
/
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
| 1 | <?php |
| 2 | /** |
| 3 | * Publicize: Share post |
| 4 | * |
| 5 | * This file is synced from the Jetpack monorepo to WPCOM. |
| 6 | * |
| 7 | * @package automattic/jetpack |
| 8 | * @since |
| 9 | */ |
| 10 | |
| 11 | use Automattic\Jetpack\Connection\Client; |
| 12 | |
| 13 | require_once __DIR__ . '/publicize-connections.php'; |
| 14 | |
| 15 | /** |
| 16 | * Publicize: Share post class. |
| 17 | */ |
| 18 | class WPCOM_REST_API_V2_Endpoint_Publicize_Share_Post extends WP_REST_Controller { |
| 19 | /** |
| 20 | * The constructor sets the route namespace, rest_base, and registers our API route and endpoint. |
| 21 | * Additionally, we check if we're executing this file on WPCOM or Jetpack. |
| 22 | */ |
| 23 | public function __construct() { |
| 24 | $this->namespace = 'wpcom/v2'; |
| 25 | |
| 26 | // $wpcom_is_wpcom_only_endpoint = true keeps WPCOM from trying to loop back to the Jetpack endpoint. |
| 27 | $this->wpcom_is_wpcom_only_endpoint = true; |
| 28 | |
| 29 | // Determine if this endpoint is running on WPCOM or not. |
| 30 | $this->is_wpcom = false; |
| 31 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 32 | $this->is_wpcom = true; |
| 33 | } |
| 34 | |
| 35 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * This file is synced from Jetpack to WPCOM and this method creates a slightly different route for both sites. |
| 40 | * Jetpack route: http://{$site}/wp-json/wpcom/v2/posts/{$postId}/publicize |
| 41 | * WPCOM route: https://public-api.wordpress.com/wpcom/v2/sites/{$siteId}/posts/{$postId}/publicize |
| 42 | */ |
| 43 | public function register_routes() { |
| 44 | register_rest_route( |
| 45 | $this->namespace, |
| 46 | '/posts/(?P<postId>\d+)/publicize', |
| 47 | array( |
| 48 | 'methods' => WP_REST_Server::CREATABLE, |
| 49 | 'callback' => array( $this, 'share_post' ), |
| 50 | 'permission_callback' => array( $this, 'permissions_check' ), |
| 51 | 'args' => array( |
| 52 | 'message' => array( |
| 53 | 'description' => __( 'The message to share.', 'jetpack' ), |
| 54 | 'type' => 'string', |
| 55 | 'required' => true, |
| 56 | 'validate_callback' => function ( $param ) { |
| 57 | return is_string( $param ); |
| 58 | }, |
| 59 | 'sanitize_callback' => 'sanitize_textarea_field', |
| 60 | ), |
| 61 | 'skipped_connections' => array( |
| 62 | 'description' => __( 'Array of external connection IDs to skip sharing.', 'jetpack' ), |
| 63 | 'type' => 'array', |
| 64 | 'required' => false, |
| 65 | 'validate_callback' => function ( $param ) { |
| 66 | return is_array( $param ); |
| 67 | }, |
| 68 | 'sanitize_callback' => function ( $param ) { |
| 69 | return array_map( 'absint', $param ); |
| 70 | }, |
| 71 | ), |
| 72 | ), |
| 73 | ), |
| 74 | // override = true because this API route was commandeered from the file |
| 75 | // wp-content/rest-api-plugins/endpoints/sites-publicize.php on WPCOM. |
| 76 | true |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Ensure the user has proper tokens and permissions to publish posts on this blog. |
| 82 | * |
| 83 | * @return WP_Error|boolean |
| 84 | */ |
| 85 | public function permissions_check() { |
| 86 | if ( ! get_current_user_id() ) { |
| 87 | return new WP_Error( |
| 88 | 'rest_cannot_view', |
| 89 | __( 'Sorry, you cannot view this resource without a valid token for this blog.', 'jetpack' ), |
| 90 | array( 'status' => rest_authorization_required_code() ) |
| 91 | ); |
| 92 | } |
| 93 | if ( ! current_user_can( 'publish_posts' ) ) { |
| 94 | return new WP_Error( 'unauthorized', 'Your token must have permission to publish posts.', array( 'status' => 401 ) ); |
| 95 | } |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * If this method callback is executed on WPCOM, we share the post using republicize_post(). If this method callback |
| 101 | * is executed on a Jetpack site, we make an API call to WPCOM using wpcom_json_api_request_as_user() and return |
| 102 | * the results. In both cases, this file and method are executed, as this file is synced from Jetpack to WPCOM. |
| 103 | * |
| 104 | * @param WP_REST_Request $request Full details about the request. |
| 105 | * @return WP_Error|array The publicize results, including two arrays: `results` and `errors` |
| 106 | */ |
| 107 | public function share_post( $request ) { |
| 108 | $post_id = $request->get_param( 'postId' ); |
| 109 | $message = trim( $request->get_param( 'message' ) ); |
| 110 | $skip_connection_ids = $request->get_param( 'skipped_connections' ); |
| 111 | |
| 112 | if ( $this->is_wpcom ) { |
| 113 | $post = get_post( $post_id ); |
| 114 | |
| 115 | if ( empty( $post ) ) { |
| 116 | return new WP_Error( 'not_found', 'Cannot find that post', array( 'status' => 404 ) ); |
| 117 | } |
| 118 | if ( 'publish' !== $post->post_status ) { |
| 119 | return new WP_Error( 'not_published', 'Cannot share an unpublished post', array( 'status' => 400 ) ); |
| 120 | } |
| 121 | |
| 122 | $publicize = publicize_init(); |
| 123 | $result = $publicize->republicize_post( (int) $post_id, $message, $skip_connection_ids, true ); |
| 124 | if ( false === $result ) { |
| 125 | return new WP_Error( 'not_found', 'Cannot find that post', array( 'status' => 404 ) ); |
| 126 | } |
| 127 | |
| 128 | return $result; |
| 129 | } else { |
| 130 | $response = $this->proxy_request( $post_id, $message, $skip_connection_ids ); |
| 131 | if ( is_wp_error( $response ) ) { |
| 132 | return rest_ensure_response( $response ); |
| 133 | } |
| 134 | |
| 135 | return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Passes the request on to the WPCOM endpoint, and returns the result. |
| 141 | * |
| 142 | * @param int $post_id The post ID being shared. |
| 143 | * @param string $message The custom message to be used. |
| 144 | * @param array $skip_connection_ids An array of connection IDs where the post shouldn't be shared. |
| 145 | * |
| 146 | * @return array|WP_Error $response Response data, else WP_Error on failure. |
| 147 | */ |
| 148 | public function proxy_request( $post_id, $message, $skip_connection_ids ) { |
| 149 | /* |
| 150 | * Publicize endpoint on WPCOM: |
| 151 | * [POST] wpcom/v2/sites/{$siteId}/posts/{$postId}/publicize |
| 152 | * body: |
| 153 | * - message: string |
| 154 | * - skipped_connections: array of connection ids to skip |
| 155 | */ |
| 156 | $url = sprintf( |
| 157 | '/sites/%d/posts/%d/publicize', |
| 158 | Jetpack_Options::get_option( 'id' ), |
| 159 | $post_id |
| 160 | ); |
| 161 | |
| 162 | return Client::wpcom_json_api_request_as_user( |
| 163 | $url, |
| 164 | 'v2', |
| 165 | array( |
| 166 | 'method' => 'POST', |
| 167 | ), |
| 168 | array( |
| 169 | 'message' => $message, |
| 170 | 'skipped_connections' => $skip_connection_ids, |
| 171 | ) |
| 172 | ); |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Publicize_Share_Post' ); |
| 177 |