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 / FrmFormActionsController.php

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

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