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

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