patternswp_license_key_option = get_option( 'patternswp_license_key' );
$license_key = isset( $license_key['patternswp_pro_license_key'] ) ? $license_key['patternswp_pro_license_key'] : '';
if (!empty( $license_key ) && strlen( $license_key ) > 8) {
$masked_key = substr( $license_key, 0, 8 ) . str_repeat( 'X', strlen( $license_key ) - 8 );
} else {
$masked_key = $license_key;
}
$get_license_status = get_option( 'patternswp_plugin_license_data' );
$license_status = isset( $get_license_status['activated'] ) ? $get_license_status['activated'] : false;
?>
patterswp_tab( 'License' );
?>
patternswp_license_key_option = get_option( 'patternswp_license_key' );
if ( isset($_POST['patternswp_save_license'] ) ) {
if (!isset( $_POST['patternswp_license_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['patternswp_license_nonce'] ) ), 'patternswp_save_license_action') ) {
wp_die(esc_attr('Security check failed. Please try again.', 'patternswp'));
}
if ( !current_user_can('manage_options' ) ) {
wp_die(esc_attr('You do not have sufficient permissions to perform this action.', 'patternswp'));
}
if ( isset( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) && !empty( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) ) {
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$license_k = isset( $_POST['patternswp_license_key'] ) ? $_POST['patternswp_license_key'] : '';
$empty_option = array();
if( !empty( $license_k ) ){
$empty_option['patternswp_pro_license_key'] = $this->patternswp_license_key_option['patternswp_pro_license_key'];
$this->handle_license_activation( $empty_option, $license_k );
}
$patterns_tkey = 'patterns_data';
delete_transient( $patterns_tkey );
}else{
$redirect_url = add_query_arg( array(
'page' => 'patternswp-license_section',
'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
'status' => 'error'
), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_url );
exit;
}
$redirect_url = add_query_arg( 'updated', 'true', wp_get_referer() );
wp_safe_redirect( $redirect_url );
exit;
}
}
/**
* Handle License Activation
*
* @param array $old_value Old license key.
* @param array $new_value New license key.
*/
public function handle_license_activation( $old_value, $new_value ) {
if ( isset( $new_value['patternswp_pro_license_key'] ) && ! empty( $new_value['patternswp_pro_license_key'] ) ) {
if ( $old_value['patternswp_pro_license_key'] === $new_value['patternswp_pro_license_key'] ) {
$redirect_url = add_query_arg( array(
'page' => 'patternswp-license_section',
'pt_msg' => urlencode( 'License Key is already activated.' ),
'status' => 'info'
), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_url );
exit;
} else {
$this->activate_license( $new_value['patternswp_pro_license_key'] );
}
} else {
$redirect_url = add_query_arg( array(
'page' => 'patternswp-license_section',
'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
'status' => 'error'
), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_url );
exit;
}
}
/**
* Activate License
*
* @param string $license_key License key.
*/
public function activate_license( $license_key ) {
global $wpdb;
$activation_url = PATTERSWP_PLUGIN_API_URL . '/activate';
$data = array( 'license_key' => $license_key, 'instance_name' => home_url() );
$response = wp_remote_post( $activation_url, array( 'body' => $data, 'headers' => array( 'Accept' => 'application/json' ), 'sslverify' => false, 'timeout' => 10 ) );
$response = json_decode( wp_remote_retrieve_body( $response ) );
if ( isset( $response->error ) && !empty( $response->error ) ) {
if( $response->error == 'license_key not found.' ){
$response->error = 'License activation failed: Please enter a valid license key.';
}
$redirect_url = add_query_arg( array(
'page' => 'patternswp-license_section',
'pt_msg' => urlencode( $response->error ),
'status' => 'error'
), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_url );
exit;
}
if( isset( $response->activated ) && $response->activated == true ) {
// Store license data in options
$license_data = (array) $response;
update_option( 'patternswp_plugin_license_data', $license_data );
$this->clear_transients();
$license_key_data = array( 'patternswp_pro_license_key' => $license_key );
$license_key = update_option( 'patternswp_license_key', $license_key_data );
do_action( 'patternswp_load_patterns_by_ra' );
$redirect_url = add_query_arg( array(
'page' => 'patternswp-license_section',
'pt_msg' => urlencode( 'License activated successfully.' ),
'status' => 'success'
), admin_url( 'admin.php' ) );
wp_safe_redirect( $redirect_url );
exit;
}
}
/**
* Clear transients
*/
private function clear_transients() {
global $wpdb;
// Delete category type transient
delete_transient( 'patternswp_category_type' );
// Delete all API-related transients
$pattern = '_transient_patternswp_';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$results = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $wpdb->esc_like( $pattern ) . '%' ) );
// Delete all patterns cache
$patterns = 'patterns_cache_';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$wpdb->query(
$wpdb->prepare(
"DELETE FROM $wpdb->options WHERE option_name LIKE %s",
$wpdb->esc_like( $patterns ) . '%'
)
);
foreach ( $results as $transient ) {
delete_option( $transient );
delete_option(str_replace('_transient_', '_transient_timeout_', $transient));
}
}
/**
* Display admin notice
*/
public function patternswp_display_admin_notice() {
if ( isset( $_GET['pt_msg'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$message = isset($_GET['pt_msg']) ? esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
$status = isset( $_GET['status'] ) && sanitize_text_field( wp_unslash( $_GET['status'] ) ) === 'success' ? 'updated' : 'error'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
printf( '', esc_attr( $status ), esc_attr( $message ) );
}
}
}
new PatternsWP_LicenseSection();