PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/acf-repeater/acf-repeater-update.php

https://gitlab.com/pankajmohale/chef2go
PHP | 474 lines | 132 code | 150 blank | 192 comment | 22 complexity | cb6bf8bbb547ab5170837fd6ab7a7039 MD5 | raw file
  1. <?php
  2. if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
  3. if( ! class_exists('acf_updates') ) :
  4. class acf_updates {
  5. // vars
  6. var $version = '2.2',
  7. $plugins = array(),
  8. $updates = false,
  9. $dev = 0;
  10. /*
  11. * __construct
  12. *
  13. * This function will setup the class functionality
  14. *
  15. * @type function
  16. * @date 5/03/2014
  17. * @since 5.0.0
  18. *
  19. * @param n/a
  20. * @return n/a
  21. */
  22. function __construct() {
  23. // append update information to transient
  24. add_filter('pre_set_site_transient_update_plugins', array($this, 'modify_plugins_transient'), 10, 1);
  25. // modify plugin data visible in the 'View details' popup
  26. add_filter('plugins_api', array($this, 'modify_plugin_details'), 10, 3);
  27. }
  28. /*
  29. * add_plugin
  30. *
  31. * This function will register a plugin
  32. *
  33. * @type function
  34. * @date 8/4/17
  35. * @since 5.5.10
  36. *
  37. * @param $plugin (array)
  38. * @return n/a
  39. */
  40. function add_plugin( $plugin ) {
  41. // validate
  42. $plugin = wp_parse_args($plugin, array(
  43. 'id' => '',
  44. 'key' => '',
  45. 'slug' => '',
  46. 'basename' => '',
  47. 'version' => '',
  48. ));
  49. // Check if is_plugin_active() function exists. This is required on the front end of the
  50. // site, since it is in a file that is normally only loaded in the admin.
  51. if( !function_exists( 'is_plugin_active' ) ) {
  52. require_once ABSPATH . 'wp-admin/includes/plugin.php';
  53. }
  54. // bail early if not active plugin (included in theme)
  55. if( !is_plugin_active($plugin['basename']) ) return;
  56. // add custom message in plugin update row
  57. // removed: decided this message will have a negative impact on user
  58. // if( is_admin() ) {
  59. //
  60. // add_action('in_plugin_update_message-' . $plugin['basename'], array($this, 'modify_plugin_update_message'), 10, 2 );
  61. //
  62. // }
  63. // append
  64. $this->plugins[ $plugin['basename'] ] = $plugin;
  65. }
  66. /*
  67. * request
  68. *
  69. * This function will make a request to the ACF update server
  70. *
  71. * @type function
  72. * @date 8/4/17
  73. * @since 5.5.10
  74. *
  75. * @param $query (string)
  76. * @param $body (array)
  77. * @return (mixed)
  78. */
  79. function request( $query = 'index.php', $body = null ) {
  80. // vars
  81. $url = 'https://connect.advancedcustomfields.com/' . $query;
  82. // test
  83. if( $this->dev ) $url = 'http://connect/' . $query;
  84. // log
  85. //acf_log('acf connect: '. $url, $body);
  86. // post
  87. $raw_response = wp_remote_post( $url, array(
  88. 'timeout' => 10,
  89. 'body' => $body
  90. ));
  91. // wp error
  92. if( is_wp_error($raw_response) ) {
  93. return $raw_response;
  94. // http error
  95. } elseif( wp_remote_retrieve_response_code($raw_response) != 200 ) {
  96. return new WP_Error( 'server_error', wp_remote_retrieve_response_message($raw_response) );
  97. }
  98. // decode response
  99. $json = json_decode( wp_remote_retrieve_body($raw_response), true );
  100. // allow non json value
  101. if( $json === null ) {
  102. return wp_remote_retrieve_body($raw_response);
  103. }
  104. // return
  105. return $json;
  106. }
  107. /*
  108. * get_plugin_info
  109. *
  110. * This function will get plugin info and save as transient
  111. *
  112. * @type function
  113. * @date 9/4/17
  114. * @since 5.5.10
  115. *
  116. * @param $id (string)
  117. * @return (mixed)
  118. */
  119. function get_plugin_info( $id = '' ) {
  120. // var
  121. $transient_name = 'acf_plugin_info_'.$id;
  122. // delete transient (force-check is used to refresh)
  123. if( !empty($_GET['force-check']) ) {
  124. delete_transient($transient_name);
  125. }
  126. // try transient
  127. $transient = get_transient($transient_name);
  128. if( $transient !== false ) return $transient;
  129. // connect
  130. $response = $this->request('v2/plugins/get-info?p='.$id);
  131. // ensure response is expected JSON array (not string)
  132. if( is_string($response) ) {
  133. $response = new WP_Error( 'server_error', esc_html($response) );
  134. }
  135. // update transient
  136. set_transient($transient_name, $response, HOUR_IN_SECONDS );
  137. // return
  138. return $response;
  139. }
  140. /*
  141. * refresh_plugins_transient
  142. *
  143. * This function will refresh plugin update info to the transient
  144. *
  145. * @type function
  146. * @date 11/4/17
  147. * @since 5.5.10
  148. *
  149. * @param n/a
  150. * @return n/a
  151. */
  152. function refresh_plugins_transient() {
  153. // vars
  154. $transient = get_site_transient('update_plugins');
  155. // bail early if no transient
  156. if( empty($transient) ) return;
  157. // update (will trigger modify function)
  158. set_site_transient( 'update_plugins', $transient );
  159. }
  160. /*
  161. * modify_plugins_transient
  162. *
  163. * This function will connect to the ACF website and find update information
  164. *
  165. * @type function
  166. * @date 16/01/2014
  167. * @since 5.0.0
  168. *
  169. * @param $transient (object)
  170. * @return $transient
  171. */
  172. function modify_plugins_transient( $transient ) {
  173. // bail early if no response (error)
  174. if( !isset($transient->response) ) return $transient;
  175. // fetch updates once (this filter is called multiple times during a single page load)
  176. if( !$this->updates ) {
  177. // vars
  178. $post = array(
  179. 'plugins' => wp_json_encode($this->plugins),
  180. 'wp' => wp_json_encode(array(
  181. 'wp_name' => get_bloginfo('name'),
  182. 'wp_url' => home_url(),
  183. 'wp_version' => get_bloginfo('version'),
  184. 'wp_language' => get_bloginfo('language'),
  185. 'wp_timezone' => get_option('timezone_string'),
  186. )),
  187. 'acf' => wp_json_encode(array(
  188. 'acf_version' => get_option('acf_version'),
  189. 'acf_pro' => (defined('ACF_PRO') && ACF_PRO),
  190. )),
  191. );
  192. // connect
  193. $this->updates = $this->request('v2/plugins/update-check', $post);
  194. }
  195. // append
  196. if( is_array($this->updates) ) {
  197. foreach( $this->updates['plugins'] as $basename => $update ) {
  198. $transient->response[ $basename ] = (object) $update;
  199. }
  200. }
  201. // return
  202. return $transient;
  203. }
  204. /*
  205. * modify_plugin_details
  206. *
  207. * This function will populate the plugin data visible in the 'View details' popup
  208. *
  209. * @type function
  210. * @date 17/01/2014
  211. * @since 5.0.0
  212. *
  213. * @param $result (bool|object)
  214. * @param $action (string)
  215. * @param $args (object)
  216. * @return $result
  217. */
  218. function modify_plugin_details( $result, $action = null, $args = null ) {
  219. // vars
  220. $plugin = false;
  221. // only for 'plugin_information' action
  222. if( $action !== 'plugin_information' ) return $result;
  223. // find plugin via slug
  224. foreach( $this->plugins as $p ) {
  225. if( $args->slug == $p['slug'] ) $plugin = $p;
  226. }
  227. // bail early if plugin not found
  228. if( !$plugin ) return $result;
  229. // connect
  230. $response = $this->get_plugin_info($plugin['id']);
  231. // bail early if no response
  232. if( !is_array($response) ) return $result;
  233. // remove tags (different context)
  234. unset($response['tags']);
  235. // convert to object
  236. $response = (object) $response;
  237. // sections
  238. $sections = array(
  239. 'description' => '',
  240. 'installation' => '',
  241. 'changelog' => '',
  242. 'upgrade_notice' => ''
  243. );
  244. foreach( $sections as $k => $v ) {
  245. $sections[ $k ] = $response->$k;
  246. }
  247. $response->sections = $sections;
  248. // return
  249. return $response;
  250. }
  251. /*
  252. * modify_plugin_update_message
  253. *
  254. * Displays an update message for plugin list screens.
  255. * Shows only the version updates from the current until the newest version
  256. *
  257. * @type function
  258. * @date 14/06/2016
  259. * @since 5.3.8
  260. *
  261. * @param $plugin_data (array)
  262. * @param $r (object)
  263. * @return n/a
  264. */
  265. /*
  266. function modify_plugin_update_message( $plugin_data, $response ) {
  267. // show notice if exists in transient data
  268. if( isset($response->notice) ) {
  269. echo '<div class="acf-plugin-upgrade-notice">' . $response->notice . '</div>';
  270. }
  271. }
  272. */
  273. }
  274. /*
  275. * acf_updates
  276. *
  277. * The main function responsible for returning the one true acf_updates instance to functions everywhere.
  278. * Use this function like you would a global variable, except without needing to declare the global.
  279. *
  280. * Example: <?php $acf_updates = acf_updates(); ?>
  281. *
  282. * @type function
  283. * @date 9/4/17
  284. * @since 5.5.12
  285. *
  286. * @param n/a
  287. * @return (object)
  288. */
  289. function acf_updates() {
  290. global $acf_updates;
  291. if( !isset($acf_updates) ) {
  292. $acf_updates = new acf_updates();
  293. }
  294. return $acf_updates;
  295. }
  296. /*
  297. * acf_register_plugin_update
  298. *
  299. * alias of acf_updates()->add_plugin()
  300. *
  301. * @type function
  302. * @date 12/4/17
  303. * @since 5.5.10
  304. *
  305. * @param $post_id (int)
  306. * @return $post_id (int)
  307. */
  308. function acf_register_plugin_update( $plugin ) {
  309. acf_updates()->add_plugin( $plugin );
  310. }
  311. endif; // class_exists check
  312. // register update
  313. acf_register_plugin_update(array(
  314. 'id' => 'repeater',
  315. 'key' => 'QJF7-L4IX-UCNP-RF2W',
  316. 'slug' => $this->settings['slug'],
  317. 'basename' => $this->settings['basename'],
  318. 'version' => $this->settings['version'],
  319. ));
  320. ?>