PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
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 / Admin / Dashboard.php
matomo / classes / WpMatomo / Admin Last commit date
Marketplace 1 month ago PluginSuggestions 1 week ago TrackingSettings 1 week ago views 1 week ago AccessSettings.php 1 week ago AdBlockDetector.php 8 months ago Admin.php 1 month ago AdminSettings.php 1 week ago AdminSettingsInterface.php 6 years ago AdvancedSettings.php 1 week ago Chart.php 2 months ago CookieConsent.php 4 years ago Dashboard.php 1 week ago ExclusionSettings.php 1 week ago GeolocationSettings.php 1 week ago GetStarted.php 1 week ago ImportWpStatistics.php 1 week ago Info.php 1 week ago InvalidIpException.php 4 years ago Marketplace.php 1 week ago MarketplaceSetupWizard.php 1 week ago MarketplaceSetupWizardBody.php 1 week ago MatomoPage.php 3 months ago MatomoPageContent.php 3 months ago Menu.php 1 week ago PluginMeasurableSettings.php 1 week ago PrivacySettings.php 1 week ago SafeModeMenu.php 1 week ago Summary.php 1 week ago SystemReport.php 1 week ago TrackingSettings.php 1 week ago WhatsNewNotifications.php 3 months ago
Dashboard.php
148 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 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
58 'unique_id' => $widget['unique_id'],
59 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
60 'report_date' => $widget['date'],
61 'limit' => 10,
62 ]
63 );
64 }
65 );
66 }
67 } catch ( Exception $e ) {
68 // dont want to break dashboard if there is any issue with matomo ... eg in case bootstrap fails
69 // or is reinstalled but matomo not yet fully installed etc
70 $logger = new Logger();
71 $logger->log( sprintf( 'Failed to add Matomo widget %s to dashboard: %s', wp_json_encode( $widget ), $e->getMessage() ) );
72 }
73 }
74 }
75 }
76
77 public function get_widgets() {
78 $meta = get_user_meta( get_current_user_id(), self::DASHBOARD_USER_OPTION, true );
79 if ( empty( $meta ) ) {
80 $meta = [];
81 }
82
83 return $meta;
84 }
85
86 public function is_valid_widget( $unique_id, $date ) {
87 if ( empty( $unique_id ) || empty( $date ) ) {
88 return false;
89 }
90
91 $metadata = new Metadata();
92 $report = $metadata->find_report_by_unique_id( $unique_id );
93
94 if ( empty( $report ) ) {
95 return false;
96 }
97
98 $report_dates_obj = new Dates();
99 $report_dates = $report_dates_obj->get_supported_dates();
100
101 if ( empty( $report_dates[ $date ] ) ) {
102 return false;
103 }
104
105 return [
106 'report' => $report,
107 'date' => $report_dates[ $date ],
108 ];
109 }
110
111 public function has_widget( $report_unique_id, $report_date ) {
112 $widgets = $this->get_widgets();
113 foreach ( $widgets as $index => $widget ) {
114 if ( $widget['unique_id'] === $report_unique_id && $widget['date'] === $report_date ) {
115 return true;
116 }
117 }
118
119 return false;
120 }
121
122 public function toggle_widget( $report_unique_id, $report_date ) {
123 $widgets = $this->get_widgets();
124 foreach ( $widgets as $index => $widget ) {
125 if ( $widget['unique_id'] === $report_unique_id && $widget['date'] === $report_date ) {
126 unset( $widgets[ $index ] );
127 $this->set_widgets( array_values( $widgets ) );
128
129 return;
130 }
131 }
132 $widgets[] = [
133 'unique_id' => $report_unique_id,
134 'date' => $report_date,
135 ];
136
137 $this->set_widgets( $widgets );
138 }
139
140 private function set_widgets( $widgets ) {
141 update_user_meta( get_current_user_id(), self::DASHBOARD_USER_OPTION, $widgets );
142 }
143
144 public function uninstall() {
145 Uninstaller::uninstall_user_meta( self::DASHBOARD_USER_OPTION );
146 }
147 }
148