PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.9.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.9.2
4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Framework / Job / JobServiceProvider.php
wp-staging / Framework / Job Last commit date
Ajax 2 months ago BackgroundProcessing 1 year ago Dto 2 weeks ago Exception 3 months ago Interfaces 2 weeks ago Jobs 5 months ago Task 2 weeks ago Traits 9 months ago AbstractJob.php 1 day ago JobProvider.php 1 year ago JobServiceProvider.php 4 months ago JobTransientCache.php 2 months 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