PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.06
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 / FrmFormActionsController.php

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

613 lines 17.3 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 FrmFormActionsController {
7 public static $action_post_type = 'frm_form_actions';
8 public static $registered_actions;
9
10 /**
11 * Variables saved in the post:
12 * post_content: json settings
13 * menu_order: form id
14 * post_excerpt: action type
15 */
16 public static function register_post_types() {
17 register_post_type(
18 self::$action_post_type,
19 array(
20 'label' => __( 'Form Actions', 'formidable' ),
21 'description' => '',
22 'public' => false,
23 'show_ui' => false,
24 'exclude_from_search' => true,
25 'show_in_nav_menus' => false,
26 'show_in_menu' => true,
27 'capability_type' => 'page',
28 'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'page-attributes' ),
29 'has_archive' => false,
30 )
31 );
32
33 self::actions_init();
34 }
35
36 public static function actions_init() {
37 self::$registered_actions = new Frm_Form_Action_Factory();
38 self::register_actions();
39 do_action( 'frm_form_actions_init' );
40 }
41
42 public static function register_actions() {
43 $action_classes = array(
44 'email' => 'FrmEmailAction',
45 'wppost' => 'FrmDefPostAction',
46 'register' => 'FrmDefRegAction',
47 'paypal' => 'FrmDefPayPalAction',
48 'payment' => 'FrmDefHrsAction',
49 'mailchimp' => 'FrmDefMlcmpAction',
50 'api' => 'FrmDefApiAction',
51
52 'salesforce' => 'FrmDefSalesforceAction',
53 'activecampaign' => 'FrmDefActiveCampaignAction',
54 'constantcontact' => 'FrmDefConstContactAction',
55 'getresponse' => 'FrmDefGetResponseAction',
56 'hubspot' => 'FrmDefHubspotAction',
57 'zapier' => 'FrmDefZapierAction',
58 'twilio' => 'FrmDefTwilioAction',
59 'highrise' => 'FrmDefHighriseAction',
60 'mailpoet' => 'FrmDefMailpoetAction',
61 'aweber' => 'FrmDefAweberAction',
62 );
63
64 $action_classes = apply_filters( 'frm_registered_form_actions', $action_classes );
65
66 include_once( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/email_action.php' );
67 include_once( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/default_actions.php' );
68
69 foreach ( $action_classes as $action_class ) {
70 self::$registered_actions->register( $action_class );
71 }
72 }
73
74 /**
75 * @since 4.0
76 *
77 * @param array $values
78 */
79 public static function email_settings( $values ) {
80 $form = FrmForm::getOne( $values['id'] );
81 $groups = self::form_action_groups();
82
83 $action_controls = self::get_form_actions();
84 self::maybe_add_action_to_group( $action_controls, $groups );
85
86 $allowed = self::active_actions( $action_controls );
87
88 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/settings.php' );
89 }
90
91 /**
92 * Add unknown actions to a group.
93 *
94 * @since 4.0
95 */
96 private static function maybe_add_action_to_group( $action_controls, &$groups ) {
97 $grouped = array();
98 foreach ( $groups as $group ) {
99 if ( isset( $group['actions'] ) ) {
100 $grouped = array_merge( $grouped, $group['actions'] );
101 }
102 }
103
104 foreach ( $action_controls as $action ) {
105 if ( isset( $groups[ $action->id_base ] ) || in_array( $action->id_base, $grouped ) ) {
106 continue;
107 }
108
109 $this_group = $action->action_options['group'];
110 if ( ! isset( $groups[ $this_group ] ) ) {
111 $this_group = 'misc';
112 }
113
114 if ( ! isset( $groups[ $this_group ]['actions'] ) ) {
115 $groups[ $this_group ]['actions'] = array();
116 }
117 $groups[ $this_group ]['actions'][] = $action->id_base;
118
119 unset( $action );
120 }
121 }
122
123 /**
124 * @since 4.0
125 *
126 * @return array
127 */
128 public static function form_action_groups() {
129 $groups = array(
130 'misc' => array(
131 'name' => '',
132 'icon' => 'frm_icon_font frm_shuffle_icon',
133 'actions' => array(
134 'email',
135 'wppost',
136 'register',
137 'twilio',
138 ),
139 ),
140 'payment' => array(
141 'name' => __( 'eCommerce', 'formidable' ),
142 'icon' => 'frm_icon_font frm_credit_card_alt_icon',
143 'actions' => array(
144 'paypal',
145 'payment',
146 ),
147 ),
148 'marketing' => array(
149 'name' => __( 'Email Marketing', 'formidable' ),
150 'icon' => 'frm_icon_font frm_mail_bulk_icon',
151 'actions' => array(
152 'mailchimp',
153 'activecampaign',
154 'constantcontact',
155 'getresponse',
156 'aweber',
157 'mailpoet',
158 ),
159 ),
160 'crm' => array(
161 'name' => __( 'CRM', 'formidable' ),
162 'icon' => 'frm_icon_font frm_address_card_icon',
163 'actions' => array(
164 'salesforce',
165 'hubspot',
166 'highrise',
167 ),
168 ),
169 );
170
171 return apply_filters( 'frm_action_groups', $groups );
172 }
173
174 /**
175 * Get the number of currently active form actions.
176 *
177 * @since 4.0
178 *
179 * @return array
180 */
181 private static function active_actions( $action_controls ) {
182 $allowed = array();
183 foreach ( $action_controls as $action_control ) {
184 if ( isset( $action_control->action_options['active'] ) && $action_control->action_options['active'] ) {
185 $allowed[] = $action_control->id_base;
186 }
187 }
188 return $allowed;
189 }
190
191 /**
192 * For each add-on, add an li, class, and javascript function. If active, add an additional class.
193 *
194 * @since 4.0
195 * @param object $action_control
196 * @param array $allowed
197 */
198 public static function show_action_icon_link( $action_control, $allowed ) {
199 $data = array();
200 $classes = ' frm_' . $action_control->id_base . '_action frm_single_action';
201
202 $group_class = ' frm-group-' . $action_control->action_options['group'];
203
204 /* translators: %s: Name of form action */
205 $upgrade_label = sprintf( esc_html__( '%s form actions', 'formidable' ), $action_control->action_options['tooltip'] );
206
207 $default_shown = array( 'wppost', 'register', 'paypal', 'payment', 'mailchimp' );
208 $default_shown = array_values( array_diff( $default_shown, $allowed ) );
209 $default_position = array_search( $action_control->id_base, $default_shown );
210 $allowed_count = count( $allowed );
211
212 if ( isset( $action_control->action_options['active'] ) && $action_control->action_options['active'] ) {
213 $classes .= ' frm_active_action';
214 } else {
215 $classes .= ' frm_inactive_action';
216 if ( $default_position !== false && ( $allowed_count + $default_position ) < 6 ) {
217 $group_class .= ' frm-default-show';
218 }
219
220 $data['data-upgrade'] = $upgrade_label;
221 $data['data-medium'] = 'settings-' . $action_control->id_base;
222
223 $upgrading = FrmAddonsController::install_link( $action_control->action_options['plugin'] );
224 if ( isset( $upgrading['url'] ) ) {
225 $data['data-oneclick'] = json_encode( $upgrading );
226 }
227
228 $requires = self::action_requires( $upgrading );
229 if ( $requires ) {
230 $data['data-requires'] = $requires;
231 }
232 }
233
234 // HTML to include on the icon.
235 $icon_atts = array();
236 if ( $action_control->action_options['color'] !== 'var(--primary-hover)' ) {
237 $icon_atts = array(
238 'style' => '--primary-hover:' . $action_control->action_options['color'],
239 );
240 }
241
242 include FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/_action_icon.php';
243 }
244
245 /**
246 * @since 5.0.06
247 *
248 * @param array $upgrading
249 * @return string|false
250 */
251 private static function action_requires( $upgrading ) {
252 if ( ! isset( $upgrading['categories'] ) || ! is_array( $upgrading['categories'] ) ) {
253 return false;
254 }
255 $plans = array( 'Business', 'Elite', 'Basic' );
256 $plus_plans = array( 'Creator', 'Personal', 'Plus' );
257 foreach ( $upgrading['categories'] as $category ) {
258 if ( in_array( $category, $plans, true ) ) {
259 return $category;
260 }
261 if ( in_array( $category, $plus_plans, true ) ) {
262 return 'Plus';
263 }
264 }
265 return false;
266 }
267
268 public static function get_form_actions( $action = 'all' ) {
269 $temp_actions = self::$registered_actions;
270 if ( empty( $temp_actions ) ) {
271 self::actions_init();
272 $temp_actions = self::$registered_actions->actions;
273 } else {
274 $temp_actions = $temp_actions->actions;
275 }
276
277 $actions = array();
278
279 foreach ( $temp_actions as $a ) {
280 if ( 'all' != $action && $a->id_base == $action ) {
281 return $a;
282 }
283
284 $actions[ $a->id_base ] = $a;
285 }
286
287 return $actions;
288 }
289
290 /**
291 * @since 2.0
292 */
293 public static function list_actions( $form, $values ) {
294 if ( empty( $form ) ) {
295 return;
296 }
297
298 /**
299 * Use this hook to migrate old settings into a new action
300 *
301 * @since 2.0
302 */
303 do_action( 'frm_before_list_actions', $form );
304
305 $filters = array(
306 'post_status' => 'all',
307 );
308 $form_actions = FrmFormAction::get_action_for_form( $form->id, 'all', $filters );
309
310 $action_controls = self::get_form_actions();
311
312 $action_map = array();
313
314 foreach ( $action_controls as $key => $control ) {
315 $action_map[ $control->id_base ] = $key;
316 }
317
318 foreach ( $form_actions as $action ) {
319 if ( ! isset( $action_map[ $action->post_excerpt ] ) ) {
320 // don't try and show settings if action no longer exists
321 continue;
322 }
323
324 self::action_control( $action, $form, $action->ID, $action_controls[ $action_map[ $action->post_excerpt ] ], $values );
325 }
326 }
327
328 public static function action_control( $form_action, $form, $action_key, $action_control, $values ) {
329 $action_control->_set( $action_key );
330
331 $use_logging = self::should_show_log_message( $form_action->post_excerpt );
332
333 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/form_action.php' );
334 }
335
336 public static function add_form_action() {
337 FrmAppHelper::permission_check( 'frm_edit_forms' );
338 check_ajax_referer( 'frm_ajax', 'nonce' );
339
340 global $frm_vars;
341
342 $action_key = FrmAppHelper::get_param( 'list_id', '', 'post', 'absint' );
343 $action_type = FrmAppHelper::get_param( 'type', '', 'post', 'sanitize_text_field' );
344
345 $action_control = self::get_form_actions( $action_type );
346 $action_control->_set( $action_key );
347
348 $form_id = FrmAppHelper::get_param( 'form_id', '', 'post', 'absint' );
349
350 $form_action = $action_control->prepare_new( $form_id );
351 $use_logging = self::should_show_log_message( $action_type );
352
353 $values = array();
354 $form = self::fields_to_values( $form_id, $values );
355
356 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/form_action.php' );
357 wp_die();
358 }
359
360 public static function fill_action() {
361 FrmAppHelper::permission_check( 'frm_edit_forms' );
362 check_ajax_referer( 'frm_ajax', 'nonce' );
363
364 $action_key = FrmAppHelper::get_param( 'action_id', '', 'post', 'absint' );
365 $action_type = FrmAppHelper::get_param( 'action_type', '', 'post', 'sanitize_text_field' );
366
367 $action_control = self::get_form_actions( $action_type );
368 if ( empty( $action_control ) ) {
369 wp_die();
370 }
371
372 $form_action = $action_control->get_single_action( $action_key );
373
374 $values = array();
375 $form = self::fields_to_values( $form_action->menu_order, $values );
376
377 $use_logging = self::should_show_log_message( $action_type );
378
379 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/_action_inside.php' );
380 wp_die();
381 }
382
383 /**
384 * @since 3.06.04
385 * @return bool
386 */
387 private static function should_show_log_message( $action_type ) {
388 $logging = array( 'api', 'salesforce', 'constantcontact', 'activecampaign' );
389 return in_array( $action_type, $logging ) && ! function_exists( 'frm_log_autoloader' );
390 }
391
392 private static function fields_to_values( $form_id, array &$values ) {
393 $form = FrmForm::getOne( $form_id );
394
395 $values = array(
396 'fields' => array(),
397 'id' => $form->id,
398 );
399
400 $fields = FrmField::get_all_for_form( $form->id );
401 foreach ( $fields as $k => $f ) {
402 $f = (array) $f;
403 $opts = (array) $f['field_options'];
404 $f = array_merge( $opts, $f );
405 if ( ! isset( $f['post_field'] ) ) {
406 $f['post_field'] = '';
407 }
408 $values['fields'][] = $f;
409 unset( $k, $f );
410 }
411
412 return $form;
413 }
414
415 public static function update_settings( $form_id ) {
416 FrmAppHelper::permission_check( 'frm_edit_forms' );
417 $process_form = FrmAppHelper::get_post_param( 'process_form', '', 'sanitize_text_field' );
418 if ( ! wp_verify_nonce( $process_form, 'process_form_nonce' ) ) {
419 wp_die( esc_html__( 'You do not have permission to do that', 'formidable' ) );
420 }
421
422 global $wpdb;
423
424 $registered_actions = self::$registered_actions->actions;
425
426 $old_actions = FrmDb::get_col(
427 $wpdb->posts,
428 array(
429 'post_type' => self::$action_post_type,
430 'menu_order' => $form_id,
431 ),
432 'ID'
433 );
434 $new_actions = array();
435
436 foreach ( $registered_actions as $registered_action ) {
437 $action_ids = $registered_action->update_callback( $form_id );
438 if ( ! empty( $action_ids ) ) {
439 $new_actions[] = $action_ids;
440 }
441 }
442
443 // Only use array_merge if there are new actions.
444 if ( ! empty( $new_actions ) ) {
445 $new_actions = call_user_func_array( 'array_merge', $new_actions );
446 }
447 $old_actions = array_diff( $old_actions, $new_actions );
448
449 self::delete_missing_actions( $old_actions );
450 }
451
452 public static function delete_missing_actions( $old_actions ) {
453 if ( ! empty( $old_actions ) ) {
454 foreach ( $old_actions as $old_id ) {
455 wp_delete_post( $old_id );
456 }
457 FrmDb::cache_delete_group( 'frm_actions' );
458 }
459 }
460
461 public static function trigger_create_actions( $entry_id, $form_id, $args = array() ) {
462 $filter_args = $args;
463 $filter_args['entry_id'] = $entry_id;
464 $filter_args['form_id'] = $form_id;
465
466 $event = apply_filters( 'frm_trigger_create_action', 'create', $args );
467
468 self::trigger_actions( $event, $form_id, $entry_id, 'all', $args );
469 }
470
471 /**
472 * @param string $event
473 */
474 public static function trigger_actions( $event, $form, $entry, $type = 'all', $args = array() ) {
475 $action_status = array(
476 'post_status' => 'publish',
477 );
478 $form_actions = FrmFormAction::get_action_for_form( ( is_object( $form ) ? $form->id : $form ), $type, $action_status );
479
480 if ( empty( $form_actions ) ) {
481 return;
482 }
483
484 FrmForm::maybe_get_form( $form );
485
486 $link_settings = self::get_form_actions( $type );
487 if ( 'all' != $type ) {
488 $link_settings = array( $type => $link_settings );
489 }
490
491 $stored_actions = array();
492 $action_priority = array();
493
494 if ( in_array( $event, array( 'create', 'update' ) ) && defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
495 $this_event = 'import';
496 } else {
497 $this_event = $event;
498 }
499
500 foreach ( $form_actions as $action ) {
501
502 $skip_this_action = ( ! in_array( $this_event, $action->post_content['event'] ) );
503 $skip_this_action = apply_filters( 'frm_skip_form_action', $skip_this_action, compact( 'action', 'entry', 'form', 'event' ) );
504 if ( $skip_this_action ) {
505 continue;
506 }
507
508 if ( ! is_object( $entry ) ) {
509 $entry = FrmEntry::getOne( $entry, true );
510 }
511
512 if ( empty( $entry ) || ( $entry->is_draft && $event != 'draft' ) ) {
513 continue;
514 }
515
516 $child_entry = ( ( is_object( $form ) && is_numeric( $form->parent_form_id ) && $form->parent_form_id ) || ( $entry && ( $entry->form_id != $form->id || $entry->parent_item_id ) ) || ( isset( $args['is_child'] ) && $args['is_child'] ) );
517
518 if ( $child_entry ) {
519 // maybe trigger actions for sub forms
520 $trigger_children = apply_filters( 'frm_use_embedded_form_actions', false, compact( 'form', 'entry' ) );
521 if ( ! $trigger_children ) {
522 continue;
523 }
524 }
525
526 // Check conditional logic.
527 $stop = FrmFormAction::action_conditions_met( $action, $entry );
528 if ( $stop ) {
529 continue;
530 }
531
532 // Store actions so they can be triggered with the correct priority.
533 $stored_actions[ $action->ID ] = $action;
534 $action_priority[ $action->ID ] = $link_settings[ $action->post_excerpt ]->action_options['priority'];
535
536 unset( $action );
537 }
538
539 if ( ! empty( $stored_actions ) ) {
540 asort( $action_priority );
541
542 // Make sure hooks are loaded.
543 new FrmNotification();
544
545 foreach ( $action_priority as $action_id => $priority ) {
546 $action = $stored_actions[ $action_id ];
547 do_action( 'frm_trigger_' . $action->post_excerpt . '_action', $action, $entry, $form, $event );
548 do_action( 'frm_trigger_' . $action->post_excerpt . '_' . $event . '_action', $action, $entry, $form );
549
550 // If post is created, get updated $entry object.
551 if ( $action->post_excerpt == 'wppost' && $event == 'create' ) {
552 $entry = FrmEntry::getOne( $entry->id, true );
553 }
554 }
555 }
556 }
557
558 public static function duplicate_form_actions( $form_id, $values, $args = array() ) {
559 if ( ! isset( $args['old_id'] ) || empty( $args['old_id'] ) ) {
560 // Continue if we know which actions to copy.
561 return;
562 }
563
564 $action_controls = self::get_form_actions();
565
566 foreach ( $action_controls as $action_control ) {
567 $action_control->duplicate_form_actions( $form_id, $args['old_id'] );
568 unset( $action_control );
569 }
570 }
571
572 public static function limit_by_type( $where ) {
573 global $frm_vars, $wpdb;
574
575 if ( ! isset( $frm_vars['action_type'] ) ) {
576 return $where;
577 }
578
579 $where .= $wpdb->prepare( ' AND post_excerpt = %s ', $frm_vars['action_type'] );
580
581 return $where;
582 }
583 }
584
585 class Frm_Form_Action_Factory {
586 public $actions = array();
587
588 public function __construct() {
589 add_action( 'frm_form_actions_init', array( $this, '_register_actions' ), 100 );
590 }
591
592 public function register( $action_class ) {
593 $this->actions[ $action_class ] = new $action_class();
594 }
595
596 public function unregister( $action_class ) {
597 if ( isset( $this->actions[ $action_class ] ) ) {
598 unset( $this->actions[ $action_class ] );
599 }
600 }
601
602 public function _register_actions() {
603 $keys = array_keys( $this->actions );
604
605 foreach ( $keys as $key ) {
606 // don't register new action if old action with the same id is already registered
607 if ( ! isset( $this->actions[ $key ] ) ) {
608 $this->actions[ $key ]->_register();
609 }
610 }
611 }
612 }
613