AJAX
5 months ago
Admin_Page
5 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
5 months ago
Date_Range
5 months ago
Ecommerce
5 months ago
Email_Reports
5 months ago
Examiner
11 months ago
Favicon
5 months ago
Form_Submissions
5 months ago
Integrations
6 months ago
Interval
1 year ago
Journey
5 months ago
Migrations
5 months ago
Models
5 months ago
Overview
5 months ago
Public_API
5 months ago
Rows
5 months ago
Statistics
5 months ago
Tables
5 months ago
Utils
5 months ago
Views
5 months ago
WooCommerceOrderMetaBox
5 months ago
Admin_Bar_Stats.php
5 months ago
Appearance.php
1 year ago
Campaign_Builder.php
5 months ago
Capability_Manager.php
1 year ago
Chart.php
9 months ago
Chart_Data.php
1 year ago
Click_Tracking.php
1 year 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
5 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
1 year 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
5 months ago
Quick_Stats.php
11 months ago
REST_API.php
6 months ago
Real_Time.php
5 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
6 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
Admin_Bar_Stats.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP; |
| 4 | |
| 5 | use IAWP\Date_Range\Date_Range; |
| 6 | use IAWP\Date_Range\Relative_Date_Range; |
| 7 | use IAWP\Utils\Number_Formatter; |
| 8 | use IAWPSCOPED\Illuminate\Database\Query\Builder; |
| 9 | use IAWPSCOPED\Illuminate\Database\Query\JoinClause; |
| 10 | /** @internal */ |
| 11 | class Admin_Bar_Stats |
| 12 | { |
| 13 | /** |
| 14 | * @var ?Resource_Identifier |
| 15 | */ |
| 16 | private $current_resource_identifier; |
| 17 | private $views_today = 0; |
| 18 | private $views_yesterday = 0; |
| 19 | private $views_last_thirty = 0; |
| 20 | private $views_total = 0; |
| 21 | public function __construct() |
| 22 | { |
| 23 | $today = new Relative_Date_Range('TODAY'); |
| 24 | $yesterday = new Relative_Date_Range('YESTERDAY'); |
| 25 | $last_thirty = new Relative_Date_Range('LAST_THIRTY'); |
| 26 | $all_time = new Relative_Date_Range('ALL_TIME'); |
| 27 | if (\is_admin()) { |
| 28 | $this->current_resource_identifier = \IAWP\Resource_Identifier::for_resource_being_edited(); |
| 29 | } else { |
| 30 | $this->current_resource_identifier = \IAWP\Resource_Identifier::for_resource_being_viewed(); |
| 31 | } |
| 32 | if (!\is_null($this->current_resource_identifier)) { |
| 33 | $this->views_today = self::get_views_in_date_range($today); |
| 34 | $this->views_yesterday = self::get_views_in_date_range($yesterday); |
| 35 | $this->views_last_thirty = self::get_views_in_date_range($last_thirty); |
| 36 | $this->views_total = self::get_views_in_date_range($all_time); |
| 37 | } |
| 38 | } |
| 39 | /** |
| 40 | * @return bool |
| 41 | */ |
| 42 | public function is_enabled() : bool |
| 43 | { |
| 44 | if (!self::is_option_enabled()) { |
| 45 | return \false; |
| 46 | } |
| 47 | if (\is_null($this->current_resource_identifier)) { |
| 48 | return \false; |
| 49 | } |
| 50 | return \true; |
| 51 | } |
| 52 | /** |
| 53 | * @return array[] |
| 54 | */ |
| 55 | private function get_menu_bar_items() : array |
| 56 | { |
| 57 | $today = Number_Formatter::decimal($this->views_today); |
| 58 | $yesterday = Number_Formatter::decimal($this->views_yesterday); |
| 59 | $last_thirty = Number_Formatter::decimal($this->views_last_thirty); |
| 60 | $total = Number_Formatter::decimal($this->views_total); |
| 61 | return [['id' => 'iawp_admin_bar', 'title' => '<span class="ab-icon dashicons-analytics"></span>' . \sprintf('%s %s', $today, \esc_html__('Views', 'independent-analytics')), 'meta' => ['class' => 'iawp_admin_bar_button']], ['id' => 'iawp_admin_bar_group_title', 'title' => '<span>' . \esc_html__('Date', 'independent-analytics') . '</span> ' . '<span>' . \esc_html__('Views', 'independent-analytics') . '</span>', 'parent' => 'iawp_admin_bar'], ['id' => 'iawp_admin_bar_today', 'title' => '<span>' . \esc_html__('Today:', 'independent-analytics') . '</span> <span>' . \esc_html__($today) . '</span>', 'parent' => 'iawp_admin_bar'], ['id' => 'iawp_admin_bar_yesterday', 'title' => '<span>' . \esc_html__('Yesterday:', 'independent-analytics') . '</span> <span>' . \esc_html__($yesterday) . '</span>', 'parent' => 'iawp_admin_bar'], ['id' => 'iawp_admin_bar_last_thirty', 'title' => '<span>' . \esc_html__('Last 30 Days:', 'independent-analytics') . '</span> <span>' . \esc_html__($last_thirty) . '</span>', 'parent' => 'iawp_admin_bar'], ['id' => 'iawp_admin_bar_total', 'title' => '<span>' . \esc_html__('All Time:', 'independent-analytics') . '</span> <span>' . \esc_html__($total) . '</span>', 'parent' => 'iawp_admin_bar'], ['id' => 'iawp_admin_bar_dashboard_group', 'parent' => 'iawp_admin_bar', 'is_group' => \true], ['id' => 'iawp_admin_bar_dashboard_link', 'title' => \esc_html__('Analytics Dashboard', 'independent-analytics') . ' →', 'href' => \esc_url(\IAWPSCOPED\iawp_dashboard_url()), 'parent' => 'iawp_admin_bar_dashboard_group']]; |
| 62 | } |
| 63 | private function get_views_in_date_range(Date_Range $date_range) : int |
| 64 | { |
| 65 | $resources_table = \IAWP\Query::get_table_name(\IAWP\Query::RESOURCES); |
| 66 | $views_table = \IAWP\Query::get_table_name(\IAWP\Query::VIEWS); |
| 67 | $resource_statistics_query = \IAWP\Illuminate_Builder::new(); |
| 68 | $resource = $this->current_resource_identifier; |
| 69 | $resource_statistics_query->selectRaw('COUNT(*) AS views')->from("{$resources_table} as resources")->join("{$views_table} AS views", function (JoinClause $join) { |
| 70 | $join->on('resources.id', '=', 'views.resource_id'); |
| 71 | })->where('resource', '=', $resource->type())->when($resource->has_meta(), function (Builder $query) use($resource) { |
| 72 | $query->where($resource->meta_key(), '=', $resource->meta_value()); |
| 73 | })->whereBetween('views.viewed_at', [$date_range->iso_start(), $date_range->iso_end()]); |
| 74 | $resource_statistics = $resource_statistics_query->get()->first(); |
| 75 | return $resource_statistics->views ?? 0; |
| 76 | } |
| 77 | public static function is_option_enabled() : bool |
| 78 | { |
| 79 | return \IAWPSCOPED\iawp()->get_option('iawp_disable_admin_toolbar_analytics', \false) === \false && \IAWP\Capability_Manager::can_view(); |
| 80 | } |
| 81 | public static function register() |
| 82 | { |
| 83 | \add_action('admin_bar_menu', function ($admin_bar) { |
| 84 | $admin_bar_stats = new self(); |
| 85 | if (!$admin_bar_stats->is_enabled()) { |
| 86 | return; |
| 87 | } |
| 88 | foreach ($admin_bar_stats->get_menu_bar_items() as $menu_bar_item) { |
| 89 | $is_group = $menu_bar_item['is_group'] ?? \false; |
| 90 | // Should the item be registered as a group or a node? |
| 91 | if ($is_group) { |
| 92 | $admin_bar->add_group($menu_bar_item); |
| 93 | } else { |
| 94 | $admin_bar->add_node($menu_bar_item); |
| 95 | } |
| 96 | } |
| 97 | }, 100); |
| 98 | } |
| 99 | } |
| 100 |