_config = Dispatcher::config(); } /** * Registers hooks for the plugin's admin functionality. * * Adds actions and filters for admin initialization, AJAX, UI updates, and admin bar menu. * * @return void */ public function run() { add_action( 'admin_init', array( $this, 'admin_init' ) ); add_action( 'w3tc_config_ui_save-w3tc_general', array( $this, 'possible_state_change' ), 2, 10 ); add_action( 'w3tc_message_action_licensing_upgrade', array( $this, 'w3tc_message_action_licensing_upgrade' ) ); add_filter( 'w3tc_admin_bar_menu', array( $this, 'w3tc_admin_bar_menu' ) ); } /** * Adds licensing menu items to the admin bar. * * @param array $menu_items Existing admin bar menu items. * * @return array Modified admin bar menu items. */ public function w3tc_admin_bar_menu( $menu_items ) { if ( ! Util_Environment::is_w3tc_pro( $this->_config ) ) { $menu_items['00020.licensing'] = array( 'id' => 'w3tc_overlay_upgrade', 'parent' => 'w3tc', 'title' => wp_kses( sprintf( // translators: 1 opening HTML span tag, 2 closing HTML span tag. __( '%1$sUpgrade Performance%2$s', 'w3-total-cache' ), '', '' ), array( 'span' => array( 'style' => array(), ), ) ), 'href' => wp_nonce_url( network_admin_url( 'admin.php?page=w3tc_dashboard&w3tc_message_action=licensing_upgrade' ), 'w3tc' ), ); } if ( defined( 'W3TC_DEBUG' ) && W3TC_DEBUG ) { $menu_items['90040.licensing'] = array( 'id' => 'w3tc_debug_overlay_upgrade', 'parent' => 'w3tc_debug_overlays', 'title' => esc_html__( 'Upgrade', 'w3-total-cache' ), 'href' => wp_nonce_url( network_admin_url( 'admin.php?page=w3tc_dashboard&w3tc_message_action=licensing_upgrade' ), 'w3tc' ), ); } return $menu_items; } /** * Handles the licensing upgrade action. * * Adds a hook to modify the admin head for licensing upgrades. * * @return void */ public function w3tc_message_action_licensing_upgrade() { add_action( 'admin_head', array( $this, 'admin_head_licensing_upgrade' ) ); } /** * Outputs JavaScript for the licensing upgrade page. * * @return void */ public function admin_head_licensing_upgrade() { ?> get_string( 'plugin.license_key' ); $new_key_set = ! empty( $new_key ); $old_key = $old_config->get_string( 'plugin.license_key' ); $old_key_set = ! empty( $old_key ); switch ( true ) { // No new key or old key. Do nothing. case ( ! $new_key_set && ! $old_key_set ): return; // Current key set but new is blank, deactivating old. case ( ! $new_key_set && $old_key_set ): $deactivate_result = Licensing_Core::deactivate_license( $old_key ); $changed = true; break; // Current key is blank but new is not, activating new. case ( $new_key_set && ! $old_key_set ): $activate_result = Licensing_Core::activate_license( $new_key, W3TC_VERSION ); $changed = true; if ( $activate_result ) { $config->set( 'common.track_usage', true ); } break; // Current key is set and new different key provided. Deactivating old and activating new. case ( $new_key_set && $old_key_set && $new_key !== $old_key ): $deactivate_result = Licensing_Core::deactivate_license( $old_key ); $activate_result = Licensing_Core::activate_license( $new_key, W3TC_VERSION ); $changed = true; break; } if ( $changed ) { $state = Dispatcher::config_state(); $state->set( 'license.next_check', 0 ); $state->save(); delete_transient( 'w3tc_imageservice_limited' ); $messages = array(); // If the old key was deactivated, add a message. if ( isset( $deactivate_result ) ) { $status = $deactivate_result->license_status; switch ( true ) { case ( strpos( $status, 'inactive.expired.' ) === 0 ): $messages[] = array( 'message' => __( 'Your previous W3 Total Cache Pro license key is expired and will remain registered to this domain.', 'w3-total-cache' ), 'type' => 'error', ); break; case ( strpos( $status, 'inactive.not_present' ) === 0 ): $messages[] = array( 'message' => __( 'Your previous W3 Total Cache Pro license key was not found and cannot be deactivated.', 'w3-total-cache' ), 'type' => 'info', ); break; case ( strpos( $status, 'inactive' ) === 0 ): $messages[] = array( 'message' => __( 'Your previous W3 Total Cache Pro license key has been deactivated.', 'w3-total-cache' ), 'type' => 'info', ); break; case ( strpos( $status, 'invalid' ) === 0 ): $messages[] = array( 'message' => __( 'Your previous W3 Total Cache Pro license key is invalid and cannot be deactivated.', 'w3-total-cache' ), 'type' => 'error', ); break; } } // Handle new activation status. if ( isset( $activate_result ) ) { $status = $activate_result->license_status; switch ( true ) { case ( strpos( $status, 'active' ) === 0 ): $messages[] = array( 'message' => __( 'The W3 Total Cache Pro license key you provided is valid and has been applied.', 'w3-total-cache' ), 'type' => 'info', ); break; } } // Store messages for processing. update_option( 'license_update_messages', $messages ); } } /** * Initializes admin-specific features and hooks. * * Adds admin notices, UI filters, and license status checks. * * @return void */ public function admin_init() { $capability = apply_filters( 'w3tc_capability_admin_notices', 'manage_options' ); $this->maybe_update_license_status(); if ( current_user_can( $capability ) ) { if ( is_admin() ) { /** * Only admin can see W3TC notices and errors */ if ( ! Util_Environment::is_wpmu() ) { add_action( 'admin_notices', array( $this, 'admin_notices' ), 1, 1 ); } add_action( 'network_admin_notices', array( $this, 'admin_notices' ), 1, 1 ); if ( Util_Admin::is_w3tc_admin_page() ) { add_filter( 'w3tc_notes', array( $this, 'w3tc_notes' ) ); } } } } /** * Checks if a status starts with a specific prefix. * * @param string $s The status string. * @param string $starts_with The prefix to check against. * * @return bool True if the status starts with the prefix, false otherwise. */ private function _status_is( $s, $starts_with ) { $s .= '.'; $starts_with .= '.'; return substr( $s, 0, strlen( $starts_with ) ) === $starts_with; } /** * Displays admin notices related to licensing. * * phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedStylesheet * phpcs:disable WordPress.WP.EnqueuedResources.NonEnqueuedScript * * @return void */ public function admin_notices() { $message = ''; $state = Dispatcher::config_state(); $status = $state->get_string( 'license.status' ); switch ( true ) { case $this->_status_is( $status, 'inactive.expired' ): $message = wp_kses( sprintf( // Translators: 1 HTML input button to renew license. __( 'Your W3 Total Cache Pro license key has expired. %1$s to continue using the Pro features', 'w3-total-cache' ), '' ), array( 'input' => array( 'type' => array(), 'class' => array(), 'data-nonce' => array(), 'data-renew-key' => array(), 'data-src' => array(), 'value' => array(), ), ) ); break; case $this->_status_is( $status, 'inactive.by_rooturi' ) || $this->_status_is( $status, 'inactive.by_rooturi.activations_limit_not_reached' ): $message = wp_kses( sprintf( // Translators: 1 opening HTML a tag to license rest link, 2 closing HTML a tag. __( 'Your W3 Total Cache license key is not active for this site. You can switch your license to this website following %1$sthis link%2$s', 'w3-total-cache' ), '', '' ), array( 'a' => array( 'class' => array(), 'href' => array(), ), ) ); break; case $this->_status_is( $status, 'inactive.by_rooturi.activations_limit_reached' ): $message = __( 'Your W3 Total Cache license key is not active and cannot be activated due to the license activation limit being reached.', 'w3-total-cache' ); break; case $this->_status_is( $status, 'inactive' ): $message = __( 'The W3 Total Cache license key is not active.', 'w3-total-cache' ); break; case $this->_status_is( $status, 'invalid' ): $license_url = admin_url( 'admin.php?page=w3tc_general#licensing' ); $network_license_url = network_admin_url( 'admin.php?page=w3tc_general#licensing' ); $message = sprintf( // Translators: 1 opening HMTL a tag to license setting, 2 closing HTML a tag. __( 'Your current W3 Total Cache Pro license key is not valid. %1$sPlease confirm it%2$s.', 'w3-total-cache' ), '', '' ); break; case ( 'no_key' === $status || $this->_status_is( $status, 'active' ) ): // License is active, do nothing. break; default: $message = __( 'The W3 Total Cache license key cannot be verified.', 'w3-total-cache' ); break; } if ( $message ) { if ( ! Util_Admin::is_w3tc_admin_page() ) { echo ''; echo ''; } Util_Ui::error_box( '
' . $message . '
' ); } $license_update_messages = get_option( 'license_update_messages' ); if ( $license_update_messages ) { foreach ( $license_update_messages as $message_data ) { if ( 'error' === $message_data['type'] ) { Util_Ui::error_box( '' . $message_data['message'] . '
' ); } elseif ( 'info' === $message_data['type'] ) { Util_Ui::e_notification_box( '' . $message_data['message'] . '
' ); } } delete_option( 'license_update_messages' ); } } /** * Modifies the notes displayed in the W3TC UI. * * @param array $notes Existing notes to display. * * @return array Modified notes with licensing terms. */ public function w3tc_notes( $notes ) { $terms = ''; $state_master = Dispatcher::config_state_master(); if ( Util_Environment::is_pro_constant( $this->_config ) ) { $terms = 'accept'; } elseif ( ! Util_Environment::is_w3tc_pro( $this->_config ) ) { $terms = $state_master->get_string( 'license.community_terms' ); $buttons = sprintf( '