| 1 |
<?php |
| 2 |
/** |
| 3 |
* @license GPL-2.0-or-later |
| 4 |
* |
| 5 |
* Modified by code-atlantic on 18-September-2023 using Strauss. |
| 6 |
* @see https://github.com/BrianHenryIE/strauss |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace ContentControl\Vendor\TrustedLogin; |
| 10 |
|
| 11 |
// Exit if accessed directly |
| 12 |
if ( ! defined('ABSPATH') ) { |
| 13 |
exit; |
| 14 |
} |
| 15 |
|
| 16 |
use \Exception; |
| 17 |
use \WP_Error; |
| 18 |
use \WP_User; |
| 19 |
use \WP_Admin_Bar; |
| 20 |
|
| 21 |
final class Cron { |
| 22 |
|
| 23 |
/** |
| 24 |
* @var \ContentControl\Vendor\TrustedLogin\Config |
| 25 |
*/ |
| 26 |
private $config; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var string |
| 30 |
*/ |
| 31 |
private $hook_name; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var null|\ContentControl\Vendor\TrustedLogin\Logging $logging |
| 35 |
*/ |
| 36 |
private $logging; |
| 37 |
|
| 38 |
/** |
| 39 |
* Cron constructor. |
| 40 |
* |
| 41 |
* @param Config $config |
| 42 |
* @param Logging|null $logging |
| 43 |
*/ |
| 44 |
public function __construct( Config $config, Logging $logging ) { |
| 45 |
$this->config = $config; |
| 46 |
$this->logging = $logging; |
| 47 |
|
| 48 |
$this->hook_name = 'trustedlogin/' . $this->config->ns() . '/access/revoke'; |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* |
| 53 |
*/ |
| 54 |
public function init() { |
| 55 |
add_action( $this->hook_name, array( $this, 'revoke' ), 1 ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @param int $expiration_timestamp |
| 60 |
* @param string $identifier_hash |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
public function schedule( $expiration_timestamp, $identifier_hash ) { |
| 65 |
|
| 66 |
$hash = Encryption::hash( $identifier_hash ); |
| 67 |
|
| 68 |
if ( is_wp_error( $hash ) ) { |
| 69 |
$this->logging->log( $hash, __METHOD__ ); |
| 70 |
|
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
$args = array( $hash ); |
| 75 |
|
| 76 |
$scheduled_expiration = wp_schedule_single_event( $expiration_timestamp, $this->hook_name, $args ); |
| 77 |
|
| 78 |
$this->logging->log( 'Scheduled Expiration: ' . var_export( $scheduled_expiration, true ) . '; identifier: ' . $identifier_hash, __METHOD__, 'info' ); |
| 79 |
|
| 80 |
return $scheduled_expiration; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* @param int $expiration_timestamp |
| 85 |
* @param string $site_identifier_hash |
| 86 |
* |
| 87 |
* @return bool |
| 88 |
*/ |
| 89 |
public function reschedule( $expiration_timestamp, $site_identifier_hash ) { |
| 90 |
|
| 91 |
$hash = Encryption::hash( $site_identifier_hash ); |
| 92 |
|
| 93 |
if ( is_wp_error( $hash ) ) { |
| 94 |
$this->logging->log( $hash, __METHOD__ ); |
| 95 |
|
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
$unschedule_expiration = wp_clear_scheduled_hook( $this->hook_name, array( $hash ) ); |
| 100 |
|
| 101 |
switch( $unschedule_expiration ) { |
| 102 |
case false: |
| 103 |
$this->logging->log( sprintf( 'Could not clear scheduled hook for %s', $this->hook_name ), __METHOD__, 'error' ); |
| 104 |
return false; |
| 105 |
case 0: |
| 106 |
$this->logging->log( sprintf( 'Cron event not found for %s', $this->hook_name ), __METHOD__, 'error' ); |
| 107 |
return false; |
| 108 |
} |
| 109 |
|
| 110 |
return $this->schedule( $expiration_timestamp, $site_identifier_hash ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Hooked Action: Revokes access for a specific support user |
| 115 |
* |
| 116 |
* @since 1.0.0 |
| 117 |
* |
| 118 |
* @param string $identifier_hash Identifier hash for the user associated with the cron job |
| 119 |
* @todo |
| 120 |
* @return void |
| 121 |
*/ |
| 122 |
public function revoke( $identifier_hash ) { |
| 123 |
|
| 124 |
$this->logging->log( 'Running cron job to disable user. ID: ' . $identifier_hash, __METHOD__, 'notice' ); |
| 125 |
|
| 126 |
$Client = new Client( $this->config, false ); |
| 127 |
|
| 128 |
$Client->revoke_access( $identifier_hash ); |
| 129 |
} |
| 130 |
} |
| 131 |
|