business-hours.php
4 years ago
class-wpcom-rest-api-v2-endpoint-admin-menu.php
2 years ago
class-wpcom-rest-api-v2-endpoint-ai.php
2 years ago
class-wpcom-rest-api-v2-endpoint-app-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-blog-stats.php
2 years ago
class-wpcom-rest-api-v2-endpoint-external-media.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
2 years ago
class-wpcom-rest-api-v2-endpoint-goodreads.php
2 years ago
class-wpcom-rest-api-v2-endpoint-google-docs.php
4 years ago
class-wpcom-rest-api-v2-endpoint-instagram-gallery.php
3 years ago
class-wpcom-rest-api-v2-endpoint-mailchimp.php
3 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-list.php
2 years ago
class-wpcom-rest-api-v2-endpoint-newsletter-categories-subscriptions-count.php
2 years ago
class-wpcom-rest-api-v2-endpoint-podcast-player.php
2 years ago
class-wpcom-rest-api-v2-endpoint-publicize-share-post.php
3 years ago
class-wpcom-rest-api-v2-endpoint-related-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-resolve-redirect.php
2 years ago
class-wpcom-rest-api-v2-endpoint-search.php
4 years ago
class-wpcom-rest-api-v2-endpoint-send-email-preview.php
2 years ago
class-wpcom-rest-api-v2-endpoint-template-loader.php
3 years ago
class-wpcom-rest-api-v2-endpoint-top-posts.php
2 years ago
class-wpcom-rest-api-v2-endpoint-transient.php
5 years ago
class-wpcom-rest-api-v3-endpoint-blogging-prompts.php
2 years ago
gutenberg-available-extensions.php
2 years ago
hello.php
4 years ago
memberships.php
2 years ago
publicize-connection-test-results.php
3 years ago
publicize-connections.php
2 years ago
publicize-services.php
3 years ago
service-api-keys.php
2 years ago
sites-posts-featured-media-url.php
2 years ago
subscribers.php
3 years ago
trait-wpcom-rest-api-proxy-request-trait.php
2 years ago
class-wpcom-rest-api-v2-endpoint-following.php
151 lines
| 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 | /** |
| 13 | * Class WPCOM_REST_API_V2_Endpoint_Following |
| 14 | */ |
| 15 | class WPCOM_REST_API_V2_Endpoint_Following extends WP_REST_Controller { |
| 16 | /** |
| 17 | * Namespace prefix. |
| 18 | * |
| 19 | * @var string |
| 20 | */ |
| 21 | public $namespace = 'wpcom/v2'; |
| 22 | |
| 23 | /** |
| 24 | * Endpoint base route. |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $rest_base = 'following'; |
| 29 | |
| 30 | /** |
| 31 | * Constructor. |
| 32 | */ |
| 33 | public function __construct() { |
| 34 | $this->wpcom_is_wpcom_only_endpoint = true; |
| 35 | $this->wpcom_is_site_specific_endpoint = false; |
| 36 | |
| 37 | add_action( 'rest_api_init', array( $this, 'register_routes' ) ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Register routes. |
| 42 | */ |
| 43 | public function register_routes() { |
| 44 | register_rest_route( |
| 45 | $this->namespace, |
| 46 | $this->rest_base . '/mine', |
| 47 | array( |
| 48 | array( |
| 49 | 'methods' => WP_REST_Server::READABLE, |
| 50 | 'callback' => array( $this, 'get_following' ), |
| 51 | 'permission_callback' => 'is_user_logged_in', |
| 52 | 'args' => array( |
| 53 | 'ignore_user_blogs' => array( |
| 54 | 'type' => 'boolean', |
| 55 | ), |
| 56 | ), |
| 57 | ), |
| 58 | ) |
| 59 | ); |
| 60 | |
| 61 | register_rest_route( |
| 62 | $this->namespace, |
| 63 | $this->rest_base . '/recommendations', |
| 64 | array( |
| 65 | array( |
| 66 | 'methods' => WP_REST_Server::READABLE, |
| 67 | 'callback' => array( $this, 'get_recommendations' ), |
| 68 | 'permission_callback' => 'is_user_logged_in', |
| 69 | 'args' => array( |
| 70 | 'number' => array( |
| 71 | 'type' => 'number', |
| 72 | 'default' => 5, |
| 73 | 'validate_callback' => function ( $param ) { |
| 74 | return is_numeric( $param ) && $param <= 20; |
| 75 | }, |
| 76 | ), |
| 77 | ), |
| 78 | ), |
| 79 | ) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Gets the sites the user is following |
| 85 | * |
| 86 | * @param WP_REST_Request $request Full details about the request. |
| 87 | * @return array|WP_Error list of followed sites, WP_Error otherwise |
| 88 | */ |
| 89 | public function get_following( $request ) { |
| 90 | $ignore_user_blogs = $request->get_param( 'ignore_user_blogs' ); |
| 91 | |
| 92 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 93 | require_lib( 'wpcom-get-user-followed-blogs' ); |
| 94 | return get_user_followed_blogs( get_current_user_id(), $ignore_user_blogs ); |
| 95 | } |
| 96 | |
| 97 | $body = Client::wpcom_json_api_request_as_user( |
| 98 | sprintf( '/me/following%s', $ignore_user_blogs ? '?ignore_user_blogs=true' : '' ), |
| 99 | '2', |
| 100 | array( |
| 101 | 'method' => 'GET', |
| 102 | 'headers' => array( |
| 103 | 'Content-Type' => 'application/json', |
| 104 | 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), |
| 105 | ), |
| 106 | ) |
| 107 | ); |
| 108 | |
| 109 | if ( is_wp_error( $body ) ) { |
| 110 | return $body; |
| 111 | } |
| 112 | |
| 113 | return json_decode( wp_remote_retrieve_body( $body ) ); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Gets recommended sites for user |
| 118 | * |
| 119 | * @param WP_REST_Request $request Full details about the request. |
| 120 | * @return array|WP_Error list of following recommendations, WP_Error otherwise |
| 121 | */ |
| 122 | public function get_recommendations( $request ) { |
| 123 | $number_of_recommendations = $request->get_param( 'number' ); |
| 124 | |
| 125 | if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) { |
| 126 | require_lib( 'wpcom-get-user-followed-blogs' ); |
| 127 | return get_user_following_recommendations( get_current_user_id(), $number_of_recommendations ); |
| 128 | } |
| 129 | |
| 130 | $body = Client::wpcom_json_api_request_as_user( |
| 131 | sprintf( '/me/following/recommendations?number=%d', $number_of_recommendations ), |
| 132 | '2', |
| 133 | array( |
| 134 | 'method' => 'GET', |
| 135 | 'headers' => array( |
| 136 | 'Content-Type' => 'application/json', |
| 137 | 'X-Forwarded-For' => ( new Visitor() )->get_ip( true ), |
| 138 | ), |
| 139 | ) |
| 140 | ); |
| 141 | |
| 142 | if ( is_wp_error( $body ) ) { |
| 143 | return $body; |
| 144 | } |
| 145 | |
| 146 | return json_decode( wp_remote_retrieve_body( $body ) ); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Following' ); |
| 151 |