class-ad-groups-list.php
9 years ago
class-ad-type.php
9 years ago
class-licenses.php
9 years ago
class-menu.php
9 years ago
class-meta-box.php
9 years ago
class-notices.php
9 years ago
class-options.php
9 years ago
class-overview-widgets.php
9 years ago
class-settings.php
9 years ago
class-shortcode-creator.php
9 years ago
notices.php
9 years ago
shortcode-creator-l10n.php
10 years ago
class-licenses.php
506 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || exit; |
| 3 | |
| 4 | /** |
| 5 | * handle add-on licenses |
| 6 | */ |
| 7 | class Advanced_Ads_Admin_Licenses { |
| 8 | /** |
| 9 | * Instance of this class. |
| 10 | * |
| 11 | * @var object |
| 12 | */ |
| 13 | protected static $instance = null; |
| 14 | |
| 15 | private function __construct() { |
| 16 | if ( ! defined( 'DOING_AJAX' ) ) { |
| 17 | add_action( 'plugins_loaded', array( $this, 'wp_plugins_loaded' ) ); |
| 18 | add_action( 'load-plugins.php', array( $this, 'check_plugin_licenses' ) ); |
| 19 | } |
| 20 | |
| 21 | // todo: check if this is loaded late enough and all add-ons are registered already |
| 22 | add_filter( 'upgrader_pre_download', array( $this, 'addon_upgrade_filter' ), 10, 3 ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * actions and filter available after all plugins are initialized |
| 27 | */ |
| 28 | public function wp_plugins_loaded() { |
| 29 | |
| 30 | // check for add-on updates |
| 31 | add_action( 'admin_init', array($this, 'add_on_updater'), 1 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Return an instance of this class. |
| 36 | * |
| 37 | * @return object A single instance of this class. |
| 38 | */ |
| 39 | public static function get_instance() { |
| 40 | // If the single instance hasn't been set, set it now. |
| 41 | if ( null == self::$instance ) { |
| 42 | self::$instance = new self; |
| 43 | } |
| 44 | |
| 45 | return self::$instance; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * initiate plugin checks |
| 50 | * |
| 51 | * @since 1.7.12 |
| 52 | */ |
| 53 | public function check_plugin_licenses(){ |
| 54 | |
| 55 | if( is_multisite() ){ |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | // gather all add-on plugin files |
| 60 | $add_ons = apply_filters( 'advanced-ads-add-ons', array() ); |
| 61 | foreach( $add_ons as $_add_on ){ |
| 62 | |
| 63 | // check license status |
| 64 | if( $this->get_license_status( $_add_on['options_slug'] ) !== 'valid' ) { |
| 65 | // register warning |
| 66 | $plugin_file = plugin_basename( $_add_on['path'] ); |
| 67 | add_action( 'after_plugin_row_' . $plugin_file, array( $this, 'add_plugin_list_license_notice'), 10, 3 ); |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * add a row below add-ons with an invalid license on the plugin list |
| 74 | * |
| 75 | * @since 1.7.12 |
| 76 | * @param string $plugin_file Path to the plugin file, relative to the plugins directory. |
| 77 | * @param array $plugin_data An array of plugin data. |
| 78 | * @param string $status Status of the plugin. Defaults are 'All', 'Active', |
| 79 | * 'Inactive', 'Recently Activated', 'Upgrade', 'Must-Use', |
| 80 | * 'Drop-ins', 'Search'. |
| 81 | * @todo make this work on multisite as well |
| 82 | */ |
| 83 | public function add_plugin_list_license_notice( $plugin_file, $plugin_data, $status ){ |
| 84 | |
| 85 | echo '<tr class="advads-plugin-update-tr plugin-update-tr active"><td class="plugin-update colspanchange" colspan="3"><div class="update-message notice inline notice-warning notice-alt"><p>' |
| 86 | . sprintf( __( 'There might be a new version of %1$s. Please <strong>provide a valid license key</strong> in order to receive updates and support <a href="%2$s">on this page</a>.', 'advanced-ads' ), $plugin_data['Title'], admin_url( 'admin.php?page=advanced-ads-settings#top#licenses' ) ) |
| 87 | . '</p></div></td></tr>'; |
| 88 | |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * save license key |
| 94 | * |
| 95 | * @since 1.2.0 |
| 96 | * @param string $addon string with addon identifier |
| 97 | */ |
| 98 | public function activate_license( $addon = '', $plugin_name = '', $options_slug = '', $license_key = '' ) { |
| 99 | |
| 100 | if ( '' === $addon || '' === $plugin_name || '' === $options_slug ) { |
| 101 | return __( 'Error while trying to register the license. Please contact support.', 'advanced-ads' ); |
| 102 | } |
| 103 | |
| 104 | $license_key = esc_attr( trim( $license_key ) ); |
| 105 | if ( '' == $license_key ) { |
| 106 | return __( 'Please enter a valid license key', 'advanced-ads' ); |
| 107 | } |
| 108 | |
| 109 | if ( has_filter( 'advanced_ads_license_'. $options_slug ) ) { |
| 110 | return apply_filters( 'advanced_ads_license_' . $options_slug, false, __METHOD__, $plugin_name, $options_slug, $license_key ); |
| 111 | } |
| 112 | |
| 113 | $api_params = array( |
| 114 | 'edd_action'=> 'activate_license', |
| 115 | 'license' => $license_key, |
| 116 | 'item_name' => urlencode( $plugin_name ), |
| 117 | 'url' => home_url() |
| 118 | ); |
| 119 | // Call the custom API. |
| 120 | $response = wp_remote_post( ADVADS_URL, array( |
| 121 | 'timeout' => 15, |
| 122 | 'sslverify' => false, |
| 123 | 'body' => $api_params |
| 124 | ) ); |
| 125 | |
| 126 | if ( is_wp_error( $response ) ) { |
| 127 | $body = wp_remote_retrieve_body( $response ); |
| 128 | if ( $body ){ |
| 129 | return $body; |
| 130 | } else { |
| 131 | // return print_r($response, true); |
| 132 | return __( 'License couldn’t be activated. Please try again later.', 'advanced-ads' ); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 137 | // save license status |
| 138 | update_option($options_slug . '-license-status', $license_data->license, false); |
| 139 | if( !empty( $license_data->expires ) ){ |
| 140 | update_option($options_slug . '-license-expires', $license_data->expires, false); |
| 141 | } |
| 142 | |
| 143 | // display activation problem |
| 144 | if( !empty( $license_data->error )) { |
| 145 | // user friendly texts for errors |
| 146 | $errors = array( |
| 147 | 'license_not_activable' => __( 'This is the bundle license key.', 'advanced-ads' ), |
| 148 | 'item_name_mismatch' => __( 'This is not the correct key for this add-on.', 'advanced-ads' ), |
| 149 | 'no_activations_left' => __( 'There are no activations left.', 'advanced-ads' ) |
| 150 | ); |
| 151 | $error = isset( $errors[ $license_data->error ] ) ? $errors[ $license_data->error ] : $license_data->error; |
| 152 | if( 'expired' === $license_data->error ){ |
| 153 | return 'ex'; |
| 154 | } else { |
| 155 | if( isset($errors[ $license_data->error ] ) ) { |
| 156 | return $error; |
| 157 | } else { |
| 158 | return sprintf( __( 'License is invalid. Reason: %s', 'advanced-ads' ), $error); |
| 159 | } |
| 160 | } |
| 161 | } else { |
| 162 | // reset license_expires admin notification |
| 163 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_expires' ); |
| 164 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_expired' ); |
| 165 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_invalid' ); |
| 166 | // save license key |
| 167 | $licenses = $this->get_licenses(); |
| 168 | $licenses[ $addon ] = $license_key; |
| 169 | $this->save_licenses( $licenses ); |
| 170 | } |
| 171 | |
| 172 | return 1; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * check if a specific license key was already activated for the current page |
| 177 | * |
| 178 | * @since 1.6.17 |
| 179 | * @return bool true if already activated |
| 180 | * @deprecated since version 1.7.2 because it only checks if a key is valid, not if the url registered with that key |
| 181 | */ |
| 182 | public function check_license( $license_key = '', $plugin_name = '', $options_slug = '' ){ |
| 183 | |
| 184 | if ( has_filter( 'advanced_ads_license_'. $options_slug ) ) { |
| 185 | return apply_filters( 'advanced_ads_license_' . $options_slug, false, __METHOD__, $plugin_name, $options_slug, $license_key ); |
| 186 | } |
| 187 | |
| 188 | $api_params = array( |
| 189 | 'edd_action' => 'check_license', |
| 190 | 'license' => $license_key, |
| 191 | 'item_name' => urlencode( $plugin_name ) |
| 192 | ); |
| 193 | $response = wp_remote_get( add_query_arg( $api_params, ADVADS_URL ), array( 'timeout' => 15, 'sslverify' => false ) ); |
| 194 | if ( is_wp_error( $response ) ) { |
| 195 | return false; |
| 196 | } |
| 197 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 198 | |
| 199 | // if this license is still valid |
| 200 | if( $license_data->license == 'valid' ) { |
| 201 | update_option($options_slug . '-license-expires', $license_data->expires, false); |
| 202 | update_option($options_slug . '-license-status', $license_data->license, false); |
| 203 | |
| 204 | return true; |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * deactivate license key |
| 211 | * |
| 212 | * @since 1.6.11 |
| 213 | * @param string $addon string with addon identifier |
| 214 | */ |
| 215 | public function deactivate_license( $addon = '', $plugin_name = '', $options_slug = '' ) { |
| 216 | |
| 217 | if ( '' === $addon || '' === $plugin_name || '' === $options_slug ) { |
| 218 | return __( 'Error while trying to disable the license. Please contact support.', 'advanced-ads' ); |
| 219 | } |
| 220 | |
| 221 | $licenses = $this->get_licenses(); |
| 222 | $license_key = isset($licenses[$addon]) ? $licenses[$addon] : ''; |
| 223 | |
| 224 | if ( has_filter( 'advanced_ads_license_'. $options_slug ) ) { |
| 225 | return apply_filters( 'advanced_ads_license_' . $options_slug, false, __METHOD__, $plugin_name, $options_slug, $license_key ); |
| 226 | } |
| 227 | |
| 228 | $api_params = array( |
| 229 | 'edd_action' => 'deactivate_license', |
| 230 | 'license' => $license_key, |
| 231 | 'item_name' => urlencode( $plugin_name ) |
| 232 | ); |
| 233 | // Send the remote request |
| 234 | $response = wp_remote_post( ADVADS_URL, array( |
| 235 | 'body' => $api_params, |
| 236 | 'timeout' => 15, |
| 237 | 'sslverify' => false, |
| 238 | ) ); |
| 239 | |
| 240 | if ( is_wp_error( $response ) ) { |
| 241 | $body = wp_remote_retrieve_body( $response ); |
| 242 | if ( $body ){ |
| 243 | return $body; |
| 244 | } else { |
| 245 | return __( 'License couldn’t be deactivated. Please try again later.', 'advanced-ads' ); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 250 | |
| 251 | // save license status |
| 252 | |
| 253 | // remove data |
| 254 | if( 'deactivated' === $license_data->license ) { |
| 255 | delete_option( $options_slug . '-license-status' ); |
| 256 | delete_option( $options_slug . '-license-expires' ); |
| 257 | Advanced_Ads_Admin_Notices::get_instance()->remove_from_queue( 'license_expires' ); |
| 258 | } elseif( 'failed' === $license_data->license ) { |
| 259 | update_option($options_slug . '-license-expires', $license_data->expires, false); |
| 260 | update_option($options_slug . '-license-status', $license_data->license, false); |
| 261 | return 'ex'; |
| 262 | } else { |
| 263 | return __( 'License couldn’t be deactivated. Please try again later.', 'advanced-ads' ); |
| 264 | } |
| 265 | |
| 266 | return 1; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * get license keys for all add-ons |
| 271 | * |
| 272 | * @since 1.6.15 |
| 273 | * @return arr $licenses licenses |
| 274 | */ |
| 275 | public function get_licenses(){ |
| 276 | |
| 277 | $licenses = array(); |
| 278 | |
| 279 | if( is_multisite() ){ |
| 280 | // if multisite, get option from main blog |
| 281 | global $current_site; |
| 282 | $licenses = get_blog_option( $current_site->blog_id, ADVADS_SLUG . '-licenses', array() ); |
| 283 | |
| 284 | } else { |
| 285 | $licenses = get_option( ADVADS_SLUG . '-licenses', array() ); |
| 286 | } |
| 287 | |
| 288 | return $licenses; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * save license keys for all add-ons |
| 293 | * |
| 294 | * @since 1.7.2 |
| 295 | * @return arr $licenses licenses |
| 296 | */ |
| 297 | public function save_licenses( $licenses = array() ){ |
| 298 | |
| 299 | if( is_multisite() ){ |
| 300 | // if multisite, get option from main blog |
| 301 | global $current_site; |
| 302 | update_blog_option( $current_site->blog_id, ADVADS_SLUG . '-licenses', $licenses ); |
| 303 | } else { |
| 304 | update_option( ADVADS_SLUG . '-licenses', $licenses ); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * get license status of an add-on |
| 310 | * |
| 311 | * @since 1.6.15 |
| 312 | * @param str $slug slug of the add-on |
| 313 | * @return str $status license status, e.g. "valid" or "invalid" |
| 314 | */ |
| 315 | public function get_license_status( $slug = '' ){ |
| 316 | |
| 317 | $status = false; |
| 318 | |
| 319 | if( is_multisite() ){ |
| 320 | // if multisite, get option from main blog |
| 321 | global $current_site; |
| 322 | $status = get_blog_option( $current_site->blog_id, $slug . '-license-status', false); |
| 323 | } else { |
| 324 | $status = get_option( $slug . '-license-status', false); |
| 325 | } |
| 326 | |
| 327 | return $status; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * get license expired value of an add-on |
| 332 | * |
| 333 | * @since 1.6.15 |
| 334 | * @param str $slug slug of the add-on |
| 335 | * @return str $date expiry date of an add-on |
| 336 | */ |
| 337 | public function get_license_expires( $slug = '' ){ |
| 338 | |
| 339 | $date = false; |
| 340 | |
| 341 | if( is_multisite() ){ |
| 342 | // if multisite, get option from main blog |
| 343 | global $current_site; |
| 344 | $date = get_blog_option( $current_site->blog_id, $slug . '-license-expires', false); |
| 345 | } else { |
| 346 | $date = get_option( $slug . '-license-expires', false); |
| 347 | } |
| 348 | |
| 349 | return $date; |
| 350 | } |
| 351 | |
| 352 | |
| 353 | /* |
| 354 | * add-on updater |
| 355 | * |
| 356 | * @since 1.5.7 |
| 357 | */ |
| 358 | public function add_on_updater(){ |
| 359 | |
| 360 | // ignore, if not main blog or is ajax |
| 361 | if( ( is_multisite() && ! is_main_site() ) ){ |
| 362 | return; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * list of registered add ons |
| 367 | * contains: |
| 368 | * name |
| 369 | * version |
| 370 | * path |
| 371 | * options_slug |
| 372 | * short option slug (=key) |
| 373 | */ |
| 374 | $add_ons = apply_filters( 'advanced-ads-add-ons', array() ); |
| 375 | |
| 376 | if( $add_ons === array() ) { |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | // load license keys |
| 381 | $licenses = get_option(ADVADS_SLUG . '-licenses', array()); |
| 382 | |
| 383 | foreach( $add_ons as $_add_on_key => $_add_on ){ |
| 384 | |
| 385 | // check if a license expired over time |
| 386 | $expiry_date = $this->get_license_expires( $_add_on['options_slug'] ); |
| 387 | $now = time(); |
| 388 | if( $expiry_date && 'lifetime' !== $expiry_date && strtotime( $expiry_date ) < $now ){ |
| 389 | // remove license status |
| 390 | delete_option( $_add_on['options_slug'] . '-license-status' ); |
| 391 | continue; |
| 392 | } |
| 393 | |
| 394 | // check status |
| 395 | if( $this->get_license_status( $_add_on['options_slug'] ) !== 'valid' ) { |
| 396 | continue; |
| 397 | } |
| 398 | |
| 399 | // retrieve our license key |
| 400 | $license_key = isset($licenses[$_add_on_key]) ? $licenses[$_add_on_key] : ''; |
| 401 | |
| 402 | // setup the updater |
| 403 | if( $license_key ){ |
| 404 | |
| 405 | // register filter to set EDD transient to 86,400 seconds (day) instead of 3,600 (hours) |
| 406 | $slug = basename( $_add_on['path'], '.php' ); |
| 407 | $transient_key = md5( serialize( $slug . $license_key ) ); |
| 408 | |
| 409 | // add_filter( 'expiration_of_transient_' . $transient_key, array( $this, 'set_expiration_of_update_transient' ) ); |
| 410 | add_filter( 'pre_update_option_' . $transient_key, array( $this, 'set_expiration_of_update_option' ) ); |
| 411 | |
| 412 | new EDD_SL_Plugin_Updater( ADVADS_URL, $_add_on['path'], array( |
| 413 | 'version' => $_add_on['version'], |
| 414 | 'license' => $license_key, |
| 415 | 'item_name' => $_add_on['name'], |
| 416 | 'author' => 'Thomas Maier' |
| 417 | ) |
| 418 | ); |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * set the expiration of the updater transient key to 1 day instead of 1 hour to prevent too many update checks |
| 425 | * |
| 426 | * @deprecated since version 1.7.14 – not using transient anymore, but option |
| 427 | */ |
| 428 | public function set_expiration_of_update_transient( $expiration ){ |
| 429 | |
| 430 | return 86400; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * set the expiration of the updater transient key to 1 day instead of 1 hour to prevent too many update checks |
| 435 | * |
| 436 | * @since 1.7.14 |
| 437 | */ |
| 438 | public function set_expiration_of_update_option( $value ){ |
| 439 | |
| 440 | $value['timeout'] = time() + 86400; |
| 441 | |
| 442 | return $value; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * add custom messages to plugin updater |
| 447 | * |
| 448 | * @param type $reply |
| 449 | * @param type $package |
| 450 | * @param type $updater |
| 451 | * @return type |
| 452 | */ |
| 453 | public function addon_upgrade_filter( $reply, $package, $updater ) { |
| 454 | |
| 455 | if( isset( $updater->skin->plugin ) ){ |
| 456 | $plugin_file = $updater->skin->plugin; |
| 457 | } elseif ( isset( $updater->skin->plugin_info['Name'] ) ){ |
| 458 | $add_on = $this->get_installed_add_on_by_name( $updater->skin->plugin_info['Name'] ); |
| 459 | $plugin_file = plugin_basename( $add_on['path'] ); |
| 460 | } |
| 461 | |
| 462 | if( isset( $plugin_file ) && $plugin_file ){ |
| 463 | // hides the download url, but makes debugging harder |
| 464 | // $updater->strings['downloading_package'] = __( 'Downloading updated version...', 'advanced-ads' ); |
| 465 | //$updater->skin->feedback( 'downloading_package' ); |
| 466 | |
| 467 | // if AJAX; show direct update link as first possible solution |
| 468 | if( defined( 'DOING_AJAX' ) ){ |
| 469 | $update_link = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $plugin_file, 'upgrade-plugin_' . $plugin_file ); |
| 470 | $updater->strings['download_failed'] = sprintf(__( 'Download failed. <a href="%s">Click here to try another method</a>.', 'advanced-ads' ), $update_link ); |
| 471 | } else { |
| 472 | $updater->strings['download_failed'] = sprintf(__( 'Download failed. <a href="%s" target="_blank">Click here to learn why</a>.', 'advanced-ads' ), ADVADS_URL . 'manual/download-failed-updating-add-ons/#utm_source=advanced-ads&utm_medium=link&utm_campaign=download-failed' ); |
| 473 | } |
| 474 | |
| 475 | } |
| 476 | |
| 477 | /*$res = $updater->fs_connect( array( WP_CONTENT_DIR ) ); |
| 478 | if ( ! $res ) { |
| 479 | return new WP_Error( 'no_credentials', __( "Error! Can't connect to filesystem", 'advanced-ads' ) ); |
| 480 | }*/ |
| 481 | |
| 482 | return $reply; |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * search if a name is in the add-on array and return the add-on data of it |
| 487 | * |
| 488 | * @param str $name name of an add-on |
| 489 | * @return arr array with the add-on data |
| 490 | */ |
| 491 | private function get_installed_add_on_by_name( $name = '' ){ |
| 492 | |
| 493 | $add_ons = apply_filters( 'advanced-ads-add-ons', array() ); |
| 494 | |
| 495 | if( is_array( $add_ons ) ) { |
| 496 | foreach ( $add_ons as $key => $_add_on ) { |
| 497 | if ($_add_on['name'] === $name ) { |
| 498 | return $_add_on; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | return null; |
| 503 | } |
| 504 | |
| 505 | |
| 506 | } |