| 1 |
<?php |
| 2 |
/** |
| 3 |
* DashboardWidget |
| 4 |
* |
| 5 |
* @package NotificationX\Admin |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace NotificationX\Admin; |
| 9 |
|
| 10 |
use NotificationX\GetInstance; |
| 11 |
use NotificationX\Core\Helper; |
| 12 |
/** |
| 13 |
* Class for Dashboard Widget for Analytics. |
| 14 |
* @method static DashboardWidget get_instance($args = null) |
| 15 |
*/ |
| 16 |
class DashboardWidget { |
| 17 |
/** |
| 18 |
* Instance of DashboardWidget |
| 19 |
* |
| 20 |
* @var DashboardWidget |
| 21 |
*/ |
| 22 |
use GetInstance; |
| 23 |
/** |
| 24 |
* Widget ID |
| 25 |
* |
| 26 |
* @constant string |
| 27 |
*/ |
| 28 |
const WIDGET_ID = 'nx_analytics_dashboard_widget'; |
| 29 |
const ASSET_URL = NOTIFICATIONX_ASSETS . 'admin/'; |
| 30 |
const VIEWS_PATH = NOTIFICATIONX_INCLUDES . 'Admin/views/'; |
| 31 |
/** |
| 32 |
* Widget Title |
| 33 |
* |
| 34 |
* @var string |
| 35 |
*/ |
| 36 |
protected $widget_name = null; |
| 37 |
|
| 38 |
/** |
| 39 |
* Constructor |
| 40 |
* Invoked automatically when object created |
| 41 |
*/ |
| 42 |
public function __construct(){ |
| 43 |
if( Settings::get_instance()->get('settings.disable_dashboard_widget', false) ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
if( ! Settings::get_instance()->get('settings.enable_analytics', true) ) { |
| 47 |
return; |
| 48 |
} |
| 49 |
$this->widget_name = __( 'NotificationX Analytics', 'notificationx' ); |
| 50 |
add_action( 'wp_dashboard_setup', array( $this, 'widget_action' ) ); |
| 51 |
add_action('admin_enqueue_scripts', [ $this, 'enqueue'] ); |
| 52 |
} |
| 53 |
public function enqueue( $hook ){ |
| 54 |
wp_register_style( 'nx-analytics-dashboard-widget', self::ASSET_URL . 'css/analytics-dashboard-widget.css', array(), NOTIFICATIONX_VERSION, 'all' ); |
| 55 |
} |
| 56 |
/** |
| 57 |
* Admin Action callback |
| 58 |
* for wp_dashboard_setup |
| 59 |
* |
| 60 |
* @return void |
| 61 |
*/ |
| 62 |
public function widget_action(){ |
| 63 |
if( ! current_user_can( 'read_notificationx_analytics' ) ) { |
| 64 |
return; |
| 65 |
} |
| 66 |
wp_add_dashboard_widget( self::WIDGET_ID, $this->widget_name, array( $this, 'widget_output' ) ); |
| 67 |
} |
| 68 |
/** |
| 69 |
* Get all analytics data |
| 70 |
* |
| 71 |
* @return mixed |
| 72 |
*/ |
| 73 |
public function analytics_counter(){ |
| 74 |
global $wpdb; |
| 75 |
|
| 76 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- False positive: the query is prepared via $this->wpdb->prepare(), which this sniff does not recognise, and only $wpdb->prefix table names are interpolated. Audited 2026-07-16. |
| 77 |
$results = $wpdb->get_row( |
| 78 |
"SELECT *, ( clicks/views ) * 100 as ctr FROM ( SELECT SUM(views) as views, SUM(clicks) as clicks FROM {$wpdb->prefix}nx_stats ) AS STATS", |
| 79 |
ARRAY_A |
| 80 |
); |
| 81 |
|
| 82 |
$views_link = admin_url( 'admin.php?page=nx-analytics&comparison=views' ); |
| 83 |
$clicks_link = admin_url( 'admin.php?page=nx-analytics&comparison=clicks' ); |
| 84 |
$ctr_link = admin_url( 'admin.php?page=nx-analytics&comparison=ctr' ); |
| 85 |
|
| 86 |
$default = [ |
| 87 |
'views_link' => $views_link, |
| 88 |
'clicks_link' => $clicks_link, |
| 89 |
'ctr_link' => $ctr_link, |
| 90 |
'views' => 0, |
| 91 |
'clicks' => 0, |
| 92 |
'ctr' => 0, |
| 93 |
]; |
| 94 |
|
| 95 |
if( ! empty( $results ) ) { |
| 96 |
$_results = []; |
| 97 |
foreach( $results as $key => $value ) { |
| 98 |
if( $key === 'ctr' ) { |
| 99 |
if( $value !== null ) { |
| 100 |
$_results[ $key ] = round( $value, 2 ); |
| 101 |
} |
| 102 |
} else { |
| 103 |
$_results[ $key ] = Helper::nice_number( $value ); |
| 104 |
} |
| 105 |
} |
| 106 |
return array_merge( $default, $_results ); |
| 107 |
} |
| 108 |
return $default; |
| 109 |
} |
| 110 |
/** |
| 111 |
* Widget Output |
| 112 |
* |
| 113 |
* @return mixed |
| 114 |
*/ |
| 115 |
public function widget_output(){ |
| 116 |
extract( $this->analytics_counter() ); |
| 117 |
$class = 'nx-analytics-widget'; |
| 118 |
if( file_exists( self::VIEWS_PATH . 'analytics.views.php' ) ) { |
| 119 |
wp_enqueue_style('nx-analytics-dashboard-widget'); |
| 120 |
return include_once self::VIEWS_PATH . 'analytics.views.php'; |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|