ad-health-notices.php
1 month ago
class-ad-network-ad-unit.php
3 weeks ago
class-ad-network.php
3 weeks ago
class-licenses.php
3 weeks ago
class-notices.php
3 weeks ago
class-overview-widgets.php
3 weeks ago
class-plugins-screen-updates.php
3 weeks ago
notices.php
3 weeks ago
class-licenses.php
303 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\Addons; |
| 12 | use AdvancedAds\Utilities\Data; |
| 13 | |
| 14 | defined( 'ABSPATH' ) || exit; |
| 15 | |
| 16 | /** |
| 17 | * Handle add-on licenses |
| 18 | */ |
| 19 | class Advanced_Ads_Admin_Licenses { |
| 20 | /** |
| 21 | * Advanced_Ads_Admin_Licenses constructor. |
| 22 | */ |
| 23 | private function __construct() { |
| 24 | add_action( 'plugins_loaded', [ $this, 'wp_plugins_loaded' ] ); |
| 25 | |
| 26 | // todo: check if this is loaded late enough and all add-ons are registered already. |
| 27 | add_filter( 'upgrader_pre_download', [ $this, 'addon_upgrade_filter' ], 10, 3 ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Actions and filter available after all plugins are initialized |
| 32 | */ |
| 33 | public function wp_plugins_loaded() { |
| 34 | add_action( 'http_api_debug', [ $this, 'update_license_after_version_info' ], 10, 5 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Return an instance of this class. |
| 39 | * |
| 40 | * @return self object A single instance of this class. |
| 41 | */ |
| 42 | public static function get_instance() { |
| 43 | static $instance; |
| 44 | |
| 45 | // If the single instance hasn't been set, set it now. |
| 46 | if ( null === $instance ) { |
| 47 | $instance = new self(); |
| 48 | } |
| 49 | |
| 50 | return $instance; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Get license keys for all add-ons |
| 55 | * |
| 56 | * @return string[] |
| 57 | */ |
| 58 | public function get_licenses() { |
| 59 | return \AdvancedAds\License\License::get_addon_key_map(); |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Deactivate license key |
| 64 | * |
| 65 | * @deprecated 2.0.21 Use {@see \AdvancedAds\License\License::deactivate_license_for_addon()} instead. |
| 66 | * |
| 67 | * @param string $addon Add-on identifier. |
| 68 | * @param string $plugin_name Name of the add-on. |
| 69 | * @param string $options_slug Slug of the option in the database. |
| 70 | * |
| 71 | * @return true|int|string |
| 72 | * @since 1.6.11 |
| 73 | */ |
| 74 | public function deactivate_license( $addon = '', $plugin_name = '', $options_slug = '' ) { |
| 75 | _deprecated_function( |
| 76 | __METHOD__, |
| 77 | '2.0.21', |
| 78 | '\AdvancedAds\License\License::deactivate_license_for_addon()' |
| 79 | ); |
| 80 | |
| 81 | unset( $plugin_name, $options_slug ); |
| 82 | |
| 83 | return \AdvancedAds\License\License::deactivate_license_for_addon( (string) $addon ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Save license keys for all add-ons |
| 88 | * |
| 89 | * @param array<string, string> $licenses Licenses keyed by add-on identifier. |
| 90 | */ |
| 91 | public function save_licenses( $licenses = [] ) { |
| 92 | if ( ! is_array( $licenses ) ) { |
| 93 | $licenses = []; |
| 94 | } |
| 95 | \AdvancedAds\License\License::persist_addon_key_map( $licenses ); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Get license status of an add-on |
| 100 | * |
| 101 | * @param string $slug slug of the add-on. |
| 102 | * |
| 103 | * @return string|false license status, "valid", "invalid" or false if option doesn't exist. |
| 104 | */ |
| 105 | public function get_license_status( $slug = '' ) { |
| 106 | return \AdvancedAds\License\License::get_mirror_status_for_options_slug( $slug ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * If two or more add-ons use the same valid license this is probably an all-access customer |
| 111 | * |
| 112 | * @return bool |
| 113 | */ |
| 114 | public function get_probably_all_access() { |
| 115 | $valid = array_filter( |
| 116 | $this->get_licenses(), |
| 117 | function ( $key ) { |
| 118 | return $this->get_license_status( ADVADS_SLUG . '-' . $key ); |
| 119 | }, |
| 120 | ARRAY_FILTER_USE_KEY |
| 121 | ); |
| 122 | |
| 123 | return [] !== $valid && max( array_count_values( $valid ) ) > 1; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Return the licence expiry time if it is equal for more than one add-on. That indicates it is likely an All Access license |
| 128 | * |
| 129 | * @return string|null |
| 130 | */ |
| 131 | public function get_probably_all_access_expiry() { |
| 132 | /** |
| 133 | * Get expiry dates of all add-ons. |
| 134 | * |
| 135 | * @param string $key Add-on key. |
| 136 | * |
| 137 | * @return string|false the expiration date or false. |
| 138 | */ |
| 139 | $expiry_counts = array_count_values( |
| 140 | array_map( |
| 141 | function ( $key ) { |
| 142 | return $this->get_license_expires( ADVADS_SLUG . '-' . $key ); |
| 143 | }, |
| 144 | array_keys( array_filter( $this->get_licenses() ) ) |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | /** |
| 149 | * Remove all licenses that are used only once. |
| 150 | * |
| 151 | * @param int $count the count from array_count_values_above |
| 152 | * |
| 153 | * @return bool whether the count is greater 1 |
| 154 | */ |
| 155 | $all_access = array_filter( |
| 156 | $expiry_counts, |
| 157 | function ( $count ) { |
| 158 | return $count > 1; |
| 159 | } |
| 160 | ); |
| 161 | |
| 162 | // if there is an item in $all_access we can assume this is from All Access and return the expiry date. |
| 163 | return empty( $all_access ) ? null : key( $all_access ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Get license expired value of an add-on |
| 168 | * |
| 169 | * @param string $slug slug of the add-on. |
| 170 | * |
| 171 | * @return string $date expiry date of an add-on, empty string if no option exists |
| 172 | */ |
| 173 | public function get_license_expires( $slug = '' ) { |
| 174 | return \AdvancedAds\License\License::get_mirror_expires_for_options_slug( $slug ); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Add custom messages to plugin updater |
| 179 | * |
| 180 | * @param bool $reply Whether to bail without returning the package. Default false. |
| 181 | * @param string $package The package file name. |
| 182 | * @param WP_Upgrader $updater The WP_Upgrader instance. |
| 183 | * |
| 184 | * @return string |
| 185 | * |
| 186 | * @todo check if this is still working. |
| 187 | */ |
| 188 | public function addon_upgrade_filter( $reply, $package, $updater ) { |
| 189 | $key = null; |
| 190 | $value = null; |
| 191 | |
| 192 | if ( isset( $updater->skin->plugin ) ) { |
| 193 | $key = 'path'; |
| 194 | $value = $updater->skin->plugin; |
| 195 | } elseif ( isset( $updater->skin->plugin_info['Name'] ) ) { |
| 196 | $key = 'name'; |
| 197 | $value = $updater->skin->plugin_info['Name']; |
| 198 | } |
| 199 | |
| 200 | $add_on = $this->get_installed_add_on_by_key( $key, $value ); |
| 201 | if ( ! $add_on || ! isset( $add_on['path'] ) ) { |
| 202 | return $reply; |
| 203 | } |
| 204 | |
| 205 | $plugin_file = plugin_basename( $add_on['path'] ); |
| 206 | if ( wp_doing_ajax() ) { |
| 207 | $update_link = wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $plugin_file, 'upgrade-plugin_' . $plugin_file ); |
| 208 | /* translators: %s plugin update link */ |
| 209 | $updater->strings['download_failed'] = sprintf( __( 'Download failed. <a href="%s">Click here to try another method</a>.', 'advanced-ads' ), $update_link ); |
| 210 | } else { |
| 211 | /* translators: %s download failed knowledgebase link */ |
| 212 | $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' ); |
| 213 | } |
| 214 | |
| 215 | return $reply; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Search if a name is in the add-on array and return the add-on data of it |
| 220 | * |
| 221 | * @param string $key Key to search for. |
| 222 | * @param string $value Value to search for. |
| 223 | * |
| 224 | * @return array<string, mixed>|null Array with the add-on data, or null if not found. |
| 225 | */ |
| 226 | private function get_installed_add_on_by_key( $key, $value ) { |
| 227 | // Early bail!! |
| 228 | if ( empty( $key ) || empty( $value ) ) { |
| 229 | return null; |
| 230 | } |
| 231 | |
| 232 | $add_ons = Data::get_addons(); |
| 233 | if ( is_array( $add_ons ) ) { |
| 234 | foreach ( $add_ons as $add_on ) { |
| 235 | if ( $add_on[ $key ] === $value ) { |
| 236 | return $add_on; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | return null; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Check if any license is valid |
| 246 | * can be used to display information for any Pro user only, like link to direct support |
| 247 | */ |
| 248 | public static function any_license_valid() { |
| 249 | $add_ons = Addons::get_installed_addons(); |
| 250 | if ( [] === $add_ons ) { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | foreach ( $add_ons as $_add_on ) { |
| 255 | if ( 'slider-ads' === $_add_on['id'] ) { |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | $status = self::get_instance()->get_license_status( $_add_on['options_slug'] ); |
| 260 | |
| 261 | // check expiry date. |
| 262 | $expiry_date = self::get_instance()->get_license_expires( $_add_on['options_slug'] ); |
| 263 | |
| 264 | if ( |
| 265 | ( $expiry_date && strtotime( $expiry_date ) > time() ) |
| 266 | || 'valid' === $status || 'lifetime' === $expiry_date |
| 267 | ) { |
| 268 | return true; |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | return false; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Update the license status based on information retrieved from the version info check |
| 277 | * |
| 278 | * @param array<string, mixed>|\WP_Error $response HTTP response or WP_Error object. |
| 279 | * @param string $context Context under which the hook is fired. |
| 280 | * @param string $http HTTP transport used. |
| 281 | * @param array<string, mixed> $parsed_args HTTP request arguments. |
| 282 | * @param string $url The request URL. |
| 283 | * |
| 284 | * @return array<string, mixed>|\WP_Error |
| 285 | */ |
| 286 | public function update_license_after_version_info( $response, $context, $http, $parsed_args, $url ) { |
| 287 | // Early bail!! |
| 288 | if ( |
| 289 | Constants::API_ENDPOINT !== $url |
| 290 | || ( |
| 291 | empty( $parsed_args['body']['edd_action'] ) |
| 292 | || 'get_version' !== $parsed_args['body']['edd_action'] |
| 293 | ) |
| 294 | || is_wp_error( $response ) |
| 295 | ) { |
| 296 | return $response; |
| 297 | } |
| 298 | |
| 299 | // Status/expiry are derived from rich licenses after flat-map retirement. |
| 300 | return $response; |
| 301 | } |
| 302 | } |
| 303 |