Completed
Push — develop ( 451e26...07b04d )
by David
05:33
created

Wordlift_Notice_Service::admin_notices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4286
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * Displays notices in the admin UI.
5
 *
6
 * @since 3.2.0
7
 */
8
class Wordlift_Notice_Service {
9
10
	/**
11
	 * The template used to display notices. The <em>notice dismissible</em> style classes make this notice dismissible
12
	 * on the WordPress UI (via a small X button on the right side of the notice).
13
	 *
14
	 * @since 3.2.0
15
	 */
16
	const TEMPLATE = '<div class="wl-notice notice is-dismissible %s"><p>%s</p></div>';
17
18
	/**
19
	 * The standard WordPress <em>update</em> style class.
20
	 *
21
	 * @since 3.2.0
22
	 */
23
	const UPDATE = 'update';
24
25
	/**
26
	 * The standard WordPress <em>update-nag</em> style class.
27
	 *
28
	 * @since 3.2.0
29
	 */
30
	const UPDATE_NAG = 'update-nag';
31
32
	/**
33
	 * The standard WordPress <em>error</em> style class.
34
	 *
35
	 * @since 3.2.0
36
	 */
37
	const ERROR = 'error';
38
39
	/**
40
	 * The array of notices.
41
	 *
42
	 * @since 3.2.0
43
	 * @access private
44
	 * @var array $notices The array of notices.
45
	 */
46
	private $notices = array();
47
48
	/**
49
	 * A singleton instance of the Notice service.
50
	 *
51
	 * @since 3.2.0
52
	 * @access private
53
	 * @var \Wordlift_Notice_Service $instance A singleton instance of the Notice service.
54
	 */
55
	private static $instance;
56
57
	/**
58
	 * Create an instance of the Notice service.
59
	 *
60
	 * @since 3.2.0
61
	 */
62
	public function __construct() {
63
64
		// Hook to be called when to display notices.
65
		add_action( 'admin_notices', array( $this, 'admin_notices' ) );
66
67
		self::$instance = $this;
68
69
	}
70
71
	/**
72
	 * Get the singleton instance of the Notice service.
73
	 *
74
	 * @since 3.2.0
75
	 * @return \Wordlift_Notice_Service The singleton instance of the Notice service.
76
	 */
77
	public static function get_instance() {
78
79
		return self::$instance;
80
	}
81
82
	/**
83
	 * Add a notice.
84
	 *
85
	 * @since 3.2.0
86
	 *
87
	 * @param string $class The css class.
88
	 * @param string $message The message.
89
	 */
90
	public function add( $class, $message ) {
91
92
		$this->notices[] = sprintf( self::TEMPLATE, $class, $message );
93
94
	}
95
96
	/**
97
	 * Add an update notice (message with a white background and a green left border).
98
	 *
99
	 * @since 3.2.0
100
	 *
101
	 * @param string $message The message to display.
102
	 */
103
	public function add_update( $message ) {
104
105
		$this->add( self::UPDATE, $message );
106
107
	}
108
109
	/**
110
	 * Add an update nag notice (message with a white background and a yellow left border).
111
	 *
112
	 * @since 3.2.0
113
	 *
114
	 * @param string $message The message to display.
115
	 */
116
	public function add_update_nag( $message ) {
117
118
		$this->add( self::UPDATE_NAG, $message );
119
120
	}
121
122
	/**
123
	 * Add an error notice (message with a white background and a red left border).
124
	 *
125
	 * @since 3.2.0
126
	 *
127
	 * @param string $message The message to display.
128
	 */
129
	public function add_error( $message ) {
130
131
		$this->add( self::ERROR, $message );
132
133
	}
134
135
	/**
136
	 * Print out the notices when the admin_notices action is called.
137
	 *
138
	 * @since 3.2.0
139
	 */
140
	public function admin_notices() {
141
142
		foreach ( $this->notices as $notice ) {
143
			echo( $notice );
144
		}
145
146
	}
147
148
}
149