PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.0.10
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.0.10
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
visualizer / vendor / codeinwp / themeisle-sdk / class-themeisle-sdk-product.php

class-themeisle-sdk-product.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.0.10, at vendor/codeinwp/themeisle-sdk/class-themeisle-sdk-product.php

636 lines 15.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The product model class for ThemeIsle SDK
4 *
5 * @package ThemeIsleSDK
6 * @subpackage Product
7 * @copyright Copyright (c) 2017, Marius Cristea
8 * @license http://opensource.org/licenses/gpl-3.0.php GNU Public License
9 * @since 1.0.0
10 */
11 // Exit if accessed directly.
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15 if ( ! class_exists( 'ThemeIsle_SDK_Product' ) ) :
16 /**
17 * Product model for ThemeIsle SDK.
18 */
19 class ThemeIsle_SDK_Product {
20 /**
21 * @var string $slug THe product slug.
22 */
23 private $slug;
24 /**
25 * @var string $basefile The file with headers.
26 */
27 private $basefile;
28 /**
29 * @var string $type The product type ( plugin | theme ).
30 */
31 private $type;
32 /**
33 * @var string $file The file name.
34 */
35 private $file;
36 /**
37 * @var string $name The product name.
38 */
39 private $name;
40 /**
41 * @var string $key The product ready key.
42 */
43 private $key;
44 /**
45 * @var string $author_url The url of the author.
46 */
47 private $author_url;
48 /**
49 * @var string $store_url The store url.
50 */
51 private $store_url;
52 /**
53 * @var int $install The date of install.
54 */
55 private $install;
56 /**
57 * @var string $store_name The store name.
58 */
59 private $store_name;
60 /**
61 * @var array $allowed_authors The allowed authors.
62 */
63 private $allowed_authors = array(
64 'proteusthemes.com',
65 'anarieldesign.com',
66 'prothemedesign.com',
67 'cssigniter.com',
68 );
69 /**
70 * @var array $allowed_external_products The allowed external_products.
71 */
72 private $allowed_products = array(
73 'zermatt',
74 'neto',
75 'olsen',
76 'benson',
77 'romero',
78 'carmack',
79 'puzzle',
80 'broadsheet',
81 'girlywp',
82 'veggie',
83 'zeko',
84 'maishawp',
85 'didi',
86 'liber',
87 'medicpress-pt',
88 'adrenaline-pt',
89 'consultpress-pt',
90 'legalpress-pt',
91 'gympress-pt',
92 'readable-pt',
93 'bolts-pt',
94 );
95 /**
96 * @var bool $requires_license Either user needs to activate it with license.
97 */
98 private $requires_license;
99 /**
100 * @var bool $wordpress_available Either is available on WordPress or not.
101 */
102 private $wordpress_available;
103 /**
104 * @var string $version The product version.
105 */
106 private $version;
107 /**
108 * @var string $logger_option Logger option key.
109 */
110 public $logger_option;
111 /**
112 * @var string $pro_slug Pro slug, if available.
113 */
114 public $pro_slug;
115 /**
116 * @var string $feedback_types All the feedback types the product supports
117 */
118 private $feedback_types = array();
119
120 /**
121 * @var string $widget_types All the widget types the product supports
122 */
123 private $widget_types = array( 'dashboard_blog' );
124
125 /**
126 * ThemeIsle_SDK_Product constructor.
127 *
128 * @param string $basefile Product basefile.
129 */
130 public function __construct( $basefile ) {
131 if ( ! empty( $basefile ) ) {
132 if ( is_readable( $basefile ) ) {
133 $this->basefile = $basefile;
134 $this->setup_from_path();
135 $this->setup_from_fileheaders();
136 }
137 }
138 $install = get_option( $this->get_key() . '_install', 0 );
139 if ( $install === 0 ) {
140 $install = time();
141 update_option( $this->get_key() . '_install', time() );
142 }
143 $this->install = $install;
144
145 $this->logger_option = $this->get_key() . '_logger_flag';
146
147 }
148
149 /**
150 * Setup props from fileheaders.
151 */
152 public function setup_from_fileheaders() {
153 $file_headers = array(
154 'Requires License' => 'Requires License',
155 'WordPress Available' => 'WordPress Available',
156 'Pro Slug' => 'Pro Slug',
157 'Version' => 'Version',
158 );
159 if ( $this->type == 'plugin' ) {
160 $file_headers['Name'] = 'Plugin Name';
161 $file_headers['AuthorName'] = 'Author';
162 $file_headers['AuthorURI'] = 'Author URI';
163 }
164 if ( $this->type == 'theme' ) {
165 $file_headers['Name'] = 'Theme Name';
166 $file_headers['AuthorName'] = 'Author';
167 $file_headers['AuthorURI'] = 'Author URI';
168 }
169 $file_headers = get_file_data( $this->basefile, $file_headers );
170
171 $this->name = $file_headers['Name'];
172 $this->store_name = $file_headers['AuthorName'];
173 $this->author_url = $file_headers['AuthorURI'];
174 $this->store_url = $file_headers['AuthorURI'];
175 if ( $this->is_external_author() ) {
176 $this->store_url = 'https://themeisle.com';
177 $this->store_name = 'ThemeIsle';
178 }
179 $this->requires_license = ( $file_headers['Requires License'] == 'yes' ) ? true : false;
180 $this->wordpress_available = ( $file_headers['WordPress Available'] == 'yes' ) ? true : false;
181 $this->pro_slug = ! empty( $file_headers['Pro Slug'] ) ? $file_headers['Pro Slug'] : '';
182 $this->version = $file_headers['Version'];
183 if ( $this->require_uninstall_feedback() ) {
184 $this->feedback_types[] = 'deactivate';
185 }
186 if ( $this->is_wordpress_available() ) {
187 $this->feedback_types[] = 'review';
188 $this->feedback_types[] = 'translate';
189 }
190 }
191
192 /**
193 * Check if the product is by external author or not.
194 *
195 * @return bool Either is external author or no.
196 */
197 public function is_external_author() {
198 foreach ( $this->allowed_authors as $author ) {
199 if ( strpos( $this->author_url, $author ) !== false ) {
200 return true;
201 }
202 if ( in_array( $this->get_slug(), $this->allowed_products ) ) {
203 return true;
204 }
205 }
206
207 return false;
208 }
209
210 /**
211 * The magic var_dump info method.
212 *
213 * @return array Debug info.
214 */
215 public function __debugInfo() {
216 return array(
217 'name' => $this->name,
218 'slug' => $this->slug,
219 'version' => $this->version,
220 'basefile' => $this->basefile,
221 'key' => $this->key,
222 'type' => $this->type,
223 'store_name' => $this->store_name,
224 'store_url' => $this->store_url,
225 'wordpress_available' => $this->wordpress_available,
226 'requires_license' => $this->requires_license,
227 );
228
229 }
230
231 /**
232 * Setup props from path.
233 */
234 public function setup_from_path() {
235 $this->file = basename( $this->basefile );
236 $dir = dirname( $this->basefile );
237 $this->slug = basename( $dir );
238 $exts = explode( '.', $this->basefile );
239 $ext = $exts[ count( $exts ) - 1 ];
240 if ( $ext == 'css' ) {
241 $this->type = 'theme';
242 }
243 if ( $ext == 'php' ) {
244 $this->type = 'plugin';
245 }
246 $this->key = self::key_ready_name( $this->slug );
247 }
248
249 /**
250 * @param string $string the String to be normalized for cron handler.
251 *
252 * @return string $name The normalized string.
253 */
254 static function key_ready_name( $string ) {
255 return str_replace( '-', '_', strtolower( trim( $string ) ) );
256 }
257
258 /**
259 * Getter for product name.
260 *
261 * @return string The product name.
262 */
263 public function get_name() {
264 return $this->name;
265 }
266
267 /**
268 * Getter for product version.
269 *
270 * @return string The product version.
271 */
272 public function get_version() {
273 return $this->version;
274 }
275
276 /**
277 * If product is available on wordpress.org or not.
278 *
279 * @return bool Either is wp available or not.
280 */
281 public function is_wordpress_available() {
282 return $this->wordpress_available;
283 }
284
285 /**
286 * @return array Array of available versions.
287 */
288 private function get_plugin_versions() {
289
290 $url = sprintf( 'https://api.wordpress.org/plugins/info/1.0/%s', $this->get_slug() );
291 $response = wp_remote_get( $url );
292 if ( is_wp_error( $response ) ) {
293 return array();
294 }
295 $response = wp_remote_retrieve_body( $response );
296 $response = maybe_unserialize( $response );
297
298 if ( ! is_object( $response ) ) {
299 return array();
300 }
301 if ( ! isset( $response->versions ) ) {
302 return array();
303 }
304 $versions = array();
305 foreach ( $response->versions as $version => $zip ) {
306 $versions[] = array(
307 'version' => $version,
308 'url' => $zip,
309 );
310 }
311
312 return $versions;
313 }
314
315 /**
316 * @return string Return license key, if available.
317 */
318 private function get_license() {
319 $license_data = get_option( $this->get_key() . '_license_data', '' );
320
321 if ( empty( $license_data ) ) {
322 return '';
323 }
324 if ( ! isset( $license_data->key ) ) {
325 return '';
326 }
327
328 return $license_data->key;
329 }
330
331 /**
332 * @return array Array of available versions.
333 */
334 private function get_pro_versions() {
335 $license = $this->get_license();
336 $store_url = trailingslashit( $this->store_url );
337 $url = sprintf( '%s?edd_action=get_versions&name=%s&url=%s&license=%s', $store_url, urlencode( $this->get_name() ), urlencode( get_site_url() ), $license );
338 $response = wp_remote_get( $url );
339 if ( is_wp_error( $response ) ) {
340 return array();
341 }
342 $response = wp_remote_retrieve_body( $response );
343 $response = json_decode( $response );
344 if ( ! is_object( $response ) ) {
345 return array();
346 }
347 if ( ! isset( $response->versions ) ) {
348 return array();
349 }
350 $versions = array();
351 foreach ( $response->versions as $key => $version ) {
352 $versions[] = array(
353 'version' => $version->version,
354 'url' => $version->file,
355 );
356 }
357
358 return $versions;
359 }
360
361 /**
362 * Return theme versions.
363 *
364 * @return array Theme versions array.
365 */
366 public function get_theme_versions() {
367 $url = sprintf( 'https://api.wordpress.org/themes/info/1.1/?action=theme_information&request[slug]=%s&request[fields][versions]=true', $this->get_slug() );
368 $response = wp_remote_get( $url );
369 if ( is_wp_error( $response ) ) {
370 return array();
371 }
372 $response = wp_remote_retrieve_body( $response );
373 $response = json_decode( $response );
374
375 if ( ! is_object( $response ) ) {
376 return array();
377 }
378 if ( ! isset( $response->versions ) ) {
379 return array();
380 }
381 $versions = array();
382 foreach ( $response->versions as $version => $zip ) {
383 $versions[] = array(
384 'version' => $version,
385 'url' => $zip,
386 );
387 }
388
389 return $versions;
390 }
391
392 /**
393 * Get versions array from wp.org
394 *
395 * @return array Array of versions.
396 */
397 private function get_api_versions() {
398
399 $cache_key = $this->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->version ) . 'versions';
400 $cache_versions = get_transient( $this->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->version ) . 'versions' );
401 if ( false == $cache_versions ) {
402 $versions = array();
403 if ( ! $this->is_wordpress_available() ) {
404 $versions = $this->get_pro_versions();
405 } else {
406 if ( $this->get_type() === 'plugin' ) {
407 $versions = $this->get_plugin_versions();
408 }
409 if ( $this->get_type() === 'theme' ) {
410 $versions = $this->get_theme_versions();
411 }
412 }
413 set_transient( $cache_key, $versions, MONTH_IN_SECONDS );
414 } else {
415 $versions = is_array( $cache_versions ) ? $cache_versions : array();
416 }
417
418 return $versions;
419 }
420
421 /**
422 * Get the last rollback for this product.
423 *
424 * @return array The rollback version.
425 */
426 public function get_rollback() {
427 $rollback = array();
428 $versions = $this->get_api_versions();
429 $versions = apply_filters( $this->get_key() . '_rollbacks', $versions );
430
431 if ( $versions ) {
432 usort( $versions, array( $this, 'sort_rollback_array' ) );
433 foreach ( $versions as $version ) {
434 if ( isset( $version['version'] ) && isset( $version['url'] ) && version_compare( $this->version, $version['version'], '>' ) ) {
435 $rollback = $version;
436 break;
437 }
438 }
439 }
440
441 return $rollback;
442 }
443
444 /**
445 * Sort the rollbacks array in descending order.
446 */
447 public function sort_rollback_array( $a, $b ) {
448 return version_compare( $a['version'], $b['version'], '<' ) > 0;
449 }
450
451 /**
452 * If product can be rolled back.
453 *
454 * @return bool Can the product be rolled back or not.
455 */
456 public function can_rollback() {
457 if ( $this->get_type() === 'theme' ) {
458 if ( ! current_user_can( 'switch_themes' ) ) {
459 return false;
460 }
461 }
462 if ( $this->get_type() === 'plugin' ) {
463 if ( ! current_user_can( 'install_plugins' ) ) {
464 return false;
465 }
466 }
467 $rollback = $this->get_rollback();
468
469 return ! empty( $rollback );
470 }
471
472 /**
473 * Return the product key.
474 *
475 * @return string The product key.
476 */
477 public function get_key() {
478 return $this->key;
479 }
480
481 /**
482 * Return friendly name.
483 *
484 * @return string Friendly name.
485 */
486 public function get_friendly_name() {
487 $name = apply_filters( $this->get_key() . '_friendly_name', trim( str_replace( 'Lite', '', $this->get_name() ) ) );
488 $name = rtrim( $name, '- ' );
489
490 return $name;
491 }
492
493 /**
494 * Either the product requires license or not.
495 *
496 * @return bool Either requires license or not.
497 */
498 public function requires_license() {
499 return $this->requires_license;
500 }
501
502 /**
503 * Check if the product is either theme or plugin.
504 *
505 * @return string Product type.
506 */
507 public function get_type() {
508 return $this->type;
509 }
510
511 /**
512 * Returns the Store name.
513 *
514 * @return string Store name.
515 */
516 public function get_store_name() {
517 return $this->store_name;
518 }
519
520 /**
521 * Returns the store url.
522 *
523 * @return string The store url.
524 */
525 public function get_store_url() {
526 return $this->store_url;
527 }
528
529 /**
530 * Returns the product slug.
531 *
532 * @return string The product slug.
533 */
534 public function get_slug() {
535 return $this->slug;
536 }
537
538 /**
539 * Returns product basefile, which holds the metaheaders.
540 *
541 * @return string The product basefile.
542 */
543 public function get_basefile() {
544 return $this->basefile;
545 }
546
547 /**
548 * Returns product filename.
549 *
550 * @return string The product filename.
551 */
552 public function get_file() {
553 return $this->file;
554 }
555
556 /**
557 * Returns feedback types
558 *
559 * @return array The feedback types.
560 */
561 public function get_feedback_types() {
562 return apply_filters( $this->get_key() . '_feedback_types', $this->feedback_types );
563 }
564
565 /**
566 * Returns widget types
567 *
568 * @return array The widget types.
569 */
570 public function get_widget_types() {
571 return apply_filters( $this->get_key() . '_widget_types', $this->widget_types );
572 }
573
574 /**
575 * We log the user website and product version.
576 *
577 * @return bool Either we log the data or not.
578 */
579 public function is_logger_active() {
580 // If is not available on WordPress log this automatically.
581 if ( ! $this->is_wordpress_available() ) {
582 return true;
583 } else {
584 $pro_slug = $this->get_pro_slug();
585 if ( ! empty( $pro_slug ) ) {
586
587 $all_products = ThemeIsle_SDK_Loader::get_products();
588 if ( isset( $all_products[ $pro_slug ] ) ) {
589 return true;
590 }
591 }
592
593 return ( get_option( $this->get_key() . '_logger_flag', 'no' ) === 'yes' );
594
595 }
596 }
597
598 /**
599 * Returns the pro slug, if available.
600 *
601 * @return string The pro slug.
602 */
603 public function get_pro_slug() {
604 return $this->pro_slug;
605 }
606
607 /**
608 * Return the install timestamp.
609 *
610 * @return int The install timestamp.
611 */
612 public function get_install_time() {
613 return $this->install;
614 }
615
616 /**
617 * We require feedback on uninstall.
618 *
619 * @return bool Either we should require feedback on uninstall or not.
620 */
621 public function require_uninstall_feedback() {
622 if ( $this->get_type() == 'theme' && ! $this->is_external_author() ) {
623 return ! get_transient( 'ti_sdk_pause_' . $this->get_key(), false );
624 }
625
626 if ( $this->get_type() == 'plugin' ) {
627
628 return true;
629 }
630
631 return false;
632 }
633
634 }
635 endif;
636