Completed
Push — master ( 1466b8...74a3d9 )
by Stephanie
14s
created

FrmFormsListHelper::extra_tablenav()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmFormsListHelper extends FrmListHelper {
7
	public $status = '';
8
9
	public function __construct( $args ) {
10
		$this->status = self::get_param( array( 'param' => 'form_type' ) );
0 ignored issues
show
Documentation Bug introduced by
It seems like self::get_param(array('param' => 'form_type')) can also be of type array. However, the property $status is declared as type string. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
11
12
		parent::__construct( $args );
13
	}
14
15
	public function prepare_items() {
16
		global $wpdb, $per_page, $mode;
17
18
		$page = $this->get_pagenum();
19
		$per_page = $this->get_items_per_page( 'formidable_page_formidable_per_page' );
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
20
21
		$mode    = self::get_param(
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
22
			array(
23
				'param'   => 'mode',
24
				'default' => 'list',
25
			)
26
		);
27
		$orderby = self::get_param(
28
			array(
29
				'param'   => 'orderby',
30
				'default' => 'name',
31
			)
32
		);
33
		$order   = self::get_param(
34
			array(
35
				'param'   => 'order',
36
				'default' => 'ASC',
37
			)
38
		);
39
		$start   = self::get_param(
40
			array(
41
				'param'   => 'start',
42
				'default' => ( $page - 1 ) * $per_page,
43
			)
44
		);
45
46
		$s_query = array(
47
			array(
48
				'or' => 1,
49
				'parent_form_id' => null,
50
				'parent_form_id <' => 1,
51
			),
52
		);
53
		switch ( $this->status ) {
54
		    case 'draft':
55
                $s_query['is_template'] = 0;
56
                $s_query['status'] = 'draft';
57
		        break;
58
		    case 'trash':
59
                $s_query['status'] = 'trash';
60
		        break;
61
		    default:
62
                $s_query['is_template'] = 0;
63
                $s_query['status !'] = 'trash';
64
		        break;
65
		}
66
67
		$s = self::get_param(
68
			array(
69
				'param'    => 's',
70
				'sanitize' => 'sanitize_text_field',
71
			)
72
		);
73
	    if ( $s != '' ) {
74
			preg_match_all( '/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches );
75
			$search_terms = array_map( 'trim', $matches[0] );
76
			foreach ( (array) $search_terms as $term ) {
77
				$s_query[] = array(
78
					'or'               => true,
79
					'name LIKE'        => $term,
80
					'description LIKE' => $term,
81
					'created_at LIKE'  => $term,
82
				);
83
				unset( $term );
84
			}
85
	    }
86
87
		$this->items = FrmForm::getAll( $s_query, $orderby . ' ' . $order, $start . ',' . $per_page );
0 ignored issues
show
Documentation Bug introduced by
It seems like \FrmForm::getAll($s_quer...tart . ',' . $per_page) can also be of type object. However, the property $items is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
88
        $total_items = FrmDb::get_count( 'frm_forms', $s_query );
89
90
		$this->set_pagination_args(
91
			array(
92
				'total_items' => $total_items,
93
				'per_page'    => $per_page,
94
			)
95
		);
96
	}
97
98
	public function no_items() {
99
		esc_html_e( 'No Forms Found.', 'formidable' );
100
		?>
101
		<a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable&frm_action=new' ) ) ?>">
102
			<?php esc_html_e( 'Add New', 'formidable' ); ?>
103
		</a>
104
<?php
105
	}
106
107
	public function get_bulk_actions() {
108
	    $actions = array();
109
110
	    if ( 'trash' == $this->status ) {
111
			if ( current_user_can( 'frm_edit_forms' ) ) {
112
	            $actions['bulk_untrash'] = __( 'Restore', 'formidable' );
113
	        }
114
115
			if ( current_user_can( 'frm_delete_forms' ) ) {
116
	            $actions['bulk_delete'] = __( 'Delete Permanently', 'formidable' );
117
	        }
118
		} elseif ( EMPTY_TRASH_DAYS && current_user_can( 'frm_delete_forms' ) ) {
119
	        $actions['bulk_trash'] = __( 'Move to Trash', 'formidable' );
120
		} elseif ( current_user_can( 'frm_delete_forms' ) ) {
121
			$actions['bulk_delete'] = __( 'Delete', 'formidable' );
122
	    }
123
124
        return $actions;
125
    }
126
127
	public function extra_tablenav( $which ) {
128
        if ( 'top' != $which ) {
129
            return;
130
        }
131
132
		if ( 'trash' == $this->status && current_user_can( 'frm_delete_forms' ) ) {
133
?>
134
            <div class="alignleft actions frm_visible_overflow">
135
			<?php submit_button( __( 'Empty Trash', 'formidable' ), 'apply', 'delete_all', false ); ?>
136
            </div>
137
<?php
138
        }
139
	}
140
141
	public function get_views() {
142
143
		$statuses = array(
144
		    'published' => __( 'My Forms', 'formidable' ),
145
		    'draft'     => __( 'Drafts', 'formidable' ),
146
		    'trash'     => __( 'Trash', 'formidable' ),
147
		);
148
149
	    $links = array();
150
	    $counts = FrmForm::get_count();
151
		$form_type = self::get_param(
152
			array(
153
				'param' => 'form_type',
154
				'default' => 'published',
155
			)
156
		);
157
158
	    foreach ( $statuses as $status => $name ) {
159
160
	        if ( $status == $form_type ) {
161
    			$class = ' class="current"';
162
    		} else {
163
    		    $class = '';
164
    		}
165
166
    		if ( $counts->{$status} || 'published' == $status ) {
167
				$links[ $status ] = '<a href="' . esc_url( '?page=formidable&form_type=' . $status ) . '" ' . $class . '>' . sprintf( __( '%1$s <span class="count">(%2$s)</span>', 'formidable' ), $name, number_format_i18n( $counts->{$status} ) ) . '</a>';
168
		    }
169
170
			unset( $status, $name );
171
	    }
172
173
		return $links;
174
	}
175
176
	public function pagination( $which ) {
177
		global $mode;
178
179
		parent::pagination( $which );
180
181
		if ( 'top' == $which ) {
182
			$this->view_switcher( $mode );
183
		}
184
	}
185
186
	public function single_row( $item, $style = '' ) {
187
	    global $frm_vars, $mode;
188
189
		// Set up the hover actions for this user
190
		$actions = array();
191
		$edit_link = '?page=formidable&frm_action=edit&id=' . $item->id;
192
193
		$this->get_actions( $actions, $item, $edit_link );
194
195
        $action_links = $this->row_actions( $actions );
196
197
		// Set up the checkbox ( because the user is editable, otherwise its empty )
198
		$checkbox = '<input type="checkbox" name="item-action[]" id="cb-item-action-' . absint( $item->id ) . '" value="' . esc_attr( $item->id ) . '" />';
199
200
		$r = '<tr id="item-action-' . absint( $item->id ) . '"' . $style . '>';
201
202
		list( $columns, $hidden ) = $this->get_column_info();
203
204
        $format = 'Y/m/d';
205
        if ( 'list' != $mode ) {
206
            $format .= ' \<\b\r \/\> g:i:s a';
207
		}
208
209
		foreach ( $columns as $column_name => $column_display_name ) {
210
			$class = $column_name . ' column-' . $column_name . ( 'name' == $column_name ? ' post-title page-title column-title' : '' );
211
212
			$style = '';
213
			if ( in_array( $column_name, $hidden ) ) {
214
                $class .= ' frm_hidden';
215
			}
216
217
			$class = 'class="' . esc_attr( $class ) . '"';
218
			$data_colname = ' data-colname="' . esc_attr( $column_display_name ) . '"';
219
			$attributes = $class . $style . $data_colname;
220
221
			switch ( $column_name ) {
222
				case 'cb':
223
					$r .= '<th scope="row" class="check-column">' . $checkbox . '</th>';
224
					break;
225
				case 'id':
226
				case 'form_key':
227
				    $val = $item->{$column_name};
228
				    break;
229
				case 'name':
230
				    $val = $this->get_form_name( $item, $actions, $edit_link, $mode );
231
			        $val .= $action_links;
232
233
				    break;
234
				case 'created_at':
235
					$date = date( $format, strtotime( $item->created_at ) );
236
					$val = '<abbr title="' . esc_attr( date( 'Y/m/d g:i:s A', strtotime( $item->created_at ) ) ) . '">' . $date . '</abbr>';
237
					break;
238
				case 'shortcode':
239
					$val = '<input type="text" readonly="readonly" class="frm_select_box" value="' . esc_attr( '[formidable id=' . $item->id . ']' ) . '" /><br/>';
240
				    if ( 'excerpt' == $mode ) {
241
						$val .= '<input type="text" readonly="readonly" class="frm_select_box" value="' . esc_attr( '[formidable key=' . $item->form_key . ']' ) . '" />';
242
				    }
243
			        break;
244
			    case 'entries':
245
					if ( isset( $item->options['no_save'] ) && $item->options['no_save'] ) {
246
						$val = '<i class="frm_icon_font frm_forbid_icon frm_bstooltip" title="' . esc_attr( 'Saving entries is disabled for this form', 'formidable' ) . '"></i>';
247
					} else {
248
						$text = FrmEntry::getRecordCount( $item->id );
249
						$val = current_user_can( 'frm_view_entries' ) ? '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-entries&form=' . $item->id ) ) . '">' . $text . '</a>' : $text;
250
						unset( $text );
251
					}
252
			}
253
254
			if ( isset( $val ) ) {
255
			    $r .= "<td $attributes>";
256
			    $r .= $val;
257
			    $r .= '</td>';
258
			}
259
			unset( $val );
260
		}
261
		$r .= '</tr>';
262
263
		return $r;
264
	}
265
266
    /**
267
     * @param string $edit_link
268
     */
269
	private function get_actions( &$actions, $item, $edit_link ) {
270
		$new_actions = FrmFormsHelper::get_action_links( $item->id, $item );
271
		foreach ( $new_actions as $link => $action ) {
272
			$new_actions[ $link ] = FrmFormsHelper::format_link_html( $action, 'short' );
273
		}
274
275
		if ( 'trash' == $this->status ) {
276
			$actions = $new_actions;
277
			return;
278
		}
279
280 View Code Duplication
		if ( current_user_can( 'frm_edit_forms' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
281
			$actions['frm_edit'] = '<a href="' . esc_url( $edit_link ) . '">' . __( 'Edit', 'formidable' ) . '</a>';
282
			$actions['frm_settings'] = '<a href="' . esc_url( '?page=formidable&frm_action=settings&id=' . $item->id ) . '">' . __( 'Settings', 'formidable' ) . '</a>';
283
		}
284
285
		$actions = array_merge( $actions, $new_actions );
286
		$actions['view'] = '<a href="' . esc_url( FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" target="_blank">' . __( 'Preview', 'formidable' ) . '</a>';
287
    }
288
289
    /**
290
     * @param string $edit_link
291
     */
292
	private function get_form_name( $item, $actions, $edit_link, $mode = 'list' ) {
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
293
        $form_name = $item->name;
294
		if ( trim( $form_name ) == '' ) {
295
			$form_name = __( '(no title)', 'formidable' );
296
		}
297
		$form_name = FrmAppHelper::kses( $form_name );
298
		if ( 'excerpt' != $mode ) {
299
			$form_name = FrmAppHelper::truncate( $form_name, 50 );
300
		}
301
302
        $val = '<strong>';
303
        if ( 'trash' == $this->status ) {
304
            $val .= $form_name;
305
        } else {
306
			$val .= '<a href="' . esc_url( isset( $actions['frm_edit'] ) ? $edit_link : FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" class="row-title">' . FrmAppHelper::kses( $form_name ) . '</a> ';
307
        }
308
309
        $this->add_draft_label( $item, $val );
310
        $val .= '</strong>';
311
312
        $this->add_form_description( $item, $val );
313
314
        return $val;
315
    }
316
317
    /**
318
     * @param string $val
319
     */
320
    private function add_draft_label( $item, &$val ) {
321
        if ( 'draft' == $item->status && 'draft' != $this->status ) {
322
			$val .= ' - <span class="post-state">' . __( 'Draft', 'formidable' ) . '</span>';
323
        }
324
    }
325
326
    /**
327
     * @param string $val
328
     */
329
    private function add_form_description( $item, &$val ) {
330
        global $mode;
331
        if ( 'excerpt' == $mode ) {
332
			$val .= FrmAppHelper::truncate( strip_tags( $item->description ), 50 );
333
        }
334
    }
335
}
336