Menu_Bar_Stats.php
104 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Menu_Bar_Stats; |
| 4 | |
| 5 | use IAWP\Capability_Manager; |
| 6 | use IAWP\Date_Range\Date_Range; |
| 7 | use IAWP\Date_Range\Relative_Date_Range; |
| 8 | use IAWP\Illuminate_Builder; |
| 9 | use IAWP\Query; |
| 10 | use IAWP\Resource_Identifier; |
| 11 | use IAWP\Utils\Number_Formatter; |
| 12 | use IAWPSCOPED\Illuminate\Database\Query\Builder; |
| 13 | use IAWPSCOPED\Illuminate\Database\Query\JoinClause; |
| 14 | /** @internal */ |
| 15 | class Menu_Bar_Stats |
| 16 | { |
| 17 | /** |
| 18 | * @var ?Resource_Identifier |
| 19 | */ |
| 20 | private $current_resource_identifier; |
| 21 | private $views_today = 0; |
| 22 | private $views_yesterday = 0; |
| 23 | private $views_last_thirty = 0; |
| 24 | private $views_total = 0; |
| 25 | public function __construct() |
| 26 | { |
| 27 | $today = new Relative_Date_Range('TODAY'); |
| 28 | $yesterday = new Relative_Date_Range('YESTERDAY'); |
| 29 | $last_thirty = new Relative_Date_Range('LAST_THIRTY'); |
| 30 | $all_time = new Relative_Date_Range('ALL_TIME'); |
| 31 | if (\is_admin()) { |
| 32 | $this->current_resource_identifier = Resource_Identifier::for_resource_being_edited(); |
| 33 | } else { |
| 34 | $this->current_resource_identifier = Resource_Identifier::for_resource_being_viewed(); |
| 35 | } |
| 36 | if (!\is_null($this->current_resource_identifier)) { |
| 37 | $this->views_today = self::get_views_in_date_range($today); |
| 38 | $this->views_yesterday = self::get_views_in_date_range($yesterday); |
| 39 | $this->views_last_thirty = self::get_views_in_date_range($last_thirty); |
| 40 | $this->views_total = self::get_views_in_date_range($all_time); |
| 41 | } |
| 42 | } |
| 43 | /** |
| 44 | * @return bool |
| 45 | */ |
| 46 | public function is_enabled() : bool |
| 47 | { |
| 48 | if (!self::is_option_enabled()) { |
| 49 | return \false; |
| 50 | } |
| 51 | if (\is_null($this->current_resource_identifier)) { |
| 52 | return \false; |
| 53 | } |
| 54 | return \true; |
| 55 | } |
| 56 | /** |
| 57 | * @return array[] |
| 58 | */ |
| 59 | private function get_menu_bar_items() : array |
| 60 | { |
| 61 | $today = Number_Formatter::decimal($this->views_today); |
| 62 | $yesterday = Number_Formatter::decimal($this->views_yesterday); |
| 63 | $last_thirty = Number_Formatter::decimal($this->views_last_thirty); |
| 64 | $total = Number_Formatter::decimal($this->views_total); |
| 65 | return [['id' => 'iawp_admin_bar', 'title' => '<span class="ab-icon dashicons-analytics"></span>' . \sprintf('%s Views', \esc_html_x($today, 'X 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']]; |
| 66 | } |
| 67 | private function get_views_in_date_range(Date_Range $date_range) : int |
| 68 | { |
| 69 | $resources_table = Query::get_table_name(Query::RESOURCES); |
| 70 | $views_table = Query::get_table_name(Query::VIEWS); |
| 71 | $resource_statistics_query = Illuminate_Builder::get_builder(); |
| 72 | $resource = $this->current_resource_identifier; |
| 73 | $resource_statistics_query->selectRaw('COUNT(*) AS views')->from("{$resources_table} as resources")->join("{$views_table} AS views", function (JoinClause $join) { |
| 74 | $join->on('resources.id', '=', 'views.resource_id'); |
| 75 | })->where('resource', '=', $resource->type())->when($resource->has_meta(), function (Builder $query) use($resource) { |
| 76 | $query->where($resource->meta_key(), '=', $resource->meta_value()); |
| 77 | })->whereBetween('views.viewed_at', [$date_range->iso_start(), $date_range->iso_end()]); |
| 78 | $resource_statistics = $resource_statistics_query->get()->first(); |
| 79 | return $resource_statistics->views ?? 0; |
| 80 | } |
| 81 | public static function is_option_enabled() : bool |
| 82 | { |
| 83 | return \IAWPSCOPED\iawp()->get_option('iawp_disable_admin_toolbar_analytics', \false) === \false && Capability_Manager::can_view(); |
| 84 | } |
| 85 | public static function register() |
| 86 | { |
| 87 | \add_action('admin_bar_menu', function ($admin_bar) { |
| 88 | $menu_bar_stats = new self(); |
| 89 | if (!$menu_bar_stats->is_enabled()) { |
| 90 | return; |
| 91 | } |
| 92 | foreach ($menu_bar_stats->get_menu_bar_items() as $menu_bar_item) { |
| 93 | $is_group = $menu_bar_item['is_group'] ?? \false; |
| 94 | // Should the item be registered as a group or a node? |
| 95 | if ($is_group) { |
| 96 | $admin_bar->add_group($menu_bar_item); |
| 97 | } else { |
| 98 | $admin_bar->add_node($menu_bar_item); |
| 99 | } |
| 100 | } |
| 101 | }, 100); |
| 102 | } |
| 103 | } |
| 104 |