PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / 1.4.1
Starter Sites & Templates by Neve v1.4.1
1.4.1 1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / vendor / codeinwp / themeisle-sdk / src / Product.php
templates-patterns-collection / vendor / codeinwp / themeisle-sdk / src Last commit date
Common 1 month ago Modules 6 days ago Loader.php 4 weeks ago Product.php 1 month ago
Product.php
498 lines
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 /**
143 * Action to be triggered when the product is first activated.
144 *
145 * @param string $basefile The basefile of the product.
146 */
147 do_action( 'themeisle_sdk_first_activation', $basefile );
148
149 update_option( $this->get_key() . '_install', time() );
150 }
151 $this->install = (int) $install;
152 self::$cached_products[ crc32( $basefile ) ] = $this;
153 $current_version = get_option( $this->slug . '_version', '' );
154
155 if ( $current_version !== $this->version && wp_cache_get( "{$this->slug}_version_upgrade" ) === false ) {
156 // Set the cache lock to avoid multiple calls.
157 wp_cache_set( "{$this->slug}_version_upgrade", true, HOUR_IN_SECONDS );
158 /**
159 * Action to be triggered when the product is updated.
160 *
161 * @param string $current_version The current version of the product.
162 * @param string $new_version The new version of the product.
163 * @param string $basefile The basefile of the product.
164 */
165 do_action( "themeisle_sdk_update_{$this->slug}", $current_version, $this->version, $basefile );
166
167 // Update the version of the product.
168 update_option( "{$this->slug}_version", $this->version );
169 // Delete the cache lock.
170 wp_cache_delete( "{$this->slug}_version_upgrade" );
171 }
172 }
173
174 /**
175 * Return a product.
176 *
177 * @param string $basefile Product basefile.
178 *
179 * @return Product Product Object.
180 */
181 public static function get( $basefile ) {
182 $key = crc32( $basefile );
183 if ( isset( self::$cached_products[ $key ] ) ) {
184 return self::$cached_products[ $key ];
185 }
186 self::$cached_products[ $key ] = new Product( $basefile );
187
188 return self::$cached_products[ $key ];
189 }
190
191 /**
192 * Setup props from path.
193 */
194 public function setup_from_path() {
195 $this->file = basename( $this->basefile );
196 $dir = dirname( $this->basefile );
197 $this->slug = basename( $dir );
198 $exts = explode( '.', $this->basefile );
199 $ext = $exts[ count( $exts ) - 1 ];
200 if ( 'css' === $ext ) {
201 $this->type = 'theme';
202 }
203 if ( 'php' === $ext ) {
204 $this->type = 'plugin';
205 }
206 $this->key = self::key_ready_name( $this->slug );
207 }
208
209 /**
210 * Normalize string.
211 *
212 * @param string $string the String to be normalized for cron handler.
213 *
214 * @return string $name The normalized string.
215 */
216 public static function key_ready_name( $string ) {
217 return str_replace( '-', '_', strtolower( trim( $string ) ) );
218 }
219
220 /**
221 * Setup props from fileheaders.
222 */
223 public function setup_from_fileheaders() {
224 $file_headers = array(
225 'Requires License' => 'Requires License',
226 'WordPress Available' => 'WordPress Available',
227 'Pro Slug' => 'Pro Slug',
228 'Version' => 'Version',
229 );
230 if ( 'plugin' === $this->type ) {
231 $file_headers['Name'] = 'Plugin Name';
232 $file_headers['AuthorName'] = 'Author';
233 $file_headers['AuthorURI'] = 'Author URI';
234 }
235 if ( 'theme' === $this->type ) {
236 $file_headers['Name'] = 'Theme Name';
237 $file_headers['AuthorName'] = 'Author';
238 $file_headers['AuthorURI'] = 'Author URI';
239 }
240 $file_headers = get_file_data( $this->basefile, $file_headers );
241
242 $this->name = $file_headers['Name'];
243 $this->store_name = $file_headers['AuthorName'];
244 $this->author_url = $file_headers['AuthorURI'];
245 $this->store_url = $file_headers['AuthorURI'];
246
247 $this->requires_license = ( 'yes' === $file_headers['Requires License'] ) ? true : false;
248 $this->wordpress_available = ( 'yes' === $file_headers['WordPress Available'] ) ? true : false;
249 $this->pro_slug = ! empty( $file_headers['Pro Slug'] ) ? $file_headers['Pro Slug'] : '';
250 $this->version = $file_headers['Version'];
251
252 }
253
254 /**
255 * Return the product key.
256 *
257 * @return string The product key.
258 */
259 public function get_key() {
260 return $this->key;
261 }
262
263 /**
264 * Check if the product is either theme or plugin.
265 *
266 * @return string Product type.
267 */
268 public function get_type() {
269 return $this->type;
270 }
271
272 /**
273 * Return if the product is used as a plugin.
274 *
275 * @return bool Is plugin?
276 */
277 public function is_plugin() {
278 return self::PLUGIN_TYPE === $this->type;
279 }
280
281 /**
282 * Return if the product is used as a theme.
283 *
284 * @return bool Is theme ?
285 */
286 public function is_theme() {
287 return self::THEME_TYPE === $this->type;
288 }
289
290 /**
291 * Returns the product slug.
292 *
293 * @return string The product slug.
294 */
295 public function get_slug() {
296 return $this->slug;
297 }
298
299 /**
300 * The magic var_dump info method.
301 *
302 * @return array Debug info.
303 */
304 public function __debugInfo() {
305 return array(
306 'name' => $this->name,
307 'slug' => $this->slug,
308 'version' => $this->version,
309 'basefile' => $this->basefile,
310 'key' => $this->key,
311 'type' => $this->type,
312 'store_name' => $this->store_name,
313 'store_url' => $this->store_url,
314 'wordpress_available' => $this->wordpress_available,
315 'requires_license' => $this->requires_license,
316 );
317
318 }
319
320 /**
321 * Getter for product version.
322 *
323 * @return string The product version.
324 */
325 public function get_version() {
326 return $this->version;
327 }
328
329 /**
330 * Returns current product license, if available.
331 *
332 * @return string Return license key, if available.
333 */
334 public function get_license() {
335
336 if ( ! $this->requires_license() && ! $this->is_wordpress_available() ) {
337 return 'free';
338 }
339 $license_data = get_option( $this->get_key() . '_license_data', '' );
340
341 if ( empty( $license_data ) ) {
342 return get_option( $this->get_key() . '_license', '' );
343 }
344 if ( ! isset( $license_data->key ) ) {
345 return get_option( $this->get_key() . '_license', '' );
346 }
347
348 return $license_data->key;
349 }
350
351 /**
352 * Either the product requires license or not.
353 *
354 * @return bool Either requires license or not.
355 */
356 public function requires_license() {
357 return $this->requires_license;
358 }
359
360 /**
361 * If product is available on wordpress.org or not.
362 *
363 * @return bool Either is wp available or not.
364 */
365 public function is_wordpress_available() {
366 return $this->wordpress_available;
367 }
368
369 /**
370 * Return friendly name.
371 *
372 * @return string Friendly name.
373 */
374 public function get_friendly_name() {
375 $name = trim( str_replace( 'Lite', '', $this->get_name() ) );
376 if ( defined( 'OTTER_BLOCKS_BASEFILE' ) && OTTER_BLOCKS_BASEFILE === $this->basefile ) {
377 $name = 'Otter Blocks';
378 }
379 if ( defined( 'OPTML_BASEFILE' ) && OPTML_BASEFILE === $this->basefile ) {
380 $name = 'Optimole';
381 }
382 if ( defined( 'WPMM_FILE' ) && WPMM_FILE === $this->basefile ) {
383 $name = 'LightStart';
384 }
385 $name = apply_filters( $this->get_key() . '_friendly_name', $name );
386 $name = rtrim( $name, '- ()' );
387
388 return $name;
389 }
390
391 /**
392 * Return the product version cache key.
393 *
394 * @return string The product version cache key.
395 */
396 public function get_cache_key() {
397 return $this->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->get_version() ) . 'versions';
398 }
399
400 /**
401 * Getter for product name.
402 *
403 * @return string The product name.
404 */
405 public function get_name() {
406 return $this->name;
407 }
408
409 /**
410 * Returns the Store name.
411 *
412 * @return string Store name.
413 */
414 public function get_store_name() {
415 return $this->store_name;
416 }
417
418 /**
419 * Returns the store url.
420 *
421 * @return string The store url.
422 */
423 public function get_store_url() {
424
425 if ( strpos( $this->store_url, '/themeisle.com' ) !== false ) {
426 return 'https://store.themeisle.com/';
427 }
428
429 return $this->store_url;
430 }
431
432 /**
433 * Returns product basefile, which holds the metaheaders.
434 *
435 * @return string The product basefile.
436 */
437 public function get_basefile() {
438 return $this->basefile;
439 }
440
441 /**
442 * Get changelog url.
443 *
444 * @return string Changelog url.
445 */
446 public function get_changelog() {
447 return add_query_arg(
448 [
449 'name' => rawurlencode( $this->get_name() ),
450 'edd_action' => 'view_changelog',
451 'locale' => get_user_locale(),
452 ],
453 $this->get_store_url()
454 );
455 }
456
457 /**
458 * Returns product filename.
459 *
460 * @return string The product filename.
461 */
462 public function get_file() {
463 return $this->file;
464 }
465
466 /**
467 * Returns the pro slug, if available.
468 *
469 * @return string The pro slug.
470 */
471 public function get_pro_slug() {
472 return $this->pro_slug;
473 }
474
475 /**
476 * Return the install timestamp.
477 *
478 * @return int The install timestamp.
479 */
480 public function get_install_time() {
481 return $this->install;
482 }
483
484 /**
485 * Returns the URL of the product base file.
486 *
487 * @param string $path The path to the file.
488 *
489 * @return string The URL of the product base file.
490 */
491 public function get_base_url( $path = '/' ) {
492 if ( $this->type ) {
493 return plugins_url( $path, $this->basefile );
494 }
495 }
496
497 }
498