Cron.php
2.87 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
<?php
/**
* @license GPL-2.0-or-later
*
* Modified by code-atlantic on 21-June-2024 using {@see https://github.com/BrianHenryIE/strauss}.
*/
namespace ContentControl\Vendor\TrustedLogin;
// Exit if accessed directly
if ( ! defined('ABSPATH') ) {
exit;
}
use \Exception;
use \WP_Error;
use \WP_User;
use \WP_Admin_Bar;
final class Cron {
/**
* @var \ContentControl\Vendor\TrustedLogin\Config
*/
private $config;
/**
* @var string
*/
private $hook_name;
/**
* @var null|\ContentControl\Vendor\TrustedLogin\Logging $logging
*/
private $logging;
/**
* Cron constructor.
*
* @param Config $config
* @param Logging|null $logging
*/
public function __construct( Config $config, Logging $logging ) {
$this->config = $config;
$this->logging = $logging;
$this->hook_name = 'trustedlogin/' . $this->config->ns() . '/access/revoke';
}
/**
*
*/
public function init() {
add_action( $this->hook_name, array( $this, 'revoke' ), 1 );
}
/**
* @param int $expiration_timestamp
* @param string $identifier_hash
*
* @return bool
*/
public function schedule( $expiration_timestamp, $identifier_hash ) {
$hash = Encryption::hash( $identifier_hash );
if ( is_wp_error( $hash ) ) {
$this->logging->log( $hash, __METHOD__ );
return false;
}
$args = array( $hash );
$scheduled_expiration = wp_schedule_single_event( $expiration_timestamp, $this->hook_name, $args );
$this->logging->log( 'Scheduled Expiration: ' . var_export( $scheduled_expiration, true ) . '; identifier: ' . $identifier_hash, __METHOD__, 'info' );
return $scheduled_expiration;
}
/**
* @param int $expiration_timestamp
* @param string $site_identifier_hash
*
* @return bool
*/
public function reschedule( $expiration_timestamp, $site_identifier_hash ) {
$hash = Encryption::hash( $site_identifier_hash );
if ( is_wp_error( $hash ) ) {
$this->logging->log( $hash, __METHOD__ );
return false;
}
$unschedule_expiration = wp_clear_scheduled_hook( $this->hook_name, array( $hash ) );
switch( $unschedule_expiration ) {
case false:
$this->logging->log( sprintf( 'Could not clear scheduled hook for %s', $this->hook_name ), __METHOD__, 'error' );
return false;
case 0:
$this->logging->log( sprintf( 'Cron event not found for %s', $this->hook_name ), __METHOD__, 'error' );
return false;
}
return $this->schedule( $expiration_timestamp, $site_identifier_hash );
}
/**
* Hooked Action: Revokes access for a specific support user
*
* @since 1.0.0
*
* @param string $identifier_hash Identifier hash for the user associated with the cron job
* @todo
* @return void
*/
public function revoke( $identifier_hash ) {
$this->logging->log( 'Running cron job to disable user. ID: ' . $identifier_hash, __METHOD__, 'notice' );
$Client = new Client( $this->config, false );
$Client->revoke_access( $identifier_hash );
}
}