PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 3.8.2
WP STAGING – WordPress Backup, Restore, Migration & Clone v3.8.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 / AnalyticsServiceProvider.php
wp-staging / Framework Last commit date
Adapter 2 years ago Analytics 2 years ago Assets 2 years ago Auth 1 year ago BackgroundProcessing 2 years ago CloningProcess 2 years ago Collection 3 years ago Command 5 years ago Component 2 years ago DI 2 years ago Database 2 years ago DependencyResolver 2 years ago Exceptions 2 years ago Facades 2 years ago Filesystem 1 year ago Interfaces 5 years ago Mails 1 year ago Network 2 years ago Notices 2 years ago Permalinks 1 year ago Queue 2 years ago Rest 2 years ago Security 1 year ago Settings 1 year ago Staging 2 years ago TemplateEngine 4 years ago ThirdParty 2 years ago Traits 1 year ago Utils 2 years ago AnalyticsServiceProvider.php 2 years ago AssetServiceProvider.php 5 years ago CommonServiceProvider.php 1 year ago ErrorHandler.php 2 years ago NoticeServiceProvider.php 2 years ago SettingsServiceProvider.php 2 years ago SiteInfo.php 2 years ago Url.php 3 years ago
AnalyticsServiceProvider.php
112 lines
1 <?php
2
3 namespace WPStaging\Framework;
4
5 use WPStaging\Core\WPStaging;
6 use WPStaging\Framework\Analytics\AnalyticsConsent;
7 use WPStaging\Framework\Analytics\AnalyticsEventDto;
8 use WPStaging\Framework\Analytics\AnalyticsSender;
9 use WPStaging\Framework\DI\FeatureServiceProvider;
10 use WPStaging\Framework\Security\Auth;
11 use WPStaging\Framework\Utils\Cache\Cache;
12 use WPStaging\Framework\Utils\Sanitize;
13
14 class AnalyticsServiceProvider extends FeatureServiceProvider
15 {
16 /** @var Sanitize */
17 private $sanitize;
18
19 public static function getFeatureTrigger()
20 {
21 return 'WPSTG_FEATURE_ANALYTICS';
22 }
23
24 protected function registerClasses()
25 {
26 $this->container->singleton(AnalyticsConsent::class);
27 $this->container->singleton(AnalyticsSender::class);
28 }
29
30 protected function addHooks()
31 {
32 add_action('wpstg.admin_notices', $this->container->callback(AnalyticsConsent::class, 'maybeShowConsentNotice'));
33 add_action('wpstg.admin_notices', $this->container->callback(AnalyticsConsent::class, 'maybeShowConsentFailureNotice'));
34 add_action('admin_init', $this->container->callback(AnalyticsConsent::class, 'listenForConsent'));
35
36 $this->sanitize = WPStaging::make(Sanitize::class);
37
38 /*
39 * Analytics error detection for Backup actions
40 *
41 * The AJAX event name avoids using "analytics_error" on purpose to
42 * avoid ad blocks from blocking the request from happening.
43 *
44 * "analytics" should never be mentioned in JavaScript, only on server-side.
45 */
46 add_action("wp_ajax_wpstg_job_error", function () { // phpcs:ignore WPStaging.Security.AuthorizationChecked
47 if (empty($_POST)) {
48 return;
49 }
50
51 if (!$this->container->make(Auth::class)->isAuthenticatedRequest()) {
52 return;
53 }
54
55 foreach (['error_message', 'job_id'] as $requiredKeys) {
56 if (!isset($_POST[$requiredKeys])) {
57 return;
58 }
59 }
60
61 $errorMessage = isset($_POST['error_message']) ? $this->sanitize->htmlDecodeAndSanitize($_POST['error_message']) : '';
62
63 $jobId = isset($_POST['job_id']) ? $this->sanitize->htmlDecodeAndSanitize($_POST['job_id']) : '';
64
65 AnalyticsEventDto::enqueueErrorEvent($jobId, $errorMessage);
66 });
67
68 // Analytics error detection for Staging actions
69 add_action("wp_ajax_wpstg_staging_job_error", function () { // phpcs:ignore WPStaging.Security.AuthorizationChecked
70 if (empty($_POST)) {
71 return;
72 }
73
74 if (!$this->container->make(Auth::class)->isAuthenticatedRequest()) {
75 return;
76 }
77
78 foreach (['error_message'] as $requiredKeys) {
79 if (!isset($_POST[$requiredKeys])) {
80 return;
81 }
82 }
83
84 // prevent emptying HTML string, as Staging errors might be returned in HTML (?)
85 $errorMessage = isset($_POST['error_message']) ? $this->sanitize->htmlDecodeAndSanitize($_POST['error_message']) : '';
86
87 /**
88 * Get the "options" object from cache
89 * @see \WPStaging\Backend\Modules\Jobs\Job::__construct
90 * @var Cache $cache
91 */
92 $cache = WPStaging::make(Cache::class);
93 $cache->setLifetime(-1); // Non-expireable file
94 $cache->setPath(WPStaging::getContentDir());
95
96 $options = $cache->get("clone_options");
97
98 if (is_object($options) && property_exists($options, 'jobIdentifier')) {
99 $jobId = $options->jobIdentifier;
100 }
101
102 if (empty($jobId)) {
103 return;
104 }
105
106 AnalyticsEventDto::enqueueErrorEvent($jobId, $errorMessage);
107 });
108
109 $this->container->make(AnalyticsSender::class)->maybeSend();
110 }
111 }
112