123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'ACF_Updates' ) ) :
- class ACF_Updates {
-
- var $version = '2.4';
-
- var $plugins = array();
-
- var $checked = 0;
-
- function __construct() {
-
- add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'modify_plugins_transient' ), 10, 1 );
-
- add_filter( 'plugins_api', array( $this, 'modify_plugin_details' ), 10, 3 );
- }
-
- function add_plugin( $plugin ) {
-
- $plugin = wp_parse_args(
- $plugin,
- array(
- 'id' => '',
- 'key' => '',
- 'slug' => '',
- 'basename' => '',
- 'version' => '',
- )
- );
-
-
- if ( ! function_exists( 'is_plugin_active' ) ) {
- require_once ABSPATH . 'wp-admin/includes/plugin.php';
- }
-
- if ( is_plugin_active( $plugin['basename'] ) ) {
- $this->plugins[ $plugin['basename'] ] = $plugin;
- }
- }
-
- function get_plugin_by( $key = '', $value = null ) {
- foreach ( $this->plugins as $plugin ) {
- if ( $plugin[ $key ] === $value ) {
- return $plugin;
- }
- }
- return false;
- }
-
- function request( $endpoint = '', $body = null ) {
-
- $url = "https://connect.advancedcustomfields.com/$endpoint";
-
- if ( defined( 'ACF_DEV_API' ) && ACF_DEV_API ) {
- $url = trailingslashit( ACF_DEV_API ) . $endpoint;
- acf_log( $url, $body );
- }
-
- $raw_response = wp_remote_post(
- $url,
- array(
- 'timeout' => 10,
- 'body' => $body,
- )
- );
-
- if ( is_wp_error( $raw_response ) ) {
- return $raw_response;
-
- } elseif ( wp_remote_retrieve_response_code( $raw_response ) != 200 ) {
- return new WP_Error( 'server_error', wp_remote_retrieve_response_message( $raw_response ) );
- }
-
- $json = json_decode( wp_remote_retrieve_body( $raw_response ), true );
-
- if ( $json === null ) {
- return wp_remote_retrieve_body( $raw_response );
- }
-
- return $json;
- }
-
- public function get_plugin_info( $id = '', $force_check = false ) {
- $transient_name = 'acf_plugin_info_' . $id;
-
- if ( ! $force_check ) {
- $transient = get_transient( $transient_name );
- if ( $transient !== false ) {
- return $transient;
- }
- }
- $response = $this->request( 'v2/plugins/get-info?p=' . $id );
-
- if ( is_string( $response ) ) {
- $response = new WP_Error( 'server_error', esc_html( $response ) );
- }
-
- $expiration = $this->get_expiration( $response, DAY_IN_SECONDS, MONTH_IN_SECONDS );
-
- set_transient( $transient_name, $response, $expiration );
- return $response;
- }
-
- function get_plugin_update( $basename = '', $force_check = false ) {
-
- $updates = $this->get_plugin_updates( $force_check );
-
- if ( is_array( $updates ) && isset( $updates['plugins'][ $basename ] ) ) {
- return $updates['plugins'][ $basename ];
- }
- return false;
- }
-
- function get_plugin_updates( $force_check = false ) {
-
- $transient_name = 'acf_plugin_updates';
-
-
- $checked = array();
- foreach ( $this->plugins as $basename => $plugin ) {
- $checked[ $basename ] = $plugin['version'];
- }
- ksort( $checked );
-
- if ( ! $force_check ) {
- $transient = get_transient( $transient_name );
-
- if ( is_array( $transient ) ) {
- $transient_checked = isset( $transient['checked'] ) ? $transient['checked'] : array();
- if ( wp_json_encode( $checked ) !== wp_json_encode( $transient_checked ) ) {
- $transient = false;
- }
- }
- if ( $transient !== false ) {
- return $transient;
- }
- }
-
- $post = array(
- 'plugins' => wp_json_encode( $this->plugins ),
- 'wp' => wp_json_encode(
- array(
- 'wp_name' => get_bloginfo( 'name' ),
- 'wp_url' => acf_get_home_url(),
- 'wp_version' => get_bloginfo( 'version' ),
- 'wp_language' => get_bloginfo( 'language' ),
- 'wp_timezone' => get_option( 'timezone_string' ),
- 'php_version' => PHP_VERSION,
- )
- ),
- 'acf' => wp_json_encode(
- array(
- 'acf_version' => get_option( 'acf_version' ),
- 'acf_pro' => ( defined( 'ACF_PRO' ) && ACF_PRO ),
- 'block_count' => acf_pro_get_registered_block_count(),
- )
- ),
- );
-
- $response = $this->request( 'v2/plugins/update-check', $post );
-
- if ( is_array( $response ) ) {
- $response['checked'] = $checked;
- }
-
- $expiration = $this->get_expiration( $response, DAY_IN_SECONDS, MONTH_IN_SECONDS );
-
- set_transient( $transient_name, $response, $expiration );
-
- return $response;
- }
-
- public function get_expiration( $response = false, $min = 0, $max = 0 ) {
- $expiration = 0;
-
- if ( is_wp_error( $response ) || is_string( $response ) ) {
- return 5 * MINUTE_IN_SECONDS;
- }
-
- if ( is_array( $response ) && isset( $response['expiration'] ) ) {
- $expiration = (int) $response['expiration'];
- }
-
- if ( $expiration < $min ) {
- return $min;
- }
-
- if ( $expiration > $max ) {
- return $max;
- }
- return $expiration;
- }
-
- function refresh_plugins_transient() {
- delete_site_transient( 'update_plugins' );
- delete_transient( 'acf_plugin_updates' );
- }
-
- function modify_plugins_transient( $transient ) {
-
- if ( ! isset( $transient->response ) ) {
- return $transient;
- }
-
- $force_check = ( $this->checked == 0 ) ? ! empty( $_GET['force-check'] ) : false;
-
- $updates = $this->get_plugin_updates( $force_check );
-
- if ( is_array( $updates ) ) {
- foreach ( $updates['plugins'] as $basename => $update ) {
- $transient->response[ $basename ] = (object) $update;
- }
- }
-
- $this->checked++;
-
- return $transient;
- }
-
- function modify_plugin_details( $result, $action = null, $args = null ) {
-
- $plugin = false;
-
- if ( $action !== 'plugin_information' ) {
- return $result;
- }
-
- $plugin = $this->get_plugin_by( 'slug', $args->slug );
- if ( ! $plugin ) {
- return $result;
- }
-
- $response = $this->get_plugin_info( $plugin['id'] );
-
- if ( ! is_array( $response ) ) {
- return $result;
- }
-
- unset( $response['tags'] );
-
- $response = (object) $response;
-
- $sections = array(
- 'description' => '',
- 'installation' => '',
- 'changelog' => '',
- 'upgrade_notice' => '',
- );
- foreach ( $sections as $k => $v ) {
- $sections[ $k ] = $response->$k;
- }
- $response->sections = $sections;
-
- return $response;
- }
- }
-
- function acf_updates() {
- global $acf_updates;
- if ( ! isset( $acf_updates ) ) {
- $acf_updates = new ACF_Updates();
- }
- return $acf_updates;
- }
-
- function acf_register_plugin_update( $plugin ) {
- acf_updates()->add_plugin( $plugin );
- }
- endif;
|