diff --git a/src/Admin.php b/src/Admin.php index eae2b08..e859bd9 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -7,7 +7,7 @@ */ namespace Shazzad\PluginUpdater; -if ( ! defined( 'ABSPATH' ) ) { +if ( ! \defined( 'ABSPATH' ) ) { exit; } @@ -16,14 +16,28 @@ /** * Class Admin * - * Handles plugin update checks, license verification, and upgrade processes. + * Renders the license management admin page and handles license save/verify via POST. * * @since 1.0 */ class Admin { + /** + * Integration instance holding shared state and API helpers. + * + * @since 1.0 + * + * @var Integration + */ public Integration $integration; + /** + * Constructor. + * + * @since 1.0 + * + * @param Integration $integration Integration instance. + */ public function __construct( Integration $integration ) { $this->integration = $integration; @@ -38,7 +52,7 @@ public function __construct( Integration $integration ) { */ public function admin_menu() { if ( empty( $this->integration->menu_label ) ) { - $this->integration->menu_label = sprintf( '%s Updates', $this->integration->product_name ); + $this->integration->menu_label = \sprintf( '%s Updates', $this->integration->product_name ); } if ( empty( $this->integration->menu_parent ) ) { @@ -64,63 +78,67 @@ public function admin_menu() { * @return void */ public function load_page() { + if ( ! isset( $_POST['wprepo_update'] ) ) { + return; + } + + check_admin_referer( 'wprepo_license_update' ); + $base_url = remove_query_arg( [ 'm' ] ); - if ( isset( $_POST['wprepo_update'] ) ) { - check_admin_referer( 'wprepo_license_update' ); - if ( empty( $_POST['wprepo_license'] ) ) { - delete_option( $this->integration->get_license_option() ); - $this->integration->clear_updates_transient(); - wp_redirect( - add_query_arg( - 'message', - urlencode( 'License deactivated' ), - $base_url - ) - ); - } else { - $key = sanitize_text_field( $_POST['wprepo_license'] ); - $response = $this->integration->api_request( 'check_license', $key ); - - if ( is_wp_error( $response ) ) { - $message = $response->get_error_message(); - wp_redirect( - add_query_arg( - 'error', - urlencode( $message ), - $base_url - ) - ); - } elseif ( ! empty( $response['license'] ) ) { - update_option( $this->integration->get_license_option(), $key ); - update_option( - $this->integration->license_name . '_data', - $response['license'] - ); + if ( empty( $_POST['wprepo_license'] ) ) { + delete_option( $this->integration->get_license_code_key() ); + $this->integration->clear_updates_transient(); + wp_redirect( + add_query_arg( + 'message', + urlencode( 'License deactivated' ), + $base_url + ) + ); + exit; + } - $this->integration->refresh_updates_transient(); - wp_redirect( - add_query_arg( - 'message', - urlencode( 'License activated' ), - $base_url - ) - ); - } else { - $message = ! empty( $response['message'] ) - ? $response['message'] - : 'Invalid License Key'; - wp_redirect( - add_query_arg( - 'error', - urlencode( $message ), - $base_url - ) - ); - } - } + $key = sanitize_text_field( $_POST['wprepo_license'] ); + $response = $this->integration->api_request( 'check_license', $key ); + + if ( is_wp_error( $response ) ) { + wp_redirect( + add_query_arg( + 'error', + urlencode( $response->get_error_message() ), + $base_url + ) + ); + exit; + } + + if ( ! empty( $response['license'] ) ) { + update_option( $this->integration->get_license_code_key(), $key ); + $this->integration->update_license_data( $response['license'] ); + + $this->integration->refresh_updates_transient(); + wp_redirect( + add_query_arg( + 'message', + urlencode( 'License activated' ), + $base_url + ) + ); exit; } + + $message = ! empty( $response['message'] ) + ? $response['message'] + : 'Invalid License Key'; + wp_redirect( + add_query_arg( + 'error', + urlencode( $message ), + $base_url + ) + ); + exit; } /** @@ -134,7 +152,7 @@ public function admin_page() {
%s
%s