Completed
Push — 125-fix/improve-preview ( be8b7d...ab5155 )
by Maria Daniel Deepak
07:24 queued 03:14
created

LogListTable::search_box()   A

Complexity

Conditions 6
Paths 32

Size

Total Lines 20
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 19
nc 32
nop 2
dl 0
loc 20
ccs 0
cts 12
cp 0
crap 42
rs 9.0111
c 0
b 0
f 0
1
<?php namespace EmailLog\Core\UI\ListTable;
2
3 1
if ( ! class_exists( 'WP_List_Table' ) ) {
4
	require_once ABSPATH . WPINC . '/class-wp-list-table.php';
5
}
6
7
/**
8
 * Table to display Email Logs.
9
 *
10
 * Based on Custom List Table Example by Matt Van Andel.
11
 */
12
class LogListTable extends \WP_List_Table {
13
	/**
14
	 * @var object The page where this table is rendered.
15
	 *
16
	 * @since 2.0
17
	 */
18
	protected $page;
19
20
	/**
21
	 * Set up a constructor that references the parent constructor.
22
	 *
23
	 * We use the parent reference to set some default configs.
24
	 *
25
	 * @param \EmailLog\Core\UI\Page\LogListPage $page
26
	 * @param mixed                              $args
27
	 */
28 1
	public function __construct( $page, $args = array() ) {
29 1
		$this->page = $page;
30
31 1
		$args = wp_parse_args( $args, array(
32 1
			'singular' => 'email-log',     // singular name of the listed records
33 1
			'plural'   => 'email-logs',    // plural name of the listed records
34 1
			'ajax'     => false,           // does this table support ajax?
35 1
			'screen'   => $this->page->get_screen(),
36 1
		) );
37
38 1
		parent::__construct( $args );
39 1
	}
40
41
	/**
42
	 * Adds extra markup in the toolbars before or after the list.
43
	 *
44
	 * @access protected
45
	 *
46
	 * @param string $which Add the markup after (bottom) or before (top) the list.
47
	 */
48
	protected function extra_tablenav( $which ) {
49
		if ( 'top' == $which ) {
50
			/**
51
			 * Triggered before the logs list table is displayed.
52
			 *
53
			 * @since 2.2.5
54
			 */
55
			do_action( 'el_before_logs_list_table' );
56
		}
57
	}
58
59
	/**
60
	 * Returns the list of column and title names.
61
	 *
62
	 * @see WP_List_Table::single_row_columns()
63
	 *
64
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
65
	 */
66 1
	public function get_columns() {
67
		$columns = array(
68 1
			'cb'        => '<input type="checkbox" />', // Render a checkbox instead of text.
69 1
			'sent_date' => __( 'Sent at', 'email-log' ),
70 1
			'to'        => __( 'To', 'email-log' ),
71 1
			'subject'   => __( 'Subject', 'email-log' ),
72 1
		);
73
74
		/**
75
		 * Filter the email log list table columns.
76
		 *
77
		 * @since 2.0
78
		 *
79
		 * @param array $columns Columns of email log list table.
80
		 */
81 1
		return apply_filters( 'el_manage_log_columns', $columns );
82
	}
83
84
	/**
85
	 * Returns the list of columns.
86
	 *
87
	 * @access protected
88
	 *
89
	 * @return array<string,array<boolean|string>> An associative array containing all the columns
90
	 *                                             that should be sortable: 'slugs'=>array('data_values',bool).
91
	 */
92
	protected function get_sortable_columns() {
93
		$sortable_columns = array(
94
			'sent_date' => array( 'sent_date', true ), // true means it's already sorted.
95
			'to'        => array( 'to_email', false ),
96
			'subject'   => array( 'subject', false ),
97
		);
98
99
		return $sortable_columns;
100
	}
101
102
	/**
103
	 * Returns value for default columns.
104
	 *
105
	 * @access protected
106
	 *
107
	 * @param object $item        Data object.
108
	 * @param string $column_name Column Name.
109
	 */
110
	protected function column_default( $item, $column_name ) {
111
		/**
112
		 * Display Email Log list table columns.
113
		 *
114
		 * @since 2.0
115
		 *
116
		 * @param string $column_name Column Name.
117
		 * @param object $item        Data object.
118
		 */
119
		do_action( 'el_display_log_columns', $column_name, $item );
120
	}
121
122
	/**
123
	 * Display sent date column.
124
	 *
125
	 * @access protected
126
	 *
127
	 * @param object $item Current item object.
128
	 *
129
	 * @return string Markup to be displayed for the column.
130
	 */
131
	protected function column_sent_date( $item ) {
132
		$email_date = mysql2date(
133
			sprintf( __( '%s @ %s', 'email-log' ), get_option( 'date_format', 'F j, Y' ), get_option( 'time_format', 'g:i A' ) ),
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format', 'F j, Y') can also be of type false; however, parameter $args of sprintf() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

133
			sprintf( __( '%s @ %s', 'email-log' ), /** @scrutinizer ignore-type */ get_option( 'date_format', 'F j, Y' ), get_option( 'time_format', 'g:i A' ) ),
Loading history...
134
			$item->sent_date
135
		);
136
137
		$actions = array();
138
139
		$content_ajax_url = add_query_arg(
140
			array(
141
				'action' => 'el-log-list-view-message',
142
				'log_id' => $item->id,
143
				'width'  => '800',
144
				'height' => '550',
145
			),
146
			'admin-ajax.php'
147
		);
148
149
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
150
			esc_url( $content_ajax_url ),
151
			__( 'Email Content', 'email-log' ),
152
			__( 'View Content', 'email-log' )
153
		);
154
155
		$delete_url = add_query_arg(
156
			array(
157
				'page'                   => $_REQUEST['page'],
158
				'action'                 => 'el-log-list-delete',
159
				$this->_args['singular'] => $item->id,
160
			)
161
		);
162
		$delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
163
164
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
165
			esc_url( $delete_url ),
166
			__( 'Delete', 'email-log' )
167
		);
168
169
		/**
170
		 * This filter can be used to modify the list of row actions that are displayed.
171
		 *
172
		 * @since 1.8
173
		 *
174
		 * @param array  $actions List of actions.
175
		 * @param object $item    The current log item.
176
		 */
177
		$actions = apply_filters( 'el_row_actions', $actions, $item );
178
179
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
180
			/*$1%s*/ $email_date,
181
			/*$2%s*/ $item->id,
182
			/*$3%s*/ $this->row_actions( $actions )
183
		);
184
	}
185
186
	/**
187
	 * To field.
188
	 *
189
	 * @access protected
190
	 *
191
	 * @param object $item
192
	 *
193
	 * @return string
194
	 */
195
	protected function column_to( $item ) {
196
		return esc_html( $item->to_email );
197
	}
198
199
	/**
200
	 * Subject field.
201
	 *
202
	 * @access protected
203
	 *
204
	 * @param object $item
205
	 *
206
	 * @return string
207
	 */
208
	protected function column_subject( $item ) {
209
		return esc_html( $item->subject );
210
	}
211
212
	/**
213
	 * Markup for action column.
214
	 *
215
	 * @access protected
216
	 *
217
	 * @param object $item
218
	 *
219
	 * @return string
220
	 */
221
	protected function column_cb( $item ) {
222
		return sprintf(
223
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
224
			/*$1%s*/ $this->_args['singular'],
225
			/*$2%s*/ $item->id
226
		);
227
	}
228
229
	/**
230
	 * Specify the list of bulk actions.
231
	 *
232
	 * @access protected
233
	 *
234
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
235
	 */
236
	protected function get_bulk_actions() {
237
		$actions = array(
238
			'el-log-list-delete'     => __( 'Delete', 'email-log' ),
239
			'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
240
		);
241
		$actions = apply_filters( 'el_bulk_actions', $actions );
242
243
		return $actions;
244
	}
245
246
	/**
247
	 * Prepare data for display.
248
	 */
249
	public function prepare_items() {
250
		$this->_column_headers = $this->get_column_info();
251
252
		// Get current page number.
253
		$current_page_no = $this->get_pagenum();
254
		$per_page        = $this->page->get_per_page();
255
256
		list( $items, $total_items ) = $this->page->get_table_manager()->fetch_log_items( $_GET, $per_page, $current_page_no );
257
258
		$this->items = $items;
259
260
		// Register pagination options & calculations.
261
		$this->set_pagination_args( array(
262
			'total_items' => $total_items,
263
			'per_page'    => $per_page,
264
			'total_pages' => ceil( $total_items / $per_page ),
265
		) );
266
	}
267
268
	/**
269
	 * Displays default message when no items are found.
270
	 */
271
	public function no_items() {
272
		_e( 'Your email log is empty', 'email-log' );
273
	}
274
275
	/**
276
	 * Displays the search box.
277
	 *
278
	 * @since 2.0
279
	 *
280
	 * @param string $text     The 'submit' button label.
281
	 * @param string $input_id ID attribute value for the search input field.
282
	 */
283
	public function search_box( $text, $input_id ) {
284
		$input_text_id  = $input_id . '-search-input';
285
		$input_date_id  = $input_id . '-search-date-input';
286
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
287
288
		if ( ! empty( $_REQUEST['orderby'] ) )
289
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
290
		if ( ! empty( $_REQUEST['order'] ) )
291
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
292
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
293
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
294
		if ( ! empty( $_REQUEST['detached'] ) )
295
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
296
		?>
297
		<p class="search-box">
298
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
299
			<input type="search" id="<?php echo esc_attr( $input_date_id ); ?>" name="d" value="<?php echo $input_date_val; ?>" placeholder="<?php _e( 'Search by date', 'email-log' ); ?>" />
300
			<input type="search" id="<?php echo esc_attr( $input_text_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" placeholder="<?php _e( 'Search by term', 'email-log' ); ?>" />
301
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
302
		</p>
303
		<?php
304
	}
305
}
306