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

235 lines 9.5 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 <?php
41 $patternswp_admin = new PatternsWP_Admin();
42 $patternswp_admin->patterswp_tab( 'License' );
43 ?>
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 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
95 $license_k = isset( $_POST['patternswp_license_key'] ) ? $_POST['patternswp_license_key'] : '';
96 $empty_option = array();
97 if( !empty( $license_k ) ){
98 $empty_option['patternswp_pro_license_key'] = $this->patternswp_license_key_option['patternswp_pro_license_key'];
99 $this->handle_license_activation( $empty_option, $license_k );
100
101 }
102 $patterns_tkey = 'patterns_data';
103 delete_transient( $patterns_tkey );
104 }else{
105 wp_redirect( add_query_arg( array(
106 'page' => 'patternswp-license_section',
107 'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
108 'status' => 'error'
109 ), admin_url( 'admin.php' ) ) );
110 exit;
111 }
112
113 wp_redirect( add_query_arg( 'updated', 'true', wp_get_referer() ) );
114 exit;
115 }
116 }
117
118 /**
119 * Handle License Activation
120 *
121 * @param array $old_value Old license key.
122 * @param array $new_value New license key.
123 */
124 public function handle_license_activation( $old_value, $new_value ) {
125 if ( isset( $new_value['patternswp_pro_license_key'] ) && ! empty( $new_value['patternswp_pro_license_key'] ) ) {
126 if ( $old_value['patternswp_pro_license_key'] === $new_value['patternswp_pro_license_key'] ) {
127 wp_redirect( add_query_arg( array(
128 'page' => 'patternswp-license_section',
129 'pt_msg' => urlencode( 'License Key is already activated.' ),
130 'status' => 'info'
131 ), admin_url( 'admin.php' ) ) );
132 exit;
133 } else {
134 $this->activate_license( $new_value['patternswp_pro_license_key'] );
135 }
136 } else {
137 wp_redirect( add_query_arg( array(
138 'page' => 'patternswp-license_section',
139 'pt_msg' => urlencode( 'License activation failed: License Key cannot be empty. Please enter a valid license key.' ),
140 'status' => 'error'
141 ), admin_url( 'admin.php' ) ) );
142 exit;
143 }
144 }
145
146 /**
147 * Activate License
148 *
149 * @param string $license_key License key.
150 */
151 public function activate_license( $license_key ) {
152 global $wpdb;
153
154 $activation_url = PATTERSWP_PLUGIN_API_URL . '/activate';
155 $data = array( 'license_key' => $license_key, 'instance_name' => home_url() );
156 $response = wp_remote_post( $activation_url, array( 'body' => $data, 'headers' => array( 'Accept' => 'application/json' ), 'sslverify' => false, 'timeout' => 10 ) );
157 $response = json_decode( wp_remote_retrieve_body( $response ) );
158
159 if ( isset( $response->error ) && !empty( $response->error ) ) {
160 if( $response->error == 'license_key not found.' ){
161 $response->error = 'License activation failed: Please enter a valid license key.';
162 }
163
164 wp_redirect( add_query_arg( array(
165 'page' => 'patternswp-license_section',
166 'pt_msg' => urlencode( $response->error ),
167 'status' => 'error'
168 ), admin_url( 'admin.php' ) ) );
169 exit;
170 }
171
172 if( isset( $response->activated ) && $response->activated == true ) {
173 // Store license data in options
174 $license_data = (array) $response;
175 update_option( 'patternswp_plugin_license_data', $license_data );
176 $this->clear_transients();
177
178 $license_key_data = array( 'patternswp_pro_license_key' => $license_key );
179 $license_key = update_option( 'patternswp_license_key', $license_key_data );
180
181 do_action( 'patternswp_load_patterns_by_ra' );
182
183 wp_redirect( add_query_arg( array(
184 'page' => 'patternswp-license_section',
185 'pt_msg' => urlencode( 'License activated successfully.' ),
186 'status' => 'success'
187 ), admin_url( 'admin.php' ) ) );
188 exit;
189
190 }
191 }
192
193 /**
194 * Clear transients
195 */
196 private function clear_transients() {
197 global $wpdb;
198
199 // Delete category type transient
200 delete_transient( 'patternswp_category_type' );
201
202 // Delete all API-related transients
203 $pattern = '_transient_patternswp_';
204 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
205 $results = $wpdb->get_col( $wpdb->prepare( "SELECT option_name FROM $wpdb->options WHERE option_name LIKE %s", $wpdb->esc_like( $pattern ) . '%' ) );
206
207 // Delete all patterns cache
208 $patterns = 'patterns_cache_';
209 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
210 $wpdb->query(
211 $wpdb->prepare(
212 "DELETE FROM $wpdb->options WHERE option_name LIKE %s",
213 $wpdb->esc_like( $patterns ) . '%'
214 )
215 );
216
217 foreach ( $results as $transient ) {
218 delete_option( $transient );
219 delete_option(str_replace('_transient_', '_transient_timeout_', $transient));
220 }
221 }
222
223 /**
224 * Display admin notice
225 */
226 public function patternswp_display_admin_notice() {
227 if ( isset( $_GET['pt_msg'] ) && ! empty( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
228 $message = isset($_GET['pt_msg']) ? esc_html( urldecode( sanitize_text_field( wp_unslash( $_GET['pt_msg'] ) ) ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
229 $status = isset( $_GET['status'] ) && sanitize_text_field( wp_unslash( $_GET['status'] ) ) === 'success' ? 'updated' : 'error'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
230 printf( '<div class="%s notice is-dismissible"><p>%s</p></div>', esc_attr( $status ), esc_attr( $message ) );
231 }
232 }
233 }
234
235 new PatternsWP_LicenseSection();