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

CronListPage::render_body()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BulkWP\BulkDelete\Core\Cron;
4
5
use BulkWP\BulkDelete\Core\Base\BasePage;
6
7
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
8
9
/**
10
 * Page that lists bulk delete cron jobs.
11
 *
12
 * @since 6.0.0
13
 */
14
class CronListPage extends BasePage {
15
16
	public function register() {
17
		parent::register();
18
19
		add_action( 'bd_delete_cron', array( $this, 'delete_cron_job' ) );
20
	}
21
22
	protected function initialize() {
23
		$this->page_slug  = \Bulk_Delete::CRON_PAGE_SLUG;
24
		$this->capability = 'manage_options';
25
26
		$this->label = array(
27
			'page_title' => __( 'Bulk Delete Schedules', 'bulk-delete' ),
28
			'menu_title' => __( 'Scheduled Jobs', 'bulk-delete' ),
29
		);
30
	}
31
32
	protected function render_body() {
33
		$cron_list_table = new CronListTable( $this->get_cron_schedules() );
34
		$cron_list_table->prepare_items();
35
		$cron_list_table->display();
36
37
		// TODO: Move this to a seperate Add-on page.
38
		bd_display_available_addon_list();
39
	}
40
41
	/**
42
	 * Process delete cron job request.
43
	 *
44
	 * @since 5.0
45
	 * @since 6.0.0 Moved into CronListPage class
46
	 */
47
	public function delete_cron_job() {
48
		$cron_id    = absint( $_GET['cron_id'] );
49
		$cron_items = $this->get_cron_schedules();
50
51
		wp_unschedule_event( $cron_items[ $cron_id ]['timestamp'], $cron_items[ $cron_id ]['type'], $cron_items[ $cron_id ]['args'] );
52
53
		$msg = __( 'The selected scheduled job was successfully deleted ', 'bulk-delete' );
54
55
		add_settings_error(
56
			\Bulk_Delete::CRON_PAGE_SLUG, // TODO: Replace this constant.
57
			'deleted-cron',
58
			$msg,
59
			'updated'
60
		);
61
	}
62
63
	/**
64
	 * Get the list of cron schedules.
65
	 *
66
	 * @since 6.0.0 Moved into CronListPage class
67
	 *
68
	 * @return array The list of cron schedules
69
	 */
70
	protected function get_cron_schedules() {
71
		$cron_items  = array();
72
		$cron        = _get_cron_array();
73
		$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' );
74
		$i           = 0;
75
76
		foreach ( $cron as $timestamp => $cronhooks ) {
77
			foreach ( (array) $cronhooks as $hook => $events ) {
78
				if ( 'do-bulk-delete-' === substr( $hook, 0, 15 ) ) {
79
					$cron_item = array();
80
81
					foreach ( (array) $events as $key => $event ) {
82
						$cron_item['timestamp'] = $timestamp;
83
						$cron_item['due']       = date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) );
84
						$cron_item['schedule']  = $event['schedule'];
85
						$cron_item['type']      = $hook;
86
						$cron_item['args']      = $event['args'];
87
						$cron_item['id']        = $i;
88
					}
89
90
					$cron_items[ $i ] = $cron_item;
91
					$i ++;
92
				}
93
			}
94
		}
95
96
		return $cron_items;
97
	}
98
}
99