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