class-controller.php
3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* Legacy Scheduled event for BLC v1.
* Handles legacy cron jobs.
*
* @link https://wordpress.org/plugins/broken-link-checker/
* @since 2.0.0
*
* @author WPMUDEV (https://wpmudev.com)
* @package WPMUDEV_BLC\App\Schedule_Events\Legacy
*
* @copyright (c) 2022, Incsub (http://incsub.com)
*/
namespace WPMUDEV_BLC\App\Scheduled_Events\Legacy;
// Abort if called directly.
defined( 'WPINC' ) || die;
use WPMUDEV_BLC\Core\Utils\Abstracts\Base;
/**
* Class Controller
*
* @package WPMUDEV_BLC\App\Schedule_Events\Scan
*/
class Controller extends Base {
/**
* @var array $legacy_crons List of all legacy scheduled events.
*/
private $legacy_crons = array(
'blc_cron_check_links',
'blc_cron_email_notifications',
'blc_cron_database_maintenance',
'blc_corn_clear_log_file',
'blc_cron_check_news',
);
/**
* @var array $v2_crons List of all v2 scheduled events.
*/
private $v2_crons = array(
'blc_recipients_activation_email_schedule',
'blc_schedule_sync_scan_results',
'blc_schedule_scan',
);
/**
* Init Schedule
*
* @since 2.0.0
*
* @return void
*/
public function init() {
add_action( 'wpmudev_blc_plugin_deactivated', array( $this, 'deactivate_legacy_crons' ) );
add_action( 'wpmudev_blc_rest_enpoints_switch_version_mode', array( $this, 'switch_version_mode' ) );
}
/**
* Switched cron jobs depending on plugin version mode (legacy or v2).
*
* @since 2.0.0
*
* @param bool $legacy_active A boolean indicating if legacy mode is set.
*
* @retun void
*/
public function switch_version_mode( bool $legacy_active = true ) {
if ( $legacy_active ) {
// Enable legacy crons.
$this->activate_legacy_crons();
// Disable v2 cron events.
$this->deactivate_v2_crons();
} else {
// Disable legacy crons.
$this->deactivate_legacy_crons();
// Enable v2 crons.
do_action( 'wpmudev_blc_plugin_activated' );
\WPMUDEV_BLC\App\Scheduled_Events\Scan\Controller::instance()->set_scan_schedule();
\WPMUDEV_BLC\App\Scheduled_Events\Sync_Scan_Results\Controller::instance()->activate_cron();
}
}
/**
* Deactivates all legacy plugin's scheduled events.
*
* @since 2.0.0
*
* @return void
*/
public function deactivate_legacy_crons() {
$this->deactivate_crons( $this->legacy_crons );
// We still need to force clear `blc_schedule_scan` cron.
wp_clear_scheduled_hook( 'blc_schedule_scan' );
add_filter( 'blc_allow_send_email_notification', '__return_false' );
}
/**
* Deactivates all v2 plugin's scheduled events.
*
* @since 2.0.0
*
* @return void
*/
public function deactivate_v2_crons() {
$this->deactivate_crons( $this->v2_crons );
}
/**
* Deactivates given list of scheduled events.
*
* @since 2.0.0
*
* @param array $crons List of scheduled events to be deactivated.
*
* @return void
*/
public function deactivate_crons( array $cron_hooks = array() ) {
if ( empty( $cron_hooks ) ) {
return;
}
foreach ( $cron_hooks as $cron_hook ) {
wp_clear_scheduled_hook( $cron_hook );
}
}
public function activate_legacy_crons() {
global $blc_config_manager;
$ws_link_checker = null;
if ( ! class_exists( 'wsBrokenLinkChecker' ) ) {
require_once BLC_DIRECTORY_LEGACY . '/core/core.php';
}
if ( $blc_config_manager instanceof \blcConfigurationManager ) {
$ws_link_checker = new \wsBrokenLinkChecker( BLC_PLUGIN_FILE_LEGACY, $blc_config_manager );
}
if ( ! is_null( $ws_link_checker ) ) {
// Enable legacy cron events.
$ws_link_checker->setup_cron_events();
}
}
}