| 1 |
<?php |
| 2 |
|
| 3 |
// Exit if accessed directly |
| 4 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 5 |
|
| 6 |
class Vimeography_Theme_List extends Vimeography_Base { |
| 7 |
|
| 8 |
public $messages = array(); |
| 9 |
public $updater; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->updater = Vimeography::get_instance()->updater; |
| 13 |
add_action('vimeography_action_activate_license', array( $this, 'activate_license' ) ); |
| 14 |
add_action('vimeography_action_deactivate_license', array( $this, 'deactivate_license' ) ); |
| 15 |
|
| 16 |
self::_remove_duplicate_keys(); |
| 17 |
$this->_check_licenses(); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns a security form field for the form. |
| 22 |
* |
| 23 |
* @access public |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
public static function nonce() { |
| 27 |
return wp_nonce_field('vimeography-install-theme','vimeography-theme-verification'); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Process an activate license request. |
| 33 |
* |
| 34 |
* @return [type] [description] |
| 35 |
*/ |
| 36 |
public function activate_license() { |
| 37 |
|
| 38 |
// Ignore if key is not set |
| 39 |
if ( ! isset( $_POST['vimeography-activation-key'] ) ) |
| 40 |
return; |
| 41 |
|
| 42 |
try { |
| 43 |
$this->updater->activate_license( $_POST['vimeography-activation-key'] ); |
| 44 |
$this->messages[] = array( |
| 45 |
'type' => 'updated', |
| 46 |
'heading' => __('License Key added.', 'vimeography'), |
| 47 |
'message' => __('Your license key has been added to this site.', 'vimeography') |
| 48 |
); |
| 49 |
} catch (Exception $e) { |
| 50 |
$this->messages[] = array( |
| 51 |
'type' => 'error', |
| 52 |
'heading' => __('Dangit.', 'vimeography'), |
| 53 |
'message' => $e->getMessage() |
| 54 |
); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Process an activate license request. |
| 60 |
* |
| 61 |
* @return [type] [description] |
| 62 |
*/ |
| 63 |
public function deactivate_license() { |
| 64 |
|
| 65 |
// Ignore if key is not set |
| 66 |
if ( ! isset( $_POST['vimeography-activation-key'] ) ) |
| 67 |
return; |
| 68 |
|
| 69 |
try { |
| 70 |
$this->updater->deactivate_license( $_POST['vimeography-activation-key'] ); |
| 71 |
$this->messages[] = array( |
| 72 |
'type' => 'updated', |
| 73 |
'heading' => __('License Key deactivated.', 'vimeography'), |
| 74 |
'message' => __('Your license key has been removed from this site.', 'vimeography') |
| 75 |
); |
| 76 |
} catch (Exception $e) { |
| 77 |
$this->messages[] = array( |
| 78 |
'type' => 'error', |
| 79 |
'heading' => __('Dangit.', 'vimeography'), |
| 80 |
'message' => $e->getMessage() |
| 81 |
); |
| 82 |
} |
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Get the activation keys for the current site for use in the admin template. |
| 89 |
* |
| 90 |
* array(1) { |
| 91 |
* [0]=> |
| 92 |
* object(stdClass)#636 (7) { |
| 93 |
* ["activation_key"]=> |
| 94 |
* string(16) "3XAMP13" |
| 95 |
* ["plugin_name"]=> |
| 96 |
* string(28) "vimeography-developer-bundle" |
| 97 |
* ["product_name"]=> |
| 98 |
* string(16) "Developer Bundle" |
| 99 |
* ["status"]=> |
| 100 |
* string(5) "valid" |
| 101 |
* ["expires"]=> |
| 102 |
* string(19) "2018-07-22 11:49:33" |
| 103 |
* ["limit"]=> |
| 104 |
* string(1) "0" |
| 105 |
* ["activations_left"]=> |
| 106 |
* string(9) "unlimited" |
| 107 |
* } |
| 108 |
* } |
| 109 |
* |
| 110 |
* @return [type] [description] |
| 111 |
*/ |
| 112 |
public static function activation_keys() { |
| 113 |
$licenses = get_site_option('vimeography_activation_keys'); |
| 114 |
if ($licenses) { |
| 115 |
foreach ($licenses as $index => $license) { |
| 116 |
$license->expires = date('F j, Y', strtotime($license->expires) ); |
| 117 |
$license->status = ucfirst( $license->status ); |
| 118 |
$licenses[$index] = $license; |
| 119 |
} |
| 120 |
} |
| 121 |
return $licenses; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* [_remove_duplicate_keys description] |
| 126 |
* @return [type] [description] |
| 127 |
*/ |
| 128 |
private static function _remove_duplicate_keys() { |
| 129 |
if ( get_option('vimeography_activation_keys') ) { |
| 130 |
$activation_keys = array_map("unserialize", array_unique(array_map("serialize", get_option('vimeography_activation_keys')))); |
| 131 |
update_option('vimeography_activation_keys', $activation_keys); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Make sure licenses are up to date with current information. |
| 137 |
* Here's what a successful response looks like: |
| 138 |
* |
| 139 |
* public 'success' => boolean true |
| 140 |
* public 'license' => string 'site_inactive' (length=13) |
| 141 |
* public 'item_name' => string '' (length=0) |
| 142 |
* public 'expires' => string '2018-07-22 11:49:33' (length=19) |
| 143 |
* public 'payment_id' => string '1234' (length=4) |
| 144 |
* public 'customer_name' => string ' ' (length=1) |
| 145 |
* public 'customer_email' => string 'email@gmail.com' (length=21) |
| 146 |
* public 'license_limit' => string '0' (length=1) |
| 147 |
* public 'site_count' => int 2 |
| 148 |
* public 'activations_left' => string 'unlimited' (length=9) |
| 149 |
* |
| 150 |
* @since 1.3.2 |
| 151 |
* @return [type] [description] |
| 152 |
*/ |
| 153 |
private function _check_licenses() { |
| 154 |
$licenses = get_site_option('vimeography_activation_keys'); |
| 155 |
|
| 156 |
if ($licenses) { |
| 157 |
foreach ($licenses as $index => $license) { |
| 158 |
|
| 159 |
// Retrieve up-to-date |
| 160 |
$result = $this->updater->check_license( $license ); |
| 161 |
|
| 162 |
// remove license if not authorized on this site. |
| 163 |
if ( $result->license == 'site_inactive' ) { |
| 164 |
unset( $licenses[$index] ); |
| 165 |
continue; |
| 166 |
} |
| 167 |
|
| 168 |
$license->status = $result->license; |
| 169 |
$license->expires = $result->expires; |
| 170 |
$license->limit = $result->license_limit; |
| 171 |
$license->activations_left = $result->activations_left; |
| 172 |
|
| 173 |
$licenses[$index] = $license; |
| 174 |
} |
| 175 |
|
| 176 |
update_site_option('vimeography_activation_keys', $licenses); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|