SG Optimizer - Version 5.5.8

Version Description

Download this release

Release Info

Developer sstoqnov
Plugin Icon 128x128 SG Optimizer
Version 5.5.8
Comparing to
See all releases

Code changes from version 5.5.7 to 5.5.8

core/Cli/Cli_Purge.php CHANGED
@@ -46,7 +46,8 @@ class Cli_Purge {
46
  */
47
  public function purge_everything() {
48
  $response = $this->supercacher->purge_everything();
49
-
 
50
  if ( true == $response ) {
51
  return \WP_CLI::success( 'Cache Successfully Purged' );
52
  }
46
  */
47
  public function purge_everything() {
48
  $response = $this->supercacher->purge_everything();
49
+ $this->supercacher->delete_assets();
50
+
51
  if ( true == $response ) {
52
  return \WP_CLI::success( 'Cache Successfully Purged' );
53
  }
core/Combinator/Fonts_Combinator.php CHANGED
@@ -229,10 +229,7 @@ class Fonts_Combinator extends Abstract_Combinator {
229
  }
230
 
231
  // Return combined tag if AMP plugin is active.
232
- if (
233
- ( function_exists( 'is_amp_endpoint' ) && is_amp_endpoint() ) ||
234
- ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() )
235
- ) {
236
  return $combined_tag;
237
  }
238
 
229
  }
230
 
231
  // Return combined tag if AMP plugin is active.
232
+ if ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ) {
 
 
 
233
  return $combined_tag;
234
  }
235
 
core/Combinator/Js_Combinator.php CHANGED
@@ -16,6 +16,23 @@ class Js_Combinator extends Abstract_Combinator {
16
  * @var array Array containing all excluded inline content.
17
  */
18
  private $excluded_inline_content = array(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  'woocs_array_of_get',
20
  'woocs_current_currency',
21
  'DokanWholesale',
@@ -440,6 +457,9 @@ class Js_Combinator extends Abstract_Combinator {
440
  'wc-authorize-net-cim',
441
  'sv-wc-payment-gateway-payment-form',
442
  'elementor-menus-frontend',
 
 
 
443
  );
444
 
445
  /**
16
  * @var array Array containing all excluded inline content.
17
  */
18
  private $excluded_inline_content = array(
19
+ 'UNCODE.initRow',
20
+ '$(\'head\').append(\'<style',
21
+ '$("head").append("<style',
22
+ '#dfd-isotope-container-',
23
+ '#dfd-horizontal-scroll-',
24
+ 'var form_container =',
25
+ 'var container = document.querySelectorAll( \'[data-view-breakpoint-pointer="',
26
+ 'var et_animation_data =',
27
+ '_ASP.initialize',
28
+ 'theChampSiteUrl',
29
+ 'var _beeketing =',
30
+ 'glami(\'track',
31
+ 'pysWooSelectContentData',
32
+ 'init_scroll_shortcode',
33
+ '_leartsInlineStyle',
34
+ 'div[rel=tipsy]\')',
35
+ 'g5core-social-networks',
36
  'woocs_array_of_get',
37
  'woocs_current_currency',
38
  'DokanWholesale',
457
  'wc-authorize-net-cim',
458
  'sv-wc-payment-gateway-payment-form',
459
  'elementor-menus-frontend',
460
+ 'uncode-app',
461
+ 'uncode-plugins',
462
+ 'uncode-init',
463
  );
464
 
465
  /**
core/Front_End_Optimization/Front_End_Optimization.php CHANGED
@@ -36,6 +36,9 @@ class Front_End_Optimization {
36
  'wc-authorize-net-cim',
37
  'sv-wc-payment-gateway-payment-form',
38
  'paypal-checkout-sdk',
 
 
 
39
  );
40
 
41
  /**
36
  'wc-authorize-net-cim',
37
  'sv-wc-payment-gateway-payment-form',
38
  'paypal-checkout-sdk',
39
+ 'uncode-app',
40
+ 'uncode-plugins',
41
+ 'uncode-init',
42
  );
43
 
44
  /**
core/Lazy_Load/Lazy_Load_Iframes.php CHANGED
@@ -13,7 +13,9 @@ class Lazy_Load_Iframes {
13
  * @since 5.4.3
14
  */
15
  public function __construct() {
16
- add_filter( 'the_content', array( $this, 'filter_html' ) );
 
 
17
  }
18
 
19
  /**
13
  * @since 5.4.3
14
  */
15
  public function __construct() {
16
+ $priority = get_option( 'siteground_optimizer_lazyload_shortcodes' ) ? 9999 : 10;
17
+
18
+ add_filter( 'the_content', array( $this, 'filter_html' ), $priority );
19
  }
20
 
21
  /**
core/Lazy_Load/Lazy_Load_Videos.php CHANGED
@@ -13,7 +13,9 @@ class Lazy_Load_Videos {
13
  * @since 5.4.3
14
  */
15
  public function __construct() {
16
- add_filter( 'the_content', array( $this, 'filter_html' ) );
 
 
17
  }
18
 
19
  /**
13
  * @since 5.4.3
14
  */
15
  public function __construct() {
16
+ $priority = get_option( 'siteground_optimizer_lazyload_shortcodes' ) ? 9999 : 10;
17
+
18
+ add_filter( 'the_content', array( $this, 'filter_html' ), $priority );
19
  }
20
 
21
  /**
core/Parser/Parser.php CHANGED
@@ -35,6 +35,11 @@ class Parser {
35
  * @return string $html The modified html.
36
  */
37
  public function run( $html ) {
 
 
 
 
 
38
  if ( Options::is_enabled( 'siteground_optimizer_optimize_html' ) ) {
39
  $html = Minifier::get_instance()->run( $html );
40
  }
@@ -54,6 +59,24 @@ class Parser {
54
  return $html;
55
  }
56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  /**
58
  * Start buffer.
59
  *
35
  * @return string $html The modified html.
36
  */
37
  public function run( $html ) {
38
+ // Do not run optimizations for amp.
39
+ if ( $this->is_amp_enabled( $html ) ) {
40
+ return $html;
41
+ }
42
+
43
  if ( Options::is_enabled( 'siteground_optimizer_optimize_html' ) ) {
44
  $html = Minifier::get_instance()->run( $html );
45
  }
59
  return $html;
60
  }
61
 
62
+ /**
63
+ * AMP Atribute check. Runs a check if AMP option is enabled
64
+ *
65
+ * @since 5.5.8
66
+ *
67
+ * @param string $html The page html.
68
+ *
69
+ * @return bool $run_amp_check Wheter the page is loaded via AMP.
70
+ */
71
+ public function is_amp_enabled( $html ) {
72
+ // Get the first 200 chars of the file to make the preg_match check faster.
73
+ $is_amp = substr( $html, 0, 200 );
74
+ // Cheks if the document is containing the amp tag.
75
+ $run_amp_check = preg_match( '/<html[^>]+(amp|⚡)[^>]*>/', $is_amp );
76
+
77
+ return $run_amp_check;
78
+ }
79
+
80
  /**
81
  * Start buffer.
82
  *
core/Rest/Rest.php CHANGED
@@ -12,8 +12,14 @@ class Rest {
12
  * The constructor.
13
  */
14
  public function __construct() {
15
- $this->helper = new Rest_Helper();
16
- $this->webp_helper = new Rest_Helper_Webp();
 
 
 
 
 
 
17
 
18
  add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
19
  }
@@ -55,7 +61,7 @@ class Rest {
55
  register_rest_route(
56
  self::REST_NAMESPACE, '/switch-php/', array(
57
  'methods' => 'POST',
58
- 'callback' => array( $this->helper, 'switch_php' ),
59
  'permission_callback' => array( $this, 'check_permissions' ),
60
  )
61
  );
@@ -63,7 +69,7 @@ class Rest {
63
  register_rest_route(
64
  self::REST_NAMESPACE, '/enable-ssl/', array(
65
  'methods' => 'POST',
66
- 'callback' => array( $this->helper, 'enable_ssl' ),
67
  'permission_callback' => array( $this, 'check_permissions' ),
68
  )
69
  );
@@ -71,7 +77,7 @@ class Rest {
71
  register_rest_route(
72
  self::REST_NAMESPACE, '/disable-ssl/', array(
73
  'methods' => 'POST',
74
- 'callback' => array( $this->helper, 'disable_ssl' ),
75
  'permission_callback' => array( $this, 'check_permissions' ),
76
  )
77
  );
@@ -86,7 +92,7 @@ class Rest {
86
  register_rest_route(
87
  self::REST_NAMESPACE, '/check-compatibility/', array(
88
  'methods' => 'POST',
89
- 'callback' => array( $this->helper, 'handle_compatibility_check' ),
90
  'permission_callback' => array( $this, 'check_permissions' ),
91
  )
92
  );
@@ -94,7 +100,7 @@ class Rest {
94
  register_rest_route(
95
  self::REST_NAMESPACE, '/check-compatibility-status/', array(
96
  'methods' => 'GET',
97
- 'callback' => array( $this->helper, 'handle_compatibility_status_check' ),
98
  'permission_callback' => array( $this, 'check_permissions' ),
99
  )
100
  );
@@ -109,7 +115,7 @@ class Rest {
109
  register_rest_route(
110
  self::REST_NAMESPACE, '/enable-option/', array(
111
  'methods' => 'POST',
112
- 'callback' => array( $this->helper, 'enable_option_from_rest' ),
113
  'permission_callback' => array( $this, 'check_permissions' ),
114
  )
115
  );
@@ -117,7 +123,7 @@ class Rest {
117
  register_rest_route(
118
  self::REST_NAMESPACE, '/disable-option/', array(
119
  'methods' => 'POST',
120
- 'callback' => array( $this->helper, 'disable_option_from_rest' ),
121
  'permission_callback' => array( $this, 'check_permissions' ),
122
  )
123
  );
@@ -125,7 +131,7 @@ class Rest {
125
  register_rest_route(
126
  self::REST_NAMESPACE, '/fetch-options/', array(
127
  'methods' => 'GET',
128
- 'callback' => array( $this->helper, 'fetch_options' ),
129
  'permission_callback' => array( $this, 'check_permissions' ),
130
  )
131
  );
@@ -133,7 +139,7 @@ class Rest {
133
  register_rest_route(
134
  self::REST_NAMESPACE, '/change-option/', array(
135
  'methods' => 'POST',
136
- 'callback' => array( $this->helper, 'change_option_from_rest' ),
137
  'permission_callback' => array( $this, 'check_permissions' ),
138
  )
139
  );
@@ -148,7 +154,7 @@ class Rest {
148
  register_rest_route(
149
  self::REST_NAMESPACE, '/update-excluded-urls/', array(
150
  'methods' => 'POST',
151
- 'callback' => array( $this->helper, 'update_excluded_urls' ),
152
  'permission_callback' => array( $this, 'check_permissions' ),
153
  )
154
  );
@@ -156,7 +162,7 @@ class Rest {
156
  register_rest_route(
157
  self::REST_NAMESPACE, '/test-url-cache/', array(
158
  'methods' => 'POST',
159
- 'callback' => array( $this->helper, 'test_cache' ),
160
  'permission_callback' => array( $this, 'check_permissions' ),
161
  )
162
  );
@@ -164,7 +170,7 @@ class Rest {
164
  register_rest_route(
165
  self::REST_NAMESPACE, '/purge-cache/', array(
166
  'methods' => 'GET',
167
- 'callback' => array( $this->helper, 'purge_cache_from_rest' ),
168
  'permission_callback' => array( $this, 'check_permissions' ),
169
  )
170
  );
@@ -172,7 +178,7 @@ class Rest {
172
  register_rest_route(
173
  self::REST_NAMESPACE, '/enable-memcache/', array(
174
  'methods' => 'GET',
175
- 'callback' => array( $this->helper, 'enable_memcache' ),
176
  'permission_callback' => array( $this, 'check_permissions' ),
177
  )
178
  );
@@ -180,7 +186,7 @@ class Rest {
180
  register_rest_route(
181
  self::REST_NAMESPACE, '/disable-memcache/', array(
182
  'methods' => 'GET',
183
- 'callback' => array( $this->helper, 'disable_memcache' ),
184
  'permission_callback' => array( $this, 'check_permissions' ),
185
  )
186
  );
@@ -196,7 +202,7 @@ class Rest {
196
  register_rest_route(
197
  self::REST_NAMESPACE, '/optimize-images/', array(
198
  'methods' => 'GET',
199
- 'callback' => array( $this->helper, 'optimize_images' ),
200
  'permission_callback' => array( $this, 'check_permissions' ),
201
  )
202
  );
@@ -204,7 +210,7 @@ class Rest {
204
  register_rest_route(
205
  self::REST_NAMESPACE, '/stop-images-optimization/', array(
206
  'methods' => 'GET',
207
- 'callback' => array( $this->helper, 'stop_images_optimization' ),
208
  'permission_callback' => array( $this, 'check_permissions' ),
209
  )
210
  );
@@ -212,7 +218,7 @@ class Rest {
212
  register_rest_route(
213
  self::REST_NAMESPACE, '/check-image-optimizing-status/', array(
214
  'methods' => 'GET',
215
- 'callback' => array( $this->helper, 'check_image_optimizing_status' ),
216
  'permission_callback' => array( $this, 'check_permissions' ),
217
  )
218
  );
@@ -220,7 +226,7 @@ class Rest {
220
  register_rest_route(
221
  self::REST_NAMESPACE, '/reset-images-optimization/', array(
222
  'methods' => 'GET',
223
- 'callback' => array( $this->helper, 'reset_images_optimization' ),
224
  'permission_callback' => array( $this, 'check_permissions' ),
225
  )
226
  );
@@ -274,7 +280,7 @@ class Rest {
274
  register_rest_route(
275
  self::REST_NAMESPACE, '/enable-multisite-optimization/', array(
276
  'methods' => 'POST',
277
- 'callback' => array( $this->helper, 'enable_multisite_optimization' ),
278
  'permission_callback' => array( $this, 'check_permissions' ),
279
  )
280
  );
@@ -282,7 +288,7 @@ class Rest {
282
  register_rest_route(
283
  self::REST_NAMESPACE, '/disable-multisite-optimization/', array(
284
  'methods' => 'POST',
285
- 'callback' => array( $this->helper, 'disable_multisite_optimization' ),
286
  'permission_callback' => array( $this, 'check_permissions' ),
287
  )
288
  );
@@ -297,7 +303,7 @@ class Rest {
297
  register_rest_route(
298
  self::REST_NAMESPACE, '/hide-rating/', array(
299
  'methods' => 'GET',
300
- 'callback' => array( $this->helper, 'handle_hide_rating' ),
301
  'permission_callback' => array( $this, 'check_permissions' ),
302
  )
303
  );
@@ -305,7 +311,7 @@ class Rest {
305
  register_rest_route(
306
  self::REST_NAMESPACE, '/get-assets/', array(
307
  'methods' => 'GET',
308
- 'callback' => array( $this->helper, 'get_assets' ),
309
  'permission_callback' => array( $this, 'check_permissions' ),
310
  )
311
  );
@@ -313,7 +319,7 @@ class Rest {
313
  register_rest_route(
314
  self::REST_NAMESPACE, '/update-exclude-list/', array(
315
  'methods' => 'POST',
316
- 'callback' => array( $this->helper, 'update_exclude_list' ),
317
  'permission_callback' => array( $this, 'check_permissions' ),
318
  )
319
  );
@@ -321,9 +327,9 @@ class Rest {
321
  register_rest_route(
322
  self::REST_NAMESPACE, '/run-analysis/', array(
323
  'methods' => 'POST',
324
- 'callback' => array( $this->helper, 'run_analysis' ),
325
  'permission_callback' => array( $this, 'check_permissions' ),
326
  )
327
  );
328
  }
329
- }
12
  * The constructor.
13
  */
14
  public function __construct() {
15
+ $this->webp_helper = new Rest_Helper_Webp();
16
+ $this->options_helper = new Rest_Helper_Options();
17
+ $this->cache_helper = new Rest_Helper_Cache();
18
+ $this->multisite_helper = new Rest_Helper_Multisite();
19
+ $this->misc_helper = new Rest_Helper_Misc();
20
+ $this->image_helper = new Rest_Helper_Images();
21
+ $this->php_ssl_helper = new Rest_Helper_Environment();
22
+ $this->compatibility_helper = new Rest_Helper_Compatibility();
23
 
24
  add_action( 'rest_api_init', array( $this, 'register_rest_routes' ) );
25
  }
61
  register_rest_route(
62
  self::REST_NAMESPACE, '/switch-php/', array(
63
  'methods' => 'POST',
64
+ 'callback' => array( $this->php_ssl_helper, 'switch_php' ),
65
  'permission_callback' => array( $this, 'check_permissions' ),
66
  )
67
  );
69
  register_rest_route(
70
  self::REST_NAMESPACE, '/enable-ssl/', array(
71
  'methods' => 'POST',
72
+ 'callback' => array( $this->php_ssl_helper, 'enable_ssl' ),
73
  'permission_callback' => array( $this, 'check_permissions' ),
74
  )
75
  );
77
  register_rest_route(
78
  self::REST_NAMESPACE, '/disable-ssl/', array(
79
  'methods' => 'POST',
80
+ 'callback' => array( $this->php_ssl_helper, 'disable_ssl' ),
81
  'permission_callback' => array( $this, 'check_permissions' ),
82
  )
83
  );
92
  register_rest_route(
93
  self::REST_NAMESPACE, '/check-compatibility/', array(
94
  'methods' => 'POST',
95
+ 'callback' => array( $this->compatibility_helper, 'handle_compatibility_check' ),
96
  'permission_callback' => array( $this, 'check_permissions' ),
97
  )
98
  );
100
  register_rest_route(
101
  self::REST_NAMESPACE, '/check-compatibility-status/', array(
102
  'methods' => 'GET',
103
+ 'callback' => array( $this->compatibility_helper, 'handle_compatibility_status_check' ),
104
  'permission_callback' => array( $this, 'check_permissions' ),
105
  )
106
  );
115
  register_rest_route(
116
  self::REST_NAMESPACE, '/enable-option/', array(
117
  'methods' => 'POST',
118
+ 'callback' => array( $this->options_helper, 'enable_option_from_rest' ),
119
  'permission_callback' => array( $this, 'check_permissions' ),
120
  )
121
  );
123
  register_rest_route(
124
  self::REST_NAMESPACE, '/disable-option/', array(
125
  'methods' => 'POST',
126
+ 'callback' => array( $this->options_helper, 'disable_option_from_rest' ),
127
  'permission_callback' => array( $this, 'check_permissions' ),
128
  )
129
  );
131
  register_rest_route(
132
  self::REST_NAMESPACE, '/fetch-options/', array(
133
  'methods' => 'GET',
134
+ 'callback' => array( $this->options_helper, 'fetch_options' ),
135
  'permission_callback' => array( $this, 'check_permissions' ),
136
  )
137
  );
139
  register_rest_route(
140
  self::REST_NAMESPACE, '/change-option/', array(
141
  'methods' => 'POST',
142
+ 'callback' => array( $this->options_helper, 'change_option_from_rest' ),
143
  'permission_callback' => array( $this, 'check_permissions' ),
144
  )
145
  );
154
  register_rest_route(
155
  self::REST_NAMESPACE, '/update-excluded-urls/', array(
156
  'methods' => 'POST',
157
+ 'callback' => array( $this->cache_helper, 'update_excluded_urls' ),
158
  'permission_callback' => array( $this, 'check_permissions' ),
159
  )
160
  );
162
  register_rest_route(
163
  self::REST_NAMESPACE, '/test-url-cache/', array(
164
  'methods' => 'POST',
165
+ 'callback' => array( $this->cache_helper, 'test_cache' ),
166
  'permission_callback' => array( $this, 'check_permissions' ),
167
  )
168
  );
170
  register_rest_route(
171
  self::REST_NAMESPACE, '/purge-cache/', array(
172
  'methods' => 'GET',
173
+ 'callback' => array( $this->cache_helper, 'purge_cache_from_rest' ),
174
  'permission_callback' => array( $this, 'check_permissions' ),
175
  )
176
  );
178
  register_rest_route(
179
  self::REST_NAMESPACE, '/enable-memcache/', array(
180
  'methods' => 'GET',
181
+ 'callback' => array( $this->cache_helper, 'enable_memcache' ),
182
  'permission_callback' => array( $this, 'check_permissions' ),
183
  )
184
  );
186
  register_rest_route(
187
  self::REST_NAMESPACE, '/disable-memcache/', array(
188
  'methods' => 'GET',
189
+ 'callback' => array( $this->cache_helper, 'disable_memcache' ),
190
  'permission_callback' => array( $this, 'check_permissions' ),
191
  )
192
  );
202
  register_rest_route(
203
  self::REST_NAMESPACE, '/optimize-images/', array(
204
  'methods' => 'GET',
205
+ 'callback' => array( $this->image_helper, 'optimize_images' ),
206
  'permission_callback' => array( $this, 'check_permissions' ),
207
  )
208
  );
210
  register_rest_route(
211
  self::REST_NAMESPACE, '/stop-images-optimization/', array(
212
  'methods' => 'GET',
213
+ 'callback' => array( $this->image_helper, 'stop_images_optimization' ),
214
  'permission_callback' => array( $this, 'check_permissions' ),
215
  )
216
  );
218
  register_rest_route(
219
  self::REST_NAMESPACE, '/check-image-optimizing-status/', array(
220
  'methods' => 'GET',
221
+ 'callback' => array( $this->image_helper, 'check_image_optimizing_status' ),
222
  'permission_callback' => array( $this, 'check_permissions' ),
223
  )
224
  );
226
  register_rest_route(
227
  self::REST_NAMESPACE, '/reset-images-optimization/', array(
228
  'methods' => 'GET',
229
+ 'callback' => array( $this->image_helper, 'reset_images_optimization' ),
230
  'permission_callback' => array( $this, 'check_permissions' ),
231
  )
232
  );
280
  register_rest_route(
281
  self::REST_NAMESPACE, '/enable-multisite-optimization/', array(
282
  'methods' => 'POST',
283
+ 'callback' => array( $this->multisite_helper, 'enable_multisite_optimization' ),
284
  'permission_callback' => array( $this, 'check_permissions' ),
285
  )
286
  );
288
  register_rest_route(
289
  self::REST_NAMESPACE, '/disable-multisite-optimization/', array(
290
  'methods' => 'POST',
291
+ 'callback' => array( $this->multisite_helper, 'disable_multisite_optimization' ),
292
  'permission_callback' => array( $this, 'check_permissions' ),
293
  )
294
  );
303
  register_rest_route(
304
  self::REST_NAMESPACE, '/hide-rating/', array(
305
  'methods' => 'GET',
306
+ 'callback' => array( $this->misc_helper, 'handle_hide_rating' ),
307
  'permission_callback' => array( $this, 'check_permissions' ),
308
  )
309
  );
311
  register_rest_route(
312
  self::REST_NAMESPACE, '/get-assets/', array(
313
  'methods' => 'GET',
314
+ 'callback' => array( $this->misc_helper, 'get_assets' ),
315
  'permission_callback' => array( $this, 'check_permissions' ),
316
  )
317
  );
319
  register_rest_route(
320
  self::REST_NAMESPACE, '/update-exclude-list/', array(
321
  'methods' => 'POST',
322
+ 'callback' => array( $this->misc_helper, 'update_exclude_list' ),
323
  'permission_callback' => array( $this, 'check_permissions' ),
324
  )
325
  );
327
  register_rest_route(
328
  self::REST_NAMESPACE, '/run-analysis/', array(
329
  'methods' => 'POST',
330
+ 'callback' => array( $this->misc_helper, 'run_analysis' ),
331
  'permission_callback' => array( $this, 'check_permissions' ),
332
  )
333
  );
334
  }
335
+ }
core/Rest/Rest_Helper.php CHANGED
@@ -1,172 +1,10 @@
1
  <?php
2
  namespace SiteGround_Optimizer\Rest;
3
 
4
- use SiteGround_Optimizer\Php_Checker\Php_Checker;
5
- use SiteGround_Optimizer\Options\Options;
6
- use SiteGround_Optimizer\Supercacher\Supercacher;
7
- use SiteGround_Optimizer\Memcache\Memcache;
8
- use SiteGround_Optimizer\Ssl\Ssl;
9
- use SiteGround_Optimizer\Htaccess\Htaccess;
10
- use SiteGround_Optimizer\Multisite\Multisite;
11
- use SiteGround_Optimizer\Images_Optimizer\Images_Optimizer;
12
- use SiteGround_Optimizer\Front_End_Optimization\Front_End_Optimization;
13
- use SiteGround_Optimizer\Helper\Helper;
14
- use SiteGround_Optimizer\Analysis\Analysis;
15
-
16
  /**
17
  * Rest Helper class that process all rest requests and provide json output for react app.
18
  */
19
- class Rest_Helper {
20
- /**
21
- * The constructor.
22
- */
23
- public function __construct() {
24
- $this->checker = new Php_Checker();
25
- $this->options = new Options();
26
- $this->memcache = new Memcache();
27
- $this->ssl = new Ssl();
28
- $this->htaccess = new Htaccess();
29
- $this->multisite = new Multisite();
30
- }
31
-
32
- /**
33
- * Update excluded urls.
34
- *
35
- * @since 5.0.0
36
- *
37
- * @param object $request Request data.
38
- */
39
- public function update_excluded_urls( $request ) {
40
- $data = json_decode( $request->get_body(), true );
41
-
42
- // Bail if the urls are not set.
43
- if ( ! isset( $data['excluded_urls'] ) ) {
44
- wp_send_json_error();
45
- }
46
-
47
- // Convert the json urls to array.
48
- $urls = json_decode( $data['excluded_urls'], true );
49
-
50
- // Update the option.
51
- $result = update_option( 'siteground_optimizer_excluded_urls', $urls );
52
-
53
- // Purge the cache.
54
- Supercacher::purge_cache();
55
-
56
- wp_send_json(
57
- array(
58
- 'success' => $result,
59
- 'data' => $urls,
60
- )
61
- );
62
- }
63
-
64
- /**
65
- * Initialize images optimization
66
- *
67
- * @since 5.0.0
68
- */
69
- public function optimize_images() {
70
- $this->images_optimizer = new Images_Optimizer();
71
- $this->images_optimizer->initialize();
72
-
73
- wp_send_json_success(
74
- array(
75
- 'image_optimization_status' => 0,
76
- 'image_optimization_stopped' => 0,
77
- 'has_images_for_optimization' => get_option( 'siteground_optimizer_total_unoptimized_images', 0 ),
78
- 'total_unoptimized_images' => get_option( 'siteground_optimizer_total_unoptimized_images', 0 ),
79
- )
80
- );
81
- }
82
-
83
- /**
84
- * Checks if the option key exists.
85
- *
86
- * @since 5.0.0
87
- *
88
- * @param object $request Request data.
89
- */
90
- public function enable_option_from_rest( $request ) {
91
- // Get the option key.
92
- $key = $this->validate_and_get_option_value( $request, 'option_key' );
93
- $is_network = $this->validate_and_get_option_value( $request, 'is_multisite', false );
94
- $result = Options::enable_option( $key, $is_network );
95
-
96
- // Enable the option.
97
- wp_send_json(
98
- array(
99
- 'success' => $result,
100
- 'data' => array(
101
- 'message' => $this->options->get_response_message( $result, $key, true ),
102
- ),
103
- )
104
- );
105
- }
106
-
107
- /**
108
- * Checks if the option key exists.
109
- *
110
- * @since 5.0.0
111
- *
112
- * @param object $request Request data.
113
- *
114
- * @return string The option key.
115
- */
116
- public function disable_option_from_rest( $request ) {
117
- // Get the option key.
118
- $key = $this->validate_and_get_option_value( $request, 'option_key' );
119
- $is_network = $this->validate_and_get_option_value( $request, 'is_multisite', false );
120
- $result = Options::disable_option( $key, $is_network );
121
-
122
- // Disable the option.
123
- return wp_send_json(
124
- array(
125
- 'success' => $result,
126
- 'data' => array(
127
- 'message' => $this->options->get_response_message( $result, $key, false ),
128
- ),
129
- )
130
- );
131
- }
132
-
133
- /**
134
- * Checks if the option key exists.
135
- *
136
- * @since 5.5.0
137
- *
138
- * @param object $request Request data.
139
- *
140
- * @return string The option key.
141
- */
142
- public function change_option_from_rest( $request ) {
143
- $allowed_options = array(
144
- 'siteground_optimizer_quality_webp',
145
- 'siteground_optimizer_quality_type',
146
- 'siteground_optimizer_quality_images',
147
- );
148
-
149
- // Get the option key.
150
- $key = $this->validate_and_get_option_value( $request, 'option_key' );
151
-
152
-
153
- // Bail if the option is now allowed.
154
- if ( ! in_array( $key, $allowed_options ) ) {
155
- wp_send_json_error();
156
- }
157
-
158
- $value = $this->validate_and_get_option_value( $request, 'value' );
159
- $is_network = $this->validate_and_get_option_value( $request, 'is_multisite', false );
160
- $result = Options::change_option( $key, $value, $is_network );
161
-
162
- // Chnage the option.
163
- return wp_send_json(
164
- array(
165
- 'success' => $result,
166
- )
167
- );
168
- }
169
-
170
  /**
171
  * Checks if the `option_key` paramether exists in rest data.
172
  *
@@ -178,7 +16,7 @@ class Rest_Helper {
178
  *
179
  * @return string The option value.
180
  */
181
- private function validate_and_get_option_value( $request, $key, $bail = true ) {
182
  $data = json_decode( $request->get_body(), true );
183
 
184
  // Bail if the option key is not set.
@@ -188,493 +26,4 @@ class Rest_Helper {
188
 
189
  return $data[ $key ];
190
  }
191
-
192
- /**
193
- * Provide all plugin options.
194
- *
195
- * @since 5.0.0
196
- */
197
- public function fetch_options() {
198
- // Fetch the options.
199
- $options = $this->options->fetch_options();
200
-
201
- if ( is_multisite() ) {
202
- $options['sites_data'] = $this->multisite->get_sites_info();
203
- }
204
-
205
- $options['has_images'] = $this->options->check_for_images();
206
- $options['has_images_for_optimization'] = $this->options->check_for_unoptimized_images();
207
- $options['assets'] = Front_End_Optimization::get_instance()->get_assets();
208
- $options['quality_type'] = get_option( 'siteground_optimizer_quality_type', '' );
209
-
210
- // Check for non converted images when we are on avalon server.
211
- if ( Helper::is_avalon() ) {
212
- $options['has_images_for_conversion'] = $this->options->check_for_non_converted_images();
213
- }
214
-
215
- // Send the options to react app.
216
- wp_send_json_success( $options );
217
- }
218
-
219
- /**
220
- * Purge the cache and send json response
221
- *
222
- * @since 5.0.0
223
- */
224
- public function purge_cache_from_rest() {
225
- Supercacher::purge_cache();
226
- // Disable the option.
227
- wp_send_json_success();
228
- }
229
-
230
- /**
231
- * Test if url is cached.
232
- *
233
- * @since 5.0.0
234
- *
235
- * @param object $request Request data.
236
- */
237
- public function test_cache( $request ) {
238
- // Get the url.
239
- $url = $this->validate_and_get_option_value( $request, 'url' );
240
-
241
- // Check if the url is cached.
242
- $is_cached = Supercacher::test_cache( $url );
243
- // Send response to the app.
244
- wp_send_json_success( array( 'cached' => $is_cached ) );
245
- }
246
-
247
- /**
248
- * Enable memcached.
249
- *
250
- * @since 5.0.0
251
- */
252
- public function enable_memcache() {
253
- $port = $this->memcache->get_memcached_port();
254
-
255
- if ( empty( $port ) ) {
256
- wp_send_json_error(
257
- array(
258
- 'message' => __( 'SG Optimizer was unable to connect to the Memcached server and it was disabled. Please, check your SiteGround control panel and turn it on if disabled.', 'sg-cachepress' ),
259
- )
260
- );
261
- }
262
-
263
- // First enable the option.
264
- $result = Options::enable_option( 'siteground_optimizer_enable_memcached' );
265
-
266
- // Remove notices.
267
- Options::disable_option( 'siteground_optimizer_memcache_notice' );
268
- Options::disable_option( 'siteground_optimizer_memcache_crashed' );
269
- Options::disable_option( 'siteground_optimizer_memcache_dropin_crashed' );
270
-
271
- // Send success if the dropin has been created.
272
- if ( $result && $this->memcache->create_memcached_dropin() ) {
273
- wp_send_json_success(
274
- array(
275
- 'message' => __( 'Memcached Enabled', 'sg-cachepress' ),
276
- )
277
- );
278
- } else {
279
- if ( 11211 === $port ) {
280
- wp_send_json_error(
281
- array(
282
- 'message' => __( 'SG Optimizer was unable to connect to the Memcached server and it was disabled. Please, check your SiteGround control panel and turn it on if disabled.', 'sg-cachepress' ),
283
- )
284
- );
285
- }
286
- }
287
-
288
- // Dropin cannot be created.
289
- wp_send_json_error(
290
- array(
291
- 'message' => __( 'Could Not Enable Memcache!', 'sg-cachepress' ),
292
- )
293
- );
294
- }
295
-
296
- /**
297
- * Disable memcached.
298
- *
299
- * @since 5.0.0
300
- */
301
- public function disable_memcache() {
302
- // First disable the option.
303
- $result = Options::disable_option( 'siteground_optimizer_enable_memcached' );
304
-
305
- // Send success if the option has been disabled and the dropin doesn't exist.
306
- if ( ! $this->memcache->dropin_exists() ) {
307
- wp_send_json_success(
308
- array(
309
- 'message' => __( 'Memcached Disabled!', 'sg-cachepress' ),
310
- )
311
- );
312
- }
313
-
314
- // Try to remove the dropin.
315
- $is_dropin_removed = $this->memcache->remove_memcached_dropin();
316
-
317
- // Remove notices.
318
- Options::disable_option( 'siteground_optimizer_memcache_notice' );
319
- Options::disable_option( 'siteground_optimizer_memcache_crashed' );
320
- Options::disable_option( 'siteground_optimizer_memcache_dropin_crashed' );
321
-
322
- // Send success if the droping has been removed.
323
- if ( $is_dropin_removed ) {
324
- wp_send_json_success(
325
- array(
326
- 'message' => __( 'Memcached Disabled!', 'sg-cachepress' ),
327
- )
328
- );
329
- }
330
-
331
- // The dropin cannot be removed.
332
- wp_send_json_error(
333
- array(
334
- 'message' => __( 'Could Not Disable Memcache!', 'sg-cachepress' ),
335
- )
336
- );
337
- }
338
-
339
- /**
340
- * Enable the ssl
341
- *
342
- * @param object $request Request data.
343
- *
344
- * @since 5.0.0
345
- */
346
- public function enable_ssl( $request ) {
347
- $key = $this->validate_and_get_option_value( $request, 'option_key' );
348
- // Bail if the domain doens't nove ssl certificate.
349
- if ( ! $this->ssl->has_certificate() ) {
350
- wp_send_json_error(
351
- array(
352
- 'message' => __( 'Please, install an SSL certificate first!', 'sg-cachepress' ),
353
- )
354
- );
355
- }
356
-
357
- $result = $this->ssl->enable();
358
-
359
- wp_send_json(
360
- array(
361
- 'success' => $result,
362
- 'data' => array(
363
- 'message' => $this->options->get_response_message( $result, $key, true ),
364
- ),
365
- )
366
- );
367
- }
368
-
369
- /**
370
- * Disable the ssl.
371
- *
372
- * @param object $request Request data.
373
- *
374
- * @since 5.0.0
375
- */
376
- public function disable_ssl( $request ) {
377
- $key = $this->validate_and_get_option_value( $request, 'option_key' );
378
- $result = $this->ssl->disable();
379
-
380
- wp_send_json(
381
- array(
382
- 'success' => $result,
383
- 'data' => array(
384
- 'message' => $this->options->get_response_message( $result, $key, false ),
385
- ),
386
- )
387
- );
388
- }
389
-
390
- /**
391
- * Handle compatibility check.
392
- *
393
- * @since 5.0.0
394
- *
395
- * @param object $request Request data.
396
- */
397
- public function handle_compatibility_check( $request ) {
398
- // Get the php version.
399
- $php_version = $this->validate_and_get_option_value( $request, 'php_version' );
400
-
401
- // Add the background processes.
402
- $this->checker->initialize( $php_version );
403
- // Send successful response.
404
- wp_send_json_success();
405
- }
406
-
407
- /**
408
- * Switch the current php version.
409
- *
410
- * @since 5.0.0
411
- *
412
- * @param object $request Request data.
413
- */
414
- public function switch_php( $request ) {
415
- // Get the php version.
416
- $php_version = $this->validate_and_get_option_value( $request, 'php_version' );
417
-
418
- $php_versions_request = wp_remote_get( 'https://updates.sgvps.net/supported-versions.json' );
419
- $php_versions = json_decode( wp_remote_retrieve_body( $php_versions_request ), true );
420
- // Add the new recommended php version.
421
- $php_versions['versions'][] = 'recommended-php';
422
-
423
- if ( ! in_array( $php_version, $php_versions['versions'], false ) ) {
424
- wp_send_json(
425
- array(
426
- 'success' => false,
427
- 'data' => array(
428
- 'message' => __( 'Cannot change PHP Version', 'sg-cachepress' ),
429
- ),
430
- )
431
- );
432
- }
433
-
434
- $this->htaccess->disable( 'php' );
435
- $result = $this->htaccess->enable(
436
- 'php',
437
- array(
438
- 'search' => 'recommended-php' === $php_version ? 'php_PHPVERSION_' : '_PHPVERSION_',
439
- 'replace' => str_replace( '.', '', $php_version ),
440
- )
441
- );
442
-
443
- // Reset the compatibility.
444
- $this->checker->complete();
445
- update_option( 'siteground_optimizer_phpcompat_is_compatible', 0 );
446
- update_option( 'siteground_optimizer_phpcompat_result', array() );
447
-
448
- wp_send_json(
449
- array(
450
- 'success' => $result,
451
- 'data' => array(
452
- 'message' => $result ? __( 'PHP Version has been changed', 'sg-cachepress' ) : __( 'Cannot change PHP Version', 'sg-cachepress' ),
453
- ),
454
- )
455
- );
456
- }
457
-
458
- /**
459
- * Return the status of current compatibility check.
460
- *
461
- * @since 5.0.0
462
- */
463
- public function handle_compatibility_status_check() {
464
- wp_send_json_success(
465
- array(
466
- 'phpcompat_status' => (int) get_option( 'siteground_optimizer_phpcompat_status', 0 ),
467
- 'phpcompat_progress' => (int) get_option( 'siteground_optimizer_phpcompat_progress', 1 ),
468
- 'phpcompat_is_compatible' => (int) get_option( 'siteground_optimizer_phpcompat_is_compatible', 0 ),
469
- 'phpcompat_result' => get_option( 'siteground_optimizer_phpcompat_result' ),
470
- )
471
- );
472
- }
473
-
474
- /**
475
- * Stops images optimization
476
- *
477
- * @since 5.0.8
478
- */
479
- public function stop_images_optimization() {
480
- // Clear the scheduled cron after the optimization is completed.
481
- wp_clear_scheduled_hook( 'siteground_optimizer_start_image_optimization_cron' );
482
-
483
- // Update the status to finished.
484
- update_option( 'siteground_optimizer_image_optimization_completed', 1, false );
485
- update_option( 'siteground_optimizer_image_optimization_status', 1, false );
486
- update_option( 'siteground_optimizer_image_optimization_stopped', 1, false );
487
-
488
- // Delete the lock.
489
- delete_option( 'siteground_optimizer_image_optimization_lock' );
490
-
491
- wp_send_json_success(
492
- array(
493
- 'image_optimization_status' => 1,
494
- 'image_optimization_stopped' => 1,
495
- 'has_images_for_optimization' => $this->options->check_for_unoptimized_images(),
496
- )
497
- );
498
- }
499
-
500
- /**
501
- * Return the status of current compatibility check.
502
- *
503
- * @since 5.0.0
504
- */
505
- public function check_image_optimizing_status() {
506
- $unoptimized_images = $this->options->check_for_unoptimized_images();
507
-
508
- if ( 0 === $unoptimized_images ) {
509
- Images_Optimizer::complete();
510
- }
511
-
512
- $status = (int) get_option( 'siteground_optimizer_image_optimization_completed', 0 );
513
-
514
- wp_send_json_success(
515
- array(
516
- 'image_optimization_status' => $status,
517
- 'has_images_for_optimization' => $unoptimized_images,
518
- 'total_unoptimized_images' => (int) get_option( 'siteground_optimizer_total_unoptimized_images' ),
519
- )
520
- );
521
- }
522
-
523
- /**
524
- * Enable specific optimizations for a blog.
525
- *
526
- * @since 5.0.0
527
- *
528
- * @param object $request Request data.
529
- */
530
- public function enable_multisite_optimization( $request ) {
531
- $setting = $this->validate_and_get_option_value( $request, 'setting' );
532
- $blog_id = $this->validate_and_get_option_value( $request, 'blog_id' );
533
-
534
- foreach ( $blog_id as $id ) {
535
- $result = call_user_func( array( $this->multisite, $setting ), $id );
536
- }
537
-
538
- // Purge the cache.
539
- Supercacher::purge_cache();
540
-
541
- wp_send_json(
542
- array(
543
- 'success' => $result,
544
- )
545
- );
546
- }
547
-
548
- /**
549
- * Disable specific optimizations for a blog.
550
- *
551
- * @since 5.0.0
552
- *
553
- * @param object $request Request data.
554
- */
555
- public function disable_multisite_optimization( $request ) {
556
- $setting = $this->validate_and_get_option_value( $request, 'setting' );
557
- $blog_id = $this->validate_and_get_option_value( $request, 'blog_id' );
558
-
559
- foreach ( $blog_id as $id ) {
560
- $result = call_user_func( array( $this->multisite, $setting ), $id );
561
- }
562
-
563
- // Purge the cache.
564
- Supercacher::purge_cache();
565
-
566
- wp_send_json(
567
- array(
568
- 'success' => $result,
569
- )
570
- );
571
- }
572
-
573
- /**
574
- * Deletes images meta_key flag to allow reoptimization.
575
- *
576
- * @since 5.0.0
577
- */
578
- public function reset_images_optimization() {
579
- Images_Optimizer::reset_image_optimization_status();
580
-
581
- wp_send_json_success();
582
- }
583
-
584
- /**
585
- * Hide the rating box
586
- *
587
- * @since 5.0.12
588
- */
589
- public function handle_hide_rating() {
590
- update_option( 'siteground_optimizer_hide_rating', 1 );
591
- update_site_option( 'siteground_optimizer_hide_rating', 1 );
592
-
593
- wp_send_json_success();
594
- }
595
-
596
- /**
597
- * Update exclude list.
598
- *
599
- * @since 5.2.0
600
- *
601
- * @param object $request Request data.
602
- */
603
- public function update_exclude_list( $request ) {
604
- // List of predefined exclude lists.
605
- $exclude_lists = array(
606
- 'minify_javascript_exclude',
607
- 'async_javascript_exclude',
608
- 'minify_css_exclude',
609
- 'minify_html_exclude',
610
- 'excluded_lazy_load_classes',
611
- 'combine_css_exclude',
612
- 'combine_javascript_exclude',
613
- );
614
-
615
- // Get the type and handles data from the request.
616
- $type = $this->validate_and_get_option_value( $request, 'type' );
617
- $handle = $this->validate_and_get_option_value( $request, 'handle' );
618
-
619
- // Bail if the type is not listed in the predefined exclude list.
620
- if ( ! in_array( $type, $exclude_lists ) ) {
621
- wp_send_json_error();
622
- }
623
-
624
- $handles = get_option( 'siteground_optimizer_' . $type, array() );
625
- $key = array_search( $handle, $handles );
626
-
627
- if ( false === $key ) {
628
- array_push( $handles, $handle );
629
- } else {
630
- unset( $handles[ $key ] );
631
- }
632
-
633
- $handles = array_values( $handles );
634
-
635
- if ( in_array( $type, array( 'minify_html_exclude', 'excluded_lazy_load_classes' ) ) ) {
636
- $handles = $handle;
637
- }
638
-
639
- // Update the option.
640
- $result = update_option( 'siteground_optimizer_' . $type, $handles );
641
-
642
- // Purge the cache.
643
- Supercacher::purge_cache();
644
-
645
- // Send response to the react app.
646
- wp_send_json(
647
- array(
648
- 'success' => $result,
649
- 'handles' => $handles,
650
- )
651
- );
652
- }
653
-
654
-
655
- /**
656
- * Disable specific optimizations for a blog.
657
- *
658
- * @since 5.4.0
659
- *
660
- * @param object $request Request data.
661
- */
662
- public function run_analysis( $request ) {
663
-
664
- // Get the required params.
665
- $device = $this->validate_and_get_option_value( $request, 'device' );
666
- $url = $this->validate_and_get_option_value( $request, 'url', false );
667
-
668
- // Bail if any of the parameters is empty.
669
- if ( empty( $device ) ) {
670
- wp_send_json_error();
671
- }
672
-
673
- $analysis = new Analysis();
674
- $result = $analysis->run_analysis_rest( $url, $device );
675
-
676
- // Send the response.
677
- wp_send_json_success( $result );
678
- }
679
-
680
  }
1
  <?php
2
  namespace SiteGround_Optimizer\Rest;
3
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  /**
5
  * Rest Helper class that process all rest requests and provide json output for react app.
6
  */
7
+ abstract class Rest_Helper {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  /**
9
  * Checks if the `option_key` paramether exists in rest data.
10
  *
16
  *
17
  * @return string The option value.
18
  */
19
+ public function validate_and_get_option_value( $request, $key, $bail = true ) {
20
  $data = json_decode( $request->get_body(), true );
21
 
22
  // Bail if the option key is not set.
26
 
27
  return $data[ $key ];
28
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  }
core/Rest/Rest_Helper_Cache.php ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Supercacher\Supercacher;
5
+ use SiteGround_Optimizer\Options\Options;
6
+ use SiteGround_Optimizer\Memcache\Memcache;
7
+ /**
8
+ * Rest Helper class that manages caching options.
9
+ */
10
+ class Rest_Helper_Cache extends Rest_Helper {
11
+ /**
12
+ * The constructor.
13
+ */
14
+ public function __construct() {
15
+ $this->memcache = new Memcache();
16
+ }
17
+
18
+ /**
19
+ * Enable memcached.
20
+ *
21
+ * @since 5.0.0
22
+ */
23
+ public function enable_memcache() {
24
+ $port = $this->memcache->get_memcached_port();
25
+
26
+ if ( empty( $port ) ) {
27
+ wp_send_json_error(
28
+ array(
29
+ 'message' => __( 'SG Optimizer was unable to connect to the Memcached server and it was disabled. Please, check your SiteGround control panel and turn it on if disabled.', 'sg-cachepress' ),
30
+ )
31
+ );
32
+ }
33
+
34
+ // First enable the option.
35
+ $result = Options::enable_option( 'siteground_optimizer_enable_memcached' );
36
+
37
+ // Remove notices.
38
+ Options::disable_option( 'siteground_optimizer_memcache_notice' );
39
+ Options::disable_option( 'siteground_optimizer_memcache_crashed' );
40
+ Options::disable_option( 'siteground_optimizer_memcache_dropin_crashed' );
41
+
42
+ // Send success if the dropin has been created.
43
+ if ( $result && $this->memcache->create_memcached_dropin() ) {
44
+ wp_send_json_success(
45
+ array(
46
+ 'message' => __( 'Memcached Enabled', 'sg-cachepress' ),
47
+ )
48
+ );
49
+ } else {
50
+ if ( 11211 === $port ) {
51
+ wp_send_json_error(
52
+ array(
53
+ 'message' => __( 'SG Optimizer was unable to connect to the Memcached server and it was disabled. Please, check your SiteGround control panel and turn it on if disabled.', 'sg-cachepress' ),
54
+ )
55
+ );
56
+ }
57
+ }
58
+
59
+ // Dropin cannot be created.
60
+ wp_send_json_error(
61
+ array(
62
+ 'message' => __( 'Could Not Enable Memcache!', 'sg-cachepress' ),
63
+ )
64
+ );
65
+ }
66
+
67
+ /**
68
+ * Disable memcached.
69
+ *
70
+ * @since 5.0.0
71
+ */
72
+ public function disable_memcache() {
73
+ // First disable the option.
74
+ $result = Options::disable_option( 'siteground_optimizer_enable_memcached' );
75
+
76
+ // Send success if the option has been disabled and the dropin doesn't exist.
77
+ if ( ! $this->memcache->dropin_exists() ) {
78
+ wp_send_json_success(
79
+ array(
80
+ 'message' => __( 'Memcached Disabled!', 'sg-cachepress' ),
81
+ )
82
+ );
83
+ }
84
+
85
+ // Try to remove the dropin.
86
+ $is_dropin_removed = $this->memcache->remove_memcached_dropin();
87
+
88
+ // Remove notices.
89
+ Options::disable_option( 'siteground_optimizer_memcache_notice' );
90
+ Options::disable_option( 'siteground_optimizer_memcache_crashed' );
91
+ Options::disable_option( 'siteground_optimizer_memcache_dropin_crashed' );
92
+
93
+ // Send success if the droping has been removed.
94
+ if ( $is_dropin_removed ) {
95
+ wp_send_json_success(
96
+ array(
97
+ 'message' => __( 'Memcached Disabled!', 'sg-cachepress' ),
98
+ )
99
+ );
100
+ }
101
+
102
+ // The dropin cannot be removed.
103
+ wp_send_json_error(
104
+ array(
105
+ 'message' => __( 'Could Not Disable Memcache!', 'sg-cachepress' ),
106
+ )
107
+ );
108
+ }
109
+
110
+ /**
111
+ * Update excluded urls.
112
+ *
113
+ * @since 5.0.0
114
+ *
115
+ * @param object $request Request data.
116
+ */
117
+ public function update_excluded_urls( $request ) {
118
+ $data = $this->validate_and_get_option_value( $request, 'excluded_urls' );
119
+
120
+ // Convert the json urls to array.
121
+ $urls = json_decode( $data, true );
122
+
123
+ // Update the option.
124
+ $result = update_option( 'siteground_optimizer_excluded_urls', $urls );
125
+
126
+ // Purge the cache.
127
+ Supercacher::purge_cache();
128
+
129
+ wp_send_json(
130
+ array(
131
+ 'success' => $result,
132
+ 'data' => $urls,
133
+ )
134
+ );
135
+ }
136
+
137
+ /**
138
+ * Test if url is cached.
139
+ *
140
+ * @since 5.0.0
141
+ *
142
+ * @param object $request Request data.
143
+ */
144
+ public function test_cache( $request ) {
145
+ // Get the url.
146
+ $url = $this->validate_and_get_option_value( $request, 'url' );
147
+ // Check if the url is cached.
148
+ $is_cached = Supercacher::test_cache( $url );
149
+ // Send response to the app.
150
+ wp_send_json_success( array( 'cached' => $is_cached ) );
151
+ }
152
+
153
+ /**
154
+ * Purge the cache and send json response
155
+ *
156
+ * @since 5.0.0
157
+ */
158
+ public function purge_cache_from_rest() {
159
+ Supercacher::purge_cache();
160
+ // Disable the option.
161
+ wp_send_json_success();
162
+ }
163
+ }
core/Rest/Rest_Helper_Compatibility.php ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Php_Checker\Php_Checker;
5
+
6
+ /**
7
+ * Rest Helper class that manages enviroment optimisation settings.
8
+ */
9
+ class Rest_Helper_Compatibility extends Rest_Helper {
10
+ /**
11
+ * Handle compatibility check.
12
+ *
13
+ * @since 5.0.0
14
+ *
15
+ * @param object $request Request data.
16
+ */
17
+ public function handle_compatibility_check( $request ) {
18
+ // Init the Checker.
19
+ $checker = new Php_Checker();
20
+ // Get the php version.
21
+ $php_version = $this->validate_and_get_option_value( $request, 'php_version' );
22
+ // Add the background processes.
23
+ $checker->initialize( $php_version );
24
+ // Send successful response.
25
+ wp_send_json_success();
26
+ }
27
+
28
+ /**
29
+ * Return the status of current compatibility check.
30
+ *
31
+ * @since 5.0.0
32
+ */
33
+ public function handle_compatibility_status_check() {
34
+ wp_send_json_success(
35
+ array(
36
+ 'phpcompat_status' => (int) get_option( 'siteground_optimizer_phpcompat_status', 0 ),
37
+ 'phpcompat_progress' => (int) get_option( 'siteground_optimizer_phpcompat_progress', 1 ),
38
+ 'phpcompat_is_compatible' => (int) get_option( 'siteground_optimizer_phpcompat_is_compatible', 0 ),
39
+ 'phpcompat_result' => get_option( 'siteground_optimizer_phpcompat_result' ),
40
+ )
41
+ );
42
+ }
43
+ }
core/Rest/Rest_Helper_Environment.php ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Php_Checker\Php_Checker;
5
+ use SiteGround_Optimizer\Options\Options;
6
+ use SiteGround_Optimizer\Ssl\Ssl;
7
+ use SiteGround_Optimizer\Htaccess\Htaccess;
8
+
9
+ /**
10
+ * Rest Helper class that manages enviroment optimisation settings.
11
+ */
12
+
13
+ class Rest_Helper_Environment extends Rest_Helper{
14
+
15
+ /**
16
+ * Link to json containing SiteGround supported PHP versions.
17
+ *
18
+ * @since 5.5.8
19
+ */
20
+ const SUPPORTED_VERSIONS = 'https://updates.sgvps.net/supported-versions.json';
21
+
22
+ /**
23
+ * The constructor.
24
+ */
25
+ public function __construct() {
26
+ $this->options = new Options();
27
+ $this->checker = new Php_Checker();
28
+ $this->htaccess = new Htaccess();
29
+ $this->ssl = new Ssl();
30
+ }
31
+
32
+ /**
33
+ * Switch the current php version.
34
+ *
35
+ * @since 5.0.0
36
+ *
37
+ * @param object $request Request data.
38
+ */
39
+ public function switch_php( $request ) {
40
+ // Get the php version.
41
+ $php_version = $this->validate_and_get_option_value( $request, 'php_version' );
42
+ // Get the supported versions.
43
+ $php_versions_request = wp_remote_get( self::SUPPORTED_VERSIONS );
44
+ // Decode the response.
45
+ $php_versions = json_decode( wp_remote_retrieve_body( $php_versions_request ), true );
46
+ // Add the new recommended php version.
47
+ $php_versions['versions'][] = 'recommended-php';
48
+
49
+ if ( ! in_array( $php_version, $php_versions['versions'], false ) ) {
50
+ wp_send_json(
51
+ array(
52
+ 'success' => false,
53
+ 'data' => array(
54
+ 'message' => __( 'Cannot change PHP Version', 'sg-cachepress' ),
55
+ ),
56
+ )
57
+ );
58
+ }
59
+
60
+ $this->htaccess->disable( 'php' );
61
+ $result = $this->htaccess->enable(
62
+ 'php',
63
+ array(
64
+ 'search' => 'recommended-php' === $php_version ? 'php_PHPVERSION_' : '_PHPVERSION_',
65
+ 'replace' => str_replace( '.', '', $php_version ),
66
+ )
67
+ );
68
+
69
+ // Reset the compatibility.
70
+ $this->checker->complete();
71
+ update_option( 'siteground_optimizer_phpcompat_is_compatible', 0 );
72
+ update_option( 'siteground_optimizer_phpcompat_result', array() );
73
+
74
+ wp_send_json(
75
+ array(
76
+ 'success' => $result,
77
+ 'data' => array(
78
+ 'message' => $result ? __( 'PHP Version has been changed', 'sg-cachepress' ) : __( 'Cannot change PHP Version', 'sg-cachepress' ),
79
+ ),
80
+ )
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Enable the ssl
86
+ *
87
+ * @param object $request Request data.
88
+ *
89
+ * @since 5.0.0
90
+ */
91
+ public function enable_ssl( $request ) {
92
+ $key = $this->validate_and_get_option_value( $request, 'option_key' );
93
+ // Bail if the domain doens't nove ssl certificate.
94
+ if ( ! $this->ssl->has_certificate() ) {
95
+ wp_send_json_error(
96
+ array(
97
+ 'message' => __( 'Please, install an SSL certificate first!', 'sg-cachepress' ),
98
+ )
99
+ );
100
+ }
101
+
102
+ $result = $this->ssl->enable();
103
+
104
+ wp_send_json(
105
+ array(
106
+ 'success' => $result,
107
+ 'data' => array(
108
+ 'message' => $this->options->get_response_message( $result, $key, true ),
109
+ ),
110
+ )
111
+ );
112
+ }
113
+
114
+ /**
115
+ * Disable the ssl.
116
+ *
117
+ * @param object $request Request data.
118
+ *
119
+ * @since 5.0.0
120
+ */
121
+ public function disable_ssl( $request ) {
122
+ $key = $this->validate_and_get_option_value( $request, 'option_key' );
123
+ $result = $this->ssl->disable();
124
+
125
+ wp_send_json(
126
+ array(
127
+ 'success' => $result,
128
+ 'data' => array(
129
+ 'message' => $this->options->get_response_message( $result, $key, false ),
130
+ ),
131
+ )
132
+ );
133
+ }
134
+ }
core/Rest/Rest_Helper_Images.php ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Images_Optimizer\Images_Optimizer;
5
+ use SiteGround_Optimizer\Options\Options;
6
+
7
+ /**
8
+ * Rest Helper class that manages image optimisation settings.
9
+ */
10
+ class Rest_Helper_Images {
11
+ /**
12
+ * The constructor.
13
+ */
14
+ public function __construct() {
15
+ $this->options = new Options();
16
+ }
17
+
18
+ /**
19
+ * Initialize images optimization
20
+ *
21
+ * @since 5.0.0
22
+ */
23
+ public function optimize_images() {
24
+ $this->images_optimizer = new Images_Optimizer();
25
+ $this->images_optimizer->initialize();
26
+
27
+ wp_send_json_success(
28
+ array(
29
+ 'image_optimization_status' => 0,
30
+ 'image_optimization_stopped' => 0,
31
+ 'has_images_for_optimization' => get_option( 'siteground_optimizer_total_unoptimized_images', 0 ),
32
+ 'total_unoptimized_images' => get_option( 'siteground_optimizer_total_unoptimized_images', 0 ),
33
+ )
34
+ );
35
+ }
36
+
37
+ /**
38
+ * Stops images optimization
39
+ *
40
+ * @since 5.0.8
41
+ */
42
+ public function stop_images_optimization() {
43
+ // Clear the scheduled cron after the optimization is completed.
44
+ wp_clear_scheduled_hook( 'siteground_optimizer_start_image_optimization_cron' );
45
+
46
+ // Update the status to finished.
47
+ update_option( 'siteground_optimizer_image_optimization_completed', 1, false );
48
+ update_option( 'siteground_optimizer_image_optimization_status', 1, false );
49
+ update_option( 'siteground_optimizer_image_optimization_stopped', 1, false );
50
+
51
+ // Delete the lock.
52
+ delete_option( 'siteground_optimizer_image_optimization_lock' );
53
+
54
+ wp_send_json_success(
55
+ array(
56
+ 'image_optimization_status' => 1,
57
+ 'image_optimization_stopped' => 1,
58
+ 'has_images_for_optimization' => $this->options->check_for_unoptimized_images(),
59
+ )
60
+ );
61
+ }
62
+
63
+ /**
64
+ * Return the status of current compatibility check.
65
+ *
66
+ * @since 5.0.0
67
+ */
68
+ public function check_image_optimizing_status() {
69
+ $unoptimized_images = $this->options->check_for_unoptimized_images();
70
+
71
+ if ( 0 === $unoptimized_images ) {
72
+ Images_Optimizer::complete();
73
+ }
74
+
75
+ $status = (int) get_option( 'siteground_optimizer_image_optimization_completed', 0 );
76
+
77
+ wp_send_json_success(
78
+ array(
79
+ 'image_optimization_status' => $status,
80
+ 'has_images_for_optimization' => $unoptimized_images,
81
+ 'total_unoptimized_images' => (int) get_option( 'siteground_optimizer_total_unoptimized_images' ),
82
+ )
83
+ );
84
+ }
85
+
86
+ /**
87
+ * Deletes images meta_key flag to allow reoptimization.
88
+ *
89
+ * @since 5.0.0
90
+ */
91
+ public function reset_images_optimization() {
92
+ Images_Optimizer::reset_image_optimization_status();
93
+
94
+ wp_send_json_success();
95
+ }
96
+ }
core/Rest/Rest_Helper_Misc.php ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Supercacher\Supercacher;
5
+ use SiteGround_Optimizer\Analysis\Analysis;
6
+ /**
7
+ * Rest Helper class that manages misc rest routes settings.
8
+ */
9
+ class Rest_Helper_Misc extends Rest_Helper {
10
+
11
+ /**
12
+ * Hide the rating box
13
+ *
14
+ * @since 5.0.12
15
+ */
16
+ public function handle_hide_rating() {
17
+ // Hide the rating box.
18
+ update_option( 'siteground_optimizer_hide_rating', 1 );
19
+ update_site_option( 'siteground_optimizer_hide_rating', 1 );
20
+
21
+ // Send the response.
22
+ wp_send_json_success();
23
+ }
24
+
25
+ /**
26
+ * Update exclude list.
27
+ *
28
+ * @since 5.2.0
29
+ *
30
+ * @param object $request Request data.
31
+ */
32
+ public function update_exclude_list( $request ) {
33
+ // List of predefined exclude lists.
34
+ $exclude_lists = array(
35
+ 'minify_javascript_exclude',
36
+ 'async_javascript_exclude',
37
+ 'minify_css_exclude',
38
+ 'minify_html_exclude',
39
+ 'excluded_lazy_load_classes',
40
+ 'combine_css_exclude',
41
+ 'combine_javascript_exclude',
42
+ );
43
+
44
+ // Get the type and handles data from the request.
45
+ $type = $this->validate_and_get_option_value( $request, 'type' );
46
+ $handle = $this->validate_and_get_option_value( $request, 'handle' );
47
+
48
+ // Bail if the type is not listed in the predefined exclude list.
49
+ if ( ! in_array( $type, $exclude_lists ) ) {
50
+ wp_send_json_error();
51
+ }
52
+
53
+ $handles = get_option( 'siteground_optimizer_' . $type, array() );
54
+ $key = array_search( $handle, $handles );
55
+
56
+ if ( false === $key ) {
57
+ array_push( $handles, $handle );
58
+ } else {
59
+ unset( $handles[ $key ] );
60
+ }
61
+
62
+ $handles = array_values( $handles );
63
+
64
+ if ( in_array( $type, array( 'minify_html_exclude', 'excluded_lazy_load_classes' ) ) ) {
65
+ $handles = $handle;
66
+ }
67
+
68
+ // Update the option.
69
+ $result = update_option( 'siteground_optimizer_' . $type, $handles );
70
+
71
+ // Purge the cache.
72
+ Supercacher::purge_cache();
73
+
74
+ // Send response to the react app.
75
+ wp_send_json(
76
+ array(
77
+ 'success' => $result,
78
+ 'handles' => $handles,
79
+ )
80
+ );
81
+ }
82
+
83
+ /**
84
+ * Disable specific optimizations for a blog.
85
+ *
86
+ * @since 5.4.0
87
+ *
88
+ * @param object $request Request data.
89
+ */
90
+ public function run_analysis( $request ) {
91
+
92
+ // Get the required params.
93
+ $device = $this->validate_and_get_option_value( $request, 'device' );
94
+ $url = $this->validate_and_get_option_value( $request, 'url', false );
95
+
96
+ // Bail if any of the parameters is empty.
97
+ if ( empty( $device ) ) {
98
+ wp_send_json_error();
99
+ }
100
+
101
+ $analysis = new Analysis();
102
+ $result = $analysis->run_analysis_rest( $url, $device );
103
+
104
+ // Send the response.
105
+ wp_send_json_success( $result );
106
+ }
107
+ }
core/Rest/Rest_Helper_Multisite.php ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Supercacher\Supercacher;
5
+ use SiteGround_Optimizer\Multisite\Multisite;
6
+
7
+ /**
8
+ * Rest Helper class that manages multisite settings.
9
+ */
10
+ class Rest_Helper_Multisite {
11
+ /**
12
+ * The constructor.
13
+ */
14
+ public function __construct() {
15
+ $this->multisite = new Multisite();
16
+ }
17
+
18
+ /**
19
+ * Enable specific optimizations for a blog.
20
+ *
21
+ * @since 5.0.0
22
+ *
23
+ * @param object $request Request data.
24
+ */
25
+ public function enable_multisite_optimization( $request ) {
26
+ $setting = $this->validate_and_get_option_value( $request, 'setting' );
27
+ $blog_id = $this->validate_and_get_option_value( $request, 'blog_id' );
28
+
29
+ foreach ( $blog_id as $id ) {
30
+ $result = call_user_func( array( $this->multisite, $setting ), $id );
31
+ }
32
+
33
+ // Purge the cache.
34
+ Supercacher::purge_cache();
35
+
36
+ wp_send_json(
37
+ array(
38
+ 'success' => $result,
39
+ )
40
+ );
41
+ }
42
+
43
+ /**
44
+ * Disable specific optimizations for a blog.
45
+ *
46
+ * @since 5.0.0
47
+ *
48
+ * @param object $request Request data.
49
+ */
50
+ public function disable_multisite_optimization( $request ) {
51
+ $setting = $this->validate_and_get_option_value( $request, 'setting' );
52
+ $blog_id = $this->validate_and_get_option_value( $request, 'blog_id' );
53
+
54
+ foreach ( $blog_id as $id ) {
55
+ $result = call_user_func( array( $this->multisite, $setting ), $id );
56
+ }
57
+
58
+ // Purge the cache.
59
+ Supercacher::purge_cache();
60
+
61
+ wp_send_json(
62
+ array(
63
+ 'success' => $result,
64
+ )
65
+ );
66
+ }
67
+ }
core/Rest/Rest_Helper_Options.php ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ namespace SiteGround_Optimizer\Rest;
3
+
4
+ use SiteGround_Optimizer\Options\Options;
5
+ use SiteGround_Optimizer\Multisite\Multisite;
6
+ use SiteGround_Optimizer\Front_End_Optimization\Front_End_Optimization;
7
+ use SiteGround_Optimizer\Helper\Helper;
8
+
9
+ /**
10
+ * Rest Helper class that manages all of the front end optimisation.
11
+ */
12
+ class Rest_Helper_Options extends Rest_Helper {
13
+ /**
14
+ * The constructor.
15
+ */
16
+ public function __construct() {
17
+ $this->options = new Options();
18
+ $this->multisite = new Multisite();
19
+ }
20
+ /**
21
+ * Checks if the option key exists.
22
+ *
23
+ * @since 5.0.0
24
+ *
25
+ * @param object $request Request data.
26
+ */
27
+ public function enable_option_from_rest( $request ) {
28
+ // Get the option key.
29
+ $key = $this->validate_and_get_option_value( $request, 'option_key' );
30
+ $is_network = $this->validate_and_get_option_value( $request, 'is_multisite', false );
31
+ $result = $this->options->enable_option( $key, $is_network );
32
+
33
+ // Enable the option.
34
+ wp_send_json(
35
+ array(
36
+ 'success' => $result,
37
+ 'data' => array(
38
+ 'message' => $this->options->get_response_message( $result, $key, true ),
39
+ ),
40
+ )
41
+ );
42
+ }
43
+
44
+ /**
45
+ * Checks if the option key exists.
46
+ *
47
+ * @since 5.0.0
48
+ *
49
+ * @param object $request Request data.
50
+ *
51
+ * @return string The option key.
52
+ */
53
+ public function disable_option_from_rest( $request ) {
54
+ // Get the option key.
55
+ $key = $this->validate_and_get_option_value( $request, 'option_key' );
56
+ $is_network = $this->validate_and_get_option_value( $request, 'is_multisite', false );
57
+ $result = $this->options->disable_option( $key, $is_network );
58
+
59
+ // Disable the option.
60
+ return wp_send_json(
61
+ array(
62
+ 'success' => $result,
63
+ 'data' => array(
64
+ 'message' => $this->options->get_response_message( $result, $key, false ),
65
+ ),
66
+ )
67
+ );
68
+ }
69
+
70
+ /**
71
+ * Checks if the option key exists.
72
+ *
73
+ * @since 5.5.0
74
+ *
75
+ * @param object $request Request data.
76
+ *
77
+ * @return string The option key.
78
+ */
79
+ public function change_option_from_rest( $request ) {
80
+ $allowed_options = array(
81
+ 'siteground_optimizer_quality_webp',
82
+ 'siteground_optimizer_quality_type',
83
+ 'siteground_optimizer_quality_images',
84
+ );
85
+
86
+ // Get the option key.
87
+ $key = $this->validate_and_get_option_value( $request, 'option_key' );
88
+
89
+ // Bail if the option is now allowed.
90
+ if ( ! in_array( $key, $allowed_options ) ) {
91
+ wp_send_json_error();
92
+ }
93
+
94
+ $value = $this->validate_and_get_option_value( $request, 'value' );
95
+ $is_network = $this->validate_and_get_option_value( $request, 'is_multisite', false );
96
+ $result = $this->options->change_option( $key, $value, $is_network );
97
+
98
+ // Chnage the option.
99
+ return wp_send_json(
100
+ array(
101
+ 'success' => $result,
102
+ )
103
+ );
104
+ }
105
+
106
+ /**
107
+ * Provide all plugin options.
108
+ *
109
+ * @since 5.0.0
110
+ */
111
+ public function fetch_options() {
112
+ // Fetch the options.
113
+ $options = $this->options->fetch_options();
114
+
115
+ if ( is_multisite() ) {
116
+ $options['sites_data'] = $this->multisite->get_sites_info();
117
+ }
118
+
119
+ $options['has_images'] = $this->options->check_for_images();
120
+ $options['has_images_for_optimization'] = $this->options->check_for_unoptimized_images();
121
+ $options['assets'] = Front_End_Optimization::get_instance()->get_assets();
122
+ $options['quality_type'] = get_option( 'siteground_optimizer_quality_type', '' );
123
+
124
+ // Check for non converted images when we are on avalon server.
125
+ if ( Helper::is_avalon() ) {
126
+ $options['has_images_for_conversion'] = $this->options->check_for_non_converted_images();
127
+ }
128
+
129
+ // Send the options to react app.
130
+ wp_send_json_success( $options );
131
+ }
132
+ }
core/Supercacher/Supercacher_Helper.php CHANGED
@@ -65,7 +65,20 @@ class Supercacher_Helper {
65
  * @since 5.0.0
66
  */
67
  public function set_bypass_cookie() {
68
- setcookie( 'wpSGCacheBypass', 1, time() + 100 * MINUTE_IN_SECONDS, '/' );
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  }
70
 
71
  /**
@@ -78,7 +91,7 @@ class Supercacher_Helper {
78
  return;
79
  }
80
 
81
- setcookie( 'wpSGCacheBypass', 0, time() - HOUR_IN_SECONDS, '/' );
82
  }
83
 
84
  /**
65
  * @since 5.0.0
66
  */
67
  public function set_bypass_cookie() {
68
+ if ( version_compare( phpversion(), '7.3', '>=' ) ) {
69
+ setcookie(
70
+ 'wpSGCacheBypass',
71
+ 1,
72
+ array(
73
+ 'expires' => time() + 100 * MINUTE_IN_SECONDS,
74
+ 'path' => COOKIEPATH,
75
+ 'httponly' => true,
76
+ 'samesite' => 'Lax',
77
+ )
78
+ );
79
+ } else {
80
+ setcookie( 'wpSGCacheBypass', 1, time() + 100 * MINUTE_IN_SECONDS, COOKIEPATH . ';samesite=Lax;' );
81
+ }
82
  }
83
 
84
  /**
91
  return;
92
  }
93
 
94
+ setcookie( 'wpSGCacheBypass', 0, time() - HOUR_IN_SECONDS, COOKIEPATH );
95
  }
96
 
97
  /**
readme.txt CHANGED
@@ -205,6 +205,13 @@ Our plugin uses a cookie in order to function properly. It does not store person
205
 
206
  == Changelog ==
207
 
 
 
 
 
 
 
 
208
  = Version 5.5.7 =
209
  * Improved Memcached Integration
210
  * Added protection for objects too big to be stored in Memcached
205
 
206
  == Changelog ==
207
 
208
+ = Version 5.5.8 =
209
+ * Added proper AMP support
210
+ * Added samesite parameter to the bypass cookie
211
+ * Added support for Shortcodes Ultimate
212
+ * Extended the sg purge wp-cli command to delete assets too
213
+ * Improved support for Uncode Themes
214
+
215
  = Version 5.5.7 =
216
  * Improved Memcached Integration
217
  * Added protection for objects too big to be stored in Memcached
sg-cachepress.php CHANGED
@@ -10,7 +10,7 @@
10
  * Plugin Name: SG Optimizer
11
  * Plugin URI: https://siteground.com
12
  * Description: This plugin will link your WordPress application with all the performance optimizations provided by SiteGround
13
- * Version: 5.5.7
14
  * Author: SiteGround
15
  * Author URI: https://www.siteground.com
16
  * Text Domain: sg-cachepress
@@ -31,7 +31,7 @@ if ( ! defined( 'WPINC' ) ) {
31
 
32
  // Define version constant.
33
  if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
34
- define( __NAMESPACE__ . '\VERSION', '5.5.7' );
35
  }
36
 
37
  // Define slug constant.
10
  * Plugin Name: SG Optimizer
11
  * Plugin URI: https://siteground.com
12
  * Description: This plugin will link your WordPress application with all the performance optimizations provided by SiteGround
13
+ * Version: 5.5.8
14
  * Author: SiteGround
15
  * Author URI: https://www.siteground.com
16
  * Text Domain: sg-cachepress
31
 
32
  // Define version constant.
33
  if ( ! defined( __NAMESPACE__ . '\VERSION' ) ) {
34
+ define( __NAMESPACE__ . '\VERSION', '5.5.8' );
35
  }
36
 
37
  // Define slug constant.