blob: 411342605fd72c6f58cdad2195df420e0d5d4497 (
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
136
137
138
139
140
|
<?php
/**
* The Search Plan class.
* Registers the REST routes for Search.
*
* @package automattic/jetpack-search
*/
namespace Automattic\Jetpack\Search;
use Automattic\Jetpack\Connection\Client;
use Jetpack_Options;
use WP_Error;
/**
* Registers the REST routes for Search.
*/
class Plan {
const JETPACK_SEARCH_PLAN_INFO_OPTION_KEY = 'jetpack_search_plan_info';
const JETPACK_SEARCH_EVER_SUPPORTED_SEARCH = 'jetpack_search_ever_supported_search';
/**
* Whether we have hooked the actions.
*
* @var boolean
*/
protected static $update_plan_hook_initialized = false;
/**
* Init hooks for updating plan info
*/
public function init_hooks() {
// Update plan info from WPCOM on Jetpack heartbeat.
// TODO: implement heartbeart for search.
if ( ! static::$update_plan_hook_initialized ) {
add_action( 'jetpack_heartbeat', array( $this, 'get_plan_info_from_wpcom' ) );
static::$update_plan_hook_initialized = true;
}
}
/**
* Refresh plan info stored in options
*/
public function get_plan_info_from_wpcom() {
$blog_id = Jetpack_Options::get_option( 'id' );
$response = Client::wpcom_json_api_request_as_user(
'/sites/' . $blog_id . '/jetpack-search/plan',
'2'
);
// store plan in options.
$this->update_search_plan_info( $response );
return $response;
}
/**
* Get plan info.
*
* @param {bool} $force_refresh - Default to false. Set true to load from WPCOM.
*/
public function get_plan_info( $force_refresh = false ) {
if ( $force_refresh ) {
$this->get_plan_info_from_wpcom();
}
$plan_info = get_option( self::JETPACK_SEARCH_PLAN_INFO_OPTION_KEY );
if ( false === $plan_info && ! $force_refresh ) {
$plan_info = $this->get_plan_info( true );
}
return $plan_info;
}
/**
* Please use `supports_instant_search` instead.
*
* @deprecated
*/
public function has_jetpack_search_product() {
return (bool) get_option( 'has_jetpack_search_product' );
}
/**
* Returns true if plan supports Instant Search.
*/
public function supports_instant_search() {
$plan_info = $this->get_plan_info();
return isset( $plan_info['supports_instant_search'] ) && $plan_info['supports_instant_search'];
}
/**
* Returns true if the plan support either Instant Search or Classic Search.
*/
public function supports_search() {
$plan_info = $this->get_plan_info();
return isset( $plan_info['supports_search'] ) && $plan_info['supports_search'];
}
/**
* Returns true if the plan only supports Classic Search.
*/
public function supports_only_classic_search() {
$plan_info = $this->get_plan_info();
return isset( $plan_info['supports_only_classic_search'] ) && $plan_info['supports_only_classic_search'];
}
/**
* Whether the plan(s) ever supported search.
*/
public function ever_supported_search() {
return (bool) get_option( self::JETPACK_SEARCH_EVER_SUPPORTED_SEARCH ) || $this->supports_search();
}
/**
* Update `has_jetpack_search_product` regarding the plan information
*
* @param array|WP_Error $response - Resopnse from WPCOM.
*/
public function update_search_plan_info( $response ) {
if ( is_wp_error( $response ) ) {
return null;
}
$body = json_decode( wp_remote_retrieve_body( $response ), true );
$status_code = wp_remote_retrieve_response_code( $response );
if ( 200 !== $status_code || ! isset( $body['supports_instant_search'] ) ) {
return null;
}
// set option whether has Jetpack Search plan for capability reason.
if ( get_option( 'has_jetpack_search_product' ) !== (bool) $body['supports_instant_search'] ) {
update_option( 'has_jetpack_search_product', (bool) $body['supports_instant_search'] );
}
// We use this option to determine the visibility of search submenu.
// If the site ever had search subscription, then we record it and show the menu after.
if ( $body['supports_instant_search'] ) {
update_option( self::JETPACK_SEARCH_EVER_SUPPORTED_SEARCH, true, false );
}
update_option( self::JETPACK_SEARCH_PLAN_INFO_OPTION_KEY, $body );
}
}
|