| Total Complexity | 129 |
| Total Lines | 658 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like FrmEntriesController 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 FrmEntriesController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 3 | class FrmEntriesController { |
||
| 4 | |||
| 5 | public static function menu() { |
||
| 6 | FrmAppHelper::force_capability( 'frm_view_entries' ); |
||
| 7 | |||
| 8 | add_submenu_page( 'formidable', 'Formidable | ' . __( 'Entries', 'formidable' ), __( 'Entries', 'formidable' ), 'frm_view_entries', 'formidable-entries', 'FrmEntriesController::route' ); |
||
| 9 | |||
| 10 | self::load_manage_entries_hooks(); |
||
| 11 | } |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @since 2.05.07 |
||
| 15 | */ |
||
| 16 | private static function load_manage_entries_hooks() { |
||
| 17 | if ( ! in_array( FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ), array( 'edit', 'show', 'new' ) ) ) { |
||
| 18 | $menu_name = FrmAppHelper::get_menu_name(); |
||
| 19 | $base = self::base_column_key( $menu_name ); |
||
| 20 | |||
| 21 | add_filter( 'manage_' . $base . '_columns', 'FrmEntriesController::manage_columns' ); |
||
| 22 | add_filter( 'get_user_option_' . self::hidden_column_key( $menu_name ), 'FrmEntriesController::hidden_columns' ); |
||
| 23 | add_filter( 'manage_' . $base . '_sortable_columns', 'FrmEntriesController::sortable_columns' ); |
||
| 24 | } else { |
||
| 25 | add_filter( 'screen_options_show_screen', __CLASS__ . '::remove_screen_options', 10, 2 ); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | /* Display in Back End */ |
||
| 30 | public static function route() { |
||
| 31 | $action = FrmAppHelper::get_param( 'frm_action', '', 'get', 'sanitize_title' ); |
||
| 32 | |||
| 33 | switch ( $action ) { |
||
| 34 | case 'show': |
||
| 35 | case 'destroy': |
||
| 36 | case 'destroy_all': |
||
| 37 | return self::$action(); |
||
| 38 | |||
| 39 | default: |
||
| 40 | do_action( 'frm_entry_action_route', $action ); |
||
| 41 | if ( apply_filters( 'frm_entry_stop_action_route', false, $action ) ) { |
||
| 42 | return; |
||
| 43 | } |
||
| 44 | |||
| 45 | return self::display_list(); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | public static function contextual_help( $help, $screen_id, $screen ) { |
||
| 50 | // Only add to certain screens. add_help_tab was introduced in WordPress 3.3 |
||
| 51 | if ( ! method_exists( $screen, 'add_help_tab' ) ) { |
||
| 52 | return $help; |
||
| 53 | } |
||
| 54 | |||
| 55 | $action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ); |
||
| 56 | $page = FrmAppHelper::simple_get( 'page', 'sanitize_title' ); |
||
| 57 | $show_help = ( $page == 'formidable-entries' && ( empty( $action ) || $action == 'list' ) ); |
||
| 58 | if ( ! $show_help ) { |
||
| 59 | return $help; |
||
| 60 | } |
||
| 61 | |||
| 62 | unset( $action, $page ); |
||
| 63 | |||
| 64 | $screen->add_help_tab( array( |
||
| 65 | 'id' => 'formidable-entries-tab', |
||
| 66 | 'title' => __( 'Overview', 'formidable' ), |
||
| 67 | 'content' => '<p>' . esc_html__( 'This screen provides access to all of your entries. You can customize the display of this screen to suit your workflow.', 'formidable' ) . '</p> <p>' . esc_html__( 'Hovering over a row in the entries list will display action links that allow you to manage your entry.', 'formidable' ) . '</p>', |
||
| 68 | )); |
||
| 69 | |||
| 70 | $screen->set_help_sidebar( |
||
| 71 | '<p><strong>' . esc_html__( 'For more information:', 'formidable' ) . '</strong></p>' . |
||
| 72 | '<p><a href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidableforms.com/knowledgebase/manage-entries-from-the-back-end/' ) ) . '" target="_blank">' . esc_html__( 'Documentation on Entries', 'formidable' ) . '</a></p>' . |
||
| 73 | '<p><a href="' . esc_url( FrmAppHelper::make_affiliate_url( 'https://formidableforms.com/help-desk/' ) ) . '" target="_blank">' . esc_html__( 'Support', 'formidable' ) . '</a></p>' |
||
| 74 | ); |
||
| 75 | |||
| 76 | return $help; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Prevent the "screen options" tab from showing when |
||
| 81 | * editing or creating an entry |
||
| 82 | * |
||
| 83 | * @since 3.0 |
||
| 84 | */ |
||
| 85 | public static function remove_screen_options( $show_screen, $screen ) { |
||
| 86 | $menu_name = sanitize_title( FrmAppHelper::get_menu_name() ); |
||
| 87 | if ( $screen->id == $menu_name . '_page_formidable-entries' ) { |
||
| 88 | $show_screen = false; |
||
| 89 | } |
||
| 90 | |||
| 91 | return $show_screen; |
||
| 92 | } |
||
| 93 | |||
| 94 | public static function manage_columns( $columns ) { |
||
| 95 | global $frm_vars; |
||
| 96 | $form_id = FrmForm::get_current_form_id(); |
||
| 97 | |||
| 98 | $columns[ $form_id . '_id' ] = 'ID'; |
||
| 99 | $columns[ $form_id . '_item_key' ] = esc_html__( 'Entry Key', 'formidable' ); |
||
| 100 | |||
| 101 | if ( $form_id ) { |
||
| 102 | self::get_columns_for_form( $form_id, $columns ); |
||
| 103 | } else { |
||
| 104 | $columns[ $form_id . '_form_id' ] = __( 'Form', 'formidable' ); |
||
| 105 | $columns[ $form_id . '_name' ] = __( 'Entry Name', 'formidable' ); |
||
| 106 | $columns[ $form_id . '_user_id' ] = __( 'Created By', 'formidable' ); |
||
| 107 | } |
||
| 108 | |||
| 109 | $columns[ $form_id . '_created_at' ] = __( 'Entry creation date', 'formidable' ); |
||
| 110 | $columns[ $form_id . '_updated_at' ] = __( 'Entry update date', 'formidable' ); |
||
| 111 | self::maybe_add_ip_col( $form_id, $columns ); |
||
| 112 | |||
| 113 | $frm_vars['cols'] = $columns; |
||
| 114 | |||
| 115 | $action = FrmAppHelper::simple_get( 'frm_action', 'sanitize_title' ); |
||
| 116 | if ( FrmAppHelper::is_admin_page( 'formidable-entries' ) && in_array( $action, array( '', 'list', 'destroy' ) ) ) { |
||
| 117 | add_screen_option( 'per_page', array( |
||
| 118 | 'label' => __( 'Entries', 'formidable' ), |
||
| 119 | 'default' => 20, |
||
| 120 | 'option' => 'formidable_page_formidable_entries_per_page', |
||
| 121 | ) ); |
||
| 122 | } |
||
| 123 | |||
| 124 | return $columns; |
||
| 125 | } |
||
| 126 | |||
| 127 | private static function get_columns_for_form( $form_id, &$columns ) { |
||
| 128 | $form_cols = FrmField::get_all_for_form( $form_id, '', 'include' ); |
||
| 129 | |||
| 130 | foreach ( $form_cols as $form_col ) { |
||
| 131 | if ( FrmField::is_no_save_field( $form_col->type ) ) { |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | |||
| 135 | if ( $form_col->type == 'form' && isset( $form_col->field_options['form_select'] ) && ! empty( $form_col->field_options['form_select'] ) ) { |
||
| 136 | $sub_form_cols = FrmField::get_all_for_form( $form_col->field_options['form_select'] ); |
||
| 137 | |||
| 138 | if ( $sub_form_cols ) { |
||
| 139 | foreach ( $sub_form_cols as $k => $sub_form_col ) { |
||
| 140 | if ( FrmField::is_no_save_field( $sub_form_col->type ) ) { |
||
| 141 | unset( $sub_form_cols[ $k ] ); |
||
| 142 | continue; |
||
| 143 | } |
||
| 144 | $columns[ $form_id . '_' . $sub_form_col->field_key . '-_-' . $form_col->id ] = FrmAppHelper::truncate( $sub_form_col->name, 35 ); |
||
| 145 | unset( $sub_form_col ); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | unset( $sub_form_cols ); |
||
| 149 | } else { |
||
| 150 | $col_id = $form_col->field_key; |
||
| 151 | if ( $form_col->form_id != $form_id ) { |
||
| 152 | $col_id .= '-_-form' . $form_col->form_id; |
||
| 153 | } |
||
| 154 | |||
| 155 | $has_separate_value = ! FrmField::is_option_empty( $form_col, 'separate_value' ); |
||
| 156 | $is_post_status = FrmField::is_option_true( $form_col, 'post_field' ) && $form_col->field_options['post_field'] == 'post_status'; |
||
| 157 | if ( $has_separate_value && ! $is_post_status ) { |
||
| 158 | $columns[ $form_id . '_frmsep_' . $col_id ] = FrmAppHelper::truncate( $form_col->name, 35 ); |
||
| 159 | } |
||
| 160 | $columns[ $form_id . '_' . $col_id ] = FrmAppHelper::truncate( $form_col->name, 35 ); |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | private static function maybe_add_ip_col( $form_id, &$columns ) { |
||
| 166 | if ( FrmAppHelper::ips_saved() ) { |
||
| 167 | $columns[ $form_id . '_ip' ] = 'IP'; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | |||
| 171 | public static function check_hidden_cols( $check, $object_id, $meta_key, $meta_value, $prev_value ) { |
||
| 172 | $this_page_name = self::hidden_column_key(); |
||
| 173 | if ( $meta_key != $this_page_name || $meta_value == $prev_value ) { |
||
| 174 | return $check; |
||
| 175 | } |
||
| 176 | |||
| 177 | if ( empty( $prev_value ) ) { |
||
| 178 | $prev_value = get_metadata( 'user', $object_id, $meta_key, true ); |
||
| 179 | } |
||
| 180 | |||
| 181 | global $frm_vars; |
||
| 182 | //add a check so we don't create a loop |
||
| 183 | $frm_vars['prev_hidden_cols'] = ( isset( $frm_vars['prev_hidden_cols'] ) && $frm_vars['prev_hidden_cols'] ) ? false : $prev_value; |
||
| 184 | |||
| 185 | return $check; |
||
| 186 | } |
||
| 187 | |||
| 188 | //add hidden columns back from other forms |
||
| 189 | public static function update_hidden_cols( $meta_id, $object_id, $meta_key, $meta_value ) { |
||
| 190 | $this_page_name = self::hidden_column_key(); |
||
| 191 | if ( $meta_key != $this_page_name ) { |
||
| 192 | return; |
||
| 193 | } |
||
| 194 | |||
| 195 | global $frm_vars; |
||
| 196 | if ( ! isset( $frm_vars['prev_hidden_cols'] ) || ! $frm_vars['prev_hidden_cols'] ) { |
||
| 197 | return; //don't continue if there's no previous value |
||
| 198 | } |
||
| 199 | |||
| 200 | foreach ( $meta_value as $mk => $mv ) { |
||
| 201 | //remove blank values |
||
| 202 | if ( empty( $mv ) ) { |
||
| 203 | unset( $meta_value[ $mk ] ); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | $cur_form_prefix = reset( $meta_value ); |
||
| 208 | $cur_form_prefix = explode( '_', $cur_form_prefix ); |
||
| 209 | $cur_form_prefix = $cur_form_prefix[0]; |
||
| 210 | $save = false; |
||
| 211 | |||
| 212 | foreach ( (array) $frm_vars['prev_hidden_cols'] as $prev_hidden ) { |
||
| 213 | if ( empty( $prev_hidden ) || in_array( $prev_hidden, $meta_value ) ) { |
||
| 214 | //don't add blank cols or process included cols |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | |||
| 218 | $form_prefix = explode( '_', $prev_hidden ); |
||
| 219 | $form_prefix = $form_prefix[0]; |
||
| 220 | if ( $form_prefix == $cur_form_prefix ) { |
||
| 221 | //don't add back columns that are meant to be hidden |
||
| 222 | continue; |
||
| 223 | } |
||
| 224 | |||
| 225 | $meta_value[] = $prev_hidden; |
||
| 226 | $save = true; |
||
| 227 | unset( $form_prefix ); |
||
| 228 | } |
||
| 229 | |||
| 230 | if ( $save ) { |
||
| 231 | $user_id = get_current_user_id(); |
||
| 232 | update_user_option( $user_id, $this_page_name, $meta_value, true ); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @since 2.05.07 |
||
| 238 | */ |
||
| 239 | private static function hidden_column_key( $menu_name = '' ) { |
||
| 240 | $base = self::base_column_key( $menu_name ); |
||
| 241 | return 'manage' . $base . 'columnshidden'; |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @since 2.05.07 |
||
| 246 | */ |
||
| 247 | private static function base_column_key( $menu_name = '' ) { |
||
| 248 | if ( empty( $menu_name ) ) { |
||
| 249 | $menu_name = FrmAppHelper::get_menu_name(); |
||
| 250 | } |
||
| 251 | return sanitize_title( $menu_name ) . '_page_formidable-entries'; |
||
| 252 | } |
||
| 253 | |||
| 254 | public static function save_per_page( $save, $option, $value ) { |
||
| 255 | if ( $option == 'formidable_page_formidable_entries_per_page' ) { |
||
| 256 | $save = (int) $value; |
||
| 257 | } |
||
| 258 | return $save; |
||
| 259 | } |
||
| 260 | |||
| 261 | public static function sortable_columns() { |
||
| 262 | $form_id = FrmForm::get_current_form_id(); |
||
| 263 | $fields = FrmField::get_all_for_form( $form_id ); |
||
| 264 | |||
| 265 | $columns = array( |
||
| 266 | $form_id . '_id' => 'id', |
||
| 267 | $form_id . '_created_at' => 'created_at', |
||
| 268 | $form_id . '_updated_at' => 'updated_at', |
||
| 269 | $form_id . '_ip' => 'ip', |
||
| 270 | $form_id . '_item_key' => 'item_key', |
||
| 271 | $form_id . '_is_draft' => 'is_draft', |
||
| 272 | ); |
||
| 273 | |||
| 274 | foreach ( $fields as $field ) { |
||
| 275 | if ( $field->type != 'checkbox' && ( ! isset( $field->field_options['post_field'] ) || $field->field_options['post_field'] == '' ) ) { |
||
| 276 | // Can't sort on checkboxes because they are stored serialized, or post fields |
||
| 277 | $columns[ $form_id . '_' . $field->field_key ] = 'meta_' . $field->id; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | return $columns; |
||
| 282 | } |
||
| 283 | |||
| 284 | public static function hidden_columns( $result ) { |
||
| 285 | $form_id = FrmForm::get_current_form_id(); |
||
| 286 | |||
| 287 | $hidden = self::user_hidden_columns_for_form( $form_id, $result ); |
||
| 288 | |||
| 289 | global $frm_vars; |
||
| 290 | $i = isset( $frm_vars['cols'] ) ? count( $frm_vars['cols'] ) : 0; |
||
| 291 | |||
| 292 | if ( ! empty( $hidden ) ) { |
||
| 293 | $result = $hidden; |
||
| 294 | $i = $i - count( $result ); |
||
| 295 | $max_columns = 11; |
||
| 296 | } else { |
||
| 297 | $max_columns = 8; |
||
| 298 | } |
||
| 299 | |||
| 300 | if ( $i <= $max_columns ) { |
||
| 301 | return $result; |
||
| 302 | } |
||
| 303 | |||
| 304 | self::remove_excess_cols( compact( 'i', 'max_columns', 'form_id' ), $result ); |
||
| 305 | |||
| 306 | return $result; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @since 2.05.07 |
||
| 311 | */ |
||
| 312 | private static function user_hidden_columns_for_form( $form_id, $result ) { |
||
| 313 | $hidden = array(); |
||
| 314 | foreach ( (array) $result as $r ) { |
||
| 315 | if ( ! empty( $r ) ) { |
||
| 316 | list( $form_prefix, $field_key ) = explode( '_', $r ); |
||
| 317 | |||
| 318 | if ( (int) $form_prefix == (int) $form_id ) { |
||
| 319 | $hidden[] = $r; |
||
| 320 | } |
||
| 321 | |||
| 322 | unset( $form_prefix ); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | return $hidden; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Remove some columns by default when there are too many |
||
| 330 | * |
||
| 331 | * @since 2.05.07 |
||
| 332 | */ |
||
| 333 | private static function remove_excess_cols( $atts, &$result ) { |
||
| 334 | global $frm_vars; |
||
| 335 | |||
| 336 | $remove_first = array( |
||
| 337 | $atts['form_id'] . '_item_key' => '', |
||
| 338 | $atts['form_id'] . '_id' => '', |
||
| 339 | ); |
||
| 340 | $cols = $remove_first + array_reverse( $frm_vars['cols'], true ); |
||
| 341 | |||
| 342 | $i = $atts['i']; |
||
| 343 | |||
| 344 | foreach ( $cols as $col_key => $col ) { |
||
| 345 | if ( $i <= $atts['max_columns'] ) { |
||
| 346 | break; |
||
| 347 | } |
||
| 348 | |||
| 349 | if ( empty( $result ) || ! in_array( $col_key, $result, true ) ) { |
||
| 350 | $result[] = $col_key; |
||
| 351 | $i--; |
||
| 352 | } |
||
| 353 | |||
| 354 | unset( $col_key, $col ); |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 358 | public static function display_list( $message = '', $errors = array() ) { |
||
| 359 | global $wpdb, $frm_vars; |
||
| 360 | |||
| 361 | $form = FrmForm::maybe_get_current_form(); |
||
| 362 | $params = FrmForm::get_admin_params( $form ); |
||
| 363 | |||
| 364 | if ( $form ) { |
||
| 365 | $params['form'] = $form->id; |
||
| 366 | $frm_vars['current_form'] = $form; |
||
| 367 | |||
| 368 | self::get_delete_form_time( $form, $errors ); |
||
| 369 | } |
||
| 370 | |||
| 371 | $table_class = apply_filters( 'frm_entries_list_class', 'FrmEntriesListHelper' ); |
||
| 372 | |||
| 373 | $wp_list_table = new $table_class( array( 'params' => $params ) ); |
||
| 374 | |||
| 375 | $pagenum = $wp_list_table->get_pagenum(); |
||
| 376 | |||
| 377 | $wp_list_table->prepare_items(); |
||
| 378 | |||
| 379 | $total_pages = $wp_list_table->get_pagination_arg( 'total_pages' ); |
||
| 380 | if ( $pagenum > $total_pages && $total_pages > 0 ) { |
||
| 381 | $url = add_query_arg( 'paged', $total_pages ); |
||
| 382 | if ( headers_sent() ) { |
||
| 383 | echo FrmAppHelper::js_redirect( $url ); |
||
| 384 | } else { |
||
| 385 | wp_redirect( esc_url_raw( $url ) ); |
||
| 386 | } |
||
| 387 | die(); |
||
| 388 | } |
||
| 389 | |||
| 390 | if ( empty( $message ) && isset( $_GET['import-message'] ) ) { |
||
| 391 | $message = __( 'Your import is complete', 'formidable' ); |
||
| 392 | } |
||
| 393 | |||
| 394 | require( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/list.php' ); |
||
| 395 | } |
||
| 396 | |||
| 397 | private static function get_delete_form_time( $form, &$errors ) { |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /* Back End CRUD */ |
||
| 406 | public static function show( $id = 0 ) { |
||
| 407 | FrmAppHelper::permission_check( 'frm_view_entries' ); |
||
| 408 | |||
| 409 | if ( ! $id ) { |
||
| 410 | $id = FrmAppHelper::get_param( 'id', 0, 'get', 'absint' ); |
||
| 411 | |||
| 412 | if ( ! $id ) { |
||
| 413 | $id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'absint' ); |
||
| 414 | } |
||
| 415 | } |
||
| 416 | |||
| 417 | $entry = FrmEntry::getOne( $id, true ); |
||
| 418 | if ( ! $entry ) { |
||
| 419 | echo '<div id="form_show_entry_page" class="wrap">' . |
||
| 420 | __( 'You are trying to view an entry that does not exist.', 'formidable' ) . |
||
| 421 | '</div>'; |
||
| 422 | return; |
||
| 423 | } |
||
| 424 | |||
| 425 | $data = maybe_unserialize( $entry->description ); |
||
| 426 | if ( ! is_array( $data ) || ! isset( $data['referrer'] ) ) { |
||
| 427 | $data = array( 'referrer' => $data ); |
||
| 428 | } |
||
| 429 | |||
| 430 | $fields = FrmField::get_all_for_form( $entry->form_id, '', 'include' ); |
||
| 431 | $to_emails = array(); |
||
| 432 | $form = FrmForm::getOne( $entry->form_id ); |
||
| 433 | |||
| 434 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/show.php' ); |
||
| 435 | } |
||
| 436 | |||
| 437 | public static function destroy() { |
||
| 438 | FrmAppHelper::permission_check( 'frm_delete_entries' ); |
||
| 439 | |||
| 440 | $params = FrmForm::get_admin_params(); |
||
| 441 | |||
| 442 | if ( isset( $params['keep_post'] ) && $params['keep_post'] ) { |
||
| 443 | self::unlink_post( $params['id'] ); |
||
| 444 | } |
||
| 445 | |||
| 446 | $message = ''; |
||
| 447 | if ( FrmEntry::destroy( $params['id'] ) ) { |
||
| 448 | $message = __( 'Entry was Successfully Destroyed', 'formidable' ); |
||
| 449 | } |
||
| 450 | |||
| 451 | self::display_list( $message ); |
||
| 452 | } |
||
| 453 | |||
| 454 | public static function destroy_all() { |
||
| 455 | if ( ! current_user_can( 'frm_delete_entries' ) ) { |
||
| 456 | $frm_settings = FrmAppHelper::get_settings(); |
||
| 457 | wp_die( $frm_settings->admin_permission ); |
||
| 458 | } |
||
| 459 | |||
| 460 | global $wpdb; |
||
| 461 | $params = FrmForm::get_admin_params(); |
||
| 462 | $message = ''; |
||
| 463 | $errors = array(); |
||
| 464 | $form_id = (int) $params['form']; |
||
| 465 | |||
| 466 | if ( $form_id ) { |
||
| 467 | $entry_ids = FrmDb::get_col( 'frm_items', array( 'form_id' => $form_id ) ); |
||
| 468 | $action = FrmFormAction::get_action_for_form( $form_id, 'wppost', 1 ); |
||
| 469 | |||
| 470 | if ( $action ) { |
||
| 471 | // this action takes a while, so only trigger it if there are posts to delete |
||
| 472 | foreach ( $entry_ids as $entry_id ) { |
||
| 473 | do_action( 'frm_before_destroy_entry', $entry_id ); |
||
| 474 | unset( $entry_id ); |
||
| 475 | } |
||
| 476 | } |
||
| 477 | |||
| 478 | $wpdb->query( $wpdb->prepare( "DELETE em.* FROM {$wpdb->prefix}frm_item_metas as em INNER JOIN {$wpdb->prefix}frm_items as e on (em.item_id=e.id) and form_id=%d", $form_id ) ); |
||
| 479 | $results = $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->prefix}frm_items WHERE form_id=%d", $form_id ) ); |
||
| 480 | if ( $results ) { |
||
| 481 | FrmEntry::clear_cache(); |
||
| 482 | $message = __( 'Entries were Successfully Destroyed', 'formidable' ); |
||
| 483 | } |
||
| 484 | } else { |
||
| 485 | $errors = __( 'No entries were specified', 'formidable' ); |
||
| 486 | } |
||
| 487 | |||
| 488 | self::display_list( $message, $errors ); |
||
| 489 | } |
||
| 490 | |||
| 491 | public static function show_form( $id = '', $key = '', $title = false, $description = false ) { |
||
| 492 | _deprecated_function( __FUNCTION__, '1.07.05', 'FrmFormsController::show_form()' ); |
||
| 493 | return FrmFormsController::show_form( $id, $key, $title, $description ); |
||
| 494 | } |
||
| 495 | |||
| 496 | public static function get_form( $filename, $form, $title, $description ) { |
||
| 499 | } |
||
| 500 | |||
| 501 | public static function process_entry( $errors = '', $ajax = false ) { |
||
| 502 | $form_id = FrmAppHelper::get_post_param( 'form_id', '', 'absint' ); |
||
| 503 | if ( FrmAppHelper::is_admin() || empty( $_POST ) || empty( $form_id ) || ! isset( $_POST['item_key'] ) ) { |
||
| 504 | return; |
||
| 505 | } |
||
| 506 | |||
| 507 | global $frm_vars; |
||
| 508 | |||
| 509 | $form = FrmForm::getOne( $form_id ); |
||
| 510 | if ( ! $form ) { |
||
| 511 | return; |
||
| 512 | } |
||
| 513 | |||
| 514 | $params = FrmForm::get_params( $form ); |
||
| 515 | |||
| 516 | if ( ! isset( $frm_vars['form_params'] ) ) { |
||
| 517 | $frm_vars['form_params'] = array(); |
||
| 518 | } |
||
| 519 | $frm_vars['form_params'][ $form->id ] = $params; |
||
| 520 | |||
| 521 | if ( isset( $frm_vars['created_entries'][ $form_id ] ) ) { |
||
| 522 | return; |
||
| 523 | } |
||
| 524 | |||
| 525 | if ( $errors == '' && ! $ajax ) { |
||
| 526 | $errors = FrmEntryValidate::validate( $_POST ); |
||
| 527 | } |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Use this filter to add trigger actions and add errors after |
||
| 531 | * all other errors have been processed |
||
| 532 | * @since 2.0.6 |
||
| 533 | */ |
||
| 534 | $errors = apply_filters( 'frm_entries_before_create', $errors, $form ); |
||
| 535 | |||
| 536 | $frm_vars['created_entries'][ $form_id ] = array( 'errors' => $errors ); |
||
| 537 | |||
| 538 | if ( empty( $errors ) ) { |
||
| 539 | $_POST['frm_skip_cookie'] = 1; |
||
| 540 | $do_success = false; |
||
| 541 | if ( $params['action'] == 'create' ) { |
||
| 542 | if ( apply_filters( 'frm_continue_to_create', true, $form_id ) && ! isset( $frm_vars['created_entries'][ $form_id ]['entry_id'] ) ) { |
||
| 543 | $frm_vars['created_entries'][ $form_id ]['entry_id'] = FrmEntry::create( $_POST ); |
||
| 544 | $params['id'] = $frm_vars['created_entries'][ $form_id ]['entry_id']; |
||
| 545 | $do_success = true; |
||
| 546 | } |
||
| 547 | } |
||
| 548 | |||
| 549 | do_action( 'frm_process_entry', $params, $errors, $form, array( 'ajax' => $ajax ) ); |
||
| 550 | if ( $do_success ) { |
||
| 551 | FrmFormsController::maybe_trigger_redirect( $form, $params, array( 'ajax' => $ajax ) ); |
||
| 552 | } |
||
| 553 | unset( $_POST['frm_skip_cookie'] ); |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Escape url entities before redirect |
||
| 559 | * |
||
| 560 | * @since 3.0 |
||
| 561 | * |
||
| 562 | * @param string $url |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public static function prepare_redirect_url( $url ) { |
||
| 566 | return str_replace( array( ' ', '[', ']', '|', '@' ), array( '%20', '%5B', '%5D', '%7C', '%40' ), $url ); |
||
| 567 | } |
||
| 568 | |||
| 569 | public static function delete_entry_before_redirect( $url, $form, $atts ) { |
||
| 570 | self::_delete_entry( $atts['id'], $form ); |
||
| 571 | return $url; |
||
| 572 | } |
||
| 573 | |||
| 574 | //Delete entry if not redirected |
||
| 575 | public static function delete_entry_after_save( $atts ) { |
||
| 576 | self::_delete_entry( $atts['entry_id'], $atts['form'] ); |
||
| 577 | } |
||
| 578 | |||
| 579 | private static function _delete_entry( $entry_id, $form ) { |
||
| 580 | if ( ! $form ) { |
||
| 581 | return; |
||
| 582 | } |
||
| 583 | |||
| 584 | $form->options = maybe_unserialize( $form->options ); |
||
| 585 | if ( isset( $form->options['no_save'] ) && $form->options['no_save'] ) { |
||
| 586 | self::unlink_post( $entry_id ); |
||
| 587 | FrmEntry::destroy( $entry_id ); |
||
| 588 | } |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * unlink entry from post |
||
| 593 | */ |
||
| 594 | private static function unlink_post( $entry_id ) { |
||
| 595 | global $wpdb; |
||
| 596 | $wpdb->update( $wpdb->prefix . 'frm_items', array( 'post_id' => '' ), array( 'id' => $entry_id ) ); |
||
| 597 | FrmEntry::clear_cache(); |
||
| 598 | } |
||
| 599 | |||
| 600 | /** |
||
| 601 | * @param $atts |
||
| 602 | * |
||
| 603 | * @return array|string |
||
| 604 | */ |
||
| 605 | public static function show_entry_shortcode( $atts ) { |
||
| 650 | } |
||
| 651 | |||
| 652 | public static function entry_sidebar( $entry ) { |
||
| 653 | $data = maybe_unserialize( $entry->description ); |
||
| 654 | $date_format = get_option( 'date_format' ); |
||
| 655 | $time_format = get_option( 'time_format' ); |
||
| 656 | if ( isset( $data['browser'] ) ) { |
||
| 657 | $browser = FrmEntriesHelper::get_browser( $data['browser'] ); |
||
| 658 | } |
||
| 659 | |||
| 660 | include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/sidebar-shared.php' ); |
||
| 661 | } |
||
| 662 | } |
||
| 663 |