| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
class Vimeography_Update { |
| 6 |
/** |
| 7 |
* All of the Vimeography activation keys that the user has stored. |
| 8 |
* |
| 9 |
* Example: |
| 10 |
* |
| 11 |
* array(1) { |
| 12 |
* [0]=> |
| 13 |
* object(stdClass)#472 (3) { |
| 14 |
* ["activation_key"]=> |
| 15 |
* string(16) "n0Ae9UP49sNfw5aFGxiyn7mzi09c1Ua7" |
| 16 |
* ["plugin_name"]=> |
| 17 |
* string(19) "vimeography-journey" |
| 18 |
* ["product_name"]=> |
| 19 |
* string(7) "Journey" |
| 20 |
* } |
| 21 |
* } |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
private $_activation_keys; |
| 26 |
|
| 27 |
/** |
| 28 |
* The endpoint of the Vimeography Updater API. |
| 29 |
* this is the URL our updater / license checker pings. |
| 30 |
* |
| 31 |
* This should be the URL of the site with EDD installed |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $_endpoint = 'https://vimeography.com'; |
| 36 |
|
| 37 |
/** |
| 38 |
* [__construct description] |
| 39 |
*/ |
| 40 |
public function __construct() { |
| 41 |
//delete_site_option('vimeography_activation_keys'); |
| 42 |
$activation_keys = get_site_option('vimeography_activation_keys'); |
| 43 |
$this->_activation_keys = $activation_keys ? $activation_keys : array(); |
| 44 |
|
| 45 |
// Setup hooks |
| 46 |
$this->_includes(); |
| 47 |
$this->_hooks(); |
| 48 |
$this->_vimeography_auto_updater(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Include the EDD updater class |
| 53 |
* |
| 54 |
* @access private |
| 55 |
* @return void |
| 56 |
*/ |
| 57 |
private function _includes() { |
| 58 |
if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) { |
| 59 |
require_once 'EDD_SL_Plugin_Updater.php'; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Setup hooks |
| 65 |
* |
| 66 |
* @access private |
| 67 |
* @return void |
| 68 |
*/ |
| 69 |
private function _hooks() { |
| 70 |
|
| 71 |
// Add activation key message for plugins with missing keys |
| 72 |
add_action( 'load-plugins.php', array( $this, 'vimeography_check_for_missing_activation_keys' ) ); |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Activate the license key |
| 78 |
* |
| 79 |
* @access public |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function activate_license($license) { |
| 83 |
|
| 84 |
$key = str_replace('-', '', strtoupper( sanitize_text_field( $license ) ) ); |
| 85 |
|
| 86 |
// Ignore if this is a duplicate incoming key. |
| 87 |
if ( $this->vimeography_check_if_activation_key_exists( $key ) ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
// Data to send to the API |
| 92 |
$api_params = array( |
| 93 |
'edd_action' => 'activate_license', |
| 94 |
'license' => $key, |
| 95 |
//'item_name' => urlencode( $this->item_name ), // the name of our product in EDD **IMPORTANT need to set EDD_BYPASS_NAME_CHECK on vimeography.com to true if omitting |
| 96 |
'url' => urlencode( home_url() ), |
| 97 |
); |
| 98 |
|
| 99 |
// Call the API |
| 100 |
$response = wp_remote_get( |
| 101 |
add_query_arg( $api_params, $this->_endpoint), |
| 102 |
array( |
| 103 |
'timeout' => 15, |
| 104 |
'sslverify' => false |
| 105 |
) |
| 106 |
); |
| 107 |
|
| 108 |
// Make sure there are no errors |
| 109 |
if ( is_wp_error( $response ) ) { |
| 110 |
throw new Exception( __('The HTTP Request failed: ' . $response->get_error_message(), 'vimeography') ); |
| 111 |
} |
| 112 |
|
| 113 |
// Decode license data |
| 114 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 115 |
|
| 116 |
if ( $license_data->success AND $license_data->license == 'valid' ) { |
| 117 |
$this->_vimeography_add_activation_key( $key, $license_data ); |
| 118 |
return TRUE; |
| 119 |
} else { |
| 120 |
// Add failed message |
| 121 |
switch ($license_data->error) { |
| 122 |
case 'missing': case 'revoked': |
| 123 |
throw new Exception( __('That license key could not be found in our system.', 'vimeography') ); |
| 124 |
case 'no_activations_left': |
| 125 |
throw new Exception( __('You have reached the max number of sites that this license can be used on.', 'vimeography') ); |
| 126 |
case 'expired': |
| 127 |
throw new Exception( __('The license key you entered has expired. Please visit http://vimeography.com to renew it.', 'vimeography') ); |
| 128 |
case 'key_mismatch': |
| 129 |
throw new Exception( __('The license key you entered does not match the one we have on file.', 'vimeography') ); |
| 130 |
case 'license_not_activable': |
| 131 |
throw new Exception( __('Looks like you are trying to activate your bundle license. Please activate each of the products in your bundle separately by using their respective individual licenses.', 'vimeography') ); |
| 132 |
default: |
| 133 |
throw new Exception( __('Unknown error: ' . $license_data->error, 'vimeography') ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Deactivate the license key |
| 140 |
* |
| 141 |
* @access public |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
public function deactivate_license( $license ) { |
| 145 |
$key = str_replace('-', '', strtoupper( sanitize_text_field( $license ) ) ); |
| 146 |
|
| 147 |
// Data to send to the API |
| 148 |
$api_params = array( |
| 149 |
'edd_action' => 'deactivate_license', |
| 150 |
'license' => $key, |
| 151 |
//'item_name' => urlencode( $this->item_name ), // the name of our product in EDD |
| 152 |
'url' => urlencode( home_url() ) |
| 153 |
); |
| 154 |
|
| 155 |
// Call the API |
| 156 |
$response = wp_remote_get( |
| 157 |
add_query_arg( $api_params, $this->_endpoint ), |
| 158 |
array( |
| 159 |
'timeout' => 15, |
| 160 |
'sslverify' => false |
| 161 |
) |
| 162 |
); |
| 163 |
|
| 164 |
// Make sure there are no errors |
| 165 |
if ( is_wp_error( $response ) ) { |
| 166 |
return; |
| 167 |
} |
| 168 |
|
| 169 |
// Decode the license data |
| 170 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 171 |
|
| 172 |
// Remove the key even if deactivation fails |
| 173 |
$this->_vimeography_remove_activation_key( $key ); |
| 174 |
|
| 175 |
if ( ! $license_data->success ) { |
| 176 |
throw new Exception( __('That license key has been removed from your site, but could not be deactivated in our system.', 'vimeography') ); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* [check_license description] |
| 182 |
* @return [type] [description] |
| 183 |
*/ |
| 184 |
public function check_license( $license ) { |
| 185 |
|
| 186 |
// Data to send to the API |
| 187 |
$api_params = array( |
| 188 |
'edd_action' => 'check_license', |
| 189 |
'license' => $license->activation_key, |
| 190 |
//'item_name' => urlencode( $this->item_name ), // the name of our product in EDD |
| 191 |
'url' => urlencode( home_url() ) |
| 192 |
); |
| 193 |
|
| 194 |
// Call the API |
| 195 |
$response = wp_remote_get( |
| 196 |
add_query_arg( $api_params, $this->_endpoint ), |
| 197 |
array( |
| 198 |
'timeout' => 15, |
| 199 |
'sslverify' => false |
| 200 |
) |
| 201 |
); |
| 202 |
|
| 203 |
// Make sure there are no errors |
| 204 |
if ( is_wp_error( $response ) ) { |
| 205 |
return FALSE; |
| 206 |
} |
| 207 |
|
| 208 |
// Decode the license data |
| 209 |
return json_decode( wp_remote_retrieve_body( $response ) ); |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Add a plugins page message to any Vimeography add-ons that are installed, |
| 214 |
* but that do not have an activation key associated with the installation. |
| 215 |
* |
| 216 |
* @return void |
| 217 |
*/ |
| 218 |
public function vimeography_check_for_missing_activation_keys() { |
| 219 |
|
| 220 |
$addons = Vimeography::get_instance()->addons->installed_addons; |
| 221 |
|
| 222 |
if ( ! empty( $addons ) ) { |
| 223 |
// If the activation key is not found for the installed plugin, |
| 224 |
// add the plugin message hook |
| 225 |
$addons_with_missing_keys = array_filter($addons, array($this, 'vimeography_is_addon_missing_activation_key') ); |
| 226 |
|
| 227 |
if ( ! empty( $addons_with_missing_keys ) ) { |
| 228 |
foreach ( $addons_with_missing_keys as $plugin ) { |
| 229 |
$hook = 'after_plugin_row_' . $plugin['basename']; |
| 230 |
add_action( $hook, array($this, 'vimeography_addon_update_message'), 10, 2 ); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Loop through the activations keys to check if one exists for the |
| 238 |
* given Vimeography addon plugin headers. |
| 239 |
* |
| 240 |
* @var $plugin Meta headers for a Vimeography addon plugin. |
| 241 |
* @return bool TRUE if missing, FALSE if found |
| 242 |
*/ |
| 243 |
public function vimeography_is_addon_missing_activation_key($plugin) { |
| 244 |
$plugins_with_keys = array(); |
| 245 |
|
| 246 |
if ( ! empty($this->_activation_keys) ) { |
| 247 |
foreach ($this->_activation_keys as $key) { |
| 248 |
$plugins_with_keys[] = $key->plugin_name; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
return !in_array( $plugin['slug'], $plugins_with_keys ); |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Loop through the activations keys to check if one exists for the |
| 257 |
* given Vimeography activation key. |
| 258 |
* |
| 259 |
* @var $key string Activation key. |
| 260 |
* @return bool TRUE if exists, FALSE if not found |
| 261 |
*/ |
| 262 |
public function vimeography_check_if_activation_key_exists($key) { |
| 263 |
$result = FALSE; |
| 264 |
|
| 265 |
if ( ! empty($this->_activation_keys) ) { |
| 266 |
foreach ( $this->_activation_keys as $entry ) { |
| 267 |
if ( $entry->activation_key == $key ) { |
| 268 |
$result = TRUE; |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
return $result; |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* Add the activation key to the database. |
| 278 |
* |
| 279 |
* @var $key string Activation key. |
| 280 |
* @var $license_data array |
| 281 |
* @return bool TRUE if successful, FALSE if failed |
| 282 |
*/ |
| 283 |
protected function _vimeography_add_activation_key( $key, $license_data ) { |
| 284 |
$entry = new stdClass(); |
| 285 |
$entry->activation_key = $key; |
| 286 |
$entry->plugin_name = $license_data->vimeography_plugin_slug; |
| 287 |
$entry->product_name = $license_data->vimeography_product_name; |
| 288 |
$entry->expires = $license_data->expires; |
| 289 |
$entry->status = $license_data->license; |
| 290 |
$entry->limit = $license_data->license_limit; |
| 291 |
$entry->activations_left = $license_data->activations_left; |
| 292 |
|
| 293 |
$this->_activation_keys[] = $entry; |
| 294 |
return update_site_option('vimeography_activation_keys', array_values( $this->_activation_keys ) ); |
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Remove the activation key to the database. |
| 299 |
* |
| 300 |
* @var $key string Activation key. |
| 301 |
* @return bool TRUE if successful, FALSE if failed |
| 302 |
*/ |
| 303 |
protected function _vimeography_remove_activation_key( $key ) { |
| 304 |
if ( ! empty( $this->_activation_keys ) ) { |
| 305 |
foreach ( $this->_activation_keys as $i => $entry ) { |
| 306 |
if ( $entry->activation_key === $key ) { |
| 307 |
unset( $this->_activation_keys[$i] ); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
return update_site_option( 'vimeography_activation_keys', array_values( $this->_activation_keys ) ); |
| 312 |
} |
| 313 |
|
| 314 |
return FALSE; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Add a reminder to add the activation key to receives updates |
| 319 |
* for the installed Vimeography theme. |
| 320 |
* |
| 321 |
* @param string $plugin_basename Folder and filename, eg: |
| 322 |
* @return [type] [description] |
| 323 |
*/ |
| 324 |
public function vimeography_addon_update_message($plugin_basename, $plugin_data) { |
| 325 |
$ineligible = array( |
| 326 |
'Vimeography Theme: Bugsauce', |
| 327 |
'Vimeography Theme: Ballistic', |
| 328 |
'Vimeography Theme: Single' |
| 329 |
); |
| 330 |
|
| 331 |
if ( in_array( $plugin_data['Name'], $ineligible ) ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
echo '<tr class="plugin-update-tr"><td colspan="3" class="plugin-update"><div class="update-message notice inline notice-warning notice-alt">'; |
| 336 |
echo '<span style="display: block; padding: 5px;">'; |
| 337 |
printf( __('Hey! Don\'t forget to <a title="Activate my Vimeography Addon" href="%1$sadmin.php?page=vimeography-manage-activations">enter your activation key</a> to receive the latest updates for the %2$s plugin.', 'vimeography'), get_admin_url(), $plugin_data['Name'] ); |
| 338 |
echo '</span>'; |
| 339 |
echo '</div></td></tr>'; |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Auto updater |
| 344 |
* |
| 345 |
* @access protected |
| 346 |
* @return void |
| 347 |
*/ |
| 348 |
protected function _vimeography_auto_updater() { |
| 349 |
// We only need to check for updates if an activation key exists |
| 350 |
// in the options table. |
| 351 |
if ( ! empty( $this->_activation_keys ) ) { |
| 352 |
foreach ( $this->_activation_keys as $plugin ) { |
| 353 |
|
| 354 |
$plugin_path = self::_vimeography_get_plugin_path( $plugin->plugin_name ); |
| 355 |
|
| 356 |
if ( $plugin_path ) { |
| 357 |
// Get the plugin headers |
| 358 |
$headers = get_file_data( $plugin_path, array('version' => 'Version') ); |
| 359 |
|
| 360 |
// setup the updater |
| 361 |
new EDD_SL_Plugin_Updater( |
| 362 |
$this->_endpoint, |
| 363 |
$plugin_path, |
| 364 |
array( |
| 365 |
'version' => $headers['version'], |
| 366 |
'license' => trim($plugin->activation_key), |
| 367 |
'item_name' => $plugin->product_name, |
| 368 |
'author' => 'Dave Kiss', |
| 369 |
) |
| 370 |
); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Get the absolute path to the provided plugin name. |
| 378 |
* |
| 379 |
* @access protected |
| 380 |
* @return string |
| 381 |
*/ |
| 382 |
protected function _vimeography_get_plugin_path( $plugin_name ) { |
| 383 |
//return str_replace('vimeography/', trailingslashit($plugin_name), VIMEOGRAPHY_PATH); |
| 384 |
$basename = '/' . trailingslashit( $plugin_name ) . $plugin_name . '.php'; |
| 385 |
|
| 386 |
if ( ! is_file( $dir = WPMU_PLUGIN_DIR . $basename ) ) { |
| 387 |
if ( ! is_file( $dir = WP_PLUGIN_DIR . $basename ) ) { |
| 388 |
return FALSE; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
return $dir; |
| 393 |
} |
| 394 |
|
| 395 |
/** |
| 396 |
* Sets the activation keys in the class |
| 397 |
* |
| 398 |
* @param array $keys [description] |
| 399 |
* @return [type] [description] |
| 400 |
*/ |
| 401 |
public function vimeography_set_activation_keys($keys = array()) { |
| 402 |
$this->_activation_keys = $keys; |
| 403 |
return $this; |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
|