| Total Complexity | 67 |
| Total Lines | 352 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like FrmFormActionsController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FrmFormActionsController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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( |
||
|
|
|||
| 9 | 'label' => __( 'Form Actions', 'formidable' ), |
||
| 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' ); |
||
| 34 | } |
||
| 35 | |||
| 36 | public static function register_actions() { |
||
| 37 | $action_classes = apply_filters( 'frm_registered_form_actions', array( |
||
| 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 ); |
||
| 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' ); |
||
| 139 | |||
| 140 | global $frm_vars; |
||
| 141 | |||
| 142 | $action_key = absint( $_POST['list_id'] ); |
||
| 143 | $action_type = sanitize_text_field( $_POST['type'] ); |
||
| 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(); |
||
| 157 | } |
||
| 158 | |||
| 159 | public static function fill_action() { |
||
| 160 | FrmAppHelper::permission_check('frm_edit_forms'); |
||
| 161 | check_ajax_referer( 'frm_ajax', 'nonce' ); |
||
| 162 | |||
| 163 | $action_key = absint( $_POST['action_id'] ); |
||
| 164 | $action_type = sanitize_text_field( $_POST['action_type'] ); |
||
| 165 | |||
| 166 | $action_control = self::get_form_actions( $action_type ); |
||
| 167 | if ( empty($action_control) ) { |
||
| 168 | wp_die(); |
||
| 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 ) { |
||
| 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 ); |
||
| 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 ); |
||
| 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' ) ); |
||
| 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 ); |
||
| 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() ) { |
||
| 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; |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | |||
| 359 | class Frm_Form_Action_Factory { |
||
| 360 | public $actions = array(); |
||
| 387 |