Adapter
1 year ago
Analytics
1 year ago
Assets
1 year 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
2 years ago
Interfaces
5 years ago
Mails
1 year ago
Network
2 years ago
Notices
2 years ago
Permalinks
5 years ago
Queue
1 year ago
Rest
2 years ago
Security
2 years ago
Settings
1 year ago
Staging
1 year ago
TemplateEngine
4 years ago
ThirdParty
2 years ago
Traits
1 year ago
Utils
1 year ago
AnalyticsServiceProvider.php
2 years ago
AssetServiceProvider.php
5 years ago
CommonServiceProvider.php
2 years 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 |