Completed
Pull Request — master (#299)
by
unknown
02:50
created

FrmEntriesListHelper   F

Complexity

Total Complexity 60

Size/Duplication

Total Lines 362
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 362
rs 3.6
c 0
b 0
f 0
wmc 60
lcom 2
cbo 8

12 Methods

Rating   Name   Duplication   Size   Complexity  
B prepare_items() 0 74 6
A no_items() 0 30 4
A search_box() 0 3 1
A display_tablenav() 0 14 3
A extra_tablenav() 0 13 3
A get_primary_column_name() 0 15 4
C single_row() 0 63 11
A get_action_columns() 0 10 2
C column_value() 0 49 17
A get_actions() 0 12 2
B get_column_value() 0 38 6
A confirm_bulk_delete() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like FrmEntriesListHelper 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 FrmEntriesListHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmEntriesListHelper extends FrmListHelper {
7
	protected $column_name;
8
	protected $item;
9
	protected $field;
10
11
	/**
12
	 * @since 4.07
13
	 */
14
	public $total_items = 0;
15
16
	public function prepare_items() {
17
		global $per_page;
18
19
		$per_page = $this->get_items_per_page( 'formidable_page_formidable_entries_per_page' );
20
		$form_id  = $this->params['form'];
21
22
		$s_query = array();
23
24
		if ( $form_id ) {
25
			$s_query['it.form_id'] = $form_id;
26
			$join_form_in_query    = false;
27
		} else {
28
			$s_query[]          = array(
29
				'or'               => 1,
30
				'parent_form_id'   => null,
31
				'parent_form_id <' => 1,
32
			);
33
			$join_form_in_query = true;
34
		}
35
36
		$s = self::get_param(
37
			array(
38
				'param'    => 's',
39
				'sanitize' => 'sanitize_text_field',
40
			)
41
		);
42
43
		if ( $s != '' && FrmAppHelper::pro_is_installed() ) {
44
			$fid     = self::get_param( array( 'param' => 'fid' ) );
45
			$s_query = FrmProEntriesHelper::get_search_str( $s_query, $s, $form_id, $fid );
46
		}
47
48
		$s_query = apply_filters( 'frm_entries_list_query', $s_query, compact( 'form_id' ) );
49
50
		$orderby = self::get_param(
51
			array(
52
				'param'   => 'orderby',
53
				'default' => 'id',
54
			)
55
		);
56
57
		if ( strpos( $orderby, 'meta' ) !== false ) {
58
			$order_field_type = FrmField::get_type( str_replace( 'meta_', '', $orderby ) );
59
			$orderby          .= in_array( $order_field_type, array( 'number', 'scale', 'star' ) ) ? '+0' : '';
60
		}
61
62
		$order = self::get_param(
63
			array(
64
				'param'   => 'order',
65
				'default' => 'DESC',
66
			)
67
		);
68
		$order = FrmDb::esc_order( $orderby . ' ' . $order );
69
70
		$page  = $this->get_pagenum();
71
		$start = (int) self::get_param(
72
			array(
73
				'param'   => 'start',
74
				'default' => ( $page - 1 ) * $per_page,
75
			)
76
		);
77
78
		$limit       = FrmDb::esc_limit( $start . ',' . $per_page );
79
		$this->items = FrmEntry::getAll( $s_query, $order, $limit, true, $join_form_in_query );
80
		$total_items = FrmEntry::getRecordCount( $s_query );
81
		$this->total_items = $total_items;
82
83
		$this->set_pagination_args(
84
			array(
85
				'total_items' => $total_items,
86
				'per_page'    => $per_page,
87
			)
88
		);
89
	}
90
91
	public function no_items() {
92
		$s = self::get_param(
93
			array(
94
				'param'    => 's',
95
				'sanitize' => 'sanitize_text_field',
96
			)
97
		);
98
		if ( ! empty( $s ) ) {
99
			esc_html_e( 'No Entries Found', 'formidable' );
100
101
			return;
102
		}
103
104
		$form_id = $this->params['form'];
105
		$form    = $this->params['form'];
106
107
		if ( $form_id ) {
108
			$form = FrmForm::getOne( $form_id );
109
		}
110
		$has_form = ! empty( $form );
111
112
		if ( ! $has_form ) {
113
			$has_form = FrmForm::getAll( array(), '', 1 );
114
			$has_form = ! empty( $has_form );
115
		}
116
117
		$colspan = $this->get_column_count();
118
119
		include( FrmAppHelper::plugin_path() . '/classes/views/frm-entries/no_entries.php' );
120
	}
121
122
	public function search_box( $text, $input_id ) {
123
		// Searching is a pro feature
124
	}
125
126
	protected function display_tablenav( $which ) {
127
		$is_footer = ( $which !== 'top' );
128
		if ( $is_footer && ! empty( $this->items ) ) {
129
			?>
130
			<p>
131
				<?php esc_html_e( 'Getting spam form submissions?', 'formidable' ); ?>
132
				<a href="https://formidableforms.com/knowledgebase/add-spam-protection/" target="_blank">
133
					<?php esc_html_e( 'Learn how to prevent them.', 'formidable' ); ?>
134
				</a>
135
			</p>
136
			<?php
137
		}
138
		parent::display_tablenav( $which );
139
	}
140
141
	protected function extra_tablenav( $which ) {
142
		$form_id = FrmAppHelper::simple_get( 'form', 'absint' );
143
		if ( $which == 'top' && empty( $form_id ) ) {
144
			echo '<div class="alignleft actions">';
145
146
			// Override the referrer to prevent it from being used for the screen options.
147
			echo '<input type="hidden" name="_wp_http_referer" value="" />';
148
149
			echo FrmFormsHelper::forms_dropdown( 'form', $form_id, array( 'blank' => __( 'View all forms', 'formidable' ) ) ); // WPCS: XSS ok.
150
			submit_button( __( 'Filter', 'formidable' ), 'filter_action action', '', false, array( 'id' => 'post-query-submit' ) );
151
			echo '</div>';
152
		}
153
	}
154
155
	/**
156
	 * Gets the name of the primary column in the Entries screen
157
	 *
158
	 * @since 2.0.14
159
	 *
160
	 * @return string $primary_column
161
	 */
162
	protected function get_primary_column_name() {
163
		$columns = get_column_headers( $this->screen );
164
		$hidden  = get_hidden_columns( $this->screen );
165
166
		$primary_column = '';
167
168
		foreach ( $columns as $column_key => $column_display_name ) {
169
			if ( 'cb' != $column_key && ! in_array( $column_key, $hidden ) ) {
170
				$primary_column = $column_key;
171
				break;
172
			}
173
		}
174
175
		return $primary_column;
176
	}
177
178
	public function single_row( $item, $style = '' ) {
179
		// Set up the hover actions for this user
180
		$actions   = array();
181
		$view_link = '?page=formidable-entries&frm_action=show&id=' . $item->id;
182
183
		$this->get_actions( $actions, $item, $view_link );
184
185
		$action_links = $this->row_actions( $actions );
186
187
		// Set up the checkbox ( because the user is editable, otherwise its empty )
188
		$checkbox = "<input type='checkbox' name='item-action[]' id='cb-item-action-{$item->id}' value='{$item->id}' />";
189
190
		$r = "<tr id='item-action-{$item->id}'$style>";
191
192
		list( $columns, $hidden, , $primary ) = $this->get_column_info();
193
		$action_col                           = false;
194
		$action_columns                       = $this->get_action_columns();
195
196
		foreach ( $columns as $column_name => $column_display_name ) {
197
			$class = $column_name . ' column-' . $column_name;
198
199
			if ( $column_name === $primary ) {
200
				$class .= ' column-primary';
201
			}
202
203
			if ( in_array( $column_name, $hidden, true ) ) {
204
				$class .= ' frm_hidden';
205
			} elseif ( ! $action_col && ! in_array( $column_name, $action_columns, true ) ) {
206
				$action_col = $column_name;
207
			}
208
209
			$attributes = 'class="' . esc_attr( $class ) . '"';
210
			unset( $class );
211
			$attributes .= ' data-colname="' . $column_display_name . '"';
212
213
			$form_id           = $this->params['form'] ? $this->params['form'] : 0;
214
			$this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name );
215
216
			if ( $this->column_name == 'cb' ) {
217
				$r .= "<th scope='row' class='check-column'>$checkbox</th>";
218
			} else {
219
				if ( in_array( $column_name, $hidden, true ) ) {
220
					$val = '';
221
				} else {
222
					$val = $this->column_value( $item );
223
				}
224
225
				$r .= "<td $attributes>";
226
				if ( $column_name == $action_col ) {
227
					$edit_link = FrmAppHelper::maybe_full_screen_link( '?page=formidable-entries&frm_action=edit&id=' . $item->id );
228
					$r         .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
229
					$r         .= $action_links;
230
				} else {
231
					$r .= $val;
232
				}
233
				$r .= '</td>';
234
			}
235
			unset( $val );
236
		}
237
		$r .= '</tr>';
238
239
		return $r;
240
	}
241
242
	/**
243
	 * Get the column names that the logged in user can action on
244
	 */
245
	private function get_action_columns() {
246
		$columns = array();
247
248
		$user_can_edit_forms = false === FrmAppHelper::permission_nonce_error( 'frm_edit_forms' );
249
		if ( $user_can_edit_forms ) {
250
			array_push( $columns, 'cb', 'form_id', 'id', 'post_id' );
251
		}
252
253
		return $columns;
254
	}
255
256
	private function column_value( $item ) {
257
		$col_name = $this->column_name;
258
259
		switch ( $col_name ) {
260
			case 'ip':
261
			case 'id':
262
			case 'item_key':
263
				$val = $item->{$col_name};
264
				break;
265
			case 'name':
266
			case 'description':
267
				$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
268
				break;
269
			case 'created_at':
270
			case 'updated_at':
271
				$date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
272
				$val  = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
273
				break;
274
			case 'is_draft':
275
				$val = empty( $item->is_draft ) ? esc_html__( 'No', 'formidable' ) : esc_html__( 'Yes', 'formidable' );
276
				break;
277
			case 'form_id':
278
				$form_id             = $item->form_id;
279
				$user_can_edit_forms = false === FrmAppHelper::permission_nonce_error( 'frm_edit_forms' );
280
				if ( $user_can_edit_forms ) {
281
					$val = FrmFormsHelper::edit_form_link( $form_id );
282
				} else {
283
					$val = FrmFormsHelper::edit_form_link_label( $form_id );
284
				}
285
				break;
286
			case 'post_id':
287
				$val = FrmAppHelper::post_edit_link( $item->post_id );
288
				break;
289
			case 'user_id':
290
				$user = get_userdata( $item->user_id );
291
				$val  = $user ? $user->user_login : '';
292
				break;
293
			case 'parent_item_id':
294
				$val = $item->parent_item_id;
295
				break;
296
			default:
297
				$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
298
				if ( $val === false ) {
299
					$this->get_column_value( $item, $val );
300
				}
301
		}
302
303
		return $val;
304
	}
305
306
	/**
307
	 * @param string $view_link
308
	 */
309
	private function get_actions( &$actions, $item, $view_link ) {
310
		$view_link = FrmAppHelper::maybe_full_screen_link( $view_link );
311
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
312
313
		if ( current_user_can( 'frm_delete_entries' ) ) {
314
			$delete_link       = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
315
			$delete_link       = FrmAppHelper::maybe_full_screen_link( $delete_link );
316
			$actions['delete'] = '<a href="' . esc_url( wp_nonce_url( $delete_link ) ) . '" class="submitdelete" data-frmverify="' . esc_attr__( 'Permanently delete this entry?', 'formidable' ) . '">' . __( 'Delete', 'formidable' ) . '</a>';
317
		}
318
319
		$actions = apply_filters( 'frm_row_actions', $actions, $item );
320
	}
321
322
	private function get_column_value( $item, &$val ) {
323
		$col_name = $this->column_name;
324
325
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
326
			$sep_val  = true;
327
			$col_name = str_replace( 'frmsep_', '', $col_name );
328
		} else {
329
			$sep_val = false;
330
		}
331
332
		if ( strpos( $col_name, '-_-' ) ) {
333
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
334
		}
335
336
		$field = FrmField::getOne( $col_name );
337
		if ( ! $field ) {
338
			return;
339
		}
340
341
		$atts = array(
342
			'type'              => $field->type,
343
			'truncate'          => true,
344
			'post_id'           => $item->post_id,
345
			'entry_id'          => $item->id,
346
			'embedded_field_id' => 0,
347
		);
348
349
		if ( $sep_val ) {
350
			$atts['saved_value'] = true;
351
		}
352
353
		if ( isset( $embedded_field_id ) ) {
354
			$atts['embedded_field_id'] = $embedded_field_id;
355
			unset( $embedded_field_id );
356
		}
357
358
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
359
	}
360
361
	/**
362
	 * @return string
363
	 */
364
	protected function confirm_bulk_delete() {
365
		return __( 'ALL selected entries in this form will be permanently deleted. Want to proceed?', 'formidable' );
366
	}
367
}
368