Adapter
1 day ago
Analytics
1 day ago
Assets
1 day ago
BackgroundProcessing
1 day ago
CloningProcess
1 day ago
Collection
1 day ago
Command
1 day ago
Component
1 day ago
DI
1 day ago
Database
1 day ago
DependencyResolver
1 day ago
Exceptions
2 years ago
Experiments
1 day ago
Facades
1 day ago
Filesystem
1 day ago
Interfaces
1 day ago
Job
1 day ago
Language
1 day ago
Logger
1 day ago
Mails
1 day ago
Network
1 day ago
Newsfeed
1 day ago
Notices
1 day ago
Onboarding
1 day ago
Performance
1 day ago
Permalinks
1 day ago
Queue
1 day ago
Rest
1 day ago
Security
1 day ago
Settings
1 day ago
TemplateEngine
1 day ago
ThirdParty
1 day ago
Traits
1 day ago
Upgrade
1 day ago
Utils
1 day ago
AnalyticsServiceProvider.php
1 day ago
AssetServiceProvider.php
1 year ago
CommonServiceProvider.php
1 day ago
ErrorHandler.php
1 day ago
NoticeServiceProvider.php
1 year ago
SettingsServiceProvider.php
4 months ago
SiteInfo.php
1 day ago
Url.php
1 day ago
AnalyticsServiceProvider.php
121 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\ErrorCode; |
| 9 | use WPStaging\Framework\Analytics\AnalyticsGenericEventHandler; |
| 10 | use WPStaging\Framework\Analytics\AnalyticsSender; |
| 11 | use WPStaging\Framework\DI\FeatureServiceProvider; |
| 12 | use WPStaging\Framework\Notices\Notices; |
| 13 | use WPStaging\Framework\Security\Auth; |
| 14 | use WPStaging\Framework\Utils\Cache\Cache; |
| 15 | use WPStaging\Framework\Utils\Sanitize; |
| 16 | |
| 17 | class AnalyticsServiceProvider extends FeatureServiceProvider |
| 18 | { |
| 19 | |
| 20 | private $sanitize; |
| 21 | |
| 22 | public static function getFeatureTrigger() |
| 23 | { |
| 24 | return 'WPSTG_FEATURE_ANALYTICS'; |
| 25 | } |
| 26 | |
| 27 | protected function registerClasses() |
| 28 | { |
| 29 | $this->container->singleton(AnalyticsConsent::class); |
| 30 | $this->container->singleton(AnalyticsSender::class); |
| 31 | } |
| 32 | |
| 33 | protected function addHooks() |
| 34 | { |
| 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 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 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 | $errorCode = isset($_POST['error_code']) ? ErrorCode::sanitize($this->sanitize->htmlDecodeAndSanitize($_POST['error_code'])) : ''; |
| 68 | |
| 69 | AnalyticsEventDto::enqueueErrorEvent($jobId, $errorMessage, $errorCode); |
| 70 | }); |
| 71 | |
| 72 | |
| 73 | add_action("wp_ajax_wpstg_staging_job_error", function () { // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 74 | if (empty($_POST)) { |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | if (!$this->container->make(Auth::class)->isAuthenticatedRequest()) { |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | foreach (['error_message'] as $requiredKeys) { |
| 83 | if (!isset($_POST[$requiredKeys])) { |
| 84 | return; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | |
| 89 | $errorMessage = isset($_POST['error_message']) ? $this->sanitize->htmlDecodeAndSanitize($_POST['error_message']) : ''; |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | $cache = WPStaging::make(Cache::class); |
| 97 | $cache->setLifetime(-1); |
| 98 | $cache->setPath(WPStaging::getContentDir()); |
| 99 | |
| 100 | $options = $cache->get("clone_options"); |
| 101 | |
| 102 | $jobId = ''; |
| 103 | if (is_object($options) && property_exists($options, 'jobIdentifier')) { |
| 104 | $jobId = $options->jobIdentifier; |
| 105 | } |
| 106 | |
| 107 | if (empty($jobId)) { |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | $errorCode = isset($_POST['error_code']) ? ErrorCode::sanitize($this->sanitize->htmlDecodeAndSanitize($_POST['error_code'])) : ''; |
| 112 | |
| 113 | AnalyticsEventDto::enqueueErrorEvent($jobId, $errorMessage, $errorCode); |
| 114 | }); |
| 115 | |
| 116 | add_action('wp_ajax_wpstg_event_generic', $this->container->callback(AnalyticsGenericEventHandler::class, 'ajaxHandleGenericEvent')); // phpcs:ignore WPStaging.Security.AuthorizationChecked |
| 117 | |
| 118 | $this->container->make(AnalyticsSender::class)->maybeSend(); |
| 119 | } |
| 120 | } |
| 121 |