Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
44.87% |
35 / 78 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_REST_API_V2_Endpoint_Following | |
46.67% |
35 / 75 |
|
0.00% |
0 / 4 |
33.85 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| register_routes | |
97.22% |
35 / 36 |
|
0.00% |
0 / 1 |
2 | |||
| get_following | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
30 | |||
| get_recommendations | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * REST API endpoint for the Jetpack Blogroll block. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 12.2 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\Jetpack\Connection\Client; |
| 10 | use Automattic\Jetpack\Status\Visitor; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit( 0 ); |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class WPCOM_REST_API_V2_Endpoint_Following |
| 18 | */ |
| 19 | class WPCOM_REST_API_V2_Endpoint_Following extends WP_REST_Controller { |
| 20 | /** |
| 21 | * Namespace prefix. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public $namespace = 'wpcom/v2'; |
| 26 | |
| 27 | /** |
| 28 | * Endpoint base route. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | public $rest_base = 'following'; |
| 33 | |
| 34 | /** |
| 35 | * Constructor. |
| 36 | */ |
| 37 | public function __construct() { |
| 38 | $this->wpcom_is_wpcom_only_endpoint = true; |
| 39 | $this->wpcom_is_site_specific_endpoint = false; |
| 40 | |
| 41 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Register routes. |
| 46 | */ |
| 47 | public function register_routes() { |
| 48 | register_rest_route( |
| 49 | $this->namespace, |
| 50 | $this->rest_base . '/mine', |
| 51 | array( |
| 52 | array( |
| 53 | 'methods' => WP_REST_Server::READABLE, |
| 54 | 'callback' => array( $this, 'get_following' ), |
| 55 | 'permission_callback' => 'is_user_logged_in', |
| 56 | 'args' => array( |
| 57 | 'ignore_user_blogs' => array( |
| 58 | 'type' => 'boolean', |
| 59 | ), |
| 60 | ), |
| 61 | ), |
| 62 | ) |
| 63 | ); |
| 64 | |
| 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 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Gets the sites the user is following |
| 89 | * |
| 90 | * @param WP_REST_Request $request Full details about the request. |
| 91 | * @return array|WP_Error list of followed sites, WP_Error otherwise |
| 92 | */ |
| 93 | public function get_following( $request ) { |
| 94 | $ignore_user_blogs = $request->get_param( 'ignore_user_blogs' ); |
| 95 | |
| 96 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 97 | require_lib( 'wpcom-get-user-followed-blogs' ); |
| 98 | return get_user_followed_blogs( get_current_user_id(), $ignore_user_blogs ); |
| 99 | } |
| 100 | |
| 101 | $body = Client::wpcom_json_api_request_as_user( |
| 102 | sprintf( '/me/following%s', $ignore_user_blogs ? '?ignore_user_blogs=true' : '' ), |
| 103 | '2', |
| 104 | array( |
| 105 | 'method' => 'GET', |
| 106 | 'headers' => array( |
| 107 | 'Content-Type' => 'application/json', |
| 108 | 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), |
| 109 | ), |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | if ( is_wp_error( $body ) ) { |
| 114 | return $body; |
| 115 | } |
| 116 | |
| 117 | return json_decode( wp_remote_retrieve_body( $body ) ); |
| 118 | } |
| 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 | } |
| 152 | } |
| 153 | |
| 154 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Following' ); |