Skip to content

Commit

Permalink
refactor dashboard widget into class of its own. add filter to modify…
Browse files Browse the repository at this point in the history
… number of top items , defaults to 5. set to 0 to disable.
  • Loading branch information
dannyvankooten committed Nov 1, 2023
1 parent 69f4014 commit b9962f9
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 52 deletions.
4 changes: 2 additions & 2 deletions assets/src/css/dashboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions koko-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
47 changes: 0 additions & 47 deletions src/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -177,27 +150,7 @@ public function footer_text(): string
return sprintf(wp_kses(__('If you enjoy using Koko Analytics, please <a href="%1$s">review the plugin on WordPress.org</a> or <a href="%2$s">write about it on your blog</a> 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
Expand Down
54 changes: 54 additions & 0 deletions src/class-dashboard-widget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace KokoAnalytics;

class Dashboard_Widget
{
public function init(): void
{
add_action('wp_dashboard_setup', array( $this, 'register_dashboard_widget' ), 10, 0);
}

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');
add_action('admin_print_scripts-index.php', array( $this, 'enqueue_scripts' ), 10, 0);
}

public function enqueue_scripts(): void
{
$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');
}

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';
}
}
6 changes: 4 additions & 2 deletions src/views/dashboard-widget.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php defined('ABSPATH') or exit;

/**
* @var int $number_of_top_items
* @var int $realtime
* @var array $posts
* @var array $referrers
Expand Down Expand Up @@ -38,8 +39,9 @@
display: inline-block;
}
</style>
<div id="ka-dashboard-widget-top-pages">

<?php if ($number_of_top_items > 0) { ?>
<div id="ka-dashboard-widget-top-pages">
<div style="display: flex; flex-flow: row wrap; margin-top: 2em;">
<div style="width: 50%; box-sizing: border-box; padding-right: 2em;">
<h3>
Expand Down Expand Up @@ -67,7 +69,7 @@
</div>
</div>
</div>

<?php } ?>

<p style="margin-top: 2em;">
<a href="<?php echo esc_attr(admin_url('index.php?page=koko-analytics')); ?>">
Expand Down
1 change: 0 additions & 1 deletion src/views/settings-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
</select>
<p class="description">
<?php esc_html_e('Visits and pageviews from users with any of the selected roles will be ignored.', 'koko-analytics'); ?>
<?php echo ' '; ?>
<?php esc_html_e('Use CTRL to select multiple options.', 'koko-analytics'); ?>
</p>
</div>
Expand Down

0 comments on commit b9962f9

Please sign in to comment.