| 1 |
<?php |
| 2 |
/** |
| 3 |
* |
| 4 |
*/ |
| 5 |
|
| 6 |
// Disallow direct access. |
| 7 |
defined('ABSPATH') or die("Access denied"); |
| 8 |
|
| 9 |
/** |
| 10 |
* |
| 11 |
*/ |
| 12 |
class CJTSetupModel { |
| 13 |
|
| 14 |
/** |
| 15 |
* |
| 16 |
*/ |
| 17 |
const EDD_PRODUCT_NAME = 'CJT Free'; |
| 18 |
|
| 19 |
/** |
| 20 |
* |
| 21 |
*/ |
| 22 |
const LICENSES_CACHE = 'cache.CJTSetupModel.licenses'; |
| 23 |
|
| 24 |
/** |
| 25 |
* put your comment there... |
| 26 |
* |
| 27 |
* @var mixed |
| 28 |
*/ |
| 29 |
protected $licenses; |
| 30 |
|
| 31 |
/** |
| 32 |
* put your comment there... |
| 33 |
* |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
// Read all cached licenses! |
| 37 |
$this->licenses = get_option(self::LICENSES_CACHE); |
| 38 |
// Make sure its array! |
| 39 |
if (!is_array($this->licenses)) { |
| 40 |
$this->licenses = array(); |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* put your comment there... |
| 46 |
* |
| 47 |
* @param mixed $component |
| 48 |
* @param mixed $action |
| 49 |
* @param mixed $state |
| 50 |
* @param mixed $pluginBase |
| 51 |
*/ |
| 52 |
public function cacheState($component, $action, $state) { |
| 53 |
// Read cache from db! |
| 54 |
$cache =& $this->getLicenses(); |
| 55 |
// Add action to the state object! |
| 56 |
$state['action'] = $action; |
| 57 |
// Cache Plugin data! |
| 58 |
$state['plugin'] = get_plugin_data(ABSPATH . PLUGINDIR . "/{$component['pluginBase']}", false); |
| 59 |
// Cache object! |
| 60 |
$cache[$component['name']] = $state; |
| 61 |
update_option(self::LICENSES_CACHE, $cache); |
| 62 |
// update local Cache! |
| 63 |
$this->licenses = $cache; |
| 64 |
// return action name |
| 65 |
return $action; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* put your comment there... |
| 70 |
* |
| 71 |
* @param mixed $action |
| 72 |
* @param mixed $component |
| 73 |
* @param mixed $license |
| 74 |
*/ |
| 75 |
public function dispatchEddCall($action, $component, $license) { |
| 76 |
// Activating License key thorugh EDD APIs! |
| 77 |
$request['edd_action'] = "{$action}_license"; |
| 78 |
$request['item_name'] = urlencode($component['name']); |
| 79 |
$request['license'] = $license['key']; |
| 80 |
/* CJT Extra Fields For EDD */ |
| 81 |
$request['CJTEFFEDD_licenseName'] = $license['name']; |
| 82 |
// Request the server! |
| 83 |
$response = wp_remote_get(add_query_arg($request, cssJSToolbox::getCJTWebSiteURL())); |
| 84 |
// We need only the JSON object returned by EDD APIs. |
| 85 |
$response = @json_decode(wp_remote_retrieve_body($response), true); |
| 86 |
// If request error compaitble the response object to be used! |
| 87 |
if (!$response) { |
| 88 |
$response = array('license' => 'error', 'component' => $component['name']); |
| 89 |
} |
| 90 |
return $response; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* put your comment there... |
| 95 |
* |
| 96 |
*/ |
| 97 |
public function getCJTComponentData() { |
| 98 |
$component = array(); |
| 99 |
// Set info! |
| 100 |
$component['name'] = self::EDD_PRODUCT_NAME; |
| 101 |
$component['pluginBase'] = CJTOOLBOX_PLUGIN_BASE; |
| 102 |
return $component; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* put your comment there... |
| 107 |
* |
| 108 |
*/ |
| 109 |
public function & getLicenses() { |
| 110 |
return $this->licenses; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Get list of all cached licenses that has |
| 115 |
* a license key in $state state! |
| 116 |
* |
| 117 |
* @param mixed $state |
| 118 |
*/ |
| 119 |
public function getStatedLicenses($state = 'activate') { |
| 120 |
// Initializing! |
| 121 |
$statedList = array(); |
| 122 |
// Read all cached licenses! |
| 123 |
$cacheList =& $this->getLicenses(); |
| 124 |
// Find license with the requested state! |
| 125 |
foreach ($cacheList as $key => $license) { |
| 126 |
if ($license['action'] == $state) { |
| 127 |
$statedList[$key] = $license; |
| 128 |
} |
| 129 |
} |
| 130 |
return $statedList; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* put your comment there... |
| 135 |
* |
| 136 |
* @param mixed $compoment |
| 137 |
* @param mixed $field |
| 138 |
*/ |
| 139 |
public function getStateStruct($compoment, $struct = null) { |
| 140 |
// Read all licenses from db! |
| 141 |
$licensesCache =& $this->getLicenses(); |
| 142 |
// If not set return clean empty array! |
| 143 |
$componentName = $compoment['name']; |
| 144 |
$state = isset($licensesCache[$componentName]) ? $licensesCache[$componentName] : false; |
| 145 |
return ($struct ? ($state[$struct] ? $state[$struct] : false) : $state); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* put your comment there... |
| 150 |
* |
| 151 |
* @param mixed $state |
| 152 |
*/ |
| 153 |
public function removeCachedLicense($state) { |
| 154 |
// Initializing! |
| 155 |
$componentName = $state['component']['name']; |
| 156 |
// Read all cached licenses! |
| 157 |
$cachedStates =& $this->getLicenses(); |
| 158 |
// Set return value! |
| 159 |
$result = isset($cachedStates[$componentName]) ? 'valid' : 'invalid'; |
| 160 |
// Remove component license even if its not exists, nothing will happen! |
| 161 |
unset($cachedStates[$componentName]); |
| 162 |
// Update cache list! |
| 163 |
update_option(self::LICENSES_CACHE, $cachedStates); |
| 164 |
// Update local cache! |
| 165 |
$this->licenses = $cachedStates; |
| 166 |
return $result; |
| 167 |
} |
| 168 |
|
| 169 |
} // End class. |