license-actions.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * License Actions class.
  4. *
  5. * @since 6.0.0
  6. *
  7. * @package MonsterInsights
  8. * @author Chris Christoff
  9. */
  10. // Exit if accessed directly
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. }
  14. final class MonsterInsights_License_Actions {
  15. /**
  16. * Holds the license key.
  17. *
  18. * @since 6.0.0
  19. *
  20. * @var string
  21. */
  22. public $key;
  23. /**
  24. * Holds any license error messages.
  25. *
  26. * @since 6.0.0
  27. *
  28. * @var array
  29. */
  30. public $errors = array();
  31. /**
  32. * Holds any license success messages.
  33. *
  34. * @since 6.0.0
  35. *
  36. * @var array
  37. */
  38. public $success = array();
  39. /**
  40. * Primary class constructor.
  41. *
  42. * @since 6.0.0
  43. */
  44. public function __construct() {
  45. add_action( 'admin_init', array( $this, 'admin_init' ) );
  46. }
  47. public function admin_init() {
  48. // Possibly verify the key.
  49. $this->maybe_verify_key();
  50. // Add potential admin notices for actions around the admin.
  51. add_action( 'admin_notices', array( $this, 'monsterinsights_notices' ), 11 );
  52. add_action( 'network_admin_notices', array( $this, 'monsterinsights_notices' ), 11 );
  53. // Grab the license key. If it is not set (even after verification), return early.
  54. $this->key = is_network_admin() ? MonsterInsights()->license->get_network_license_key() : MonsterInsights()->license->get_site_license_key();
  55. if ( ! $this->key ) {
  56. return;
  57. }
  58. // Possibly handle validating, deactivating and refreshing license keys.
  59. $this->maybe_validate_key();
  60. $this->maybe_deactivate_key();
  61. $this->maybe_refresh_key();
  62. }
  63. /**
  64. * Maybe verifies a license key entered by the user.
  65. *
  66. * @since 6.0.0
  67. *
  68. * @return null Return early if the key fails to be verified.
  69. */
  70. public function maybe_verify_key() {
  71. if ( empty( $_POST['monsterinsights-license-key'] ) || strlen( $_POST['monsterinsights-license-key'] ) < 10 || empty( $_POST['monsterinsights-verify-submit'] ) ) {
  72. return;
  73. }
  74. if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) {
  75. return;
  76. }
  77. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  78. return;
  79. }
  80. $this->verify_key();
  81. }
  82. /**
  83. * Verifies a license key entered by the user.
  84. *
  85. * @since 6.0.0
  86. */
  87. public function verify_key() {
  88. // Perform a request to verify the key.
  89. $verify = $this->perform_remote_request( 'verify-key', array( 'tgm-updater-key' => trim( $_POST['monsterinsights-license-key'] ) ) );
  90. // If it returns false, send back a generic error message and return.
  91. if ( ! $verify ) {
  92. $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' );
  93. return;
  94. }
  95. // If an error is returned, set the error and return.
  96. if ( ! empty( $verify->error ) ) {
  97. $this->errors[] = $verify->error;
  98. return;
  99. }
  100. // Otherwise, our request has been done successfully. Update the option and set the success message.
  101. $option = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license();
  102. $option['key'] = trim( $_POST['monsterinsights-license-key'] );
  103. $option['type'] = isset( $verify->type ) ? $verify->type : $option['type'];
  104. $option['is_expired'] = false;
  105. $option['is_disabled'] = false;
  106. $option['is_invalid'] = false;
  107. $this->success[] = isset( $verify->success ) ? $verify->success : esc_html__( 'Congratulations! This site is now receiving automatic updates.', 'google-analytics-for-wordpress' );
  108. is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
  109. delete_transient( '_monsterinsights_addons' );
  110. monsterinsights_get_addons_data( $option['key'] );
  111. }
  112. /**
  113. * Maybe validates a license key entered by the user.
  114. *
  115. * @since 6.0.0
  116. *
  117. * @return null Return early if the transient has not expired yet.
  118. */
  119. public function maybe_validate_key() {
  120. $check = is_network_admin() ? MonsterInsights()->license->time_to_check_network_license() : MonsterInsights()->license->time_to_check_site_license();
  121. if ( $check ) {
  122. $this->validate_key();
  123. }
  124. }
  125. /**
  126. * Validates a license key entered by the user.
  127. *
  128. * @since 6.0.0
  129. *
  130. * @param bool $forced Force to set contextual messages (false by default).
  131. */
  132. public function validate_key( $forced = false ) {
  133. $validate = $this->perform_remote_request( 'validate-key', array( 'tgm-updater-key' => $this->key ) );
  134. // If there was a basic API error in validation, only set the transient for 10 minutes before retrying.
  135. if ( ! $validate ) {
  136. // If forced, set contextual success message.
  137. if ( $forced ) {
  138. $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' );
  139. }
  140. return;
  141. }
  142. $option = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license();
  143. $option['is_expired'] = false;
  144. $option['is_disabled'] = false;
  145. $option['is_invalid'] = false;
  146. // If a key or author error is returned, the license no longer exists or the user has been deleted, so reset license.
  147. if ( isset( $validate->key ) || isset( $validate->author ) ) {
  148. $option['is_invalid'] = true;
  149. is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
  150. return;
  151. }
  152. // If the license has expired, set the transient and expired flag and return.
  153. if ( isset( $validate->expired ) ) {
  154. $option['is_expired'] = true;
  155. is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
  156. return;
  157. }
  158. // If the license is disabled, set the transient and disabled flag and return.
  159. if ( isset( $validate->disabled ) ) {
  160. $option['is_disabled'] = true;
  161. is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
  162. return;
  163. }
  164. // If forced, set contextual success message.
  165. if ( ( ! empty( $validate->key ) || ! empty( $this->key ) ) && $forced ) {
  166. $key = ! empty( $validate->key ) ? $validate->key : $this->key;
  167. delete_transient( '_monsterinsights_addons' );
  168. monsterinsights_get_addons_data( $key );
  169. $this->success[] = esc_html__( 'Congratulations! Your key has been refreshed successfully.', 'google-analytics-for-wordpress' );
  170. }
  171. $option = array();
  172. $option = is_network_admin() ? MonsterInsights()->license->get_network_license() : MonsterInsights()->license->get_site_license();
  173. $option['type'] = isset( $validate->type ) ? $validate->type : $option['type'];
  174. $option['is_expired'] = false;
  175. $option['is_disabled'] = false;
  176. $option['is_invalid'] = false;
  177. is_network_admin() ? MonsterInsights()->license->set_network_license( $option ) : MonsterInsights()->license->set_site_license( $option );
  178. }
  179. /**
  180. * Maybe deactivates a license key entered by the user.
  181. *
  182. * @since 6.0.0
  183. *
  184. * @return null Return early if the key fails to be deactivated.
  185. */
  186. public function maybe_deactivate_key() {
  187. if ( empty( $_POST['monsterinsights-license-key'] ) || empty( $_POST['monsterinsights-deactivate-submit'] ) ) {
  188. return;
  189. }
  190. if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) {
  191. return;
  192. }
  193. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  194. return;
  195. }
  196. $this->deactivate_key();
  197. }
  198. /**
  199. * Deactivates a license key entered by the user.
  200. *
  201. * @since 6.0.0
  202. */
  203. public function deactivate_key() {
  204. // Perform a request to deactivate the key.
  205. $deactivate = $this->perform_remote_request( 'deactivate-key', array( 'tgm-updater-key' => $_POST['monsterinsights-license-key'] ) );
  206. // If it returns false, send back a generic error message and return.
  207. if ( ! $deactivate ) {
  208. $this->errors[] = esc_html__( 'There was an error connecting to the remote key API. Please try again later.', 'google-analytics-for-wordpress' );
  209. return;
  210. }
  211. // If an error is returned, set the error and return.
  212. if ( ! empty( $deactivate->error ) && ! monsterinsights_is_debug_mode() ) {
  213. $this->errors[] = $deactivate->error;
  214. return;
  215. }
  216. // Otherwise, our request has been done successfully. Reset the option and set the success message.
  217. $this->success[] = isset( $deactivate->success ) ? $deactivate->success : esc_html__( 'Congratulations! You have deactivated the key from this site successfully.', 'google-analytics-for-wordpress' );
  218. is_network_admin() ? MonsterInsights()->license->delete_network_license() : MonsterInsights()->license->delete_site_license();
  219. }
  220. /**
  221. * Maybe refreshes a license key.
  222. *
  223. * @since 6.0.0
  224. *
  225. * @return null Return early if the key fails to be refreshed.
  226. */
  227. public function maybe_refresh_key() {
  228. if ( empty( $_POST['monsterinsights-license-key'] ) || empty( $_POST['monsterinsights-refresh-submit'] ) ) {
  229. return;
  230. }
  231. if ( ! wp_verify_nonce( $_POST['monsterinsights-key-nonce'], 'monsterinsights-key-nonce' ) ) {
  232. return;
  233. }
  234. if ( ! current_user_can( 'monsterinsights_save_settings' ) ) {
  235. return;
  236. }
  237. // Refreshing is simply a word alias for validating a key. Force true to set contextual messages.
  238. $this->validate_key( true );
  239. }
  240. /**
  241. * Outputs any notices generated by the class.
  242. *
  243. * @since 7.0.0
  244. */
  245. public function monsterinsights_notices() {
  246. if ( ! monsterinsights_is_pro_version() ) {
  247. return;
  248. }
  249. $screen = get_current_screen();
  250. if ( empty( $screen->id ) || strpos( $screen->id, 'monsterinsights' ) === false ) {
  251. return;
  252. }
  253. if ( ! empty( $this->errors ) ) { ?>
  254. <div class="error">
  255. <p><?php echo implode( '<br>', $this->errors ); ?></p>
  256. </div>
  257. <?php } else if ( ! empty( $this->success ) ) { ?>
  258. <div class="updated">
  259. <p><?php echo implode( '<br>', $this->success ); ?></p>
  260. </div>
  261. <?php }
  262. }
  263. /**
  264. * Queries the remote URL via wp_remote_post and returns a json decoded response.
  265. *
  266. * @since 6.0.0
  267. *
  268. * @param string $action The name of the $_POST action var.
  269. * @param array $body The content to retrieve from the remote URL.
  270. * @param array $headers The headers to send to the remote URL.
  271. * @param string $return_format The format for returning content from the remote URL.
  272. * @return string|bool Json decoded response on success, false on failure.
  273. */
  274. public function perform_remote_request( $action, $body = array(), $headers = array(), $return_format = 'json' ) {
  275. // Build the body of the request.
  276. $body = wp_parse_args(
  277. $body,
  278. array(
  279. 'tgm-updater-action' => $action,
  280. 'tgm-updater-key' => $this->key,
  281. 'tgm-updater-wp-version' => get_bloginfo( 'version' ),
  282. 'tgm-updater-referer' => site_url(),
  283. 'tgm-updater-mi-version' => MONSTERINSIGHTS_VERSION,
  284. 'tgm-updater-is-pro' => monsterinsights_is_pro_version(),
  285. )
  286. );
  287. $body = http_build_query( $body, '', '&' );
  288. // Build the headers of the request.
  289. $headers = wp_parse_args(
  290. $headers,
  291. array(
  292. 'Content-Type' => 'application/x-www-form-urlencoded',
  293. 'Content-Length' => strlen( $body )
  294. )
  295. );
  296. // Setup variable for wp_remote_post.
  297. $post = array(
  298. 'headers' => $headers,
  299. 'body' => $body
  300. );
  301. // Perform the query and retrieve the response.
  302. $response = wp_remote_post( monsterinsights_get_licensing_url(), $post );
  303. $response_code = wp_remote_retrieve_response_code( $response );
  304. $response_body = wp_remote_retrieve_body( $response );
  305. // Bail out early if there are any errors.
  306. if ( 200 != $response_code || is_wp_error( $response_body ) ) {
  307. return false;
  308. }
  309. // Return the json decoded content.
  310. return json_decode( $response_body );
  311. }
  312. }