Completed
Push — improve/use-plan-icon-in-my-pl... ( 986e79...6cc615 )
by
unknown
34:39 queued 25:39
created

WPCOM_REST_API_V2_Endpoint_Search::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Proxy endpoint for Jetpack Search
4
 *
5
 * @package Jetpack
6
 */
7
8
use Automattic\Jetpack\Connection\Client;
9
10
/**
11
 * Jetpack Search: Makes authenticated requests to the site search API using blog tokens.
12
 * This endpoint will only be used when trying to search private Jetpack and WordPress.com sites.
13
 *
14
 * @since 9.0.0
15
 */
16
class WPCOM_REST_API_V2_Endpoint_Search extends WP_REST_Controller {
17
	/**
18
	 * Constructor.
19
	 */
20
	public function __construct() {
21
		$this->namespace = 'wpcom/v2';
22
		$this->rest_base = 'search';
23
24
		add_action( 'rest_api_init', array( $this, 'register_routes' ) );
25
	}
26
27
	/**
28
	 * Called automatically on `rest_api_init()`.
29
	 */
30 View Code Duplication
	public function register_routes() {
31
		register_rest_route(
32
			$this->namespace,
33
			$this->rest_base,
34
			array(
35
				array(
36
					'methods'             => WP_REST_Server::READABLE,
37
					'callback'            => array( $this, 'get_search_results' ),
38
					'permission_callback' => 'is_user_logged_in',
39
				),
40
			)
41
		);
42
	}
43
44
	/**
45
	 * Returns search results for the current blog.
46
	 *
47
	 * @param WP_REST_Request $request The REST API request data.
48
	 * @return mixed The REST API response from public-api.
49
	 */
50
	public function get_search_results( $request ) {
51
		$is_wpcom = ( defined( 'IS_WPCOM' ) && IS_WPCOM );
52
		$site_id  = $is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
53
		if ( ! $site_id ) {
54
			return new WP_Error(
55
				'unavailable_site_id',
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with 'unavailable_site_id'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
56
				__( 'Sorry, something is wrong with your Jetpack connection.', 'jetpack' ),
57
				403
58
			);
59
		}
60
61
		$path    = add_query_arg(
62
			$request->get_query_params(),
63
			sprintf( '/sites/%d/search', absint( $site_id ) )
64
		);
65
		$request = Client::wpcom_json_api_request_as_blog( $path, '1.3' );
66
		$body    = json_decode( wp_remote_retrieve_body( $request ) );
67
		if ( 200 === wp_remote_retrieve_response_code( $request ) ) {
68
			return $body;
69
		}
70
71
		return new WP_Error(
72
			$body->error,
0 ignored issues
show
Unused Code introduced by
The call to WP_Error::__construct() has too many arguments starting with $body->error.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
73
			$body->message,
74
			array( 'status' => wp_remote_retrieve_response_code( $request ) )
75
		);
76
	}
77
}
78
79
wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Search' );
80