PluginProbe ʕ •ᴥ•ʔ
Independent Analytics – WordPress Analytics Plugin / 2.3.1
Independent Analytics – WordPress Analytics Plugin v2.3.1
2.15.0 2.14.10 trunk 1.1 1.10 1.10.1 1.11 1.12 1.13 1.14 1.15 1.16 1.17 1.17.1 1.17.2 1.17.3 1.17.4 1.18 1.18.1 1.19.0 1.19.1 1.2 1.20.0 1.21.0 1.22.0 1.22.1 1.23.0 1.23.1 1.24.0 1.24.1 1.25.0 1.25.1 1.26.0 1.27.0 1.28.0 1.28.1 1.28.2 1.28.3 1.29.0 1.3 1.30.0 1.30.1 1.4 1.5 1.6 1.7 1.8 1.9 2.0.0 2.0.1 2.1.4 2.1.5 2.1.6 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.10 2.11.2 2.11.3 2.11.4 2.11.5 2.11.6 2.11.7 2.11.8 2.11.9 2.12.0 2.12.1 2.12.2 2.13.1 2.13.2 2.13.5 2.13.6 2.14.0 2.14.1 2.14.2 2.14.4 2.14.6 2.14.7 2.14.8 2.14.9 2.2.0 2.2.1 2.3.1 2.3.2 2.4.2 2.4.3 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.6.3 2.6.4 2.7.0 2.7.1 2.7.2 2.7.3 2.8.2 2.8.3 2.8.4 2.8.5 2.8.6 2.8.7 2.8.8 2.8.9 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7
independent-analytics / IAWP / View_Counter.php
independent-analytics / IAWP Last commit date
AJAX 2 years ago Admin_Page 2 years ago Date_Range 2 years ago Filter_Lists 2 years ago Interval 2 years ago Menu_Bar_Stats 2 years ago Migrations 2 years ago Models 2 years ago Public_API 2 years ago Rows 2 years ago Statistics 2 years ago Tables 2 years ago Utils 2 years ago Campaign_Builder.php 2 years ago Capability_Manager.php 2 years ago Chart.php 2 years ago Chart_Geo.php 2 years ago Cron_Manager.php 2 years ago Current_Traffic_Finder.php 2 years ago Dashboard_Options.php 2 years ago Dashboard_Widget.php 2 years ago Database.php 2 years ago Database_Manager.php 2 years ago Email_Chart.php 2 years ago Email_Reports.php 2 years ago Empty_Report_Option.php 2 years ago Env.php 2 years ago Filters.php 2 years ago Geo_Database_Background_Job.php 2 years ago Geo_Database_Manager.php 2 years ago Geoposition.php 2 years ago Icon_Directory.php 2 years ago Icon_Directory_Factory.php 2 years ago Illuminate_Builder.php 2 years ago Independent_Analytics.php 2 years ago Interrupt.php 2 years ago Known_Referrers.php 2 years ago Plugin_Conflict_Detector.php 2 years ago Query.php 2 years ago Quick_Stats.php 2 years ago REST_API.php 2 years ago Real_Time.php 2 years ago Report.php 2 years ago Report_Finder.php 2 years ago Report_Options_Parser.php 2 years ago Resource_Identifier.php 2 years ago Settings.php 2 years ago Sort_Configuration.php 2 years ago Track_Resource_Changes.php 2 years ago View.php 2 years ago View_Counter.php 2 years ago Visitors_Over_Time_Finder.php 2 years ago WP_Option_Cache_Bust.php 2 years ago WooCommerce_Order.php 2 years ago WooCommerce_Referrer_Meta_Box.php 2 years ago
View_Counter.php
154 lines
1 <?php
2
3 namespace IAWP;
4
5 use IAWP\Date_Range\Relative_Date_Range;
6 use IAWP\Utils\Number_Formatter;
7 use IAWP\Utils\Security;
8 use IAWPSCOPED\Illuminate\Database\Query\Builder;
9 use IAWPSCOPED\Illuminate\Database\Query\JoinClause;
10 /** @internal */
11 class View_Counter
12 {
13 public function __construct()
14 {
15 \add_action('the_content', [$this, 'output_counter']);
16 \add_action('init', [$this, 'add_shortcode']);
17 \add_action('add_meta_boxes', [$this, 'maybe_add_meta_box'], 10);
18 \add_action('save_post', [$this, 'save_metabox_settings']);
19 }
20 public function output_counter($content)
21 {
22 if (!$this->passes_checks()) {
23 return $content;
24 }
25 $position = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_position', 'after');
26 $counter = $this->get_counter_html();
27 if ($position == 'before' || $position == 'both') {
28 $content = $counter . $content;
29 }
30 if ($position == 'after' || $position == 'both') {
31 $content .= $counter;
32 }
33 return $content;
34 }
35 public function get_counter_html($label = null, $icon = null)
36 {
37 $current_resource = \IAWP\Resource_Identifier::for_resource_being_viewed();
38 // Get stats for individual posts in the loop if shortcode added to each post
39 global $post;
40 if ($post->ID != $current_resource->meta_value() && \is_main_query() && \in_the_loop()) {
41 $current_resource = \IAWP\Resource_Identifier::for_post_id($post->ID);
42 }
43 if (\is_null($current_resource)) {
44 return;
45 }
46 $view_count = $this->get_view_count($current_resource);
47 if (\IAWPSCOPED\iawp()->get_option('iawp_view_counter_manual_adjustment', \false)) {
48 $view_count += \intval(\get_post_meta($current_resource->meta_value(), 'iawp_view_counter_adjustment', \true));
49 }
50 $view_count = Number_Formatter::decimal($view_count);
51 if (\is_null($label)) {
52 $default = \function_exists('IAWPSCOPED\\pll__') ? pll__('Views:', 'independent-analytics') : \__('Views:', 'independent-analytics');
53 $label = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_label', $default);
54 }
55 if (\is_null($icon)) {
56 $icon = \get_option('iawp_view_counter_icon', \true);
57 }
58 if ($icon) {
59 $svg = '<svg height="20" viewBox="0 0 192 192" width="20" fill="currentColor" style="margin-right:6px; margin-top:-2px;"><path d="m16 176v-136h-16v144a8 8 0 0 0 8 8h184v-16z"/><path d="m72 112a8 8 0 0 0 -8-8h-24a8 8 0 0 0 -8 8v56h40z"/><path d="m128 80a8 8 0 0 0 -8-8h-24a8 8 0 0 0 -8 8v88h40z"/><path d="m184 48a8 8 0 0 0 -8-8h-24a8 8 0 0 0 -8 8v120h40z"/></svg>';
60 $label = $svg . ' ' . $label;
61 }
62 return '<div class="iawp-view-counter" style="display: flex;"><span class="view-counter-text" style="display: flex; align-items: center;">' . Security::svg($label) . '</span> <span class="view-counter-value" style="margin-left: 3px;">' . \esc_html($view_count) . '</span></div>';
63 }
64 public function add_shortcode()
65 {
66 \add_shortcode('iawp_view_counter', [$this, 'shortcode']);
67 }
68 public function shortcode($atts)
69 {
70 $a = \shortcode_atts(['label' => \IAWPSCOPED\iawp()->get_option('iawp_view_counter_label', \esc_html__('Views:', 'independent-analytics')), 'icon' => \true], $atts);
71 return $this->get_counter_html($a['label'], $a['icon']);
72 }
73 public function maybe_add_meta_box() : void
74 {
75 if (!\IAWPSCOPED\iawp()->get_option('iawp_view_counter_manual_adjustment', \false)) {
76 return;
77 }
78 foreach (\IAWPSCOPED\iawp()->get_option('iawp_view_counter_post_types', []) as $screen) {
79 \add_meta_box('iawp-view-counter-adjustment', \esc_html__('View Counter Adjustment', 'independent-analytics'), [$this, 'render_meta_box_content'], $screen, 'side');
80 }
81 }
82 public function render_meta_box_content() : void
83 {
84 global $post;
85 echo '<p>' . \esc_html__('Increase count by:', 'independent-analytics') . '<input type="number" name="iawp_view_counter_adjustment" id="iawp_view_counter_adjustment"
86 value="' . \esc_attr(\get_post_meta($post->ID, 'iawp_view_counter_adjustment', \true)) . '"
87 placeholder="0" style="max-width: 80px; margin-left: 8px" />
88 <a class="info-link"
89 href="https://independentwp.com/knowledgebase/dashboard/display-view-counter/"
90 target="_blank"
91 style="text-decoration:none;float:right;margin-top:4px">
92 <span class="dashicons dashicons-editor-help"></span>
93 </a>
94 </p>';
95 }
96 public function save_metabox_settings(int $post_id)
97 {
98 if (\array_key_exists('iawp_view_counter_adjustment', $_POST)) {
99 \update_post_meta($post_id, 'iawp_view_counter_adjustment', \absint($_POST['iawp_view_counter_adjustment']));
100 }
101 }
102 private function passes_checks() : bool
103 {
104 if (!\is_singular() || !\is_main_query()) {
105 return \false;
106 }
107 if (\IAWPSCOPED\iawp()->get_option('iawp_view_counter_enable', \false) == \false) {
108 return \false;
109 }
110 if (!\in_array(\get_post_type(), \IAWPSCOPED\iawp()->get_option('iawp_view_counter_post_types', []))) {
111 return \false;
112 }
113 if (\IAWPSCOPED\iawp()->get_option('iawp_view_counter_private', \false) && !\is_user_logged_in()) {
114 return \false;
115 }
116 $exclude = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_exclude', '');
117 if ($exclude != '') {
118 $exclude = \explode(',', $exclude);
119 if (\in_array(\get_the_ID(), $exclude)) {
120 return \false;
121 }
122 }
123 return \true;
124 }
125 private function get_view_count(\IAWP\Resource_Identifier $resource) : int
126 {
127 $resources_table = \IAWP\Query::get_table_name(\IAWP\Query::RESOURCES);
128 $views_table = \IAWP\Query::get_table_name(\IAWP\Query::VIEWS);
129 $is_today = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_views_to_count', 'total') === 'today';
130 $is_last_thirty = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_views_to_count', 'total') === 'last_thirty';
131 $is_this_month = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_views_to_count', 'total') === 'this_month';
132 $is_last_month = \IAWPSCOPED\iawp()->get_option('iawp_view_counter_views_to_count', 'total') === 'last_month';
133 $query = \IAWP\Illuminate_Builder::get_builder()->selectRaw('COUNT(views.id) AS views')->from($resources_table, 'resources')->leftJoin("{$views_table} AS views", function (JoinClause $join) {
134 $join->on('resources.id', '=', 'views.resource_id');
135 })->where('resource', '=', $resource->type())->when($resource->has_meta(), function (Builder $query) use($resource) {
136 $query->where($resource->meta_key(), '=', $resource->meta_value());
137 })->when($is_today, function (Builder $query) {
138 $today = new Relative_Date_Range('TODAY');
139 $query->whereBetween('viewed_at', [$today->iso_start(), $today->iso_end()]);
140 })->when($is_last_thirty, function (Builder $query) {
141 $today = new Relative_Date_Range('LAST_THIRTY');
142 $query->whereBetween('viewed_at', [$today->iso_start(), $today->iso_end()]);
143 })->when($is_this_month, function (Builder $query) {
144 $today = new Relative_Date_Range('THIS_MONTH');
145 $query->whereBetween('viewed_at', [$today->iso_start(), $today->iso_end()]);
146 })->when($is_last_month, function (Builder $query) {
147 $today = new Relative_Date_Range('LAST_MONTH');
148 $query->whereBetween('viewed_at', [$today->iso_start(), $today->iso_end()]);
149 })->groupBy('resources.id');
150 $views = $query->value('views');
151 return \is_null($views) ? 0 : $views;
152 }
153 }
154