PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backup, Restore, Migration & Clone / 4.2.0
WP STAGING – WordPress Backup, Restore, Migration & Clone v4.2.0
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 1 year ago Analytics 1 year ago Assets 1 year ago BackgroundProcessing 1 year ago CloningProcess 1 year ago Collection 3 years ago Command 5 years ago Component 2 years ago DI 1 year ago Database 1 year ago DependencyResolver 2 years ago Exceptions 2 years ago Facades 1 year ago Filesystem 1 year ago Interfaces 5 years ago Job 1 year ago Language 1 year ago Logger 1 year ago Mails 1 year ago Network 1 year ago Newsfeed 1 year ago Notices 1 year ago Performance 1 year ago Permalinks 1 year ago Queue 1 year ago Rest 1 year ago Security 1 year ago Settings 1 year ago TemplateEngine 1 year ago ThirdParty 1 year ago Traits 1 year ago Utils 1 year ago AnalyticsServiceProvider.php 2 years ago AssetServiceProvider.php 1 year ago CommonServiceProvider.php 1 year ago ErrorHandler.php 2 years ago NoticeServiceProvider.php 2 years ago SettingsServiceProvider.php 2 years ago SiteInfo.php 1 year 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