PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.1
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.1
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 All 126 releases
wp-to-buffer / lib / social / includes / class-log-table.php

class-log-table.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.1, at lib/social/includes/class-log-table.php

615 lines 15.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Log Table class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Controls the Log WP_List_Table.
13 *
14 * @package WPZinc\Social
15 * @author WP Zinc
16 * @version 3.9.6
17 */
18 class Log_Table extends \WP_List_Table {
19
20 /**
21 * Holds the base class object.
22 *
23 * @since 3.9.6
24 *
25 * @var object
26 */
27 public $base;
28
29 /**
30 * Constructor.
31 *
32 * @since 3.9.6
33 *
34 * @param object $base Base Plugin Class.
35 */
36 public function __construct( $base ) {
37
38 // Store base class.
39 $this->base = $base;
40
41 parent::__construct(
42 array(
43 'singular' => 'wpzinc-social-log', // Singular label.
44 'plural' => 'wpzinc-social-log', // plural label, also this well be one of the table css class.
45 'ajax' => false, // We won't support Ajax for this table.
46 )
47 );
48
49 }
50
51 /**
52 * Display dropdowns for Bulk Actions and Filtering.
53 *
54 * @since 3.9.6
55 *
56 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
57 * This is designated as optional for backward compatibility.
58 */
59 protected function bulk_actions( $which = '' ) {
60
61 // Get Bulk Actions.
62 $this->_actions = $this->get_bulk_actions();
63
64 // Define <select> name.
65 $bulk_actions_name = 'bulk_action' . ( $which !== 'top' ? '2' : '' );
66 ?>
67 <label for="bulk-action-selector-<?php echo esc_attr( $which ); ?>" class="screen-reader-text">
68 <?php esc_html_e( 'Select bulk action', 'wp-to-buffer' ); ?>
69 </label>
70 <select name="<?php echo esc_attr( $bulk_actions_name ); ?>" id="bulk-action-selector-<?php echo esc_attr( $which ); ?>" size="1">
71 <option value="-1"><?php esc_attr_e( 'Bulk Actions', 'wp-to-buffer' ); ?></option>
72
73 <?php
74 foreach ( $this->_actions as $name => $title ) {
75 ?>
76 <option value="<?php echo esc_attr( $name ); ?>"><?php echo esc_attr( $title ); ?></option>
77 <?php
78 }
79 ?>
80 </select>
81
82 <?php
83 // Output our custom filters to the top only.
84 if ( $which === 'top' ) {
85 $profiles = $this->base->get_class( 'log' )->get_profile_id_names();
86 ?>
87 <!-- Custom Filters -->
88 <select name="action" size="1">
89 <option value=""<?php selected( $this->get_action(), '' ); ?>><?php esc_attr_e( 'Filter by Action', 'wp-to-buffer' ); ?></option>
90 <?php
91 foreach ( $this->base->get_class( 'common' )->get_post_actions() as $action => $label ) {
92 ?>
93 <option value="<?php echo esc_attr( $action ); ?>"<?php selected( $this->get_action(), $action ); ?>><?php echo esc_attr( $label ); ?></option>
94 <?php
95 }
96 ?>
97 </select>
98 <select name="profile_id" size="1">
99 <option value=""<?php selected( $this->get_profile(), '' ); ?>><?php esc_attr_e( 'Filter by Profile', 'wp-to-buffer' ); ?></option>
100 <?php
101 foreach ( $profiles as $profile_id => $label ) {
102 ?>
103 <option value="<?php echo esc_attr( $profile_id ); ?>"<?php selected( $this->get_profile_id(), $profile_id ); ?>><?php echo esc_attr( $label ); ?></option>
104 <?php
105 }
106 ?>
107 </select>
108 <select name="result" size="1">
109 <option value=""<?php selected( $this->get_result(), '' ); ?>><?php esc_attr_e( 'Filter by Result', 'wp-to-buffer' ); ?></option>
110 <?php
111 foreach ( $this->base->get_class( 'log' )->get_result_options() as $result_option => $label ) {
112 ?>
113 <option value="<?php echo esc_attr( $result_option ); ?>"<?php selected( $this->get_result(), $result_option ); ?>>
114 <?php echo esc_attr( $label ); ?>
115 </option>
116 <?php
117 }
118 ?>
119 </select>
120
121 <input type="date" name="request_sent_start_date" value="<?php echo esc_attr( $this->get_request_sent_start_date() ); ?>" />
122 -
123 <input type="date" name="request_sent_end_date" value="<?php echo esc_attr( $this->get_request_sent_end_date() ); ?>"/>
124 <?php
125 }
126
127 submit_button( __( 'Apply', 'wp-to-buffer' ), 'action', '', false, array( 'id' => 'doaction' ) );
128
129 $clear_logs_url = add_query_arg(
130 array(
131 'page' => $this->base->plugin->name . '-log',
132 'bulk_action3' => 'delete_all',
133 '_wpnonce' => wp_create_nonce( 'bulk-wpzinc-social-log' ),
134 ),
135 admin_url( 'admin.php' )
136 );
137 ?>
138
139 <a href="<?php echo esc_url( $clear_logs_url ); ?>" class="<?php echo esc_attr( $this->base->plugin->name ); ?>-clear-log button wpzinc-button-red" data-message="<?php esc_html_e( 'Are you sure you want to clear ALL logs?', 'wp-to-buffer' ); ?>">
140 <?php esc_html_e( 'Clear Log', 'wp-to-buffer' ); ?>
141 </a>
142 <?php
143
144 }
145
146 /**
147 * Defines the message to display when no items exist in the table
148 *
149 * @since 3.9.6
150 */
151 public function no_items() {
152
153 esc_html_e( 'No log entries found based on the given search and filter criteria.', 'wp-to-buffer' );
154
155 }
156
157 /**
158 * Displays the search box.
159 *
160 * @since 3.1.0
161 *
162 * @param string $text The 'submit' button label.
163 * @param string $input_id ID attribute value for the search input field.
164 */
165 public function search_box( $text, $input_id ) {
166
167 // Build default values for filters.
168 $filters_values = array();
169 foreach ( $this->base->get_class( 'common' )->get_log_filters() as $filter ) {
170 $filters_values[ $filter ] = false;
171 }
172
173 // If a nonce is present, read the request.
174 if ( isset( $_REQUEST['_wpnonce'] ) && wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
175 foreach ( $this->base->get_class( 'common' )->get_log_filters() as $filter ) {
176 if ( ! array_key_exists( $filter, $_REQUEST ) ) {
177 continue;
178 }
179 $filters_values[ $filter ] = sanitize_text_field( wp_unslash( $_REQUEST[ $filter ] ) );
180 }
181 }
182
183 $input_id = $input_id . '-search-input';
184
185 // Preserve Filters by storing any defined as hidden form values.
186 foreach ( $this->base->get_class( 'common' )->get_log_filters() as $filter ) {
187 if ( $filters_values[ $filter ] !== false ) {
188 ?>
189 <input type="hidden" name="<?php echo esc_attr( $filter ); ?>" value="<?php echo esc_attr( $filters_values[ $filter ] ); ?>" />
190 <?php
191 }
192 }
193 ?>
194 <p class="search-box">
195 <label class="screen-reader-text" for="<?php echo esc_attr( $input_id ); ?>"><?php echo esc_attr( $text ); ?>:</label>
196 <input type="search" id="<?php echo esc_attr( $input_id ); ?>" name="s" value="<?php _admin_search_query(); ?>" placeholder="<?php esc_attr_e( 'Post ID or Title', 'wp-to-buffer' ); ?>" />
197 <?php submit_button( $text, '', '', false, array( 'id' => 'search-submit' ) ); ?>
198 </p>
199 <?php
200 }
201
202 /**
203 * Define the columns that are going to be used in the table
204 *
205 * @since 3.9.6
206 *
207 * @return array Columns to use with the table
208 */
209 public function get_columns() {
210
211 return array(
212 'cb' => '<input type="checkbox" class="toggle" />',
213 'post_id' => __( 'Post ID', 'wp-to-buffer' ),
214 'request_sent' => __( 'Request Sent', 'wp-to-buffer' ),
215 'action' => __( 'Action', 'wp-to-buffer' ),
216 'profile_name' => __( 'Profile', 'wp-to-buffer' ),
217 'status_text' => __( 'Status Text', 'wp-to-buffer' ),
218 'result' => __( 'Result', 'wp-to-buffer' ),
219 'result_message' => __( 'Response', 'wp-to-buffer' ),
220 'status_created_at' => sprintf(
221 /* translators: Social Media Service Name (Buffer, Hootsuite) */
222 __( '%s: Status Created At', 'wp-to-buffer' ),
223 $this->base->plugin->account
224 ),
225 'status_due_at' => sprintf(
226 /* translators: Social Media Service Name (Buffer, Hootsuite) */
227 __( '%s: Status Scheduled For', 'wp-to-buffer' ),
228 $this->base->plugin->account
229 ),
230 );
231
232 }
233
234 /**
235 * Decide which columns to activate the sorting functionality on
236 *
237 * @since 3.9.6
238 *
239 * @return array Columns that can be sorted by the user
240 */
241 public function get_sortable_columns() {
242
243 return array(
244 'post_id' => array( 'post_id', true ),
245 'request_sent' => array( 'request_sent', true ),
246 'action' => array( 'action', true ),
247 'profile_name' => array( 'profile_name', true ),
248 'status_text' => array( 'status_text', true ),
249 'result' => array( 'result', true ),
250 'result_message' => array( 'result_message', true ),
251 'status_created_at' => array( 'status_created_at', true ),
252 'status_due_at' => array( 'status_due_at', true ),
253 );
254
255 }
256
257 /**
258 * Overrides the list of bulk actions in the select dropdowns above and below the table
259 *
260 * @since 3.9.6
261 *
262 * @return array Bulk Actions
263 */
264 public function get_bulk_actions() {
265
266 return array(
267 'delete' => __( 'Delete', 'wp-to-buffer' ),
268 );
269
270 }
271
272 /**
273 * Prepare the table with different parameters, pagination, columns and table elements
274 *
275 * @since 3.9.6
276 */
277 public function prepare_items() {
278
279 global $_wp_column_headers;
280
281 $screen = get_current_screen();
282
283 // Get params.
284 $params = $this->get_search_params();
285 $order_by = $this->get_order_by();
286 $order = $this->get_order();
287 $page = $this->get_page();
288 $per_page = $this->get_items_per_page( $this->base->plugin->filter_name . '_logs_per_page', 20 );
289
290 // Get total records for this query.
291 $total = $this->base->get_class( 'log' )->total( $params );
292
293 // Define pagination.
294 $this->set_pagination_args(
295 array(
296 'total_items' => $total,
297 'total_pages' => ceil( $total / $per_page ),
298 'per_page' => $per_page,
299 )
300 );
301
302 // Set column headers.
303 $this->_column_headers = $this->get_column_info();
304
305 // Set rows.
306 $this->items = $this->base->get_class( 'log' )->search( $order_by, $order, $page, $per_page, $params );
307
308 }
309
310 /**
311 * Generate the table navigation above or below the table
312 *
313 * @since 3.1.0
314 *
315 * @param string $which The location of the bulk actions: 'top' or 'bottom'.
316 * This is designated as optional for backward compatibility.
317 */
318 protected function display_tablenav( $which ) {
319
320 if ( 'top' === $which ) {
321 wp_nonce_field( 'bulk-' . $this->_args['plural'] );
322 }
323 ?>
324 <div class="tablenav <?php echo esc_attr( $which ); ?>">
325 <div class="alignleft actions bulkactions">
326 <?php $this->bulk_actions( $which ); ?>
327 </div>
328 <?php
329 $this->extra_tablenav( $which );
330 $this->pagination( $which );
331 ?>
332
333 <br class="clear" />
334 </div>
335 <?php
336
337 }
338
339 /**
340 * Display the rows of records in the table
341 *
342 * @since 3.9.6
343 */
344 public function display_rows() {
345
346 echo wp_kses(
347 $this->base->get_class( 'log' )->build_log_table_output( $this->items, true, $this->_column_headers ),
348 array(
349 'tr' => array(
350 'class' => array(),
351 ),
352 'th' => array(
353 'scope' => array(),
354 'class' => array(),
355 ),
356 'td' => array(
357 'class' => array(),
358 ),
359 'a' => array(
360 'href' => array(),
361 'target' => array(),
362 ),
363 'br' => array(),
364 'input' => array(
365 'type' => array(
366 'checkbox',
367 ),
368 'name' => array(),
369 'value' => array(),
370 ),
371 )
372 );
373
374 }
375
376 /**
377 * Get search parameters.
378 *
379 * @since 3.9.6
380 *
381 * @return array Search Parameters
382 */
383 private function get_search_params() {
384
385 // Build search params.
386 $params = array(
387 'action' => $this->get_action(),
388 'profile_id' => $this->get_profile_id(),
389 'result' => $this->get_result(),
390 'request_sent_start_date' => $this->get_request_sent_start_date(),
391 'request_sent_end_date' => $this->get_request_sent_end_date(),
392 );
393
394 // Return params if freeform search isn't supplied.
395 if ( ! filter_has_var( INPUT_GET, 's' ) ) {
396 return $params;
397 }
398
399 // Get search.
400 $search = filter_input( INPUT_GET, 's', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
401
402 // If search is a number, add it as the Post ID and return.
403 if ( is_numeric( $search ) ) {
404 $params['post_id'] = absint( $search );
405 return $params;
406 }
407
408 // Add it as the Post Title and return.
409 $params['post_title'] = $search;
410
411 return $params;
412
413 }
414
415 /**
416 * Returns whether a search has been performed on the table.
417 *
418 * @since 3.9.0
419 *
420 * @return bool Search has been performed.
421 */
422 public function is_search() {
423
424 return filter_has_var( INPUT_GET, 's' );
425
426 }
427
428 /**
429 * Get the Search requested by the user
430 *
431 * @since 3.9.0
432 *
433 * @return string
434 */
435 public function get_search() {
436
437 // Bail if nonce is not valid.
438 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
439 return '';
440 }
441
442 if ( ! array_key_exists( 's', $_REQUEST ) ) {
443 return '';
444 }
445
446 return urldecode( sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) );
447
448 }
449
450 /**
451 * Get the Action Filter requested by the user
452 *
453 * @since 3.9.7
454 *
455 * @return string
456 */
457 private function get_action() {
458
459 // Bail if nonce is not valid.
460 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
461 return '';
462 }
463
464 if ( ! array_key_exists( 'action', $_REQUEST ) ) {
465 return '';
466 }
467
468 return sanitize_text_field( wp_unslash( $_REQUEST['action'] ) );
469
470 }
471
472 /**
473 * Get the Profile ID Filter requested by the user
474 *
475 * @since 3.9.7
476 *
477 * @return string
478 */
479 private function get_profile_id() {
480
481 // Bail if nonce is not valid.
482 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
483 return '';
484 }
485
486 if ( ! array_key_exists( 'profile_id', $_REQUEST ) ) {
487 return '';
488 }
489
490 return sanitize_text_field( wp_unslash( $_REQUEST['profile_id'] ) );
491
492 }
493
494 /**
495 * Get the Status Result Filter requested by the user
496 *
497 * @since 3.9.7
498 *
499 * @return string
500 */
501 private function get_result() {
502
503 // Bail if nonce is not valid.
504 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
505 return '';
506 }
507
508 if ( ! array_key_exists( 'result', $_REQUEST ) ) {
509 return '';
510 }
511
512 return sanitize_text_field( wp_unslash( $_REQUEST['result'] ) );
513
514 }
515
516 /**
517 * Get the Request Sent Start Date Filter requested by the user
518 *
519 * @since 3.9.8
520 *
521 * @return string
522 */
523 private function get_request_sent_start_date() {
524
525 // Bail if nonce is not valid.
526 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
527 return '';
528 }
529
530 if ( ! array_key_exists( 'request_sent_start_date', $_REQUEST ) ) {
531 return '';
532 }
533
534 return sanitize_text_field( wp_unslash( $_REQUEST['request_sent_start_date'] ) );
535
536 }
537
538 /**
539 * Get the Request Sent End Date Filter requested by the user
540 *
541 * @since 3.9.8
542 *
543 * @return string
544 */
545 private function get_request_sent_end_date() {
546
547 // Bail if nonce is not valid.
548 if ( ! isset( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( sanitize_key( wp_unslash( $_REQUEST['_wpnonce'] ) ), 'bulk-wpzinc-social-log' ) ) {
549 return '';
550 }
551
552 if ( ! array_key_exists( 'get_request_sent_end_date', $_REQUEST ) ) {
553 return '';
554 }
555
556 return sanitize_text_field( wp_unslash( $_REQUEST['get_request_sent_end_date'] ) );
557
558 }
559
560 /**
561 * Get the Order By requested by the user
562 *
563 * @since 3.9.7
564 *
565 * @return string
566 */
567 private function get_order_by() {
568
569 // Don't nonce check because order by may not include a nonce if no search performed.
570 if ( ! filter_has_var( INPUT_GET, 'orderby' ) ) {
571 return 'request_sent';
572 }
573
574 return filter_input( INPUT_GET, 'orderby', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
575
576 }
577
578 /**
579 * Get the Order requested by the user
580 *
581 * @since 3.9.7
582 *
583 * @return string
584 */
585 private function get_order() {
586
587 // Don't nonce check because order may not include a nonce if no search performed.
588 if ( ! filter_has_var( INPUT_GET, 'order' ) ) {
589 return 'DESC';
590 }
591
592 return filter_input( INPUT_GET, 'order', FILTER_SANITIZE_FULL_SPECIAL_CHARS );
593
594 }
595
596 /**
597 * Get the Pagination Page requested by the user
598 *
599 * @since 3.9.7
600 *
601 * @return string
602 */
603 private function get_page() {
604
605 // Don't nonce check because pagination may not include a nonce if no search performed.
606 if ( ! filter_has_var( INPUT_GET, 'paged' ) ) {
607 return 1;
608 }
609
610 return absint( filter_input( INPUT_GET, 'paged', FILTER_SANITIZE_FULL_SPECIAL_CHARS ) );
611
612 }
613
614 }
615