| @@ -70,8 +70,14 @@ | ||
| 70 | 70 | * @var string $key The product ready key. |
| 71 | 71 | */ |
| 72 | 72 | private $key; |
| 73 | 73 | /** |
| 74 | + * Author URL | |
| 75 | + * | |
| 76 | + * @var string $author_url The author url. | |
| 77 | + */ | |
| 78 | + private $author_url; | |
| 79 | + /** | |
| 74 | 80 | * Product store url. |
| 75 | 81 | * |
| 76 | 82 | * @var string $store_url The store url. |
| 77 | 83 | */ |
| @@ -105,8 +111,18 @@ | ||
| 105 | 111 | * |
| 106 | 112 | * @var string $version The product version. |
| 107 | 113 | */ |
| 108 | 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/'; | |
| 109 | 125 | |
| 110 | 126 | /** |
| 111 | 127 | * ThemeIsle_SDK_Product constructor. |
| 112 | 128 | * |
| @@ -113,9 +129,9 @@ | ||
| 113 | 129 | * @param string $basefile Product basefile. |
| 114 | 130 | */ |
| 115 | 131 | public function __construct( $basefile ) { |
| 116 | 132 | if ( ! empty( $basefile ) ) { |
| 117 | - if ( is_readable( $basefile ) ) { | |
| 133 | + if ( is_file( $basefile ) ) { | |
| 118 | 134 | $this->basefile = $basefile; |
| 119 | 135 | $this->setup_from_path(); |
| 120 | 136 | $this->setup_from_fileheaders(); |
| 121 | 137 | } |
| @@ -124,10 +140,27 @@ | ||
| 124 | 140 | if ( 0 === $install ) { |
| 125 | 141 | $install = time(); |
| 126 | 142 | update_option( $this->get_key() . '_install', time() ); |
| 127 | 143 | } |
| 128 | - $this->install = $install; | |
| 144 | + $this->install = $install; | |
| 145 | + self::$cached_products[ crc32( $basefile ) ] = $this; | |
| 146 | + } | |
| 129 | 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 ]; | |
| 130 | 163 | } |
| 131 | 164 | |
| 132 | 165 | /** |
| 133 | 166 | * Setup props from path. |
| @@ -153,9 +186,9 @@ | ||
| 153 | 186 | * @param string $string the String to be normalized for cron handler. |
| 154 | 187 | * |
| 155 | 188 | * @return string $name The normalized string. |
| 156 | 189 | */ |
| 157 | - static function key_ready_name( $string ) { | |
| 190 | + public static function key_ready_name( $string ) { | |
| 158 | 191 | return str_replace( '-', '_', strtolower( trim( $string ) ) ); |
| 159 | 192 | } |
| 160 | 193 | |
| 161 | 194 | /** |
| @@ -199,8 +232,9 @@ | ||
| 199 | 232 | */ |
| 200 | 233 | public function get_key() { |
| 201 | 234 | return $this->key; |
| 202 | 235 | } |
| 236 | + | |
| 203 | 237 | /** |
| 204 | 238 | * Check if the product is either theme or plugin. |
| 205 | 239 | * |
| 206 | 240 | * @return string Product type. |
| @@ -311,9 +345,19 @@ | ||
| 311 | 345 | * |
| 312 | 346 | * @return string Friendly name. |
| 313 | 347 | */ |
| 314 | 348 | public function get_friendly_name() { |
| 315 | - $name = apply_filters( $this->get_key() . '_friendly_name', trim( str_replace( 'Lite', '', $this->get_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 ); | |
| 316 | 360 | $name = rtrim( $name, '- ()' ); |
| 317 | 361 | |
| 318 | 362 | return $name; |
| 319 | 363 | } |
| @@ -318,8 +362,17 @@ | ||
| 318 | 362 | return $name; |
| 319 | 363 | } |
| 320 | 364 | |
| 321 | 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 | + /** | |
| 322 | 375 | * Getter for product name. |
| 323 | 376 | * |
| 324 | 377 | * @return string The product name. |
| 325 | 378 | */ |
| @@ -341,8 +394,13 @@ | ||
| 341 | 394 | * |
| 342 | 395 | * @return string The store url. |
| 343 | 396 | */ |
| 344 | 397 | public function get_store_url() { |
| 398 | + | |
| 399 | + if ( strpos( $this->store_url, '/themeisle.com' ) !== false ) { | |
| 400 | + return 'https://store.themeisle.com/'; | |
| 401 | + } | |
| 402 | + | |
| 345 | 403 | return $this->store_url; |
| 346 | 404 | } |
| 347 | 405 | |
| 348 | 406 | /** |
| @@ -354,8 +412,24 @@ | ||
| 354 | 412 | return $this->basefile; |
| 355 | 413 | } |
| 356 | 414 | |
| 357 | 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 | + /** | |
| 358 | 432 | * Returns product filename. |
| 359 | 433 | * |
| 360 | 434 | * @return string The product filename. |
| 361 | 435 | */ |
| @@ -361,8 +435,9 @@ | ||
| 361 | 435 | */ |
| 362 | 436 | public function get_file() { |
| 363 | 437 | return $this->file; |
| 364 | 438 | } |
| 439 | + | |
| 365 | 440 | /** |
| 366 | 441 | * Returns the pro slug, if available. |
| 367 | 442 | * |
| 368 | 443 | * @return string The pro slug. |
| @@ -377,7 +452,20 @@ | ||
| 377 | 452 | * @return int The install timestamp. |
| 378 | 453 | */ |
| 379 | 454 | public function get_install_time() { |
| 380 | 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 | + } | |
| 381 | 469 | } |
| 382 | 470 | |
| 383 | 471 | } |