-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathFrmAddonsController.php
More file actions
1669 lines (1415 loc) · 43.4 KB
/
FrmAddonsController.php
File metadata and controls
1669 lines (1415 loc) · 43.4 KB
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
if ( ! defined( 'ABSPATH' ) ) {
die( 'You are not allowed to call this page directly.' );
}
class FrmAddonsController {
/**
* @var string
*/
const SCRIPT_HANDLE = 'frm-addons-page';
/**
* @var array
*/
private static $categories = array();
/**
* @var string
*/
private static $request_addon_url;
/**
* @var string
*/
protected static $plugin;
/**
* @since 6.15
*
* @return void
*/
public static function load_admin_hooks() {
add_action( 'admin_menu', self::class . '::menu', 100 );
add_filter( 'pre_set_site_transient_update_plugins', self::class . '::check_update' );
if ( ! FrmAppHelper::is_admin_page( 'formidable-addons' ) ) {
return;
}
self::$request_addon_url = 'https://connect.formidableforms.com/add-on-request/';
add_action( 'admin_enqueue_scripts', self::class . '::enqueue_assets', 15 );
add_filter( 'frm_show_footer_links', '__return_false' );
}
/**
* Enqueues the Add-Ons page scripts and styles.
*
* @since 6.15
*
* @return void
*/
public static function enqueue_assets() {
$plugin_url = FrmAppHelper::plugin_url();
$version = FrmAppHelper::plugin_version();
$js_dependencies = array(
'wp-i18n',
// This prevents a console error "wp.hooks is undefined" in WP versions older than 5.7.
'wp-hooks',
'formidable_dom',
);
// Enqueue styles that needed.
wp_enqueue_style( 'formidable-admin' );
wp_enqueue_style( 'formidable-grids' );
// Register and enqueue Add-Ons page style.
wp_register_style( self::SCRIPT_HANDLE, $plugin_url . '/css/admin/addons-page.css', array(), $version );
wp_enqueue_style( self::SCRIPT_HANDLE );
// Register and enqueue Add-Ons page script.
wp_register_script( self::SCRIPT_HANDLE, $plugin_url . '/js/addons-page.js', $js_dependencies, $version, true );
wp_localize_script( self::SCRIPT_HANDLE, 'frmAddonsVars', self::get_js_variables() );
wp_enqueue_script( self::SCRIPT_HANDLE );
wp_set_script_translations( self::SCRIPT_HANDLE, 'formidable' );
FrmAppHelper::dequeue_extra_global_scripts();
}
/**
* Get the Add-Ons page JS variables as an array.
*
* @since 6.15
*
* @return array
*/
private static function get_js_variables() {
return array(
'proIsIncluded' => FrmAppHelper::pro_is_included(),
'addonRequestURL' => self::$request_addon_url,
);
}
/**
* @return void
*/
public static function menu() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$label = __( 'Add-Ons', 'formidable' );
$label = '<span style="color:#3FCA89">' . esc_html( $label ) . '</span>';
add_submenu_page( 'formidable', 'Formidable | ' . __( 'Add-Ons', 'formidable' ), $label, 'frm_view_forms', 'formidable-addons', 'FrmAddonsController::list_addons' );
// Remove default created subpage, make the page with highest priority as default.
remove_submenu_page( 'formidable', 'formidable' );
if ( ! FrmAppHelper::pro_is_installed() ) {
$cta_text = FrmSalesApi::get_best_sale_value( 'menu_cta_text' );
if ( ! $cta_text ) {
$cta_text = __( 'Upgrade', 'formidable' );
}
add_submenu_page(
'formidable',
'Formidable | ' . __( 'Upgrade', 'formidable' ),
'<span class="frm-upgrade-submenu">' . esc_html( $cta_text ) . '</span>',
'frm_view_forms',
'formidable-pro-upgrade',
function () {
// This function doesn't need to do anything.
// The redirect is handled earlier to avoid issues with the headers being sent.
}
);
} elseif ( 'formidable-pro-upgrade' === FrmAppHelper::get_param( 'page' ) ) {
wp_safe_redirect( admin_url( 'admin.php?page=formidable' ) );
exit;
}//end if
}
/**
* @return void
*/
public static function list_addons() {
FrmAppHelper::include_svg();
$view_path = FrmAppHelper::plugin_path() . '/classes/views/addons/';
$installed_addons = apply_filters( 'frm_installed_addons', array() );
$addons = self::get_api_addons();
$errors = array();
$license_type = '';
$request_addon_url = self::$request_addon_url;
if ( isset( $addons['error'] ) ) {
$api = new FrmFormApi();
$errors = $api->get_error_from_response( $addons );
$license_type = $addons['error']['type'] ?? '';
unset( $addons['error'] );
}
$pro = array(
'pro' => array(
'title' => 'Formidable Forms Pro',
'slug' => 'formidable-pro',
'released' => '2011-02-05',
'docs' => 'knowledgebase/',
'categories' => array( 'basic', 'plus', 'business', 'elite' ),
'excerpt' => 'Create calculators, surveys, smart forms, and data-driven applications. Build directories, real estate listings, job boards, and much more.',
),
);
$addons = $pro + $addons;
self::prepare_addons( $addons );
$pricing = FrmAppHelper::admin_upgrade_link( 'addons' );
self::organize_and_get_categories();
$categories = self::$categories;
include $view_path . 'index.php';
}
/**
* Organize and set categories.
*
* @since 6.15
*
* @return void
*/
protected static function organize_and_get_categories() {
unset( self::$categories['strategy11'] );
ksort( self::$categories );
$bottom_categories = array();
$plans = FrmFormsHelper::get_license_types(
array(
'include_all' => false,
'case_lower' => true,
)
);
// Extract the elements to move
foreach ( $plans as $plan ) {
if ( ! isset( self::$categories[ $plan ] ) ) {
continue;
}
$bottom_categories[ $plan ] = self::$categories[ $plan ];
unset( self::$categories[ $plan ] );
}
$special_categories = array();
if ( 'elite' !== self::license_type() ) {
$special_categories['available-addons'] = array(
'name' => __( 'Available', 'formidable' ),
// To be assigned via JavaScript.
'count' => 0,
);
}
$special_categories['active-addons'] = array(
'name' => __( 'Active', 'formidable' ),
// To be assigned via JavaScript.
'count' => 0,
);
$special_categories['all-items'] = array(
'name' => __( 'All Add-Ons', 'formidable' ),
// To be assigned via JavaScript.
'count' => 0,
);
self::$categories = array_merge(
$special_categories,
self::$categories,
$bottom_categories
);
}
/**
* Organize and set categories.
*
* @since 6.15
*
* @param array $addon The addon array that will be modified by reference.
*
* @return void
*/
protected static function set_categories( &$addon ) {
if ( ! isset( $addon['categories'] ) ) {
return;
}
$addon['category-slugs'] = array();
foreach ( $addon['categories'] as $category ) {
$category = FrmFormsHelper::convert_legacy_package_names( $category );
$category_slug = sanitize_title( $category );
// Add the slug to the new array.
$addon['category-slugs'][] = $category_slug;
if ( ! isset( self::$categories[ $category_slug ] ) ) {
self::$categories[ $category_slug ] = array(
'name' => $category,
'count' => 0,
);
}
++self::$categories[ $category_slug ]['count'];
}
}
/**
* @return void
*/
public static function license_settings() {
$plugins = apply_filters( 'frm_installed_addons', array() );
if ( ! $plugins ) {
esc_html_e( 'There are no plugins on your site that require a license', 'formidable' );
return;
}
ksort( $plugins );
include FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php';
}
/**
* @return array
*/
protected static function get_api_addons() {
$api = new FrmFormApi();
$addons = $api->get_api_info();
if ( ! $addons ) {
return self::fallback_plugin_list();
}
foreach ( $addons as $k => $addon ) {
if ( empty( $addon['excerpt'] ) && $k !== 'error' ) {
unset( $addons[ $k ] );
}
}
return $addons;
}
/**
* Retrieves the count of available addons.
*
* @since 6.9
*
* @return int Count of addons.
*/
public static function get_addons_count() {
return count( self::get_api_addons() );
}
/**
* If the API is unable to connect, show something on the addons page
*
* @since 3.04.03
*
* @return array
*/
protected static function fallback_plugin_list() {
$list = array(
'formidable-pro' => array(
'title' => 'Formidable Forms Pro',
'link' => 'pricing/',
'docs' => '',
'excerpt' => 'Enhance your basic Formidable forms with a plethora of Pro field types and features. Create advanced forms and data-driven applications in minutes.',
),
'mailchimp' => array(
'title' => 'Mailchimp Forms',
'excerpt' => 'Get on the path to more sales and leads in a matter of minutes. Add leads to a Mailchimp mailing list when they submit forms and update their information along with the entry.', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
),
'registration' => array(
'title' => 'User Registration Forms',
'link' => 'downloads/user-registration/',
'excerpt' => 'Give new users access to your site as quickly and painlessly as possible. Allow users to register, edit and be able to login to their profiles on your site from the front end in a clean, customized registration form.', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
),
'paypal' => array(
'title' => 'PayPal Standard Forms',
'link' => 'downloads/paypal-standard/',
'excerpt' => 'Automate your business by collecting instant payments from your clients. Collect information, calculate a total, and send them on to PayPal. Require a payment before publishing content on your site.', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
),
'stripe' => array(
'title' => 'Stripe Forms',
'docs' => 'knowledgebase/stripe/',
'excerpt' => 'Any Formidable forms on your site can accept credit card payments without users ever leaving your site.',
),
'authorize-net' => array(
'title' => 'Authorize.net AIM Forms',
'link' => 'downloads/authorize-net-aim/',
'docs' => 'knowledgebase/authorize-net-aim/',
'excerpt' => 'Accept one-time payments directly on your site, using Authorize.net AIM.',
),
'woocommerce' => array(
'title' => 'WooCommerce Forms',
'excerpt' => 'Use a Formidable form on your WooCommerce product pages.',
),
'autoresponder' => array(
'title' => 'Form Action Automation',
'docs' => 'knowledgebase/schedule-autoresponder/',
'excerpt' => 'Schedule email notifications, SMS messages, and API actions.',
),
'modal' => array(
'title' => 'Bootstrap Modal Forms',
'link' => 'downloads/bootstrap-modal/',
'docs' => 'knowledgebase/bootstrap-modal/',
'excerpt' => 'Open a view or form in a Bootstrap popup.',
),
'bootstrap' => array(
'title' => 'Bootstrap Style Forms',
'excerpt' => 'Instantly add Bootstrap styling to all your Formidable forms.',
),
'zapier' => array(
'title' => 'Zapier Forms',
'excerpt' => 'Connect with hundreds of different applications through Zapier. Insert a new row in a Google docs spreadsheet, post on Twitter, or add a new Dropbox file with your form.', // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
),
'signature' => array(
'title' => 'Digital Signature Forms',
'excerpt' => 'Add a signature field to your form. The user may write their signature with a trackpad/mouse or just type it.',
),
'api' => array(
'title' => 'Formidable Forms API',
'link' => 'downloads/formidable-api/',
'excerpt' => 'Send entry results to any other site that has a Rest API. This includes the option of sending entries from one Formidable site to another.',
),
'twilio' => array(
'title' => 'Twilio SMS Forms',
'docs' => 'knowledgebase/twilio-add-on/',
'excerpt' => 'Allow users to text their votes for polls created by Formidable Forms, or send SMS notifications when entries are submitted or updated.',
),
'views' => array(
'title' => 'Formidable Views',
'excerpt' => 'Add the power of views to your Formidable Forms to display your form submissions in listings, tables, calendars, and more.',
),
'quiz_maker' => array(
'title' => 'Quiz Maker',
'link' => 'downloads/quiz-maker/',
'excerpt' => 'Make quizzes, automatically score them and show user scores.',
),
);
$defaults = array(
'released' => '',
);
foreach ( $list as $k => $info ) {
$info['slug'] = $k;
$list[ $k ] = array_merge( $defaults, $info );
}
return $list;
}
/**
* If Pro is missing but has been authenticated, include a download URL
*
* @since 3.04.03
*
* @return string
*/
public static function get_pro_download_url() {
$api = new FrmFormApi( self::get_pro_license() );
$downloads = $api->get_api_info();
$pro = self::get_pro_from_addons( $downloads );
return $pro['url'] ?? '';
}
/**
* @since 4.08
*
* @return string
*/
public static function get_pro_license() {
$pro_cred_store = 'frmpro-credentials';
$pro_wpmu_store = 'frmpro-wpmu-sitewide';
if ( is_multisite() && get_site_option( $pro_wpmu_store ) ) {
$creds = get_site_option( $pro_cred_store );
} else {
$creds = get_option( $pro_cred_store );
}
if ( empty( $creds ) || ! is_array( $creds ) || ! isset( $creds['license'] ) ) {
return '';
}
$license = $creds['license'];
if ( ! $license ) {
return '';
}
if ( str_contains( $license, '-' ) ) {
// This is a fix for licenses saved in the past
return strtoupper( $license );
}
return $license;
}
/**
* @since 4.08
*
* @param array $addons
*
* @return array
*/
protected static function get_pro_from_addons( $addons ) {
return $addons['93790'] ?? array();
}
/**
* @since 4.06
* @since x.x Added the $force_type param.
*
* @param bool $force_type Whether to resolve grandfathered licenses to their real license type.
*
* @return string
*/
public static function license_type( $force_type = false ) {
if ( is_callable( 'FrmProAddonsController::license_type' ) ) {
return FrmProAddonsController::license_type( $force_type );
}
return 'free';
}
/**
* Determine the license status for payment fee decisions.
*
* Mirrors the API's determine_status_from_license_details logic.
*
* @since x.x
*
* @return string 'active', 'expired', or 'free'.
*/
public static function get_payment_license_status() {
$version_info = self::get_primary_license_info();
if ( ! $version_info ) {
return 'free';
}
$error = $version_info['error'] ?? array();
if ( is_array( $error ) ) {
$code = $error['code'] ?? '';
if ( 'expired' === $code ) {
return 'expired';
}
if ( 'grandfathered' === $code && isset( $error['expires'] ) && gmdate( 'Y-m-d', $error['expires'] ) < '2016-04-26' ) {
return 'free';
}
}
if ( in_array( self::license_type( true ), array( 'elite', 'business' ), true ) ) {
return 'active';
}
return 'free';
}
/**
* @since 4.0.01
*
* @return bool
*/
public static function is_license_expired() {
$version_info = self::get_primary_license_info();
if ( ! isset( $version_info['error'] ) ) {
return false;
}
$expires = $version_info['error']['expires'] ?? 0;
if ( ! $expires || $expires > time() ) {
return false;
}
$rate_limited = ! empty( $version_info['response_code'] ) && 429 === (int) $version_info['response_code'];
if ( $rate_limited ) {
// Do not return false positives for rate limited responses.
return false;
}
return $version_info['error'];
}
/**
* @since 4.08
* @since 6.7 This is public.
*
* @return array|false
*/
public static function get_primary_license_info() {
$installed_addons = apply_filters( 'frm_installed_addons', array() );
if ( ! $installed_addons || ! isset( $installed_addons['formidable_pro'] ) ) {
return false;
}
$installed_addons = array(
'formidable_pro' => $installed_addons['formidable_pro'],
);
return self::fill_update_addon_info( $installed_addons );
}
/**
* @since 3.04.03
*
* @param mixed $transient
*
* @return object
*/
public static function check_update( $transient ) {
if ( ! FrmAppHelper::pro_is_installed() ) {
// Don't make any changes if only Lite is installed.
return $transient;
}
if ( ! is_object( $transient ) ) {
$transient = new stdClass();
}
$installed_addons = apply_filters( 'frm_installed_addons', array() );
if ( ! $installed_addons ) {
return $transient;
}
$version_info = self::fill_update_addon_info( $installed_addons );
$transient->last_checked = time();
$wp_plugins = self::get_plugins();
foreach ( $version_info as $plugin ) {
$plugin = (object) $plugin;
if ( ! isset( $plugin->new_version ) || ! isset( $plugin->package ) ) {
continue;
}
$folder = $plugin->plugin;
if ( ! $folder ) {
continue;
}
if ( ! self::is_installed( $folder ) ) {
// Don't show an update if the plugin isn't installed
continue;
}
$wp_plugin = $wp_plugins[ $folder ] ?? array();
$wp_version = $wp_plugin['Version'] ?? '1.0';
$plugin->slug = explode( '/', $folder )[0];
if ( version_compare( $wp_version, $plugin->new_version, '<' ) ) {
$transient->response[ $folder ] = $plugin;
} else {
$transient->no_update[ $folder ] = $plugin;
}
$transient->checked[ $folder ] = $wp_version;
}//end foreach
return $transient;
}
/**
* Copy of FrmAppHelper::get_plugins.
* Because this gets called on "pre_set_site_transient_update_plugins" an old version of FrmAppHelper may be loaded on plugin update.
* This means that trying to access FrmAppHelper::get_plugins when upgrading from a Lite version before v5.5 results in a one-off error.
*
* @since 5.5.2
*
* @return array
*/
protected static function get_plugins() {
if ( ! function_exists( 'get_plugins' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return get_plugins();
}
/**
* Check if a plugin is installed before showing an update for it
*
* @since 3.05
*
* @param string $plugin The folder/filename.php for a plugin.
*
* @return bool - True if installed
*/
protected static function is_installed( $plugin ) {
$all_plugins = self::get_plugins();
return isset( $all_plugins[ $plugin ] );
}
/**
* @since 3.04.03
*
* @param array $installed_addons
*
* @return array
*/
protected static function fill_update_addon_info( $installed_addons ) {
$checked_licenses = array();
$version_info = array();
foreach ( $installed_addons as $addon ) {
if ( $addon->store_url !== 'https://formidableforms.com' ) {
// Check if this is a third-party addon
continue;
}
$new_license = $addon->license;
if ( ! $new_license || in_array( $new_license, $checked_licenses, true ) ) {
continue;
}
$checked_licenses[] = $new_license;
$api = new FrmFormApi( $new_license );
if ( ! $version_info ) {
$version_info = $api->get_api_info();
continue;
}
$plugin = $api->get_addon_for_license( $addon, $version_info );
if ( ! $plugin ) {
continue;
}
$download_id = $plugin['id'] ?? 0;
if ( ! $download_id || isset( $version_info[ $download_id ]['package'] ) ) {
continue;
}
// If this addon is using its own license, get the update url
$addon_info = $api->get_api_info();
$version_info[ $download_id ] = $addon_info[ $download_id ];
if ( isset( $addon_info['error'] ) ) {
$version_info[ $download_id ]['error'] = array(
'message' => $addon_info['error']['message'],
'code' => $addon_info['error']['code'],
);
}
}//end foreach
return $version_info;
}
/**
* Get the action link for an addon that isn't active.
*
* @since 3.06.03
*
* @param string $plugin The plugin slug.
*
* @return array
*/
public static function install_link( $plugin ) {
$link = array();
$addon = self::get_addon( $plugin );
if ( $addon ) {
if ( $addon['status']['type'] === 'installed' && ! empty( $addon['activate_url'] ) ) {
$link = array(
'url' => $addon['plugin'],
'class' => 'frm-activate-addon',
);
} elseif ( ! empty( $addon['url'] ) ) {
$link = array(
'url' => $addon['url'],
'class' => 'frm-install-addon',
);
} elseif ( ! empty( $addon['categories'] ) ) {
$link = array(
'categories' => $addon['categories'],
);
}
if ( $link ) {
$link['status'] = $addon['status']['type'];
}
} elseif ( current_user_can( 'activate_plugins' ) && self::is_installed( 'formidable-' . $plugin . '/formidable-' . $plugin . '.php' ) ) {
$link = array(
'url' => 'formidable-' . $plugin . '/formidable-' . $plugin . '.php',
'class' => 'frm-activate-addon',
);
}//end if
return $link;
}
/**
* Get the JSON-encoded install data for a plugin update.
*
* @since 6.29
*
* @param string $addon_slug The addon slug (e.g. 'pro', 'dates').
*
* @return string JSON-encoded install data, or empty string if no URL is available.
*/
public static function get_update_install_data( $addon_slug ) {
$upgrading = self::install_link( $addon_slug );
if ( isset( $upgrading['class'] ) && 'frm-install-addon' === $upgrading['class'] ) {
return (string) json_encode( $upgrading );
}
if ( 'pro' === $addon_slug ) {
$download_url = self::get_pro_download_url();
$plugin_file = 'formidable-pro/formidable-pro.php';
} else {
$addon_data = self::get_addon( $addon_slug );
$download_url = $addon_data && ! empty( $addon_data['url'] ) ? $addon_data['url'] : '';
$plugin_file = $addon_data && ! empty( $addon_data['plugin'] ) ? $addon_data['plugin'] : 'formidable-' . $addon_slug . '/formidable-' . $addon_slug . '.php';
}
if ( ! $download_url ) {
$update_plugins = get_site_transient( 'update_plugins' );
$plugin_update = $update_plugins->response[ $plugin_file ] ?? null;
$download_url = $plugin_update && ! empty( $plugin_update->package ) ? $plugin_update->package : '';
}
return $download_url ? (string) json_encode(
array(
'url' => $download_url,
'class' => 'frm-install-addon',
)
) : '';
}
/**
* @since 4.09
*
* @param string $plugin The plugin slug.
*
* @return array|false
*/
public static function get_addon( $plugin ) {
$addons = self::get_api_addons();
self::prepare_addons( $addons );
foreach ( $addons as $addon ) {
$slug = explode( '/', $addon['plugin'] );
if ( $slug[0] === 'formidable-' . $plugin ) {
return $addon;
}
}
return false;
}
/**
* @since 4.09
*
* @return string
*/
protected static function get_license_type() {
$license_type = '';
$addons = self::get_api_addons();
if ( isset( $addons['error'] ) && isset( $addons['error']['type'] ) ) {
return $addons['error']['type'];
}
return $license_type;
}
/**
* @since 3.04.03
*
* @param array $addons
* @param object $license The FrmAddon object.
*
* @return array
*/
public static function get_addon_for_license( $addons, $license ) {
$download_id = $license->download_id;
$plugin = array();
if ( ! $download_id && $addons ) {
foreach ( $addons as $addon ) {
if ( 0 === strcasecmp( $license->plugin_name, $addon['title'] ) ) {
return $addon;
}
}
} elseif ( isset( $addons[ $download_id ] ) ) {
$plugin = $addons[ $download_id ];
}
return $plugin;
}
/**
* @param array $addons
*
* @return void
*/
protected static function prepare_addons( &$addons ) {
// Reset categories to prevent count accumulation across multiple calls.
self::$categories = array();
$activate_url = '';
if ( current_user_can( 'activate_plugins' ) ) {
$activate_url = add_query_arg( array( 'action' => 'activate' ), admin_url( 'plugins.php' ) );
}
$loop_addons = $addons;
foreach ( $loop_addons as $id => $addon ) {
if ( is_numeric( $id ) ) {
$slug = str_replace( array( '-wordpress-plugin', '-wordpress' ), '', $addon['slug'] );
$file_name = $addon['plugin'];
} else {
$slug = $id;
$base_file = $addon['file'] ?? 'formidable-' . $slug;
$file_name = $base_file . '/' . $base_file . '.php';
if ( ! isset( $addon['plugin'] ) ) {
$addon['plugin'] = $file_name;
}
}
$addon['installed'] = self::is_installed( $file_name );
if ( 'highrise' === $slug && ! $addon['installed'] ) {
unset( $addons[ $id ] );
continue;
}
if ( $addon['installed'] && 'formidable-views/formidable-views.php' === $file_name ) {
$active_views_version = self::get_active_views_version();
if ( false !== $active_views_version && $slug !== $active_views_version ) {
$addon['installed'] = false;
}
}
$addon['activate_url'] = '';
if ( $addon['installed'] && $activate_url && ! self::is_plugin_active( $file_name, $slug ) ) {
$addon['activate_url'] = add_query_arg(
array(
'_wpnonce' => wp_create_nonce( 'activate-plugin_' . $file_name ),
'plugin' => $file_name,
),
$activate_url
);
}
if ( ! isset( $addon['docs'] ) ) {
$addon['docs'] = 'knowledgebase/formidable-' . $slug . '/';
}
self::prepare_addon_link( $addon['docs'] );
if ( ! isset( $addon['link'] ) ) {
$addon['link'] = 'downloads/' . $slug . '/';
}
self::prepare_addon_link( $addon['link'] );
self::set_addon_status( $addon );
self::set_categories( $addon );
$addons[ $id ] = $addon;
}//end foreach
}
/**
* @param string $file_name
* @param string $slug
*
* @return bool
*/
private static function is_plugin_active( $file_name, $slug ) {
if ( 'formidable-views/formidable-views.php' === $file_name ) {
return self::get_active_views_version() === $slug;
}
return is_plugin_active( $file_name );
}
/**
* @return false|string either 'visual-views' or 'views', false if one is not found.
*/
private static function get_active_views_version() {
if ( ! is_callable( 'FrmViewsAppHelper::plugin_version' ) ) {
return false;
}
$plugin_version = FrmViewsAppHelper::plugin_version();
return version_compare( $plugin_version, '5.0', '>=' ) ? 'visual-views' : 'views';
}
/**
* @since 3.04.02
*
* @param string $link
*
* @return void
*/
protected static function prepare_addon_link( &$link ) {
$site_url = 'https://formidableforms.com/';
if ( ! str_starts_with( $link, 'http' ) ) {
$link = $site_url . $link;
}
$link = FrmAppHelper::make_affiliate_url( $link );
$utm = array(
'campaign' => 'addons',
);
$link = FrmAppHelper::maybe_add_missing_utm( $link, $utm );
}
/**
* Add the status to the addon array. Status options are:
* installed, active, not installed
*
* @since 3.04.02
*
* @param array $addon
*
* @return void