| 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 LICENSES_CACHE = 'cache.CJTSetupModel.licenses'; |
| 18 |
|
| 19 |
/** |
| 20 |
* put your comment there... |
| 21 |
* |
| 22 |
* @var mixed |
| 23 |
*/ |
| 24 |
protected $licenses; |
| 25 |
|
| 26 |
/** |
| 27 |
* put your comment there... |
| 28 |
* |
| 29 |
*/ |
| 30 |
public function __construct() { |
| 31 |
// Read all cached licenses! |
| 32 |
$this->licenses = get_option(self::LICENSES_CACHE); |
| 33 |
// Make sure its array! |
| 34 |
if (!is_array($this->licenses)) { |
| 35 |
$this->licenses = array(); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* put your comment there... |
| 41 |
* |
| 42 |
* @param mixed $component |
| 43 |
* @param mixed $action |
| 44 |
* @param mixed $state |
| 45 |
* @param mixed $pluginBase |
| 46 |
*/ |
| 47 |
public function cacheState($component, $action, $state) { |
| 48 |
// Read cache from db! |
| 49 |
$cache =& $this->getLicenses(); |
| 50 |
// Add action to the state object! |
| 51 |
$state['action'] = $action; |
| 52 |
// Cache Plugin data! |
| 53 |
$state['plugin'] = get_plugin_data(ABSPATH . PLUGINDIR . "/{$component['pluginBase']}", false); |
| 54 |
// Cache object! |
| 55 |
$cache[$component['name']] = $state; |
| 56 |
update_option(self::LICENSES_CACHE, $cache); |
| 57 |
// update local Cache! |
| 58 |
$this->licenses = $cache; |
| 59 |
// return action name |
| 60 |
return $action; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* put your comment there... |
| 65 |
* |
| 66 |
* @param mixed $action |
| 67 |
* @param mixed $component |
| 68 |
* @param mixed $license |
| 69 |
*/ |
| 70 |
public function dispatchLicenseAction($action, $component, $license) { |
| 71 |
# Get Plugin File from component base name |
| 72 |
$pluginFile = WP_PLUGIN_DIR . $component[ 'pluginBase' ]; |
| 73 |
# Get CJT Store object |
| 74 |
$store = new CJTStore( $component [ 'name' ], $license[ 'key' ], $pluginFile ); |
| 75 |
# Build method name from the given action |
| 76 |
$methodName = "{$action}License"; |
| 77 |
try { |
| 78 |
# Call requested method |
| 79 |
$result = $store->$methodName( $license[ 'name' ] ); |
| 80 |
# Build response object locally |
| 81 |
# The structure is taken from EDD license extension as it was used here |
| 82 |
# when the plugin is orignally developed |
| 83 |
if ( $result ) { // Success operation |
| 84 |
$response['license'] = 'valid'; |
| 85 |
} |
| 86 |
else { // Operation faild |
| 87 |
$response['license'] = 'invalid'; |
| 88 |
} |
| 89 |
} |
| 90 |
catch ( CJTServicesAPICallException $exception ) { |
| 91 |
// If request error compaitble the response object to be used! |
| 92 |
$response = array('license' => 'error', 'component' => $component['name']); |
| 93 |
} |
| 94 |
return $response; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* put your comment there... |
| 99 |
* |
| 100 |
*/ |
| 101 |
public function getCJTComponentData() { |
| 102 |
$component = array(); |
| 103 |
// Set info! |
| 104 |
$component['pluginBase'] = CJTOOLBOX_PLUGIN_BASE; |
| 105 |
$component['title'] = 'CSS & JavaScript Toolbox'; |
| 106 |
return $component; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* put your comment there... |
| 111 |
* |
| 112 |
* @param mixed $component |
| 113 |
*/ |
| 114 |
public function getExtensionProductTypes($component) { |
| 115 |
# Initialize |
| 116 |
$types = array(); |
| 117 |
# Extension plugin file |
| 118 |
$pluginDirName = dirname($component['pluginBase']); |
| 119 |
$pluginXMLFile = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . |
| 120 |
$pluginDirName . DIRECTORY_SEPARATOR . |
| 121 |
$pluginDirName . '.xml'; |
| 122 |
# Use XML |
| 123 |
$exDef = new SimpleXMLElement(file_get_contents($pluginXMLFile)); |
| 124 |
# Register XPath namespace |
| 125 |
$license = $exDef->license; |
| 126 |
$license->registerXPathNamespace('ext', 'https://css-javascript-toolbox.com/extensions/xmldeffile'); |
| 127 |
# Get types |
| 128 |
$typesSrc = $exDef->license->xpath('name'); |
| 129 |
foreach ($typesSrc as $type) { |
| 130 |
# Product name |
| 131 |
$name = (string) $type; |
| 132 |
# Old Definiton document doesnt support text attributes. |
| 133 |
# If not there use name node value as TEXT |
| 134 |
if (!$text = ((string) $type->attributes()->text)) { |
| 135 |
$text = $name; |
| 136 |
} |
| 137 |
# Add to list |
| 138 |
$types[$name] = array('name' => $name, 'text' => $text); |
| 139 |
} |
| 140 |
return $types; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* put your comment there... |
| 145 |
* |
| 146 |
*/ |
| 147 |
public function & getLicenses() { |
| 148 |
return $this->licenses; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Get list of all cached licenses that has |
| 153 |
* a license key in $state state! |
| 154 |
* |
| 155 |
* @param mixed $state |
| 156 |
*/ |
| 157 |
public function getStatedLicenses($state = 'activate') { |
| 158 |
// Initializing! |
| 159 |
$statedList = array(); |
| 160 |
// Read all cached licenses! |
| 161 |
$cacheList =& $this->getLicenses(); |
| 162 |
// Find license with the requested state! |
| 163 |
foreach ($cacheList as $key => $license) { |
| 164 |
if ($license['action'] == $state) { |
| 165 |
$statedList[$key] = $license; |
| 166 |
} |
| 167 |
} |
| 168 |
return $statedList; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* put your comment there... |
| 173 |
* |
| 174 |
* @param mixed $licenseTypes |
| 175 |
* @param mixed $compoment |
| 176 |
* @param mixed $struct |
| 177 |
*/ |
| 178 |
public function getStateStruct($licenseTypes, $struct = null) { |
| 179 |
// INit |
| 180 |
$state = null; |
| 181 |
// Read all licenses from db! |
| 182 |
$licensesCache =& $this->getLicenses(); |
| 183 |
// Find license |
| 184 |
foreach ($licenseTypes as $type) { |
| 185 |
# Get product name to search |
| 186 |
$productName = $type['name']; |
| 187 |
if (isset($licensesCache[$productName])) { |
| 188 |
# Get product state |
| 189 |
$state = $licensesCache[$productName]; |
| 190 |
# Filter to section |
| 191 |
if ($struct) { |
| 192 |
$state = $state[$struct]; |
| 193 |
} |
| 194 |
# ALways push product name |
| 195 |
$state['productName'] = $productName; |
| 196 |
# Exit for |
| 197 |
break; |
| 198 |
} |
| 199 |
} |
| 200 |
return $state; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* put your comment there... |
| 205 |
* |
| 206 |
* @param mixed $state |
| 207 |
*/ |
| 208 |
public function removeCachedLicense($state) { |
| 209 |
// Initializing! |
| 210 |
$componentName = $state['component']['name']; |
| 211 |
// Read all cached licenses! |
| 212 |
$cachedStates =& $this->getLicenses(); |
| 213 |
// Set return value! |
| 214 |
$result = isset($cachedStates[$componentName]) ? 'valid' : 'invalid'; |
| 215 |
// Remove component license even if its not exists, nothing will happen! |
| 216 |
unset($cachedStates[$componentName]); |
| 217 |
// Update cache list! |
| 218 |
update_option(self::LICENSES_CACHE, $cachedStates); |
| 219 |
// Update local cache! |
| 220 |
$this->licenses = $cachedStates; |
| 221 |
return $result; |
| 222 |
} |
| 223 |
|
| 224 |
} // End class. |