diff --git a/assets/src/css/dashboard.css b/assets/src/css/dashboard.css index 8b0164a0..0a7751ce 100644 --- a/assets/src/css/dashboard.css +++ b/assets/src/css/dashboard.css @@ -69,10 +69,10 @@ h1.ka-logo, margin: 12px 0; } .ka-margin-m { - margin: 24px 0; + margin: 2em 0; } .ka-margin-l { - margin: 48px 0; + margin: 4em 0; } .ka-right { float: right; diff --git a/koko-analytics.php b/koko-analytics.php index f34517bc..edb45822 100755 --- a/koko-analytics.php +++ b/koko-analytics.php @@ -49,6 +49,9 @@ } elseif (is_admin()) { $admin = new Admin(); $admin->init(); + + $dashboard_widget = new Dashboard_Widget(); + $dashboard_widget->init(); } else { $loader = new Script_Loader(); $loader->init(); diff --git a/src/class-admin.php b/src/class-admin.php index d9254d37..78568722 100644 --- a/src/class-admin.php +++ b/src/class-admin.php @@ -16,8 +16,6 @@ public function init(): void add_action('init', array( $this, 'maybe_run_actions' ), 10, 0); add_action('admin_menu', array( $this, 'register_menu' ), 10, 0); - add_action('admin_enqueue_scripts', array( $this, 'enqueue_scripts' ), 10, 1); - add_action('wp_dashboard_setup', array( $this, 'register_dashboard_widget' ), 10, 0); add_action('koko_analytics_install_optimized_endpoint', array( $this, 'install_optimized_endpoint' ), 10, 0); add_action('koko_analytics_save_settings', array( $this, 'save_settings' ), 10, 0); add_action('koko_analytics_reset_statistics', array( $this, 'reset_statistics' ), 10, 0); @@ -54,31 +52,6 @@ public function maybe_run_actions(): void exit; } - public function enqueue_scripts($page): void - { - // do not load any scripts if user is missing required capability for viewing - if (! current_user_can('view_koko_analytics')) { - return; - } - - switch ($page) { - case 'index.php': - $script_data = array( - 'root' => rest_url(), - 'nonce' => wp_create_nonce('wp_rest'), - 'i18n' => array( - 'Visitors' => __('Visitors', 'koko-analytics'), - 'Pageviews' => __('Pageviews', 'koko-analytics'), - ) - ); - // load scripts for dashboard widget - wp_enqueue_style('koko-analytics-dashboard', plugins_url('assets/dist/css/dashboard.css', KOKO_ANALYTICS_PLUGIN_FILE)); - wp_enqueue_script('koko-analytics-dashboard-widget', plugins_url('/assets/dist/js/dashboard-widget.js', KOKO_ANALYTICS_PLUGIN_FILE), array(), KOKO_ANALYTICS_VERSION, true); - wp_add_inline_script('koko-analytics-dashboard-widget', 'var koko_analytics = ' . json_encode($script_data), 'before'); - break; - } - } - private function get_available_roles(): array { $roles = array(); @@ -177,27 +150,7 @@ public function footer_text(): string return sprintf(wp_kses(__('If you enjoy using Koko Analytics, please review the plugin on WordPress.org or write about it on your blog to help out.', 'koko-analytics'), array( 'a' => array( 'href' => array() ) )), 'https://wordpress.org/support/view/plugin-reviews/koko-analytics?rate=5#postform', admin_url('post-new.php')); } - public function register_dashboard_widget(): void - { - // only show if user can view stats - if (! current_user_can('view_koko_analytics')) { - return; - } - add_meta_box('koko-analytics-dashboard-widget', 'Koko Analytics', array( $this, 'dashboard_widget' ), 'dashboard', 'side', 'high'); - } - - public function dashboard_widget(): void - { - $stats = new Stats(); - $dateStart = create_local_datetime('today, midnight')->format('Y-m-d'); - $dateEnd = create_local_datetime('tomorrow, midnight')->format('Y-m-d'); - $realtime = get_realtime_pageview_count('-1 hour'); - $totals = $stats->get_totals($dateStart, $dateEnd); - $posts = $stats->get_posts($dateStart, $dateEnd, 0, 5); - $referrers = $stats->get_referrers($dateStart, $dateEnd, 0, 5); - require __DIR__ . '/views/dashboard-widget.php'; - } /** * Add the settings link to the Plugins overview diff --git a/src/class-dashboard-widget.php b/src/class-dashboard-widget.php new file mode 100644 index 00000000..f4d6f5dd --- /dev/null +++ b/src/class-dashboard-widget.php @@ -0,0 +1,54 @@ + rest_url(), + 'nonce' => wp_create_nonce('wp_rest'), + 'i18n' => array( + 'Visitors' => __('Visitors', 'koko-analytics'), + 'Pageviews' => __('Pageviews', 'koko-analytics'), + ) + ); + // load scripts for dashboard widget + wp_enqueue_style('koko-analytics-dashboard', plugins_url('assets/dist/css/dashboard.css', KOKO_ANALYTICS_PLUGIN_FILE)); + wp_enqueue_script('koko-analytics-dashboard-widget', plugins_url('/assets/dist/js/dashboard-widget.js', KOKO_ANALYTICS_PLUGIN_FILE), array(), KOKO_ANALYTICS_VERSION, true); + wp_add_inline_script('koko-analytics-dashboard-widget', 'var koko_analytics = ' . json_encode($script_data), 'before'); + } + + public function dashboard_widget(): void + { + $number_of_top_items = (int) apply_filters('koko_analytics_dashboard_widget_number_of_top_items', 5); + $stats = new Stats(); + $dateStart = create_local_datetime('today, midnight')->format('Y-m-d'); + $dateEnd = create_local_datetime('tomorrow, midnight')->format('Y-m-d'); + $realtime = get_realtime_pageview_count('-1 hour'); + $totals = $stats->get_totals($dateStart, $dateEnd); + + if ($number_of_top_items > 0) { + $posts = $stats->get_posts($dateStart, $dateEnd, 0, $number_of_top_items); + $referrers = $stats->get_referrers($dateStart, $dateEnd, 0, $number_of_top_items); + } + require __DIR__ . '/views/dashboard-widget.php'; + } +} diff --git a/src/views/dashboard-widget.php b/src/views/dashboard-widget.php index fced74bd..a0a28e36 100644 --- a/src/views/dashboard-widget.php +++ b/src/views/dashboard-widget.php @@ -1,6 +1,7 @@ -
+ 0) { ?> +

@@ -67,7 +69,7 @@

- +

diff --git a/src/views/settings-page.php b/src/views/settings-page.php index d296cf58..1008c7bd 100644 --- a/src/views/settings-page.php +++ b/src/views/settings-page.php @@ -43,7 +43,6 @@

-