PluginProbe ʕ •ᴥ•ʔ
Responsive Lightbox & Gallery / 2.7.2
Responsive Lightbox & Gallery v2.7.2
2.7.8 trunk 1.0.0 1.0.1 1.0.1.1 1.0.2 1.0.3 1.0.4 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.0.1 1.4.1 1.4.11 1.4.12 1.4.13 1.4.14 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.6.0 1.6.1 1.6.10 1.6.11 1.6.12 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.3.1 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.3.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.4.8 2.4.9 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7
responsive-lightbox / includes / settings / class-settings-licenses.php
responsive-lightbox / includes / settings Last commit date
class-settings-addons.php 4 months ago class-settings-base.php 5 months ago class-settings-builder.php 5 months ago class-settings-capabilities.php 5 months ago class-settings-folders.php 5 months ago class-settings-galleries.php 5 months ago class-settings-general.php 5 months ago class-settings-licenses.php 5 months ago class-settings-lightboxes.php 4 months ago class-settings-remote-library.php 5 months ago
class-settings-licenses.php
300 lines
1 <?php
2 // exit if accessed directly
3 if ( ! defined( 'ABSPATH' ) )
4 exit;
5
6 /**
7 * Responsive_Lightbox_Settings_Licenses class.
8 *
9 * Settings page class for Licenses tab migration to new Settings API.
10 *
11 * @class Responsive_Lightbox_Settings_Licenses
12 */
13 class Responsive_Lightbox_Settings_Licenses extends Responsive_Lightbox_Settings_Base {
14
15 /**
16 * Tab key identifier.
17 *
18 * @var string
19 */
20 const TAB_KEY = 'licenses';
21
22 /**
23 * Register this tab as migrated to new API.
24 *
25 * Override - only register if extensions exist.
26 *
27 * @param array $tabs Migrated tabs.
28 * @return array
29 */
30 public function register_migrated_tab( $tabs ) {
31 $extensions = apply_filters( 'rl_settings_licenses', [] );
32 if ( $extensions ) {
33 $tabs[] = self::TAB_KEY;
34 }
35 return $tabs;
36 }
37
38 /**
39 * Validate settings for licenses tab.
40 *
41 * Override to use licenses-specific validation.
42 *
43 * @param array $input Input data from form submission.
44 * @return array Validated data.
45 */
46 public function validate( $input ) {
47 // use licenses-specific validation
48 return $this->validate_licenses( $input );
49 }
50
51 /**
52 * Provide settings data for licenses.
53 *
54 * @param array $data Settings data.
55 * @return array
56 */
57 public function settings_data( $data ) {
58 // get extensions
59 $extensions = apply_filters( 'rl_settings_licenses', [] );
60
61 if ( $extensions ) {
62 $data[self::TAB_KEY] = [
63 'option_name' => 'responsive_lightbox_licenses',
64 'option_group' => 'responsive_lightbox_licenses',
65 'validate' => [ $this, 'validate' ],
66 'sections' => [
67 'responsive_lightbox_licenses' => [
68 'title' => __( 'Licenses', 'responsive-lightbox' ),
69 'callback' => [ $this, 'licenses_section_cb' ]
70 ]
71 ],
72 'fields' => []
73 ];
74
75 // add fields for each extension
76 foreach ( $extensions as $id => $extension ) {
77 $data[self::TAB_KEY]['fields'][$id] = [
78 'title' => $extension['name'],
79 'section' => 'responsive_lightbox_licenses',
80 'type' => 'custom',
81 'callback' => [ $this, 'license_field_cb' ],
82 'callback_args' => $extension
83 ];
84 }
85 }
86
87 return $data;
88 }
89
90 /**
91 * Licenses section callback.
92 *
93 * @return void
94 */
95 public function licenses_section_cb() {
96 ?><p class="description"><?php esc_html_e( 'A list of licenses for your Responsive Lightbox & Gallery extensions.', 'responsive-lightbox' ); ?></p><?php
97 }
98
99 /**
100 * License field callback.
101 *
102 * @param array $args
103 * @return void
104 */
105 public function license_field_cb( $args ) {
106 // get extension data from callback_args (new API) or directly from args (legacy)
107 $extension = ! empty( $args['callback_args'] ) ? $args['callback_args'] : $args;
108 $extension_id = ! empty( $extension['id'] ) ? $extension['id'] : '';
109 $extension_name = ! empty( $extension['name'] ) ? $extension['name'] : '';
110
111 $licenses = get_option( 'responsive_lightbox_licenses' );
112
113 if ( ! empty( $licenses ) && ! empty( $extension_id ) ) {
114 $license = isset( $licenses[$extension_id]['license'] ) ? $licenses[$extension_id]['license'] : '';
115 $status = ! empty( $licenses[$extension_id]['status'] );
116 } else {
117 $license = '';
118 $status = false;
119 } ?>
120 <fieldset class="rl_license rl_license-<?php echo esc_attr( $extension_id ); ?>">
121 <input type="text" class="regular-text" name="responsive_lightbox_licenses[<?php echo esc_attr( $extension_id ); ?>][license]" value="<?php echo esc_attr( $license ); ?>"><span class="dashicons <?php echo ( $status ? 'dashicons-yes' : 'dashicons-no' ); ?>"></span>
122 <p class="description"><?php echo esc_html( sprintf( __( 'Enter your license key to activate %s extension and enable automatic upgrade notices.', 'responsive-lightbox' ), $extension_name ) ); ?></p>
123 </fieldset>
124 <?php
125 }
126
127 /**
128 * Validate licenses function.
129 *
130 * Handles license activation/deactivation with external license server API.
131 * Called by validate() method.
132 *
133 * @param array $input
134 * @return array
135 */
136 private function validate_licenses( $input ) {
137 // check cap
138 if ( ! current_user_can( apply_filters( 'rl_lightbox_settings_capability', Responsive_Lightbox()->options['capabilities']['active'] ? 'edit_lightbox_settings' : 'manage_options' ) ) )
139 return $input;
140
141 // check option page
142 $option_page = isset( $_POST['option_page'] ) ? sanitize_key( $_POST['option_page'] ) : '';
143
144 // check page
145 if ( ! $option_page )
146 return $input;
147
148 $rl_licenses = [];
149
150 if ( isset( $_POST['responsive_lightbox_licenses'] ) && is_array( $_POST['responsive_lightbox_licenses'] ) && ! empty( $_POST['responsive_lightbox_licenses'] ) ) {
151 foreach ( $_POST['responsive_lightbox_licenses'] as $extension => $data ) {
152 $ext = sanitize_key( $extension );
153
154 if ( is_array( $data ) && ! empty( $data['license'] ) )
155 $rl_licenses[$ext]['license'] = preg_replace( '/[^a-z0-9]/i', '', $data['license'] );
156 else
157 $rl_licenses[$ext]['license'] = '';
158 }
159 }
160
161 // check data
162 if ( ! $rl_licenses )
163 return $input;
164
165 // get extension licenses
166 $extensions = apply_filters( 'rl_settings_licenses', [] );
167
168 if ( empty( $extensions ) )
169 return $input;
170
171 // save settings
172 if ( isset( $_POST['save_responsive_lightbox_licenses'] ) || isset( $_POST['save_rl_licenses'] ) ) {
173 $licenses = get_option( 'responsive_lightbox_licenses' );
174 $statuses = [ 'updated' => 0, 'error' => 0 ];
175
176 foreach ( $extensions as $extension ) {
177 if ( ! isset( $rl_licenses[$extension['id']] ) )
178 continue;
179
180 $license = $rl_licenses[$extension['id']]['license'];
181 $status = ! empty( $licenses ) && ! empty( $licenses[$extension['id']]['status'] );
182
183 // update license
184 $input[$extension['id']]['license'] = $license;
185
186 // request data
187 $request_args = [
188 'action' => 'activate_license',
189 'license' => $license,
190 'item_name' => $extension['item_name']
191 ];
192
193 // request
194 $response = $this->license_request( $request_args );
195
196 // validate request
197 if ( is_wp_error( $response ) ) {
198 $input[$extension['id']]['status'] = false;
199 $statuses['error']++;
200 } else {
201 // decode the license data
202 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
203
204 // assign the data
205 if ( $license_data->license === 'valid' ) {
206 $input[$extension['id']]['status'] = true;
207
208 if ( $status === false )
209 $statuses['updated']++;
210 } else {
211 $input[$extension['id']]['status'] = false;
212 $statuses['error']++;
213 }
214 }
215 }
216
217 // success notice
218 if ( $statuses['updated'] > 0 )
219 add_settings_error( 'rl_licenses_settings', 'license_activated', esc_html( sprintf( _n( '%s license successfully activated.', '%s licenses successfully activated.', (int) $statuses['updated'], 'responsive-lightbox' ), (int) $statuses['updated'] ) ), 'updated' );
220
221 // failed notice
222 if ( $statuses['error'] > 0 )
223 add_settings_error( 'rl_licenses_settings', 'license_activation_failed', esc_html( sprintf( _n( '%s license activation failed.', '%s licenses activation failed.', (int) $statuses['error'], 'responsive-lightbox' ), (int) $statuses['error'] ) ), 'error' );
224 } elseif ( isset( $_POST['reset_responsive_lightbox_licenses'] ) || isset( $_POST['reset_rl_licenses'] ) ) {
225 $licenses = get_option( 'responsive_lightbox_licenses' );
226 $statuses = [
227 'updated' => 0,
228 'error' => 0
229 ];
230
231 foreach ( $extensions as $extension ) {
232 $license = ! empty( $licenses ) && isset( $licenses[$extension['id']]['license'] ) ? $licenses[$extension['id']]['license'] : '';
233 $status = ! empty( $licenses ) && ! empty( $licenses[$extension['id']]['status'] );
234
235 if ( $status === true || ( $status === false && ! empty( $license ) ) ) {
236 // request data
237 $request_args = [
238 'action' => 'deactivate_license',
239 'license' => trim( $license ),
240 'item_name' => $extension['item_name']
241 ];
242
243 // request
244 $response = $this->license_request( $request_args );
245
246 // validate request
247 if ( is_wp_error( $response ) )
248 $statuses['error']++;
249 else {
250 // decode the license data
251 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
252
253 // assign the data
254 if ( $license_data->license == 'deactivated' ) {
255 $input[$extension['id']]['license'] = '';
256 $input[$extension['id']]['status'] = false;
257
258 $statuses['updated']++;
259 } else
260 $statuses['error']++;
261 }
262 }
263 }
264
265 // success notice
266 if ( $statuses['updated'] > 0 )
267 add_settings_error( 'rl_licenses_settings', 'license_deactivated', esc_html( sprintf( _n( '%s license successfully deactivated.', '%s licenses successfully deactivated.', (int) $statuses['updated'], 'responsive-lightbox' ), (int) $statuses['updated'] ) ), 'updated' );
268
269 // failed notice
270 if ( $statuses['error'] > 0 )
271 add_settings_error( 'rl_licenses_settings', 'license_deactivation_failed', esc_html( sprintf( _n( '%s license deactivation failed.', '%s licenses deactivation failed.', (int) $statuses['error'], 'responsive-lightbox' ), (int) $statuses['error'] ) ), 'error' );
272 }
273
274 return $input;
275 }
276
277 /**
278 * License request function.
279 *
280 * @param array $args
281 * @return mixed
282 */
283 private function license_request( $args ) {
284 // data to send in our API request
285 $api_params = [
286 'edd_action' => $args['action'],
287 'license' => sanitize_key( $args['license'] ),
288 'item_name' => urlencode( $args['item_name'] ),
289 // 'item_id' => $args['item_id'],
290 'url' => home_url(),
291 'timeout' => 60,
292 'sslverify' => false
293 ];
294
295 // call the custom API
296 $response = wp_remote_get( add_query_arg( $api_params, 'https://www.dfactory.co' ) );
297
298 return $response;
299 }
300 }