| @@ -13,8 +13,9 @@ | ||
| 13 | 13 | public function __construct() { |
| 14 | 14 | add_action( 'admin_menu', array( $this, 'patternswp_add_license_page' ) ); |
| 15 | 15 | add_action( 'admin_init', array( $this, 'patternswp_save_license_key' ) ); |
| 16 | 16 | add_action( 'admin_notices', array( $this, 'patternswp_display_admin_notice' ) ); |
| 17 | + add_action( 'wp_ajax_patternswp_activate_license', array( $this, 'ajax_activate_license' ) ); | |
| 17 | 18 | } |
| 18 | 19 | |
| 19 | 20 | /** |
| 20 | 21 | * Add License Page |
| @@ -23,58 +24,20 @@ | ||
| 23 | 24 | add_submenu_page( 'patternswp-plugin-menu', 'License', 'License', 'manage_options', 'patternswp-license_section', array( $this, 'patternswp_plugin_license_section' ) ); |
| 24 | 25 | } |
| 25 | 26 | |
| 26 | 27 | /** |
| 27 | - * License Section | |
| 28 | + * License Section — React admin mount. | |
| 28 | 29 | */ |
| 29 | 30 | public function patternswp_plugin_license_section() { |
| 30 | - $license_key = $this->patternswp_license_key_option = get_option( 'patternswp_license_key' ); | |
| 31 | - $license_key = isset( $license_key['patternswp_pro_license_key'] ) ? $license_key['patternswp_pro_license_key'] : ''; | |
| 32 | - if (!empty( $license_key ) && strlen( $license_key ) > 8) { | |
| 33 | - $masked_key = substr( $license_key, 0, 8 ) . str_repeat( 'X', strlen( $license_key ) - 8 ); | |
| 34 | - } else { | |
| 35 | - $masked_key = $license_key; | |
| 31 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 32 | + return; | |
| 36 | 33 | } |
| 37 | - $get_license_status = get_option( 'patternswp_plugin_license_data' ); | |
| 38 | - $license_status = isset( $get_license_status['activated'] ) ? $get_license_status['activated'] : false; | |
| 39 | 34 | ?> |
| 40 | - <div class="wrap"> | |
| 41 | - <h1><?php esc_attr_e( 'License', 'patternswp' ); ?></h1> | |
| 42 | - <?php settings_errors(); ?> | |
| 43 | - <h2 class="nav-tab-wrapper"> | |
| 44 | - <?php | |
| 45 | - $patternswp_admin = new PatternsWP_Admin(); | |
| 46 | - $patternswp_admin->patterswp_tab( 'License' ); | |
| 47 | - ?> | |
| 48 | - </h2> | |
| 49 | - <div class="patterns-wp-tabs-content"> | |
| 50 | - <div id="tab-1" class="patterns-wp-tab-content patterns-wp-tab-active"> | |
| 51 | - <div class="wrap"> | |
| 52 | - | |
| 53 | - <div class="container" style="padding: 40px;background-color: white;border-radius: 5px;overflow: hidden;width: 70%;"> | |
| 54 | - <div class="about__section has-1-columns"> | |
| 55 | - <div class="column" style=""> | |
| 56 | - <h1 class="feature-title"><?php esc_html_e('License', 'patternswp'); ?></h1> | |
| 57 | - <p><?php esc_html_e('Already purchased? Simply enter your license key below to connect with PatternsWP Pro!', 'patternswp'); ?></p> | |
| 58 | - </div> | |
| 59 | - <div style="height: 1px; background-color: #ccc; width: 100%; margin: 20px 0;"></div> | |
| 60 | - </div> | |
| 61 | - <div class=""> | |
| 62 | - <form id="patternswp_license_form" method="post" > | |
| 63 | - <input class="regular-text" type="text" placeholder="Enter your license key here..." name="patternswp_license_key[patternswp_pro_license_key]" id="patternswp_pro_license_key" value="<?php echo esc_attr( $masked_key ) ?>"> | |
| 64 | - <?php wp_nonce_field('patternswp_save_license_action', 'patternswp_license_nonce'); ?> | |
| 65 | - <input name="patternswp_save_license" type="submit" id="custom_submit_btn" class="button button-primary" value="Save & Activate" style="margin-left: 5px;"> | |
| 66 | - <?php | |
| 67 | - if ( $license_status == true ) { | |
| 68 | - echo '<div style="margin-top: 10px;"><span style="color: green;font-weight: bold;">Your license is active and verified. Enjoy all premium features!</span></div>'; | |
| 69 | - } | |
| 70 | - ?> | |
| 71 | - <div> | |
| 72 | - </div> | |
| 73 | - </form> | |
| 74 | - </div> | |
| 75 | - </div> | |
| 76 | - </div> | |
| 35 | + <div class="wrap patternswp-admin-wrap"> | |
| 36 | + <h1 class="screen-reader-text"><?php esc_html_e( 'License', 'patternswp' ); ?></h1> | |
| 37 | + <div id="patternswp-admin-root"> | |
| 38 | + <div class="patternswp-admin__loading"> | |
| 39 | + <span class="spinner is-active" style="float:none;margin:0;"></span> | |
| 77 | 40 | </div> |
| 78 | 41 | </div> |
| 79 | 42 | </div> |
| 80 | 43 | <?php |
| @@ -80,24 +43,161 @@ | ||
| 80 | 43 | <?php |
| 81 | 44 | } |
| 82 | 45 | |
| 83 | 46 | /** |
| 84 | - * Save License Key | |
| 47 | + * Mask a license key for display. | |
| 48 | + * | |
| 49 | + * @param string $license_key Raw license key. | |
| 50 | + * @return string | |
| 85 | 51 | */ |
| 52 | + private function mask_license_key( $license_key ) { | |
| 53 | + if ( ! empty( $license_key ) && strlen( $license_key ) > 8 ) { | |
| 54 | + return substr( $license_key, 0, 8 ) . str_repeat( 'X', strlen( $license_key ) - 8 ); | |
| 55 | + } | |
| 56 | + return $license_key; | |
| 57 | + } | |
| 58 | + | |
| 59 | + /** | |
| 60 | + * AJAX: activate license key. | |
| 61 | + */ | |
| 62 | + public function ajax_activate_license() { | |
| 63 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 64 | + wp_send_json_error( array( 'message' => __( 'You do not have permission to manage the license.', 'patternswp' ) ), 403 ); | |
| 65 | + } | |
| 66 | + | |
| 67 | + check_ajax_referer( 'patternswp_admin_nonce', 'nonce' ); | |
| 68 | + | |
| 69 | + $license_key = isset( $_POST['license_key'] ) ? sanitize_text_field( wp_unslash( $_POST['license_key'] ) ) : ''; | |
| 70 | + | |
| 71 | + // Ignore masked keys (contain X padding from previous activation display). | |
| 72 | + if ( false !== strpos( $license_key, 'X' ) && preg_match( '/X{4,}/', $license_key ) ) { | |
| 73 | + $stored = get_option( 'patternswp_license_key', array() ); | |
| 74 | + $stored_key = isset( $stored['patternswp_pro_license_key'] ) ? $stored['patternswp_pro_license_key'] : ''; | |
| 75 | + if ( ! empty( $stored_key ) && $this->mask_license_key( $stored_key ) === $license_key ) { | |
| 76 | + $license_data = get_option( 'patternswp_plugin_license_data', array() ); | |
| 77 | + if ( ! empty( $license_data['activated'] ) ) { | |
| 78 | + wp_send_json_success( | |
| 79 | + array( | |
| 80 | + 'message' => __( 'License Key is already activated.', 'patternswp' ), | |
| 81 | + 'status' => 'info', | |
| 82 | + 'activated' => true, | |
| 83 | + 'maskedKey' => $this->mask_license_key( $stored_key ), | |
| 84 | + ) | |
| 85 | + ); | |
| 86 | + } | |
| 87 | + $license_key = $stored_key; | |
| 88 | + } | |
| 89 | + } | |
| 90 | + | |
| 91 | + if ( empty( $license_key ) ) { | |
| 92 | + wp_send_json_error( | |
| 93 | + array( | |
| 94 | + 'message' => __( 'License activation failed: License Key cannot be empty. Please enter a valid license key.', 'patternswp' ), | |
| 95 | + ) | |
| 96 | + ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + $result = $this->activate_license_ajax( $license_key ); | |
| 100 | + | |
| 101 | + if ( is_wp_error( $result ) ) { | |
| 102 | + wp_send_json_error( array( 'message' => $result->get_error_message() ) ); | |
| 103 | + } | |
| 104 | + | |
| 105 | + wp_send_json_success( $result ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Activate license and return structured result (no redirects). | |
| 110 | + * | |
| 111 | + * @param string $license_key License key. | |
| 112 | + * @return array|WP_Error | |
| 113 | + */ | |
| 114 | + public function activate_license_ajax( $license_key ) { | |
| 115 | + $stored = get_option( 'patternswp_license_key', array() ); | |
| 116 | + $old_key = isset( $stored['patternswp_pro_license_key'] ) ? $stored['patternswp_pro_license_key'] : ''; | |
| 117 | + $license_data = get_option( 'patternswp_plugin_license_data', array() ); | |
| 118 | + | |
| 119 | + if ( $old_key === $license_key && ! empty( $license_data['activated'] ) ) { | |
| 120 | + return array( | |
| 121 | + 'message' => __( 'License Key is already activated.', 'patternswp' ), | |
| 122 | + 'status' => 'info', | |
| 123 | + 'activated' => true, | |
| 124 | + 'maskedKey' => $this->mask_license_key( $license_key ), | |
| 125 | + ); | |
| 126 | + } | |
| 127 | + | |
| 128 | + $activation_url = PATTERSWP_PLUGIN_API_URL . '/activate'; | |
| 129 | + $data = array( | |
| 130 | + 'license_key' => $license_key, | |
| 131 | + 'instance_name' => home_url(), | |
| 132 | + ); | |
| 133 | + $response = wp_remote_post( | |
| 134 | + $activation_url, | |
| 135 | + array( | |
| 136 | + 'body' => $data, | |
| 137 | + 'headers' => array( 'Accept' => 'application/json' ), | |
| 138 | + 'timeout' => 15, | |
| 139 | + ) | |
| 140 | + ); | |
| 141 | + | |
| 142 | + if ( is_wp_error( $response ) ) { | |
| 143 | + return new WP_Error( | |
| 144 | + 'patternswp_license_request_failed', | |
| 145 | + $response->get_error_message() | |
| 146 | + ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + $body = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 150 | + | |
| 151 | + if ( isset( $body->error ) && ! empty( $body->error ) ) { | |
| 152 | + $error = $body->error; | |
| 153 | + if ( 'license_key not found.' === $error ) { | |
| 154 | + $error = __( 'License activation failed: Please enter a valid license key.', 'patternswp' ); | |
| 155 | + } | |
| 156 | + return new WP_Error( 'patternswp_license_invalid', $error ); | |
| 157 | + } | |
| 158 | + | |
| 159 | + if ( isset( $body->activated ) && true === $body->activated ) { | |
| 160 | + update_option( 'patternswp_plugin_license_data', (array) $body ); | |
| 161 | + $this->clear_transients(); | |
| 162 | + update_option( | |
| 163 | + 'patternswp_license_key', | |
| 164 | + array( 'patternswp_pro_license_key' => $license_key ) | |
| 165 | + ); | |
| 166 | + do_action( 'patternswp_load_patterns_by_ra' ); | |
| 167 | + | |
| 168 | + return array( | |
| 169 | + 'message' => __( 'License activated successfully.', 'patternswp' ), | |
| 170 | + 'status' => 'success', | |
| 171 | + 'activated' => true, | |
| 172 | + 'maskedKey' => $this->mask_license_key( $license_key ), | |
| 173 | + ); | |
| 174 | + } | |
| 175 | + | |
| 176 | + return new WP_Error( | |
| 177 | + 'patternswp_license_failed', | |
| 178 | + __( 'License activation failed. Please try again.', 'patternswp' ) | |
| 179 | + ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Save License Key (legacy form POST — kept for compatibility). | |
| 184 | + */ | |
| 86 | 185 | public function patternswp_save_license_key() { |
| 87 | 186 | $this->patternswp_license_key_option = get_option( 'patternswp_license_key' ); |
| 88 | 187 | |
| 89 | 188 | if ( isset($_POST['patternswp_save_license'] ) ) { |
| 90 | 189 | if (!isset( $_POST['patternswp_license_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['patternswp_license_nonce'] ) ), 'patternswp_save_license_action') ) { |
| 91 | - wp_die(esc_attr('Security check failed. Please try again.', 'patternswp')); | |
| 190 | + wp_die(esc_html__('Security check failed. Please try again.', 'patternswp')); | |
| 92 | 191 | } |
| 93 | 192 | if ( !current_user_can('manage_options' ) ) { |
| 94 | - wp_die(esc_attr('You do not have sufficient permissions to perform this action.', 'patternswp')); | |
| 193 | + wp_die(esc_html__('You do not have sufficient permissions to perform this action.', 'patternswp')); | |
| 95 | 194 | } |
| 96 | 195 | |
| 97 | 196 | if ( isset( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) && !empty( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) ) { |
| 98 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 99 | - $license_k = isset( $_POST['patternswp_license_key'] ) ? $_POST['patternswp_license_key'] : ''; | |
| 197 | + $license_k = array( | |
| 198 | + 'patternswp_pro_license_key' => sanitize_text_field( wp_unslash( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) ), | |
| 199 | + ); | |
| 100 | 200 | $empty_option = array(); |
| 101 | 201 | if( !empty( $license_k ) ){ |
| 102 | 202 | $empty_option['patternswp_pro_license_key'] = $this->patternswp_license_key_option['patternswp_pro_license_key']; |
| 103 | 203 | $this->handle_license_activation( $empty_option, $license_k ); |
| @@ -156,49 +256,27 @@ | ||
| 156 | 256 | * |
| 157 | 257 | * @param string $license_key License key. |
| 158 | 258 | */ |
| 159 | 259 | public function activate_license( $license_key ) { |
| 160 | - global $wpdb; | |
| 161 | - | |
| 162 | - $activation_url = PATTERSWP_PLUGIN_API_URL . '/activate'; | |
| 163 | - $data = array( 'license_key' => $license_key, 'instance_name' => home_url() ); | |
| 164 | - $response = wp_remote_post( $activation_url, array( 'body' => $data, 'headers' => array( 'Accept' => 'application/json' ), 'sslverify' => false, 'timeout' => 10 ) ); | |
| 165 | - $response = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 166 | - | |
| 167 | - if ( isset( $response->error ) && !empty( $response->error ) ) { | |
| 168 | - if( $response->error == 'license_key not found.' ){ | |
| 169 | - $response->error = 'License activation failed: Please enter a valid license key.'; | |
| 170 | - } | |
| 260 | + $result = $this->activate_license_ajax( $license_key ); | |
| 171 | 261 | |
| 172 | - $redirect_url = add_query_arg( array( | |
| 173 | - 'page' => 'patternswp-license_section', | |
| 174 | - 'pt_msg' => urlencode( $response->error ), | |
| 175 | - 'status' => 'error' | |
| 262 | + if ( is_wp_error( $result ) ) { | |
| 263 | + $redirect_url = add_query_arg( array( | |
| 264 | + 'page' => 'patternswp-license_section', | |
| 265 | + 'pt_msg' => urlencode( $result->get_error_message() ), | |
| 266 | + 'status' => 'error', | |
| 176 | 267 | ), admin_url( 'admin.php' ) ); |
| 177 | 268 | wp_safe_redirect( $redirect_url ); |
| 178 | 269 | exit; |
| 179 | 270 | } |
| 180 | 271 | |
| 181 | - if( isset( $response->activated ) && $response->activated == true ) { | |
| 182 | - // Store license data in options | |
| 183 | - $license_data = (array) $response; | |
| 184 | - update_option( 'patternswp_plugin_license_data', $license_data ); | |
| 185 | - $this->clear_transients(); | |
| 186 | - | |
| 187 | - $license_key_data = array( 'patternswp_pro_license_key' => $license_key ); | |
| 188 | - $license_key = update_option( 'patternswp_license_key', $license_key_data ); | |
| 189 | - | |
| 190 | - do_action( 'patternswp_load_patterns_by_ra' ); | |
| 191 | - | |
| 192 | - $redirect_url = add_query_arg( array( | |
| 193 | - 'page' => 'patternswp-license_section', | |
| 194 | - 'pt_msg' => urlencode( 'License activated successfully.' ), | |
| 195 | - 'status' => 'success' | |
| 196 | - ), admin_url( 'admin.php' ) ); | |
| 197 | - wp_safe_redirect( $redirect_url ); | |
| 198 | - exit; | |
| 199 | - | |
| 200 | - } | |
| 272 | + $redirect_url = add_query_arg( array( | |
| 273 | + 'page' => 'patternswp-license_section', | |
| 274 | + 'pt_msg' => urlencode( $result['message'] ), | |
| 275 | + 'status' => isset( $result['status'] ) ? $result['status'] : 'success', | |
| 276 | + ), admin_url( 'admin.php' ) ); | |
| 277 | + wp_safe_redirect( $redirect_url ); | |
| 278 | + exit; | |
| 201 | 279 | } |
| 202 | 280 | |
| 203 | 281 | /** |
| 204 | 282 | * Clear transients |
| @@ -230,11 +308,17 @@ | ||
| 230 | 308 | } |
| 231 | 309 | } |
| 232 | 310 | |
| 233 | 311 | /** |
| 234 | - * Display admin notice | |
| 312 | + * Display admin notice (legacy URL flash messages — React also reads these via localize). | |
| 235 | 313 | */ |
| 236 | 314 | public function patternswp_display_admin_notice() { |
| 315 | + // Notices are shown inside the React admin app when on PatternsWP pages. | |
| 316 | + $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; | |
| 317 | + if ( $screen && isset( $screen->id ) && false !== strpos( $screen->id, 'patternswp' ) ) { | |
| 318 | + return; | |
| 319 | + } | |
| 320 | + | |
| 237 | 321 | if ( isset( $_GET['pt_msg'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 238 | 322 | $message = isset($_GET['pt_msg']) ? esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 239 | 323 | $status = isset( $_GET['status'] ) && sanitize_text_field( wp_unslash( $_GET['status'] ) ) === 'success' ? 'updated' : 'error'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 240 | 324 | printf( '<div class="%s notice is-dismissible"><p>%s</p></div>', esc_attr( $status ), esc_attr( $message ) ); |
| @@ -241,5 +325,5 @@ | ||
| 241 | 325 | } |
| 242 | 326 | } |
| 243 | 327 | } |
| 244 | 328 | |
| 245 | -new PatternsWP_LicenseSection(); | |
| 329 | +new PatternsWP_LicenseSection(); | |