Ajax
1 month ago
BackgroundProcessing
1 year ago
Dto
1 week ago
Exception
3 months ago
Interfaces
1 week ago
Jobs
5 months ago
Task
1 week ago
Traits
8 months ago
AbstractJob.php
1 month ago
JobProvider.php
1 year ago
JobServiceProvider.php
4 months ago
JobTransientCache.php
1 month ago
ProcessLock.php
1 year ago
JobServiceProvider.php
75 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Framework\Job; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\DI\FeatureServiceProvider; |
| 7 | use WPStaging\Framework\Job\Ajax\Cancel; |
| 8 | use WPStaging\Framework\Job\Ajax\Heartbeat; |
| 9 | use WPStaging\Framework\Job\Ajax\LoginUrl; |
| 10 | use WPStaging\Framework\Job\Ajax\PrepareCancel; |
| 11 | use WPStaging\Framework\Job\Dto\JobCancelDataDto; |
| 12 | use WPStaging\Framework\Job\Dto\JobDataDto; |
| 13 | use WPStaging\Framework\Job\Jobs\JobCancel; |
| 14 | use WPStaging\Framework\Logger\BackgroundLogger; |
| 15 | use WPStaging\Framework\Rest\Rest; |
| 16 | use WPStaging\Framework\Security\Auth; |
| 17 | |
| 18 | class JobServiceProvider extends FeatureServiceProvider |
| 19 | { |
| 20 | protected function registerClasses() |
| 21 | { |
| 22 | $this->container->singleton(BackgroundLogger::class); |
| 23 | |
| 24 | $this->container->when(JobCancel::class) |
| 25 | ->needs(JobDataDto::class) |
| 26 | ->give(JobCancelDataDto::class); |
| 27 | } |
| 28 | |
| 29 | protected function addHooks() |
| 30 | { |
| 31 | $this->enqueueAjaxListeners(); |
| 32 | |
| 33 | // This is needed for PHP 8.4 otherwise wordpress sent header and we cannot change it for event streaming. |
| 34 | add_filter('rest_pre_dispatch', $this->container->callback(BackgroundLogger::class, 'maybePrepareSseStream'), 10, 3); |
| 35 | add_action('rest_api_init', [$this, 'registerRestEndpoints']); |
| 36 | } |
| 37 | |
| 38 | protected function enqueueAjaxListeners() |
| 39 | { |
| 40 | add_action('wp_ajax_wpstg--job--heartbeat', $this->container->callback(Heartbeat::class, 'ajaxProcess')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 41 | add_action('wp_ajax_wpstg--job--prepare-cancel', $this->container->callback(PrepareCancel::class, 'ajaxPrepare')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 42 | add_action('wp_ajax_wpstg--job--cancel', $this->container->callback(Cancel::class, 'ajaxProcess')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 43 | add_action('wp_ajax_raw_wpstg--login-url', $this->container->callback(LoginUrl::class, 'ajaxLoginUrl')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 44 | |
| 45 | // no-priv |
| 46 | add_action('wp_ajax_nopriv_raw_wpstg--login-url', $this->container->callback(LoginUrl::class, 'ajaxLoginUrl')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 47 | add_action('wp_ajax_nopriv_wpstg--job--heartbeat', $this->container->callback(Heartbeat::class, 'ajaxProcess')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 48 | } |
| 49 | |
| 50 | public function registerRestEndpoints() |
| 51 | { |
| 52 | register_rest_route(Rest::WPSTG_ROUTE_NAMESPACE_V1, '/ping', [ |
| 53 | 'methods' => 'GET', |
| 54 | 'callback' => function () { |
| 55 | wp_send_json_success(); |
| 56 | }, |
| 57 | 'permission_callback' => function () { |
| 58 | /** @var Auth $auth */ |
| 59 | $auth = WPStaging::make(Auth::class); |
| 60 | if (!$auth->isAuthenticatedRequest()) { |
| 61 | return new \WP_Error('rest_forbidden', esc_html__('You are not allowed to access this resource.', 'wp-staging'), ['status' => 403]); |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | }, |
| 66 | ]); |
| 67 | |
| 68 | register_rest_route(Rest::WPSTG_ROUTE_NAMESPACE_V1, '/sse-logs', [ |
| 69 | 'methods' => 'GET', |
| 70 | 'callback' => $this->container->callback(BackgroundLogger::class, 'restEventStream'), |
| 71 | 'permission_callback' => $this->container->callback(BackgroundLogger::class, 'verifyRestRequest'), |
| 72 | ]); |
| 73 | } |
| 74 | } |
| 75 |