-
Notifications
You must be signed in to change notification settings - Fork 0
/
gravityperks.php
1998 lines (1480 loc) · 57.2 KB
/
gravityperks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Plugin Name: Gravity Perks
* Plugin URI: https://gravitywiz.com/
* Description: Effortlessly install and manage small functionality enhancements (aka "perks") for Gravity Forms.
* Version: 2.3.4
* Author: Gravity Wiz
* Author URI: https://gravitywiz.com/
* License: GPL2
* Text Domain: gravityperks
* Domain Path: /languages
* Update URI: https://gravitywiz.com/updates/gravityperks
*/
define( 'GRAVITY_PERKS_VERSION', '2.3.4' );
/**
* Include the perk model as early as possible to when Perk plugins are loaded, they can safely extend
* the GWPerk class.
*/
require_once( plugin_dir_path( __FILE__ ) . 'model/perk.php' );
add_action( 'init', array( 'GravityPerks', 'init' ) );
add_action( 'gform_loaded', array( 'GravityPerks', 'init_perk_as_plugin_functionality' ) );
class GravityPerks {
public static $version = GRAVITY_PERKS_VERSION;
public static $tooltip_template = '<h6>%s</h6> %s';
private static $basename;
private static $slug = 'gravityperks';
private static $min_gravity_forms_version = '2.2';
private static $min_wp_version = '4.8';
/**
* @var GWAPI
*/
private static $api;
/**
* TODO: review...
*
* Need to store a modified version of the form object based the on the gform_admin_pre_render hook for use
* in perk hooks.
*
* Example usage: GWPreventSubmit::add_form_setting()
*
* @var array
*/
public static $form;
/**
* Set to true by the GWPerk class when any perk enqueues a form setting via the
* GWPerk::enqueue_form_setting() function
*
* @var bool
*/
public static $has_form_settings;
/**
* Set to true by the GWPerk class when any perk enqueues a field setting via the
* GWPerk::enqueue_field_setting() function.
*
* @var bool
*/
private static $has_field_settings;
/**
* When displaying a plugin row message, the first message display will also output a small style to fix the bottom
* border styling issue which WP handles for plugins with updates, but not with notices.
*
* @see self::display_plugin_row_message()
*
* @var mixed
*
*/
private static $plugin_row_styled;
// CACHE VARIABLES //
private static $installed_perks;
// INITIALIZE //
public static function init() {
self::define_constants();
self::$basename = plugin_basename( __FILE__ );
require_once( self::get_base_path() . '/includes/functions.php' );
require_once( self::get_base_path() . '/includes/deprecated.php' );
load_plugin_textdomain( 'gravityperks', false, basename( dirname( __file__ ) ) . '/languages/' );
if ( ! self::is_gravity_forms_supported() ) {
self::handle_error( 'gravity_forms_required' );
} elseif ( ! self::is_wp_supported() ) {
self::handle_error( 'wp_required' );
}
self::maybe_setup();
self::load_api();
self::register_scripts();
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) {
global $pagenow;
self::include_admin_files();
// enqueue welcome pointer script
add_action( 'admin_enqueue_scripts', array( 'GWPerks', 'welcome_pointer' ) );
// show Perk item in GF menu
add_filter( 'gform_addon_navigation', array( 'GWPerks', 'add_menu_item' ) );
// show various plugin messages after the plugin row
add_action( 'after_plugin_row_' . self::$basename, array( 'GWPerks', 'after_plugin_row' ), 10, 2 );
add_action( 'after_plugin_row', array( 'GWPerks', 'after_perk_plugin_row' ), 10, 2 );
if ( self::is_gravity_perks_page() ) {
if ( rgget( 'view' ) ) {
require_once( self::get_base_path() . '/admin/manage_perks.php' );
GWPerksPage::load_perk_settings();
}
add_thickbox();
}
if ( self::is_gf_version_lte( '2.5-beta-1' ) && self::is_gravity_page() ) {
add_filter( 'gform_admin_pre_render', array( 'GWPerks', 'store_modified_form' ), 11 );
add_action( 'gform_editor_js', array( 'GWPerks', 'add_form_editor_tabs' ), 1 );
}
} elseif ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
add_action( 'wp_ajax_gwp_manage_perk', array( 'GWPerks', 'manage_perk' ) );
add_action( 'wp_ajax_gwp_dismiss_pointers', array( __class__, 'dismiss_pointers' ) );
add_action( 'wp_ajax_gwp_dismiss_announcement', array( 'GWPerks', 'dismiss_announcement' ) );
}
add_filter( 'admin_body_class', array( __class__, 'add_helper_body_classes' ) );
add_action( 'gform_logging_supported', array( __class__, 'enable_logging_support' ) );
// Add Perks tab to form editor.
add_action( 'gform_field_settings_tabs', array( __class__, 'add_perks_tab' ) );
add_action( 'gform_field_settings_tab_content_gravity-perks', array( __class__, 'add_perks_tab_settings' ) );
add_action( 'gform_field_standard_settings', array( __class__, 'dynamic_setting_actions' ), 10, 2 );
add_action( 'gform_field_appearance_settings', array( __class__, 'dynamic_setting_actions' ), 10, 2 );
add_action( 'gform_field_advanced_settings', array( __class__, 'dynamic_setting_actions' ), 10, 2 );
add_action( 'activate_plugin', array( __class__, 'register_perk_activation_hooks' ) );
add_filter( 'plugin_action_links', array( __class__, 'plugin_manage_perks_link' ), 10, 2 );
add_action( 'admin_notices', array( __class__, 'get_dashboard_announcements' ) );
add_filter( 'plugin_auto_update_setting_html', array( __CLASS__, 'disable_auto_updater' ), 10, 3 );
// load and init all active perks
self::initialize_perks();
}
public static function add_helper_body_classes( $body_class ) {
if ( is_callable( array( 'GFForms', 'get_page' ) ) && GFForms::get_page() && ! self::is_gf_version_gte( '2.5-beta-1' ) ) {
$body_class .= ' gf-legacy-ui';
}
return $body_class;
}
public static function register_perk_activation_hooks( $plugin ) {
if ( ! GP_Perk::is_perk( $plugin ) ) {
return;
}
$perk = GWPerk::get_perk( $plugin );
if ( is_wp_error( $perk ) || ! $perk->is_supported() ) {
return;
}
$perk->activate();
}
public static function plugin_manage_perks_link( $links, $file ) {
if ( $file != plugin_basename( __FILE__ ) ) {
return $links;
}
array_unshift( $links, '<a href="' . esc_url( admin_url( 'admin.php' ) ) . '?page=gwp_perks">' . esc_html__( 'Manage Perks', 'gravityperks' ) . '</a>' );
return $links;
}
public static function define_constants() {
if ( ! defined( 'GW_DOMAIN' ) ) {
define( 'GW_DOMAIN', 'gravitywiz.com' );
}
if ( ! defined( 'GW_PROTOCOL' ) ) {
define( 'GW_PROTOCOL', 'https' );
}
define( 'GW_URL', GW_PROTOCOL . '://' . GW_DOMAIN );
if ( ! defined( 'GWAPI_URL' ) ) {
define( 'GWAPI_URL', GW_URL . '/gwapi/v3/' ); // @used storefront_api.php
}
define( 'GW_UPGRADE_URL', GW_URL . '/upgrade/' );
define( 'GW_ACCOUNT_URL', GW_URL . '/account/' );
define( 'GW_SUPPORT_URL', GW_URL . '/support/' );
define( 'GW_BUY_URL', GW_URL );
define( 'GW_GFORM_AFFILIATE_URL', 'http://gwiz.io/gravityforms' );
define( 'GW_MANAGE_PERKS_URL', admin_url( 'admin.php?page=gwp_perks' ) );
define( 'GW_PRICE_ID_LEGACY_SINGLE', 0 );
define( 'GW_PRICE_ID_LEGACY_UNLIMITED', 1 );
define( 'GW_PRICE_ID_BASIC', 2 );
define( 'GW_PRICE_ID_ADVANCED', 3 );
define( 'GW_PRICE_ID_PRO', 4 );
/**
* @deprecated 2.0
* @remove 2.1
*/
define( 'GW_SETTINGS_URL', admin_url( 'admin.php?page=gf_settings&addon=Perks&subview=Perks' ) );
// WP Engine throws an error if we set this directly.
$register_license_url = esc_url_raw( add_query_arg( array( 'register' => 1 ), GW_SETTINGS_URL ) );
/**
* @deprecated 2.0
* @remove 2.1
*/
define( 'GW_REGISTER_LICENSE_URL', $register_license_url );
/**
* @deprecated 2.0
* @remove 2.1
*/
define( 'GW_BUY_GPERKS_URL', GW_URL );
}
public static function activation() {
self::init_perk_as_plugin_functionality();
}
public static function disable_auto_updater( $html, $plugin_file, $plugin_data ) {
if ( self::has_valid_license() ) {
return $html;
}
if ( in_array( $plugin_file, array_keys( GWPerks::get_installed_perks() ) ) ) {
$html = '<em>' . __( 'Register Gravity Perks to enable auto-updates.', 'gravityperks' ) . '</em>';
}
return $html;
}
/**
* Get all active perks, load Perk objects, and initialize.
*
* By default, perks are only initialized by Gravity Perks. Since they are plugins they have the option to
* initialize themselves; however, they will need to use a different init function name than "init" as this
* will always be loaded by default.
*
* IF IS NETWORK ADMIN
* - only init network-activated perks
* - only handle errors for network-activated perks
* IF IS SINGLE ADMIN
* - init network-activated perks and single-activated perks
* - only handles errors for
*
*/
private static function initialize_perks() {
$network_perks = get_site_option( 'gwp_active_network_perks' );
// if on the network admin, only handle network-activated perks
$perks = is_network_admin() ? array() : get_option( 'gwp_active_perks' );
if ( ! $network_perks ) {
$network_perks = array();
}
if ( ! $perks ) {
$perks = array();
}
$perks = array_merge( $network_perks, $perks );
foreach ( $perks as $perk_file => $perk_data ) {
$perk = GWPerk::get_perk( $perk_file );
// New perks (which have a 'parent' property) will be initialized by Gravity Forms via the Add-on Framework.
if ( is_wp_error( $perk ) || ! $perk->is_old_school() ) {
continue;
}
if ( $perk->is_supported() ) {
$perk->init();
} else {
foreach ( $perk->get_failed_requirements() as $requirement ) {
self::handle_error( gwar( $requirement, 'code' ), $perk_file, gwar( $requirement, 'message' ) );
}
}
}
}
/**
* Include admin files required on all pages
*
*/
private static function include_admin_files() {
require_once( self::get_base_path() . '/model/notice.php' );
}
private static function maybe_setup() {
// maybe set up Gravity Perks; only on admin requests for single site installs and always for multisite
$is_non_ajax_admin = is_admin() && ( defined( 'DOING_AJAX' ) && DOING_AJAX === true ) === false;
if ( ! $is_non_ajax_admin && ! is_multisite() ) {
return;
}
$has_version_changed = get_option( 'gperks_version' ) != self::$version;
// making sure version has really changed; gets around aggressive caching issue on some sites that cause setup to run multiple times
if ( $has_version_changed && is_callable( array( 'GFForms', 'get_wp_option' ) ) ) {
$has_version_changed = GFForms::get_wp_option( 'gperks_version' ) != self::$version;
}
if ( ! $has_version_changed ) {
return;
}
self::setup();
}
private static function setup() {
// force license to be revalidated
self::flush_license( true );
update_option( 'gperks_version', self::$version );
}
// CLASS INTERFACE //
/**
* Called by perks when the "Perks" field settings tab is required.
*/
public static function enqueue_field_settings() {
self::$has_field_settings = true;
}
public static function add_perks_tab( $tabs ) {
$tabs[] = array(
'id' => 'gravity-perks',
'title' => __( 'Perks', 'gravity-perks' ),
'toggle_classes' => array(),
'body_classes' => array( 'panel-block-tabs__body--settings' ),
);
return $tabs;
}
public static function add_perks_tab_settings() {
do_action( 'gws_field_settings' );
do_action( 'gperk_field_settings' );
}
// ERRORS AND NOTICES //
public static function handle_error( $error_slug, $plugin_file = false, $message = '' ) {
global $pagenow;
$plugin_file = $plugin_file ? $plugin_file : self::$basename;
$is_perk = $plugin_file != self::$basename;
$action = $is_perk ? array( 'GWPerks', 'after_perk_plugin_row' ) : array( 'GWPerks', 'after_plugin_row' );
$is_plugins_page = self::is_plugins_page();
switch ( $error_slug ) {
case 'gravity_forms_required':
$message = self::get_message( $error_slug, $plugin_file );
$message_function = array(
new GP_Late_Static_Binding( array(
'message' => $message,
'class' => 'error',
) ),
'GravityPerks_maybe_display_admin_message',
);
add_action( 'admin_notices', $message_function );
add_action( 'network_admin_notices', $message_function );
break;
case 'wp_required':
$message = self::get_message( $error_slug, $plugin_file );
$message_function = array(
new GP_Late_Static_Binding( array(
'message' => $message,
'class' => 'error',
) ),
'GravityPerks_maybe_display_admin_message',
);
add_action( 'admin_notices', $message_function );
add_action( 'network_admin_notices', $message_function );
break;
case 'gravity_perks_required':
$message = self::get_message( $error_slug, $plugin_file );
$message_function = array(
new GP_Late_Static_Binding( array(
'message' => $message,
'class' => 'error',
) ),
'GravityPerks_maybe_display_admin_message',
);
add_action( 'admin_notices', $message_function );
add_action( 'network_admin_notices', $message_function );
break;
default:
if ( ! $message || ! $is_plugins_page ) {
return;
}
$message_function = array(
new GP_Late_Static_Binding( array(
'message' => $message,
'class' => 'error',
) ),
'GravityPerks_display_admin_message',
);
add_action( 'admin_notices', $message_function );
add_action( 'network_admin_notices', $message_function );
}
if ( isset( $message_function ) ) {
wp_enqueue_style( 'gwp-plugins', self::get_base_url() . '/styles/plugins.css' );
}
return;
}
public static function get_message( $message_slug, $plugin_file = false ) {
$min_gravity_forms_version = self::$min_gravity_forms_version;
$min_wp_version = self::$min_wp_version;
// if a $plugin_file is provided AND it is not the same as the base plugin, let's assume it is a perk
$is_perk = $plugin_file && $plugin_file != self::$basename;
if ( $is_perk ) {
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$perk = GWPerk::get_perk( $plugin_file );
$perk_data = GWPerk::get_perk_data( $plugin_file );
if ( $perk->is_old_school() ) {
$min_gravity_forms_version = $perk->get_property( 'min_gravity_forms_version' );
$min_wp_version = $perk->get_property( 'min_wp_version' );
} else {
$requirements = $perk->parent->minimum_requirements();
$min_gravity_forms_version = rgars( $requirements, 'gravityforms/version' );
$min_wp_version = rgars( $requirements, 'wordpress/version' );
}
}
switch ( $message_slug ) {
case 'gravity_forms_required':
if ( class_exists( 'GFForms' ) ) {
return sprintf(__( 'Current Gravity Forms version (%1$s) does not meet minimum Gravity Forms version requirement (%2$s).', 'gravityperks' ),
GFForms::$version, $min_gravity_forms_version);
} else {
return sprintf(__( 'Gravity Forms %1$s or greater is required. Activate it now or %2$spurchase it today!%3$s', 'gravityperks' ),
$min_gravity_forms_version, '<a href="' . GW_GFORM_AFFILIATE_URL . '">', '</a>');
}
case 'wp_required':
if ( isset( $perk ) ) {
return sprintf(__( '%1$s requires WordPress %2$s or greater. You must upgrade WordPress in order to use this perk.', 'gravityperks' ),
$perk_data['Name'], $min_wp_version);
} else {
return sprintf(__( 'Gravity Perks requires WordPress %1$s or greater. You must upgrade WordPress in order to use Gravity Perks.', 'gravityperks' ),
$min_wp_version);
}
case 'gravity_perks_required':
return sprintf(__( '%1$s requires Gravity Perks %2$s or greater. Activate it now or %3$spurchase it today!%4$s', 'gravityperks' ),
$perk_data['Name'], $perk->get_property( 'min_gravity_perks_version' ), '<a href="' . GW_BUY_URL . '" target="_blank">', '</a>');
case 'register_gravity_perks':
if ( isset( $perk ) ) {
return sprintf(__( '%1$sRegister%2$s your copy of Gravity Perks to receive access to automatic upgrades and support for this perk. Need a license key? %3$sPurchase one now.%2$s', 'gravityperks' ),
'<a href="' . GW_MANAGE_PERKS_URL . '">', '</a>', '<a href="' . GW_BUY_URL . '" target="_blank">');
} else {
return sprintf(__( '%1$sRegister%2$s your copy of Gravity Perks to receive access to automatic upgrades and support. Need a license key? %3$sPurchase one now.%2$s', 'gravityperks' ),
'<a href="' . GW_MANAGE_PERKS_URL . '">', '</a>', '<a href="' . GW_BUY_URL . '" target="_blank">');
}
}
return '';
}
public static function after_plugin_row( $plugin_file, $plugin_data ) {
$template = '<p>%s</p>';
if ( ! self::has_valid_license() ) {
$message = self::get_message( 'register_gravity_perks' );
self::display_plugin_row_message( sprintf( $template, $message ), $plugin_data, true, $plugin_file );
} elseif ( ! self::is_gravity_forms_supported() ) {
$message = self::get_message( 'gravity_forms_required' );
self::display_plugin_row_message( sprintf( $template, $message ), $plugin_data, true, $plugin_file );
} elseif ( ! self::is_wp_supported() ) {
$message = self::get_message( 'wp_required' );
self::display_plugin_row_message( sprintf( $template, $message ), $plugin_data, true, $plugin_file );
}
}
public static function after_perk_plugin_row( $plugin_file, $plugin_data ) {
if ( empty( $plugin_data['Perk'] ) ) {
return;
}
if ( ! self::has_valid_license() ) {
$message = self::get_message( 'register_gravity_perks', $plugin_file );
self::display_plugin_row_message( "<p>{$message}</p>", $plugin_data, true, $plugin_file );
}
$perk = GWPerk::get_perk( $plugin_file );
if ( is_wp_error( $perk ) ) {
return;
}
if ( ! $perk->is_supported() && $perk->is_old_school() ) {
$messages = $perk->get_requirement_messages( $perk->get_failed_requirements() );
$message = count( $messages ) > 1 ? '<ul><li>' . implode( '</li><li>', $messages ) . '</li></ul>' : "<p>{$messages[0]}</p>";
self::display_plugin_row_message( $message, $plugin_data, true, $plugin_file );
}
}
public static function display_admin_message( $message, $class ) {
?>
<div id="message" class="<?php echo $class; ?> gwp-message"><p><?php echo $message; ?></p></div>
<?php
}
public static function display_plugin_row_message( $message, $plugin_data, $is_error = false, $plugin_file = false ) {
$id = sanitize_title( $plugin_data['Name'] );
$is_active = false;
if ( $plugin_file ) {
$is_active = is_network_admin() ? is_plugin_active_for_network( $plugin_file ) : is_plugin_active( $plugin_file );
}
$active = $is_active ? 'active' : 'inactive';
?>
<style type="text/css" scoped>
<?php printf( '#%1$s td, #%1$s th', $id ); ?>,
<?php printf( 'tr[data-slug="%1$s"] td, tr[data-slug="%1$s"] th', $id ); ?>
{
border-bottom: 0;
box-shadow: none !important;
-webkit-box-shadow: none !important;
}
.gwp-plugin-notice td {
box-shadow: none !important;
padding: 0 !important;
}
.gwp-plugin-notice + tr[data-slug]:not(.plugin-update-tr) {
box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.1);
}
/*.gwp-plugin-notice + tr.active[data-slug]:not(.plugin-update-tr) > * {*/
/* box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.1) !important;*/
/*}*/
tr.plugin-update-tr.active.gwp-plugin-notice > * {
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.1) !important;
}
.plugin-update-tr[data-slug^="gp-"] + tr[data-slug]:not(.plugin-update-tr) {
box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.1);
}
</style>
<tr class="plugin-update-tr <?php echo $active; ?> gwp-plugin-notice">
<td colspan="4" class="colspanchange">
<div class="update-message notice inline notice-error notice-alt"><?php echo $message; ?></div>
</td>
</tr>
<?php
}
// IS SUPPORTED //
public static function is_gravity_forms_supported( $min_version = false ) {
$min_version = $min_version ? $min_version : self::$min_gravity_forms_version;
return class_exists( 'GFCommon' ) && version_compare( GFCommon::$version, $min_version, '>=' );
}
public static function is_wp_supported( $min_version = false ) {
$min_version = $min_version ? $min_version : self::$min_wp_version;
return version_compare( get_bloginfo( 'version' ), $min_version, '>=' );
}
public static function get_version() {
return self::$version;
}
// PERKS AS PLUGINS //
/**
* Initalize all functionality that enables Perks to be plugins but also managed as perks.
*
*/
public static function init_perk_as_plugin_functionality() {
require_once( 'model/class-gp-plugin.php' );
require_once( 'model/class-gp-feed-plugin.php' );
add_filter( 'extra_plugin_headers', array( __class__, 'extra_perk_headers' ) );
// any time a plugin is activated/deactivated, refresh the list of active perks
add_action( 'update_site_option_active_sitewide_plugins', array( __class__, 'refresh_active_sitewide_perks' ) );
add_action( 'update_option_active_plugins', array( __class__, 'refresh_active_perks' ) );
// any time a plugin is activated/deactivated, reorder plugin loading to load Gravity Perks first
add_filter( 'pre_update_site_option_active_sitewide_plugins', array( __class__, 'reorder_plugins' ) );
add_filter( 'pre_update_option_active_plugins', array( __class__, 'reorder_plugins' ) );
// add "manage perks" link after perk install/update
add_filter( 'install_plugin_complete_actions', array( __class__, 'add_manage_perks_action' ), 10, 3 );
add_filter( 'update_plugin_complete_actions', array( __class__, 'add_manage_perks_action' ), 10, 2 );
// add "upgrade license" button after perk install/update as necessary
add_filter( 'install_plugin_complete_actions', array( __class__, 'add_upgrade_license_action' ), 10, 3 );
add_filter( 'update_plugin_complete_actions', array( __class__, 'add_upgrade_license_action' ), 10, 2 );
// add "buy license" button and text after perk install/update as necessary
add_filter( 'install_plugin_complete_actions', array( __class__, 'add_invalid_license_action' ), 10, 3 );
add_filter( 'update_plugin_complete_actions', array( __class__, 'add_invalid_license_action' ), 10, 2 );
// display "back to perks" link on plugins page
add_action( 'pre_current_active_plugins', array( __CLASS__, 'display_perks_status_message' ) );
// when deleting plugins, output a script to update the "No, Return me to the plugin list" button verbiage
add_action( 'admin_action_delete-selected', array( __class__, 'maybe_add_perk_delete_confirmation' ) );
if ( is_multisite() ) {
// prevent perks from being network activated if Gravity Perks is not network activated, priority 11 so it fires after 'save_last_modified_plugin'
add_action( 'admin_action_activate', array( __class__, 'require_gravity_perks_network_activation' ), 11 );
}
// initiate a fix for Windows servers where the GP package file name is too long and prevents installs/updates from processing
add_filter( 'upgrader_pre_download', array( __class__, 'maybe_shorten_edd_filename' ), 10, 4 );
do_action( 'gperks_loaded' );
}
/**
* Check if the URL that is about to be downloaded is an EDD package URL. If so, hook our function to shorten
* the filename.
*
* @param mixed $return
* @param string $package The URL of the file to be downloaded.
*
* @return mixed
*/
public static function maybe_shorten_edd_filename( $return, $package ) {
if ( strpos( $package, '/edd-sl/package_download/' ) !== false ) {
add_filter( 'wp_unique_filename', array( __class__, 'shorten_edd_filename' ), 10, 2 );
}
return $return;
}
/**
* Truncate the temporary filename to 50 characters. This resolves issues with some Windows servers which
* can not handle very long filenames.
*
* @param string $filename
* @param string $ext
*
* @return string
*/
public static function shorten_edd_filename( $filename, $ext ) {
$filename = substr( $filename, 0, 50 ) . $ext;
remove_filter( 'wp_unique_filename', array( __class__, 'shorten_edd_filename' ), 10 );
return $filename;
}
public static function add_manage_perks_action( $actions, $api, $plugin_file = false ) {
if ( ( ! $plugin_file || ! GWPerk::is_perk( $plugin_file, true ) ) && ! self::is_request_from_gravity_perks() ) {
return $actions;
}
// if we're coming from Manage Perk's page...
if ( self::is_request_from_gravity_perks() ) {
$actions['manage_perks'] = '<a href="' . GW_MANAGE_PERKS_URL . '">' . __( 'Back to Manage Perks', 'gravityperks' ) . '</a>';
unset( $actions['plugins_page'] );
} else {
$actions['manage_perks'] = ' | <a href="' . GW_MANAGE_PERKS_URL . '">' . __( 'Manage Perks', 'gravityperks' ) . '</a>';
}
if ( isset( $actions['activate_plugin'] ) ) {
$actions['activate_plugin'] = str_replace( __( 'Activate Plugin' ), __( 'Activate Perk', 'gravityperks' ), $actions['activate_plugin'] );
}
return $actions;
}
public static function add_upgrade_license_action( $actions, $api, $plugin_file = false ) {
if ( ( ! $plugin_file || ! GWPerk::is_perk( $plugin_file, true ) ) && ! self::is_request_from_gravity_perks() ) {
return $actions;
}
if ( empty( $api->download_link ) && self::has_valid_license() && ! self::has_available_perks() ) {
$actions['upgrade_license'] = '<div class="notice notice-info gp-plugin-action-perk-limit">
<p>
You‘ve reached your perk registration limit. Upgrade your license to install more perks.
</p>
<p>
<a class="button-primary" href="' . self::get_license_upgrade_url() . '">Upgrade License</a>
</p>
</div>';
}
return $actions;
}
public static function add_invalid_license_action( $actions, $api, $plugin_file = false ) {
if ( ( ! $plugin_file || ! GWPerk::is_perk( $plugin_file, true ) ) && ! self::is_request_from_gravity_perks() ) {
return $actions;
}
if ( ! self::has_valid_license() && empty( $api->download_link ) ) {
$actions['invalid_license'] = '<div class="notice notice-error gp-plugin-action-invalid-license">
<p>
<strong>Uh-oh!</strong> It looks like this site doesn‘t have a valid Gravity Perks license.
Please add your license under Manage Perks if you already have a license.
</p>
<p>
Otherwise, you may purchase a license by clicking “Buy License” below.
</p>
<p>
<a class="button-secondary" href="' . GW_MANAGE_PERKS_URL . '">Manage Perks</a>
<a class="button-secondary" href="' . GW_BUY_URL . '">Buy License</a>
</p>
</div>';
}
return $actions;
}
/**
* Pull the "Perk" header out of the plugin header data. Used to determine if the plugin is intended to be
* run by Gravity Perks.
*
*/
public static function extra_perk_headers( $headers ) {
array_push( $headers, 'Perk' );
return $headers;
}
/**
* Refresh the list of active perks. Triggered anytime the "active_plugins" or "active_sitewide_plugins" option is updated.
* This option is updated anytime a plugin is activated or deactivated.
*
*/
public static function refresh_active_perks( $old_value ) {
$plugins = self::get_plugins();
$perks = array();
$network_perks = array();
foreach ( $plugins as $plugin_file => $plugin ) {
// skip all non-perk plugins
if ( rgar( $plugin, 'Perk' ) != 'True' ) {
continue;
}
if ( is_multisite() && is_plugin_active_for_network( $plugin_file ) ) {
$network_perks[ $plugin_file ] = $plugin;
} elseif ( is_plugin_active( $plugin_file ) ) {
$perks[ $plugin_file ] = $plugin;
}
}
// if multsite, update network perks
if ( is_multisite() ) {
update_site_option( 'gwp_active_network_perks', $network_perks );
}
// update active perks every time
update_option( 'gwp_active_perks', $perks );
}
public static function refresh_active_sitewide_perks( $old_value ) {
self::refresh_active_perks( $old_value );
}
/**
* Update plugin loading order. Anytime the "active_plugins" option is updated, this function reorders the plugins, placing
* Gravity Perks as the first plugin to load to ensure that it is loaded before any individual Perk plugin.
*
*/
public static function reorder_plugins( $plugins ) {
$perks_file = plugin_basename( __FILE__ );
$index = array_search( $perks_file, $plugins );
if ( $index === false ) {
$index = array_key_exists( $perks_file, $plugins ) ? $perks_file : false;
}
if ( $index === false ) {
return $plugins;
}
$perks_item = array( $index => $plugins[ $index ] );
unset( $plugins[ $index ] );
if ( is_numeric( $index ) ) {
array_unshift( $plugins, $perks_file );
} else {
$plugins = array_merge( $perks_item, $plugins );
}
return $plugins;
}
public static function save_requesting_blog_id( $value ) {
$blog_id = isset( $_REQUEST['blog_id'] ) ? intval( $_REQUEST['blog_id'] ) : false;
update_option( 'gperk_requestee_blog_id', $blog_id );
}
public static function get_requesting_blog_id() {
return get_option( 'gperk_requestee_blog_id' );
}
public static function get_plugin_actions() {
return array( 'install-plugin', 'delete-selected', 'deactivate', 'activate' );
}
public static function display_perks_status_message() {
// @todo Revisit this function; see pre 1.2.21 version for original version. Has been stripped down for now.
$error = isset( $_GET['gwp_error'] ) ? $_GET['gwp_error'] : false;
if ( ! $error ) {
return;
}
$is_error = true;
$message = '';
switch ( $error ) {
case 'networkperks':
$message = __( '<strong>Gravity Perks</strong> must be network activated before a <strong>perk</strong> can be network activated.', 'gravityperks' );
break;
}
?>
<div class="<?php echo $is_error ? 'error' : 'updated'; ?> gwp-message">
<p><?php echo $message; ?></p>
</div>
<style type="text/css">
#message + div.gwp-message { margin-top:-17px; border-top-style: dotted;
border-top-right-radius: 0; border-top-left-radius: 0; }
</style>
<?php
}
public static function maybe_add_perk_delete_confirmation() {
// only show 'Return to Manage Perks Page' button if request came from GPerks
if ( self::is_request_from_gravity_perks() ) {
add_action( 'in_admin_footer', array( __class__, 'add_perk_delete_confirmation_script' ) );
}
}
/**
* Add 'Return to Manage Perks Page' button on plugin delete confirmation screen.
*
* There is no way to filter the default buttons on this screen, so let's add our own button and hide
* the existing button via JS.
*
*/
public static function add_perk_delete_confirmation_script() {
?>
<script type="text/javascript">
(function($){
var cancelButton = $('input[value="<?php _e( 'No, Return me to the plugin list' ); ?>"]');
var perksButton = $('<input value="<?php _e( 'No, Return to Manage Perks page', 'gravityperks' ); ?>" type="submit" class="button" />');
perksButton.insertAfter(cancelButton);
cancelButton.hide();
})(jQuery);
</script>
<?php
}
public static function get_manage_perks_site_select() {
$blogs = get_blogs_of_user( get_current_user_id() );
$site_select = '
<span id="manage-perks-site-select" style="display:none;">
<select onchange="if(this.value != \'\') { window.location.href = this.value };">
<option value="">Select Site</option>';
foreach ( $blogs as $blog ) {
$site_select .= '<option value="' . get_admin_url( $blog->userblog_id, 'admin.php?page=gwp_perks' ) . '">' . $blog->blogname . '</option>';
}
$site_select .= '
</select>
</span>';
return $site_select;
}