Completed
Push — master ( 1e1c87...a1ea0d )
by Stephanie
02:47
created

FrmEntriesListHelper   C

Complexity

Total Complexity 57

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 8

Importance

Changes 0
Metric Value
dl 0
loc 341
rs 5.04
c 0
b 0
f 0
wmc 57
lcom 2
cbo 8

11 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 62 11
C column_value() 0 43 16
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
195
		foreach ( $columns as $column_name => $column_display_name ) {
196
			$class = $column_name . ' column-' . $column_name;
197
198
			if ( $column_name === $primary ) {
199
				$class .= ' column-primary';
200
			}
201
202
			if ( in_array( $column_name, $hidden ) ) {
203
				$class .= ' frm_hidden';
204
			} elseif ( ! $action_col && ! in_array( $column_name, array( 'cb', 'id', 'form_id', 'post_id' ) ) ) {
205
				$action_col = $column_name;
206
			}
207
208
			$attributes = 'class="' . esc_attr( $class ) . '"';
209
			unset( $class );
210
			$attributes .= ' data-colname="' . $column_display_name . '"';
211
212
			$form_id           = $this->params['form'] ? $this->params['form'] : 0;
213
			$this->column_name = preg_replace( '/^(' . $form_id . '_)/', '', $column_name );
214
215
			if ( $this->column_name == 'cb' ) {
216
				$r .= "<th scope='row' class='check-column'>$checkbox</th>";
217
			} else {
218
				if ( in_array( $column_name, $hidden ) ) {
219
					$val = '';
220
				} else {
221
					$val = $this->column_value( $item );
222
				}
223
224
				$r .= "<td $attributes>";
225
				if ( $column_name == $action_col ) {
226
					$edit_link = FrmAppHelper::maybe_full_screen_link( '?page=formidable-entries&frm_action=edit&id=' . $item->id );
227
					$r         .= '<a href="' . esc_url( isset( $actions['edit'] ) ? $edit_link : $view_link ) . '" class="row-title" >' . $val . '</a> ';
228
					$r         .= $action_links;
229
				} else {
230
					$r .= $val;
231
				}
232
				$r .= '</td>';
233
			}
234
			unset( $val );
235
		}
236
		$r .= '</tr>';
237
238
		return $r;
239
	}
240
241
	private function column_value( $item ) {
242
		$col_name = $this->column_name;
243
244
		switch ( $col_name ) {
245
			case 'ip':
246
			case 'id':
247
			case 'item_key':
248
				$val = $item->{$col_name};
249
				break;
250
			case 'name':
251
			case 'description':
252
				$val = FrmAppHelper::truncate( strip_tags( $item->{$col_name} ), 100 );
253
				break;
254
			case 'created_at':
255
			case 'updated_at':
256
				$date = FrmAppHelper::get_formatted_time( $item->{$col_name} );
257
				$val  = '<abbr title="' . esc_attr( FrmAppHelper::get_formatted_time( $item->{$col_name}, '', 'g:i:s A' ) ) . '">' . $date . '</abbr>';
258
				break;
259
			case 'is_draft':
260
				$val = empty( $item->is_draft ) ? esc_html__( 'No', 'formidable' ) : esc_html__( 'Yes', 'formidable' );
261
				break;
262
			case 'form_id':
263
				$val = FrmFormsHelper::edit_form_link( $item->form_id );
264
				break;
265
			case 'post_id':
266
				$val = FrmAppHelper::post_edit_link( $item->post_id );
267
				break;
268
			case 'user_id':
269
				$user = get_userdata( $item->user_id );
270
				$val  = $user ? $user->user_login : '';
271
				break;
272
			case 'parent_item_id':
273
				$val = $item->parent_item_id;
274
				break;
275
			default:
276
				$val = apply_filters( 'frm_entries_' . $col_name . '_column', false, compact( 'item' ) );
277
				if ( $val === false ) {
278
					$this->get_column_value( $item, $val );
279
				}
280
		}
281
282
		return $val;
283
	}
284
285
	/**
286
	 * @param string $view_link
287
	 */
288
	private function get_actions( &$actions, $item, $view_link ) {
289
		$view_link = FrmAppHelper::maybe_full_screen_link( $view_link );
290
		$actions['view'] = '<a href="' . esc_url( $view_link ) . '">' . __( 'View', 'formidable' ) . '</a>';
291
292
		if ( current_user_can( 'frm_delete_entries' ) ) {
293
			$delete_link       = '?page=formidable-entries&frm_action=destroy&id=' . $item->id . '&form=' . $this->params['form'];
294
			$delete_link       = FrmAppHelper::maybe_full_screen_link( $delete_link );
295
			$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>';
296
		}
297
298
		$actions = apply_filters( 'frm_row_actions', $actions, $item );
299
	}
300
301
	private function get_column_value( $item, &$val ) {
302
		$col_name = $this->column_name;
303
304
		if ( strpos( $col_name, 'frmsep_' ) === 0 ) {
305
			$sep_val  = true;
306
			$col_name = str_replace( 'frmsep_', '', $col_name );
307
		} else {
308
			$sep_val = false;
309
		}
310
311
		if ( strpos( $col_name, '-_-' ) ) {
312
			list( $col_name, $embedded_field_id ) = explode( '-_-', $col_name );
313
		}
314
315
		$field = FrmField::getOne( $col_name );
316
		if ( ! $field ) {
317
			return;
318
		}
319
320
		$atts = array(
321
			'type'              => $field->type,
322
			'truncate'          => true,
323
			'post_id'           => $item->post_id,
324
			'entry_id'          => $item->id,
325
			'embedded_field_id' => 0,
326
		);
327
328
		if ( $sep_val ) {
329
			$atts['saved_value'] = true;
330
		}
331
332
		if ( isset( $embedded_field_id ) ) {
333
			$atts['embedded_field_id'] = $embedded_field_id;
334
			unset( $embedded_field_id );
335
		}
336
337
		$val = FrmEntriesHelper::prepare_display_value( $item, $field, $atts );
338
	}
339
340
	/**
341
	 * @return string
342
	 */
343
	protected function confirm_bulk_delete() {
344
		return __( 'ALL selected entries in this form will be permanently deleted. Want to proceed?', 'formidable' );
345
	}
346
}
347