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