PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.9.4
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.9.4
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.9.4, at vendor/codeinwp/themeisle-sdk/src/Product.php

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