| Total Complexity | 88 |
| Total Lines | 530 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like MonsterInsights_License_Actions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MonsterInsights_License_Actions, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | final class MonsterInsights_License_Actions { |
||
| 17 | /** |
||
| 18 | * Holds the license key. |
||
| 19 | * |
||
| 20 | * @since 6.0.0 |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | public $key; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Holds any license error messages. |
||
| 28 | * |
||
| 29 | * @since 6.0.0 |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | public $errors = array(); |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Holds any license success messages. |
||
| 37 | * |
||
| 38 | * @since 6.0.0 |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | */ |
||
| 42 | public $success = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Primary class constructor. |
||
| 46 | * |
||
| 47 | * @since 6.0.0 |
||
| 48 | */ |
||
| 49 | public function __construct() { |
||
| 57 | } |
||
| 58 | |||
| 59 | public function admin_init() { |
||
| 60 | // Possibly verify the key. |
||
| 61 | $this->maybe_verify_key(); |
||
| 62 | |||
| 63 | // Add potential admin notices for actions around the admin. |
||
| 64 | add_action( 'admin_notices', array( $this, 'monsterinsights_notices' ), 11 ); |
||
| 65 | add_action( 'network_admin_notices', array( $this, 'monsterinsights_notices' ), 11 ); |
||
| 66 | |||
| 67 | // Grab the license key. If it is not set (even after verification), return early. |
||
| 68 | $this->key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key(); |
||
|
|
|||
| 69 | if ( ! $this->key ) { |
||
| 70 | return; |
||
| 71 | } |
||
| 72 | |||
| 73 | // Possibly handle validating, deactivating and refreshing license keys. |
||
| 74 | $this->maybe_validate_key(); |
||
| 75 | $this->maybe_deactivate_key(); |
||
| 76 | $this->maybe_refresh_key(); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Maybe verifies a license key entered by the user. |
||
| 81 | * |
||
| 82 | * @since 6.0.0 |
||
| 83 | * |
||
| 84 | * @return null Return early if the key fails to be verified. |
||
| 85 | */ |
||
| 86 | public function maybe_verify_key() { |
||
| 87 | |||
| 88 | if ( empty( $_POST['monsterinsights-license-key'] ) || strlen( $_POST['monsterinsights-license-key'] ) < 10 || empty( $_POST['monsterinsights-verify-submit'] ) ) { |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | |||
| 92 | if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) { |
||
| 93 | return; |
||
| 94 | } |
||
| 95 | |||
| 96 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | |||
| 100 | $this->verify_key(); |
||
| 101 | |||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Ajax handler for verifying the License key. |
||
| 106 | */ |
||
| 107 | public function ajax_verify_key() { |
||
| 169 | ) |
||
| 170 | ); |
||
| 171 | |||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Verifies a license key entered by the user. |
||
| 176 | * |
||
| 177 | * @since 6.0.0 |
||
| 178 | */ |
||
| 179 | public function verify_key() { |
||
| 180 | |||
| 181 | // Perform a request to verify the key. |
||
| 182 | $verify = $this->perform_remote_request( 'verify-key', array( 'tgm-updater-key' => trim( $_POST['monsterinsights-license-key'] ) ) ); |
||
| 183 | |||
| 184 | // If it returns false, send back a generic error message and return. |
||
| 185 | if ( ! $verify ) { |
||
| 186 | $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' ); |
||
| 187 | |||
| 188 | return; |
||
| 189 | } |
||
| 190 | |||
| 191 | // If an error is returned, set the error and return. |
||
| 192 | if ( ! empty( $verify->error ) ) { |
||
| 193 | $this->errors[] = $verify->error; |
||
| 194 | |||
| 195 | return; |
||
| 196 | } |
||
| 197 | |||
| 198 | // Otherwise, our request has been done successfully. Update the option and set the success message. |
||
| 199 | $option = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license(); |
||
| 200 | $option['key'] = trim( $_POST['monsterinsights-license-key'] ); |
||
| 201 | $option['type'] = isset( $verify->type ) ? $verify->type : $option['type']; |
||
| 202 | $option['is_expired'] = false; |
||
| 203 | $option['is_disabled'] = false; |
||
| 204 | $option['is_invalid'] = false; |
||
| 205 | $this->success[] = isset( $verify->success ) ? $verify->success : esc_html__( 'Congratulations! This site is now receiving automatic updates.', 'google-analytics-for-wordpress' ); |
||
| 206 | is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option ); |
||
| 207 | delete_transient( '_monsterinsights_addons' ); |
||
| 208 | monsterinsights_get_addons_data( $option['key'] ); |
||
| 209 | // Make sure users can now update their plugins if they previously an expired key. |
||
| 210 | wp_clean_plugins_cache( true ); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Maybe validates a license key entered by the user. |
||
| 215 | * |
||
| 216 | * @since 6.0.0 |
||
| 217 | * |
||
| 218 | * @return null Return early if the transient has not expired yet. |
||
| 219 | */ |
||
| 220 | public function maybe_validate_key() { |
||
| 221 | $check = is_network_admin() ? MonsterInsights()->license->time_to_check_network_license() : MonsterInsights()->license->time_to_check_site_license(); |
||
| 222 | if ( $check ) { |
||
| 223 | $this->validate_key(); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Ajax handler for validating the current key. |
||
| 229 | */ |
||
| 230 | public function ajax_validate_key() { |
||
| 231 | |||
| 232 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
| 233 | |||
| 234 | $this->validate_key( true ); |
||
| 235 | |||
| 236 | if ( ! empty( $this->errors ) ) { |
||
| 237 | wp_send_json_error( array( |
||
| 238 | 'error' => $this->errors[0], |
||
| 239 | ) ); |
||
| 240 | } |
||
| 241 | |||
| 242 | if ( ! empty( $this->success ) ) { |
||
| 243 | wp_send_json_success( |
||
| 244 | array( |
||
| 245 | 'message' => $this->success[0], |
||
| 246 | ) |
||
| 247 | ); |
||
| 248 | } |
||
| 249 | |||
| 250 | wp_die(); |
||
| 251 | |||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Validates a license key entered by the user. |
||
| 256 | * |
||
| 257 | * @since 6.0.0 |
||
| 258 | * |
||
| 259 | * @param bool $forced Force to set contextual messages (false by default). |
||
| 260 | */ |
||
| 261 | public function validate_key( $forced = false, $network = null ) { |
||
| 262 | |||
| 263 | if ( is_null( $network ) ) { |
||
| 264 | $network = is_network_admin(); |
||
| 265 | } |
||
| 266 | |||
| 267 | $validate = $this->perform_remote_request( 'validate-key', array( 'tgm-updater-key' => $this->key ) ); |
||
| 268 | |||
| 269 | // If there was a basic API error in validation, only set the transient for 10 minutes before retrying. |
||
| 270 | if ( ! $validate ) { |
||
| 271 | // If forced, set contextual success message. |
||
| 272 | if ( $forced ) { |
||
| 273 | $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' ); |
||
| 274 | } |
||
| 275 | |||
| 276 | return; |
||
| 277 | } |
||
| 278 | |||
| 279 | $option = $network ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license(); |
||
| 280 | $option['is_expired'] = false; |
||
| 281 | $option['is_disabled'] = false; |
||
| 282 | $option['is_invalid'] = false; |
||
| 283 | |||
| 284 | // If a key or author error is returned, the license no longer exists or the user has been deleted, so reset license. |
||
| 285 | if ( isset( $validate->key ) || isset( $validate->author ) ) { |
||
| 286 | $option['is_invalid'] = true; |
||
| 287 | $network ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option ); |
||
| 288 | $this->errors[] = esc_html__( 'Your license key for MonsterInsights is invalid. The key no longer exists or the user associated with the key has been deleted. Please use a different key.', 'google-analytics-for-wordpress' ); |
||
| 289 | |||
| 290 | return; |
||
| 291 | } |
||
| 292 | |||
| 293 | // If the license has expired, set the transient and expired flag and return. |
||
| 294 | if ( isset( $validate->expired ) ) { |
||
| 295 | $option['is_expired'] = true; |
||
| 296 | $network ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option ); |
||
| 297 | // Translators: The link to the MonsterInsights website. |
||
| 298 | $this->errors[] = sprintf( esc_html__( 'Your license key for MonsterInsights has expired. %1$sPlease click here to renew your license key.%2$s', 'google-analytics-for-wordpress' ), '<a href="' . monsterinsights_get_url( 'admin-notices', 'expired-license', 'https://www.monsterinsights.com/login/' ) . '" target="_blank" rel="noopener noreferrer" referrer="no-referrer">', '</a>' ); |
||
| 299 | |||
| 300 | return; |
||
| 301 | } |
||
| 302 | |||
| 303 | // If the license is disabled, set the transient and disabled flag and return. |
||
| 304 | if ( isset( $validate->disabled ) ) { |
||
| 305 | $option['is_disabled'] = true; |
||
| 306 | $network ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option ); |
||
| 307 | $this->errors[] = esc_html__( 'Your license key for MonsterInsights has been disabled. Please use a different key.', 'google-analytics-for-wordpress' ); |
||
| 308 | |||
| 309 | return; |
||
| 310 | } |
||
| 311 | |||
| 312 | // If forced, set contextual success message. |
||
| 313 | if ( ( ! empty( $validate->key ) || ! empty( $this->key ) ) && $forced ) { |
||
| 314 | $key = ! empty( $validate->key ) ? $validate->key : $this->key; |
||
| 315 | delete_transient( '_monsterinsights_addons' ); |
||
| 316 | monsterinsights_get_addons_data( $key ); |
||
| 317 | $this->success[] = esc_html__( 'Congratulations! Your key has been refreshed successfully.', 'google-analytics-for-wordpress' ); |
||
| 318 | } |
||
| 319 | |||
| 320 | $option = array(); |
||
| 321 | $option = $network ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license(); |
||
| 322 | $option['type'] = isset( $validate->type ) ? $validate->type : $option['type']; |
||
| 323 | $option['is_expired'] = false; |
||
| 324 | $option['is_disabled'] = false; |
||
| 325 | $option['is_invalid'] = false; |
||
| 326 | $network ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option ); |
||
| 327 | |||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Maybe deactivates a license key entered by the user. |
||
| 332 | * |
||
| 333 | * @since 6.0.0 |
||
| 334 | * |
||
| 335 | * @return null Return early if the key fails to be deactivated. |
||
| 336 | */ |
||
| 337 | public function maybe_deactivate_key() { |
||
| 338 | |||
| 339 | if ( empty( $_POST['monsterinsights-license-key'] ) || empty( $_POST['monsterinsights-deactivate-submit'] ) ) { |
||
| 340 | return; |
||
| 341 | } |
||
| 342 | |||
| 343 | if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) { |
||
| 344 | return; |
||
| 345 | } |
||
| 346 | |||
| 347 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
| 348 | return; |
||
| 349 | } |
||
| 350 | |||
| 351 | $this->deactivate_key(); |
||
| 352 | |||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Deactivate the license key with ajax. |
||
| 357 | */ |
||
| 358 | public function ajax_deactivate_key() { |
||
| 359 | check_ajax_referer( 'mi-admin-nonce', 'nonce' ); |
||
| 360 | |||
| 361 | $network_admin = isset( $_POST['network'] ) && $_POST['network']; |
||
| 362 | |||
| 363 | $license = ! empty( $_POST['license'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['license'] ) ) ) : false; |
||
| 364 | |||
| 365 | if ( ! $license ) { |
||
| 366 | wp_send_json_error( |
||
| 367 | array( |
||
| 368 | 'error' => esc_html__( 'Please refresh the page and try again.', 'google-analytics-for-wordpress' ), |
||
| 369 | ) |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | |||
| 373 | if ( ! current_user_can( 'monsterinsights_save_settings' ) ) { |
||
| 374 | wp_send_json_error( |
||
| 375 | array( |
||
| 376 | 'error' => esc_html__( 'You are not allowed to deactivate the license key.', 'google-analytics-for-wordpress' ), |
||
| 377 | ) |
||
| 378 | ); |
||
| 379 | } |
||
| 380 | |||
| 381 | $deactivate = $this->perform_remote_request( 'deactivate-key', array( 'tgm-updater-key' => $license ) ); |
||
| 382 | |||
| 383 | if ( ! $deactivate ) { |
||
| 384 | wp_send_json_error( |
||
| 385 | array( |
||
| 386 | 'error' => esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' ), |
||
| 387 | ) |
||
| 388 | ); |
||
| 389 | } |
||
| 390 | |||
| 391 | if ( ! empty( $deactivate->error ) ) { |
||
| 392 | wp_send_json_error( |
||
| 393 | array( |
||
| 394 | 'error' => $deactivate->error, |
||
| 395 | ) |
||
| 396 | ); |
||
| 397 | |||
| 398 | } |
||
| 399 | |||
| 400 | $network_admin ? MonsterInsights()->license->delete_network_license() : MonsterInsights()->license->delete_site_license(); |
||
| 401 | |||
| 402 | wp_send_json_success( |
||
| 403 | array( |
||
| 404 | 'message' => isset( $deactivate->success ) ? $deactivate->success : esc_html__( 'Congratulations! You have deactivated the key from this site successfully.', 'google-analytics-for-wordpress' ), |
||
| 405 | ) |
||
| 406 | ); |
||
| 407 | } |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Deactivates a license key entered by the user. |
||
| 411 | * |
||
| 412 | * @since 6.0.0 |
||
| 413 | */ |
||
| 414 | public function deactivate_key() { |
||
| 436 | |||
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Maybe refreshes a license key. |
||
| 441 | * |
||
| 442 | * @since 6.0.0 |
||
| 443 | * |
||
| 444 | * @return null Return early if the key fails to be refreshed. |
||
| 445 | */ |
||
| 446 | public function maybe_refresh_key() { |
||
| 462 | |||
| 463 | } |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Outputs any notices generated by the class. |
||
| 467 | * |
||
| 468 | * @since 7.0.0 |
||
| 469 | */ |
||
| 470 | public function monsterinsights_notices() { |
||
| 471 | if ( ! monsterinsights_is_pro_version() ) { |
||
| 472 | return; |
||
| 473 | } |
||
| 474 | |||
| 475 | $screen = get_current_screen(); |
||
| 476 | if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) { |
||
| 477 | return; |
||
| 478 | } |
||
| 479 | |||
| 480 | if ( ! empty( $this->errors ) ) { ?> |
||
| 481 | <div class="error"> |
||
| 482 | <p><?php echo implode( '<br>', $this->errors ); ?></p> |
||
| 483 | </div> |
||
| 484 | <?php } else if ( ! empty( $this->success ) ) { ?> |
||
| 485 | <div class="updated"> |
||
| 486 | <p><?php echo implode( '<br>', $this->success ); ?></p> |
||
| 487 | </div> |
||
| 488 | <?php } |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Queries the remote URL via wp_remote_post and returns a json decoded response. |
||
| 493 | * |
||
| 494 | * @since 6.0.0 |
||
| 495 | * |
||
| 496 | * @param string $action The name of the $_POST action var. |
||
| 497 | * @param array $body The content to retrieve from the remote URL. |
||
| 498 | * @param array $headers The headers to send to the remote URL. |
||
| 499 | * @param string $return_format The format for returning content from the remote URL. |
||
| 500 | * |
||
| 501 | * @return string|bool Json decoded response on success, false on failure. |
||
| 502 | */ |
||
| 503 | public function perform_remote_request( $action, $body = array(), $headers = array(), $return_format = 'json' ) { |
||
| 546 | } |
||
| 547 | } |
||
| 548 |