← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-following.php
+61
-13
12.2.3
→
16.3-a.7
View file →
| @@ -1,7 +1,7 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * REST API endpoint for the Jetpack author recommendations block. | |
| 3 | + * REST API endpoint for the Jetpack Blogroll block. | |
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| 6 | 6 | * @since 12.2 |
| 7 | 7 | */ |
| @@ -8,8 +8,12 @@ | ||
| 8 | 8 | |
| 9 | 9 | use Automattic\Jetpack\Connection\Client; |
| 10 | 10 | use Automattic\Jetpack\Status\Visitor; |
| 11 | 11 | |
| 12 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 13 | + exit( 0 ); | |
| 14 | +} | |
| 15 | + | |
| 12 | 16 | /** |
| 13 | 17 | * Class WPCOM_REST_API_V2_Endpoint_Following |
| 14 | 18 | */ |
| 15 | 19 | class WPCOM_REST_API_V2_Endpoint_Following extends WP_REST_Controller { |
| @@ -46,10 +50,10 @@ | ||
| 46 | 50 | $this->rest_base . '/mine', |
| 47 | 51 | array( |
| 48 | 52 | array( |
| 49 | 53 | 'methods' => WP_REST_Server::READABLE, |
| 50 | - 'callback' => array( $this, 'get_response' ), | |
| 51 | - 'permission_callback' => array( $this, 'permission_check' ), | |
| 54 | + 'callback' => array( $this, 'get_following' ), | |
| 55 | + 'permission_callback' => 'is_user_logged_in', | |
| 52 | 56 | 'args' => array( |
| 53 | 57 | 'ignore_user_blogs' => array( |
| 54 | 58 | 'type' => 'boolean', |
| 55 | 59 | ), |
| @@ -56,17 +60,29 @@ | ||
| 56 | 60 | ), |
| 57 | 61 | ), |
| 58 | 62 | ) |
| 59 | 63 | ); |
| 60 | - } | |
| 61 | 64 | |
| 62 | - /** | |
| 63 | - * Check if user has edit post permission. | |
| 64 | - * | |
| 65 | - * @return true|WP_Error True if the request has access to create items, WP_Error object otherwise. | |
| 66 | - */ | |
| 67 | - public function permission_check() { | |
| 68 | - return current_user_can( 'edit_posts' ); | |
| 65 | + register_rest_route( | |
| 66 | + $this->namespace, | |
| 67 | + $this->rest_base . '/recommendations', | |
| 68 | + array( | |
| 69 | + array( | |
| 70 | + 'methods' => WP_REST_Server::READABLE, | |
| 71 | + 'callback' => array( $this, 'get_recommendations' ), | |
| 72 | + 'permission_callback' => 'is_user_logged_in', | |
| 73 | + 'args' => array( | |
| 74 | + 'number' => array( | |
| 75 | + 'type' => 'number', | |
| 76 | + 'default' => 5, | |
| 77 | + 'validate_callback' => function ( $param ) { | |
| 78 | + return is_numeric( $param ) && $param <= 20; | |
| 79 | + }, | |
| 80 | + ), | |
| 81 | + ), | |
| 82 | + ), | |
| 83 | + ) | |
| 84 | + ); | |
| 69 | 85 | } |
| 70 | 86 | |
| 71 | 87 | /** |
| 72 | 88 | * Gets the sites the user is following |
| @@ -73,13 +89,13 @@ | ||
| 73 | 89 | * |
| 74 | 90 | * @param WP_REST_Request $request Full details about the request. |
| 75 | 91 | * @return array|WP_Error list of followed sites, WP_Error otherwise |
| 76 | 92 | */ |
| 77 | - public function get_response( $request ) { | |
| 93 | + public function get_following( $request ) { | |
| 78 | 94 | $ignore_user_blogs = $request->get_param( 'ignore_user_blogs' ); |
| 79 | 95 | |
| 80 | 96 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 81 | - require_once WP_CONTENT_DIR . '/lib/wpcom-get-user-followed-blogs.php'; | |
| 97 | + require_lib( 'wpcom-get-user-followed-blogs' ); | |
| 82 | 98 | return get_user_followed_blogs( get_current_user_id(), $ignore_user_blogs ); |
| 83 | 99 | } |
| 84 | 100 | |
| 85 | 101 | $body = Client::wpcom_json_api_request_as_user( |
| @@ -100,7 +116,39 @@ | ||
| 100 | 116 | |
| 101 | 117 | return json_decode( wp_remote_retrieve_body( $body ) ); |
| 102 | 118 | } |
| 103 | 119 | |
| 120 | + /** | |
| 121 | + * Gets recommended sites for user | |
| 122 | + * | |
| 123 | + * @param WP_REST_Request $request Full details about the request. | |
| 124 | + * @return array|WP_Error list of following recommendations, WP_Error otherwise | |
| 125 | + */ | |
| 126 | + public function get_recommendations( $request ) { | |
| 127 | + $number_of_recommendations = $request->get_param( 'number' ); | |
| 128 | + | |
| 129 | + if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { | |
| 130 | + require_lib( 'wpcom-get-user-followed-blogs' ); | |
| 131 | + return get_user_following_recommendations( get_current_user_id(), $number_of_recommendations ); | |
| 132 | + } | |
| 133 | + | |
| 134 | + $body = Client::wpcom_json_api_request_as_user( | |
| 135 | + sprintf( '/me/following/recommendations?number=%d', $number_of_recommendations ), | |
| 136 | + '2', | |
| 137 | + array( | |
| 138 | + 'method' => 'GET', | |
| 139 | + 'headers' => array( | |
| 140 | + 'Content-Type' => 'application/json', | |
| 141 | + 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), | |
| 142 | + ), | |
| 143 | + ) | |
| 144 | + ); | |
| 145 | + | |
| 146 | + if ( is_wp_error( $body ) ) { | |
| 147 | + return $body; | |
| 148 | + } | |
| 149 | + | |
| 150 | + return json_decode( wp_remote_retrieve_body( $body ) ); | |
| 151 | + } | |
| 104 | 152 | } |
| 105 | 153 | |
| 106 | 154 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Following' ); |