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