MonsterInsights_Updater

The Google Analytics for WordPress by MonsterInsights MonsterInsights Updater class.

Defined (1)

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

/includes/admin/licensing/updater.php  
  1. class MonsterInsights_Updater { 
  2.  
  3. /** 
  4. * Plugin name. 
  5. * @since 6.0.0 
  6. * @var bool|string 
  7. */ 
  8. public $plugin_name = false; 
  9.  
  10. /** 
  11. * Plugin slug. 
  12. * @since 6.0.0 
  13. * @var bool|string 
  14. */ 
  15. public $plugin_slug = false; 
  16.  
  17. /** 
  18. * Plugin path. 
  19. * @since 6.0.0 
  20. * @var bool|string 
  21. */ 
  22. public $plugin_path = false; 
  23.  
  24. /** 
  25. * URL of the plugin. 
  26. * @since 6.0.0 
  27. * @var bool|string 
  28. */ 
  29. public $plugin_url = false; 
  30.  
  31. /** 
  32. * Remote URL for getting plugin updates. 
  33. * @since 6.0.0 
  34. * @var bool|string 
  35. */ 
  36. public $remote_url = false; 
  37.  
  38. /** 
  39. * Version number of the plugin. 
  40. * @since 6.0.0 
  41. * @var bool|int 
  42. */ 
  43. public $version = false; 
  44.  
  45. /** 
  46. * License key for the plugin. 
  47. * @since 6.0.0 
  48. * @var bool|string 
  49. */ 
  50. public $key = false; 
  51.  
  52. /** 
  53. * Holds the update data returned from the API. 
  54. * @since 6.0.0 
  55. * @var bool|object 
  56. */ 
  57. public $update = false; 
  58.  
  59. /** 
  60. * Holds the plugin info details for the update. 
  61. * @since 6.0.0 
  62. * @var bool|object 
  63. */ 
  64. public $info = false; 
  65.  
  66. /** 
  67. * Holds the base class object. 
  68. * @since 6.0.0 
  69. * @var object 
  70. */ 
  71. public $base; 
  72.  
  73. /** 
  74. * Primary class constructor. 
  75. * @since 6.0.0 
  76. * @param array $config Array of updater config args. 
  77. */ 
  78. public function __construct( array $config ) { 
  79.  
  80. // Load the base class object. 
  81. $this->base = MonsterInsights(); 
  82.  
  83. // Set class properties. 
  84. $accepted_args = array( 
  85. 'plugin_name',  
  86. 'plugin_slug',  
  87. 'plugin_path',  
  88. 'plugin_url',  
  89. 'remote_url',  
  90. 'version',  
  91. 'key' 
  92. ); 
  93. foreach ( $accepted_args as $arg ) { 
  94. $this->$arg = $config[$arg]; 
  95.  
  96. // If the user cannot update plugins, stop processing here. 
  97. if ( ! current_user_can( 'update_plugins' ) ) { 
  98. return; 
  99.  
  100. // Load the updater hooks and filters. 
  101. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'update_plugins_filter' ) ); 
  102. //add_filter( 'set_site_transient_update_plugins', array( $this, 'set_site_transient_update_plugins' ) ); 
  103. //add_filter( 'transient_update_plugins', array( $this, 'transient_update_plugins' ) ); 
  104. add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); 
  105. add_filter( 'plugins_api', array( $this, 'plugins_api' ), 10, 3 ); 
  106.  
  107.  
  108. /** 
  109. * Infuse plugin update details when WordPress runs its update checker. 
  110. * @since 6.0.0 
  111. * @param object $value The WordPress update object. 
  112. * @return object $value Amended WordPress update object on success, default if object is empty. 
  113. */ 
  114. public function update_plugins_filter( $value ) { 
  115.  
  116. // If no update object exists, return early. 
  117. if ( empty( $value ) ) { 
  118. return $value; 
  119.  
  120. // Run update check by pinging the external API. If it fails, return the default update object. 
  121. if ( ! $this->update ) { 
  122. $this->update = $this->perform_remote_request( 'get-plugin-update', array( 'tgm-updater-plugin' => $this->plugin_slug ) ); 
  123. if ( ! $this->update || ! empty( $this->update->error ) ) { 
  124. $this->update = false; 
  125. return $value; 
  126.  
  127. // Infuse the update object with our data if the version from the remote API is newer. 
  128. if ( isset( $this->update->new_version ) && version_compare( $this->version, $this->update->new_version, '<' ) ) { 
  129. // The $plugin_update object contains new_version, package, slug and last_update keys. 
  130. $value->response[$this->plugin_path] = $this->update; 
  131.  
  132. // Return the update object. 
  133. return $value; 
  134.  
  135.  
  136. /** 
  137. * Disables SSL verification to prevent download package failures. 
  138. * @since 6.0.0 
  139. * @param array $args Array of request args. 
  140. * @param string $url The URL to be pinged. 
  141. * @return array $args Amended array of request args. 
  142. */ 
  143. public function http_request_args( $args, $url ) { 
  144.  
  145. // If this is an SSL request and we are performing an upgrade routine, disable SSL verification. 
  146. if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'tgm-updater-action=get-plugin-update' ) ) { 
  147. $args['sslverify'] = false; 
  148.  
  149. return $args; 
  150.  
  151.  
  152. /** 
  153. * Filters the plugins_api function to get our own custom plugin information 
  154. * from our private repo. 
  155. * @since 6.0.0 
  156. * @param object $api The original plugins_api object. 
  157. * @param string $action The action sent by plugins_api. 
  158. * @param array $args Additional args to send to plugins_api. 
  159. * @return object $api New stdClass with plugin information on success, default response on failure. 
  160. */ 
  161. public function plugins_api( $api, $action = '', $args = null ) { 
  162.  
  163. $plugin = ( 'plugin_information' == $action ) && isset( $args->slug ) && ( $this->plugin_slug == $args->slug ); 
  164.  
  165. // If our plugin matches the request, set our own plugin data, else return the default response. 
  166. if ( $plugin ) { 
  167. return $this->set_plugins_api( $api ); 
  168. } else { 
  169. return $api; 
  170.  
  171.  
  172. /** 
  173. * Pings a remote API to retrieve plugin information for WordPress to display. 
  174. * @since 6.0.0 
  175. * @param object $default_api The default API object. 
  176. * @return object $api Return custom plugin information to plugins_api. 
  177. */ 
  178. public function set_plugins_api( $default_api ) { 
  179.  
  180. // Perform the remote request to retrieve our plugin information. If it fails, return the default object. 
  181. if ( ! $this->info ) { 
  182. $this->info = $this->perform_remote_request( 'get-plugin-info', array( 'tgm-updater-plugin' => $this->plugin_slug ) ); 
  183. if ( ! $this->info || ! empty( $this->info->error ) ) { 
  184. $this->info = false; 
  185. return $default_api; 
  186.  
  187. // Create a new stdClass object and populate it with our plugin information. 
  188. $api = new stdClass; 
  189. $api->name = isset( $this->info->name ) ? $this->info->name : ''; 
  190. $api->slug = isset( $this->info->slug ) ? $this->info->slug : ''; 
  191. $api->version = isset( $this->info->version ) ? $this->info->version : ''; 
  192. $api->author = isset( $this->info->author ) ? $this->info->author : ''; 
  193. $api->author_profile = isset( $this->info->author_profile ) ? $this->info->author_profile : ''; 
  194. $api->requires = isset( $this->info->requires ) ? $this->info->requires : ''; 
  195. $api->tested = isset( $this->info->tested ) ? $this->info->tested : ''; 
  196. $api->last_updated = isset( $this->info->last_updated ) ? $this->info->last_updated : ''; 
  197. $api->homepage = isset( $this->info->homepage ) ? $this->info->homepage : ''; 
  198.  
  199. $changelog = isset( $this->info->changelog ) ? $this->info->changelog : ''; 
  200. $description = isset( $this->info->description ) ? $this->info->description : ''; 
  201.  
  202. if ( ! empty( $changelog ) ) { 
  203. if ( ! empty( $description ) ) { 
  204. $api->sections['description'] = $description; 
  205. $api->sections['changelog'] = $changelog; 
  206. } else { 
  207. $api->sections['changelog'] = $changelog; 
  208. } else if ( ! empty( $description ) ) { 
  209. $api->sections['description'] = $description; 
  210. } else { 
  211. $api->sections = array(); 
  212. }  
  213.  
  214. $api->download_link = isset( $this->info->download_link ) ? $this->info->download_link : ''; 
  215.  
  216. // Return the new API object with our custom data. 
  217. return $api; 
  218.  
  219.  
  220. /** 
  221. * Queries the remote URL via wp_remote_post and returns a json decoded response. 
  222. * @since 6.0.0 
  223. * @param string $action The name of the $_POST action var. 
  224. * @param array $body The content to retrieve from the remote URL. 
  225. * @param array $headers The headers to send to the remote URL. 
  226. * @param string $return_format The format for returning content from the remote URL. 
  227. * @return string|bool Json decoded response on success, false on failure. 
  228. */ 
  229. public function perform_remote_request( $action, $body = array(), $headers = array(), $return_format = 'json' ) { 
  230.  
  231. // Build the body of the request. 
  232. $body = wp_parse_args( 
  233. $body,  
  234. array( 
  235. 'tgm-updater-action' => $action,  
  236. 'tgm-updater-key' => $this->key,  
  237. 'tgm-updater-wp-version' => get_bloginfo( 'version' ),  
  238. 'tgm-updater-referer' => site_url(),  
  239. 'tgm-updater-mi-version' => MONSTERINSIGHTS_VERSION,  
  240. 'tgm-updater-is-pro' => monsterinsights_is_pro_version(),  
  241. ); 
  242. $body = http_build_query( $body, '', '&' ); 
  243.  
  244. // Build the headers of the request. 
  245. $headers = wp_parse_args( 
  246. $headers,  
  247. array( 
  248. 'Content-Type' => 'application/x-www-form-urlencoded',  
  249. 'Content-Length' => strlen( $body ) 
  250. ); 
  251.  
  252. // Setup variable for wp_remote_post. 
  253. $post = array( 
  254. 'headers' => $headers,  
  255. 'body' => $body 
  256. ); 
  257.  
  258. // Perform the query and retrieve the response. 
  259. $response = wp_remote_post( esc_url_raw( $this->remote_url ), $post ); 
  260. $response_code = wp_remote_retrieve_response_code( $response ); 
  261. $response_body = wp_remote_retrieve_body( $response ); 
  262.  
  263. // Bail out early if there are any errors. 
  264. if ( 200 != $response_code || is_wp_error( $response_body ) ) { 
  265. return false; 
  266.  
  267. // Return the json decoded content. 
  268. return json_decode( $response_body ); 
  269.