AJAX
2 years ago
Admin_Page
2 years ago
Date_Range
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_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
94 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP_SCOPED\IAWP; |
| 4 | |
| 5 | use IAWP_SCOPED\IAWP\Utils\Number_Formatter; |
| 6 | use IAWP_SCOPED\IAWP\Utils\Security; |
| 7 | /** @internal */ |
| 8 | class View_Counter |
| 9 | { |
| 10 | public function __construct() |
| 11 | { |
| 12 | \add_action('the_content', [$this, 'output_counter']); |
| 13 | \add_action('init', [$this, 'add_shortcode']); |
| 14 | } |
| 15 | public function output_counter($content) |
| 16 | { |
| 17 | if (!$this->passes_checks()) { |
| 18 | return $content; |
| 19 | } |
| 20 | $position = \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_position', 'after'); |
| 21 | $counter = $this->get_counter_html(); |
| 22 | if ($position == 'before' || $position == 'both') { |
| 23 | $content = $counter . $content; |
| 24 | } |
| 25 | if ($position == 'after' || $position == 'both') { |
| 26 | $content .= $counter; |
| 27 | } |
| 28 | return $content; |
| 29 | } |
| 30 | public function get_counter_html($label = null, $icon = null) |
| 31 | { |
| 32 | $current_resource = Resource_Identifier::for_resource_being_viewed(); |
| 33 | if (\is_null($current_resource)) { |
| 34 | return; |
| 35 | } |
| 36 | $view_count = Number_Formatter::decimal($this->get_view_count($current_resource)); |
| 37 | if (\is_null($label)) { |
| 38 | $default = \function_exists('IAWP_SCOPED\\pll__') ? pll__('Views:', 'independent-analytics') : \__('Views:', 'independent-analytics'); |
| 39 | $label = \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_label', $default); |
| 40 | } |
| 41 | if (\is_null($icon)) { |
| 42 | $icon = \get_option('iawp_view_counter_icon', \true); |
| 43 | } |
| 44 | if ($icon) { |
| 45 | $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>'; |
| 46 | $label = $svg . ' ' . $label; |
| 47 | } |
| 48 | 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 style="margin-left: 3px;">' . \esc_html($view_count) . '</span></div>'; |
| 49 | } |
| 50 | public function add_shortcode() |
| 51 | { |
| 52 | \add_shortcode('iawp_view_counter', [$this, 'shortcode']); |
| 53 | } |
| 54 | public function shortcode($atts) |
| 55 | { |
| 56 | $a = \shortcode_atts(['label' => \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_label', \esc_html__('Views:', 'independent-analytics')), 'icon' => \true], $atts); |
| 57 | return $this->get_counter_html($a['label'], $a['icon']); |
| 58 | } |
| 59 | private function passes_checks() : bool |
| 60 | { |
| 61 | if (!\is_singular() || !\is_main_query()) { |
| 62 | return \false; |
| 63 | } |
| 64 | if (\IAWP_SCOPED\iawp()->get_option('iawp_view_counter_enable', \false) == \false) { |
| 65 | return \false; |
| 66 | } |
| 67 | if (!\in_array(\get_post_type(), \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_post_types', []))) { |
| 68 | return \false; |
| 69 | } |
| 70 | $exclude = \IAWP_SCOPED\iawp()->get_option('iawp_view_counter_exclude', ''); |
| 71 | if ($exclude != '') { |
| 72 | $exclude = \explode(',', $exclude); |
| 73 | if (\in_array(\get_the_ID(), $exclude)) { |
| 74 | return \false; |
| 75 | } |
| 76 | } |
| 77 | return \true; |
| 78 | } |
| 79 | private function get_view_count(Resource_Identifier $resource) : int |
| 80 | { |
| 81 | global $wpdb; |
| 82 | $resources_table = Query::get_table_name(Query::RESOURCES); |
| 83 | $views_table = Query::get_table_name(Query::VIEWS); |
| 84 | if ($resource->has_meta()) { |
| 85 | $meta_key = $resource->meta_key(); |
| 86 | $query = $wpdb->prepare("\n SELECT COUNT(views.id) AS views\n FROM {$resources_table} AS resources\n LEFT JOIN {$views_table} AS views ON resources.id = views.resource_id\n WHERE resource = %s\n AND {$meta_key} = %s\n GROUP BY resources.id;\n ", $resource->type(), $resource->meta_value()); |
| 87 | } else { |
| 88 | $query = $wpdb->prepare("\n SELECT COUNT(views.id) AS views\n FROM {$resources_table} AS resources\n LEFT JOIN {$views_table} AS views ON resources.id = views.resource_id\n WHERE resource = %s\n GROUP BY resources.id;\n ", $resource->type()); |
| 89 | } |
| 90 | $views = $wpdb->get_var($query); |
| 91 | return isset($views) ? $wpdb->get_var($query) : 0; |
| 92 | } |
| 93 | } |
| 94 |