MI_Update_Manager

The Google Analytics by MonsterInsights MI Update Manager class.

Defined (1)

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

/vendor/yoast/license-manager/class-update-manager.php  
  1. class MI_Update_Manager { 
  2.  
  3. /** 
  4. * @var MI_Product 
  5. */ 
  6. protected $product; 
  7.  
  8. /** 
  9. * @var MI_License_Manager 
  10. */ 
  11. protected $license_manager; 
  12.  
  13. /** 
  14. * @var string 
  15. */ 
  16. protected $error_message = ''; 
  17.  
  18. /** 
  19. * @var object 
  20. */ 
  21. protected $update_response = null; 
  22.  
  23. /** 
  24. * @var string The transient name storing the API response 
  25. */ 
  26. private $response_transient_key = ''; 
  27.  
  28. /** 
  29. * @var string The transient name that stores failed request tries 
  30. */ 
  31. private $request_failed_transient_key = ''; 
  32.  
  33. /** 
  34. * Constructor 
  35. * @param MI_Product $product The product. 
  36. * @param MI_License_Manager $license_manager The License Manager. 
  37. */ 
  38. public function __construct( MI_Product $product, $license_manager ) { 
  39. $this->product = $product; 
  40. $this->license_manager = $license_manager; 
  41.  
  42. // generate transient names 
  43. $this->response_transient_key = $this->product->get_transient_prefix() . '-update-response'; 
  44. $this->request_failed_transient_key = $this->product->get_transient_prefix() . '-update-request-failed'; 
  45.  
  46. // maybe delete transient 
  47. $this->maybe_delete_transients(); 
  48.  
  49. /** 
  50. * Deletes the various transients 
  51. * If we're on the update-core.php?force-check=1 page 
  52. */ 
  53. private function maybe_delete_transients() { 
  54. global $pagenow; 
  55.  
  56. if ( $pagenow === 'update-core.php' && isset( $_GET['force-check'] ) ) { 
  57. delete_transient( $this->response_transient_key ); 
  58. delete_transient( $this->request_failed_transient_key ); 
  59.  
  60. /** 
  61. * If the update check returned a WP_Error, show it to the user 
  62. */ 
  63. public function show_update_error() { 
  64.  
  65. if ( $this->error_message === '' ) { 
  66. return; 
  67.  
  68. ?> 
  69. <div class="error"> 
  70. <p><?php printf( __( '%s failed to check for updates because of the following error: <em>%s</em>', $this->product->get_text_domain() ), $this->product->get_item_name(), $this->error_message ); ?></p> 
  71. </div> 
  72. <?php 
  73.  
  74. /** 
  75. * Calls the API and, if successfull, returns the object delivered by the API. 
  76. * @uses get_bloginfo() 
  77. * @uses wp_remote_post() 
  78. * @uses is_wp_error() 
  79. * @return false||object 
  80. */ 
  81. private function call_remote_api() { 
  82.  
  83. // only check if the failed transient is not set (or if it's expired) 
  84. if ( get_transient( $this->request_failed_transient_key ) !== false ) { 
  85. return false; 
  86.  
  87. // start request process 
  88. global $wp_version; 
  89.  
  90. // set a transient to prevent failed update checks on every page load 
  91. // this transient will be removed if a request succeeds 
  92. set_transient( $this->request_failed_transient_key, 'failed', 10800 ); 
  93.  
  94. // setup api parameters 
  95. $api_params = array( 
  96. 'edd_action' => 'get_version',  
  97. 'license' => $this->license_manager->get_license_key(),  
  98. 'item_name' => $this->product->get_item_name(),  
  99. 'wp_version' => $wp_version,  
  100. 'item_version' => $this->product->get_version(),  
  101. 'url' => home_url(),  
  102. 'slug' => $this->product->get_slug(),  
  103. ); 
  104.  
  105. // Add product ID from product if it is implemented. 
  106. if ( method_exists( $this->product, 'get_product_id' ) ) { 
  107. $product_id = $this->product->get_product_id(); 
  108. if ( $product_id > 0 ) { 
  109. $api_params['product_id'] = $this->product->get_product_id(); 
  110.  
  111. // setup request parameters 
  112. $request_params = array( 
  113. 'method' => 'POST',  
  114. 'body' => $api_params 
  115. ); 
  116.  
  117. require_once dirname( __FILE__ ) . '/class-api-request.php'; 
  118. $request = new MI_API_Request( $this->product->get_api_url(), $request_params ); 
  119.  
  120. if ( $request->is_valid() !== true ) { 
  121.  
  122. // show error message 
  123. $this->error_message = $request->get_error_message(); 
  124. add_action( 'admin_notices', array( $this, 'show_update_error' ) ); 
  125.  
  126. return false; 
  127.  
  128. // request succeeded, delete transient indicating a request failed 
  129. delete_transient( $this->request_failed_transient_key ); 
  130.  
  131. // decode response 
  132. $response = $request->get_response(); 
  133.  
  134. // check if response returned that a given site was inactive 
  135. if ( isset( $response->license_check ) && ! empty( $response->license_check ) && $response->license_check != 'valid' ) { 
  136.  
  137. // deactivate local license 
  138. $this->license_manager->set_license_status( 'invalid' ); 
  139.  
  140. // show notice to let the user know we deactivated his/her license 
  141. $this->error_message = __( "This site has not been activated properly on yoast.com and thus cannot check for future updates. Please activate your site with a valid license key.", $this->product->get_text_domain() ); 
  142. add_action( 'admin_notices', array( $this, 'show_update_error' ) ); 
  143.  
  144. $response->sections = maybe_unserialize( $response->sections ); 
  145.  
  146. // store response 
  147. set_transient( $this->response_transient_key, $response, 10800 ); 
  148.  
  149. return $response; 
  150.  
  151. /** 
  152. * Gets the remote product data (from the EDD API) 
  153. * - If it was previously fetched in the current requests, this gets it from the instance property 
  154. * - Next, it tries the 3-hour transient 
  155. * - Next, it calls the remote API and stores the result 
  156. * @return object 
  157. */ 
  158. protected function get_remote_data() { 
  159.  
  160. // always use property if it's set 
  161. if ( null !== $this->update_response ) { 
  162. return $this->update_response; 
  163.  
  164. // get cached remote data 
  165. $data = $this->get_cached_remote_data(); 
  166.  
  167. // if cache is empty or expired, call remote api 
  168. if ( $data === false ) { 
  169. $data = $this->call_remote_api(); 
  170.  
  171. $this->update_response = $data; 
  172.  
  173. return $data; 
  174.  
  175. /** 
  176. * Gets the remote product data from a 3-hour transient 
  177. * @return bool|mixed 
  178. */ 
  179. private function get_cached_remote_data() { 
  180.  
  181. $data = get_transient( $this->response_transient_key ); 
  182.  
  183. if ( $data ) { 
  184. return $data; 
  185.  
  186. return false; 
  187.