PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.11.10
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.11.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 / src / Product.php

Product.php in Visualizer – Tables & Charts Manager with Built-in AI Generator 3.11.10, at vendor/codeinwp/themeisle-sdk/src/Product.php

472 lines 10.3 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
12 namespace ThemeisleSDK;
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Product model for ThemeIsle SDK.
21 */
22 class Product {
23 /**
24 * Define plugin type string.
25 */
26 const PLUGIN_TYPE = 'plugin';
27 /**
28 * Define theme type string.
29 */
30 const THEME_TYPE = 'theme';
31 /**
32 * If the product has a pro version, contains the pro slug.
33 *
34 * @var string $pro_slug Pro slug, if available.
35 */
36 public $pro_slug;
37 /**
38 * Current product slug.
39 *
40 * @var string $slug THe product slug.
41 */
42 private $slug;
43 /**
44 * Product basefile, with the proper metadata.
45 *
46 * @var string $basefile The file with headers.
47 */
48 private $basefile;
49 /**
50 * Type of the product.
51 *
52 * @var string $type The product type ( plugin | theme ).
53 */
54 private $type;
55 /**
56 * The file name.
57 *
58 * @var string $file The file name.
59 */
60 private $file;
61 /**
62 * Product name, fetched from the file headers.
63 *
64 * @var string $name The product name.
65 */
66 private $name;
67 /**
68 * Product normalized key.
69 *
70 * @var string $key The product ready key.
71 */
72 private $key;
73 /**
74 * Author URL
75 *
76 * @var string $author_url The author url.
77 */
78 private $author_url;
79 /**
80 * Product store url.
81 *
82 * @var string $store_url The store url.
83 */
84 private $store_url;
85 /**
86 * Product install timestamp.
87 *
88 * @var int $install The date of install.
89 */
90 private $install;
91 /**
92 * Product store/author name.
93 *
94 * @var string $store_name The store name.
95 */
96 private $store_name;
97 /**
98 * Does the product requires license.
99 *
100 * @var bool $requires_license Either user needs to activate it with license.
101 */
102 private $requires_license;
103 /**
104 * Is the product available on wordpress.org
105 *
106 * @var bool $wordpress_available Either is available on WordPress or not.
107 */
108 private $wordpress_available;
109 /**
110 * Current version of the product.
111 *
112 * @var string $version The product version.
113 */
114 private $version;
115 /**
116 * Holds a map of loaded products objects.
117 *
118 * @var array Array of loaded products.
119 */
120 private static $cached_products = [];
121 /**
122 * Root api endpoint.
123 */
124 const API_URL = 'https://api.themeisle.com/';
125
126 /**
127 * ThemeIsle_SDK_Product constructor.
128 *
129 * @param string $basefile Product basefile.
130 */
131 public function __construct( $basefile ) {
132 if ( ! empty( $basefile ) ) {
133 if ( is_file( $basefile ) ) {
134 $this->basefile = $basefile;
135 $this->setup_from_path();
136 $this->setup_from_fileheaders();
137 }
138 }
139 $install = get_option( $this->get_key() . '_install', 0 );
140 if ( 0 === $install ) {
141 $install = time();
142 update_option( $this->get_key() . '_install', time() );
143 }
144 $this->install = $install;
145 self::$cached_products[ crc32( $basefile ) ] = $this;
146 }
147
148 /**
149 * Return a product.
150 *
151 * @param string $basefile Product basefile.
152 *
153 * @return Product Product Object.
154 */
155 public static function get( $basefile ) {
156 $key = crc32( $basefile );
157 if ( isset( self::$cached_products[ $key ] ) ) {
158 return self::$cached_products[ $key ];
159 }
160 self::$cached_products[ $key ] = new Product( $basefile );
161
162 return self::$cached_products[ $key ];
163 }
164
165 /**
166 * Setup props from path.
167 */
168 public function setup_from_path() {
169 $this->file = basename( $this->basefile );
170 $dir = dirname( $this->basefile );
171 $this->slug = basename( $dir );
172 $exts = explode( '.', $this->basefile );
173 $ext = $exts[ count( $exts ) - 1 ];
174 if ( 'css' === $ext ) {
175 $this->type = 'theme';
176 }
177 if ( 'php' === $ext ) {
178 $this->type = 'plugin';
179 }
180 $this->key = self::key_ready_name( $this->slug );
181 }
182
183 /**
184 * Normalize string.
185 *
186 * @param string $string the String to be normalized for cron handler.
187 *
188 * @return string $name The normalized string.
189 */
190 public static function key_ready_name( $string ) {
191 return str_replace( '-', '_', strtolower( trim( $string ) ) );
192 }
193
194 /**
195 * Setup props from fileheaders.
196 */
197 public function setup_from_fileheaders() {
198 $file_headers = array(
199 'Requires License' => 'Requires License',
200 'WordPress Available' => 'WordPress Available',
201 'Pro Slug' => 'Pro Slug',
202 'Version' => 'Version',
203 );
204 if ( 'plugin' === $this->type ) {
205 $file_headers['Name'] = 'Plugin Name';
206 $file_headers['AuthorName'] = 'Author';
207 $file_headers['AuthorURI'] = 'Author URI';
208 }
209 if ( 'theme' === $this->type ) {
210 $file_headers['Name'] = 'Theme Name';
211 $file_headers['AuthorName'] = 'Author';
212 $file_headers['AuthorURI'] = 'Author URI';
213 }
214 $file_headers = get_file_data( $this->basefile, $file_headers );
215
216 $this->name = $file_headers['Name'];
217 $this->store_name = $file_headers['AuthorName'];
218 $this->author_url = $file_headers['AuthorURI'];
219 $this->store_url = $file_headers['AuthorURI'];
220
221 $this->requires_license = ( 'yes' === $file_headers['Requires License'] ) ? true : false;
222 $this->wordpress_available = ( 'yes' === $file_headers['WordPress Available'] ) ? true : false;
223 $this->pro_slug = ! empty( $file_headers['Pro Slug'] ) ? $file_headers['Pro Slug'] : '';
224 $this->version = $file_headers['Version'];
225
226 }
227
228 /**
229 * Return the product key.
230 *
231 * @return string The product key.
232 */
233 public function get_key() {
234 return $this->key;
235 }
236
237 /**
238 * Check if the product is either theme or plugin.
239 *
240 * @return string Product type.
241 */
242 public function get_type() {
243 return $this->type;
244 }
245
246 /**
247 * Return if the product is used as a plugin.
248 *
249 * @return bool Is plugin?
250 */
251 public function is_plugin() {
252 return self::PLUGIN_TYPE === $this->type;
253 }
254
255 /**
256 * Return if the product is used as a theme.
257 *
258 * @return bool Is theme ?
259 */
260 public function is_theme() {
261 return self::THEME_TYPE === $this->type;
262 }
263
264 /**
265 * Returns the product slug.
266 *
267 * @return string The product slug.
268 */
269 public function get_slug() {
270 return $this->slug;
271 }
272
273 /**
274 * The magic var_dump info method.
275 *
276 * @return array Debug info.
277 */
278 public function __debugInfo() {
279 return array(
280 'name' => $this->name,
281 'slug' => $this->slug,
282 'version' => $this->version,
283 'basefile' => $this->basefile,
284 'key' => $this->key,
285 'type' => $this->type,
286 'store_name' => $this->store_name,
287 'store_url' => $this->store_url,
288 'wordpress_available' => $this->wordpress_available,
289 'requires_license' => $this->requires_license,
290 );
291
292 }
293
294 /**
295 * Getter for product version.
296 *
297 * @return string The product version.
298 */
299 public function get_version() {
300 return $this->version;
301 }
302
303 /**
304 * Returns current product license, if available.
305 *
306 * @return string Return license key, if available.
307 */
308 public function get_license() {
309
310 if ( ! $this->requires_license() && ! $this->is_wordpress_available() ) {
311 return 'free';
312 }
313 $license_data = get_option( $this->get_key() . '_license_data', '' );
314
315 if ( empty( $license_data ) ) {
316 return get_option( $this->get_key() . '_license', '' );
317 }
318 if ( ! isset( $license_data->key ) ) {
319 return get_option( $this->get_key() . '_license', '' );
320 }
321
322 return $license_data->key;
323 }
324
325 /**
326 * Either the product requires license or not.
327 *
328 * @return bool Either requires license or not.
329 */
330 public function requires_license() {
331 return $this->requires_license;
332 }
333
334 /**
335 * If product is available on wordpress.org or not.
336 *
337 * @return bool Either is wp available or not.
338 */
339 public function is_wordpress_available() {
340 return $this->wordpress_available;
341 }
342
343 /**
344 * Return friendly name.
345 *
346 * @return string Friendly name.
347 */
348 public function get_friendly_name() {
349 $name = trim( str_replace( 'Lite', '', $this->get_name() ) );
350 if ( defined( 'OTTER_BLOCKS_BASEFILE' ) && OTTER_BLOCKS_BASEFILE === $this->basefile ) {
351 $name = 'Otter Blocks';
352 }
353 if ( defined( 'OPTML_BASEFILE' ) && OPTML_BASEFILE === $this->basefile ) {
354 $name = 'Optimole';
355 }
356 if ( defined( 'WPMM_FILE' ) && WPMM_FILE === $this->basefile ) {
357 $name = 'LightStart';
358 }
359 $name = apply_filters( $this->get_key() . '_friendly_name', $name );
360 $name = rtrim( $name, '- ()' );
361
362 return $name;
363 }
364
365 /**
366 * Return the product version cache key.
367 *
368 * @return string The product version cache key.
369 */
370 public function get_cache_key() {
371 return $this->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->get_version() ) . 'versions';
372 }
373
374 /**
375 * Getter for product name.
376 *
377 * @return string The product name.
378 */
379 public function get_name() {
380 return $this->name;
381 }
382
383 /**
384 * Returns the Store name.
385 *
386 * @return string Store name.
387 */
388 public function get_store_name() {
389 return $this->store_name;
390 }
391
392 /**
393 * Returns the store url.
394 *
395 * @return string The store url.
396 */
397 public function get_store_url() {
398
399 if ( strpos( $this->store_url, '/themeisle.com' ) !== false ) {
400 return 'https://store.themeisle.com/';
401 }
402
403 return $this->store_url;
404 }
405
406 /**
407 * Returns product basefile, which holds the metaheaders.
408 *
409 * @return string The product basefile.
410 */
411 public function get_basefile() {
412 return $this->basefile;
413 }
414
415 /**
416 * Get changelog url.
417 *
418 * @return string Changelog url.
419 */
420 public function get_changelog() {
421 return add_query_arg(
422 [
423 'name' => rawurlencode( $this->get_name() ),
424 'edd_action' => 'view_changelog',
425 'locale' => get_user_locale(),
426 ],
427 $this->get_store_url()
428 );
429 }
430
431 /**
432 * Returns product filename.
433 *
434 * @return string The product filename.
435 */
436 public function get_file() {
437 return $this->file;
438 }
439
440 /**
441 * Returns the pro slug, if available.
442 *
443 * @return string The pro slug.
444 */
445 public function get_pro_slug() {
446 return $this->pro_slug;
447 }
448
449 /**
450 * Return the install timestamp.
451 *
452 * @return int The install timestamp.
453 */
454 public function get_install_time() {
455 return $this->install;
456 }
457
458 /**
459 * Returns the URL of the product base file.
460 *
461 * @param string $path The path to the file.
462 *
463 * @return string The URL of the product base file.
464 */
465 public function get_base_url( $path = '/' ) {
466 if ( $this->type ) {
467 return plugins_url( $path, $this->basefile );
468 }
469 }
470
471 }
472