PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
jetpack / jetpack_vendor / automattic / jetpack-search / src / class-plan.php

class-plan.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at jetpack_vendor/automattic/jetpack-search/src/class-plan.php

185 lines 5.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Search Plan class.
4 * Registers the REST routes for Search.
5 *
6 * @package automattic/jetpack-search
7 */
8
9 namespace Automattic\Jetpack\Search;
10
11 use Automattic\Jetpack\Connection\Client;
12 use Jetpack_Options;
13 use WP_Error;
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit( 0 );
17 }
18
19 /**
20 * Registers the REST routes for Search.
21 */
22 class Plan {
23 const JETPACK_SEARCH_PLAN_INFO_OPTION_KEY = 'jetpack_search_plan_info';
24 const JETPACK_SEARCH_EVER_SUPPORTED_SEARCH = 'jetpack_search_ever_supported_search';
25
26 // The pricing update starting from August 2022.
27 const JETPACK_SEARCH_NEW_PRICING_VERSION = '202208';
28 const JETPACK_SEARCH_FREE_PRODUCT_SLUG = 'jetpack_search_free';
29
30 /**
31 * Whether we have hooked the actions.
32 *
33 * @var boolean
34 */
35 protected static $update_plan_hook_initialized = false;
36
37 /**
38 * Init hooks for updating plan info
39 */
40 public function init_hooks() {
41 // Update plan info from WPCOM on Jetpack heartbeat.
42 // TODO: implement heartbeart for search.
43 if ( ! static::$update_plan_hook_initialized ) {
44 add_action( 'jetpack_heartbeat', array( $this, 'get_plan_info_from_wpcom' ) );
45 static::$update_plan_hook_initialized = true;
46 }
47 }
48
49 /**
50 * Refresh plan info stored in options
51 */
52 public function get_plan_info_from_wpcom() {
53 $blog_id = Jetpack_Options::get_option( 'id' );
54 $response = Client::wpcom_json_api_request_as_blog(
55 '/sites/' . $blog_id . '/jetpack-search/plan',
56 '2',
57 array(),
58 null,
59 'wpcom'
60 );
61
62 // store plan in options.
63 $this->update_search_plan_info( $response );
64
65 return $response;
66 }
67
68 /**
69 * Get plan info.
70 *
71 * @param bool $force_refresh - Default to false. Set true to load from WPCOM.
72 */
73 public function get_plan_info( $force_refresh = false ) {
74 if ( $force_refresh ) {
75 $this->get_plan_info_from_wpcom();
76 }
77 $plan_info = get_option( self::JETPACK_SEARCH_PLAN_INFO_OPTION_KEY );
78 if ( false === $plan_info && ! $force_refresh ) {
79 $plan_info = $this->get_plan_info( true );
80 }
81 return $plan_info;
82 }
83
84 /**
85 * Please use `supports_instant_search` instead.
86 *
87 * @deprecated
88 */
89 public function has_jetpack_search_product() {
90 return (bool) get_option( 'has_jetpack_search_product' );
91 }
92
93 /**
94 * Returns true if plan supports Instant Search.
95 */
96 public function supports_instant_search() {
97 $plan_info = $this->get_plan_info();
98 return ( isset( $plan_info['supports_instant_search'] ) && $plan_info['supports_instant_search'] );
99 }
100
101 /**
102 * Returns true if the plan support either Instant Search or Classic Search.
103 */
104 public function supports_search() {
105 $plan_info = $this->get_plan_info();
106 return ( isset( $plan_info['supports_search'] ) && $plan_info['supports_search'] );
107 }
108
109 /**
110 * Returns true if the plan usage is exceeded and search should no longer work.
111 */
112 public function must_upgrade() {
113 $plan_info = $this->get_plan_info();
114 return isset( $plan_info['plan_usage']['must_upgrade'] ) && $plan_info['plan_usage']['must_upgrade'];
115 }
116
117 /**
118 * Returns true if the plan only supports Classic Search.
119 */
120 public function supports_only_classic_search() {
121 $plan_info = $this->get_plan_info();
122 return isset( $plan_info['supports_only_classic_search'] ) && $plan_info['supports_only_classic_search'];
123 }
124
125 /**
126 * Whether the plan(s) ever supported search.
127 */
128 public function ever_supported_search() {
129 return (bool) get_option( self::JETPACK_SEARCH_EVER_SUPPORTED_SEARCH ) || $this->supports_search();
130 }
131
132 /**
133 * Returns true if the site is on free plan.
134 */
135 public function is_free_plan() {
136 $plan_info = $this->get_plan_info();
137 return Helper::is_forced_free_plan() || ( isset( $plan_info['effective_subscription']['product_slug'] ) && $plan_info['effective_subscription']['product_slug'] === self::JETPACK_SEARCH_FREE_PRODUCT_SLUG );
138 }
139
140 /**
141 * Update `has_jetpack_search_product` regarding the plan information
142 *
143 * @param array|WP_Error $response - Resopnse from WPCOM.
144 * @return bool - true on success, false on failure.
145 */
146 public function update_search_plan_info( $response ) {
147 if ( is_wp_error( $response ) ) {
148 return false;
149 }
150 $body = json_decode( wp_remote_retrieve_body( $response ), true );
151 $status_code = wp_remote_retrieve_response_code( $response );
152
153 if ( 200 !== $status_code ) {
154 return false;
155 }
156
157 return $this->set_plan_options( $body );
158 }
159
160 /**
161 * Set plan info to options table
162 *
163 * @param array $plan_info - the decoded plan info array.
164 */
165 public function set_plan_options( $plan_info ) {
166 if ( isset( $plan_info['swap_classic_to_inline_search'] ) && is_bool( $plan_info['swap_classic_to_inline_search'] ) ) {
167 update_option( Module_Control::SEARCH_MODULE_SWAP_CLASSIC_TO_INLINE_OPTION_KEY, (bool) $plan_info['swap_classic_to_inline_search'] );
168 }
169 if ( ! isset( $plan_info['supports_instant_search'] ) ) {
170 return false;
171 }
172 // set option whether has Jetpack Search plan for capability reason.
173 if ( get_option( 'has_jetpack_search_product' ) !== (bool) $plan_info['supports_instant_search'] ) {
174 update_option( 'has_jetpack_search_product', (bool) $plan_info['supports_instant_search'] );
175 }
176 // We use this option to determine the visibility of search submenu.
177 // If the site ever had search subscription, then we record it and show the menu after.
178 if ( $plan_info['supports_instant_search'] ) {
179 update_option( self::JETPACK_SEARCH_EVER_SUPPORTED_SEARCH, true, false );
180 }
181 update_option( self::JETPACK_SEARCH_PLAN_INFO_OPTION_KEY, $plan_info );
182 return true;
183 }
184 }
185