PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.14.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.14.2
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / Report / Renderer.php
matomo / classes / WpMatomo / Report Last commit date
views 4 years ago Data.php 4 years ago Dates.php 4 years ago Metadata.php 4 years ago Renderer.php 4 years ago
Renderer.php
116 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 * @package matomo
8 */
9
10 namespace WpMatomo\Report;
11
12 use WpMatomo\Capabilities;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // if accessed directly
16 }
17
18 class Renderer {
19 const CUSTOM_UNIQUE_ID_VISITS_OVER_TIME = 'visits_over_time';
20
21 public function register_hooks() {
22 add_shortcode( 'matomo_report', [ $this, 'show_report' ] );
23 }
24
25 public function show_visits_over_time( $limit, $period ) {
26 $cannot_view = $this->check_cannot_view();
27 if ( $cannot_view ) {
28 return $cannot_view;
29 }
30
31 if ( is_numeric( $limit ) ) {
32 $limit = (int) $limit;
33 } else {
34 $limit = 14;
35 }
36
37 $report_meta = [
38 'module' => 'VisitsSummary',
39 'action' => 'get',
40 ];
41
42 $data = new Data();
43 $report = $data->fetch_report( $report_meta, $period, 'last' . $limit, 'label', $limit );
44 $first_metric_name = 'nb_visits';
45 $matomo_graph_data = ' data-chart="VisitsSumary"';
46 ob_start();
47
48 include 'views/table_map_no_dimension.php';
49
50 return ob_get_clean();
51 }
52
53 private function check_cannot_view() {
54 if ( ! current_user_can( Capabilities::KEY_VIEW ) ) {
55 // not needed as processRequest checks permission anyway but it's faster this way and double ensures to not
56 // letting users view it when they have no access.
57 return esc_html__( 'Sorry, you are not allowed to view this report.', 'matomo' );
58 }
59 }
60
61 public function show_report( $atts ) {
62 $a = shortcode_atts(
63 [
64 'unique_id' => '',
65 'report_date' => Dates::YESTERDAY,
66 'limit' => 10,
67 ],
68 $atts
69 );
70
71 $cannot_view = $this->check_cannot_view();
72 if ( $cannot_view ) {
73 return $cannot_view;
74 }
75
76 $dates = new Dates();
77 list( $period, $date ) = $dates->detect_period_and_date( $a['report_date'] );
78
79 if ( 'visits_over_time' === $a['unique_id'] ) {
80 $is_default_limit = 10 === $a['limit'];
81 if ( $is_default_limit ) {
82 $a['limit'] = 14;
83 }
84
85 return $this->show_visits_over_time( $a['limit'], $period );
86 }
87
88 $metadata = new Metadata();
89 $report_meta = $metadata->find_report_by_unique_id( $a['unique_id'] );
90
91 if ( empty( $report_meta ) ) {
92 return sprintf( esc_html__( 'Report %s not found', 'matomo' ), esc_html( $a['unique_id'] ) );
93 }
94
95 $metric_keys = array_keys( $report_meta['metrics'] );
96 $first_metric_name = reset( $metric_keys );
97 $first_metric_display_name = reset( $report_meta['metrics'] );
98
99 $report_data = new Data();
100 $report = $report_data->fetch_report( $report_meta, $period, $date, $first_metric_name, $a['limit'] );
101 $has_report_data = ! empty( $report['reportData'] ) && $report['reportData']->getRowsCount();
102
103 ob_start();
104
105 if ( ! $has_report_data ) {
106 include 'views/table_no_data.php';
107 } elseif ( empty( $report_meta['dimension'] ) ) {
108 include 'views/table_no_dimension.php';
109 } else {
110 include 'views/table.php';
111 }
112
113 return ob_get_clean();
114 }
115 }
116