AJAX
3 months ago
Admin_Page
3 months ago
Click_Tracking
5 months ago
ColumnOptions
6 months ago
Cron
9 months ago
Custom_WordPress_Columns
8 months ago
Data_Pruning
5 months ago
Date_Picker
3 months ago
Date_Range
5 months ago
Ecommerce
5 months ago
Email_Reports
3 months ago
Examiner
3 months ago
Favicon
5 months ago
Form_Submissions
5 months ago
Integrations
3 months ago
Interval
1 year ago
Journey
5 months ago
Migrations
5 months ago
Models
5 months ago
Overview
3 months ago
Public_API
5 months ago
Rows
3 months ago
Statistics
3 months ago
Tables
3 months ago
Utils
5 months ago
Views
5 months ago
WooCommerceOrderMetaBox
5 months ago
ActivationLifecycle.php
3 months ago
Admin_Bar_Stats.php
3 months ago
Appearance.php
1 year ago
Campaign_Builder.php
3 months ago
Capability_Manager.php
3 months ago
Chart.php
3 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
3 months ago
ComplianzIntegration.php
3 months ago
Cron_Job.php
5 months ago
Cron_Manager.php
5 months ago
Current_Traffic_Finder.php
1 year ago
Dashboard_Options.php
6 months ago
Dashboard_Widget.php
2 years ago
Database.php
8 months ago
Database_Manager.php
5 months ago
Empty_Report_Option.php
2 years ago
Env.php
6 months ago
Examiner_Config.php
11 months ago
FetchFaviconsJob.php
5 months ago
Filters.php
3 months ago
Geo_Database_Health_Check_Job.php
9 months ago
Geo_Database_Manager.php
6 months ago
Geoposition.php
1 year ago
Icon_Directory.php
6 months ago
Icon_Directory_Factory.php
2 years ago
Illuminate_Builder.php
10 months ago
Independent_Analytics.php
5 months ago
Interrupt.php
3 months ago
Known_Referrers.php
6 months ago
MainWP.php
1 year ago
Map.php
9 months ago
Map_Data.php
1 year ago
Migration_Fixer_Job.php
5 months ago
Patch.php
1 year ago
Payload_Validator.php
9 months ago
Plugin_Conflict_Detector.php
6 months ago
Plugin_Group.php
6 months ago
Plugin_Group_Option.php
2 years ago
Query.php
1 year ago
Query_Taps.php
3 months ago
Quick_Stats.php
3 months ago
REST_API.php
3 months ago
Real_Time.php
3 months ago
Report.php
1 year ago
Report_Finder.php
6 months ago
Report_Options_Parser.php
9 months ago
Resource_Identifier.php
9 months ago
Settings.php
3 months ago
Sort_Configuration.php
9 months ago
Tables.php
8 months ago
Track_Resource_Changes.php
1 year ago
View_Counter.php
6 months ago
Views_Over_Time_Finder.php
1 year ago
VisitorSaltRefreshInterval.php
6 months ago
WP_Option_Cache_Bust.php
2 years ago
MainWP.php
70 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWPSCOPED\Carbon\CarbonImmutable; |
| 6 | use IAWP\Date_Range\Relative_Date_Range; |
| 7 | use IAWP\Statistics\Intervals\Intervals; |
| 8 | use IAWP\Tables\Table_Pages; |
| 9 | use IAWP\Utils\Number_Formatter; |
| 10 | use IAWP\Utils\Security; |
| 11 | use IAWP\Utils\Timezone; |
| 12 | /** @internal */ |
| 13 | class MainWP |
| 14 | { |
| 15 | public static function initialize() |
| 16 | { |
| 17 | \add_filter('mainwp_site_sync_others_data', function ($information, $data = []) { |
| 18 | return self::attach_analytics($information, $data); |
| 19 | }, 10, 2); |
| 20 | \add_filter('mainwp_child_prepared_reports_data', function ($data, $sections, $other_tokens, $options) { |
| 21 | return self::attach_analytics_to_pro_report($data, $sections, $other_tokens, $options); |
| 22 | }, 10, 4); |
| 23 | } |
| 24 | private static function attach_analytics(array $information, array $data) : array |
| 25 | { |
| 26 | $should_sync_analytics = \array_key_exists('iawp_sync_analytics', $data) && \true === $data['iawp_sync_analytics']; |
| 27 | if (!$should_sync_analytics) { |
| 28 | return $information; |
| 29 | } |
| 30 | try { |
| 31 | $table = new Table_Pages(); |
| 32 | $statistics_class = $table->group()->statistics_class(); |
| 33 | $date_range = new Relative_Date_Range('LAST_THIRTY'); |
| 34 | $chart_interval = Intervals::default_for($date_range->number_of_days()); |
| 35 | $statistics = new $statistics_class($date_range, null, $chart_interval); |
| 36 | $views = $statistics->get_statistic('views'); |
| 37 | $visitors = $statistics->get_statistic('visitors'); |
| 38 | $labels = \array_map(function ($data_point) use($statistics) { |
| 39 | return Security::json_encode($statistics->chart_interval()->get_label_for($data_point[0])); |
| 40 | }, $views->statistic_over_time()); |
| 41 | $views_over_time = \array_map(function ($data_point) { |
| 42 | return $data_point[1]; |
| 43 | }, $views->statistic_over_time()); |
| 44 | $visitors_over_time = \array_map(function ($data_point) { |
| 45 | return $data_point[1]; |
| 46 | }, $visitors->statistic_over_time()); |
| 47 | $information['iawp_analytics'] = ['analytics_dashboard_url' => \IAWPSCOPED\iawp_dashboard_url(), 'labels' => $labels, 'views_over_time' => $views_over_time, 'visitors_over_time' => $visitors_over_time, 'views' => Number_Formatter::integer($views->value()), 'visitors' => Number_Formatter::integer($visitors->value())]; |
| 48 | } catch (\Throwable $e) { |
| 49 | // Do nothing |
| 50 | } |
| 51 | return $information; |
| 52 | } |
| 53 | private static function attach_analytics_to_pro_report($data, $sections, $other_tokens, $options) |
| 54 | { |
| 55 | try { |
| 56 | $from = CarbonImmutable::parse($options['date_from'], Timezone::site_timezone()); |
| 57 | $to = CarbonImmutable::parse($options['date_to'], Timezone::site_timezone()); |
| 58 | $analytics = \IAWPSCOPED\iawp_analytics($from->toDate(), $to->toDate()); |
| 59 | $data['other_tokens_data']['body']['[iawp.views]'] = Number_Formatter::integer($analytics->views); |
| 60 | $data['other_tokens_data']['header']['[iawp.views]'] = Number_Formatter::integer($analytics->views); |
| 61 | $data['other_tokens_data']['body']['[iawp.visitors]'] = Number_Formatter::integer($analytics->visitors); |
| 62 | $data['other_tokens_data']['header']['[iawp.visitors]'] = Number_Formatter::integer($analytics->visitors); |
| 63 | $data['other_tokens_data']['body']['[iawp.sessions]'] = Number_Formatter::integer($analytics->sessions); |
| 64 | $data['other_tokens_data']['header']['[iawp.sessions]'] = Number_Formatter::integer($analytics->sessions); |
| 65 | } catch (\Throwable $e) { |
| 66 | } |
| 67 | return $data; |
| 68 | } |
| 69 | } |
| 70 |