PluginProbe ʕ •ᴥ•ʔ
10Web Booster – Website speed optimization, Cache & Page Speed optimizer / trunk
10Web Booster – Website speed optimization, Cache & Page Speed optimizer vtrunk
2.33.0 2.30.5 2.30.7 2.30.9 2.31.10 2.31.8 2.32.11 2.32.21 2.32.3 2.32.4 2.32.7 2.6.31 2.6.40 2.6.42 2.6.7 2.7.37 2.7.44 2.7.47 2.8.18 2.8.19 2.8.32 2.8.34 2.8.35 2.9.23 2.9.24 2.9.25 2.9.27 v2.27.4 trunk 2.0.10 2.0.11 2.0.12 2.0.13 2.0.14 2.0.15 2.0.17 2.0.18 2.0.21 2.0.22 2.0.25 2.0.26 2.0.27 2.0.3 2.0.7 2.0.9 2.10.46 2.10.65 2.10.66 2.10.68 2.11.41 2.11.42 2.11.43 2.12.15 2.12.21 2.12.22 2.12.23 2.12.26 2.13.37 2.13.40 2.13.41 2.13.42 2.13.44 2.13.45 2.13.47 2.14.49 2.14.50 2.15.18 2.17.21 2.17.23 2.18.17 2.19.44 2.19.45 2.19.46 2.19.49 2.2.12 2.2.15 2.2.16 2.2.18 2.2.8 2.20.31 2.20.32 2.20.33 2.21.11 2.21.12 2.21.16 2.21.25 2.22.32 2.23.13 2.23.15 2.23.16 2.23.18 2.24.12 2.24.14 2.24.18 2.25.14 2.26.6 2.28.10 2.28.13 2.28.14 2.28.7 2.29.1 2.29.2 2.29.3 2.3.0 2.3.1 2.3.2 2.3.3 2.30.18
tenweb-speed-optimizer / vendor / 10web / authorization / src / Helper.php
tenweb-speed-optimizer / vendor / 10web / authorization / src Last commit date
config 1 month ago Amazon.php 4 years ago Helper.php 1 month ago InstalledPlugin.php 3 years ago InstalledTheme.php 4 years ago Login.php 1 month ago Product.php 4 years ago ProductActions.php 4 years ago ProductState.php 1 year ago WpAjaxUpgraderSkin.php 4 years ago
Helper.php
1383 lines
1 <?php
2 namespace Tenweb_Authorization;
3 use Tenweb_Authorization\ProductState;
4 class Helper
5 {
6 private static $site_state = array();
7 private static $error_logs = array();
8 private static $plugins_state = array();
9 private static $themes_state = array();
10 private static $addons_state = array();
11 private static $domain_id;
12
13 private static $instance;
14 private static $network_domain_id;
15 private static $auto_update_plugins;
16 private $login_instance;
17 public static $products_raw_data = array();
18
19 private static $installed_plugins_wp_info = null;
20 private static $installed_themes_wp_info = null;
21 private static $expiration = array(
22 'send_states' => array(
23 'expiration' => 43200,//12 hour 43200
24 'block_time' => 300,//5 minute 300
25 ),
26 'user_info' => array(
27 'expiration' => 43200,//12 hour
28 'block_time' => 300,//5 minute
29 ),
30 'user_agreements' => array(
31 'expiration' => 43200,//12 hour
32 'block_time' => 300,//5 minute
33 ),
34 'user_products' => array(
35 'expiration' => 86400,//24 hour
36 'block_time' => 3600,//1 hour
37 'max_blocks' => 24,//cap backoff at 24h
38 ),
39 );
40
41 private static $plugins = array();
42 private static $themes = array();
43 private static $addons = array();
44
45 public static $notices = array();
46 public $last_response;
47
48 public function __construct()
49 {
50 $this->login_instance = Login::get_instance();
51 self::$network_domain_id = get_site_option('tenweb_domain_id');
52 self::$domain_id = get_option('tenweb_domain_id');
53
54 add_action('tenweb_force_updates_check', array($this, 'tenweb_request_updates_check'));
55 add_action('wp_ajax_tenweb_do_updates_check', array($this, 'tenweb_update_plugins_themes_info'));
56 }
57
58 public function tenweb_request_updates_check() {
59 // Update the last check time
60 update_site_option('tenweb_last_update_check', time());
61 update_site_option('tenweb_last_update_request', time());
62
63 // Make a non-blocking request to our update endpoint
64 wp_remote_post(admin_url('admin-ajax.php?action=tenweb_do_updates_check'), array(
65 'timeout' => 0.01,
66 'blocking' => false,
67 'sslverify' => false,
68 'cookies' => array()
69 ));
70 }
71
72 public function tenweb_update_plugins_themes_info() {
73 include_once ABSPATH . WPINC . '/update.php';
74
75 // Perform the updates
76 wp_update_plugins();
77 wp_update_themes();
78
79 if (wp_doing_ajax()) {
80 wp_send_json_success();
81 }
82 }
83
84 public static function get_site_info($blog_id = null, $reset = false)
85 {
86 if (self::$site_state != null && $reset === false && is_null($blog_id)) {
87 return self::$site_state;
88 }
89
90 global $wp_version, $wpdb;
91
92 if ((is_multisite() && is_null($blog_id)) || (is_multisite() && $blog_id == 'multisite')) {
93 $home_url = network_admin_url();// or site_url
94 $admin_url = network_admin_url();
95 $site_title = get_site_option('site_name');
96 } else {
97 $home_url = get_home_url($blog_id);
98 $admin_url = get_admin_url($blog_id);
99 $site_title = get_bloginfo('name');
100 }
101
102 $sql_version = $wpdb->get_var("SELECT VERSION() AS version");
103
104 if (is_multisite() && $blog_id && $blog_id != 'multisite') {
105 $time_zone = get_blog_option($blog_id, 'timezone_string');
106 } else {
107 $time_zone = get_option('timezone_string');
108 }
109
110 if (empty($time_zone)) {
111 $time_zone = date_default_timezone_get();
112 if (!$time_zone || empty($time_zone)) {
113 $time_zone = "America/Los_Angeles";
114 }
115 }
116
117 $server_software = isset($_SERVER['SERVER_SOFTWARE']) && trim($_SERVER['SERVER_SOFTWARE']) !== '' ? $_SERVER['SERVER_SOFTWARE'] : 'unknown';
118
119 $iowd_version = defined('TENWEBIO_VERSION') ? 'iowd_'. TENWEBIO_VERSION : 'iowd_';
120
121 // Detect builder type
122 $pages_count = wp_count_posts('page');
123 $builder_type = null;
124 if ($pages_count && isset($pages_count->publish) && (int)$pages_count->publish > 0) {
125 $builder_type = self::detect_builder_type();
126 }
127
128 $site_info = array(
129 'platform' => 'wordpress',
130 'site_url' => $home_url,
131 'admin_url' => $admin_url,
132 'name' => $home_url,
133 'site_title' => $site_title,
134 'site_screenshot_url' => $home_url,
135 'platform_version' => $wp_version,
136 'php_version' => PHP_VERSION,
137 'mysql_version' => $sql_version,
138 'timezone' => $time_zone,//todo check on multisite
139 'server_type' => $server_software,
140 'server_version' => $server_software,
141 'other_data' => array(
142 'file_system' => array(
143 'method' => self::get_fs_method(),
144 'config' => self::check_fs_configs() ? 1 : 0
145 ),
146 "is_network" => ((is_multisite()) ? 1 : 0),
147 "blog_id" => $blog_id,
148 "manager_version" => TENWEB_VERSION
149 ),
150 "is_network" => ((is_multisite()) ? 1 : 0),
151 "manager_version" => get_site_option(TENWEB_PREFIX . '_from_image_optimizer') ? $iowd_version : TENWEB_VERSION,
152 );
153
154 // Add builder_type as separate field if detected
155 if ($builder_type !== null) {
156 $site_info['builder_type'] = $builder_type;
157 }
158
159 if (is_multisite() && is_numeric($blog_id)) {
160 $blog_details = get_blog_details($blog_id);
161 if (!empty($blog_details)) {
162 $site_info['other_data']['multisite_data'] = array(
163 'registered' => $blog_details->registered,
164 'last_updated' => $blog_details->last_updated,
165 );
166 }
167 }
168
169 self::$site_state = $site_info;
170
171 return self::$site_state;
172 }
173
174 public static function get_blogs_info()
175 {
176 $domains = array();
177 if (is_multisite()) {
178 $sites = get_sites();
179 foreach ($sites as $site) {
180 $blog_time_zone = get_blog_option($site->blog_id, 'timezone_string');
181 if (empty($blog_time_zone)) {
182 $blog_time_zone = date_default_timezone_get();
183 if (!$blog_time_zone || empty($blog_time_zone)) {
184 $blog_time_zone = "America/Los_Angeles";
185 }
186 }
187
188 $blog_details = get_blog_details($site->blog_id);
189 $domains[$site->blog_id]['site_url'] = get_home_url($site->blog_id);
190 $domains[$site->blog_id]['admin_url'] = get_admin_url($site->blog_id);
191 $domains[$site->blog_id]['site_title'] = $blog_details->blogname;
192 $domains[$site->blog_id]['timezone'] = $blog_time_zone;
193 $domains[$site->blog_id]['name'] = get_home_url($site->blog_id);
194 }
195 }
196
197 return $domains;
198 }
199
200 public static function set_error_log($key, $msg)
201 {
202 $logs = self::get_error_logs();
203 $logs[$key] = array('msg' => $msg, 'date' => date('Y-m-d H:i:s'));
204 $expiration = 31 * 24 * 60 * 60;
205 set_site_transient(TENWEB_PREFIX . '_auth_error_logs', $logs, $expiration);
206 self::$error_logs = $logs;
207 }
208 public static function clear_cache()
209 {
210 delete_site_transient(TENWEB_PREFIX . '_client_products_transient');
211 delete_site_transient(TENWEB_PREFIX . '_send_states_transient');
212 delete_site_transient(TENWEB_PREFIX . '_user_info_transient');
213 delete_site_transient(TENWEB_PREFIX . '_user_agreements_transient');
214 delete_site_option(TENWEB_PREFIX . '_requests_block');
215 }
216
217 public static function check_site_state($force_send = false, $screen_id = null, $current_blog_id = null, $additional_data=null)
218 {
219 if (is_multisite()) {
220 if (in_array($screen_id, array('options-general', 'site-info-network', 'plugins', 'themes'))) {
221 switch_to_blog($current_blog_id);
222 self::check_site_state_single($force_send, $current_blog_id);
223 restore_current_blog();
224 } else if ($screen_id == 'settings-network') {
225 self::check_site_state_single($force_send, 'multisite');
226 } else {
227 self::check_site_state_single($force_send, 'multisite');
228 $sites = get_sites();
229 foreach ($sites as $site) {
230 switch_to_blog($site->blog_id);
231 self::check_site_state_single($force_send, $site->blog_id);
232 restore_current_blog();
233 }
234 }
235 } else {
236 self::check_site_state_single($force_send, null, $additional_data);
237 }
238 }
239
240 public static function get_error_logs()
241 {
242
243 if (self::$error_logs == null) {
244 $logs = get_site_transient(TENWEB_PREFIX . '_auth_error_logs');
245 if (!is_array($logs)) {
246 $logs = array();
247 }
248 self::$error_logs = $logs;
249 }
250
251 return self::$error_logs;
252 }
253
254 public static function remove_error_logs(){
255 delete_site_transient(TENWEB_PREFIX . '_auth_error_logs');
256 }
257
258 private static function check_site_state_single($force_send, $blog_id = null, $additional_data=null)
259 {
260 $self = self::get_instance();
261 $self->set_products();
262 self::site_state($force_send, $blog_id, $additional_data);
263 }
264
265 public static function site_state($force_send = false, $current_blog_id = null, $additional_data=null)
266 {
267 $plugins_hash = get_option(TENWEB_PREFIX . '_plugins_state_hash');
268 $themes_hash = get_option(TENWEB_PREFIX . '_themes_state_hash');
269 $addons_hash = get_option(TENWEB_PREFIX . '_addons_state_hash');
270 if ($current_blog_id == 'multisite') {
271 $site_hash = get_site_option(TENWEB_PREFIX . '_site_state_hash');
272 } else {
273 $site_hash = get_option(TENWEB_PREFIX . '_site_state_hash');
274 }
275 $plugins_current_state = md5(json_encode(self::$plugins_state));
276 $themes_current_state = md5(json_encode(self::$themes_state));
277 $addons_current_state = md5(json_encode(self::$addons_state));
278 $site_info = self::get_site_info($current_blog_id);
279 $site_current_state = md5(json_encode($site_info));
280
281 if ($force_send === false) {
282 /* transient expired after 12 hour*/
283 $transient = get_site_transient(TENWEB_PREFIX . '_send_states_transient');
284 } else {
285 $transient = false;
286 }
287
288 $state_data = array("blog_id" => $current_blog_id);
289 if ($current_blog_id != 'multisite') {
290 if ($plugins_hash !== $plugins_current_state || $transient == false) {
291
292 $state_data['plugins_info'] = array(
293 "is_network" => ((is_multisite()) ? 1 : 0),
294 "products" => self::states_to_array(self::$plugins_state)
295 );
296 update_option(TENWEB_PREFIX . '_plugins_state_hash', $plugins_current_state);
297 }
298 if ($themes_hash !== $themes_current_state || $transient == false) {
299
300 $state_data['themes_info'] = array(
301 "is_network" => ((is_multisite()) ? 1 : 0),
302 "products" => self::states_to_array(self::$themes_state)
303 );
304 update_option(TENWEB_PREFIX . '_themes_state_hash', $themes_current_state);
305 }
306
307 if ($addons_hash !== $addons_current_state || $transient == false) {
308
309 $state_data['addons_info'] = array(
310 "is_network" => ((is_multisite()) ? 1 : 0),
311 "products" => self::states_to_array(self::$addons_state)
312 );
313 update_option(TENWEB_PREFIX . '_addons_state_hash', $addons_current_state);
314 }
315 }
316 if ($site_hash !== $site_current_state || $transient == false) {
317
318 $state_data['site_info'] = $site_info;
319 if ($current_blog_id == 'multisite') {
320 update_site_option(TENWEB_PREFIX . '_site_state_hash', $site_current_state);
321 } else {
322 update_option(TENWEB_PREFIX . '_site_state_hash', $site_current_state);
323 }
324 }
325
326 if (!empty($state_data)) {
327 if ($current_blog_id == 'multisite') {
328 $domain_id = get_site_option('tenweb_domain_id');
329 } else {
330 $domain_id = get_option('tenweb_domain_id');
331 }
332 self::set_domain_id($domain_id);
333
334 $result = self::send_site_state($state_data, $additional_data);
335
336 $send_all_data = (
337 !empty($state_data['plugins_info']) &&
338 !empty($state_data['themes_info']) &&
339 !empty($state_data['addons_info']) &&
340 !empty($state_data['site_info'])
341 );
342
343 if ($send_all_data == true && $result == true) {
344 $expiration = self::$expiration['send_states']['expiration'];
345 self::calc_request_block('send_states', true);
346 } else {
347 $block_count = self::calc_request_block('send_states');
348 $expiration = self::$expiration['send_states']['block_time'] * $block_count;
349 }
350
351 set_site_transient(TENWEB_PREFIX . '_send_states_transient', '1', $expiration);
352 do_action('tenweb_state_changed');
353 }
354 }
355 public static function states_to_array($states = array())
356 {
357 foreach ($states as $key => $state) {
358 if ($state instanceof ProductState) {
359 $states[$key] = $state->get_info();
360 } else {
361 unset($states[$key]);
362 }
363 }
364
365 if (!is_array($states)) {
366 return array();
367 }
368
369 return $states;
370 }
371 public static function send_site_state($data, $additional_data=null)
372 {
373 $connected_from = get_site_option(TENWEB_PREFIX . '_connected_from');
374 if ($connected_from == 'speed_optimizer') {
375 $data = self::filter_plugins_data($data, $additional_data);
376 }
377 if(empty(self::$domain_id)) {
378 self::$domain_id = get_option('tenweb_domain_id');
379 }
380 $url = TENWEB_API_URL . '/site-state/' . self::$domain_id;
381 if (!empty($data["site_info"])) {
382 $data["site_info"]["other_data"] = json_encode($data["site_info"]["other_data"]);
383 }
384 $args = array(
385 'method' => 'POST',
386 'body' => array('data' => $data)
387 );
388 $self = self::get_instance();
389 $response = $self->request($url, $args, 'send_site_state');
390
391
392 if ($response == null || isset($response['error'])) {
393 false;
394 }
395
396 return true;
397 }
398
399 public static function set_domain_id($domain_id)
400 {
401 self::$domain_id = $domain_id;
402 }
403
404 public function request($url, $args = array(), $error_key = null)
405 {
406 $blocked_request_option = get_site_transient(TENWEB_PREFIX . '_refresh_request_count', 0);
407
408 if ($blocked_request_option > 2 && $error_key !== 'check_single_token') {
409 return null;
410 }
411
412 if ($this->check_url($url) === false) {
413 return null;
414 }
415
416 if (empty($args['headers'])) {
417 $args['headers'] = array();
418 }
419
420 if ($error_key == null) {
421 $error_key = uniqid();
422 }
423
424 $args['headers']["Authorization"] = "Bearer " . $this->login_instance->get_access_token();
425 if (empty($args['headers']["Accept"])) {
426 $args['headers']["Accept"] = "application/x.10webmanager.v1+json";
427 }
428 $args['timeout'] = 50000;
429 $result = wp_remote_request($url, $args);
430
431 $this->last_response = $result;
432
433
434 if (is_wp_error($result)) {
435 self::set_error_log($error_key . '_wp_error', $result->get_error_message());
436
437 return null;
438 }
439
440 $body = json_decode($result['body'], true);
441 unset($args['headers']["Authorization"]); //do not log Auth token
442
443 $code = wp_remote_retrieve_response_code($result);
444 $is_hosted_website = self::check_if_manager_mu();
445 /* token refresh */
446 if (
447 $code == 401 &&
448 isset($body['error']['status_code']) && $body['error']['status_code'] == 401 &&
449 isset($body['error']['message']) &&
450 $body['error']['message'] == '10WebError:Authorization Error') {
451
452 self::set_error_log($error_key . '_token_error', json_encode($body['error']));
453
454 $in_progress_key = TENWEB_PREFIX . '_refreshing_token_in_progress';
455 $refreshing_token_in_progress = get_site_transient($in_progress_key);
456
457 if ($refreshing_token_in_progress) {
458 // Do NOT allow other refresh tokens while one of them is in progress
459 return $body;
460 } else {
461 set_site_transient($in_progress_key, 1, 300);
462
463 $token_refreshed = $this->refresh_token();
464
465 delete_site_transient($in_progress_key);
466
467 if ($token_refreshed) {
468 set_site_transient(TENWEB_PREFIX . '_refresh_request_count', ($blocked_request_option + 1), 3600 * 2); // 2 hours
469 // repeat current request
470 return $this->request($url, $args = array(), $error_key = null);
471 } else {
472 // error log already preserved
473 // force logout, token_refresh failed
474 if (!$is_hosted_website) {
475 $this->login_instance->logout(false);
476 }
477
478 return $body;
479 }
480 }
481 } else if ($code == 401) { // unknown authorization error
482 self::set_error_log($error_key . '_api_auth_error', json_encode([$body['error'], $url, $args]));
483 if (!$is_hosted_website) {
484 $this->login_instance->logout(false);
485 }
486
487 return $body;
488 } else if (isset($body['error'])) { // other errors
489 self::set_error_log($error_key . '_api_error', json_encode([$body['error'], $url, $args]));
490 }
491
492
493 return $body;
494 }
495
496 public function check_url($url)
497 {
498 global $tenweb_services;
499 $parsed_url = parse_url($url);
500
501 return (in_array($parsed_url['host'], $tenweb_services));
502 }
503
504 public static function check_if_manager_mu()
505 {
506 if (is_file(WPMU_PLUGIN_DIR . '/10web-manager/10web-manager.php')) {
507 return true;
508 }
509
510 return false;
511 }
512
513 public function refresh_token()
514 {
515 $tokens_data = array(
516 'refresh_token' => $this->login_instance->get_refresh_token(),
517 'access_token' => $this->login_instance->get_access_token(),
518 );
519
520
521 $url = TENWEB_API_URL . '/token/refresh';
522 $args = array(
523 'method' => 'POST',
524 'body' => $tokens_data,
525 'headers' => array(
526 'Accept' => "application/x.10webmanager.v1+json"
527 )
528 );
529
530 $this->login_instance->set_access_token(false);
531 $result = wp_remote_request($url, $args);
532
533
534 if (is_wp_error($result)) {
535 self::set_error_log('refresh_token_error', $result->get_error_message());
536
537 return false;
538 }
539
540 $res_array = json_decode($result['body'], true);
541
542 if (isset($res_array['error'])) {
543 /*API error */
544
545 self::set_error_log('refresh_token_error', json_encode($res_array['error']));
546
547 $this->login_instance->set_refresh_token(false);
548
549 return false;
550
551 } else if (isset($res_array['status']) && $res_array['status'] == 'ok') {
552
553 /* success */
554
555 $access_token = isset($res_array['token']) ? $res_array['token'] : false;
556 $refresh_token = isset($res_array['refresh_token']) ? $res_array['refresh_token'] : false;
557
558
559 self::set_error_log('refresh_token_success', ($access_token ? 'A' : '') . ($refresh_token ? 'R' : ''));
560
561 $this->login_instance->set_access_token($access_token);
562 $this->login_instance->set_refresh_token($refresh_token);
563
564 return true;
565 } else {
566 /* unknown error */
567 self::set_error_log('refresh_token_error', "unknown error");
568
569 return false;
570 }
571
572 }
573
574 public static function calc_request_block($key, $reset = false)
575 {
576
577 $blocks = get_site_option(TENWEB_PREFIX . '_requests_block');
578
579 if (!is_array($blocks)) {
580 $blocks = array();
581 }
582
583 if ($reset == true) {
584 $blocks[$key] = 0;
585 update_site_option(TENWEB_PREFIX . '_requests_block', $blocks);
586
587 return 1;
588 }
589
590 if (!isset($blocks[$key]) || $blocks[$key] < 0) {
591 $blocks[$key] = 0;
592 }
593
594 $blocks[$key] = ($blocks[$key] == 0) ? 1 : $blocks[$key] * 2;
595
596 $expiration_config = self::get_expiration($key);
597 $max_blocks = (isset($expiration_config['max_blocks'])) ? $expiration_config['max_blocks'] : 200;
598 if ($blocks[$key] > $max_blocks) {
599 $blocks[$key] = $max_blocks;
600 }
601
602 update_site_option(TENWEB_PREFIX . '_requests_block', $blocks);
603
604 return $blocks[$key];
605 }
606 /**
607 * Detect builder type based on active theme
608 * @return string|null Returns 'wvc' for wvc-theme, 'section_based' for tenweb-website-builder-theme, or null
609 */
610 public static function detect_builder_type()
611 {
612 $active_theme = wp_get_theme();
613 $theme_slug = $active_theme->get_stylesheet(); // Gets theme directory name
614 $text_domain = $active_theme->get('TextDomain');
615 $theme_name = $active_theme->get('Name');
616
617 // Check by theme directory/slug or text domain
618 if ($theme_slug === 'wvc-theme' || $text_domain === 'wvc-theme') {
619 return 'wvc';
620 }
621
622 if ($theme_slug === 'tenweb-website-builder-theme' || $text_domain === 'tenweb-website-builder-theme') {
623 return 'section_based';
624 }
625
626 // Fallback: check by theme name
627 if (stripos($theme_name, 'WordPress AI Builder') !== false ||
628 stripos($theme_name, 'wvc') !== false) {
629 return 'wvc';
630 }
631
632 if (stripos($theme_name, 'Builder Theme') !== false) {
633 return 'section_based';
634 }
635
636 return null;
637 }
638
639 public static function get_fs_method()
640 {
641 require_once(ABSPATH . 'wp-admin/includes/file.php');
642 require_once(ABSPATH . 'wp-admin/includes/misc.php'); // extract_from_markers() wp-super-cache deactivation fatal error fix
643
644 return get_filesystem_method();
645 }
646
647 public static function check_fs_configs()
648 {
649
650 $fs_method = self::get_fs_method();
651
652 if ($fs_method == "direct") {
653 return true;
654 }
655
656 $credentials['connection_type'] = $fs_method;
657 $credentials['hostname'] = (defined('FTP_HOST')) ? FTP_HOST : "";
658 $credentials['username'] = (defined('FTP_USER')) ? FTP_USER : "";
659 $credentials['password'] = (defined('FTP_PASS')) ? FTP_PASS : "";
660 $credentials['public_key'] = (defined('FTP_PUBKEY')) ? FTP_PUBKEY : "";
661 $credentials['private_key'] = (defined('FTP_PRIKEY')) ? FTP_PRIKEY : "";
662
663 if (
664 (!empty($credentials['password']) && !empty($credentials['username']) && !empty($credentials['hostname'])) ||
665 ('ssh' == $credentials['connection_type'] && !empty($credentials['public_key']) && !empty($credentials['private_key']))
666 ) {
667 return true;
668 } else {
669 return false;
670 }
671
672 }
673
674 public static function get_instance()
675 {
676 if (null == self::$instance) {
677 self::$instance = new self;
678 }
679
680 return self::$instance;
681 }
682
683 public function set_products($reset = false)
684 {
685 $plugins = get_site_option(TENWEB_PREFIX . '_plugins_list');
686 $themes = get_site_option(TENWEB_PREFIX . '_themes_list');
687 $addons = get_site_option(TENWEB_PREFIX . '_addons_list');
688
689 $transient = get_site_transient(TENWEB_PREFIX . '_client_products_transient');
690 if ($transient === false || $reset === true) {
691 $lock_key = TENWEB_PREFIX . '_products_lock';
692 $acquired = wp_cache_add($lock_key, 1, 'tenweb', 120);
693
694 if ($acquired) {
695 $products = $this->get_products();
696
697 if (!(empty($products['plugins']) && empty($products['themes']) && empty($products['addons']))) {
698 $plugins = $products['plugins'];
699 $themes = $products['themes'];
700 $addons = $products['addons'];
701
702 update_site_option(TENWEB_PREFIX . '_plugins_list', $plugins);
703 update_site_option(TENWEB_PREFIX . '_themes_list', $themes);
704 update_site_option(TENWEB_PREFIX . '_addons_list', $addons);
705
706 self::calc_request_block('user_products', true);
707 $expiration = self::get_expiration('user_products');
708 $expiration = $expiration['expiration'];
709 } else {
710 $block_count = self::calc_request_block('user_products');
711
712 $expiration = self::get_expiration('user_products');
713 $expiration = $expiration['block_time'] * $block_count;
714 }
715
716 set_site_transient(TENWEB_PREFIX . '_client_products_transient', '1', $expiration);
717 wp_cache_delete($lock_key, 'tenweb');
718 }
719 }
720
721 //if first api call failed
722 $plugins = (!is_array($plugins)) ? array() : $plugins;
723 $themes = (!is_array($themes)) ? array() : $themes;
724 $addons = (!is_array($addons)) ? array() : $addons;
725
726 self::$products_raw_data = array('plugins' => $plugins, 'themes' => $themes, 'addons' => $addons);
727
728 $products_objects = self::get_products_objects($plugins, $themes, $addons);
729
730 self::$plugins = $products_objects['plugins'];
731 self::$themes = $products_objects['themes'];
732 self::$addons = $products_objects['addons'];
733 }
734
735 public function get_products($type = 'all')
736 {
737
738 $result = array(
739 'plugins' => array(),
740 'themes' => array(),
741 'addons' => array()
742 );
743 $endpoint = TENWEB_API_URL . '/products';
744
745 /*$data = $this->get_product_data_from_api($endpoint . '/plugins');
746
747 if (!empty($data)) {
748 $result['plugins'] = $data;
749 }*/
750
751 $data = $this->get_product_data_from_api($endpoint);
752
753 if (!empty($data['plugins'])) {
754 $result['plugins'] = $data['plugins'];
755 }
756 if (!empty($data['themes'])) {
757 $result['themes'] = $data['themes'];
758 }
759 if (!empty($data['addons'])) {
760 $result['addons'] = $data['addons'];
761 }
762
763
764 return $result;
765 }
766
767 public function get_product_data_from_api($url)
768 {
769 $args = array(
770 'method' => 'GET',
771 );
772
773 $response = $this->request($url, $args, 'get_product_data');
774
775 if ($response == null || isset($response['error'])) {
776 null;
777 }
778
779 if (!empty($response['data'])) {
780 return $response['data'];
781 }
782
783 return array();
784 }
785
786 public static function get_products_objects($plugins_data = array(), $themes_data = array(), $addons_data = array())
787 {
788
789 if (!function_exists('get_plugins')) {
790 require_once ABSPATH . 'wp-admin/includes/plugin.php';
791 }
792
793 self::$plugins_state = array();
794 self::$addons_state = array();
795 self::$themes_state = array();
796 self::$auto_update_plugins = get_site_option('auto_update_plugins', array());
797
798 $site_installed_plugins = get_plugins();
799 //if domain hosted on 10Web
800 $mu_plugins = get_mu_plugins();
801 if (isset($mu_plugins['tenweb-init.php'])) {
802 $site_installed_plugins['10web-manager/10web-manager.php'] = $mu_plugins['tenweb-init.php'];
803 }
804
805 $site_installed_themes = wp_get_themes(array('errors' => null));
806
807 $plugins = self::get_plugins_objects($plugins_data, $site_installed_plugins);
808 $addons = self::get_plugins_objects($addons_data, $site_installed_plugins, true);
809 $themes = self::get_themes_objects($themes_data, $site_installed_themes);
810
811 $result = array(
812 'plugins' => $plugins,
813 'addons' => $addons,
814 'themes' => $themes
815 );
816 self::add_more_states($site_installed_plugins, $site_installed_themes);
817
818 return $result;
819 }
820
821
822 private static function add_more_states($site_installed_plugins, $site_installed_themes)
823 {
824
825 foreach ($site_installed_plugins as $file_name => $installed_plugin) {
826 $slug = explode('/', $file_name);
827 $slug = $slug[0];
828
829 $found = false;
830 foreach (self::$plugins_state as $state) {
831 if ($state->slug === $slug) {
832 $found = true;
833 break;
834 }
835 }
836
837
838 if ($found === false) {
839 foreach (self::$addons_state as $state) {
840 if ($state->slug === $slug) {
841 $found = true;
842 break;
843 }
844 }
845 }
846
847 if ($found === false) {
848 $state = new ProductState(
849 0,
850 $slug,
851 $installed_plugin['Title'],
852 $installed_plugin['Description'],
853 'plugin',
854 $installed_plugin['Version'],
855 1
856 );
857
858 $state->set_active($file_name);
859 $state->set_tenweb_product(false);
860 $state->set_other_wp_info($file_name, $installed_plugin, self::get_installed_plugins_wp_info());
861 $state->set_is_autoupdate_enabled(in_array($file_name, self::$auto_update_plugins, true));
862
863
864 self::$plugins_state[] = $state;
865 }
866
867 }
868
869 foreach ($site_installed_themes as $slug => $installed_theme) {
870
871 $found = false;
872 foreach (self::$themes_state as $state) {
873 if ($state->slug === $slug) {
874 $found = true;
875 break;
876 }
877 }
878
879 if ($found === false) {
880 $state = new ProductState(
881 0,
882 $slug,
883 $installed_theme['Name'],
884 $installed_theme->get('Description'),
885 'theme',
886 $installed_theme['Version'],
887 1
888 );
889
890 $state->set_active($slug);
891 $state->set_screenshot(self::get_theme_screenshot_url($slug));
892 $state->set_tenweb_product(false);
893 $state->set_other_wp_info($slug, $installed_theme, self::get_installed_themes_wp_info());
894
895 self::$themes_state[] = $state;
896 }
897
898 }
899 }
900
901 private static function get_themes_objects($themes_data, $site_installed_themes)
902 {
903
904
905 $themes = array();
906 $installed_themes = array();
907
908 foreach ($themes_data as $theme_data) {
909
910 if (isset($site_installed_themes[$theme_data['slug']])) {
911
912 $installed_theme = $site_installed_themes[$theme_data['slug']];
913
914 $state = new ProductState(
915 $theme_data['product_id'],
916 $theme_data['slug'],
917 $installed_theme['Name'],
918 $installed_theme->get('Description'),
919 'theme',
920 $installed_theme['Version'],
921 1
922 );
923
924 $state->set_active($theme_data['slug']);
925 $state->set_screenshot(self::get_theme_screenshot_url($theme_data['slug']));
926 $state->set_is_paid($theme_data['current_version']);
927 $state->set_other_wp_info($theme_data['slug'], $installed_theme, self::get_installed_themes_wp_info());
928
929 self::$themes_state[] = $state;
930
931 $theme = new InstalledTheme(
932 $state,
933 $theme_data['product_id'],
934 $theme_data['slug'],
935 $theme_data['title'],
936 $theme_data['description']
937 );
938
939 $theme->set_product_data($theme_data);
940 $installed_themes[] = $theme;
941
942 } else {
943
944 $theme = new Product(
945 $theme_data['product_id'],
946 $theme_data['slug'],
947 $theme_data['title'],
948 $theme_data['description'],
949 'theme'
950 );
951
952 $theme->set_product_data($theme_data);
953 $themes[] = $theme;
954
955 }
956
957 }
958
959 return array(
960 'installed_products' => $installed_themes,
961 'products' => $themes
962 );
963 }
964
965 private static function get_plugins_objects($plugins_data, $site_installed_plugins, $addons = false)
966 {
967
968 $plugins = array();
969 $installed_plugins = array();
970
971 $manager_exists = false;
972 $installed_plugins_info = self::get_installed_plugins_wp_info();
973 foreach ($plugins_data as $plugin_data) {
974
975 $plugin_slug = null;
976 foreach ($site_installed_plugins as $slug => $plugin) {
977 $slug_data = explode('/', $slug);
978 if ($slug_data[0] == $plugin_data['slug']) {
979 $plugin_slug = $slug;
980 break;
981 }
982 }
983
984 if ($plugin_slug != null) {
985 $installed_plugin = $site_installed_plugins[$plugin_slug];
986
987 if ($plugin_data['product_id'] == TENWEB_MANAGER_ID) {
988 $manager_exists = true;
989 }
990
991 $state = new ProductState(
992 $plugin_data['product_id'],
993 $plugin_data['slug'],
994 $installed_plugin['Title'],
995 $installed_plugin['Description'],
996 'plugin',
997 $installed_plugin['Version'],
998 1
999 );
1000
1001 $state->set_active($plugin_slug);
1002 $state->set_is_paid($plugin_data['current_version']);
1003 $state->set_other_wp_info($plugin_slug, $installed_plugin, $installed_plugins_info);
1004 $state->set_is_autoupdate_enabled(in_array($plugin_slug, self::$auto_update_plugins, true));
1005
1006
1007 if ($addons == true) {
1008 self::$addons_state[] = $state;
1009 } else {
1010 self::$plugins_state[] = $state;
1011 }
1012
1013
1014 $plugin = new InstalledPlugin(
1015 $state,
1016 $plugin_data['product_id'],
1017 $plugin_data['slug'],
1018 $plugin_data['title'],
1019 $plugin_data['description'],
1020 $plugin_slug
1021 );
1022
1023 $plugin->set_product_data($plugin_data);
1024 $installed_plugins[] = $plugin;
1025 } else {
1026
1027 $plugin = new Product(
1028 $plugin_data['product_id'],
1029 $plugin_data['slug'],
1030 $plugin_data['title'],
1031 $plugin_data['description']
1032 );
1033
1034 $plugin->set_product_data($plugin_data);
1035 $plugins[] = $plugin;
1036 }
1037
1038 }
1039
1040 if ($manager_exists == false && $addons == false && is_admin()) {
1041 $plugin = self::create_manager_plugin_object();
1042 $installed_plugins[] = $plugin;
1043 $notice = "Fail on connection with api. <a href='#' class='tenweb_clear_cache_button'>Try again</a>";
1044 self::add_notices($notice);
1045 }
1046
1047 return array(
1048 'installed_products' => $installed_plugins,
1049 'products' => $plugins
1050 );
1051 }
1052
1053 public static function get_theme_screenshot_url($slug)
1054 {
1055 $theme_folder = get_theme_root();
1056 $theme_folder .= '/' . $slug;
1057
1058 //file extensions https://codex.wordpress.org/Theme_Development#Screenshot
1059 $file_name = "";
1060 if (file_exists($theme_folder . '/screenshot.png')) {
1061 $file_name = 'screenshot.png';
1062 } else if (file_exists($theme_folder . '/screenshot.jpg')) {
1063 $file_name = 'screenshot.jpg';
1064 } else if (file_exists($theme_folder . '/screenshot.jpeg')) {
1065 $file_name = 'screenshot.jpeg';
1066 } else if (file_exists($theme_folder . '/screenshot.gif')) {
1067 $file_name = 'screenshot.gif';
1068 }
1069
1070 if (!empty($file_name)) {
1071 $file = get_theme_root_uri();
1072 $file .= '/' . $slug . '/' . $file_name;
1073
1074 return $file;
1075 } else {
1076 return "";
1077 }
1078
1079 }
1080
1081
1082 public static function get_expiration($key)
1083 {
1084 return (isset(self::$expiration[$key])) ? self::$expiration[$key] : null;
1085 }
1086
1087 public static function get_installed_plugins_wp_info()
1088 {
1089 if (self::$installed_plugins_wp_info === null) {
1090 // Trigger an async update check if needed
1091 $last_check = get_site_option('tenweb_last_update_request', 0);
1092 $check_interval = 24 * HOUR_IN_SECONDS;
1093
1094 if (time() - $last_check > $check_interval) {
1095 do_action('tenweb_force_updates_check');
1096 }
1097 self::$installed_plugins_wp_info = get_site_transient('update_plugins');
1098 self::filter_installed_plugins_wp_info();
1099 }
1100 return self::$installed_plugins_wp_info;
1101 }
1102
1103 public static function get_installed_themes_wp_info()
1104 {
1105 if (self::$installed_themes_wp_info === null) {
1106 self::$installed_themes_wp_info = get_site_transient('update_themes');
1107 self::filter_installed_themes_wp_info();
1108 }
1109 return self::$installed_themes_wp_info;
1110 }
1111
1112
1113 private static function filter_installed_plugins_wp_info()
1114 {
1115 $slugs = array(
1116 'js_composer/js_composer.php',
1117 'elementor-pro/elementor-pro.php',
1118 'wordpress-seo-premium/wp-seo-premium.php'
1119 );
1120
1121 foreach ($slugs as $slug) {
1122
1123 if (isset(self::$installed_plugins_wp_info->response[$slug])) {
1124 unset(self::$installed_plugins_wp_info->response[$slug]);
1125 }
1126
1127 if (isset(self::$installed_plugins_wp_info->no_update[$slug])) {
1128 unset(self::$installed_plugins_wp_info->no_update[$slug]);
1129 }
1130 }
1131 }
1132
1133 private static function filter_installed_themes_wp_info()
1134 {
1135 $slugs = array('divi');
1136
1137 foreach ($slugs as $slug) {
1138 if (isset(self::$installed_themes_wp_info->response[$slug])) {
1139 unset(self::$installed_themes_wp_info->response[$slug]);
1140 }
1141 }
1142
1143 }
1144
1145 public function get_amazon_tokens($product_id)
1146 {
1147 $url = TENWEB_API_URL . '/products/' . $product_id . '/request';
1148 $args = array(
1149 'method' => 'GET',
1150 );
1151
1152 $response = $this->request($url, $args, 'get_amazon_tokens');
1153 if ($response == null || isset($response['error'])) {
1154 return null;
1155 }
1156
1157 return $response;
1158 }
1159
1160 public static function send_state_before_deactivation()
1161 {
1162 if (is_multisite()) {
1163 $sites = get_sites();
1164 foreach ($sites as $site) {
1165 switch_to_blog($site->blog_id);
1166 self::send_state_before_deactivation_single();
1167 restore_current_blog();
1168 }
1169 } else {
1170 self::send_state_before_deactivation_single();
1171 }
1172 }
1173 private static function send_state_before_deactivation_single()
1174 {
1175 $self = self::get_instance();
1176 $self->set_products();
1177
1178 foreach (self::$plugins_state as $i => $state) {
1179 if ($state->product_id == TENWEB_MANAGER_ID) {
1180 $state->active = false;
1181 }
1182 }
1183
1184 $manager_info = self::get_manager_info();
1185 $result = self::send_site_state($manager_info);
1186 }
1187
1188 public static function get_manager_info()
1189 {
1190 return array(
1191 'site_info' => self::get_site_info(),
1192 'plugins_info' => array(
1193 "is_network" => ((is_multisite()) ? 1 : 0),
1194 "products" => self::states_to_array(self::$plugins_state)
1195 ),
1196 'themes_info' => array(
1197 "is_network" => ((is_multisite()) ? 1 : 0),
1198 "products" => self::states_to_array(self::$themes_state)
1199 ),
1200 'addons_info' => array(
1201 "is_network" => ((is_multisite()) ? 1 : 0),
1202 "products" => self::states_to_array(self::$addons_state)
1203 )
1204 );
1205 }
1206 public static function get_site_full_state()
1207 {
1208
1209 $plugins_state = array();
1210 $themes_state = array();
1211
1212 $plugins = get_plugins();
1213
1214 foreach ($plugins as $slug => $plugin) {
1215 $state = new ProductState($slug, $slug, $plugin['Title'], $plugin['Description'], 'plugin', $plugin['Version'], 1);
1216 $state->set_active($slug);
1217 $plugins_state[] = $state->get_wp_info();
1218 }
1219
1220 $themes = wp_get_themes(array('errors' => null));
1221 foreach ($themes as $slug => $theme) {
1222 $state = new ProductState($slug, $slug, $theme['Name'], $theme->get('Description'), 'theme', $theme['Version'], 1);
1223 $state->set_active($slug);
1224 $state->set_screenshot(self::get_theme_screenshot_url($slug));
1225 $themes_state[] = $state->get_wp_info();
1226 }
1227
1228 return array(
1229 'site_info' => self::get_site_info(),
1230 'plugins' => $plugins_state,
1231 'themes' => $themes_state
1232 );
1233
1234 }
1235
1236 private static function create_manager_plugin_object()
1237 {
1238 $plugin_slug = explode('/', TENWEB_SLUG);
1239 $plugin_slug = $plugin_slug[0];
1240
1241
1242 $state = new ProductState(
1243 TENWEB_MANAGER_ID,
1244 $plugin_slug,
1245 "10WEB Manager",
1246 "",
1247 'plugin',
1248 "0.0.0",
1249 1
1250 );
1251
1252 $state->active = true;
1253 $state->is_paid = false;
1254
1255 self::$plugins_state[] = $state;
1256
1257 $plugin = new InstalledPlugin(
1258 $state,
1259 TENWEB_MANAGER_ID,
1260 $plugin_slug,
1261 "10WEB Manager",
1262 "",
1263 TENWEB_SLUG
1264 );
1265
1266 return $plugin;
1267 }
1268 public static function get_products_state()
1269 {
1270 return array(
1271 'plugins' => self::$plugins_state,
1272 'addons' => self::$addons_state,
1273 'themes' => self::$themes_state
1274 );
1275 }
1276
1277
1278 public static function get_site_info_diff($screen)
1279 {
1280 $current_blog_id = null;
1281 if (is_multisite()) {
1282 $current_blog_id = $screen->id == 'settings-network' ? 'multisite' : get_current_blog_id();
1283 }
1284
1285 $site_info = self::get_site_info($current_blog_id);
1286 $site_current_state = md5(json_encode($site_info));
1287
1288 if ($current_blog_id == 'multisite') {
1289 $site_hash = get_site_option(TENWEB_PREFIX . '_site_state_hash');
1290 } else {
1291 $site_hash = get_option(TENWEB_PREFIX . '_site_state_hash');
1292 }
1293
1294 if ($site_hash !== $site_current_state) {
1295 return true;
1296 }
1297
1298 return false;
1299 }
1300
1301 public static function add_notices($notice_text, $error = true)
1302 {
1303 $container_class = "notice is-dismissible";
1304 if ($error) {
1305 $container_class .= " error";
1306 }
1307 if (!function_exists('get_current_screen')) {
1308 return false;
1309 }
1310
1311 $screen = get_current_screen();
1312 $notice = '<div class="' . $container_class . ' tenweb_manager_notice ' . ($screen !== null && $screen->parent_base == "tenweb_menu" ? "tenweb_menu_notice" : "") . '">'
1313 . '<p>' . $notice_text . '</p>'
1314 . '</div>';
1315 self::$notices[] = $notice;
1316 }
1317
1318
1319 public function check_single_token($token, $check_for_network = false, $is_login = false, $email = null)
1320 {
1321 if ($check_for_network) {
1322 $domain_id = self::$network_domain_id;
1323 } else {
1324 $domain_id = self::$domain_id;
1325 }
1326 $body = array('one_time_token' => $token);
1327
1328 if ($email) {
1329 $body['email'] = $email;
1330 }
1331
1332 if ($is_login) {
1333 $body['is_login'] = true;
1334 }
1335
1336 $args = array(
1337 'method' => 'POST',
1338 'body' => $body
1339 );
1340
1341 $url = TENWEB_API_URL . '/domains/' . $domain_id . '/check-single';
1342 $response = $this->request($url, $args, 'check_single_token');
1343
1344 if ($response == null || isset($response['error'])) {
1345 return false;
1346 }
1347
1348 return (!empty($response['status']) && $response['status'] == "ok");
1349 }
1350
1351 private static function filter_plugins_data($data, $additional_data=null)
1352 {
1353 $slugs = array(
1354 'tenweb-speed-optimizer',
1355 'image-optimizer-wd'
1356 );
1357 foreach($data as $k => $v) {
1358
1359 if ($k != "plugins_info" && $k != "site_info"){
1360 unset($data[$k]);
1361 } else if ($k == "plugins_info") {
1362 foreach ($data["plugins_info"] as $key => $value) {
1363
1364 if(!empty($value) and is_array($value)){
1365 foreach ($value as $k=>$v){
1366 if (!in_array($v["slug"], $slugs)){
1367 unset($data["plugins_info"][$key][$k]);
1368
1369 } else {
1370 if (!empty($additional_data) && isset($additional_data[$v["slug"]])){
1371 $data["plugins_info"][$key][$k]["active"] = $additional_data[$v["slug"]];
1372 }
1373 }
1374 }
1375 }
1376 }
1377 }
1378 }
1379 $data['plugins_info']['products'] = array_values($data['plugins_info']['products']);
1380 unset($data["themes_info"]);
1381 return $data;
1382 }
1383 }