PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / 2.0.6
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF v2.0.6
2.0.7 2.0.6 2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / vendor / codeinwp / themeisle-sdk / src / Product.php
robin-image-optimizer / vendor / codeinwp / themeisle-sdk / src Last commit date
Common 1 month ago Modules 1 month ago Loader.php 1 month ago Product.php 1 month ago
Product.php
495 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_add( "{$this->slug}_version_upgrade", true, '', HOUR_IN_SECONDS ) ) {
156 /**
157 * Action to be triggered when the product is updated.
158 *
159 * @param string $current_version The current version of the product.
160 * @param string $new_version The new version of the product.
161 * @param string $basefile The basefile of the product.
162 */
163 do_action( "themeisle_sdk_update_{$this->slug}", $current_version, $this->version, $basefile );
164
165 // Update the version of the product.
166 update_option( "{$this->slug}_version", $this->version );
167 wp_cache_delete( "{$this->slug}_version_upgrade" );
168 }
169 }
170
171 /**
172 * Return a product.
173 *
174 * @param string $basefile Product basefile.
175 *
176 * @return Product Product Object.
177 */
178 public static function get( $basefile ) {
179 $key = crc32( $basefile );
180 if ( isset( self::$cached_products[ $key ] ) ) {
181 return self::$cached_products[ $key ];
182 }
183 self::$cached_products[ $key ] = new Product( $basefile );
184
185 return self::$cached_products[ $key ];
186 }
187
188 /**
189 * Setup props from path.
190 */
191 public function setup_from_path() {
192 $this->file = basename( $this->basefile );
193 $dir = dirname( $this->basefile );
194 $this->slug = basename( $dir );
195 $exts = explode( '.', $this->basefile );
196 $ext = $exts[ count( $exts ) - 1 ];
197 if ( 'css' === $ext ) {
198 $this->type = 'theme';
199 }
200 if ( 'php' === $ext ) {
201 $this->type = 'plugin';
202 }
203 $this->key = self::key_ready_name( $this->slug );
204 }
205
206 /**
207 * Normalize string.
208 *
209 * @param string $string the String to be normalized for cron handler.
210 *
211 * @return string $name The normalized string.
212 */
213 public static function key_ready_name( $string ) {
214 return str_replace( '-', '_', strtolower( trim( $string ) ) );
215 }
216
217 /**
218 * Setup props from fileheaders.
219 */
220 public function setup_from_fileheaders() {
221 $file_headers = array(
222 'Requires License' => 'Requires License',
223 'WordPress Available' => 'WordPress Available',
224 'Pro Slug' => 'Pro Slug',
225 'Version' => 'Version',
226 );
227 if ( 'plugin' === $this->type ) {
228 $file_headers['Name'] = 'Plugin Name';
229 $file_headers['AuthorName'] = 'Author';
230 $file_headers['AuthorURI'] = 'Author URI';
231 }
232 if ( 'theme' === $this->type ) {
233 $file_headers['Name'] = 'Theme Name';
234 $file_headers['AuthorName'] = 'Author';
235 $file_headers['AuthorURI'] = 'Author URI';
236 }
237 $file_headers = get_file_data( $this->basefile, $file_headers );
238
239 $this->name = $file_headers['Name'];
240 $this->store_name = $file_headers['AuthorName'];
241 $this->author_url = $file_headers['AuthorURI'];
242 $this->store_url = $file_headers['AuthorURI'];
243
244 $this->requires_license = ( 'yes' === $file_headers['Requires License'] ) ? true : false;
245 $this->wordpress_available = ( 'yes' === $file_headers['WordPress Available'] ) ? true : false;
246 $this->pro_slug = ! empty( $file_headers['Pro Slug'] ) ? $file_headers['Pro Slug'] : '';
247 $this->version = $file_headers['Version'];
248
249 }
250
251 /**
252 * Return the product key.
253 *
254 * @return string The product key.
255 */
256 public function get_key() {
257 return $this->key;
258 }
259
260 /**
261 * Check if the product is either theme or plugin.
262 *
263 * @return string Product type.
264 */
265 public function get_type() {
266 return $this->type;
267 }
268
269 /**
270 * Return if the product is used as a plugin.
271 *
272 * @return bool Is plugin?
273 */
274 public function is_plugin() {
275 return self::PLUGIN_TYPE === $this->type;
276 }
277
278 /**
279 * Return if the product is used as a theme.
280 *
281 * @return bool Is theme ?
282 */
283 public function is_theme() {
284 return self::THEME_TYPE === $this->type;
285 }
286
287 /**
288 * Returns the product slug.
289 *
290 * @return string The product slug.
291 */
292 public function get_slug() {
293 return $this->slug;
294 }
295
296 /**
297 * The magic var_dump info method.
298 *
299 * @return array Debug info.
300 */
301 public function __debugInfo() {
302 return array(
303 'name' => $this->name,
304 'slug' => $this->slug,
305 'version' => $this->version,
306 'basefile' => $this->basefile,
307 'key' => $this->key,
308 'type' => $this->type,
309 'store_name' => $this->store_name,
310 'store_url' => $this->store_url,
311 'wordpress_available' => $this->wordpress_available,
312 'requires_license' => $this->requires_license,
313 );
314
315 }
316
317 /**
318 * Getter for product version.
319 *
320 * @return string The product version.
321 */
322 public function get_version() {
323 return $this->version;
324 }
325
326 /**
327 * Returns current product license, if available.
328 *
329 * @return string Return license key, if available.
330 */
331 public function get_license() {
332
333 if ( ! $this->requires_license() && ! $this->is_wordpress_available() ) {
334 return 'free';
335 }
336 $license_data = get_option( $this->get_key() . '_license_data', '' );
337
338 if ( empty( $license_data ) ) {
339 return get_option( $this->get_key() . '_license', '' );
340 }
341 if ( ! isset( $license_data->key ) ) {
342 return get_option( $this->get_key() . '_license', '' );
343 }
344
345 return $license_data->key;
346 }
347
348 /**
349 * Either the product requires license or not.
350 *
351 * @return bool Either requires license or not.
352 */
353 public function requires_license() {
354 return $this->requires_license;
355 }
356
357 /**
358 * If product is available on wordpress.org or not.
359 *
360 * @return bool Either is wp available or not.
361 */
362 public function is_wordpress_available() {
363 return $this->wordpress_available;
364 }
365
366 /**
367 * Return friendly name.
368 *
369 * @return string Friendly name.
370 */
371 public function get_friendly_name() {
372 $name = trim( str_replace( 'Lite', '', $this->get_name() ) );
373 if ( defined( 'OTTER_BLOCKS_BASEFILE' ) && OTTER_BLOCKS_BASEFILE === $this->basefile ) {
374 $name = 'Otter Blocks';
375 }
376 if ( defined( 'OPTML_BASEFILE' ) && OPTML_BASEFILE === $this->basefile ) {
377 $name = 'Optimole';
378 }
379 if ( defined( 'WPMM_FILE' ) && WPMM_FILE === $this->basefile ) {
380 $name = 'LightStart';
381 }
382 $name = apply_filters( $this->get_key() . '_friendly_name', $name );
383 $name = rtrim( $name, '- ()' );
384
385 return $name;
386 }
387
388 /**
389 * Return the product version cache key.
390 *
391 * @return string The product version cache key.
392 */
393 public function get_cache_key() {
394 return $this->get_key() . '_' . preg_replace( '/[^0-9a-zA-Z ]/m', '', $this->get_version() ) . 'versions';
395 }
396
397 /**
398 * Getter for product name.
399 *
400 * @return string The product name.
401 */
402 public function get_name() {
403 return $this->name;
404 }
405
406 /**
407 * Returns the Store name.
408 *
409 * @return string Store name.
410 */
411 public function get_store_name() {
412 return $this->store_name;
413 }
414
415 /**
416 * Returns the store url.
417 *
418 * @return string The store url.
419 */
420 public function get_store_url() {
421
422 if ( strpos( $this->store_url, '/themeisle.com' ) !== false ) {
423 return 'https://store.themeisle.com/';
424 }
425
426 return $this->store_url;
427 }
428
429 /**
430 * Returns product basefile, which holds the metaheaders.
431 *
432 * @return string The product basefile.
433 */
434 public function get_basefile() {
435 return $this->basefile;
436 }
437
438 /**
439 * Get changelog url.
440 *
441 * @return string Changelog url.
442 */
443 public function get_changelog() {
444 return add_query_arg(
445 [
446 'name' => rawurlencode( $this->get_name() ),
447 'edd_action' => 'view_changelog',
448 'locale' => get_user_locale(),
449 ],
450 $this->get_store_url()
451 );
452 }
453
454 /**
455 * Returns product filename.
456 *
457 * @return string The product filename.
458 */
459 public function get_file() {
460 return $this->file;
461 }
462
463 /**
464 * Returns the pro slug, if available.
465 *
466 * @return string The pro slug.
467 */
468 public function get_pro_slug() {
469 return $this->pro_slug;
470 }
471
472 /**
473 * Return the install timestamp.
474 *
475 * @return int The install timestamp.
476 */
477 public function get_install_time() {
478 return $this->install;
479 }
480
481 /**
482 * Returns the URL of the product base file.
483 *
484 * @param string $path The path to the file.
485 *
486 * @return string The URL of the product base file.
487 */
488 public function get_base_url( $path = '/' ) {
489 if ( $this->type ) {
490 return plugins_url( $path, $this->basefile );
491 }
492 }
493
494 }
495