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