models
1 month ago
processing
5 months ago
processors
5 months ago
class-rio-attachment.php
5 months ago
class-rio-backup.php
7 months ago
class-rio-bulk-optimization.php
1 month ago
class-rio-cron.php
7 months ago
class-rio-image-query.php
5 months ago
class-rio-image-statistic.php
5 months ago
class-rio-media-library.php
1 month ago
class-rio-multisite.php
7 months ago
class-rio-optimization-orchestrator.php
5 months ago
class-rio-optimization-tools.php
7 months ago
class-rio-views.php
5 months ago
class-wrio-license.php
7 months ago
class-wrio-premium-provider.php
7 months ago
class-wrio-support.php
7 months ago
index.php
7 months ago
class-wrio-premium-provider.php
559 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Premium Provider class |
| 4 | * |
| 5 | * Manages premium state via composition with WRIO_License. |
| 6 | * This is a lightweight replacement for the Freemius Premium Provider. |
| 7 | * |
| 8 | * @package Robin_Image_Optimizer |
| 9 | * @subpackage Classes |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Class WRIO_Premium_Provider |
| 19 | * |
| 20 | * Handles premium license state and operations. |
| 21 | */ |
| 22 | class WRIO_Premium_Provider { |
| 23 | |
| 24 | /** |
| 25 | * License instance |
| 26 | * |
| 27 | * @var WRIO_License |
| 28 | */ |
| 29 | private $license; |
| 30 | |
| 31 | /** |
| 32 | * Whether a license is activated |
| 33 | * |
| 34 | * @var bool |
| 35 | */ |
| 36 | private $is_activated = false; |
| 37 | |
| 38 | /** |
| 39 | * Constructor |
| 40 | * |
| 41 | * Initializes the license instance. |
| 42 | */ |
| 43 | public function __construct() { |
| 44 | $this->license = new WRIO_License(); |
| 45 | $this->is_activated = $this->license->has_license(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Check if a license key is activated (exists) |
| 50 | * |
| 51 | * @return bool |
| 52 | */ |
| 53 | public function is_activate() { |
| 54 | return $this->is_activated; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Check if the license is active (activated AND valid/not expired) |
| 59 | * |
| 60 | * @return bool |
| 61 | */ |
| 62 | public function is_active() { |
| 63 | if ( ! $this->is_activated ) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return $this->license->is_valid(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Get the plan name/title |
| 72 | * |
| 73 | * @return string|null |
| 74 | */ |
| 75 | public function get_plan() { |
| 76 | if ( ! $this->is_activated ) { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | return $this->license->get_plan(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get the plan ID |
| 85 | * |
| 86 | * @return int|null |
| 87 | */ |
| 88 | public function get_plan_id() { |
| 89 | if ( ! $this->is_activated ) { |
| 90 | return null; |
| 91 | } |
| 92 | |
| 93 | return $this->license->get_plan_id(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get billing cycle |
| 98 | * |
| 99 | * @return int|null 1 = monthly, 12 = yearly, null = lifetime |
| 100 | */ |
| 101 | public function get_billing_cycle() { |
| 102 | if ( ! $this->is_activated ) { |
| 103 | return null; |
| 104 | } |
| 105 | |
| 106 | return $this->license->get_billing_cycle(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Get the license instance |
| 111 | * |
| 112 | * @return WRIO_License |
| 113 | */ |
| 114 | public function get_license() { |
| 115 | return $this->license; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Check if there is an active paid subscription (auto-renewal) |
| 120 | * |
| 121 | * @return bool |
| 122 | */ |
| 123 | public function has_paid_subscription() { |
| 124 | if ( ! $this->is_activated ) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | return ! empty( $this->license->get_billing_cycle() ); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Activate a license key |
| 133 | * |
| 134 | * Routes to appropriate activation method based on key prefix: |
| 135 | * - sk_ prefix: Freemius license (stored in wbcr_io_license) |
| 136 | * - No prefix: ThemeIsle SDK license (processed via SDK filter) |
| 137 | * |
| 138 | * @param string $key The license key to activate. |
| 139 | * @return bool True on success. |
| 140 | * @throws Exception If activation fails. |
| 141 | */ |
| 142 | public function activate( $key ) { |
| 143 | $key = trim( $key ); |
| 144 | |
| 145 | if ( empty( $key ) ) { |
| 146 | throw new Exception( esc_html__( 'License key is empty.', 'robin-image-optimizer' ) ); |
| 147 | } |
| 148 | |
| 149 | // Detect license type by prefix |
| 150 | $is_freemius = strpos( $key, 'sk_' ) === 0; |
| 151 | |
| 152 | if ( $is_freemius ) { |
| 153 | return $this->activate_freemius( $key ); |
| 154 | } |
| 155 | |
| 156 | return $this->activate_sdk( $key ); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Activate a Freemius license key via API |
| 161 | * |
| 162 | * Makes a direct API call to Freemius activation endpoint. |
| 163 | * |
| 164 | * @param string $key The Freemius license key (sk_ prefixed). |
| 165 | * @return bool True on success. |
| 166 | * @throws Exception If activation fails. |
| 167 | */ |
| 168 | private function activate_freemius( $key ) { |
| 169 | $plugin_id = $this->get_setting( 'plugin_id' ); |
| 170 | |
| 171 | // Generate unique site identifier (32 chars) |
| 172 | $uid = $this->get_or_create_site_uid(); |
| 173 | |
| 174 | // Build API endpoint |
| 175 | $api_url = sprintf( |
| 176 | 'https://api.freemius.com/v1/plugins/%s/activate.json', |
| 177 | $plugin_id |
| 178 | ); |
| 179 | |
| 180 | // Prepare request body |
| 181 | $body = [ |
| 182 | 'license_key' => $key, |
| 183 | 'uid' => $uid, |
| 184 | 'url' => get_home_url(), |
| 185 | 'title' => get_bloginfo( 'name' ), |
| 186 | 'version' => WRIO_Plugin::app()->getPluginVersion(), |
| 187 | ]; |
| 188 | |
| 189 | // Add user info for new license activations |
| 190 | $current_user = wp_get_current_user(); |
| 191 | if ( $current_user->ID ) { |
| 192 | $body['user_email'] = $current_user->user_email; |
| 193 | $body['first_name'] = $current_user->first_name ? $current_user->first_name : $current_user->display_name; |
| 194 | $body['last_name'] = $current_user->last_name ? $current_user->last_name : ''; |
| 195 | } else { |
| 196 | $body['user_email'] = get_option( 'admin_email' ); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Filter request body before API call |
| 201 | * |
| 202 | * @param array<string, mixed> $body The request body. |
| 203 | * @param string $key The license key. |
| 204 | */ |
| 205 | $body = apply_filters( 'wrio_freemius_activate_request', $body, $key ); |
| 206 | |
| 207 | $body_json = wp_json_encode( $body ); |
| 208 | if ( false === $body_json ) { |
| 209 | throw new Exception( esc_html__( 'Failed to encode request body.', 'robin-image-optimizer' ) ); |
| 210 | } |
| 211 | |
| 212 | // Make API request |
| 213 | $response = wp_remote_post( |
| 214 | $api_url, |
| 215 | [ |
| 216 | 'timeout' => 30, |
| 217 | 'headers' => [ |
| 218 | 'Content-Type' => 'application/json', |
| 219 | ], |
| 220 | 'body' => $body_json, |
| 221 | 'sslverify' => true, |
| 222 | ] |
| 223 | ); |
| 224 | |
| 225 | if ( is_wp_error( $response ) ) { |
| 226 | throw new Exception( |
| 227 | esc_html( |
| 228 | sprintf( |
| 229 | /* translators: %s: error message */ |
| 230 | __( 'API request failed: %s', 'robin-image-optimizer' ), |
| 231 | $response->get_error_message() |
| 232 | ) |
| 233 | ) |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | $response_code = wp_remote_retrieve_response_code( $response ); |
| 238 | $response_body = wp_remote_retrieve_body( $response ); |
| 239 | $data = json_decode( $response_body, true ); |
| 240 | |
| 241 | // Check for API errors |
| 242 | if ( 200 !== $response_code || isset( $data['error'] ) ) { |
| 243 | $error_message = isset( $data['error']['message'] ) |
| 244 | ? $data['error']['message'] |
| 245 | : __( 'License activation failed.', 'robin-image-optimizer' ); |
| 246 | |
| 247 | throw new Exception( esc_html( $error_message ) ); |
| 248 | } |
| 249 | |
| 250 | // Validate response has required fields |
| 251 | if ( empty( $data['install_id'] ) ) { |
| 252 | throw new Exception( esc_html__( 'Invalid API response: missing install_id.', 'robin-image-optimizer' ) ); |
| 253 | } |
| 254 | |
| 255 | // Build license data from API response |
| 256 | $license_data = $this->build_license_data_from_response( $data, $key, $uid ); |
| 257 | |
| 258 | /** |
| 259 | * Filter license data before saving |
| 260 | * |
| 261 | * @param array $license_data The license data to save. |
| 262 | * @param string $key The license key being activated. |
| 263 | * @param array $data The raw API response. |
| 264 | */ |
| 265 | $license_data = apply_filters( 'wrio_license_activate_data', $license_data, $key, $data ); |
| 266 | |
| 267 | update_option( 'wbcr_io_license', $license_data ); |
| 268 | |
| 269 | // Reinitialize license |
| 270 | $this->license = new WRIO_License(); |
| 271 | $this->is_activated = true; |
| 272 | |
| 273 | /** |
| 274 | * Action fired after license is activated |
| 275 | * |
| 276 | * @param string $key The activated license key. |
| 277 | * @param array $license_data The stored license data. |
| 278 | * @param array $data The raw API response. |
| 279 | */ |
| 280 | do_action( 'wrio_license_activated', $key, $license_data, $data ); |
| 281 | |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * Get or create a unique site identifier for Freemius |
| 287 | * |
| 288 | * @return string 32-character unique identifier. |
| 289 | */ |
| 290 | private function get_or_create_site_uid() { |
| 291 | $uid = get_option( 'wrio_freemius_uid' ); |
| 292 | |
| 293 | if ( empty( $uid ) ) { |
| 294 | // Generate a 32-char unique identifier |
| 295 | $uid = wp_generate_uuid4(); |
| 296 | $uid = str_replace( '-', '', $uid ); |
| 297 | update_option( 'wrio_freemius_uid', $uid ); |
| 298 | } |
| 299 | |
| 300 | return $uid; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Build license data array from Freemius API response |
| 305 | * |
| 306 | * @param array<string, mixed> $data The API response data. |
| 307 | * @param string $key The license key. |
| 308 | * @param string $uid The site UID. |
| 309 | * @return array<string, mixed> License data for storage. |
| 310 | */ |
| 311 | private function build_license_data_from_response( $data, $key, $uid ) { |
| 312 | $install = isset( $data['install'] ) ? $data['install'] : $data; |
| 313 | $user = isset( $data['user'] ) ? $data['user'] : []; |
| 314 | $license = isset( $data['license'] ) ? $data['license'] : []; |
| 315 | |
| 316 | return [ |
| 317 | 'license' => [ |
| 318 | 'id' => isset( $license['id'] ) ? (int) $license['id'] : 0, |
| 319 | 'secret_key' => $key, |
| 320 | 'plan_id' => isset( $license['plan_id'] ) ? (int) $license['plan_id'] : 0, |
| 321 | 'plan_title' => isset( $license['plan_title'] ) ? $license['plan_title'] : 'Premium', |
| 322 | 'expiration' => isset( $license['expiration'] ) ? $license['expiration'] : null, |
| 323 | 'quota' => isset( $license['quota'] ) ? (int) $license['quota'] : null, |
| 324 | 'activated' => 1, |
| 325 | 'activated_local' => 1, |
| 326 | 'is_cancelled' => isset( $license['is_cancelled'] ) ? (bool) $license['is_cancelled'] : false, |
| 327 | 'is_block_features' => isset( $license['is_block_features'] ) ? (bool) $license['is_block_features'] : false, |
| 328 | 'billing_cycle' => isset( $license['billing_cycle'] ) ? (int) $license['billing_cycle'] : null, |
| 329 | 'created' => current_time( 'mysql' ), |
| 330 | 'updated' => current_time( 'mysql' ), |
| 331 | ], |
| 332 | 'site' => [ |
| 333 | 'id' => isset( $install['id'] ) ? (int) $install['id'] : ( isset( $data['install_id'] ) ? (int) $data['install_id'] : 0 ), |
| 334 | 'uid' => $uid, |
| 335 | 'url' => get_home_url(), |
| 336 | 'public_key' => isset( $install['public_key'] ) ? $install['public_key'] : '', |
| 337 | 'secret_key' => isset( $install['secret_key'] ) ? $install['secret_key'] : '', |
| 338 | 'install_api_token' => isset( $data['install_api_token'] ) ? $data['install_api_token'] : '', |
| 339 | ], |
| 340 | 'user' => [ |
| 341 | 'id' => isset( $user['id'] ) ? (int) $user['id'] : 0, |
| 342 | 'email' => isset( $user['email'] ) ? $user['email'] : get_option( 'admin_email' ), |
| 343 | 'public_key' => isset( $user['public_key'] ) ? $user['public_key'] : '', |
| 344 | ], |
| 345 | ]; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Activate a ThemeIsle SDK license key |
| 350 | * |
| 351 | * Uses the SDK's do_license_process() via the registered filter. |
| 352 | * The SDK handles all API calls and stores license data automatically. |
| 353 | * |
| 354 | * @param string $key The SDK license key (unprefixed). |
| 355 | * @return bool True on success. |
| 356 | * @throws Exception If activation fails. |
| 357 | */ |
| 358 | private function activate_sdk( $key ) { |
| 359 | $namespace = WRIO_Plugin::get_sdk_namespace(); |
| 360 | |
| 361 | // Use SDK filter - it handles API calls via do_license_process() |
| 362 | // SDK returns: true on success, WP_Error on failure |
| 363 | $response = apply_filters( |
| 364 | 'themeisle_sdk_license_process_' . $namespace, |
| 365 | $key, |
| 366 | 'activate' |
| 367 | ); |
| 368 | |
| 369 | if ( is_wp_error( $response ) ) { |
| 370 | throw new Exception( esc_html( $response->get_error_message() ) ); |
| 371 | } |
| 372 | |
| 373 | if ( true !== $response ) { |
| 374 | throw new Exception( esc_html__( 'License activation failed.', 'robin-image-optimizer' ) ); |
| 375 | } |
| 376 | |
| 377 | // Reinitialize license (SDK already stored the data) |
| 378 | $this->license = new WRIO_License(); |
| 379 | $this->is_activated = true; |
| 380 | |
| 381 | /** |
| 382 | * Action fired after license is activated |
| 383 | * |
| 384 | * @param string $key The activated license key. |
| 385 | * @param array $data Additional data. |
| 386 | */ |
| 387 | do_action( 'wrio_license_activated', $key, [] ); |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Deactivate the current license |
| 394 | * |
| 395 | * Clears the appropriate option based on license source. |
| 396 | * |
| 397 | * @return bool True on success. |
| 398 | */ |
| 399 | public function deactivate() { |
| 400 | if ( ! $this->is_activated ) { |
| 401 | return true; |
| 402 | } |
| 403 | |
| 404 | $old_key = $this->license->get_key(); |
| 405 | $source = $this->license->get_source(); |
| 406 | |
| 407 | /** |
| 408 | * Action fired before license is deactivated |
| 409 | * |
| 410 | * @param string|null $key The license key being deactivated. |
| 411 | */ |
| 412 | do_action( 'wrio_license_before_deactivate', $old_key ); |
| 413 | |
| 414 | // Clear the appropriate option based on source |
| 415 | if ( WRIO_License::SOURCE_SDK === $source ) { |
| 416 | $namespace = WRIO_Plugin::get_sdk_namespace(); |
| 417 | |
| 418 | // Use SDK filter - it handles API calls and deletes options |
| 419 | apply_filters( |
| 420 | 'themeisle_sdk_license_process_' . $namespace, |
| 421 | $old_key, |
| 422 | 'deactivate' |
| 423 | ); |
| 424 | |
| 425 | // Ensure cleanup (SDK may have already deleted these) |
| 426 | delete_option( $namespace . '_license_data' ); |
| 427 | delete_transient( $namespace . '_license_data' ); |
| 428 | } else { |
| 429 | delete_option( 'wbcr_io_license' ); |
| 430 | } |
| 431 | |
| 432 | // Reinitialize license |
| 433 | $this->license = new WRIO_License(); |
| 434 | $this->is_activated = false; |
| 435 | |
| 436 | /** |
| 437 | * Action fired after license is deactivated |
| 438 | * |
| 439 | * @param string|null $key The deactivated license key. |
| 440 | */ |
| 441 | do_action( 'wrio_license_deactivated', $old_key ); |
| 442 | |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Cancel paid subscription |
| 448 | * |
| 449 | * In a full implementation, this would call the payment provider API. |
| 450 | * For now, it just removes the billing cycle from local data. |
| 451 | * |
| 452 | * @return bool True on success. |
| 453 | */ |
| 454 | public function cancel_paid_subscription() { |
| 455 | if ( ! $this->is_activated || ! $this->has_paid_subscription() ) { |
| 456 | return false; |
| 457 | } |
| 458 | |
| 459 | $license_data = get_option( 'wbcr_io_license', [] ); |
| 460 | |
| 461 | if ( isset( $license_data['license']['billing_cycle'] ) ) { |
| 462 | $license_data['license']['billing_cycle'] = null; |
| 463 | update_option( 'wbcr_io_license', $license_data ); |
| 464 | } |
| 465 | |
| 466 | // Refresh license |
| 467 | $this->license = new WRIO_License(); |
| 468 | |
| 469 | /** |
| 470 | * Action fired after subscription is cancelled |
| 471 | * |
| 472 | * @param WRIO_License $license The license instance. |
| 473 | */ |
| 474 | do_action( 'wrio_subscription_cancelled', $this->license ); |
| 475 | |
| 476 | return true; |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Get a setting value (for compatibility with Freemius provider) |
| 481 | * |
| 482 | * @param string $key Setting key. |
| 483 | * @param mixed $default Default value. |
| 484 | * @return mixed |
| 485 | */ |
| 486 | /** |
| 487 | * Get plugin setting |
| 488 | * |
| 489 | * @param string|null $key The setting key. |
| 490 | * @param mixed $default_value The default value if key not found. |
| 491 | * @return mixed The setting value or default. |
| 492 | */ |
| 493 | public function get_setting( $key, $default_value = null ) { |
| 494 | $settings = [ |
| 495 | 'plugin_id' => '3464', |
| 496 | 'public_key' => 'pk_cafff5a51bd5fcf09c6bde806956d', |
| 497 | 'slug' => 'robin-image-optimizer', |
| 498 | ]; |
| 499 | |
| 500 | return isset( $settings[ $key ] ) ? $settings[ $key ] : $default_value; |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Get upgrade price (stub for Factory framework compatibility) |
| 505 | * |
| 506 | * @return int |
| 507 | */ |
| 508 | public function get_price() { |
| 509 | return 0; |
| 510 | } |
| 511 | |
| 512 | /** |
| 513 | * Check if premium package is installed (stub for Factory framework compatibility) |
| 514 | * |
| 515 | * @return bool |
| 516 | */ |
| 517 | public function is_install_package() { |
| 518 | return false; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Get package data (stub for Factory framework compatibility) |
| 523 | * |
| 524 | * @return array<string, mixed>|null |
| 525 | */ |
| 526 | public function get_package_data() { |
| 527 | return null; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Get package download URL (stub for Factory framework compatibility) |
| 532 | * |
| 533 | * @return string |
| 534 | */ |
| 535 | public function get_package_download_url() { |
| 536 | return ''; |
| 537 | } |
| 538 | |
| 539 | /** |
| 540 | * Get downloadable package info (stub for Factory framework compatibility) |
| 541 | * |
| 542 | * @return array<string, mixed>|null |
| 543 | */ |
| 544 | public function get_downloadable_package_info() { |
| 545 | return null; |
| 546 | } |
| 547 | |
| 548 | /** |
| 549 | * Sync license (stub for Factory framework compatibility) |
| 550 | * |
| 551 | * No-op since sync functionality was removed. |
| 552 | * |
| 553 | * @return bool |
| 554 | */ |
| 555 | public function sync() { |
| 556 | return true; |
| 557 | } |
| 558 | } |
| 559 |