user_chart_widget.php
The code provided is a WordPress plugin that adds a dashboard widget displaying user registrations over the past 7 days and over all time using Frappe Charts. The User Registration Charts Dashboard Widget plugin creates a class called UserRegistrationChartsWidget, which handles the functionality. The constructor of the class sets up hooks to enqueue the necessary scripts and add the dashboard widget. The display_widget method generates the necessary HTML and JavaScript code to render the bar chart for user registrations over the past 7 days and the line chart for user registrations over all time. The get_user_registrations method retrieves the user registration data from the WordPress database. Overall, this plugin provides a visual representation of user registrations for site administrators to track user growth.
<?php /* Plugin Name: User Registration Charts Dashboard Widget Description: This plugin adds a dashboard widget that displays user registrations over past 7 days with a bar chart and over all time with a line chart using Frappe Charts. Author: CodeWP Version: 1.0 */ class UserRegistrationChartsWidget { private $registrations; function __construct() { add_action('wp_dashboard_setup', array($this, 'add_dashboard_widgets')); add_action('admin_enqueue_scripts', array($this, 'enqueue_scripts')); $this->registrations = $this->get_user_registrations(); } function enqueue_scripts() { wp_enqueue_script('frappe-charts', 'https://unpkg.com/frappe-charts@latest'); } function add_dashboard_widgets() { wp_add_dashboard_widget('user_registration_charts_widget', 'User Registration Charts', array($this, 'display_widget')); } function display_widget() { $registrations_last_7_days = array_slice($this->registrations, -7); $registrations_all_time = $this->registrations; echo '<div id="chart1"></div>'; echo '<div id="chart2"></div>'; echo " <script> new frappe.Chart('#chart1', { title: 'User Registrations Last 7 Days', data: { labels: " . json_encode(array_keys($registrations_last_7_days)) . ", datasets: [ { name: 'User registrations', values: " . json_encode(array_values($registrations_last_7_days)) . " } ] }, type: 'bar', }); new frappe.Chart('#chart2', { title: 'User Registrations All Time', data: { labels: " . json_encode(array_keys($registrations_all_time)) . ", datasets: [ { name: 'User registrations', values: " . json_encode(array_values($registrations_all_time)) . " } ] }, type: 'line', }); </script> "; } function get_user_registrations() { global $wpdb; $results = $wpdb->get_results("SELECT DATE(user_registered) as date, COUNT(ID) as count FROM $wpdb->users GROUP BY DATE(user_registered)", ARRAY_A); $registrations = array(); foreach ($results as $result) { $registrations[$result['date']] = $result['count']; } return $registrations; } } new UserRegistrationChartsWidget();