PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0.11
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.0.11
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.11, at classes/controllers/FrmFormActionsController.php

617 lines 17.4 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', 'hubspot' );
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 if ( isset( $action_control->action_options['message'] ) ) {
229 $data['data-message'] = $action_control->action_options['message'];
230 }
231
232 $requires = self::action_requires( $upgrading );
233 if ( $requires ) {
234 $data['data-requires'] = $requires;
235 }
236 }
237
238 // HTML to include on the icon.
239 $icon_atts = array();
240 if ( $action_control->action_options['color'] !== 'var(--primary-hover)' ) {
241 $icon_atts = array(
242 'style' => '--primary-hover:' . $action_control->action_options['color'],
243 );
244 }
245
246 include FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/_action_icon.php';
247 }
248
249 /**
250 * @since 5.0.06
251 *
252 * @param array $upgrading
253 * @return string|false
254 */
255 private static function action_requires( $upgrading ) {
256 if ( ! isset( $upgrading['categories'] ) || ! is_array( $upgrading['categories'] ) ) {
257 return false;
258 }
259 $plans = array( 'Business', 'Elite', 'Basic' );
260 $plus_plans = array( 'Creator', 'Personal', 'Plus' );
261 foreach ( $upgrading['categories'] as $category ) {
262 if ( in_array( $category, $plans, true ) ) {
263 return $category;
264 }
265 if ( in_array( $category, $plus_plans, true ) ) {
266 return 'Plus';
267 }
268 }
269 return false;
270 }
271
272 public static function get_form_actions( $action = 'all' ) {
273 $temp_actions = self::$registered_actions;
274 if ( empty( $temp_actions ) ) {
275 self::actions_init();
276 $temp_actions = self::$registered_actions->actions;
277 } else {
278 $temp_actions = $temp_actions->actions;
279 }
280
281 $actions = array();
282
283 foreach ( $temp_actions as $a ) {
284 if ( 'all' != $action && $a->id_base == $action ) {
285 return $a;
286 }
287
288 $actions[ $a->id_base ] = $a;
289 }
290
291 return $actions;
292 }
293
294 /**
295 * @since 2.0
296 */
297 public static function list_actions( $form, $values ) {
298 if ( empty( $form ) ) {
299 return;
300 }
301
302 /**
303 * Use this hook to migrate old settings into a new action
304 *
305 * @since 2.0
306 */
307 do_action( 'frm_before_list_actions', $form );
308
309 $filters = array(
310 'post_status' => 'all',
311 );
312 $form_actions = FrmFormAction::get_action_for_form( $form->id, 'all', $filters );
313
314 $action_controls = self::get_form_actions();
315
316 $action_map = array();
317
318 foreach ( $action_controls as $key => $control ) {
319 $action_map[ $control->id_base ] = $key;
320 }
321
322 foreach ( $form_actions as $action ) {
323 if ( ! isset( $action_map[ $action->post_excerpt ] ) ) {
324 // don't try and show settings if action no longer exists
325 continue;
326 }
327
328 self::action_control( $action, $form, $action->ID, $action_controls[ $action_map[ $action->post_excerpt ] ], $values );
329 }
330 }
331
332 public static function action_control( $form_action, $form, $action_key, $action_control, $values ) {
333 $action_control->_set( $action_key );
334
335 $use_logging = self::should_show_log_message( $form_action->post_excerpt );
336
337 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/form_action.php' );
338 }
339
340 public static function add_form_action() {
341 FrmAppHelper::permission_check( 'frm_edit_forms' );
342 check_ajax_referer( 'frm_ajax', 'nonce' );
343
344 global $frm_vars;
345
346 $action_key = FrmAppHelper::get_param( 'list_id', '', 'post', 'absint' );
347 $action_type = FrmAppHelper::get_param( 'type', '', 'post', 'sanitize_text_field' );
348
349 $action_control = self::get_form_actions( $action_type );
350 $action_control->_set( $action_key );
351
352 $form_id = FrmAppHelper::get_param( 'form_id', '', 'post', 'absint' );
353
354 $form_action = $action_control->prepare_new( $form_id );
355 $use_logging = self::should_show_log_message( $action_type );
356
357 $values = array();
358 $form = self::fields_to_values( $form_id, $values );
359
360 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/form_action.php' );
361 wp_die();
362 }
363
364 public static function fill_action() {
365 FrmAppHelper::permission_check( 'frm_edit_forms' );
366 check_ajax_referer( 'frm_ajax', 'nonce' );
367
368 $action_key = FrmAppHelper::get_param( 'action_id', '', 'post', 'absint' );
369 $action_type = FrmAppHelper::get_param( 'action_type', '', 'post', 'sanitize_text_field' );
370
371 $action_control = self::get_form_actions( $action_type );
372 if ( empty( $action_control ) ) {
373 wp_die();
374 }
375
376 $form_action = $action_control->get_single_action( $action_key );
377
378 $values = array();
379 $form = self::fields_to_values( $form_action->menu_order, $values );
380
381 $use_logging = self::should_show_log_message( $action_type );
382
383 include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/_action_inside.php' );
384 wp_die();
385 }
386
387 /**
388 * @since 3.06.04
389 * @return bool
390 */
391 private static function should_show_log_message( $action_type ) {
392 $logging = array( 'api', 'salesforce', 'constantcontact', 'activecampaign' );
393 return in_array( $action_type, $logging ) && ! function_exists( 'frm_log_autoloader' );
394 }
395
396 private static function fields_to_values( $form_id, array &$values ) {
397 $form = FrmForm::getOne( $form_id );
398
399 $values = array(
400 'fields' => array(),
401 'id' => $form->id,
402 );
403
404 $fields = FrmField::get_all_for_form( $form->id );
405 foreach ( $fields as $k => $f ) {
406 $f = (array) $f;
407 $opts = (array) $f['field_options'];
408 $f = array_merge( $opts, $f );
409 if ( ! isset( $f['post_field'] ) ) {
410 $f['post_field'] = '';
411 }
412 $values['fields'][] = $f;
413 unset( $k, $f );
414 }
415
416 return $form;
417 }
418
419 public static function update_settings( $form_id ) {
420 FrmAppHelper::permission_check( 'frm_edit_forms' );
421 $process_form = FrmAppHelper::get_post_param( 'process_form', '', 'sanitize_text_field' );
422 if ( ! wp_verify_nonce( $process_form, 'process_form_nonce' ) ) {
423 wp_die( esc_html__( 'You do not have permission to do that', 'formidable' ) );
424 }
425
426 global $wpdb;
427
428 $registered_actions = self::$registered_actions->actions;
429
430 $old_actions = FrmDb::get_col(
431 $wpdb->posts,
432 array(
433 'post_type' => self::$action_post_type,
434 'menu_order' => $form_id,
435 ),
436 'ID'
437 );
438 $new_actions = array();
439
440 foreach ( $registered_actions as $registered_action ) {
441 $action_ids = $registered_action->update_callback( $form_id );
442 if ( ! empty( $action_ids ) ) {
443 $new_actions[] = $action_ids;
444 }
445 }
446
447 // Only use array_merge if there are new actions.
448 if ( ! empty( $new_actions ) ) {
449 $new_actions = call_user_func_array( 'array_merge', $new_actions );
450 }
451 $old_actions = array_diff( $old_actions, $new_actions );
452
453 self::delete_missing_actions( $old_actions );
454 }
455
456 public static function delete_missing_actions( $old_actions ) {
457 if ( ! empty( $old_actions ) ) {
458 foreach ( $old_actions as $old_id ) {
459 wp_delete_post( $old_id );
460 }
461 FrmDb::cache_delete_group( 'frm_actions' );
462 }
463 }
464
465 public static function trigger_create_actions( $entry_id, $form_id, $args = array() ) {
466 $filter_args = $args;
467 $filter_args['entry_id'] = $entry_id;
468 $filter_args['form_id'] = $form_id;
469
470 $event = apply_filters( 'frm_trigger_create_action', 'create', $args );
471
472 self::trigger_actions( $event, $form_id, $entry_id, 'all', $args );
473 }
474
475 /**
476 * @param string $event
477 */
478 public static function trigger_actions( $event, $form, $entry, $type = 'all', $args = array() ) {
479 $action_status = array(
480 'post_status' => 'publish',
481 );
482 $form_actions = FrmFormAction::get_action_for_form( ( is_object( $form ) ? $form->id : $form ), $type, $action_status );
483
484 if ( empty( $form_actions ) ) {
485 return;
486 }
487
488 FrmForm::maybe_get_form( $form );
489
490 $link_settings = self::get_form_actions( $type );
491 if ( 'all' != $type ) {
492 $link_settings = array( $type => $link_settings );
493 }
494
495 $stored_actions = array();
496 $action_priority = array();
497
498 if ( in_array( $event, array( 'create', 'update' ) ) && defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
499 $this_event = 'import';
500 } else {
501 $this_event = $event;
502 }
503
504 foreach ( $form_actions as $action ) {
505
506 $skip_this_action = ( ! in_array( $this_event, $action->post_content['event'] ) );
507 $skip_this_action = apply_filters( 'frm_skip_form_action', $skip_this_action, compact( 'action', 'entry', 'form', 'event' ) );
508 if ( $skip_this_action ) {
509 continue;
510 }
511
512 if ( ! is_object( $entry ) ) {
513 $entry = FrmEntry::getOne( $entry, true );
514 }
515
516 if ( empty( $entry ) || ( $entry->is_draft && $event != 'draft' ) ) {
517 continue;
518 }
519
520 $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'] ) );
521
522 if ( $child_entry ) {
523 // maybe trigger actions for sub forms
524 $trigger_children = apply_filters( 'frm_use_embedded_form_actions', false, compact( 'form', 'entry' ) );
525 if ( ! $trigger_children ) {
526 continue;
527 }
528 }
529
530 // Check conditional logic.
531 $stop = FrmFormAction::action_conditions_met( $action, $entry );
532 if ( $stop ) {
533 continue;
534 }
535
536 // Store actions so they can be triggered with the correct priority.
537 $stored_actions[ $action->ID ] = $action;
538 $action_priority[ $action->ID ] = $link_settings[ $action->post_excerpt ]->action_options['priority'];
539
540 unset( $action );
541 }
542
543 if ( ! empty( $stored_actions ) ) {
544 asort( $action_priority );
545
546 // Make sure hooks are loaded.
547 new FrmNotification();
548
549 foreach ( $action_priority as $action_id => $priority ) {
550 $action = $stored_actions[ $action_id ];
551 do_action( 'frm_trigger_' . $action->post_excerpt . '_action', $action, $entry, $form, $event );
552 do_action( 'frm_trigger_' . $action->post_excerpt . '_' . $event . '_action', $action, $entry, $form );
553
554 // If post is created, get updated $entry object.
555 if ( $action->post_excerpt == 'wppost' && $event == 'create' ) {
556 $entry = FrmEntry::getOne( $entry->id, true );
557 }
558 }
559 }
560 }
561
562 public static function duplicate_form_actions( $form_id, $values, $args = array() ) {
563 if ( ! isset( $args['old_id'] ) || empty( $args['old_id'] ) ) {
564 // Continue if we know which actions to copy.
565 return;
566 }
567
568 $action_controls = self::get_form_actions();
569
570 foreach ( $action_controls as $action_control ) {
571 $action_control->duplicate_form_actions( $form_id, $args['old_id'] );
572 unset( $action_control );
573 }
574 }
575
576 public static function limit_by_type( $where ) {
577 global $frm_vars, $wpdb;
578
579 if ( ! isset( $frm_vars['action_type'] ) ) {
580 return $where;
581 }
582
583 $where .= $wpdb->prepare( ' AND post_excerpt = %s ', $frm_vars['action_type'] );
584
585 return $where;
586 }
587 }
588
589 class Frm_Form_Action_Factory {
590 public $actions = array();
591
592 public function __construct() {
593 add_action( 'frm_form_actions_init', array( $this, '_register_actions' ), 100 );
594 }
595
596 public function register( $action_class ) {
597 $this->actions[ $action_class ] = new $action_class();
598 }
599
600 public function unregister( $action_class ) {
601 if ( isset( $this->actions[ $action_class ] ) ) {
602 unset( $this->actions[ $action_class ] );
603 }
604 }
605
606 public function _register_actions() {
607 $keys = array_keys( $this->actions );
608
609 foreach ( $keys as $key ) {
610 // don't register new action if old action with the same id is already registered
611 if ( ! isset( $this->actions[ $key ] ) ) {
612 $this->actions[ $key ]->_register();
613 }
614 }
615 }
616 }
617