Yoast_Update_Manager

The Google Analytics by Yoast Yoast Update Manager class.

Defined (1)

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

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