PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.3.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.3.3
5.13.0 5.12.1 5.12.0 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 2 years ago Dates.php 1 year ago Metadata.php 1 year ago Renderer.php 1 year ago
Renderer.php
137 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 Piwik\DataTable;
13 use Piwik\DataTable\DataTableInterface;
14 use WpMatomo\Capabilities;
15
16 if ( ! defined( 'ABSPATH' ) ) {
17 exit; // if accessed directly
18 }
19
20 class Renderer {
21 const CUSTOM_UNIQUE_ID_VISITS_OVER_TIME = 'visits_over_time';
22
23 public function register_hooks() {
24 add_shortcode( 'matomo_report', [ $this, 'show_report' ] );
25 }
26
27 public function show_visits_over_time( $limit, $period ) {
28 $cannot_view = $this->check_cannot_view();
29 if ( $cannot_view ) {
30 return $cannot_view;
31 }
32
33 if ( is_numeric( $limit ) ) {
34 $limit = (int) $limit;
35 } else {
36 $limit = 14;
37 }
38
39 $report_meta = [
40 'module' => 'VisitsSummary',
41 'action' => 'get',
42 ];
43
44 $data = new Data();
45 $report = $data->fetch_report( $report_meta, $period, 'last' . $limit, 'label', $limit );
46 $first_metric_name = 'nb_visits';
47 $matomo_graph_data = ' data-chart="VisitsSumary"';
48 ob_start();
49
50 include 'views/table_map_no_dimension.php';
51
52 return ob_get_clean();
53 }
54
55 private function check_cannot_view() {
56 if ( ! current_user_can( Capabilities::KEY_VIEW ) ) {
57 // not needed as processRequest checks permission anyway but it's faster this way and double ensures to not
58 // letting users view it when they have no access.
59 return esc_html__( 'Sorry, you are not allowed to view this report.', 'matomo' );
60 }
61 }
62
63 public function show_report( $atts ) {
64 $a = shortcode_atts(
65 [
66 'unique_id' => '',
67 'report_date' => Dates::YESTERDAY,
68 'limit' => 10,
69 ],
70 $atts
71 );
72
73 $cannot_view = $this->check_cannot_view();
74 if ( $cannot_view ) {
75 return $cannot_view;
76 }
77
78 $dates = new Dates();
79 list( $period, $date ) = $dates->detect_period_and_date( $a['report_date'] );
80
81 if ( 'visits_over_time' === $a['unique_id'] ) {
82 $is_default_limit = 10 === $a['limit'];
83 if ( $is_default_limit ) {
84 $a['limit'] = 14;
85 }
86
87 return $this->show_visits_over_time( $a['limit'], $period );
88 }
89
90 $metadata = new Metadata();
91 $report_meta = $metadata->find_report_by_unique_id( $a['unique_id'] );
92
93 if ( empty( $report_meta ) ) {
94 return sprintf( esc_html__( 'Report %s not found', 'matomo' ), esc_html( $a['unique_id'] ) );
95 }
96
97 $metric_keys = array_keys( $report_meta['metrics'] );
98 $first_metric_name = reset( $metric_keys );
99 $first_metric_display_name = reset( $report_meta['metrics'] );
100
101 $report_data = new Data();
102 $report = $report_data->fetch_report( $report_meta, $period, $date, $first_metric_name, $a['limit'] );
103 $has_report_data = ! empty( $report['reportData'] ) && $this->has_rows_with_data( $report['reportData'], $first_metric_name );
104
105 ob_start();
106
107 if ( ! $has_report_data ) {
108 include 'views/table_no_data.php';
109 } elseif ( empty( $report_meta['dimension'] ) ) {
110 include 'views/table_no_dimension.php';
111 } else {
112 include 'views/table.php';
113 }
114
115 return ob_get_clean();
116 }
117
118 private function has_rows_with_data( DataTableInterface $table, $first_metric_name ) {
119 $has_data = false;
120 $table->filter(
121 function ( DataTable $table ) use ( &$has_data, $first_metric_name ) {
122 if ( $has_data ) {
123 return;
124 }
125
126 foreach ( $table->getRows() as $row ) {
127 if ( ! empty( $row[ $first_metric_name ] ) ) {
128 $has_data = true;
129 break;
130 }
131 }
132 }
133 );
134 return $has_data;
135 }
136 }
137