Completed
Pull Request — dev/2.3.2 (#242)
by Maria Daniel Deepak
06:48 queued 03:59
created

DashboardWidget::load()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php namespace EmailLog\Core\UI\Component;
2
3
use EmailLog\Core\Loadie;
4
use EmailLog\Util;
5
6
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
7
8
/*
9
 * Widget that displays email log information in dashboard.
10
 *
11
 * @since 2.2.0
12
 */
13
class DashboardWidget implements Loadie {
14
15
	/**
16
	 * Setup hooks.
17
	 *
18
	 * @inheritdoc
19
	 */
20
	public function load() {
21
		add_action( 'wp_dashboard_setup', array( $this, 'register' ) );
22
	}
23
24
	/**
25
	 * Adds the dashboard widget to display Email Log activity.
26
	 */
27
	public function register() {
28
		wp_add_dashboard_widget(
29
			'email_log_dashboard_widget',
30
			__( 'Email Logs Summary', 'email-log' ),
31
			array( $this, 'render' )
32
		);
33
	}
34
35
	/**
36
	 * Outputs the contents on the Dashboard Widget.
37
	 *
38
	 * @since 2.4.0 Added div.el-addons-list
39
	 *
40
	 * TODO: Move the inline styles to the stylesheet.
41
	 * TODO: Make the content within el-summary-list-right element dynamic.
42
	 */
43
	public function render() {
44
		$email_log  = email_log();
45
		$logs_count = $email_log->table_manager->get_logs_count();
46
		?>
47
		<style>
48
			#email_log_dashboard_widget .inside {
49
				margin: 0;
50
				padding-bottom: 0;
51
			}
52
53
			.el-summary-list-left {
54
				min-width: 150px;
55
				margin-right: 5px;
56
				color: #72777c;
57
			}
58
59
			.el-addons-list ul {
60
				margin: 0 -12px;
61
			}
62
63
			.el-addons-list ul li {
64
				padding-left: 12px;
65
				padding-right: 12px;
66
			}
67
68
			.el-addons-list ul li:first-child {
69
				padding-top: 12px;
70
				box-shadow: inset 0 1px 0 rgba(0, 0, 0, 0.06);
71
			}
72
73
			#el-inactive-addons-list {
74
				color: #72777c;
75
			}
76
77
			#email_log_dashboard_widget .subsubsub {
78
				margin: 0 0;
79
				padding-bottom: 4px;
80
			}
81
		</style>
82
		<div id="el-summary" class="activity-block">
83
			<h3><?php _e( 'Summary', 'email-log' ); ?></h3>
84
				<ul>
85
					<li>
86
						<span class="el-summary-list-left"><?php _e( 'Total number of emails logged' ,	'email-log'	);
87
						?></span>
88
						<span class="el-summary-list-right"><?php echo number_format( absint(
89
							$logs_count ), 0, ',', ','
90
							); ?></span>
91
					</li>
92
					<li>
93
						<span class="el-summary-list-left"><?php esc_html_e( 'Next Auto delete logs cron run', 'email-log' ); ?></span>
94
						<span class="el-summary-list-right">August 2, 2019 2:01 pm</span>
95
					</li>
96
				</ul>
97
98
			<?php // Util\render_auto_delete_logs_next_run_schedule(); ?>
99
		</div>
100
		<div id="el-active-addons-list" class="el-addons-list activity-block">
101
			<h3>Installed & Active</h3>
102
			<ul>
103
				<?php
104
					foreach( $this->get_active_addons_list() as $addon ) :
105
					?>
106
						<li><?php printf( esc_html( $addon ) ); ?></li>
107
				<?php
108
					endforeach;
109
				?>
110
			</ul>
111
		</div>
112
		<div id="el-inactive-addons-list" class="el-addons-list activity-block">
113
			<h3>Installed & In-active</h3>
114
			<ul>
115
				<?php
116
				foreach( $this->get_inactive_addons_list() as $addon ) :
117
					?>
118
					<li><?php printf( esc_html( $addon ) ); ?></li>
119
				<?php
120
				endforeach;
121
				?>
122
			</ul>
123
		</div>
124
		<div class="activity-block">
125
			<ul class="subsubsub" style="float: none">
126
				<li><?php printf( __( '<a href="%s">Email Logs</a>', 'email-log' ), 'admin.php?page=email-log' ); ?> <span style="color: #ddd"> | </span></li>
127
				<li><?php printf( __( '<a href="%s">Settings</a>', 'email-log' ), 'admin.php?page=email-log-settings' ); ?> <span style="color: #ddd"> | </span></li>
128
				<li><?php printf( __( '<a href="%s">Addons</a>', 'email-log' ), 'admin.php?page=email-log-addons' ); ?></li>
129
			</ul>
130
		</div>
131
		<?php
132
	}
133
134
	/**
135
	 * Gets the list of active addons.
136
	 *
137
	 * @since 2.4.0
138
	 *
139
	 * TODO: The data from the licenser is cached as transients. Cross check before returning.
140
	 *
141
	 * @return array
142
	 */
143
	public function get_active_addons_list() {
144
		$inactive_adddons = $this->get_inactive_addons_list();
145
		$addons           = array();
146
		try {
147
			foreach ( email_log()->get_licenser()->get_addon_list()->get_addons() as $addon ) {
148
				if ( ! property_exists( get_class( $addon ), 'name' ) ) {
149
					continue;
150
				}
151
				$addons[] = $addon->name;
152
			}
153
		} catch ( \Exception $exception ) {
154
			// TODO: How should we handle errors?
155
		}
156
157
		return array_diff( $addons, $inactive_adddons );
158
	}
159
160
	/**
161
	 * Gets the list of inactive addons.
162
	 *
163
	 * @since 2.4.0
164
	 *
165
	 * TODO: The data from the licenser is cached as transients. Cross check before returning.
166
	 *
167
	 * @return array
168
	 */
169
	public function get_inactive_addons_list() {
170
		$inactive_addons = array();
171
		try {
172
			foreach ( email_log()->get_licenser()->get_addon_list()->get_inactive_addons() as
173
				$addon ) {
174
				if ( ! property_exists( get_class( $addon ), 'name' ) ) {
175
					continue;
176
				}
177
				$inactive_addons[] = $addon->name;
178
			}
179
		} catch ( \Exception $exception ) {
180
			// TODO: How should we handle errors?
181
		}
182
183
		return $inactive_addons;
184
	}
185
}
186