| 1 |
<?php |
| 2 |
/** |
| 3 |
* Our API calls responsible for handling requests from admins requesting a scheduled refresh. |
| 4 |
* |
| 5 |
* @package ForceRefresh |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace JordanLeven\Plugins\ForceRefresh\Api; |
| 9 |
|
| 10 |
use JordanLeven\Plugins\ForceRefresh\Api\Api_Handler_Admin; |
| 11 |
use JordanLeven\Plugins\ForceRefresh\Api\Interfaces\Api_Handler_Admin_Interface; |
| 12 |
use JordanLeven\Plugins\ForceRefresh\Services\Versions_Storage_Service; |
| 13 |
|
| 14 |
/** |
| 15 |
* Main class controller. |
| 16 |
*/ |
| 17 |
class Api_Handler_Admin_Schedule_Refresh_Site extends Api_Handler_Admin implements Api_Handler_Admin_Interface { |
| 18 |
|
| 19 |
/** |
| 20 |
* The path for this endpoint. |
| 21 |
* |
| 22 |
* @var string |
| 23 |
*/ |
| 24 |
const ENDPOINT_PATH = '/schedule-site-version'; |
| 25 |
|
| 26 |
/** |
| 27 |
* The version for this endpoint. |
| 28 |
* |
| 29 |
* @var int |
| 30 |
*/ |
| 31 |
const ENDPOINT_VERSION = 1; |
| 32 |
|
| 33 |
const ACTION_NAME_SCHEDULE_REFRESH_SITE = 'force_refresh_scheduled_site_refresh'; |
| 34 |
|
| 35 |
/** |
| 36 |
* Method for registering the endpoints for this class. |
| 37 |
* |
| 38 |
* @return void |
| 39 |
*/ |
| 40 |
public function register_routes(): void { |
| 41 |
self::register_rest_endpoint( |
| 42 |
self::ENDPOINT_PATH, |
| 43 |
self::ENDPOINT_VERSION, |
| 44 |
array( |
| 45 |
'methods' => \WP_REST_Server::EDITABLE, |
| 46 |
'callback' => array( $this, 'schedule_refresh_site' ), |
| 47 |
'permission_callback' => array( $this, 'user_is_able_to_admin_force_refresh' ), |
| 48 |
), |
| 49 |
); |
| 50 |
|
| 51 |
self::register_rest_endpoint( |
| 52 |
self::ENDPOINT_PATH, |
| 53 |
self::ENDPOINT_VERSION, |
| 54 |
array( |
| 55 |
'methods' => \WP_REST_Server::ALLMETHODS, |
| 56 |
'callback' => array( $this, 'delete_schedule_refresh_site' ), |
| 57 |
'permission_callback' => array( $this, 'user_is_able_to_admin_force_refresh' ), |
| 58 |
), |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Method for registering all of our actions. |
| 64 |
* |
| 65 |
* @return void |
| 66 |
*/ |
| 67 |
public function register_actions(): void { |
| 68 |
add_action( self::ACTION_NAME_SCHEDULE_REFRESH_SITE, array( $this, 'executeSiteRefresh' ) ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Method for executing a scheduled site refresh. |
| 73 |
* |
| 74 |
* @param string $time The scheduled time. |
| 75 |
* |
| 76 |
* @return void |
| 77 |
*/ |
| 78 |
public function executeSiteRefresh( $time ): void { |
| 79 |
$site_version = Versions_Storage_Service::get_new_version(); |
| 80 |
Versions_Storage_Service::set_site_version( $site_version ); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Method to get all scheduled refreshes from a cron event. |
| 85 |
* |
| 86 |
* @param array $cron_event The cron event. |
| 87 |
* |
| 88 |
* @return array The scheduled refreshes |
| 89 |
*/ |
| 90 |
public static function get_scheduled_refreshes_from_cron_event( array $cron_event ) { |
| 91 |
$scheduled_refreshes = array(); |
| 92 |
foreach ( $cron_event as $event_name => $event_data ) { |
| 93 |
if ( self::ACTION_NAME_SCHEDULE_REFRESH_SITE === $event_name ) { |
| 94 |
array_push( |
| 95 |
$scheduled_refreshes, |
| 96 |
array( |
| 97 |
'timestamp' => $timestamp, |
| 98 |
) |
| 99 |
); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return $scheduled_refreshes; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Method for getting all scheduled refresh. |
| 108 |
* |
| 109 |
* @return mixed The scheduled events. |
| 110 |
*/ |
| 111 |
public static function get_scheduled_refreshes() { |
| 112 |
$scheduled_refreshes = array(); |
| 113 |
$cron = get_option( 'cron' ); |
| 114 |
|
| 115 |
if ( ! is_array( $cron ) ) { |
| 116 |
return $scheduled_refreshes; |
| 117 |
} |
| 118 |
|
| 119 |
foreach ( $cron as $timestamp => $cron_event ) { |
| 120 |
if ( ! is_array( $cron_event ) ) { |
| 121 |
continue; |
| 122 |
} |
| 123 |
|
| 124 |
$scheduled_refreshes = array_merge( |
| 125 |
$scheduled_refreshes, |
| 126 |
self::get_scheduled_refreshes_from_cron_event( $cron_event ), |
| 127 |
); |
| 128 |
} |
| 129 |
|
| 130 |
return $scheduled_refreshes; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* Method for deleting a scheduled refresh. |
| 135 |
* |
| 136 |
* @param \WP_REST_Request $request The request object. |
| 137 |
* |
| 138 |
* @return void |
| 139 |
*/ |
| 140 |
public function delete_schedule_refresh_site( \WP_REST_Request $request ): void { |
| 141 |
$scheduled_refresh = (int) $request->get_param( 'schedule_refresh_timestamp' ) ?? null; |
| 142 |
|
| 143 |
wp_clear_scheduled_hook( self::ACTION_NAME_SCHEDULE_REFRESH_SITE, array( $scheduled_refresh ) ); |
| 144 |
|
| 145 |
$this->return_api_response( |
| 146 |
202, |
| 147 |
'You\'ve successfully deleted a site refresh.', |
| 148 |
array( |
| 149 |
'scheduled_refresh_time' => $scheduled_refresh, |
| 150 |
) |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Method for refreshing the site version. |
| 156 |
* |
| 157 |
* @param \WP_REST_Request $request The request object. |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
public function schedule_refresh_site( \WP_REST_Request $request ): void { |
| 162 |
$scheduled_refresh = $request->get_param( 'schedule_refresh_timestamp' ) ?? null; |
| 163 |
$scheduled_refresh_time = strtotime( $scheduled_refresh ); |
| 164 |
|
| 165 |
wp_schedule_single_event( $scheduled_refresh_time, self::ACTION_NAME_SCHEDULE_REFRESH_SITE, array( $scheduled_refresh_time ) ); |
| 166 |
|
| 167 |
$this->return_api_response( |
| 168 |
201, |
| 169 |
'You\'ve successfully scheduled a site refresh.', |
| 170 |
array( |
| 171 |
'scheduled_refresh_time' => $scheduled_refresh_time, |
| 172 |
) |
| 173 |
); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Method for getting the endpoint for this service. |
| 178 |
* |
| 179 |
* @return string The service endpoint. |
| 180 |
*/ |
| 181 |
public static function get_rest_endpoint(): string { |
| 182 |
return self::get_formatted_rest_endpoint( self::ENDPOINT_PATH, self::ENDPOINT_VERSION ); |
| 183 |
} |
| 184 |
} |
| 185 |
|