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