Completed
Push — 193-feature/delete-post-revisi... ( 4c008c...2cafa6 )
by Rajan
141:08 queued 137:21
created

CronListTable::get_columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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