PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.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 / Admin / Dashboard.php
matomo / classes / WpMatomo / Admin Last commit date
PluginSuggestions 2 months ago TrackingSettings 2 months ago views 2 months ago AccessSettings.php 4 years ago AdBlockDetector.php 6 months ago Admin.php 2 months ago AdminSettings.php 2 months ago AdminSettingsInterface.php 6 years ago AdvancedSettings.php 10 months ago Chart.php 2 months ago CookieConsent.php 4 years ago Dashboard.php 2 months ago ExclusionSettings.php 6 months ago GeolocationSettings.php 2 years ago GetStarted.php 2 months ago ImportWpStatistics.php 2 months ago Info.php 2 months ago InvalidIpException.php 4 years ago Marketplace.php 2 months ago MarketplaceSetupWizard.php 2 months ago MarketplaceSetupWizardBody.php 3 months ago MatomoPage.php 2 months ago MatomoPageContent.php 2 months ago Menu.php 2 months ago PluginMeasurableSettings.php 2 years ago PrivacySettings.php 1 year ago SafeModeMenu.php 2 months ago Summary.php 2 months ago SystemReport.php 2 months ago TrackingSettings.php 4 months ago WhatsNewNotifications.php 2 months ago
Dashboard.php
147 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\Admin;
11
12 use Exception;
13 use WpMatomo\Capabilities;
14 use WpMatomo\Feature;
15 use WpMatomo\Logger;
16 use WpMatomo\Report\Dates;
17 use WpMatomo\Report\Metadata;
18 use WpMatomo\Report\Renderer;
19 use WpMatomo\Uninstaller;
20
21 if ( ! defined( 'ABSPATH' ) ) {
22 exit; // if accessed directly
23 }
24
25 class Dashboard extends Feature {
26 const DASHBOARD_USER_OPTION = 'matomo_dashboard_widgets';
27
28 public function is_active() {
29 return is_admin();
30 }
31
32 public function register_hooks() {
33 add_action( 'wp_dashboard_setup', [ $this, 'add_dashboard_widgets' ] );
34 }
35
36 public function add_dashboard_widgets() {
37 $widgets = $this->get_widgets();
38 if ( ! empty( $widgets ) && is_array( $widgets ) && current_user_can( Capabilities::KEY_VIEW ) ) {
39 do_action( 'matomo_load_chartjs' );
40 foreach ( $widgets as $widget ) {
41 try {
42 $widget_meta = $this->is_valid_widget( $widget['unique_id'], $widget['date'] );
43 if ( ! empty( $widget_meta['report']['name'] ) ) {
44 $id = 'matomo_dashboard_widget_' . $widget['unique_id'] . '_' . $widget['date'];
45
46 $title = $widget_meta['report']['name'] . ' - ' . $widget_meta['date'] . ' - Matomo';
47
48 wp_add_dashboard_widget(
49 $id,
50 esc_html( $title ),
51 function () use ( $widget ) {
52 $renderer = new Renderer();
53 // do not escape the content, we want the HTML
54 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
55 echo $renderer->show_report(
56 [
57 'unique_id' => $widget['unique_id'],
58 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
59 'report_date' => $widget['date'],
60 'limit' => 10,
61 ]
62 );
63 }
64 );
65 }
66 } catch ( Exception $e ) {
67 // dont want to break dashboard if there is any issue with matomo ... eg in case bootstrap fails
68 // or is reinstalled but matomo not yet fully installed etc
69 $logger = new Logger();
70 $logger->log( sprintf( 'Failed to add Matomo widget %s to dashboard: %s', wp_json_encode( $widget ), $e->getMessage() ) );
71 }
72 }
73 }
74 }
75
76 public function get_widgets() {
77 $meta = get_user_meta( get_current_user_id(), self::DASHBOARD_USER_OPTION, true );
78 if ( empty( $meta ) ) {
79 $meta = [];
80 }
81
82 return $meta;
83 }
84
85 public function is_valid_widget( $unique_id, $date ) {
86 if ( empty( $unique_id ) || empty( $date ) ) {
87 return false;
88 }
89
90 $metadata = new Metadata();
91 $report = $metadata->find_report_by_unique_id( $unique_id );
92
93 if ( empty( $report ) ) {
94 return false;
95 }
96
97 $report_dates_obj = new Dates();
98 $report_dates = $report_dates_obj->get_supported_dates();
99
100 if ( empty( $report_dates[ $date ] ) ) {
101 return false;
102 }
103
104 return [
105 'report' => $report,
106 'date' => $report_dates[ $date ],
107 ];
108 }
109
110 public function has_widget( $report_unique_id, $report_date ) {
111 $widgets = $this->get_widgets();
112 foreach ( $widgets as $index => $widget ) {
113 if ( $widget['unique_id'] === $report_unique_id && $widget['date'] === $report_date ) {
114 return true;
115 }
116 }
117
118 return false;
119 }
120
121 public function toggle_widget( $report_unique_id, $report_date ) {
122 $widgets = $this->get_widgets();
123 foreach ( $widgets as $index => $widget ) {
124 if ( $widget['unique_id'] === $report_unique_id && $widget['date'] === $report_date ) {
125 unset( $widgets[ $index ] );
126 $this->set_widgets( array_values( $widgets ) );
127
128 return;
129 }
130 }
131 $widgets[] = [
132 'unique_id' => $report_unique_id,
133 'date' => $report_date,
134 ];
135
136 $this->set_widgets( $widgets );
137 }
138
139 private function set_widgets( $widgets ) {
140 update_user_meta( get_current_user_id(), self::DASHBOARD_USER_OPTION, $widgets );
141 }
142
143 public function uninstall() {
144 Uninstaller::uninstall_user_meta( self::DASHBOARD_USER_OPTION );
145 }
146 }
147