MonsterInsights_License

The Google Analytics for WordPress by MonsterInsights MonsterInsights License class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/licensing/license.php  
  1. final class MonsterInsights_License { 
  2.  
  3. /** 
  4. * Holds the base class object. 
  5. * @since 6.0.0 
  6. * @var object 
  7. */ 
  8. public $base; 
  9.  
  10. /** 
  11. * Holds the license key. 
  12. * @since 6.0.0 
  13. * @var string 
  14. */ 
  15. public $key; 
  16.  
  17. /** 
  18. * Holds any license error messages. 
  19. * @since 6.0.0 
  20. * @var array 
  21. */ 
  22. public $errors = array(); 
  23.  
  24. /** 
  25. * Holds any license success messages. 
  26. * @since 6.0.0 
  27. * @var array 
  28. */ 
  29. public $success = array(); 
  30.  
  31. /** 
  32. * Primary class constructor. 
  33. * @since 6.0.0 
  34. */ 
  35. public function __construct() { 
  36. // Load the base class object. 
  37. $this->base = MonsterInsights(); 
  38. add_action( 'admin_init', array( $this, 'admin_init' ) ); 
  39.  
  40. public function admin_init() { 
  41. // Possibly verify the key. 
  42. $this->maybe_verify_key(); 
  43.  
  44. // Add potential admin notices for actions around the admin. 
  45. add_action( 'admin_notices', array( $this, 'monsterinsights_notices' ) ); 
  46. add_action( 'network_admin_notices', array( $this, 'monsterinsights_notices' ) ); 
  47.  
  48. // Grab the license key. If it is not set (even after verification), return early. 
  49. $this->key = monsterinsights_get_license_key(); 
  50. if ( ! $this->key ) { 
  51. return; 
  52.  
  53. // Possibly handle validating, deactivating and refreshing license keys. 
  54. $this->maybe_validate_key(); 
  55. $this->maybe_deactivate_key(); 
  56. $this->maybe_refresh_key(); 
  57.  
  58. /** 
  59. * Maybe verifies a license key entered by the user. 
  60. * @since 6.0.0 
  61. * @return null Return early if the key fails to be verified. 
  62. */ 
  63. public function maybe_verify_key() { 
  64.  
  65. if ( ! $this->is_verifying_key() ) { 
  66. return; 
  67.  
  68. if ( ! $this->verify_key_action() ) { 
  69. return; 
  70.  
  71. $this->verify_key(); 
  72.  
  73.  
  74. /** 
  75. * Verifies a license key entered by the user. 
  76. * @since 6.0.0 
  77. */ 
  78. public function verify_key() { 
  79.  
  80. // Perform a request to verify the key. 
  81. $verify = $this->perform_remote_request( 'verify-key', array( 'tgm-updater-key' => trim( $_POST['monsterinsights-license-key'] ) ) ); 
  82.  
  83. // If it returns false, send back a generic error message and return. 
  84. if ( ! $verify ) { 
  85. $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' ); 
  86. return; 
  87.  
  88. // If an error is returned, set the error and return. 
  89. if ( ! empty( $verify->error ) ) { 
  90. $this->errors[] = $verify->error; 
  91. return; 
  92.  
  93. // Otherwise, our request has been done successfully. Update the option and set the success message. 
  94. if ( is_multisite() && is_network_admin() ) { 
  95. $option = get_site_option( 'monsterinsights_license' ); 
  96. $option['key'] = trim( $_POST['monsterinsights-license-key'] ); 
  97. $option['type'] = isset( $verify->type ) ? $verify->type : $option['type']; 
  98. $option['is_expired'] = false; 
  99. $option['is_disabled'] = false; 
  100. $option['is_invalid'] = false; 
  101. $this->success[] = isset( $verify->success ) ? $verify->success : esc_html__( 'Congratulations! This site is now receiving automatic updates.', 'google-analytics-for-wordpress' ); 
  102. update_site_option( 'monsterinsights_license', $option ); 
  103. } else { 
  104. $option = get_option( 'monsterinsights_license' ); 
  105. $option['key'] = trim( $_POST['monsterinsights-license-key'] ); 
  106. $option['type'] = isset( $verify->type ) ? $verify->type : $option['type']; 
  107. $option['is_expired'] = false; 
  108. $option['is_disabled'] = false; 
  109. $option['is_invalid'] = false; 
  110. $this->success[] = isset( $verify->success ) ? $verify->success : esc_html__( 'Congratulations! This site is now receiving automatic updates.', 'google-analytics-for-wordpress' ); 
  111. update_option( 'monsterinsights_license', $option );  
  112.  
  113.  
  114. /** 
  115. * Flag to determine if a key is being verified. 
  116. * @since 6.0.0 
  117. * @return bool True if being verified, false otherwise. 
  118. */ 
  119. public function is_verifying_key() { 
  120.  
  121. return isset( $_POST['monsterinsights-license-key'] ) && isset( $_POST['monsterinsights-verify-submit'] ); 
  122.  
  123.  
  124. /** 
  125. * Verifies nonces that allow key verification. 
  126. * @since 6.0.0 
  127. * @return bool True if nonces check out, false otherwise. 
  128. */ 
  129. public function verify_key_action() { 
  130.  
  131. return isset( $_POST['monsterinsights-verify-submit'] ) && wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ); 
  132.  
  133.  
  134. /** 
  135. * Maybe validates a license key entered by the user. 
  136. * @since 6.0.0 
  137. * @return null Return early if the transient has not expired yet. 
  138. */ 
  139. public function maybe_validate_key() { 
  140.  
  141. // Only run every 12 hours. 
  142. $timestamp = get_option( 'monsterinsights_license_updates' ); 
  143. if ( ! $timestamp ) { 
  144. $timestamp = strtotime( '+8 hours' ); 
  145. update_option( 'monsterinsights_license_updates', $timestamp ); 
  146. $this->validate_key(); 
  147. } else { 
  148. $current_timestamp = time(); 
  149. if ( $current_timestamp < $timestamp ) { 
  150. return; 
  151. } else { 
  152. update_option( 'monsterinsights_license_updates', strtotime( '+8 hours' ) ); 
  153. $this->validate_key(); 
  154.  
  155. /** 
  156. * Validates a license key entered by the user. 
  157. * @since 6.0.0 
  158. * @param bool $forced Force to set contextual messages (false by default). 
  159. */ 
  160. public function validate_key( $forced = false ) { 
  161.  
  162. $validate = $this->perform_remote_request( 'validate-key', array( 'tgm-updater-key' => $this->key ) ); 
  163.  
  164. // If there was a basic API error in validation, only set the transient for 10 minutes before retrying. 
  165. if ( ! $validate ) { 
  166. // If forced, set contextual success message. 
  167. if ( $forced ) { 
  168. $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' ); 
  169.  
  170. return; 
  171.  
  172. // If a key or author error is returned, the license no longer exists or the user has been deleted, so reset license. 
  173. if ( isset( $validate->key ) || isset( $validate->author ) ) { 
  174. $option = array(); 
  175. if ( is_multisite() && is_network_admin() ) { 
  176. $option = get_site_option( 'monsterinsights_license' ); 
  177. } else { 
  178. $option = get_option( 'monsterinsights_license' ); 
  179. $option['is_expired'] = false; 
  180. $option['is_disabled'] = false; 
  181. $option['is_invalid'] = true; 
  182. if ( is_multisite() && is_network_admin() ) { 
  183. update_site_option( 'monsterinsights_license', $option ); 
  184. } else { 
  185. update_option( 'monsterinsights_license', $option ); 
  186. return; 
  187.  
  188. // If the license has expired, set the transient and expired flag and return. 
  189. if ( isset( $validate->expired ) ) { 
  190. $option = array(); 
  191. if ( is_multisite() && is_network_admin() ) { 
  192. $option = get_site_option( 'monsterinsights_license' ); 
  193. } else { 
  194. $option = get_option( 'monsterinsights_license' ); 
  195. $option['is_expired'] = true; 
  196. $option['is_disabled'] = false; 
  197. $option['is_invalid'] = false; 
  198. if ( is_multisite() && is_network_admin() ) { 
  199. update_site_option( 'monsterinsights_license', $option ); 
  200. } else { 
  201. update_option( 'monsterinsights_license', $option ); 
  202. return; 
  203.  
  204. // If the license is disabled, set the transient and disabled flag and return. 
  205. if ( isset( $validate->disabled ) ) { 
  206. $option = array(); 
  207. if ( is_multisite() && is_network_admin() ) { 
  208. $option = get_site_option( 'monsterinsights_license' ); 
  209. } else { 
  210. $option = get_option( 'monsterinsights_license' ); 
  211. $option['is_expired'] = false; 
  212. $option['is_disabled'] = true; 
  213. $option['is_invalid'] = false; 
  214. if ( is_multisite() && is_network_admin() ) { 
  215. update_site_option( 'monsterinsights_license', $option ); 
  216. } else { 
  217. update_option( 'monsterinsights_license', $option ); 
  218. return; 
  219.  
  220. // If forced, set contextual success message. 
  221. if ( ( ! empty( $validate->key ) || ! empty( $this->key ) ) && $forced ) { 
  222. $key = ''; 
  223. if ( ! empty( $validate->key ) ) { 
  224. $key = $validate->key; 
  225. } else { 
  226. $key = $this->key; 
  227. delete_transient( '_monsterinsights_addons' ); 
  228. monsterinsights_get_addons_data( $key ); 
  229. $this->success[] = esc_html__( 'Congratulations! Your key has been refreshed successfully.', 'google-analytics-for-wordpress' ); 
  230.  
  231. $option = array(); 
  232. if ( is_multisite() && is_network_admin() ) { 
  233. $option = get_site_option( 'monsterinsights_license' ); 
  234. } else { 
  235. $option = get_option( 'monsterinsights_license' ); 
  236. $option['type'] = isset( $validate->type ) ? $validate->type : $option['type']; 
  237. $option['is_expired'] = false; 
  238. $option['is_disabled'] = false; 
  239. $option['is_invalid'] = false; 
  240. if ( is_multisite() && is_network_admin() ) { 
  241. update_site_option( 'monsterinsights_license', $option ); 
  242. } else { 
  243. update_option( 'monsterinsights_license', $option ); 
  244.  
  245.  
  246. /** 
  247. * Maybe deactivates a license key entered by the user. 
  248. * @since 6.0.0 
  249. * @return null Return early if the key fails to be deactivated. 
  250. */ 
  251. public function maybe_deactivate_key() { 
  252.  
  253. if ( ! $this->is_deactivating_key() ) { 
  254. return; 
  255.  
  256. if ( ! $this->deactivate_key_action() ) { 
  257. return; 
  258.  
  259. $this->deactivate_key(); 
  260.  
  261.  
  262. /** 
  263. * Deactivates a license key entered by the user. 
  264. * @since 6.0.0 
  265. */ 
  266. public function deactivate_key() { 
  267.  
  268. // Perform a request to deactivate the key. 
  269. $deactivate = $this->perform_remote_request( 'deactivate-key', array( 'tgm-updater-key' => $_POST['monsterinsights-license-key'] ) ); 
  270.  
  271. // If it returns false, send back a generic error message and return. 
  272. if ( ! $deactivate ) { 
  273. $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' ); 
  274. return; 
  275.  
  276. // If an error is returned, set the error and return. 
  277. if ( ! empty( $deactivate->error ) && ! monsterinsights_is_debug_mode() ) { 
  278. $this->errors[] = $deactivate->error; 
  279. return; 
  280.  
  281. // Otherwise, our request has been done successfully. Reset the option and set the success message. 
  282. $this->success[] = isset( $deactivate->success ) ? $deactivate->success : esc_html__( 'Congratulations! You have deactivated the key from this site successfully.', 'google-analytics-for-wordpress' ); 
  283. update_option( 'monsterinsights_license', array() ); 
  284. if ( is_multisite() && is_network_admin() ) { 
  285. update_site_option( 'monsterinsights_license', array() ); 
  286. } else { 
  287. update_option( 'monsterinsights_license', array() ); 
  288.  
  289.  
  290. /** 
  291. * Flag to determine if a key is being deactivated. 
  292. * @since 6.0.0 
  293. * @return bool True if being verified, false otherwise. 
  294. */ 
  295. public function is_deactivating_key() { 
  296.  
  297. return isset( $_POST['monsterinsights-license-key'] ) && isset( $_POST['monsterinsights-deactivate-submit'] ); 
  298.  
  299.  
  300. /** 
  301. * Verifies nonces that allow key deactivation. 
  302. * @since 6.0.0 
  303. * @return bool True if nonces check out, false otherwise. 
  304. */ 
  305. public function deactivate_key_action() { 
  306.  
  307. return isset( $_POST['monsterinsights-deactivate-submit'] ) && wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ); 
  308.  
  309.  
  310. /** 
  311. * Maybe refreshes a license key. 
  312. * @since 6.0.0 
  313. * @return null Return early if the key fails to be refreshed. 
  314. */ 
  315. public function maybe_refresh_key() { 
  316.  
  317. if ( ! $this->is_refreshing_key() ) { 
  318. return; 
  319.  
  320. if ( ! $this->refresh_key_action() ) { 
  321. return; 
  322.  
  323. // Refreshing is simply a word alias for validating a key. Force true to set contextual messages. 
  324. $this->validate_key( true ); 
  325.  
  326.  
  327. /** 
  328. * Flag to determine if a key is being refreshed. 
  329. * @since 6.0.0 
  330. * @return bool True if being refreshed, false otherwise. 
  331. */ 
  332. public function is_refreshing_key() { 
  333.  
  334. return isset( $_POST['monsterinsights-license-key'] ) && isset( $_POST['monsterinsights-refresh-submit'] ); 
  335.  
  336.  
  337. /** 
  338. * Verifies nonces that allow key refreshing. 
  339. * @since 6.0.0 
  340. * @return bool True if nonces check out, false otherwise. 
  341. */ 
  342. public function refresh_key_action() { 
  343.  
  344. return isset( $_POST['monsterinsights-refresh-submit'] ) && wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ); 
  345.  
  346.  
  347. /** 
  348. * Outputs any notices generated by the class. 
  349. * @since 6.0.0 
  350. */ 
  351. public function monsterinsights_notices() { 
  352.  
  353. // Grab the option and output any nag dealing with license keys. 
  354. $key = monsterinsights_get_license_key(); 
  355.  
  356. $option = array(); 
  357. if ( is_multisite() && is_network_admin() ) { 
  358. $option = get_site_option( 'monsterinsights_license' ); 
  359. } else { 
  360. $option = get_option( 'monsterinsights_license' ); 
  361.  
  362. if ( is_multisite() && is_network_admin() ) { 
  363. // If a key has expired, output nag about renewing the key. 
  364. if ( isset( $option['is_expired'] ) && $option['is_expired'] ) : 
  365. ?> 
  366. <div class="error"> 
  367. <p><?php printf( esc_html__( 'Your license key for MonsterInsights has expired. %1$sPlease click here to renew your license key and continue receiving automatic updates.%2$s', 'google-analytics-for-wordpress' ), '<a href="https://www.monsterinsights.com/login/" target="_blank" rel="noopener noreferrer" referrer="no-referrer">', '</a>' ); ?></p> 
  368. </div> 
  369. <?php 
  370. endif; 
  371.  
  372. // If a key has been disabled, output nag about using another key. 
  373. if ( isset( $option['is_disabled'] ) && $option['is_disabled'] ) : 
  374. ?> 
  375. <div class="error"> 
  376. <p><?php esc_html_e( 'Your license key for MonsterInsights has been disabled. Please use a different key to continue receiving automatic updates.', 'google-analytics-for-wordpress' ); ?></p> 
  377. </div> 
  378. <?php 
  379. endif; 
  380.  
  381. // If a key is invalid, output nag about using another key. 
  382. if ( isset( $option['is_invalid'] ) && $option['is_invalid'] ) : 
  383. ?> 
  384. <div class="error"> 
  385. <p><?php esc_html_e( '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 to continue receiving automatic updates.', 'google-analytics-for-wordpress' ); ?></p> 
  386. </div> 
  387. <?php 
  388. endif; 
  389.  
  390. // If there are any license errors, output them now. 
  391. if ( ! empty( $this->errors ) ) : 
  392. ?> 
  393. <div class="error"> 
  394. <p><?php echo implode( '<br>', $this->errors ); ?></p> 
  395. </div> 
  396. <?php 
  397. endif; 
  398.  
  399. // If there are any success messages, output them now. 
  400. if ( ! empty( $this->success ) ) : 
  401. ?> 
  402. <div class="updated"> 
  403. <p><?php echo implode( '<br>', $this->success ); ?></p> 
  404. </div> 
  405. <?php 
  406. endif; 
  407. } else { 
  408. // If there is no license key, output nag about ensuring key is set for automatic updates. 
  409. if ( ! $key ) : 
  410. if ( ! monsterinsights_is_pro_version() ) {  
  411. return; 
  412. $screen = get_current_screen();  
  413. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) !== false ) { 
  414. return; 
  415. ?> 
  416. <div class="error"> 
  417. <p><?php printf( esc_html__( 'No valid license key has been entered, so automatic updates for MonsterInsights have been turned off. %1$sPlease click here to enter your license key and begin receiving automatic updates.%2$s', 'google-analytics-for-wordpress' ), '<a href="'. esc_url( add_query_arg( array( 'page' => 'monsterinsights_settings' ), admin_url( 'admin.php' ) ) ) . '" target="_blank" rel="noopener noreferrer">', '</a>' ); ?></p> 
  418. </div> 
  419. <?php 
  420. endif; 
  421.  
  422. // If a key has expired, output nag about renewing the key. 
  423. if ( isset( $option['is_expired'] ) && $option['is_expired'] ) : 
  424. ?> 
  425. <div class="error"> 
  426. <p><?php printf( esc_html__( 'Your license key for MonsterInsights has expired. %1$sPlease click here to renew your license key and continue receiving automatic updates.%2$s', 'google-analytics-for-wordpress' ), '<a href="https://www.monsterinsights.com/login/" target="_blank" rel="noopener noreferrer">', '</a>' ); ?></p> 
  427. </div> 
  428. <?php 
  429. endif; 
  430.  
  431. // If a key has been disabled, output nag about using another key. 
  432. if ( isset( $option['is_disabled'] ) && $option['is_disabled'] ) : 
  433. ?> 
  434. <div class="error"> 
  435. <p><?php esc_html_e( 'Your license key for MonsterInsights has been disabled. Please use a different key to continue receiving automatic updates.', 'google-analytics-for-wordpress' ); ?></p> 
  436. </div> 
  437. <?php 
  438. endif; 
  439.  
  440. // If a key is invalid, output nag about using another key. 
  441. if ( isset( $option['is_invalid'] ) && $option['is_invalid'] ) : 
  442. ?> 
  443. <div class="error"> 
  444. <p><?php esc_html_e( '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 to continue receiving automatic updates.', 'google-analytics-for-wordpress' ); ?></p> 
  445. </div> 
  446. <?php 
  447. endif; 
  448.  
  449. // If there are any license errors, output them now. 
  450. if ( ! empty( $this->errors ) ) : 
  451. ?> 
  452. <div class="error"> 
  453. <p><?php echo implode( '<br>', $this->errors ); ?></p> 
  454. </div> 
  455. <?php 
  456. endif; 
  457.  
  458. // If there are any success messages, output them now. 
  459. if ( ! empty( $this->success ) ) : 
  460. ?> 
  461. <div class="updated"> 
  462. <p><?php echo implode( '<br>', $this->success ); ?></p> 
  463. </div> 
  464. <?php 
  465. endif; 
  466.  
  467. /** 
  468. * Queries the remote URL via wp_remote_post and returns a json decoded response. 
  469. * @since 6.0.0 
  470. * @param string $action The name of the $_POST action var. 
  471. * @param array $body The content to retrieve from the remote URL. 
  472. * @param array $headers The headers to send to the remote URL. 
  473. * @param string $return_format The format for returning content from the remote URL. 
  474. * @return string|bool Json decoded response on success, false on failure. 
  475. */ 
  476. public function perform_remote_request( $action, $body = array(), $headers = array(), $return_format = 'json' ) { 
  477.  
  478. // Build the body of the request. 
  479. $body = wp_parse_args( 
  480. $body,  
  481. array( 
  482. 'tgm-updater-action' => $action,  
  483. 'tgm-updater-key' => $this->key,  
  484. 'tgm-updater-wp-version' => get_bloginfo( 'version' ),  
  485. 'tgm-updater-referer' => site_url(),  
  486. 'tgm-updater-mi-version' => MONSTERINSIGHTS_VERSION,  
  487. 'tgm-updater-is-pro' => monsterinsights_is_pro_version(),  
  488. ); 
  489. $body = http_build_query( $body, '', '&' ); 
  490.  
  491. // Build the headers of the request. 
  492. $headers = wp_parse_args( 
  493. $headers,  
  494. array( 
  495. 'Content-Type' => 'application/x-www-form-urlencoded',  
  496. 'Content-Length' => strlen( $body ) 
  497. ); 
  498.  
  499. // Setup variable for wp_remote_post. 
  500. $post = array( 
  501. 'headers' => $headers,  
  502. 'body' => $body 
  503. ); 
  504.  
  505. // Perform the query and retrieve the response. 
  506. $response = wp_remote_post( 'https://www.monsterinsights.com', $post ); 
  507. $response_code = wp_remote_retrieve_response_code( $response ); 
  508. $response_body = wp_remote_retrieve_body( $response ); 
  509.  
  510. // Bail out early if there are any errors. 
  511. if ( 200 != $response_code || is_wp_error( $response_body ) ) { 
  512. return false; 
  513.  
  514. // Return the json decoded content. 
  515. return json_decode( $response_body ); 
  516.