summaryrefslogtreecommitdiff
blob: c8fba69c592540cfa667fb7f4046012bb648f0fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
 * This is the endpoint class for `/site` endpoints.
 *
 */
class Jetpack_Core_API_Site_Endpoint {

	/**
	 * Returns the result of `/sites/%s/features` endpoint call.
	 * @return object $features has 'active' and 'available' properties each of which contain feature slugs.
	 *                  'active' is a simple array of slugs that are active on the current plan.
	 *                  'available' is an object with keys that represent feature slugs and values are arrays
	 *                     of plan slugs that enable these features
	 */
	public static function get_features() {

		// Make the API request
		$request = sprintf( '/sites/%d/features', Jetpack_Options::get_option( 'id' ) );
		$response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1' );

		// Bail if there was an error or malformed response
		if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
			return new WP_Error(
				'failed_to_fetch_data',
				esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
				array( 'status' => 500 )
			);
		}

		// Decode the results
		$results = json_decode( $response['body'], true );

		// Bail if there were no results or plan details returned
		if ( ! is_array( $results ) ) {
			return new WP_Error(
				'failed_to_fetch_data',
				esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
				array( 'status' => 500 )
			);
		}

		return rest_ensure_response( array(
				'code' => 'success',
				'message' => esc_html__( 'Site features correctly received.', 'jetpack' ),
				'data' => wp_remote_retrieve_body( $response ),
			)
		);
	}

	/**
	 * Returns the result of `/sites/%s/posts/%d/related` endpoint call.
	 * Results are not cached and are retrieved in real time.
	 *
	 * @since 6.7.0
	 *
	 * @param int ID of the post to get related posts of
	 *
	 * @return array
	 */
	public static function get_related_posts( $api_request ) {
		$params = $api_request->get_params();
		$post_id = ! empty( $params['post_id'] ) ? absint( $params['post_id'] ) : 0;

		if ( ! $post_id ) {
			return new WP_Error(
				'incorrect_post_id',
				esc_html__( 'You need to specify a correct ID of a post to return related posts for.', 'jetpack' ),
				array( 'status' => 400 )
			);
		}

		// Make the API request
		$request = sprintf( '/sites/%d/posts/%d/related', Jetpack_Options::get_option( 'id' ), $post_id );
		$request_args = array(
			'headers' => array(
				'Content-Type' => 'application/json',
			),
			'timeout'    => 10,
			'method' => 'POST',
		);
		$response = Jetpack_Client::wpcom_json_api_request_as_blog( $request, '1.1', $request_args );

		// Bail if there was an error or malformed response
		if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
			return new WP_Error(
				'failed_to_fetch_data',
				esc_html__( 'Unable to fetch the requested data.', 'jetpack' ),
				array( 'status' => 400 )
			);
		}

		// Decode the results
		$results = json_decode( wp_remote_retrieve_body( $response ), true );

		$related_posts = array();
		if ( isset( $results['hits'] ) && is_array( $results['hits'] ) ) {
			$related_posts_ids = array_map( array( 'Jetpack_Core_API_Site_Endpoint', 'get_related_post_id' ), $results['hits'] );

			$related_posts_instance = Jetpack_RelatedPosts::init();
			foreach ( $related_posts_ids as $related_post_id ) {
				$related_posts[] = $related_posts_instance->get_related_post_data_for_post( $related_post_id, 0, 0 );
			}
		}

		return rest_ensure_response( array(
				'code' => 'success',
				'message' => esc_html__( 'Related posts retrieved successfully.', 'jetpack' ),
				'posts' => $related_posts,
			)
		);
	}

	/**
	 * Check that the current user has permissions to request information about this site.
	 *
	 * @since 5.1.0
	 *
	 * @return bool
	 */
	public static function can_request() {
		return current_user_can( 'jetpack_manage_modules' );
	}

	/**
	 * Returns the post ID out of a related post entry from the
	 * `/sites/%s/posts/%d/related` WP.com endpoint.
	 *
	 * @since 6.7.0
	 *
	 * @return int
	 */
	public static function get_related_post_id( $item ) {
		return $item['fields']['post_id'];
	}
}