PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.10.8
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.10.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 3.10.4 All 148 releases
visualizer / vendor / codeinwp / themeisle-sdk / src / Product.php

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

461 lines 9.9 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 = apply_filters( $this->get_key() . '_friendly_name', trim( str_replace( 'Lite', '', $this->get_name() ) ) );
350 $name = rtrim( $name, '- ()' );
351
352 return $name;
353 }
354
355 /**
356 * Return the product version cache key.
357 *
358 * @return string The product version cache key.
359 */
360 public function get_cache_key() {
361 return $this->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->get_version() ) . 'versions';
362 }
363
364 /**
365 * Getter for product name.
366 *
367 * @return string The product name.
368 */
369 public function get_name() {
370 return $this->name;
371 }
372
373 /**
374 * Returns the Store name.
375 *
376 * @return string Store name.
377 */
378 public function get_store_name() {
379 return $this->store_name;
380 }
381
382 /**
383 * Returns the store url.
384 *
385 * @return string The store url.
386 */
387 public function get_store_url() {
388
389 if ( strpos( $this->store_url, '/themeisle.com' ) !== false ) {
390 return 'https://store.themeisle.com/';
391 }
392
393 return $this->store_url;
394 }
395
396 /**
397 * Returns product basefile, which holds the metaheaders.
398 *
399 * @return string The product basefile.
400 */
401 public function get_basefile() {
402 return $this->basefile;
403 }
404
405 /**
406 * Get changelog url.
407 *
408 * @return string Changelog url.
409 */
410 public function get_changelog() {
411 return add_query_arg(
412 [
413 'name' => rawurlencode( $this->get_name() ),
414 'edd_action' => 'view_changelog',
415 ],
416 $this->get_store_url()
417 );
418 }
419
420 /**
421 * Returns product filename.
422 *
423 * @return string The product filename.
424 */
425 public function get_file() {
426 return $this->file;
427 }
428
429 /**
430 * Returns the pro slug, if available.
431 *
432 * @return string The pro slug.
433 */
434 public function get_pro_slug() {
435 return $this->pro_slug;
436 }
437
438 /**
439 * Return the install timestamp.
440 *
441 * @return int The install timestamp.
442 */
443 public function get_install_time() {
444 return $this->install;
445 }
446
447 /**
448 * Returns the URL of the product base file.
449 *
450 * @param string $path The path to the file.
451 *
452 * @return string The URL of the product base file.
453 */
454 public function get_base_url( $path = '/' ) {
455 if ( $this->type ) {
456 return plugins_url( $path, $this->basefile );
457 }
458 }
459
460 }
461