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

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

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