ad-health-notices.php
1 day ago
class-ad-network-ad-unit.php
5 months ago
class-ad-network.php
1 year ago
class-licenses.php
1 day ago
class-notices.php
1 day ago
class-overview-widgets.php
1 day ago
class-plugins-screen-updates.php
3 months ago
notices.php
1 day ago
class-licenses.php
364 lines
| 1 | <?php // phpcs:ignoreFile |
| 2 | /** |
| 3 | * Handle add-on licenses |
| 4 | * |
| 5 | * @package AdvancedAds |
| 6 | * @author Advanced Ads <info@wpadvancedads.com> |
| 7 | * @since 1.x.x |
| 8 | */ |
| 9 | |
| 10 | use AdvancedAds\Constants; |
| 11 | use AdvancedAds\Utilities\Data; |
| 12 | |
| 13 | defined( 'ABSPATH' ) || exit; |
| 14 | |
| 15 | /** |
| 16 | * Handle add-on licenses |
| 17 | */ |
| 18 | class Advanced_Ads_Admin_Licenses { |
| 19 | /** |
| 20 | * Advanced_Ads_Admin_Licenses constructor. |
| 21 | */ |
| 22 | private function __construct() { |
| 23 | add_action( 'plugins_loaded', [ $this, 'wp_plugins_loaded' ] ); |
| 24 | |
| 25 | // todo: check if this is loaded late enough and all add-ons are registered already. |
| 26 | add_filter( 'upgrader_pre_download', [ $this, 'addon_upgrade_filter' ], 10, 3 ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Actions and filter available after all plugins are initialized |
| 31 | */ |
| 32 | public function wp_plugins_loaded() { |
| 33 | add_action( 'http_api_debug', [ $this, 'update_license_after_version_info' ], 10, 5 ); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Return an instance of this class. |
| 38 | * |
| 39 | * @return self object A single instance of this class. |
| 40 | */ |
| 41 | public static function get_instance() { |
| 42 | static $instance; |
| 43 | |
| 44 | // If the single instance hasn't been set, set it now. |
| 45 | if ( null === $instance ) { |
| 46 | $instance = new self(); |
| 47 | } |
| 48 | |
| 49 | return $instance; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Get license keys for all add-ons |
| 54 | * |
| 55 | * @return string[] |
| 56 | */ |
| 57 | public function get_licenses() { |
| 58 | return \AdvancedAds\License\License::get_addon_key_map(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Deactivate license key |
| 63 | * |
| 64 | * @deprecated 2.0.21 Use {@see \AdvancedAds\License\License::deactivate_license_for_addon()} instead. |
| 65 | * |
| 66 | * @param string $addon Add-on identifier. |
| 67 | * @param string $plugin_name Name of the add-on. |
| 68 | * @param string $options_slug Slug of the option in the database. |
| 69 | * |
| 70 | * @return true|int|string |
| 71 | * @since 1.6.11 |
| 72 | */ |
| 73 | public function deactivate_license( $addon = '', $plugin_name = '', $options_slug = '' ) { |
| 74 | _deprecated_function( |
| 75 | __METHOD__, |
| 76 | '2.0.21', |
| 77 | '\AdvancedAds\License\License::deactivate_license_for_addon()' |
| 78 | ); |
| 79 | |
| 80 | unset( $plugin_name, $options_slug ); |
| 81 | |
| 82 | return \AdvancedAds\License\License::deactivate_license_for_addon( (string) $addon ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Save license keys for all add-ons |
| 87 | * |
| 88 | * @param array $licenses licenses. |
| 89 | */ |
| 90 | public function save_licenses( $licenses = [] ) { |
| 91 | if ( ! is_array( $licenses ) ) { |
| 92 | $licenses = []; |
| 93 | } |
| 94 | \AdvancedAds\License\License::persist_addon_key_map( $licenses ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get license status of an add-on |
| 99 | * |
| 100 | * @param string $slug slug of the add-on. |
| 101 | * |
| 102 | * @return string|false license status, "valid", "invalid" or false if option doesn't exist. |
| 103 | */ |
| 104 | public function get_license_status( $slug = '' ) { |
| 105 | return \AdvancedAds\License\License::get_mirror_status_for_options_slug( $slug ); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * If two or more add-ons use the same valid license this is probably an all-access customer |
| 110 | * |
| 111 | * @return bool |
| 112 | */ |
| 113 | public function get_probably_all_access() { |
| 114 | $valid = array_filter( |
| 115 | $this->get_licenses(), |
| 116 | function ( $key ) { |
| 117 | return $this->get_license_status( ADVADS_SLUG . '-' . $key ); |
| 118 | }, |
| 119 | ARRAY_FILTER_USE_KEY |
| 120 | ); |
| 121 | |
| 122 | return [] !== $valid && max( array_count_values( $valid ) ) > 1; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Return the licence expiry time if it is equal for more than one add-on. That indicates it is likely an All Access license |
| 127 | * |
| 128 | * @return string|null |
| 129 | */ |
| 130 | public function get_probably_all_access_expiry() { |
| 131 | /** |
| 132 | * Get expiry dates of all add-ons. |
| 133 | * |
| 134 | * @param string $key Add-on key. |
| 135 | * |
| 136 | * @return string|false the expiration date or false. |
| 137 | */ |
| 138 | $expiry_counts = array_count_values( |
| 139 | array_map( |
| 140 | function ( $key ) { |
| 141 | return $this->get_license_expires( ADVADS_SLUG . '-' . $key ); |
| 142 | }, |
| 143 | array_keys( array_filter( $this->get_licenses() ) ) |
| 144 | ) |
| 145 | ); |
| 146 | |
| 147 | /** |
| 148 | * Remove all licenses that are used only once. |
| 149 | * |
| 150 | * @param int $count the count from array_count_values_above |
| 151 | * |
| 152 | * @return bool whether the count is greater 1 |
| 153 | */ |
| 154 | $all_access = array_filter( |
| 155 | $expiry_counts, |
| 156 | function ( $count ) { |
| 157 | return $count > 1; |
| 158 | } |
| 159 | ); |
| 160 | |
| 161 | // if there is an item in $all_access we can assume this is from All Access and return the expiry date. |
| 162 | return empty( $all_access ) ? null : key( $all_access ); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Get license expired value of an add-on |
| 167 | * |
| 168 | * @param string $slug slug of the add-on. |
| 169 | * |
| 170 | * @return string $date expiry date of an add-on, empty string if no option exists |
| 171 | */ |
| 172 | public function get_license_expires( $slug = '' ) { |
| 173 | return \AdvancedAds\License\License::get_mirror_expires_for_options_slug( $slug ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Add custom messages to plugin updater |
| 178 | * |
| 179 | * @param bool $reply Whether to bail without returning the package. Default false. |
| 180 | * @param string $package The package file name. |
| 181 | * @param WP_Upgrader $updater The WP_Upgrader instance. |
| 182 | * |
| 183 | * @return string |
| 184 | * |
| 185 | * @todo check if this is still working. |
| 186 | */ |
| 187 | public function addon_upgrade_filter( $reply, $package, $updater ) { |
| 188 | $key = null; |
| 189 | $value = null; |
| 190 | |
| 191 | if ( isset( $updater->skin->plugin ) ) { |
| 192 | $key = 'path'; |
| 193 | $value = $updater->skin->plugin; |
| 194 | } elseif ( isset( $updater->skin->plugin_info['Name'] ) ) { |
| 195 | $key = 'name'; |
| 196 | $value = $updater->skin->plugin_info['Name']; |
| 197 | } |
| 198 | |
| 199 | $add_on = $this->get_installed_add_on_by_key( $key, $value ); |
| 200 | if ( ! $add_on || ! isset( $add_on['path'] ) ) { |
| 201 | return $reply; |
| 202 | } |
| 203 | |
| 204 | $plugin_file = plugin_basename( $add_on['path'] ); |
| 205 | if ( wp_doing_ajax() ) { |
| 206 | $update_link = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $plugin_file, 'upgrade-plugin_' . $plugin_file ); |
| 207 | /* translators: %s plugin update link */ |
| 208 | $updater->strings['download_failed'] = sprintf( __( 'Download failed. <a href="%s">Click here to try another method</a>.', 'advanced-ads' ), $update_link ); |
| 209 | } else { |
| 210 | /* translators: %s download failed knowledgebase link */ |
| 211 | $updater->strings['download_failed'] = sprintf( __( 'Download failed. <a href="%s" target="_blank">Click here to learn why</a>.', 'advanced-ads' ), 'https://wpadvancedads.com/manual/download-failed-updating-add-ons/#utm_source=advanced-ads&utm_medium=link&utm_campaign=download-failed' ); |
| 212 | } |
| 213 | |
| 214 | return $reply; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Search if a name is in the add-on array and return the add-on data of it |
| 219 | * |
| 220 | * @param string $key key to search for. |
| 221 | * @param string $value value to search for. |
| 222 | * |
| 223 | * @return array array with the add-on data |
| 224 | */ |
| 225 | private function get_installed_add_on_by_key( $key, $value ) { |
| 226 | // Early bail!! |
| 227 | if ( empty( $key ) || empty( $value ) ) { |
| 228 | return null; |
| 229 | } |
| 230 | |
| 231 | $add_ons = Data::get_addons(); |
| 232 | if ( is_array( $add_ons ) ) { |
| 233 | foreach ( $add_ons as $add_on ) { |
| 234 | if ( $add_on[ $key ] === $value ) { |
| 235 | return $add_on; |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | return null; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Check if any license is valid |
| 245 | * can be used to display information for any Pro user only, like link to direct support |
| 246 | */ |
| 247 | public static function any_license_valid() { |
| 248 | $add_ons = Data::get_addons(); |
| 249 | if ( [] === $add_ons ) { |
| 250 | return false; |
| 251 | } |
| 252 | |
| 253 | foreach ( $add_ons as $_add_on ) { |
| 254 | if ( 'slider-ads' === $_add_on['id'] ) { |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | $status = self::get_instance()->get_license_status( $_add_on['options_slug'] ); |
| 259 | |
| 260 | // check expiry date. |
| 261 | $expiry_date = self::get_instance()->get_license_expires( $_add_on['options_slug'] ); |
| 262 | |
| 263 | if ( |
| 264 | ( $expiry_date && strtotime( $expiry_date ) > time() ) |
| 265 | || 'valid' === $status || 'lifetime' === $expiry_date |
| 266 | ) { |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Update the license status based on information retrieved from the version info check |
| 276 | * |
| 277 | * @param array|WP_Error $response HTTP response or WP_Error object. |
| 278 | * @param string $context Context under which the hook is fired. |
| 279 | * @param string $http HTTP transport used. |
| 280 | * @param array $parsed_args HTTP request arguments. |
| 281 | * @param string $url The request URL. |
| 282 | * @return array|WP_Error |
| 283 | */ |
| 284 | public function update_license_after_version_info( $response, $context, $http, $parsed_args, $url ) { |
| 285 | // Early bail!! |
| 286 | if ( |
| 287 | Constants::API_ENDPOINT !== $url |
| 288 | || ( |
| 289 | empty( $parsed_args['body']['edd_action'] ) |
| 290 | || 'get_version' !== $parsed_args['body']['edd_action'] |
| 291 | ) |
| 292 | || is_wp_error( $response ) |
| 293 | ) { |
| 294 | return $response; |
| 295 | } |
| 296 | |
| 297 | $params = json_decode( wp_remote_retrieve_body( $response ) ); |
| 298 | if ( empty( $params->name ) ) { |
| 299 | return $response; |
| 300 | } |
| 301 | |
| 302 | $new_license_status = null; |
| 303 | $new_expiry_date = null; |
| 304 | |
| 305 | // Some of the conditions could happen at the same time, |
| 306 | // though due to different conditions in EDD we are safer to have multiple checks. |
| 307 | if ( isset( $params->valid_until ) ) { |
| 308 | if ( 'invalid' === $params->valid_until ) { |
| 309 | $new_license_status = 'invalid'; |
| 310 | } |
| 311 | if ( 'lifetime' === $params->valid_until ) { |
| 312 | $new_license_status = 'valid'; |
| 313 | $new_expiry_date = 'lifetime'; |
| 314 | } |
| 315 | |
| 316 | if ( is_int( $params->valid_until ) ) { |
| 317 | $new_expiry_date = (int) $params->valid_until; |
| 318 | if ( time() < $params->valid_until ) { |
| 319 | $new_license_status = 'valid'; |
| 320 | } |
| 321 | } |
| 322 | } elseif ( empty( $params->download_link ) || empty( $params->package ) || isset( $params->msg ) ) { |
| 323 | // If either of these two parameters is missing then the user does not have a valid license according to our store |
| 324 | // If there is a "msg" parameter then the license did also not work for another reason. |
| 325 | $new_license_status = 'invalid'; |
| 326 | } |
| 327 | |
| 328 | if ( ! $new_license_status && ! $new_expiry_date ) { |
| 329 | return $response; |
| 330 | } |
| 331 | |
| 332 | if ( \AdvancedAds\License\License::is_flat_map_retired() ) { |
| 333 | return $response; |
| 334 | } |
| 335 | |
| 336 | $add_ons = Data::get_addons(); |
| 337 | |
| 338 | // Look for the add-on with the appropriate license key. |
| 339 | foreach ( $add_ons as $_add_on ) { |
| 340 | if ( ! isset( $_add_on['name'] ) || $params->name !== $_add_on['name'] ) { |
| 341 | continue; |
| 342 | } |
| 343 | |
| 344 | $options_slug = $_add_on['options_slug']; |
| 345 | |
| 346 | if ( $new_license_status ) { |
| 347 | update_option( $options_slug . '-license-status', $new_license_status, false ); |
| 348 | } |
| 349 | |
| 350 | if ( $new_expiry_date ) { |
| 351 | if ( 'lifetime' !== $new_expiry_date ) { |
| 352 | $new_expiry_date = gmdate( 'Y-m-d 23:59:49', $new_expiry_date ); |
| 353 | } |
| 354 | update_option( $options_slug . '-license-expires', $new_expiry_date, false ); |
| 355 | } |
| 356 | |
| 357 | // Return with the first match since there should only be one plugin per name. |
| 358 | return $response; |
| 359 | } |
| 360 | |
| 361 | return $response; |
| 362 | } |
| 363 | } |
| 364 |