| 1 |
<?php |
| 2 |
/** |
| 3 |
* The UpStream extensions license checker. |
| 4 |
* |
| 5 |
* @package UpStream |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace UpStream; |
| 9 |
|
| 10 |
// Prevent direct access. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Class License_Checker |
| 17 |
* |
| 18 |
* @since 2.0.9 |
| 19 |
*/ |
| 20 |
class License_Checker { |
| 21 |
|
| 22 |
/** |
| 23 |
* Constant for plugin name. |
| 24 |
* |
| 25 |
* @var String |
| 26 |
*/ |
| 27 |
const PLUGIN_NAME = 'upstream'; |
| 28 |
|
| 29 |
/** |
| 30 |
* Constant for valid status. |
| 31 |
* |
| 32 |
* @var String |
| 33 |
*/ |
| 34 |
const LICENSE_STATUS_VALID = 'valid'; |
| 35 |
|
| 36 |
/** |
| 37 |
* License registration page. |
| 38 |
* |
| 39 |
* @var String |
| 40 |
*/ |
| 41 |
const LICENSE_REGISTRATION_PAGE = 'admin.php?page=upstream_extensions'; |
| 42 |
|
| 43 |
/** |
| 44 |
* The url to send the EDD license check request. |
| 45 |
* |
| 46 |
* @var String |
| 47 |
*/ |
| 48 |
const EDD_API_URL = 'https://upstreamplugin.com'; |
| 49 |
|
| 50 |
/** |
| 51 |
* Periodically days to check. |
| 52 |
* |
| 53 |
* @var Integer |
| 54 |
*/ |
| 55 |
const DAYS_TO_CHECK = 15; |
| 56 |
|
| 57 |
/** |
| 58 |
* The EDD id of the addon to check. |
| 59 |
* |
| 60 |
* @var String |
| 61 |
*/ |
| 62 |
public $addon_edd_id; |
| 63 |
|
| 64 |
/** |
| 65 |
* The addon slug to check. |
| 66 |
* |
| 67 |
* @var String |
| 68 |
*/ |
| 69 |
public $addon_slug; |
| 70 |
|
| 71 |
/** |
| 72 |
* The wp_options option name for the addon. |
| 73 |
* |
| 74 |
* @var String |
| 75 |
*/ |
| 76 |
public $option_name; |
| 77 |
|
| 78 |
/** |
| 79 |
* Class constructor. |
| 80 |
* |
| 81 |
* @since 2.0.9 |
| 82 |
* |
| 83 |
* @param String $addon_edd_id The EDD id of the addon to check. |
| 84 |
* @param String $addon_slug The addon name to check. |
| 85 |
*/ |
| 86 |
public function __construct( $addon_edd_id, $addon_slug ) { |
| 87 |
$this->addon_edd_id = $addon_edd_id; |
| 88 |
$this->addon_slug = $addon_slug; |
| 89 |
$this->option_name = str_replace( '-', '_', $this->addon_slug ); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get addon license key from database. |
| 94 |
* |
| 95 |
* @since 2.0.9 |
| 96 |
* @return String License key. |
| 97 |
*/ |
| 98 |
private function get_license_key() { |
| 99 |
return get_option( $this->option_name . '_license_key', '' ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Get addon license status from database. |
| 104 |
* |
| 105 |
* @since 2.0.9 |
| 106 |
* @return String License status. |
| 107 |
*/ |
| 108 |
private function get_license_status() { |
| 109 |
return get_option( $this->option_name . '_license_status', 'missing' ); |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Update addon license status to database. |
| 114 |
* |
| 115 |
* @since 2.0.9 |
| 116 |
*/ |
| 117 |
private function update_license_status( $status ) { |
| 118 |
update_option( $this->option_name . '_license_status', $status, true); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Update when the last time a license has been checked. |
| 123 |
* |
| 124 |
* @since 2.0.9 |
| 125 |
*/ |
| 126 |
private function update_license_checked_date() { |
| 127 |
update_option( $this->option_name . '_license_checked', date( 'Y-m-d' ), true ); |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* Update license notification message. |
| 132 |
* |
| 133 |
* @since 2.0.9 |
| 134 |
* @param String $message Notification message to store. |
| 135 |
*/ |
| 136 |
private function update_license_message( $message ) { |
| 137 |
update_option( $this->option_name . '_license_message', $message, true ); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Get default error notification message. |
| 142 |
* |
| 143 |
* @since 2.0.9 |
| 144 |
* @return String Notification message. |
| 145 |
*/ |
| 146 |
private function get_default_error_message() { |
| 147 |
$addon_name = ucwords( str_replace( '-', ' ', $this->addon_slug ) ); |
| 148 |
|
| 149 |
return sprintf( |
| 150 |
__( 'The license for %s is not valid. Please %sregister your license key here.%s', 'upstream' ), |
| 151 |
$addon_name, |
| 152 |
'<a href="' . admin_url( self::LICENSE_REGISTRATION_PAGE ) . '">', |
| 153 |
'</a>' |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Get the invalid license notification message. |
| 159 |
* |
| 160 |
* @since 2.0.9 |
| 161 |
* @return String|Boolean Notification message or false if we have the valid license. |
| 162 |
*/ |
| 163 |
public function get_invalid_license_message() { |
| 164 |
$license_status = $this->get_license_status(); |
| 165 |
|
| 166 |
if ( self::LICENSE_STATUS_VALID !== $license_status ) { |
| 167 |
return get_option( $this->option_name . '_license_message', $this->get_default_error_message() ); |
| 168 |
} |
| 169 |
|
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Check whether the addon's turn to check or not. |
| 175 |
* |
| 176 |
* @since 2.0.9 |
| 177 |
* @return Boolean True if it is the addon's turn to check. |
| 178 |
*/ |
| 179 |
public function is_turn_to_check() { |
| 180 |
$date_checked = get_option( $this->option_name . '_license_checked', date( 'Y-m-d', strtotime( '-' . self::DAYS_TO_CHECK . ' days' ) ) ); |
| 181 |
$times_between = time() - strtotime( $date_checked ); |
| 182 |
$days_between = round( $times_between / (60 * 60 * 24) ); |
| 183 |
|
| 184 |
if ( $days_between >= self::DAYS_TO_CHECK ) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Check the current addon license. |
| 193 |
* Use the `get_invalid_license_message` method to get this license_check result. |
| 194 |
* |
| 195 |
* @since 2.0.9 |
| 196 |
*/ |
| 197 |
public function license_check() { |
| 198 |
try { |
| 199 |
$addon_name = ucwords( str_replace( '-', ' ', $this->addon_slug ) ); |
| 200 |
$license_status = $this->get_license_status(); |
| 201 |
$license_key = $this->get_license_key(); |
| 202 |
|
| 203 |
// Update when the last time a license has been checked. |
| 204 |
$this->update_license_checked_date(); |
| 205 |
|
| 206 |
// Check the license key from the database. |
| 207 |
if ( '' === $license_key ) { |
| 208 |
throw new Exception( |
| 209 |
sprintf( |
| 210 |
__( 'You haven\'t registered the license key for %s. Please %sregister your license key here.%s', 'upstream' ), |
| 211 |
$addon_name, |
| 212 |
'<a href="' . admin_url( self::LICENSE_REGISTRATION_PAGE ) . '">', |
| 213 |
'</a>' |
| 214 |
) |
| 215 |
); |
| 216 |
} |
| 217 |
|
| 218 |
// Check the current status from the database. |
| 219 |
if ( self::LICENSE_STATUS_VALID !== $license_status ) { |
| 220 |
throw new Exception( $this->get_default_error_message() ); |
| 221 |
} |
| 222 |
|
| 223 |
// Make the request. |
| 224 |
$edd_response = wp_remote_post( |
| 225 |
self::EDD_API_URL, |
| 226 |
array( |
| 227 |
'timeout' => 30, |
| 228 |
'sslverify' => true, |
| 229 |
'body' => array( |
| 230 |
'edd_action' => 'check_license', |
| 231 |
'license' => $license_key, |
| 232 |
'item_id' => $this->addon_edd_id, |
| 233 |
'url' => site_url(), |
| 234 |
), |
| 235 |
) |
| 236 |
); |
| 237 |
|
| 238 |
// Is the response an error? |
| 239 |
if ( is_wp_error( $edd_response ) || 200 !== wp_remote_retrieve_response_code( $edd_response ) ) { |
| 240 |
throw new Exception( |
| 241 |
sprintf( |
| 242 |
__( 'An error occurred when checking the license for %s. (invalid_response)', 'upstream' ), |
| 243 |
$addon_name, |
| 244 |
) |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
// Convert data response to an object. |
| 249 |
$data = json_decode( wp_remote_retrieve_body( $edd_response ) ); |
| 250 |
|
| 251 |
// Do we have empty data? |
| 252 |
if ( empty( $data ) || ! is_object( $data ) ) { |
| 253 |
throw new Exception( |
| 254 |
sprintf( |
| 255 |
__( 'An error occurred when checking the license for %s. (invalid_data)', 'upstream' ), |
| 256 |
$addon_name, |
| 257 |
) |
| 258 |
); |
| 259 |
} |
| 260 |
|
| 261 |
// Deal with invalid licenses. |
| 262 |
if ( isset( $data->license ) && self::LICENSE_STATUS_VALID !== $data->license ) { |
| 263 |
$this->update_license_status( $data->license ); |
| 264 |
throw new Exception( $this->get_default_error_message() ); |
| 265 |
} |
| 266 |
} catch ( Exception $e ) { |
| 267 |
$message = $e->getMessage(); |
| 268 |
$this->update_license_message( $message ); |
| 269 |
} |
| 270 |
} |
| 271 |
} |