Passed
Push — dev/6.0.0 ( f03c64...a274a9 )
by Sudar
03:13
created

CronListTable::extra_tablenav()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Cron;
4
5
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
6
7
if ( ! class_exists( '\WP_List_Table' ) ) {
8
	require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
9
}
10
11
/**
12
 * Table that lists bulk delete cron jobs.
13
 *
14
 * @since 6.0.0 Added namespace.
15
 */
16
class CronListTable extends \WP_List_Table {
17
18
	/**
19
	 * Constructor for creating new CronListTable.
20
	 *
21
	 * @param array $cron_jobs List of cron jobs.
22
	 */
23
	public function __construct( $cron_jobs ) {
24
		$this->items = $cron_jobs;
25
26
		parent::__construct(
27
			array(
28
				'singular' => 'cron_list',  // Singular label.
29
				'plural'   => 'cron_lists', // Plural label, also this well be one of the table css class.
30
				'ajax'     => false,        // We won't support Ajax for this table.
31
			)
32
		);
33
	}
34
35
	/**
36
	 * Add extra markup in the toolbars before or after the list.
37
	 *
38
	 * @param string $which Whether the markup should appear after (bottom) or before (top) the list.
39
	 */
40
	public function extra_tablenav( $which ) {
41
		if ( 'top' === $which ) {
42
			echo '<p>';
43
			_e( 'This is the list of jobs that are currently scheduled for auto deleting posts in Bulk Delete Plugin.', 'bulk-delete' );
44
			echo ' <strong>';
45
			_e( 'Note: ', 'bulk-delete' );
46
			echo '</strong>';
47
			_e( 'Scheduling auto post or user deletion is available only when you buy pro addons.', 'bulk-delete' );
48
			echo '</p>';
49
		}
50
	}
51
52
	/**
53
	 * Define the columns that are going to be used in the table.
54
	 *
55
	 * @return array Array of columns to use with the table
56
	 */
57
	public function get_columns() {
58
		return array(
59
			'col_cron_due'      => __( 'Next Due', 'bulk-delete' ),
60
			'col_cron_schedule' => __( 'Schedule', 'bulk-delete' ),
61
			'col_cron_type'     => __( 'Type', 'bulk-delete' ),
62
			'col_cron_options'  => __( 'Options', 'bulk-delete' ),
63
		);
64
	}
65
66
	/**
67
	 * Decide which columns to activate the sorting functionality on.
68
	 *
69
	 * @return array Array of columns that can be sorted by the user
70
	 */
71
	public function get_sortable_columns() {
72
		return array(
73
			'col_cron_type' => array( 'cron_type', true ),
74
		);
75
	}
76
77
	/**
78
	 * Prepare the table with different parameters, pagination, columns and table elements.
79
	 */
80
	public function prepare_items() {
81
		$total_items    = count( $this->items );
82
		$items_per_page = 50;
83
84
		$this->set_pagination_args( array(
85
			'total_items' => $total_items,
86
			'total_pages' => ceil( $total_items / $items_per_page ),
87
			'per_page'    => $items_per_page,
88
		) );
89
90
		$columns               = $this->get_columns();
91
		$hidden                = array();
92
		$sortable              = $this->get_sortable_columns();
93
		$this->_column_headers = array( $columns, $hidden, $sortable );
94
	}
95
96
	/**
97
	 * Display cron due date column.
98
	 *
99
	 * @param array $item The item to be displayed in the row.
100
	 *
101
	 * @return string Column output.
102
	 */
103
	public function column_col_cron_due( $item ) {
104
		$actions = array(
105
			'delete' => sprintf( '<a href="?page=%s&bd_action=%s&cron_id=%s&%s=%s">%s</a>',
106
				$_REQUEST['page'],
107
				'delete_cron',
108
				$item['id'],
109
				'bd-delete_cron-nonce',
110
				wp_create_nonce( 'bd-delete_cron' ),
111
				__( 'Delete', 'bulk-delete' )
112
			),
113
		);
114
115
		//Return the title contents
116
		return sprintf( '%1$s <span style="color:silver">(%2$s)</span>%3$s',
117
			/*$1%s*/
118
			$item['due'],
119
			/*$2%s*/
120
			( $item['timestamp'] + get_option( 'gmt_offset' ) * 60 * 60 ),
121
			/*$3%s*/
122
			$this->row_actions( $actions )
123
		);
124
	}
125
126
	/**
127
	 * Display cron schedule column.
128
	 *
129
	 * @param array $item The item to be displayed in the row.
130
	 */
131
	public function column_col_cron_schedule( $item ) {
132
		echo $item['schedule'];
133
	}
134
135
	/**
136
	 * Display cron type column.
137
	 *
138
	 * @param array $item The item to be displayed in the row.
139
	 */
140
	public function column_col_cron_type( $item ) {
141
		echo $item['type'];
142
	}
143
144
	/**
145
	 * Display cron options column.
146
	 *
147
	 * @param array $item The item to be displayed in the row.
148
	 */
149
	public function column_col_cron_options( $item ) {
150
		// TODO: Make it pretty
151
		print_r( $item['args'] );
152
	}
153
154
	/**
155
	 * Generates the message when no items are present.
156
	 */
157
	public function no_items() {
158
		_e( 'You have not scheduled any bulk delete jobs.', 'bulk-delete' );
159
	}
160
}
161