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 |