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