123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597 |
- <?php
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- if ( ! class_exists( 'acf_pro_updates' ) ) :
- class acf_pro_updates {
-
- function __construct() {
-
- add_action( 'init', array( $this, 'init' ), 20 );
- }
-
- function init() {
-
- if ( ! acf_get_setting( 'show_updates' ) ) {
- return;
- }
-
- if ( ! acf_is_plugin_active() ) {
- return;
- }
-
- acf_register_plugin_update(
- array(
- 'id' => 'pro',
- 'key' => acf_pro_get_license_key(),
- 'slug' => acf_get_setting( 'slug' ),
- 'basename' => acf_get_setting( 'basename' ),
- 'version' => acf_get_setting( 'version' ),
- )
- );
- add_action( 'admin_init', 'acf_pro_check_defined_license', 20 );
- add_action( 'current_screen', 'acf_pro_display_activation_error', 30 );
-
- if ( is_admin() ) {
- add_action( 'in_plugin_update_message-' . acf_get_setting( 'basename' ), array( $this, 'modify_plugin_update_message' ), 10, 2 );
- }
- }
-
- function modify_plugin_update_message( $plugin_data, $response ) {
-
- if ( acf_pro_get_license_key() ) {
- return;
- }
-
- echo '<br />' . sprintf( __( 'To enable updates, please enter your license key on the <a href="%1$s">Updates</a> page. If you don\'t have a licence key, please see <a href="%2$s" target="_blank">details & pricing</a>.', 'acf' ), admin_url( 'edit.php?post_type=acf-field-group&page=acf-settings-updates' ), acf_add_url_utm_tags( 'https://www.advancedcustomfields.com/pro/', 'ACF upgrade', 'updates' ) );
- }
- }
-
- new acf_pro_updates();
- endif;
- function acf_pro_check_defined_license() {
-
- if ( ! defined( 'ACF_PRO_LICENSE' ) || empty( ACF_PRO_LICENSE ) || ! is_string( ACF_PRO_LICENSE ) ) {
- return;
- }
-
- if ( ! acf_get_setting( 'show_admin' ) ) {
- return;
- }
-
- if ( acf_verify_nonce( 'acf_delete_activation_transient' ) || ( isset( $_REQUEST['acf_retry_nonce'] ) && wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['acf_retry_nonce'] ) ), 'acf_retry_activation' ) ) ) {
- delete_transient( 'acf_activation_error' );
- } else {
-
- $activation_data = acf_pro_get_activation_failure_transient();
- if ( $activation_data && $activation_data['license'] === ACF_PRO_LICENSE ) {
- return;
- }
- }
-
- $license = acf_pro_get_license();
- if ( $license ) {
-
- if ( acf_pro_get_license_key() !== ACF_PRO_LICENSE ) {
-
- $deactivation_response = acf_pro_deactivate_license( true );
-
- if ( is_wp_error( $deactivation_response ) ) {
- return acf_pro_set_activation_failure_transient( __( '<b>ACF Activation Error</b>. Your defined license key has changed, but an error occurred when connecting to activation server', 'acf' ) . ' <span class="description">(' . esc_html( $deactivation_response->get_error_message() ) . ').</span>', ACF_PRO_LICENSE );
-
- } elseif ( ! $deactivation_response['success'] ) {
- return acf_pro_set_activation_failure_transient( __( '<b>ACF Activation Error</b>. Your defined license key has changed, but an error occurred when deactivating your old licence', 'acf' ) . ' <span class="description">(' . $deactivation_response['message'] . ').</span>', ACF_PRO_LICENSE );
- }
- } else {
-
- $basename = acf_get_setting( 'basename' );
- $update = acf_updates()->get_plugin_update( $basename );
- if ( isset( $update['license_valid'] ) && ! $update['license_valid'] ) {
-
- acf_pro_update_license( '' );
- } else {
-
- return;
- }
- }
- }
-
- $activation_response = acf_pro_activate_license( ACF_PRO_LICENSE, true );
- $error_text = false;
-
- if ( is_wp_error( $activation_response ) ) {
- $error_text = __( '<b>ACF Activation Error</b>. An error occurred when connecting to activation server', 'acf' ) . ' <span class="description">(' . esc_html( $activation_response->get_error_message() ) . ').</span>';
-
- } elseif ( ! $activation_response['success'] ) {
- $error_text = __( '<b>ACF Activation Error</b>', 'acf' ) . ': <span class="description">' . $activation_response['message'] . '.</span>';
- } else {
-
- delete_transient( 'acf_activation_error' );
-
- acf_add_admin_notice( '<b>ACF </b>' . acf_esc_html( $activation_response['message'] ), 'success' );
- return;
- }
- return acf_pro_set_activation_failure_transient( $error_text, ACF_PRO_LICENSE );
- }
- function acf_pro_set_activation_failure_transient( $error_text, $license_key ) {
- set_transient(
- 'acf_activation_error',
- array(
- 'error' => $error_text,
- 'license' => $license_key,
- ),
- HOUR_IN_SECONDS
- );
- }
- function acf_pro_get_activation_failure_transient() {
- return get_transient( 'acf_activation_error' );
- }
- function acf_pro_display_activation_error() {
-
- if ( ! is_admin() ) {
- return;
- }
-
- if ( ! acf_current_user_can_admin() ) {
- return;
- }
-
- $activation_data = acf_pro_get_activation_failure_transient();
-
- if ( ! $activation_data ) {
- return;
- }
-
- if ( ! defined( 'ACF_PRO_LICENSE' ) || empty( ACF_PRO_LICENSE ) || ! is_string( ACF_PRO_LICENSE ) ) {
- delete_transient( 'acf_activation_error' );
- return;
- }
-
- global $plugin_page;
- if ( ! $plugin_page || 'acf-settings-updates' !== $plugin_page ) {
- $nonce = wp_create_nonce( 'acf_retry_activation' );
- $check_again_url = admin_url( 'edit.php?post_type=acf-field-group&page=acf-settings-updates&acf_retry_nonce=' . $nonce );
- $activation_data['error'] = $activation_data['error'] . ' <a href="' . $check_again_url . '">' . __( 'Check Again', 'acf' ) . '</a>';
- }
-
- acf_add_admin_notice( acf_esc_html( $activation_data['error'] ), 'error', false );
- }
- function acf_pro_get_license() {
-
- $license = get_option( 'acf_pro_license' );
-
- if ( ! $license ) {
- return false;
- }
-
- $license = maybe_unserialize( base64_decode( $license ) );
-
- if ( ! is_array( $license ) ) {
- return false;
- }
-
- return $license;
- }
- function acf_get_home_url() {
-
- add_filter( 'wpml_get_home_url', 'acf_licence_wpml_intercept', 99, 2 );
- $home_url = home_url();
-
- remove_filter( 'wpml_get_home_url', 'acf_licence_wpml_intercept', 99 );
- return $home_url;
- }
- function acf_licence_wpml_intercept( $home_url, $url ) {
- return $url;
- }
- function acf_pro_get_license_key( $skip_url_check = false ) {
- $license = acf_pro_get_license();
- $home_url = acf_get_home_url();
-
- if ( ! $license || ! $license['key'] ) {
- return false;
- }
-
- if ( ! $skip_url_check && acf_strip_protocol( $license['url'] ) !== acf_strip_protocol( $home_url ) ) {
- return false;
- }
-
- return $license['key'];
- }
- function acf_pro_update_license( $key = '' ) {
-
- $value = '';
-
- if ( $key ) {
-
- $data = array(
- 'key' => $key,
- 'url' => acf_get_home_url(),
- );
-
- $value = base64_encode( maybe_serialize( $data ) );
- }
-
- acf_register_plugin_update(
- array(
- 'id' => 'pro',
- 'key' => $key,
- 'slug' => acf_get_setting( 'slug' ),
- 'basename' => acf_get_setting( 'basename' ),
- 'version' => acf_get_setting( 'version' ),
- )
- );
-
- return update_option( 'acf_pro_license', $value );
- }
- function acf_pro_get_registered_block_count() {
- return acf_get_store( 'block-types' )->count();
- }
- function acf_pro_activate_license( $license_key, $silent = false ) {
-
- $post = array(
- 'acf_license' => trim( $license_key ),
- 'acf_version' => acf_get_setting( 'version' ),
- '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,
- 'block_count' => acf_pro_get_registered_block_count(),
- );
- $response = acf_updates()->request( 'v2/plugins/activate?p=pro', $post );
-
- if ( is_string( $response ) ) {
- $response = new WP_Error( 'server_error', esc_html( $response ) );
- }
-
- if ( is_wp_error( $response ) ) {
- if ( ! $silent ) {
- display_wp_activation_error( $response );
- }
- return $response;
- }
- $success = false;
-
- if ( $response['status'] == 1 ) {
-
- acf_pro_update_license( $response['license'] );
-
- acf_updates()->refresh_plugins_transient();
-
- if ( ! $silent ) {
- acf_add_admin_notice( acf_esc_html( $response['message'] ), 'success' );
- }
- $success = true;
-
- } else {
-
- if ( ! $silent ) {
- acf_add_admin_notice( acf_esc_html( $response['message'] ), 'warning' );
- }
- }
-
- return array(
- 'success' => $success,
- 'message' => $response['message'],
- );
- }
- function acf_pro_deactivate_license( $silent = false ) {
-
- $license = acf_pro_get_license_key( true );
-
- if ( ! $license ) {
- return false;
- }
-
- $post = array(
- 'acf_license' => $license,
- 'wp_url' => acf_get_home_url(),
- );
- $response = acf_updates()->request( 'v2/plugins/deactivate?p=pro', $post );
-
- if ( is_string( $response ) ) {
- $response = new WP_Error( 'server_error', esc_html( $response ) );
- }
-
- if ( is_wp_error( $response ) ) {
- if ( ! $silent ) {
- display_wp_activation_error( $response );
- }
- return $response;
- }
-
- acf_pro_update_license( '' );
-
- acf_updates()->refresh_plugins_transient();
- $success = $response['status'] == 1;
- if ( ! $silent ) {
- $notice_class = $success ? 'info' : 'warning';
- acf_add_admin_notice( acf_esc_html( $response['message'] ), $notice_class );
- }
-
- return array(
- 'success' => $success,
- 'message' => $response['message'],
- );
- }
- function display_wp_activation_error( $wp_error ) {
-
- if ( acf_has_done( 'display_wp_error' ) ) {
- return;
- }
-
- acf_new_admin_notice(
- array(
- 'text' => __( '<b>ACF Activation Error</b>. Could not connect to activation server', 'acf' ) . ' <span class="description">(' . esc_html( $wp_error->get_error_message() ) . ').</span>',
- 'type' => 'error',
- )
- );
- }
|