PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / trunk
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI vtrunk
8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 7.8.7 7.8.6 7.8.5 All 44 releases
gamipress / includes / admin / pages / licenses.php

licenses.php in GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI trunk, at includes/admin/pages/licenses.php

433 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Licenses Page
4 *
5 * @package GamiPress\Admin\Licenses
6 * @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com>
7 * @since 1.3.9.3
8 */
9 // Exit if accessed directly
10 if( !defined( 'ABSPATH' ) ) exit;
11
12 /**
13 * Setup the default parameters to license meta boxes
14 *
15 * @since 1.1.1
16 * @updated 1.3.9.3 Filter changed to gamipress_licenses_meta_boxes
17 *
18 * @param array $meta_boxes
19 *
20 * @return array
21 */
22 function gamipress_licenses_meta_boxes_params( $meta_boxes ) {
23
24 // Loop settings section meta boxes
25 foreach( $meta_boxes as $meta_box_id => $meta_box ) {
26
27 // Only add settings meta box if has fields
28 if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) {
29
30 // Loop meta box fields
31 foreach( $meta_box['fields'] as $field_id => $field ) {
32
33 // Update edd_license fields with default parameters to the GamiPress server
34 if( $field['type'] !== 'edd_license' ) {
35 continue;
36 }
37
38 // if not server provider, then add GamiPress server
39 if( ! isset( $field['server'] ) ) {
40 $field['server'] = 'https://gamipress.com/edd-sl-api';
41 }
42
43 // Check if is a GamiPress hosted plugin
44 if( $field['server'] !== 'https://gamipress.com/edd-sl-api' ) {
45 continue;
46 }
47
48 // Renew link
49 $field['renew_license_link'] = 'https://gamipress.com/renew-a-license';
50 $field['license_management_link'] = 'https://gamipress.com/account';
51 $field['contact_link'] = 'https://gamipress.com/contact';
52
53 // Before field row hook to render some extra information
54 $field['before_row'] = 'gamipress_license_field_before';
55
56 // Update the field definition
57 $meta_boxes[$meta_box_id]['fields'][$field_id] = $field;
58 $meta_boxes[$meta_box_id]['priority'] = 'high'; // Fixes issue with CMB2 2.9.0
59
60 }
61
62 }
63
64 }
65
66 return $meta_boxes;
67
68 }
69 add_filter( 'gamipress_licenses_meta_boxes', 'gamipress_licenses_meta_boxes_params', 9999 );
70
71 /**
72 * Setup the thumbnail to license meta boxes
73 *
74 * @since 1.9.5
75 *
76 * @param array $meta_boxes
77 *
78 * @return array
79 */
80 function gamipress_licenses_meta_boxes_thumbnails( $meta_boxes ) {
81
82 // Check if we are on the licenses page to prevent API calls outside this page
83 if( ! isset( $_GET['page'] ) ) {
84 return $meta_boxes;
85 }
86
87 if( $_GET['page'] !== 'gamipress_licenses' ) {
88 return $meta_boxes;
89 }
90
91 // Get our add-ons
92 $plugins = gamipress_plugins_api();
93
94 // Loop settings section meta boxes
95 foreach( $meta_boxes as $meta_box_id => $meta_box ) {
96
97 // Only add settings meta box if has fields
98 if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) {
99
100 // Loop meta box fields
101 foreach( $meta_box['fields'] as $field_id => $field ) {
102
103 // Update edd_license fields with default parameters to the GamiPress server
104 if( $field['type'] !== 'edd_license' ) {
105 continue;
106 }
107
108 // if not server provider, then add GamiPress server
109 if( ! isset( $field['server'] ) ) {
110 $field['server'] = 'https://gamipress.com/edd-sl-api';
111 }
112
113 // Check if is a GamiPress hosted plugin
114 if( $field['server'] !== 'https://gamipress.com/edd-sl-api' ) {
115 continue;
116 }
117
118 // Try to find the plugin thumbnail from plugins API
119 if ( ! is_wp_error( $plugins ) && isset( $field['file'] ) && ! isset( $field['thumbnail'] ) ) {
120
121 foreach ( $plugins as $plugin ) {
122
123 $slug = basename( $field['file'], '.php' );
124
125 if( $slug === $plugin->info->slug ) {
126 $field['thumbnail'] = $plugin->info->thumbnail;
127 // Thumbnail found so exit loop
128 break;
129 }
130
131 }
132
133 }
134
135 // Update the field definition
136 $meta_boxes[$meta_box_id]['fields'][$field_id] = $field;
137
138 }
139
140 }
141
142 }
143
144 return $meta_boxes;
145
146 }
147 add_filter( 'gamipress_licenses_meta_boxes', 'gamipress_licenses_meta_boxes_thumbnails', 99999 );
148
149 /**
150 * Register licenses page.
151 *
152 * @since 1.3.9.3
153 *
154 * @return void
155 */
156 function gamipress_register_licenses_page() {
157
158 $tabs = array();
159 $boxes = array();
160
161 $meta_boxes = array();
162
163 // TODO: Keep for backward compatibility with gamipress_settings_licenses_meta_boxes filter
164 $meta_boxes = apply_filters( "gamipress_settings_licenses_meta_boxes", $meta_boxes );
165
166 /**
167 * Filter: gamipress_licenses_{$section_id}_meta_boxes
168 *
169 * @param array $meta_boxes
170 *
171 * @return array
172 */
173 $meta_boxes = apply_filters( "gamipress_licenses_meta_boxes", $meta_boxes );
174
175 if( ! empty( $meta_boxes ) ) {
176
177 // Loop licenses section meta boxes
178 foreach( $meta_boxes as $meta_box_id => $meta_box ) {
179
180 // Check meta box tabs
181 if( isset( $meta_box['tabs'] ) && ! empty( $meta_box['tabs'] ) ) {
182
183 // Loop meta box tabs
184 foreach( $meta_box['tabs'] as $tab_id => $tab ) {
185
186 $tab['id'] = $tab_id;
187
188 $meta_box['tabs'][$tab_id] = $tab;
189
190 }
191
192 }
193
194 // Only add licenses meta box if has fields
195 if( isset( $meta_box['fields'] ) && ! empty( $meta_box['fields'] ) ) {
196
197 // Loop meta box fields
198 foreach( $meta_box['fields'] as $field_id => $field ) {
199
200 $field['id'] = $field_id;
201
202 // Support for group fields
203 if( isset( $field['fields'] ) && is_array( $field['fields'] ) ) {
204
205 foreach( $field['fields'] as $group_field_id => $group_field ) {
206
207 $field['fields'][$group_field_id]['id'] = $group_field_id;
208
209 }
210
211 }
212
213 // Register custom update message on plugins menu
214 if( isset( $field['file'] ) && ! is_multisite() ) {
215
216 $plugin_file = plugin_basename( $field['file'] );
217
218 // Register custom plugin row for licensed plugins to update package if an active license exists
219 add_action( "after_plugin_row_$plugin_file", 'gamipress_license_plugin_update_row', 5, 2 );
220 add_action( "in_plugin_update_message-$plugin_file", 'gamipress_license_in_plugin_update_message', 10, 2 );
221
222 }
223
224 $meta_box['fields'][$field_id] = $field;
225
226 }
227
228 $meta_box['id'] = $meta_box_id;
229
230 $meta_box['display_cb'] = false;
231 $meta_box['admin_menu_hook'] = false;
232
233 $meta_box['show_on'] = array(
234 'key' => 'options-page',
235 'value' => array( 'gamipress_licenses' ),
236 'option_key' => 'gamipress_settings',
237 );
238
239 $box = new_cmb2_box( $meta_box );
240
241 $box->object_type( 'options-page' );
242
243 $boxes[] = $box;
244
245 }
246 }
247 }
248
249 try {
250 // Create the options page
251 new Cmb2_Metatabs_Options( array(
252 'key' => 'gamipress_settings',
253 'class' => 'gamipress-page',
254 'title' => __( 'Licenses', 'gamipress' ),
255 'topmenu' => 'gamipress',
256 'view_capability' => gamipress_get_manager_capability(),
257 'cols' => 1,
258 'boxes' => $boxes,
259 //'tabs' => $tabs,
260 'menuargs' => array(
261 'menu_title' => __( 'Licenses', 'gamipress' ),
262 'menu_slug' => 'gamipress_licenses',
263 ),
264 'savetxt' => __( 'Save Changes', 'gamipress' ),
265 'resettxt' => false,
266 ) );
267 } catch ( Exception $e ) {
268
269 }
270
271 }
272 add_action( 'cmb2_admin_init', 'gamipress_register_licenses_page' );
273
274 /**
275 * License field thumbnail.
276 *
277 * @since 1.1.1
278 *
279 * @param array $field_args Current field args
280 * @param CMB2_Field $field Current field object
281 */
282 function gamipress_license_field_before( $field_args, $field ) {
283
284 if( isset( $field_args['thumbnail'] ) && ! empty( $field_args['thumbnail'] ) ) : ?>
285
286 <div class="gamipress-license-thumbnail">
287 <img src="<?php echo $field_args['thumbnail']; ?>" alt="<?php echo $field_args['item_name']; ?>">
288 </div>
289
290 <?php endif;
291
292 }
293
294 /**
295 * Force package and download link update for licensed plugins.
296 *
297 * @since 1.4.8
298 *
299 * @param string $file Plugin file
300 * @param array $plugin_data An array of plugin data.
301 */
302 function gamipress_license_plugin_update_row( $file, $plugin_data ) {
303
304 $update_cache = get_site_transient( 'update_plugins' );
305
306 if ( ! isset( $update_cache->response[ $file ] ) ) {
307 return;
308 }
309
310 $response = $update_cache->response[ $file ];
311
312 // If there is not a package link, then try to update it
313 if ( empty( $response->package ) ) {
314
315 // Turn plugin slug like 'plugin-slug' to 'plugin_slug'
316 $slug = str_replace( '-', '_', $response->slug );
317
318 // Get the stored license key
319 $license = gamipress_get_option( $slug . '_license', '' );
320
321 // Check the license status
322 $license_status = rgc_cmb2_edd_license_status( $license );
323
324 if( $license_status === 'valid' ) {
325
326 // Make a new request to the API to check package and download link
327 $api_params = array(
328 'edd_action' => 'get_version',
329 'license' => $license,
330 'item_name' => $response->name,
331 'slug' => $response->slug,
332 'url' => home_url(),
333 );
334
335 $api_request = wp_remote_post( 'https://gamipress.com/edd-sl-api', array( 'timeout' => 15, 'sslverify' => true, 'body' => $api_params ) );
336
337 if ( ! is_wp_error( $api_request ) ) {
338
339 // Decode the API response
340 $version_info = json_decode( wp_remote_retrieve_body( $api_request ) );
341
342 // If package link provided, update it
343 if( ! empty( $version_info->package ) ) {
344 $update_cache->response[ $file ]->package = $version_info->package;
345 }
346
347 // If download link provided, update it
348 if( ! empty( $version_info->download_link ) ) {
349 $update_cache->response[ $file ]->download_link = $version_info->download_link;
350 }
351
352 // Update site transient with updated data
353 set_site_transient( 'update_plugins', $update_cache );
354
355 }
356
357 }
358
359 }
360
361 }
362
363 /**
364 * Advice to user about invalid license keys
365 *
366 * @param array $plugin_data
367 * @param array $response
368 */
369 function gamipress_license_in_plugin_update_message( $plugin_data, $response ) {
370
371 // Turn plugin slug like 'plugin-slug' to 'plugin_slug'
372 $slug = str_replace( '-', '_', $response->slug );
373
374 // Get the stored license key
375 $license = gamipress_get_option( $slug . '_license', '' );
376
377 // Check the license status
378 $license_status = rgc_cmb2_edd_license_status( $license );
379
380 if( $license_status !== 'valid' ) {
381
382 echo '&nbsp;<strong><a href="' . esc_url( admin_url( 'admin.php?page=gamipress_licenses' ) ) . '">' . __( 'Enter valid license key for automatic updates.', 'gamipress' ) . '</a></strong>';
383
384 }
385
386 }
387
388 /**
389 * Filter to set correct option key for our licenses
390 *
391 * @since 1.3.9.6
392 *
393 * @param string $option_key
394 * @param CMB2 $cmb
395 *
396 * @return string
397 */
398 function gamipress_licenses_option_key( $option_key, $cmb ) {
399
400 if( isset( $cmb->meta_box['show_on'] ) && isset( $cmb->meta_box['show_on']['option_key'] ) ) {
401 return $cmb->meta_box['show_on']['option_key'];
402 }
403
404 return $option_key;
405
406 }
407 add_filter( 'cmb2_edd_license_option_key', 'gamipress_licenses_option_key', 10, 2 );
408
409 /**
410 * Before licenses form
411 *
412 * @since 1.0.0
413 *
414 * @param string $filterable
415 * @param string $page
416 *
417 * @return string
418 */
419 function gamipress_licenses_before_form( $filterable, $page ) {
420
421 if( $page !== 'gamipress_licenses' ) {
422 return $filterable;
423 }
424
425 $output = '<em class="gamipress-licenses-intructions">'
426 . sprintf( __( 'Looking to install a pro add-on? Check the <a href="%s" target="_blank">installation instructions</a>.', 'gamipress' ), 'https://gamipress.com/docs/getting-started/installing-pro-add-ons/' )
427 . '</em>';
428
429 return $filterable . $output;
430
431 }
432 add_filter( 'cmb2metatabs_before_form', 'gamipress_licenses_before_form', 10, 2 );
433