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

CronListPage   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 9

5 Methods

Rating   Name   Duplication   Size   Complexity  
A delete_cron_job() 0 13 1
B get_cron_schedules() 0 27 5
A initialize() 0 7 1
A render_body() 0 7 1
A register() 0 4 1
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
	}
20
21
	protected function initialize() {
22
		$this->page_slug  = \Bulk_Delete::CRON_PAGE_SLUG;
23
		$this->capability = 'manage_options';
24
25
		$this->label = array(
26
			'page_title' => __( 'Bulk Delete Schedules', 'bulk-delete' ),
27
			'menu_title' => __( 'Scheduled Jobs', 'bulk-delete' ),
28
		);
29
	}
30
31
	protected function render_body() {
32
		$cron_list_table = new CronListTable( $this->get_cron_schedules() );
33
		$cron_list_table->prepare_items();
34
		$cron_list_table->display();
35
36
		// TODO: Move this to a seperate Add-on page.
37
		bd_display_available_addon_list();
38
	}
39
40
	/**
41
	 * Process delete cron job request.
42
	 *
43
	 * @since 5.0
44
	 * @since 6.0.0 Moved into CronListPage class
45
	 */
46
	public function delete_cron_job() {
47
		$cron_id    = absint( $_GET['cron_id'] );
48
		$cron_items = $this->get_cron_schedules();
49
50
		wp_unschedule_event( $cron_items[ $cron_id ]['timestamp'], $cron_items[ $cron_id ]['type'], $cron_items[ $cron_id ]['args'] );
51
52
		$msg = __( 'The selected scheduled job was successfully deleted ', 'bulk-delete' );
53
54
		add_settings_error(
55
			\Bulk_Delete::CRON_PAGE_SLUG, // TODO: Replace this constant.
56
			'deleted-cron',
57
			$msg,
58
			'updated'
59
		);
60
	}
61
62
	/**
63
	 * Get the list of cron schedules.
64
	 *
65
	 * @since 6.0.0 Moved into CronListPage class
66
	 *
67
	 * @return array The list of cron schedules
68
	 */
69
	protected function get_cron_schedules() {
70
		$cron_items  = array();
71
		$cron        = _get_cron_array();
72
		$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' );
73
		$i           = 0;
74
75
		foreach ( $cron as $timestamp => $cronhooks ) {
76
			foreach ( (array) $cronhooks as $hook => $events ) {
77
				if ( 'do-bulk-delete-' === substr( $hook, 0, 15 ) ) {
78
					$cron_item = array();
79
80
					foreach ( (array) $events as $key => $event ) {
81
						$cron_item['timestamp'] = $timestamp;
82
						$cron_item['due']       = date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) );
83
						$cron_item['schedule']  = $event['schedule'];
84
						$cron_item['type']      = $hook;
85
						$cron_item['args']      = $event['args'];
86
						$cron_item['id']        = $i;
87
					}
88
89
					$cron_items[ $i ] = $cron_item;
90
					$i ++;
91
				}
92
			}
93
		}
94
95
		return $cron_items;
96
	}
97
}
98