| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class PatternsWP_LicenseSection { |
| 8 |
private $patternswp_license_key_option; |
| 9 |
|
| 10 |
/** |
| 11 |
* Constructor |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'admin_menu', array( $this, 'patternswp_add_license_page' ) ); |
| 15 |
add_action( 'admin_init', array( $this, 'patternswp_save_license_key' ) ); |
| 16 |
add_action( 'admin_notices', array( $this, 'patternswp_display_admin_notice' ) ); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Add License Page |
| 21 |
*/ |
| 22 |
public function patternswp_add_license_page() { |
| 23 |
add_submenu_page( 'patternswp-plugin-menu', 'License', 'License', 'manage_options', 'patternswp-license_section', array( $this, 'patternswp_plugin_license_section' ) ); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* License Section |
| 28 |
*/ |
| 29 |
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; |
| 36 |
} |
| 37 |
$get_license_status = get_option( 'patternswp_plugin_license_data' ); |
| 38 |
$license_status = isset( $get_license_status['activated'] ) ? $get_license_status['activated'] : false; |
| 39 |
?> |
| 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> |
| 77 |
</div> |
| 78 |
</div> |
| 79 |
</div> |
| 80 |
<?php |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Save License Key |
| 85 |
*/ |
| 86 |
public function patternswp_save_license_key() { |
| 87 |
$this->patternswp_license_key_option = get_option( 'patternswp_license_key' ); |
| 88 |
|
| 89 |
if ( isset($_POST['patternswp_save_license'] ) ) { |
| 90 |
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')); |
| 92 |
} |
| 93 |
if ( !current_user_can('manage_options' ) ) { |
| 94 |
wp_die(esc_attr('You do not have sufficient permissions to perform this action.', 'patternswp')); |
| 95 |
} |
| 96 |
|
| 97 |
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'] : ''; |
| 100 |
$empty_option = array(); |
| 101 |
if( !empty( $license_k ) ){ |
| 102 |
$empty_option['patternswp_pro_license_key'] = $this->patternswp_license_key_option['patternswp_pro_license_key']; |
| 103 |
$this->handle_license_activation( $empty_option, $license_k ); |
| 104 |
|
| 105 |
} |
| 106 |
$patterns_tkey = 'patterns_data'; |
| 107 |
delete_transient( $patterns_tkey ); |
| 108 |
}else{ |
| 109 |
$redirect_url = add_query_arg( array( |
| 110 |
'page' => 'patternswp-license_section', |
| 111 |
'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ), |
| 112 |
'status' => 'error' |
| 113 |
), admin_url( 'admin.php' ) ); |
| 114 |
wp_safe_redirect( $redirect_url ); |
| 115 |
exit; |
| 116 |
} |
| 117 |
|
| 118 |
$redirect_url = add_query_arg( 'updated', 'true', wp_get_referer() ); |
| 119 |
wp_safe_redirect( $redirect_url ); |
| 120 |
exit; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Handle License Activation |
| 126 |
* |
| 127 |
* @param array $old_value Old license key. |
| 128 |
* @param array $new_value New license key. |
| 129 |
*/ |
| 130 |
public function handle_license_activation( $old_value, $new_value ) { |
| 131 |
if ( isset( $new_value['patternswp_pro_license_key'] ) && ! empty( $new_value['patternswp_pro_license_key'] ) ) { |
| 132 |
if ( $old_value['patternswp_pro_license_key'] === $new_value['patternswp_pro_license_key'] ) { |
| 133 |
$redirect_url = add_query_arg( array( |
| 134 |
'page' => 'patternswp-license_section', |
| 135 |
'pt_msg' => urlencode( 'License Key is already activated.' ), |
| 136 |
'status' => 'info' |
| 137 |
), admin_url( 'admin.php' ) ); |
| 138 |
wp_safe_redirect( $redirect_url ); |
| 139 |
exit; |
| 140 |
} else { |
| 141 |
$this->activate_license( $new_value['patternswp_pro_license_key'] ); |
| 142 |
} |
| 143 |
} else { |
| 144 |
$redirect_url = add_query_arg( array( |
| 145 |
'page' => 'patternswp-license_section', |
| 146 |
'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ), |
| 147 |
'status' => 'error' |
| 148 |
), admin_url( 'admin.php' ) ); |
| 149 |
wp_safe_redirect( $redirect_url ); |
| 150 |
exit; |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Activate License |
| 156 |
* |
| 157 |
* @param string $license_key License key. |
| 158 |
*/ |
| 159 |
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 |
} |
| 171 |
|
| 172 |
$redirect_url = add_query_arg( array( |
| 173 |
'page' => 'patternswp-license_section', |
| 174 |
'pt_msg' => urlencode( $response->error ), |
| 175 |
'status' => 'error' |
| 176 |
), admin_url( 'admin.php' ) ); |
| 177 |
wp_safe_redirect( $redirect_url ); |
| 178 |
exit; |
| 179 |
} |
| 180 |
|
| 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 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Clear transients |
| 205 |
*/ |
| 206 |
private function clear_transients() { |
| 207 |
global $wpdb; |
| 208 |
|
| 209 |
// Delete category type transient |
| 210 |
delete_transient( 'patternswp_category_type' ); |
| 211 |
|
| 212 |
// Delete all API-related transients |
| 213 |
$pattern = '_transient_patternswp_'; |
| 214 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 215 |
$results = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $wpdb->esc_like( $pattern ) . '%' ) ); |
| 216 |
|
| 217 |
// Delete all patterns cache |
| 218 |
$patterns = 'patterns_cache_'; |
| 219 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 220 |
$wpdb->query( |
| 221 |
$wpdb->prepare( |
| 222 |
"DELETE FROM $wpdb->options WHERE option_name LIKE %s", |
| 223 |
$wpdb->esc_like( $patterns ) . '%' |
| 224 |
) |
| 225 |
); |
| 226 |
|
| 227 |
foreach ( $results as $transient ) { |
| 228 |
delete_option( $transient ); |
| 229 |
delete_option(str_replace('_transient_', '_transient_timeout_', $transient)); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Display admin notice |
| 235 |
*/ |
| 236 |
public function patternswp_display_admin_notice() { |
| 237 |
if ( isset( $_GET['pt_msg'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 238 |
$message = isset($_GET['pt_msg']) ? esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 239 |
$status = isset( $_GET['status'] ) && sanitize_text_field( wp_unslash( $_GET['status'] ) ) === 'success' ? 'updated' : 'error'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 240 |
printf( '<div class="%s notice is-dismissible"><p>%s</p></div>', esc_attr( $status ), esc_attr( $message ) ); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
new PatternsWP_LicenseSection(); |