CronListTable::no_items()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
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
			$total_items = count( $this->items );
44
			if ( 0 === $total_items ) {
45
				echo ' <strong>';
46
				_e( 'Note: ', 'bulk-delete' );
47
				echo '</strong>';
48
				_e( 'Scheduling auto post or user deletion is available only when you buy pro addons.', 'bulk-delete' );
49
			}
50
			echo '</p>';
51
		}
52
	}
53
54
	/**
55
	 * Define the columns that are going to be used in the table.
56
	 *
57
	 * @return array Array of columns to use with the table
58
	 */
59
	public function get_columns() {
60
		return array(
61
			'col_cron_due'      => __( 'Next Due', 'bulk-delete' ),
62
			'col_cron_schedule' => __( 'Schedule', 'bulk-delete' ),
63
			'col_cron_type'     => __( 'Type', 'bulk-delete' ),
64
			'col_cron_options'  => __( 'Options', 'bulk-delete' ),
65
		);
66
	}
67
68
	/**
69
	 * Decide which columns to activate the sorting functionality on.
70
	 *
71
	 * @return array Array of columns that can be sorted by the user
72
	 */
73
	public function get_sortable_columns() {
74
		return array(
75
			'col_cron_type' => array( 'cron_type', true ),
76
		);
77
	}
78
79
	/**
80
	 * Prepare the table with different parameters, pagination, columns and table elements.
81
	 */
82
	public function prepare_items() {
83
		$total_items    = count( $this->items );
84
		$items_per_page = 50;
85
86
		$this->set_pagination_args( array(
87
			'total_items' => $total_items,
88
			'total_pages' => ceil( $total_items / $items_per_page ),
89
			'per_page'    => $items_per_page,
90
		) );
91
92
		$columns               = $this->get_columns();
93
		$hidden                = array();
94
		$sortable              = $this->get_sortable_columns();
95
		$this->_column_headers = array( $columns, $hidden, $sortable );
96
	}
97
98
	/**
99
	 * Display cron due date column.
100
	 *
101
	 * @param array $item The item to be displayed in the row.
102
	 *
103
	 * @return string Column output.
104
	 */
105
	public function column_col_cron_due( $item ) {
106
		$actions = array(
107
			'delete' => sprintf( '<a href="?page=%s&bd_action=%s&cron_id=%s&%s=%s">%s</a>',
108
				$_REQUEST['page'],
109
				'delete_cron',
110
				$item['id'],
111
				'bd-delete_cron-nonce',
112
				wp_create_nonce( 'bd-delete_cron' ),
113
				__( 'Delete', 'bulk-delete' )
114
			),
115
			'run' => sprintf( '<a href="?page=%s&bd_action=%s&cron_id=%s&%s=%s" onclick="return confirm(%s)">%s</a>',
116
				$_REQUEST['page'],
117
				'run_cron',
118
				$item['id'],
119
				'bd-run_cron-nonce',
120
				wp_create_nonce( 'bd-run_cron' ),
121
				__( "'Are you sure you want to run the schedule job manually'", 'bulk-delete' ),
122
				__( 'Run Now', 'bulk-delete' )
123
			),
124
		);
125
126
		// Return the title contents.
127
		return sprintf( '%1$s <span style="color:silver">(%2$s)</span>%3$s',
128
			/*$1%s*/
129
			$item['due'],
130
			/*$2%s*/
131
			( $item['timestamp'] + get_option( 'gmt_offset' ) * 60 * 60 ),
132
			/*$3%s*/
133
			$this->row_actions( $actions )
134
		);
135
	}
136
137
	/**
138
	 * Display cron schedule column.
139
	 *
140
	 * @param array $item The item to be displayed in the row.
141
	 */
142
	public function column_col_cron_schedule( $item ) {
143
		echo $item['schedule'];
144
	}
145
146
	/**
147
	 * Display cron type column.
148
	 *
149
	 * @param array $item The item to be displayed in the row.
150
	 */
151
	public function column_col_cron_type( $item ) {
152
		if ( isset( $item['args'][0]['cron_label'] ) ) {
153
			echo esc_html( $item['args'][0]['cron_label'] );
154
		} else {
155
			echo esc_html( $item['type'] );
156
		}
157
	}
158
159
	/**
160
	 * Display cron options column.
161
	 *
162
	 * @param array $item The item to be displayed in the row.
163
	 */
164
	public function column_col_cron_options( $item ) {
165
		// TODO: Make it pretty
166
		print_r( $item['args'] );
167
	}
168
169
	/**
170
	 * Generates the message when no items are present.
171
	 */
172
	public function no_items() {
173
		_e( 'You have not scheduled any bulk delete jobs.', 'bulk-delete' );
174
	}
175
}
176