Completed
Push — master ( 3b08d7...894d17 )
by Sudar
01:42
created

LogListTable::extra_tablenav()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 10
cp 0
crap 6
rs 9.4285
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 1
	public function __construct( $page, $args = array() ) {
26 1
		$this->page = $page;
27
28 1
		$args = wp_parse_args( $args, array(
29 1
			'singular' => 'email-log',     // singular name of the listed records
30 1
			'plural'   => 'email-logs',    // plural name of the listed records
31 1
			'ajax'     => false,           // does this table support ajax?
32 1
			'screen'   => $this->page->get_screen(),
33 1
		) );
34
35 1
		parent::__construct( $args );
36 1
	}
37
38
	/**
39
	 * Adds extra markup in the toolbars before or after the list.
40
	 *
41
	 * @access protected
42
	 *
43
	 * @param string $which Add the markup after (bottom) or before (top) the list.
44
	 */
45
	protected function extra_tablenav( $which ) {
46
		if ( 'top' == $which ) {
47
			// The code that goes before the table is here.
48
			echo '<span id = "el-pro-msg">';
49
			_e( 'Additional fields are available in Pro add-on. ', 'email-log' );
50
			echo '<a href="https://wpemaillog.com/addons/more-fields/?utm_campaign=Upsell&utm_medium=wpadmin&utm_source=inline&utm_content=mf" style="color:red">';
51
			_e( 'Buy Now', 'email-log' );
52
			echo '</a>';
53
			echo '</span>';
54
		}
55
	}
56
57
	/**
58
	 * Returns the list of column and title names.
59
	 *
60
	 * @see WP_List_Table::single_row_columns()
61
	 *
62
	 * @return array An associative array containing column information: 'slugs'=>'Visible Titles'.
63
	 */
64 1
	public function get_columns() {
65
		$columns = array(
66 1
			'cb'        => '<input type="checkbox" />', // Render a checkbox instead of text.
67 1
			'sent_date' => __( 'Sent at', 'email-log' ),
68 1
			'to'        => __( 'To', 'email-log' ),
69 1
			'subject'   => __( 'Subject', 'email-log' ),
70 1
		);
71
72
		/**
73
		 * Filter the email log list table columns.
74
		 *
75
		 * @since 2.0
76
		 * @param array $columns Columns of email log list table.
77
		 */
78 1
		return apply_filters( 'el_manage_log_columns', $columns );
79
	}
80
81
	/**
82
	 * Returns the list of columns.
83
	 *
84
	 * @access protected
85
	 *
86
	 * @return array<string,array<string|boolean>> An associative array containing all the columns
87
	 * that should be sortable: 'slugs'=>array('data_values',bool).
88
	 */
89
	protected function get_sortable_columns() {
90
		$sortable_columns = array(
91
			'sent_date'   => array( 'sent_date', true ), // true means it's already sorted.
92
			'to'          => array( 'to_email', false ),
93
			'subject'     => array( 'subject', false ),
94
		);
95
		return $sortable_columns;
96
	}
97
98
	/**
99
	 * Returns value for default columns.
100
	 *
101
	 * @access protected
102
	 *
103
	 * @param object $item        Data object.
104
	 * @param string $column_name Column Name.
105
	 */
106
	protected function column_default( $item, $column_name ) {
107
		/**
108
		 * Display Email Log list table columns.
109
		 *
110
		 * @since 2.0
111
		 *
112
		 * @param string $column_name Column Name.
113
		 * @param object $item        Data object.
114
		 */
115
		do_action( 'el_display_log_columns', $column_name, $item );
116
	}
117
118
	/**
119
	 * Display sent date column.
120
	 *
121
	 * @access protected
122
	 *
123
	 * @param  object $item Current item object.
124
	 * @return string       Markup to be displayed for the column.
125
	 */
126
	protected function column_sent_date( $item ) {
127
		$email_date = mysql2date(
128
			sprintf( __( '%s @ %s', 'email-log' ), get_option( 'date_format', 'F j, Y' ), get_option( 'time_format', 'g:i A' ) ),
129
			$item->sent_date
130
		);
131
132
		$actions = array();
133
134
		$content_ajax_url = add_query_arg(
135
			array(
136
				'action'    => 'el-log-list-view-message',
137
				'log_id'    => $item->id,
138
				'width'     => '800',
139
				'height'    => '550',
140
			),
141
			'admin-ajax.php'
142
		);
143
144
		$actions['view-content'] = sprintf( '<a href="%1$s" class="thickbox" title="%2$s">%3$s</a>',
145
			esc_url( $content_ajax_url ),
146
			__( 'Email Content', 'email-log' ),
147
			__( 'View Content', 'email-log' )
148
		);
149
150
		$delete_url = add_query_arg(
151
			array(
152
				'page'                   => $_REQUEST['page'],
153
				'action'                 => 'el-log-list-delete',
154
				$this->_args['singular'] => $item->id,
155
			)
156
		);
157
		$delete_url = add_query_arg( $this->page->get_nonce_args(), $delete_url );
158
159
		$actions['delete'] = sprintf( '<a href="%s">%s</a>',
160
			esc_url( $delete_url ),
161
			__( 'Delete', 'email-log' )
162
		);
163
164
		/**
165
		 * This filter can be used to modify the list of row actions that are displayed.
166
		 *
167
		 * @since 1.8
168
		 *
169
		 * @param array $actions List of actions.
170
		 * @param object $item The current log item.
171
		 */
172
		$actions = apply_filters( 'el_row_actions', $actions, $item );
173
174
		return sprintf( '%1$s <span style="color:silver">(id:%2$s)</span>%3$s',
175
			/*$1%s*/ $email_date,
176
			/*$2%s*/ $item->id,
177
			/*$3%s*/ $this->row_actions( $actions )
178
		);
179
	}
180
181
	/**
182
	 * To field.
183
	 *
184
	 * @access protected
185
	 *
186
	 * @param object $item
187
	 * @return string
188
	 */
189
	protected function column_to( $item ) {
190
		return esc_html( $item->to_email );
191
	}
192
193
	/**
194
	 * Subject field.
195
	 *
196
	 * @access protected
197
	 *
198
	 * @param object $item
199
	 * @return string
200
	 */
201
	protected function column_subject( $item ) {
202
		return esc_html( $item->subject );
203
	}
204
205
	/**
206
	 * Markup for action column.
207
	 *
208
	 * @access protected
209
	 *
210
	 * @param object $item
211
	 * @return string
212
	 */
213
	protected function column_cb( $item ) {
214
		return sprintf(
215
			'<input type="checkbox" name="%1$s[]" value="%2$s" />',
216
			/*$1%s*/ $this->_args['singular'],
217
			/*$2%s*/ $item->id
218
		);
219
	}
220
221
	/**
222
	 * Specify the list of bulk actions.
223
	 *
224
	 * @access protected
225
	 *
226
	 * @return array An associative array containing all the bulk actions: 'slugs'=>'Visible Titles'.
227
	 */
228
	protected function get_bulk_actions() {
229
		$actions = array(
230
			'el-log-list-delete'     => __( 'Delete', 'email-log' ),
231
			'el-log-list-delete-all' => __( 'Delete All Logs', 'email-log' ),
232
		);
233
		$actions = apply_filters( 'el_bulk_actions', $actions );
234
		return $actions;
235
	}
236
237
	/**
238
	 * Prepare data for display.
239
	 */
240
	public function prepare_items() {
241
		$this->_column_headers = $this->get_column_info();
242
243
		// Get current page number.
244
		$current_page_no = $this->get_pagenum();
245
		$per_page        = $this->page->get_per_page();
246
247
		list( $items, $total_items ) = $this->page->get_table_manager()->fetch_log_items( $_GET, $per_page, $current_page_no );
248
249
		$this->items = $items;
250
251
		// Register pagination options & calculations.
252
		$this->set_pagination_args( array(
253
			'total_items' => $total_items,
254
			'per_page'    => $per_page,
255
			'total_pages' => ceil( $total_items / $per_page ),
256
		) );
257
	}
258
259
	/**
260
	 * Displays default message when no items are found.
261
	 */
262
	public function no_items() {
263
		_e( 'Your email log is empty', 'email-log' );
264
	}
265
266
	/**
267
	 * Displays the search box.
268
	 *
269
	 * @since 2.0
270
	 *
271
	 * @param string $text     The 'submit' button label.
272
	 * @param string $input_id ID attribute value for the search input field.
273
	 */
274
	public function search_box( $text, $input_id ) {
275
		$input_text_id  = $input_id . '-search-input';
276
		$input_date_id  = $input_id . '-search-date-input';
277
		$input_date_val = ( ! empty( $_REQUEST['d'] ) ) ? sanitize_text_field( $_REQUEST['d'] ) : '';
278
279
280
		if ( ! empty( $_REQUEST['orderby'] ) )
281
			echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
282
		if ( ! empty( $_REQUEST['order'] ) )
283
			echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
284
		if ( ! empty( $_REQUEST['post_mime_type'] ) )
285
			echo '<input type="hidden" name="post_mime_type" value="' . esc_attr( $_REQUEST['post_mime_type'] ) . '" />';
286
		if ( ! empty( $_REQUEST['detached'] ) )
287
			echo '<input type="hidden" name="detached" value="' . esc_attr( $_REQUEST['detached'] ) . '" />';
288
		?>
289
		<p class="search-box">
290
			<label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo $text; ?>:</label>
291
			<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' ); ?>" />
292
			<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' ); ?>" />
293
			<?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
294
		</p>
295
		<?php
296
	}
297
}
298