Adapter
2 months ago
Analytics
1 day ago
Assets
1 day ago
BackgroundProcessing
1 week ago
CloningProcess
1 day 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 day 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 day 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
116 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, '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 | $jobId = ''; |
| 100 | if (is_object($options) && property_exists($options, 'jobIdentifier')) { |
| 101 | $jobId = $options->jobIdentifier; |
| 102 | } |
| 103 | |
| 104 | if (empty($jobId)) { |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | AnalyticsEventDto::enqueueErrorEvent($jobId, $errorMessage); |
| 109 | }); |
| 110 | |
| 111 | add_action('wp_ajax_wpstg_event_generic', $this->container->callback(AnalyticsGenericEventHandler::class, 'ajaxHandleGenericEvent')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 112 | |
| 113 | $this->container->make(AnalyticsSender::class)->maybeSend(); |
| 114 | } |
| 115 | } |
| 116 |