PluginProbe
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar / 3.2.11
NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar v3.2.11
3.3.1 3.3.0 3.2.14 3.2.13 3.2.12 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 trunk 0.2.5.5 0.2.5.6 0.2.5.7 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.2.0 1.2.1 All 156 releases
notificationx / includes / Admin / DashboardWidget.php

DashboardWidget.php in NotificationX – FOMO, Live Sales Notification, WooCommerce Sales Popup, GDPR, Social Proof, Announcement Banner & Floating Notification Bar 3.2.11, at includes/Admin/DashboardWidget.php

123 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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(), false, '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 $results = $wpdb->get_row(
77 "SELECT *, ( clicks/views ) * 100 as ctr FROM ( SELECT SUM(views) as views, SUM(clicks) as clicks FROM {$wpdb->prefix}nx_stats ) AS STATS",
78 ARRAY_A
79 );
80
81 $views_link = admin_url( 'admin.php?page=nx-analytics&comparison=views' );
82 $clicks_link = admin_url( 'admin.php?page=nx-analytics&comparison=clicks' );
83 $ctr_link = admin_url( 'admin.php?page=nx-analytics&comparison=ctr' );
84
85 $default = [
86 'views_link' => $views_link,
87 'clicks_link' => $clicks_link,
88 'ctr_link' => $ctr_link,
89 'views' => 0,
90 'clicks' => 0,
91 'ctr' => 0,
92 ];
93
94 if( ! empty( $results ) ) {
95 $_results = [];
96 foreach( $results as $key => $value ) {
97 if( $key === 'ctr' ) {
98 if( $value !== null ) {
99 $_results[ $key ] = round( $value, 2 );
100 }
101 } else {
102 $_results[ $key ] = Helper::nice_number( $value );
103 }
104 }
105 return array_merge( $default, $_results );
106 }
107 return $default;
108 }
109 /**
110 * Widget Output
111 *
112 * @return mixed
113 */
114 public function widget_output(){
115 extract( $this->analytics_counter() );
116 $class = 'nx-analytics-widget';
117 if( file_exists( self::VIEWS_PATH . 'analytics.views.php' ) ) {
118 wp_enqueue_style('nx-analytics-dashboard-widget');
119 return include_once self::VIEWS_PATH . 'analytics.views.php';
120 }
121 }
122 }
123