CronListPage   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 126
ccs 0
cts 83
cp 0
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A delete_cron_job() 0 21 3
A run_cron_job() 0 21 3
A initialize() 0 7 1
A render_body() 0 7 1
A register() 0 5 1
B get_cron_schedules() 0 33 6
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
	public function register() {
16
		parent::register();
17
18
		add_action( 'bd_delete_cron', array( $this, 'delete_cron_job' ) );
19
		add_action( 'bd_run_cron', array( $this, 'run_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 run cron job request.
43
	 *
44
	 * @since 6.0
45
	 */
46
	public function run_cron_job() {
47
		$cron_id    = absint( $_GET['cron_id'] );
48
		$cron_items = $this->get_cron_schedules();
49
50
		if ( 0 === $cron_id ) {
51
			return;
52
		}
53
54
		if ( ! isset( $cron_items[ $cron_id ] ) ) {
55
			return;
56
		}
57
58
		wp_schedule_single_event( time(), $cron_items[ $cron_id ]['type'], $cron_items[ $cron_id ]['args'] );
59
60
		$msg = __( 'The selected scheduled job has been successfully started. It will now run in the background.', 'bulk-delete' );
61
62
		add_settings_error(
63
			\Bulk_Delete::CRON_PAGE_SLUG, // TODO: Replace this constant.
64
			'deleted-cron',
65
			$msg,
66
			'updated'
67
		);
68
	}
69
70
	/**
71
	 * Process delete cron job request.
72
	 *
73
	 * @since 5.0
74
	 * @since 6.0.0 Moved into CronListPage class
75
	 */
76
	public function delete_cron_job() {
77
		$cron_id    = absint( $_GET['cron_id'] );
78
		$cron_items = $this->get_cron_schedules();
79
80
		if ( 0 === $cron_id ) {
81
			return;
82
		}
83
84
		if ( ! isset( $cron_items[ $cron_id ] ) ) {
85
			return;
86
		}
87
88
		wp_unschedule_event( $cron_items[ $cron_id ]['timestamp'], $cron_items[ $cron_id ]['type'], $cron_items[ $cron_id ]['args'] );
89
90
		$msg = __( 'The selected scheduled job was successfully deleted ', 'bulk-delete' );
91
92
		add_settings_error(
93
			\Bulk_Delete::CRON_PAGE_SLUG, // TODO: Replace this constant.
94
			'deleted-cron',
95
			$msg,
96
			'updated'
97
		);
98
	}
99
100
	/**
101
	 * Get the list of cron schedules.
102
	 *
103
	 * @since 6.0.0 Moved into CronListPage class
104
	 *
105
	 * @return array The list of cron schedules
106
	 */
107
	protected function get_cron_schedules() {
108
		$cron_items  = array();
109
		$cron        = _get_cron_array();
110
		$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' );
111
		$schedules   = wp_get_schedules();
112
		$i           = 1;
113
114
		foreach ( $cron as $timestamp => $cronhooks ) {
115
			foreach ( (array) $cronhooks as $hook => $events ) {
116
				if ( 'do-bulk-delete-' === substr( $hook, 0, 15 ) ) {
117
					$cron_item = array();
118
119
					foreach ( (array) $events as $key => $event ) {
120
						$cron_item['timestamp'] = $timestamp;
121
						$cron_item['due']       = date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) );
122
						$cron_item['type']      = $hook;
123
						$cron_item['args']      = $event['args'];
124
						$cron_item['id']        = $i;
125
126
						if ( isset( $schedules[ $event['schedule'] ] ) ) {
127
							$cron_item['schedule'] = $schedules[ $event['schedule'] ]['display'];
128
						} else {
129
							$cron_item['schedule'] = $event['schedule'];
130
						}
131
					}
132
133
					$cron_items[ $i ] = $cron_item;
134
					$i ++;
135
				}
136
			}
137
		}
138
139
		return $cron_items;
140
	}
141
}
142