Passed
Push — master ( ec16eb...0357f3 )
by Stephanie
03:05
created

FrmFormActionsController::trigger_actions()   F

Complexity

Conditions 27
Paths 3425

Size

Total Lines 76
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 27
eloc 42
nc 3425
nop 5
dl 0
loc 76
rs 2.4485
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class FrmFormActionsController {
4
    public static $action_post_type = 'frm_form_actions';
5
    public static $registered_actions;
6
7
    public static function register_post_types() {
8
        register_post_type( self::$action_post_type, array(
0 ignored issues
show
Bug introduced by
The function register_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

8
        /** @scrutinizer ignore-call */ 
9
        register_post_type( self::$action_post_type, array(
Loading history...
9
            'label' => __( 'Form Actions', 'formidable' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

9
            'label' => /** @scrutinizer ignore-call */ __( 'Form Actions', 'formidable' ),
Loading history...
10
            'description' => '',
11
            'public' => false,
12
            'show_ui' => false,
13
            'exclude_from_search' => true,
14
            'show_in_nav_menus' => false,
15
            'show_in_menu' => true,
16
            'capability_type' => 'page',
17
            'supports' => array( 'title', 'editor', 'excerpt', 'custom-fields', 'page-attributes' ),
18
            'has_archive' => false,
19
        ) );
20
21
        /**
22
         * post_content: json settings
23
         * menu_order: form id
24
         * post_excerpt: action type
25
         */
26
27
        self::actions_init();
28
    }
29
30
    public static function actions_init() {
31
        self::$registered_actions = new Frm_Form_Action_Factory();
32
        self::register_actions();
33
        do_action( 'frm_form_actions_init' );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        /** @scrutinizer ignore-call */ 
34
        do_action( 'frm_form_actions_init' );
Loading history...
34
    }
35
36
    public static function register_actions() {
37
        $action_classes = apply_filters( 'frm_registered_form_actions', array(
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        $action_classes = /** @scrutinizer ignore-call */ apply_filters( 'frm_registered_form_actions', array(
Loading history...
38
            'email'     => 'FrmEmailAction',
39
            'wppost'    => 'FrmDefPostAction',
40
            'register'  => 'FrmDefRegAction',
41
            'paypal'    => 'FrmDefPayPalAction',
42
            //'aweber'    => 'FrmDefAweberAction',
43
            'mailchimp' => 'FrmDefMlcmpAction',
44
            'twilio'    => 'FrmDefTwilioAction',
45
            'highrise'  => 'FrmDefHrsAction',
46
        ) );
47
48
        include_once(FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/email_action.php');
49
        include_once(FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/default_actions.php');
50
51
        foreach ( $action_classes as $action_class ) {
52
            self::$registered_actions->register($action_class);
53
        }
54
    }
55
56
	public static function get_form_actions( $action = 'all' ) {
57
        $temp_actions = self::$registered_actions;
58
        if ( empty($temp_actions) ) {
59
            self::actions_init();
60
            $temp_actions = self::$registered_actions->actions;
61
        } else {
62
            $temp_actions = $temp_actions->actions;
63
        }
64
65
        $actions = array();
66
67
        foreach ( $temp_actions as $a ) {
68
            if ( 'all' != $action && $a->id_base == $action ) {
69
                return $a;
70
            }
71
72
			$actions[ $a->id_base ] = $a;
73
        }
74
        unset( $temp_actions, $a );
75
76
        $action_limit = 10;
77
        if ( count( $actions ) <= $action_limit ) {
78
            return $actions;
79
        }
80
81
        // remove the last few inactive icons if there are too many
82
        $temp_actions = $actions;
83
        arsort( $temp_actions );
84
        foreach ( $temp_actions as $type => $a ) {
85
            if ( ! isset( $a->action_options['active'] ) || empty( $a->action_options['active'] ) ) {
86
				unset( $actions[ $type ] );
87
                if ( count( $actions ) <= $action_limit ) {
88
                    break;
89
                }
90
            }
91
            unset( $type, $a );
92
        }
93
94
        return $actions;
95
    }
96
97
	/**
98
	 * @since 2.0
99
	 */
100
    public static function list_actions( $form, $values ) {
101
        if ( empty( $form ) ) {
102
            return;
103
        }
104
105
		/**
106
		 * use this hook to migrate old settings into a new action
107
		 * @since 2.0
108
		 */
109
		do_action( 'frm_before_list_actions', $form );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
		/** @scrutinizer ignore-call */ 
110
  do_action( 'frm_before_list_actions', $form );
Loading history...
110
111
		$form_actions = FrmFormAction::get_action_for_form( $form->id );
112
113
        $action_controls = self::get_form_actions();
114
115
        $action_map = array();
116
117
		foreach ( $action_controls as $key => $control ) {
118
            $action_map[ $control->id_base ] = $key;
119
        }
120
121
    	foreach ( $form_actions as $action ) {
122
    	    if ( ! isset( $action_map[ $action->post_excerpt ] ) ) {
123
    	        // don't try and show settings if action no longer exists
124
    	        continue;
125
    	    }
126
127
    		self::action_control( $action, $form, $action->ID, $action_controls[ $action_map[ $action->post_excerpt ] ], $values );
128
    	}
129
    }
130
131
	public static function action_control( $form_action, $form, $action_key, $action_control, $values ) {
132
        $action_control->_set($action_key);
133
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/form_action.php' );
134
    }
135
136
    public static function add_form_action() {
137
		FrmAppHelper::permission_check('frm_edit_forms');
138
        check_ajax_referer( 'frm_ajax', 'nonce' );
0 ignored issues
show
Bug introduced by
The function check_ajax_referer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

138
        /** @scrutinizer ignore-call */ 
139
        check_ajax_referer( 'frm_ajax', 'nonce' );
Loading history...
139
140
        global $frm_vars;
141
142
		$action_key = absint( $_POST['list_id'] );
0 ignored issues
show
Bug introduced by
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

142
		$action_key = /** @scrutinizer ignore-call */ absint( $_POST['list_id'] );
Loading history...
143
        $action_type = sanitize_text_field( $_POST['type'] );
0 ignored issues
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

143
        $action_type = /** @scrutinizer ignore-call */ sanitize_text_field( $_POST['type'] );
Loading history...
144
145
        $action_control = self::get_form_actions( $action_type );
146
        $action_control->_set($action_key);
147
148
        $form_id = absint( $_POST['form_id'] );
149
150
        $form_action = $action_control->prepare_new($form_id);
151
152
        $values = array();
153
        $form = self::fields_to_values($form_id, $values);
154
155
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/form_action.php' );
156
        wp_die();
0 ignored issues
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
        /** @scrutinizer ignore-call */ 
157
        wp_die();
Loading history...
157
    }
158
159
    public static function fill_action() {
160
		FrmAppHelper::permission_check('frm_edit_forms');
161
        check_ajax_referer( 'frm_ajax', 'nonce' );
0 ignored issues
show
Bug introduced by
The function check_ajax_referer was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

161
        /** @scrutinizer ignore-call */ 
162
        check_ajax_referer( 'frm_ajax', 'nonce' );
Loading history...
162
163
        $action_key = absint( $_POST['action_id'] );
0 ignored issues
show
Bug introduced by
The function absint was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
        $action_key = /** @scrutinizer ignore-call */ absint( $_POST['action_id'] );
Loading history...
164
        $action_type = sanitize_text_field( $_POST['action_type'] );
0 ignored issues
show
Bug introduced by
The function sanitize_text_field was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
        $action_type = /** @scrutinizer ignore-call */ sanitize_text_field( $_POST['action_type'] );
Loading history...
165
166
        $action_control = self::get_form_actions( $action_type );
167
        if ( empty($action_control) ) {
168
            wp_die();
0 ignored issues
show
Bug introduced by
The function wp_die was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
            /** @scrutinizer ignore-call */ 
169
            wp_die();
Loading history...
169
        }
170
171
        $form_action = $action_control->get_single_action( $action_key );
172
173
        $values = array();
174
        $form = self::fields_to_values($form_action->menu_order, $values);
175
176
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-form-actions/_action_inside.php' );
177
        wp_die();
178
    }
179
180
	private static function fields_to_values( $form_id, array &$values ) {
181
        $form = FrmForm::getOne($form_id);
182
183
		$values = array(
184
			'fields' => array(),
185
			'id'     => $form->id,
186
		);
187
188
        $fields = FrmField::get_all_for_form($form->id);
189
        foreach ( $fields as $k => $f ) {
190
            $f = (array) $f;
191
            $opts = (array) $f['field_options'];
192
            $f = array_merge($opts, $f);
193
            if ( ! isset( $f['post_field'] ) ) {
194
                $f['post_field'] = '';
195
            }
196
            $values['fields'][] = $f;
197
            unset($k, $f);
198
        }
199
200
        return $form;
201
    }
202
203
	public static function update_settings( $form_id ) {
204
        global $wpdb;
205
206
        $registered_actions = self::$registered_actions->actions;
207
208
		$old_actions = FrmDb::get_col( $wpdb->posts, array(
209
			'post_type' => self::$action_post_type,
210
			'menu_order' => $form_id,
211
		), 'ID' );
212
        $new_actions = array();
213
214
        foreach ( $registered_actions as $registered_action ) {
215
            $action_ids = $registered_action->update_callback($form_id);
216
            if ( ! empty( $action_ids ) ) {
217
                $new_actions[] = $action_ids;
218
            }
219
        }
220
221
        //Only use array_merge if there are new actions
222
        if ( ! empty( $new_actions ) ) {
223
            $new_actions = call_user_func_array( 'array_merge', $new_actions );
224
        }
225
        $old_actions = array_diff( $old_actions, $new_actions );
226
227
		self::delete_missing_actions( $old_actions );
228
    }
229
230
	public static function delete_missing_actions( $old_actions ) {
231
		if ( ! empty( $old_actions ) ) {
232
			foreach ( $old_actions as $old_id ) {
233
				wp_delete_post( $old_id );
0 ignored issues
show
Bug introduced by
The function wp_delete_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

233
				/** @scrutinizer ignore-call */ 
234
    wp_delete_post( $old_id );
Loading history...
234
			}
235
			FrmDb::cache_delete_group( 'frm_actions' );
236
		}
237
	}
238
239
	public static function trigger_create_actions( $entry_id, $form_id, $args = array() ) {
240
		$filter_args = $args;
241
		$filter_args['entry_id'] = $entry_id;
242
		$filter_args['form_id']  = $form_id;
243
		$event = apply_filters( 'frm_trigger_create_action', 'create', $args );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

243
		$event = /** @scrutinizer ignore-call */ apply_filters( 'frm_trigger_create_action', 'create', $args );
Loading history...
244
245
		self::trigger_actions( $event, $form_id, $entry_id, 'all', $args );
246
	}
247
248
    /**
249
     * @param string $event
250
     */
251
	public static function trigger_actions( $event, $form, $entry, $type = 'all', $args = array() ) {
252
		$form_actions = FrmFormAction::get_action_for_form( ( is_object( $form ) ? $form->id : $form ), $type );
253
254
		if ( empty( $form_actions ) ) {
255
            return;
256
        }
257
258
		FrmForm::maybe_get_form( $form );
259
260
        $link_settings = self::get_form_actions( $type );
261
        if ( 'all' != $type ) {
262
            $link_settings = array( $type => $link_settings );
263
        }
264
265
		$stored_actions = array();
266
		$action_priority = array();
267
268
		if ( in_array( $event, array( 'create', 'update' ) ) && defined( 'WP_IMPORTING' ) && WP_IMPORTING ) {
269
			$this_event = 'import';
270
		} else {
271
			$this_event = $event;
272
		}
273
274
        foreach ( $form_actions as $action ) {
275
276
			$skip_this_action = ( ! in_array( $this_event, $action->post_content['event'] ) );
277
			$skip_this_action = apply_filters( 'frm_skip_form_action', $skip_this_action, compact( 'action', 'entry', 'form', 'event' ) );
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

277
			$skip_this_action = /** @scrutinizer ignore-call */ apply_filters( 'frm_skip_form_action', $skip_this_action, compact( 'action', 'entry', 'form', 'event' ) );
Loading history...
278
			if ( $skip_this_action ) {
279
                continue;
280
            }
281
282
            if ( ! is_object( $entry ) ) {
283
                $entry = FrmEntry::getOne( $entry, true );
284
            }
285
286
			if ( empty( $entry ) || ( $entry->is_draft && $event != 'draft' ) ) {
287
				continue;
288
			}
289
290
			$child_entry = ( ( $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'] ) );
291
292
			if ( $child_entry ) {
293
				// maybe trigger actions for sub forms
294
				$trigger_children = apply_filters( 'frm_use_embedded_form_actions', false, compact( 'form', 'entry' ) );
295
				if ( ! $trigger_children ) {
296
					continue;
297
				}
298
            }
299
300
            // check conditional logic
301
			$stop = FrmFormAction::action_conditions_met( $action, $entry );
302
            if ( $stop ) {
303
                continue;
304
            }
305
306
            // store actions so they can be triggered with the correct priority
307
            $stored_actions[ $action->ID ] = $action;
308
            $action_priority[ $action->ID ] = $link_settings[ $action->post_excerpt ]->action_options['priority'];
309
310
            unset($action);
311
        }
312
313
        if ( ! empty( $stored_actions ) ) {
314
            asort($action_priority);
315
316
            // make sure hooks are loaded
317
            new FrmNotification();
318
319
            foreach ( $action_priority as $action_id => $priority ) {
320
                $action = $stored_actions[ $action_id ];
321
				do_action( 'frm_trigger_' . $action->post_excerpt . '_action', $action, $entry, $form, $event );
0 ignored issues
show
Bug introduced by
The function do_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

321
				/** @scrutinizer ignore-call */ 
322
    do_action( 'frm_trigger_' . $action->post_excerpt . '_action', $action, $entry, $form, $event );
Loading history...
322
				do_action( 'frm_trigger_' . $action->post_excerpt . '_' . $event . '_action', $action, $entry, $form );
323
324
                // If post is created, get updated $entry object
325
                if ( $action->post_excerpt == 'wppost' && $event == 'create' ) {
326
                    $entry = FrmEntry::getOne($entry->id, true);
327
                }
328
            }
329
        }
330
    }
331
332
	public static function duplicate_form_actions( $form_id, $values, $args = array() ) {
0 ignored issues
show
Unused Code introduced by
The parameter $values is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

332
	public static function duplicate_form_actions( $form_id, /** @scrutinizer ignore-unused */ $values, $args = array() ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
333
        if ( ! isset($args['old_id']) || empty($args['old_id']) ) {
334
            // continue if we know which actions to copy
335
            return;
336
        }
337
338
		$action_controls = self::get_form_actions();
339
340
        foreach ( $action_controls as $action_control ) {
341
            $action_control->duplicate_form_actions( $form_id, $args['old_id'] );
342
            unset( $action_control );
343
        }
344
    }
345
346
    public static function limit_by_type( $where ) {
347
        global $frm_vars, $wpdb;
348
349
        if ( ! isset( $frm_vars['action_type'] ) ) {
350
            return $where;
351
        }
352
353
        $where .= $wpdb->prepare( ' AND post_excerpt = %s ', $frm_vars['action_type'] );
354
        return $where;
355
    }
356
}
357
358
359
class Frm_Form_Action_Factory {
360
	public $actions = array();
361
362
	public function __construct() {
363
		add_action( 'frm_form_actions_init', array( $this, '_register_actions' ), 100 );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

363
		/** @scrutinizer ignore-call */ 
364
  add_action( 'frm_form_actions_init', array( $this, '_register_actions' ), 100 );
Loading history...
364
	}
365
366
	public function register( $action_class ) {
367
		$this->actions[ $action_class ] = new $action_class();
368
	}
369
370
	public function unregister( $action_class ) {
371
		if ( isset( $this->actions[ $action_class ] ) ) {
372
			unset($this->actions[ $action_class ]);
373
		}
374
	}
375
376
	public function _register_actions() {
377
		$keys = array_keys($this->actions);
378
379
		foreach ( $keys as $key ) {
380
			// don't register new action if old action with the same id is already registered
381
			if ( ! isset( $this->actions[ $key ] ) ) {
382
			    $this->actions[ $key ]->_register();
383
			}
384
		}
385
	}
386
}
387