PluginProbe
PatternsWP – Gutenberg Block Patterns & Page Templates Library / 1.1.0
PatternsWP – Gutenberg Block Patterns & Page Templates Library v1.1.0
1.1.0 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.1.0, at includes/lib/class-patterns-license.php

330 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 add_action( 'wp_ajax_patternswp_activate_license', array( $this, 'ajax_activate_license' ) );
18 }
19
20 /**
21 * Add License Page
22 */
23 public function patternswp_add_license_page() {
24 add_submenu_page( 'patternswp-plugin-menu', 'License', 'License', 'manage_options', 'patternswp-license_section', array( $this, 'patternswp_plugin_license_section' ) );
25 }
26
27 /**
28 * License Section — React admin mount.
29 */
30 public function patternswp_plugin_license_section() {
31 if ( ! current_user_can( 'manage_options' ) ) {
32 return;
33 }
34 ?>
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>
40 </div>
41 </div>
42 </div>
43 <?php
44 }
45
46 /**
47 * Mask a license key for display.
48 *
49 * @param string $license_key Raw license key.
50 * @return string
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 */
185 public function patternswp_save_license_key() {
186 $this->patternswp_license_key_option = get_option( 'patternswp_license_key' );
187
188 if ( isset($_POST['patternswp_save_license'] ) ) {
189 if (!isset( $_POST['patternswp_license_nonce'] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['patternswp_license_nonce'] ) ), 'patternswp_save_license_action') ) {
190 wp_die(esc_html__('Security check failed. Please try again.', 'patternswp'));
191 }
192 if ( !current_user_can('manage_options' ) ) {
193 wp_die(esc_html__('You do not have sufficient permissions to perform this action.', 'patternswp'));
194 }
195
196 if ( isset( $_POST['patternswp_license_key']['patternswp_pro_license_key'] ) && !empty( $_POST['patternswp_license_key']['patternswp_pro_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 );
200 $empty_option = array();
201 if( !empty( $license_k ) ){
202 $empty_option['patternswp_pro_license_key'] = $this->patternswp_license_key_option['patternswp_pro_license_key'];
203 $this->handle_license_activation( $empty_option, $license_k );
204
205 }
206 $patterns_tkey = 'patterns_data';
207 delete_transient( $patterns_tkey );
208 }else{
209 $redirect_url = add_query_arg( array(
210 'page' => 'patternswp-license_section',
211 'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
212 'status' => 'error'
213 ), admin_url( 'admin.php' ) );
214 wp_safe_redirect( $redirect_url );
215 exit;
216 }
217
218 $redirect_url = add_query_arg( 'updated', 'true', wp_get_referer() );
219 wp_safe_redirect( $redirect_url );
220 exit;
221 }
222 }
223
224 /**
225 * Handle License Activation
226 *
227 * @param array $old_value Old license key.
228 * @param array $new_value New license key.
229 */
230 public function handle_license_activation( $old_value, $new_value ) {
231 if ( isset( $new_value['patternswp_pro_license_key'] ) && ! empty( $new_value['patternswp_pro_license_key'] ) ) {
232 if ( $old_value['patternswp_pro_license_key'] === $new_value['patternswp_pro_license_key'] ) {
233 $redirect_url = add_query_arg( array(
234 'page' => 'patternswp-license_section',
235 'pt_msg' => urlencode( 'License Key is already activated.' ),
236 'status' => 'info'
237 ), admin_url( 'admin.php' ) );
238 wp_safe_redirect( $redirect_url );
239 exit;
240 } else {
241 $this->activate_license( $new_value['patternswp_pro_license_key'] );
242 }
243 } else {
244 $redirect_url = add_query_arg( array(
245 'page' => 'patternswp-license_section',
246 'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
247 'status' => 'error'
248 ), admin_url( 'admin.php' ) );
249 wp_safe_redirect( $redirect_url );
250 exit;
251 }
252 }
253
254 /**
255 * Activate License
256 *
257 * @param string $license_key License key.
258 */
259 public function activate_license( $license_key ) {
260 $result = $this->activate_license_ajax( $license_key );
261
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',
267 ), admin_url( 'admin.php' ) );
268 wp_safe_redirect( $redirect_url );
269 exit;
270 }
271
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;
279 }
280
281 /**
282 * Clear transients
283 */
284 private function clear_transients() {
285 global $wpdb;
286
287 // Delete category type transient
288 delete_transient( 'patternswp_category_type' );
289
290 // Delete all API-related transients
291 $pattern = '_transient_patternswp_';
292 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
293 $results = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $wpdb->esc_like( $pattern ) . '%' ) );
294
295 // Delete all patterns cache
296 $patterns = 'patterns_cache_';
297 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
298 $wpdb->query(
299 $wpdb->prepare(
300 "DELETE FROM $wpdb->options WHERE option_name LIKE %s",
301 $wpdb->esc_like( $patterns ) . '%'
302 )
303 );
304
305 foreach ( $results as $transient ) {
306 delete_option( $transient );
307 delete_option(str_replace('_transient_', '_transient_timeout_', $transient));
308 }
309 }
310
311 /**
312 * Display admin notice (legacy URL flash messages — React also reads these via localize).
313 */
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
321 if ( isset( $_GET['pt_msg'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
322 $message = isset($_GET['pt_msg']) ? esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
323 $status = isset( $_GET['status'] ) && sanitize_text_field( wp_unslash( $_GET['status'] ) ) === 'success' ? 'updated' : 'error'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
324 printf( '<div class="%s notice is-dismissible"><p>%s</p></div>', esc_attr( $status ), esc_attr( $message ) );
325 }
326 }
327 }
328
329 new PatternsWP_LicenseSection();
330