summaryrefslogtreecommitdiff
path: root/Postman/Postman-Controller/PostmanDashboardWidgetController.php
blob: 623331507993aa6c5867332cdcf1252d18bccb65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
if (! class_exists ( "PostmanDashboardWidgetController" )) {
	
	//
	class PostmanDashboardWidgetController {
		private $rootPluginFilenameAndPath;
		private $options;
		private $authorizationToken;
		private $wpMailBinder;
		
		/**
		 * Start up
		 */
		public function __construct($rootPluginFilenameAndPath, PostmanOptions $options, PostmanOAuthToken $authorizationToken, PostmanWpMailBinder $binder) {
			assert ( ! empty ( $rootPluginFilenameAndPath ) );
			assert ( ! empty ( $options ) );
			assert ( ! empty ( $authorizationToken ) );
			assert ( ! empty ( $binder ) );
			$this->rootPluginFilenameAndPath = $rootPluginFilenameAndPath;
			$this->options = $options;
			$this->authorizationToken = $authorizationToken;
			$this->wpMailBinder = $binder;
			
			add_action ( 'wp_dashboard_setup', array (
					$this,
					'addDashboardWidget' 
			) );
			
			add_action ( 'wp_network_dashboard_setup', array (
					$this,
					'addNetworkDashboardWidget' 
			) );
			
			// dashboard glance mod
			if ($this->options->isMailLoggingEnabled ()) {
				add_filter ( 'dashboard_glance_items', array (
						$this,
						'customizeAtAGlanceDashboardWidget' 
				), 10, 1 );
			}
			
			// Postman API: register the human-readable plugin state
			add_filter ( 'print_postman_status', array (
					$this,
					'print_postman_status' 
			) );
		}
		
		/**
		 * Add a widget to the dashboard.
		 *
		 * This function is hooked into the 'wp_dashboard_setup' action below.
		 */
		public function addDashboardWidget() {
			// only display to the widget to administrator
			if (PostmanUtils::isAdmin ()) {
				wp_add_dashboard_widget ( 'example_dashboard_widget', __ ( 'Postman SMTP', 'post-smtp' ), array (
						$this,
						'printDashboardWidget' 
				) ); // Display function.
			}
		}
		
		/**
		 * Add a widget to the network dashboard
		 */
		public function addNetworkDashboardWidget() {
			// only display to the widget to administrator
			if (PostmanUtils::isAdmin ()) {
				wp_add_dashboard_widget ( 'example_dashboard_widget', __ ( 'Postman SMTP', 'post-smtp' ), array (
						$this,
						'printNetworkDashboardWidget' 
				) ); // Display function.
			}
		}
		
		/**
		 * Create the function to output the contents of our Dashboard Widget.
		 */
		public function printDashboardWidget() {
			$goToSettings = sprintf ( '<a href="%s">%s</a>', PostmanUtils::getSettingsPageUrl (), __ ( 'Settings', 'post-smtp' ) );
			$goToEmailLog = sprintf ( '%s', _x ( 'Email Log', 'The log of Emails that have been delivered', 'post-smtp' ) );
			if ($this->options->isMailLoggingEnabled ()) {
				$goToEmailLog = sprintf ( '<a href="%s">%s</a>', PostmanUtils::getEmailLogPageUrl (), $goToEmailLog );
			}
			apply_filters ( 'print_postman_status', null );
			printf ( '<p>%s | %s</p>', $goToEmailLog, $goToSettings );
		}
		
		/**
		 * Print the human-readable plugin state
		 */
		public function print_postman_status() {
			if (! PostmanPreRequisitesCheck::isReady ()) {
				printf ( '<p><span style="color:red">%s</span></p>', __ ( 'Error: Postman is missing a required PHP library.', 'post-smtp' ) );
			} else if ($this->wpMailBinder->isUnboundDueToException ()) {
				printf ( '<p><span style="color:red">%s</span></p>', __ ( 'Postman: wp_mail has been declared by another plugin or theme, so you won\'t be able to use Postman until the conflict is resolved.', 'post-smtp' ) );
			} else {
				if ($this->options->getRunMode () != PostmanOptions::RUN_MODE_PRODUCTION) {
					printf ( '<p><span style="background-color:yellow">%s</span></p>', __ ( 'Postman is in <em>non-Production</em> mode and is dumping all emails.', 'post-smtp' ) );
				} else if (PostmanTransportRegistry::getInstance ()->getSelectedTransport ()->isConfiguredAndReady ()) {
					printf ( '<p class="wp-menu-image dashicons-before dashicons-email"> %s </p>', sprintf ( _n ( '<span style="color:green">Postman is configured</span> and has delivered <span style="color:green">%d</span> email.', '<span style="color:green">Postman is configured</span> and has delivered <span style="color:green">%d</span> emails.', PostmanState::getInstance ()->getSuccessfulDeliveries (), 'post-smtp' ), PostmanState::getInstance ()->getSuccessfulDeliveries () ) );
				} else {
					printf ( '<p><span style="color:red">%s</span></p>', __ ( 'Postman is <em>not</em> configured and is mimicking out-of-the-box WordPress email delivery.', 'post-smtp' ) );
				}
				$currentTransport = PostmanTransportRegistry::getInstance ()->getActiveTransport ();
				$deliveryDetails = $currentTransport->getDeliveryDetails ( $this->options );
				printf ( '<p>%s</p>', $deliveryDetails );
			}
		}
		
		/**
		 * Create the function to output the contents of our Dashboard Widget.
		 */
		public function printNetworkDashboardWidget() {
			printf ( '<p class="wp-menu-image dashicons-before dashicons-email"> %s</p>', __ ( 'Postman is operating in per-site mode.', 'post-smtp' ) );
		}
		
		/**
		 * From http://www.hughlashbrooke.com/2014/02/wordpress-add-items-glance-widget/
		 * http://coffeecupweb.com/how-to-add-custom-post-types-to-at-a-glance-dashboard-widget-in-wordpress/
		 *
		 * @param mixed $items        	
		 * @return string
		 */
		function customizeAtAGlanceDashboardWidget($items = array()) {
			// only modify the At-a-Glance for administrators
			if (PostmanUtils::isAdmin ()) {
				$post_types = array (
						PostmanEmailLogPostType::POSTMAN_CUSTOM_POST_TYPE_SLUG 
				);
				
				foreach ( $post_types as $type ) {
					
					if (! post_type_exists ( $type ))
						continue;
					
					$num_posts = wp_count_posts ( $type );
					
					if ($num_posts) {
						
						$published = intval ( $num_posts->publish );
						$privated = intval ( $num_posts->private );
						$post_type = get_post_type_object ( $type );
						
						$text = _n ( '%s ' . $post_type->labels->singular_name, '%s ' . $post_type->labels->name, $privated, 'post-smtp' );
						$text = sprintf ( $text, number_format_i18n ( $privated ) );
						
						$items [] = sprintf ( '<a class="%1$s-count" href="%3$s">%2$s</a>', $type, $text, PostmanUtils::getEmailLogPageUrl () ) . "\n";
					}
				}
				
				return $items;
			}
		}
	}
}