PluginProbe
PatternsWP – Gutenberg Block Patterns & Page Templates Library / 1.0.5
PatternsWP – Gutenberg Block Patterns & Page Templates Library v1.0.5
trunk 1.0.0 1.0.1 1.0.10 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9
patternswp / includes / lib / class-patterns-license.php

class-patterns-license.php in PatternsWP – Gutenberg Block Patterns & Page Templates Library 1.0.5, at includes/lib/class-patterns-license.php

230 lines 9.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class PatternsWP_LicenseSection {
4 private $patternswp_license_key_option;
5
6 /**
7 * Constructor
8 */
9 public function __construct() {
10 add_action( 'admin_menu', array( $this, 'patternswp_add_license_page' ) );
11 add_action( 'admin_init', array( $this, 'patternswp_save_license_key' ) );
12 add_action( 'admin_notices', array( $this, 'patternswp_display_admin_notice' ) );
13 }
14
15 /**
16 * Add License Page
17 */
18 public function patternswp_add_license_page() {
19 add_submenu_page( 'patternswp-plugin-menu', 'License', 'License', 'manage_options', 'patternswp-license_section', array( $this, 'patternswp_plugin_license_section' ) );
20 }
21
22 /**
23 * License Section
24 */
25 public function patternswp_plugin_license_section() {
26 $license_key = $this->patternswp_license_key_option = get_option( 'patternswp_license_key' );
27 $license_key = isset( $license_key['patternswp_pro_license_key'] ) ? $license_key['patternswp_pro_license_key'] : '';
28 if (!empty( $license_key ) && strlen( $license_key ) > 8) {
29 $masked_key = substr( $license_key, 0, 8 ) . str_repeat( 'X', strlen( $license_key ) - 8 );
30 } else {
31 $masked_key = $license_key;
32 }
33 $get_license_status = get_option( 'patternswp_plugin_license_data' );
34 $license_status = isset( $get_license_status['activated'] ) ? $get_license_status['activated'] : false;
35 ?>
36 <div class="wrap">
37 <h1><?php esc_attr_e( 'License', 'patternswp' ); ?></h1>
38 <?php settings_errors(); ?>
39 <h2 class="nav-tab-wrapper">
40 <a href="?page=patternswp-plugin-menu" class="nav-tab"><?php esc_attr_e( 'Dashboard', 'patternswp' ); ?></a>
41 <a href="?page=patternswp-plugin-page-1" class="nav-tab"><?php esc_attr_e( 'Support', 'patternswp' ); ?></a>
42 <a href="?page=patternswp-clear-cache" class="nav-tab"><?php esc_attr_e( 'Clear Cache', 'patternswp' ); ?></a>
43 <a href="?page=patternswp-license_section" class="nav-tab nav-tab-active"><?php esc_attr_e( 'License', 'patternswp' ); ?></a>
44 </h2>
45 <div class="patterns-wp-tabs-content">
46 <div id="tab-1" class="patterns-wp-tab-content patterns-wp-tab-active">
47 <div class="wrap">
48
49 <div class="container" style="padding: 40px;background-color: white;border-radius: 5px;overflow: hidden;width: 70%;">
50 <div class="about__section has-1-columns">
51 <div class="column" style="">
52 <h1 class="feature-title"><?php esc_html_e('License', 'patternswp'); ?></h1>
53 <p><?php esc_html_e('Already purchased? Simply enter your license key below to connect with PatternsWP Pro!', 'patternswp'); ?></p>
54 </div>
55 <div style="height: 1px; background-color: #ccc; width: 100%; margin: 20px 0;"></div>
56 </div>
57 <div class="">
58 <form id="patternswp_license_form" method="post" >
59 <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 ) ?>">
60 <?php wp_nonce_field('patternswp_save_license_action', 'patternswp_license_nonce'); ?>
61 <input name="patternswp_save_license" type="submit" id="custom_submit_btn" class="button button-primary" value="Save & Activate" style="margin-left: 5px;">
62 <?php
63 if ( $license_status == true ) {
64 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>';
65 }
66 ?>
67 <div>
68 </div>
69 </form>
70 </div>
71 </div>
72 </div>
73 </div>
74 </div>
75 </div>
76 <?php
77 }
78
79 /**
80 * Save License Key
81 */
82 public function patternswp_save_license_key() {
83 $this->patternswp_license_key_option = get_option( 'patternswp_license_key' );
84
85 if ( isset($_POST['patternswp_save_license'] ) ) {
86 if (!isset( $_POST['patternswp_license_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['patternswp_license_nonce'] ) ), 'patternswp_save_license_action') ) {
87 wp_die(esc_attr('Security check failed. Please try again.', 'patternswp'));
88 }
89 if ( !current_user_can('manage_options' ) ) {
90 wp_die(esc_attr('You do not have sufficient permissions to perform this action.', 'patternswp'));
91 }
92
93 if ( isset( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) && !empty( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) ) {
94 $license_k = isset( $_POST['patternswp_license_key'] ) ? $_POST['patternswp_license_key'] : '';
95 $empty_option = array();
96 if( !empty( $license_k ) ){
97 $empty_option['patternswp_pro_license_key'] = $this->patternswp_license_key_option['patternswp_pro_license_key'];
98 $this->handle_license_activation( $empty_option, $license_k );
99
100 }
101 $patterns_tkey = 'patterns_data';
102 delete_transient( $patterns_tkey );
103 }else{
104 wp_redirect( add_query_arg( array(
105 'page' => 'patternswp-license_section',
106 'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
107 'status' => 'error'
108 ), admin_url( 'admin.php' ) ) );
109 exit;
110 }
111
112 wp_redirect( add_query_arg( 'updated', 'true', wp_get_referer() ) );
113 exit;
114 }
115 }
116
117 /**
118 * Handle License Activation
119 *
120 * @param array $old_value Old license key.
121 * @param array $new_value New license key.
122 */
123 public function handle_license_activation( $old_value, $new_value ) {
124 if ( isset( $new_value['patternswp_pro_license_key'] ) && ! empty( $new_value['patternswp_pro_license_key'] ) ) {
125 if ( $old_value['patternswp_pro_license_key'] === $new_value['patternswp_pro_license_key'] ) {
126 wp_redirect( add_query_arg( array(
127 'page' => 'patternswp-license_section',
128 'pt_msg' => urlencode( 'License Key is already activated.' ),
129 'status' => 'info'
130 ), admin_url( 'admin.php' ) ) );
131 exit;
132 } else {
133 $this->activate_license( $new_value['patternswp_pro_license_key'] );
134 }
135 } else {
136 wp_redirect( add_query_arg( array(
137 'page' => 'patternswp-license_section',
138 'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
139 'status' => 'error'
140 ), admin_url( 'admin.php' ) ) );
141 exit;
142 }
143 }
144
145 /**
146 * Activate License
147 *
148 * @param string $license_key License key.
149 */
150 public function activate_license( $license_key ) {
151 global $wpdb;
152
153 $activation_url = PATTERSWP_PLUGIN_API_URL . '/activate';
154 $data = array( 'license_key' => $license_key, 'instance_name' => home_url() );
155 $response = wp_remote_post( $activation_url, array( 'body' => $data, 'headers' => array( 'Accept' => 'application/json' ), 'sslverify' => false, 'timeout' => 10 ) );
156 $response = json_decode( wp_remote_retrieve_body( $response ) );
157
158 if ( isset( $response->error ) && !empty( $response->error ) ) {
159 if( $response->error == 'license_key not found.' ){
160 $response->error = 'License activation failed: Please enter a valid license key.';
161 }
162
163 wp_redirect( add_query_arg( array(
164 'page' => 'patternswp-license_section',
165 'pt_msg' => urlencode( $response->error ),
166 'status' => 'error'
167 ), admin_url( 'admin.php' ) ) );
168 exit;
169 }
170
171 if( isset( $response->activated ) && $response->activated == true ) {
172 // Store license data in options
173 $license_data = (array) $response;
174 update_option( 'patternswp_plugin_license_data', $license_data );
175 $this->clear_transients();
176
177 $license_key_data = array( 'patternswp_pro_license_key' => $license_key );
178 $license_key = update_option( 'patternswp_license_key', $license_key_data );
179
180 wp_redirect( add_query_arg( array(
181 'page' => 'patternswp-license_section',
182 'pt_msg' => urlencode( 'License activated successfully.' ),
183 'status' => 'success'
184 ), admin_url( 'admin.php' ) ) );
185 exit;
186
187 }
188 }
189
190 /**
191 * Clear transients
192 */
193 private function clear_transients() {
194 global $wpdb;
195
196 // Delete category type transient
197 delete_transient( 'patternswp_category_type' );
198
199 // Delete all API-related transients
200 $pattern = '_transient_patternswp_';
201 $results = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $wpdb->esc_like( $pattern ) . '%' ) );
202
203 // Delete all patterns cache
204 $patterns = 'patterns_cache_';
205 $wpdb->query(
206 $wpdb->prepare(
207 "DELETE FROM $wpdb->options WHERE option_name LIKE %s",
208 $wpdb->esc_like( $patterns ) . '%'
209 )
210 );
211
212 foreach ( $results as $transient ) {
213 delete_option( $transient );
214 delete_option(str_replace('_transient_', '_transient_timeout_', $transient));
215 }
216 }
217
218 /**
219 * Display admin notice
220 */
221 public function patternswp_display_admin_notice() {
222 if ( isset( $_GET['pt_msg'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) {
223 $message = isset($_GET['pt_msg']) ? esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) : '';
224 $status = isset( $_GET['status'] ) && sanitize_text_field( wp_unslash( $_GET['status'] ) ) === 'success' ? 'updated' : 'error';
225 printf( '<div class="%s notice is-dismissible"><p>%s</p></div>', esc_attr( $status ), esc_attr( $message ) );
226 }
227 }
228 }
229
230 new PatternsWP_LicenseSection();