views
6 years ago
Data.php
6 years ago
Dates.php
6 years ago
Metadata.php
6 years ago
Renderer.php
6 years ago
Renderer.php
115 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', array( $this, 'show_report' ) ); |
| 23 | } |
| 24 | |
| 25 | public function show_visits_over_time($limit) |
| 26 | { |
| 27 | $cannot_view = $this->check_cannot_view(); |
| 28 | if ($cannot_view) { |
| 29 | return $cannot_view; |
| 30 | } |
| 31 | |
| 32 | if (is_numeric($limit)) { |
| 33 | $limit = (int) $limit; |
| 34 | } else { |
| 35 | $limit = 14; |
| 36 | } |
| 37 | |
| 38 | $report_meta = array('module' => 'VisitsSummary', 'action' => 'get'); |
| 39 | |
| 40 | $data = new Data(); |
| 41 | $report = $data->fetch_report($report_meta, 'day', 'last' . $limit, 'label', $limit); |
| 42 | $first_metric_name = 'nb_visits'; |
| 43 | |
| 44 | ob_start(); |
| 45 | |
| 46 | include 'views/table_map_no_dimension.php'; |
| 47 | |
| 48 | return ob_get_clean(); |
| 49 | } |
| 50 | |
| 51 | private function check_cannot_view() |
| 52 | { |
| 53 | if ( ! current_user_can( Capabilities::KEY_VIEW ) ) { |
| 54 | // not needed as processRequest checks permission anyway but it's faster this way and double ensures to not |
| 55 | // letting users view it when they have no access. |
| 56 | return esc_html__( 'Sorry, you are not allowed to view this report.', 'matomo' ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public function show_report( $atts ) { |
| 61 | $a = shortcode_atts( |
| 62 | array( |
| 63 | 'unique_id' => '', |
| 64 | 'report_date' => Dates::YESTERDAY, |
| 65 | 'limit' => 10, |
| 66 | ), |
| 67 | $atts |
| 68 | ); |
| 69 | |
| 70 | $cannot_view = $this->check_cannot_view(); |
| 71 | if ($cannot_view) { |
| 72 | return $cannot_view; |
| 73 | } |
| 74 | |
| 75 | if ($a['unique_id'] === 'visits_over_time') { |
| 76 | $is_default_limit = $a['limit'] === 10; |
| 77 | if ($is_default_limit) { |
| 78 | $a['limit'] = 14; |
| 79 | } |
| 80 | return $this->show_visits_over_time($a['limit']); |
| 81 | } |
| 82 | |
| 83 | $metadata = new Metadata(); |
| 84 | $report_meta = $metadata->find_report_by_unique_id( $a['unique_id'] ); |
| 85 | |
| 86 | if ( empty( $report_meta ) ) { |
| 87 | return sprintf( esc_html__( 'Report %s not found', 'matomo' ), esc_html( $a['unique_id'] ) ); |
| 88 | } |
| 89 | |
| 90 | $metric_keys = array_keys( $report_meta['metrics'] ); |
| 91 | $first_metric_name = reset( $metric_keys ); |
| 92 | $first_metric_display_name = reset( $report_meta['metrics'] ); |
| 93 | |
| 94 | $dates = new Dates(); |
| 95 | list( $period, $date ) = $dates->detect_period_and_date( $a['report_date'] ); |
| 96 | |
| 97 | $report_data = new Data(); |
| 98 | $report = $report_data->fetch_report( $report_meta, $period, $date, $first_metric_name, $a['limit'] ); |
| 99 | $has_report_data = ! empty( $report['reportData'] ) && $report['reportData']->getRowsCount(); |
| 100 | |
| 101 | ob_start(); |
| 102 | |
| 103 | if ( ! $has_report_data ) { |
| 104 | include 'views/table_no_data.php'; |
| 105 | } elseif ( empty( $report_meta['dimension'] ) ) { |
| 106 | include 'views/table_no_dimension.php'; |
| 107 | } else { |
| 108 | include 'views/table.php'; |
| 109 | } |
| 110 | |
| 111 | return ob_get_clean(); |
| 112 | } |
| 113 | |
| 114 | } |
| 115 |