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