PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 4.07.01
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v4.07.01
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / controllers / FrmAddonsController.php

FrmAddonsController.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 4.07.01, at classes/controllers/FrmAddonsController.php

1,012 lines 27.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmAddonsController {
7
8 public static function menu() {
9 if ( ! current_user_can( 'activate_plugins' ) ) {
10 return;
11 }
12
13 $label = __( 'Add-Ons', 'formidable' );
14 if ( FrmAppHelper::pro_is_installed() ) {
15 $label = '<span style="color:#fe5a1d">' . $label . '</span>';
16 }
17 add_submenu_page( 'formidable', 'Formidable | ' . __( 'Add-Ons', 'formidable' ), $label, 'frm_view_forms', 'formidable-addons', 'FrmAddonsController::list_addons' );
18
19 if ( ! FrmAppHelper::pro_is_installed() ) {
20 add_submenu_page(
21 'formidable',
22 'Formidable | ' . __( 'Upgrade', 'formidable' ),
23 '<span style="color:#fe5a1d">' . __( 'Upgrade', 'formidable' ) . '</span>',
24 'frm_view_forms',
25 'formidable-pro-upgrade',
26 'FrmAddonsController::upgrade_to_pro'
27 );
28 }
29 }
30
31 public static function list_addons() {
32 FrmAppHelper::include_svg();
33 $installed_addons = apply_filters( 'frm_installed_addons', array() );
34 $license_type = '';
35
36 $addons = self::get_api_addons();
37 $errors = array();
38
39 if ( isset( $addons['error'] ) ) {
40 $api = new FrmFormApi();
41 $errors = $api->get_error_from_response( $addons );
42 $license_type = isset( $addons['error']['type'] ) ? $addons['error']['type'] : '';
43 unset( $addons['error'] );
44 }
45 self::prepare_addons( $addons );
46
47 $pricing = FrmAppHelper::admin_upgrade_link( 'addons' );
48
49 include( FrmAppHelper::plugin_path() . '/classes/views/addons/list.php' );
50 }
51
52 public static function license_settings() {
53 $plugins = apply_filters( 'frm_installed_addons', array() );
54 if ( empty( $plugins ) ) {
55 esc_html_e( 'There are no plugins on your site that require a license', 'formidable' );
56
57 return;
58 }
59
60 ksort( $plugins );
61
62 include( FrmAppHelper::plugin_path() . '/classes/views/addons/settings.php' );
63 }
64
65 private static function get_api_addons() {
66 $api = new FrmFormApi();
67 $addons = $api->get_api_info();
68
69 if ( empty( $addons ) ) {
70 $addons = self::fallback_plugin_list();
71 } else {
72 foreach ( $addons as $k => $addon ) {
73 if ( empty( $addon['excerpt'] ) && $k !== 'error' ) {
74 unset( $addons[ $k ] );
75 }
76 }
77 }
78
79 return $addons;
80 }
81
82 /**
83 * If the API is unable to connect, show something on the addons page
84 *
85 * @since 3.04.03
86 * @return array
87 */
88 private static function fallback_plugin_list() {
89 return array(
90 'formidable-pro' => array(
91 'title' => 'Formidable Forms',
92 'link' => 'pricing/',
93 'docs' => '',
94 'excerpt' => 'Enhance your basic Formidable forms with a plethora of Pro field types and features. Create advanced forms and data-driven applications in minutes.',
95 ),
96 'mailchimp' => array(
97 'title' => 'MailChimp Forms',
98 '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.',
99 ),
100 'registration' => array(
101 'title' => 'User Registration Forms',
102 'link' => 'downloads/user-registration/',
103 '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.',
104 ),
105 'paypal' => array(
106 'title' => 'PayPal Standard Forms',
107 'link' => 'downloads/paypal-standard/',
108 '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.',
109 ),
110 'stripe' => array(
111 'title' => 'Stripe Forms',
112 'docs' => 'knowledgebase/stripe/',
113 'excerpt' => 'Any Formidable forms on your site can accept credit card payments without users ever leaving your site.',
114 ),
115 'authorize-net' => array(
116 'title' => 'Authorize.net AIM Forms',
117 'link' => 'downloads/authorize-net-aim/',
118 'docs' => 'knowledgebase/authorize-net-aim/',
119 'excerpt' => 'Accept one-time payments directly on your site, using Authorize.net AIM.',
120 ),
121 'woocommerce' => array(
122 'title' => 'WooCommerce Forms',
123 'excerpt' => 'Use a Formidable form on your WooCommerce product pages.',
124 ),
125 'autoresponder' => array(
126 'title' => 'Form Action Automation',
127 'docs' => 'knowledgebase/schedule-autoresponder/',
128 'excerpt' => 'Schedule email notifications, SMS messages, and API actions.',
129 ),
130 'modal' => array(
131 'title' => 'Bootstrap Modal Forms',
132 'link' => 'downloads/bootstrap-modal/',
133 'docs' => 'knowledgebase/bootstrap-modal/',
134 'excerpt' => 'Open a view or form in a Bootstrap popup.',
135 ),
136 'bootstrap' => array(
137 'title' => 'Bootstrap Style Forms',
138 'excerpt' => 'Instantly add Bootstrap styling to all your Formidable forms.',
139 ),
140 'zapier' => array(
141 'title' => 'Zapier Forms',
142 '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.',
143 ),
144 'signature' => array(
145 'title' => 'Digital Signature Forms',
146 'excerpt' => 'Add a signature field to your form. The user may write their signature with a trackpad/mouse or just type it.',
147 ),
148 'api' => array(
149 'title' => 'Formidable Forms API',
150 'link' => 'downloads/formidable-api/',
151 '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.',
152 ),
153 'twilio' => array(
154 'title' => 'Twilio SMS Forms',
155 'docs' => 'knowledgebase/twilio-add-on/',
156 'excerpt' => 'Allow users to text their votes for polls created by Formidable Forms, or send SMS notifications when entries are submitted or updated.',
157 ),
158 );
159 }
160
161 /**
162 * If Pro is missing but has been authenticated, include a download URL
163 *
164 * @since 3.04.03
165 * @return string
166 */
167 public static function get_pro_download_url() {
168 $pro_cred_store = 'frmpro-credentials';
169 $pro_wpmu_store = 'frmpro-wpmu-sitewide';
170 if ( is_multisite() && get_site_option( $pro_wpmu_store ) ) {
171 $creds = get_site_option( $pro_cred_store );
172 } else {
173 $creds = get_option( $pro_cred_store );
174 }
175
176 if ( empty( $creds ) || ! is_array( $creds ) || ! isset( $creds['license'] ) ) {
177 return '';
178 }
179
180 $license = $creds['license'];
181 if ( empty( $license ) ) {
182 return '';
183 }
184
185 if ( strpos( $license, '-' ) ) {
186 // this is a fix for licenses saved in the past
187 $license = strtoupper( $license );
188 }
189
190 $api = new FrmFormApi( $license );
191 $downloads = $api->get_api_info();
192 $pro = isset( $downloads['93790'] ) ? $downloads['93790'] : array();
193
194 return isset( $pro['url'] ) ? $pro['url'] : '';
195 }
196
197 /**
198 * @since 4.06
199 */
200 public static function license_type() {
201 $api = new FrmFormApi();
202 $addons = $api->get_api_info();
203 $type = 'free';
204
205 if ( isset( $addons['error'] ) ) {
206 if ( isset( $addons['error']['code'] ) && $addons['error']['code'] === 'expired' ) {
207 return $addons['error']['code'];
208 }
209 $type = isset( $addons['error']['type'] ) ? $addons['error']['type'] : $type;
210 }
211
212 $pro = isset( $addons['93790'] ) ? $addons['93790'] : array();
213 if ( $type === 'free' ) {
214 $type = isset( $pro['type'] ) ? $pro['type'] : $type;
215 if ( $type === 'free' ) {
216 return $type;
217 }
218 }
219
220 if ( isset( $pro['code'] ) && $pro['code'] === 'grandfathered' ) {
221 return $pro['code'];
222 }
223
224 $expires = isset( $pro['expires'] ) ? $pro['expires'] : '';
225 $expired = $expires ? $expires < time() : false;
226 return $expired ? 'expired' : strtolower( $type );
227 }
228
229 /**
230 * @since 4.0.01
231 */
232 public static function is_license_expired() {
233 $installed_addons = apply_filters( 'frm_installed_addons', array() );
234 if ( empty( $installed_addons ) || ! isset( $installed_addons['formidable_pro'] ) ) {
235 return false;
236 }
237 $installed_addons = array(
238 'formidable_pro' => $installed_addons['formidable_pro'],
239 );
240
241 $version_info = self::fill_update_addon_info( $installed_addons );
242 if ( ! isset( $version_info['error'] ) ) {
243 return false;
244 }
245
246 return $version_info['error'];
247 }
248
249 /**
250 * @since 3.04.03
251 */
252 public static function check_update( $transient ) {
253 if ( ! is_object( $transient ) ) {
254 $transient = new stdClass();
255 }
256
257 $installed_addons = apply_filters( 'frm_installed_addons', array() );
258 if ( empty( $installed_addons ) ) {
259 return $transient;
260 }
261
262 $version_info = self::fill_update_addon_info( $installed_addons );
263
264 $transient->last_checked = time();
265
266 if ( ! function_exists( 'get_plugins' ) ) {
267 require_once ABSPATH . 'wp-admin/includes/plugin.php';
268 }
269 $wp_plugins = get_plugins();
270
271 foreach ( $version_info as $id => $plugin ) {
272 $plugin = (object) $plugin;
273
274 if ( ! isset( $plugin->new_version ) || ! isset( $plugin->package ) ) {
275 continue;
276 }
277
278 $folder = $plugin->plugin;
279 if ( empty( $folder ) ) {
280 continue;
281 }
282
283 if ( ! self::is_installed( $folder ) ) {
284 // don't show an update if the plugin isn't installed
285 continue;
286 }
287
288 $wp_plugin = isset( $wp_plugins[ $folder ] ) ? $wp_plugins[ $folder ] : array();
289 $wp_version = isset( $wp_plugin['Version'] ) ? $wp_plugin['Version'] : '1.0';
290
291 if ( version_compare( $wp_version, $plugin->new_version, '<' ) ) {
292 $slug = explode( '/', $folder );
293 $plugin->slug = $slug[0];
294 $transient->response[ $folder ] = $plugin;
295 }
296
297 $transient->checked[ $folder ] = $wp_version;
298
299 }
300
301 return $transient;
302 }
303
304 /**
305 * Check if a plugin is installed before showing an update for it
306 *
307 * @since 3.05
308 *
309 * @param string $plugin - the folder/filename.php for a plugin
310 *
311 * @return bool - True if installed
312 */
313 private static function is_installed( $plugin ) {
314 if ( ! function_exists( 'get_plugins' ) ) {
315 require_once ABSPATH . 'wp-admin/includes/plugin.php';
316 }
317
318 $all_plugins = get_plugins();
319
320 return isset( $all_plugins[ $plugin ] );
321 }
322
323 /**
324 * @since 3.04.03
325 *
326 * @param array $installed_addons
327 *
328 * @return array
329 */
330 private static function fill_update_addon_info( $installed_addons ) {
331 $checked_licenses = array();
332 $version_info = array();
333
334 foreach ( $installed_addons as $addon ) {
335 if ( $addon->store_url !== 'https://formidableforms.com' ) {
336 // check if this is a third-party addon
337 continue;
338 }
339
340 $new_license = $addon->license;
341 if ( empty( $new_license ) || in_array( $new_license, $checked_licenses ) ) {
342 continue;
343 }
344
345 $checked_licenses[] = $new_license;
346
347 $api = new FrmFormApi( $new_license );
348 if ( empty( $version_info ) ) {
349 $version_info = $api->get_api_info();
350 continue;
351 }
352
353 $plugin = $api->get_addon_for_license( $addon, $version_info );
354 if ( empty( $plugin ) ) {
355 continue;
356 }
357
358 $download_id = isset( $plugin['id'] ) ? $plugin['id'] : 0;
359 if ( ! empty( $download_id ) && ! isset( $version_info[ $download_id ]['package'] ) ) {
360 // if this addon is using its own license, get the update url
361 $addon_info = $api->get_api_info();
362
363 $version_info[ $download_id ] = $addon_info[ $download_id ];
364 if ( isset( $addon_info['error'] ) ) {
365 $version_info[ $download_id ]['error'] = array(
366 'message' => $addon_info['error']['message'],
367 'code' => $addon_info['error']['code'],
368 );
369 }
370 }
371 }
372
373 return $version_info;
374 }
375
376 /**
377 * Get the action link for an addon that isn't active.
378 *
379 * @since 3.06.03
380 * @param string $addon The plugin slug
381 * @return array
382 */
383 public static function install_link( $plugin ) {
384 $link = array();
385 $addons = self::get_api_addons();
386 self::prepare_addons( $addons );
387
388 foreach ( $addons as $addon ) {
389 $slug = explode( '/', $addon['plugin'] );
390 if ( $slug[0] !== 'formidable-' . $plugin ) {
391 continue;
392 }
393
394 if ( $addon['status']['type'] === 'installed' && ! empty( $addon['activate_url'] ) ) {
395 $link = array(
396 'url' => $addon['plugin'],
397 'class' => 'frm-activate-addon',
398 );
399 } elseif ( isset( $addon['url'] ) && ! empty( $addon['url'] ) ) {
400 $link = array(
401 'url' => $addon['url'],
402 'class' => 'frm-install-addon',
403 );
404 } elseif ( isset( $addon['categories'] ) && ! empty( $addon['categories'] ) ) {
405 $link = array(
406 'categories' => $addon['categories'],
407 );
408 }
409
410 if ( ! empty( $link ) ) {
411 $link['status'] = $addon['status']['type'];
412 }
413
414 return $link;
415 }
416 }
417
418 /**
419 * @since 3.04.03
420 *
421 * @param array $addons
422 * @param object $license The FrmAddon object
423 *
424 * @return array
425 */
426 public static function get_addon_for_license( $addons, $license ) {
427 $download_id = $license->download_id;
428 $plugin = array();
429 if ( empty( $download_id ) && ! empty( $addons ) ) {
430 foreach ( $addons as $addon ) {
431 if ( strtolower( $license->plugin_name ) == strtolower( $addon['title'] ) ) {
432 return $addon;
433 }
434 }
435 } elseif ( isset( $addons[ $download_id ] ) ) {
436 $plugin = $addons[ $download_id ];
437 }
438
439 return $plugin;
440 }
441
442 private static function prepare_addons( &$addons ) {
443 $activate_url = '';
444 if ( current_user_can( 'activate_plugins' ) ) {
445 $activate_url = add_query_arg( array( 'action' => 'activate' ), admin_url( 'plugins.php' ) );
446 }
447
448 $loop_addons = $addons;
449 foreach ( $loop_addons as $id => $addon ) {
450 if ( is_numeric( $id ) ) {
451 $slug = str_replace( array( '-wordpress-plugin', '-wordpress' ), '', $addon['slug'] );
452 $file_name = $addon['plugin'];
453 } else {
454 $slug = $id;
455 if ( isset( $addon['file'] ) ) {
456 $base_file = $addon['file'];
457 } else {
458 $base_file = 'formidable-' . $slug;
459 }
460 $file_name = $base_file . '/' . $base_file . '.php';
461 }
462
463 $addon['installed'] = self::is_installed( $file_name );
464 $addon['activate_url'] = '';
465
466 if ( $addon['installed'] && ! empty( $activate_url ) && ! is_plugin_active( $file_name ) ) {
467 $addon['activate_url'] = add_query_arg(
468 array(
469 '_wpnonce' => wp_create_nonce( 'activate-plugin_' . $file_name ),
470 'plugin' => $file_name,
471 ),
472 $activate_url
473 );
474 }
475
476 if ( ! isset( $addon['docs'] ) ) {
477 $addon['docs'] = 'knowledgebase/formidable-' . $slug . '/';
478 }
479 self::prepare_addon_link( $addon['docs'] );
480
481 if ( ! isset( $addon['link'] ) ) {
482 $addon['link'] = 'downloads/' . $slug . '/';
483 }
484 self::prepare_addon_link( $addon['link'] );
485
486 self::set_addon_status( $addon );
487 $addons[ $id ] = $addon;
488 }
489 }
490
491 /**
492 * @since 3.04.02
493 */
494 private static function prepare_addon_link( &$link ) {
495 $site_url = 'https://formidableforms.com/';
496 if ( strpos( $link, 'http' ) !== 0 ) {
497 $link = $site_url . $link;
498 }
499 $link = FrmAppHelper::make_affiliate_url( $link );
500 $query_args = array(
501 'utm_source' => 'WordPress',
502 'utm_medium' => 'addons',
503 'utm_campaign' => 'liteplugin',
504 );
505 $link = add_query_arg( $query_args, $link );
506 }
507
508 /**
509 * Add the status to the addon array. Status options are:
510 * installed, active, not installed
511 *
512 * @since 3.04.02
513 */
514 private static function set_addon_status( &$addon ) {
515 if ( ! empty( $addon['activate_url'] ) ) {
516 $addon['status'] = array(
517 'type' => 'installed',
518 'label' => __( 'Installed', 'formidable' ),
519 );
520 } elseif ( $addon['installed'] ) {
521 $addon['status'] = array(
522 'type' => 'active',
523 'label' => __( 'Active', 'formidable' ),
524 );
525 } else {
526 $addon['status'] = array(
527 'type' => 'not-installed',
528 'label' => __( 'Not Installed', 'formidable' ),
529 );
530 }
531 }
532
533 public static function upgrade_to_pro() {
534 FrmAppHelper::include_svg();
535
536 $link_parts = array(
537 'medium' => 'upgrade',
538 'content' => 'button',
539 );
540
541 $features = array(
542 'Display Entries' => array(
543 array(
544 'label' => 'Display form data with virtually limitless views',
545 'link' => array(
546 'content' => 'views',
547 'param' => 'views-display-form-data',
548 ),
549 'lite' => false,
550 ),
551 array(
552 'label' => 'Generate graphs and stats based on your submitted data',
553 'link' => array(
554 'content' => 'graphs',
555 'param' => 'statistics-graphs-wordpress-forms',
556 ),
557 'lite' => false,
558 ),
559 ),
560 'Entry Management' => array(
561 array(
562 'label' => 'Import entries from a CSV',
563 'link' => array(
564 'content' => 'import-entries',
565 'param' => 'importing-exporting-wordpress-forms',
566 ),
567 'lite' => false,
568 ),
569 array(
570 'label' => 'Logged-in users can save drafts and return later',
571 'link' => array(
572 'content' => 'save-drafts',
573 'param' => 'save-drafts-wordpress-form',
574 ),
575 'lite' => false,
576 ),
577 array(
578 'label' => 'Flexibly and powerfully view, edit, and delete entries from anywhere on your site',
579 'link' => array(
580 'content' => 'front-edit',
581 'param' => 'wordpress-front-end-editing',
582 ),
583 'lite' => false,
584 ),
585 array(
586 'label' => 'View form submissions from the back-end',
587 'lite' => true,
588 ),
589 array(
590 'label' => 'Export your entries to a CSV',
591 'lite' => true,
592 ),
593 ),
594 'Form Building' => array(
595 array(
596 'label' => 'Save a calculated value into a field',
597 'link' => array(
598 'content' => 'calculations',
599 'param' => 'field-calculations-wordpress-form',
600 ),
601 'lite' => false,
602 ),
603 array(
604 'label' => 'Allow multiple file uploads',
605 'link' => array(
606 'content' => 'file-uploads',
607 'param' => 'wordpress-multi-file-upload-fields',
608 ),
609 'lite' => false,
610 ),
611 array(
612 'label' => 'Repeat sections of fields',
613 'link' => array(
614 'content' => 'repeaters',
615 'param' => 'repeatable-sections-forms',
616 ),
617 'lite' => false,
618 ),
619 array(
620 'label' => 'Hide and show fields conditionally based on other fields or the user\'s role',
621 'link' => array(
622 'content' => 'conditional-logic',
623 'param' => 'conditional-logic-wordpress-forms',
624 ),
625 'lite' => false,
626 ),
627 array(
628 'label' => 'Confirmation fields',
629 'link' => array(
630 'content' => 'confirmation-fields',
631 'param' => 'confirmation-fields-wordpress-forms',
632 ),
633 'lite' => false,
634 ),
635 array(
636 'label' => 'Multi-paged forms',
637 'link' => array(
638 'content' => 'page-breaks',
639 'param' => 'wordpress-multi-page-forms',
640 ),
641 'lite' => false,
642 ),
643 array(
644 'label' => 'Include section headings, page breaks, rich text, dates, times, scales, star ratings, sliders, toggles, dynamic fields populated from other forms, passwords, and tags in advanced forms.',
645 'lite' => false,
646 ),
647 array(
648 'label' => 'Include text, email, url, paragraph text, radio, checkbox, dropdown fields, hidden fields, user ID fields, and HTML blocks in your form.',
649 'lite' => true,
650 ),
651 array(
652 'label' => 'Drag & Drop Form building',
653 'link' => array(
654 'content' => 'drag-drop',
655 'param' => 'drag-drop-forms',
656 ),
657 'lite' => true,
658 ),
659 array(
660 'label' => 'Create forms from Templates',
661 'link' => array(
662 'content' => 'form-templates',
663 'param' => 'wordpress-form-templates',
664 ),
665 'lite' => true,
666 ),
667 array(
668 'label' => 'Import and export forms with XML',
669 'link' => array(
670 'content' => 'import',
671 'param' => 'importing-exporting-wordpress-forms',
672 ),
673 'lite' => true,
674 ),
675 array(
676 'label' => 'Use input placeholder text in your fields that clear when typing starts.',
677 'lite' => true,
678 ),
679 ),
680 'Form Actions' => array(
681 array(
682 'label' => 'Conditionally send your email notifications based on values in your form',
683 'link' => array(
684 'content' => 'conditional-emails',
685 ),
686 'lite' => false,
687 ),
688 array(
689 'label' => 'Create and edit WordPress posts or custom posts from the front-end',
690 'link' => array(
691 'content' => 'create-posts',
692 'param' => 'create-posts-pages-wordpress-forms',
693 ),
694 'lite' => false,
695 ),
696 array(
697 'label' => 'Send multiple emails and autoresponders',
698 'link' => array(
699 'content' => 'multiple-emails',
700 'param' => 'virtually-unlimited-emails',
701 ),
702 'lite' => true,
703 ),
704 ),
705 'Form Appearance' => array(
706 array(
707 'label' => 'Create Multiple styles for different forms',
708 'link' => array(
709 'content' => 'multiple-styles',
710 'param' => 'wordpress-visual-form-styler',
711 ),
712 'lite' => false,
713 ),
714 array(
715 'label' => 'Customizable layout with CSS classes',
716 'link' => array(
717 'content' => 'form-layout',
718 'param' => 'wordpress-mobile-friendly-forms',
719 ),
720 'lite' => true,
721 ),
722 array(
723 'label' => 'Customize the HTML for your forms',
724 'link' => array(
725 'content' => 'custom-html',
726 'param' => 'customizable-html-wordpress-form',
727 ),
728 'lite' => true,
729 ),
730 array(
731 'label' => 'Style your form with the Visual Form Styler',
732 'lite' => true,
733 ),
734 ),
735 );
736
737 include( FrmAppHelper::plugin_path() . '/classes/views/addons/upgrade_to_pro.php' );
738 }
739
740 /**
741 * Install Pro after connection with Formidable.
742 *
743 * @since 4.02.05
744 */
745 public static function connect_pro() {
746 FrmAppHelper::permission_check( 'install_plugins' );
747 check_ajax_referer( 'frm_ajax', 'nonce' );
748
749 $url = FrmAppHelper::get_post_param( 'plugin', '', 'sanitize_text_field' );
750 if ( FrmAppHelper::pro_is_installed() || empty( $url ) ) {
751 wp_die();
752 }
753
754 $response = array();
755
756 // It's already installed and active.
757 $active = activate_plugin( 'formidable-pro/formidable-pro.php', false, false, true );
758 if ( is_wp_error( $active ) ) {
759 // The plugin was installed, but not active. Download it now.
760 self::ajax_install_addon();
761 } else {
762 $response['active'] = true;
763 }
764
765 echo json_encode( $response );
766 wp_die();
767 }
768
769 /**
770 * @since 3.04.02
771 */
772 public static function ajax_install_addon() {
773
774 self::install_addon_permissions();
775
776 // Set the current screen to avoid undefined notices.
777 global $hook_suffix;
778 set_current_screen();
779
780 self::maybe_show_cred_form();
781
782 $installed = self::install_addon();
783 self::maybe_activate_addon( $installed );
784
785 // Send back a response.
786 echo json_encode( __( 'Your plugin has been installed. Please reload the page to see more options.', 'formidable' ) );
787 wp_die();
788 }
789
790 /**
791 * @since 3.04.02
792 */
793 private static function maybe_show_cred_form() {
794 // Start output bufferring to catch the filesystem form if credentials are needed.
795 ob_start();
796
797 $show_form = false;
798 $method = '';
799 $url = add_query_arg( array( 'page' => 'formidable-settings' ), admin_url( 'admin.php' ) );
800 $url = esc_url_raw( $url );
801 $creds = request_filesystem_credentials( $url, $method, false, false, null );
802
803 if ( false === $creds ) {
804 $show_form = true;
805 } elseif ( ! WP_Filesystem( $creds ) ) {
806 request_filesystem_credentials( $url, $method, true, false, null );
807 $show_form = true;
808 }
809
810 if ( $show_form ) {
811 //$form = ob_get_clean();
812 //TODO: test this: echo json_encode( array( 'form' => $form ) );
813 echo json_encode( array( 'form' => __( 'Sorry, you\'re site requires FTP authentication. Please install plugins manaully.', 'formidable' ) ) );
814 wp_die();
815 }
816
817 ob_end_clean();
818 }
819
820 /**
821 * We do not need any extra credentials if we have gotten this far,
822 * so let's install the plugin.
823 *
824 * @since 3.04.02
825 */
826 private static function install_addon() {
827 require_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
828
829 $download_url = FrmAppHelper::get_param( 'plugin', '', 'post', 'esc_url_raw' );
830
831 // Create the plugin upgrader with our custom skin.
832 $installer = new Plugin_Upgrader( new FrmInstallerSkin() );
833 $installer->install( $download_url );
834
835 // Flush the cache and return the newly installed plugin basename.
836 wp_cache_flush();
837
838 return $installer->plugin_info();
839 }
840
841 /**
842 * @since 3.06.03
843 */
844 public static function ajax_activate_addon() {
845
846 self::install_addon_permissions();
847
848 // Set the current screen to avoid undefined notices.
849 global $hook_suffix;
850 set_current_screen();
851
852 $plugin = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' );
853 self::maybe_activate_addon( $plugin );
854
855 // Send back a response.
856 echo json_encode( __( 'Your plugin has been activated. Please reload the page to see more options.', 'formidable' ) );
857 wp_die();
858 }
859
860 /**
861 * @since 4.06.02
862 */
863 public static function ajax_multiple_addons() {
864 self::install_addon_permissions();
865
866 // Set the current screen to avoid undefined notices.
867 global $hook_suffix;
868 set_current_screen();
869
870 $download_urls = FrmAppHelper::get_param( 'plugin', '', 'post' );
871 $download_urls = explode( ',', $download_urls );
872 FrmAppHelper::sanitize_value( 'esc_url_raw', $download_urls );
873
874 foreach ( $download_urls as $download_url ) {
875 $_POST['plugin'] = $download_url;
876 if ( strpos( $download_url, 'http' ) !== false ) {
877 // Installing.
878 self::maybe_show_cred_form();
879
880 $installed = self::install_addon();
881 self::maybe_activate_addon( $installed );
882 } else {
883 // Activating.
884 self::maybe_activate_addon( $download_url );
885 }
886 }
887
888 echo json_encode( __( 'Your plugins have been installed and activated.', 'formidable' ) );
889
890 wp_die();
891 }
892
893 /**
894 * @since 3.04.02
895 * @param string $installed The plugin folder name with file name
896 */
897 private static function maybe_activate_addon( $installed ) {
898 if ( ! $installed ) {
899 return;
900 }
901
902 $activate = activate_plugin( $installed );
903 if ( is_wp_error( $activate ) ) {
904 // Ignore the invalid header message that shows with nested plugins.
905 if ( $activate->get_error_code() !== 'no_plugin_header' ) {
906 echo json_encode( array( 'error' => $activate->get_error_message() ) );
907 wp_die();
908 }
909 }
910 }
911
912 /**
913 * Run security checks before installing
914 *
915 * @since 3.04.02
916 */
917 private static function install_addon_permissions() {
918 check_ajax_referer( 'frm_ajax', 'nonce' );
919
920 if ( ! current_user_can( 'activate_plugins' ) || ! isset( $_POST['plugin'] ) ) {
921 echo json_encode( true );
922 wp_die();
923 }
924 }
925
926 /**
927 * @since 3.04.03
928 * @deprecated 3.06
929 * @codeCoverageIgnore
930 * @return array
931 */
932 public static function error_for_license( $license ) {
933 return FrmDeprecated::error_for_license( $license );
934 }
935
936 /**
937 * @since 3.04.03
938 * @deprecated 3.06
939 * @codeCoverageIgnore
940 */
941 public static function get_pro_updater() {
942 return FrmDeprecated::get_pro_updater();
943 }
944
945 /**
946 * @since 3.04.03
947 * @deprecated 3.06
948 * @codeCoverageIgnore
949 *
950 * @return array
951 */
952 public static function get_addon_info( $license = '' ) {
953 return FrmDeprecated::get_addon_info( $license );
954 }
955
956 /**
957 * @since 3.04.03
958 * @deprecated 3.06
959 * @codeCoverageIgnore
960 *
961 * @return string
962 */
963 public static function get_cache_key( $license ) {
964 return FrmDeprecated::get_cache_key( $license );
965 }
966
967 /**
968 * @since 3.04.03
969 * @deprecated 3.06
970 * @codeCoverageIgnore
971 */
972 public static function reset_cached_addons( $license = '' ) {
973 FrmDeprecated::reset_cached_addons( $license );
974 }
975
976 /**
977 * @since 2.03.08
978 * @deprecated 3.04.03
979 * @codeCoverageIgnore
980 *
981 * @param boolean $return
982 * @param string $package
983 *
984 * @return boolean
985 */
986 public static function add_shorten_edd_filename_filter( $return, $package ) {
987 return FrmDeprecated::add_shorten_edd_filename_filter( $return, $package );
988 }
989
990 /**
991 * @since 2.03.08
992 * @deprecated 3.04.03
993 * @codeCoverageIgnore
994 *
995 * @param string $filename
996 * @param string $ext
997 *
998 * @return string
999 */
1000 public static function shorten_edd_filename( $filename, $ext ) {
1001 return FrmDeprecated::shorten_edd_filename( $filename, $ext );
1002 }
1003
1004 /**
1005 * @deprecated 3.04.03
1006 * @codeCoverageIgnore
1007 */
1008 public static function get_licenses() {
1009 FrmDeprecated::get_licenses();
1010 }
1011 }
1012