Passed
Push — master ( 31e4e3...e9b806 )
by Chris
03:06
created

MonsterInsights_Dashboard_Widget   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 68
dl 0
loc 132
rs 10
c 1
b 0
f 0
wmc 19

5 Methods

Rating   Name   Duplication   Size   Complexity  
A widget_scripts() 0 7 5
A register_dashboard_widget() 0 15 1
B dashboard_widget_content() 0 45 6
A widget_content_no_auth() 0 8 2
A __construct() 0 16 5
1
<?php
2
/**
3
 * Manage the MonsterInsights Dashboard Widget
4
 *
5
 * @since 7.1
6
 *
7
 * @package MonsterInsights
8
 */
9
10
// Exit if accessed directly.
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
/**
16
 * Class MonsterInsights_Dashboard_Widget
17
 */
18
class MonsterInsights_Dashboard_Widget {
19
20
	const WIDGET_KEY = 'monsterinsights_reports_widget';
21
22
	/**
23
	 * The widget options.
24
	 *
25
	 * @var array $options
26
	 */
27
	public $options;
28
29
	/**
30
	 * MonsterInsights_Dashboard_Widget constructor.
31
	 */
32
	public function __construct() {
33
		// Allow dashboard widget to be hidden on multisite installs
34
		$show_widget         = is_multisite() ? apply_filters( 'monsterinsights_show_dashboard_widget', true ) : true;
35
		if ( ! $show_widget ) {
36
			return false;
37
		}
38
39
		// Check if reports should be visible.
40
		$dashboards_disabled = monsterinsights_get_option( 'dashboards_disabled', false );
41
		if ( ! current_user_can( 'monsterinsights_view_dashboard' ) || $dashboards_disabled ) {
42
			return false;
43
		}
44
45
		add_action( 'wp_dashboard_setup', array( $this, 'register_dashboard_widget' ) );
46
47
		add_action( 'admin_enqueue_scripts', array( $this, 'widget_scripts' ) );
48
	}
49
50
	/**
51
	 * Register the dashboard widget.
52
	 */
53
	public function register_dashboard_widget() {
54
		global $wp_meta_boxes;
55
56
		wp_add_dashboard_widget(
57
			self::WIDGET_KEY,
58
			esc_html__( 'MonsterInsights', 'google-analytics-for-wordpress' ),
59
			array( $this, 'dashboard_widget_content' )
60
		);
61
62
		// Attept to place the widget at the top.
63
		$normal_dashboard = $wp_meta_boxes['dashboard']['normal']['core'];
64
		$widget_instance  = array( self::WIDGET_KEY => $normal_dashboard[ self::WIDGET_KEY ] );
65
		unset( $normal_dashboard[ self::WIDGET_KEY ] );
66
		$sorted_dashboard                             = array_merge( $widget_instance, $normal_dashboard );
67
		$wp_meta_boxes['dashboard']['normal']['core'] = $sorted_dashboard;
68
	}
69
70
	/**
71
	 * Load the widget content.
72
	 */
73
	public function dashboard_widget_content() {
74
75
		$is_authed = ( MonsterInsights()->auth->is_authed() || MonsterInsights()->auth->is_network_authed() );
0 ignored issues
show
Bug Best Practice introduced by
The property $auth is declared protected in MonsterInsights_Lite. Since you implement __get, consider adding a @property or @property-read.
Loading history...
76
77
		if ( ! $is_authed ) {
78
			$this->widget_content_no_auth();
79
		} else {
80
			$datepicker_options = array( 30, 7 );
81
			$url                = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_reports' ) : admin_url( 'admin.php?page=monsterinsights_reports' );
82
			?>
83
			<div class="mi-dw-controls">
84
				<div class="mi-dw-datepicker mi-dw-btn-group" data-type="datepicker">
85
					<button class="mi-dw-btn-group-label">
86
						<?php
87
						// Translators: %d is the number of days.
88
						printf( esc_html__( 'Last %d days', 'google-analytics-for-wordpress' ), 30 );
89
						?>
90
					</button>
91
					<div class="mi-dw-btn-list">
92
						<?php foreach ( $datepicker_options as $datepicker_option ) { ?>
93
							<button class="mi-dw-btn <?php echo 30 === $datepicker_option ? 'selected' : ''; ?>" data-value=" <?php echo esc_attr( $datepicker_option ); ?>">
94
								<?php
95
								// Translators: %d is the number of days.
96
								printf( esc_html__( 'Last %d days', 'google-analytics-for-wordpress' ), esc_attr( $datepicker_option ) );
97
								?>
98
							</button>
99
						<?php } ?>
100
					</div>
101
				</div>
102
				<label class="mi-dw-styled-toggle mi-dw-widget-width-toggle-container" title="<?php esc_attr_e( 'Show in full-width mode', 'google-analytics-for-wordpress' ); ?>">
103
					<input type="checkbox" class="mi-dw-widget-width-toggle"/>
104
				</label>
105
				<div class="mi-dw-dropdown">
106
					<button class="mi-dw-button-cog mi-dw-dropdown-toggle" data-target="#mi-dw-reports-options" type="button"></button>
107
					<ul class="mi-dw-reports-options" id="mi-dw-reports-options"></ul>
108
				</div>
109
				<img class="mi-dw-mascot" src="<?php echo esc_url( plugins_url( 'assets/css/images/mascot.png', MONSTERINSIGHTS_PLUGIN_FILE ) ); ?>" srcset="<?php echo esc_url( plugins_url( 'assets/css/images/[email protected]', MONSTERINSIGHTS_PLUGIN_FILE ) ); ?> 2x"/>
110
			</div>
111
			<div class="mi-dw-lite">
112
				<div class="mi-dw-lite-content">
113
					<h2><?php esc_html_e( 'View All Analytics on the WordPress Dashboard', 'google-analytics-for-wordpress' ); ?></h2>
114
					<p><?php esc_html_e( 'Once you upgrade to MonsterInsights Pro, you can see your analytics on the Dashboard', 'google-analytics-for-wordpress' ); ?></p>
115
					<a href="<?php echo esc_url( monsterinsights_get_upgrade_link( 'dashboard-widget', 'lite-cta' ) ); ?>" target="_blank" class="mi-dw-btn-large"><?php esc_html_e( 'Get MonsterInsights Pro', 'google-analytics-for-wordpress' ); ?></a>
116
					<br/>
117
					<a href="<?php echo esc_url( $url ); ?>"><?php esc_html_e( 'Go to MonsterInsights Reports', 'google-analytics-for-wordpress' ); ?></a>
118
				</div>
119
			</div>
120
			<?php
121
		}
122
123
	}
124
125
	/**
126
	 * Message to display when the plugin is not authenticated.
127
	 */
128
	public function widget_content_no_auth() {
129
130
		$url = is_network_admin() ? network_admin_url( 'admin.php?page=monsterinsights_settings' ) : admin_url( 'admin.php?page=monsterinsights_settings' );
131
		?>
132
		<div class="mi-dw-not-authed">
133
			<h2><?php esc_html_e( 'Reports are not available', 'google-analytics-for-wordpress' ); ?></h2>
134
			<p><?php esc_html_e( 'Please connect MonsterInsights to Google Analytics to see reports.', 'google-analytics-for-wordpress' ); ?></p>
135
			<a href="<?php echo esc_url( $url ); ?>" class="mi-dw-btn-large"><?php esc_html_e( 'Configure MonsterInsights', 'google-analytics-for-wordpress' ); ?></a>
136
		</div>
137
		<?php
138
	}
139
140
	/**
141
	 * Load widget-specific scripts.
142
	 */
143
	public function widget_scripts() {
144
		$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
145
		$screen = get_current_screen();
146
		if ( isset( $screen->id ) && 'dashboard' === $screen->id ) {
147
			wp_enqueue_style( 'monsterinsights-dashboard-widget-styles', plugins_url( 'lite/assets/css/admin-dashboard-widget' . $suffix . '.css', MONSTERINSIGHTS_PLUGIN_FILE ), array(), monsterinsights_get_asset_version() );
148
			wp_enqueue_script( 'jquery-ui-tooltip' );
149
			wp_enqueue_script( 'monsterinsights-dashboard-widget', plugins_url( 'lite/assets/js/admin-dashboard-widget' . $suffix . '.js', MONSTERINSIGHTS_PLUGIN_FILE ), array( 'jquery' ), monsterinsights_get_asset_version(), true );
150
		}
151
	}
152
}
153