Notice   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 55
c 2
b 0
f 0
dl 0
loc 150
ccs 0
cts 100
cp 0
rs 10
wmc 20

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 8 3
A buildNotice() 0 7 2
A generate() 0 9 3
A __construct() 0 3 1
A buildMessage() 0 7 3
A __get() 0 6 2
A activateButton() 0 8 1
A title() 0 3 1
A addNotice() 0 8 1
A updateButton() 0 8 1
A button() 0 11 1
A installButton() 0 8 1
1
<?php
2
3
namespace GeminiLabs\Pollux;
4
5
use BadMethodCallException;
6
use Exception;
7
use GeminiLabs\Pollux\Application;
8
9
/**
10
 * @property array $all
11
 * @method void addError( mixed $messages, bool $dismissible = true )
12
 * @method void addInfo( mixed $messages, bool $dismissible = true )
13
 * @method void addSuccess( mixed $messages, bool $dismissible = true )
14
 * @method void addWarning( mixed $messages, bool $dismissible = true )
15
 */
16
class Notice
17
{
18
	/**
19
	 * @var Application
20
	 */
21
	protected $app;
22
23
	public function __construct( Application $app )
24
	{
25
		$this->app = $app;
26
	}
27
28
	public function __call( $name, $args )
29
	{
30
		$method = strtolower( $name );
31
		$status = substr( $method, 3 );
32
		if( 'add' == substr( $method, 0, 3 ) && in_array( $status, ['error', 'info', 'success', 'warning'] )) {
33
			return call_user_func_array( [$this, 'addNotice'], array_merge( [$status], $args ));
34
		}
35
		throw new BadMethodCallException( sprintf( 'Not a valid method: %s', $name ));
36
	}
37
38
	public function __get( $property )
39
	{
40
		if( $property == 'all' ) {
41
			return $this->app->notices;
42
		}
43
		throw new Exception( sprintf( 'Not a valid property: %s', $property ));
44
	}
45
46
	/**
47
	 * @return string
48
	 */
49
	public function activateButton( array $plugin )
50
	{
51
		$actionUrl = self_admin_url( sprintf( 'options-general.php?page=%s&action=activate&plugin=%s', $this->app->id, $plugin['plugin'] ));
52
		return $this->button( sprintf( '%s %s', __( 'Activate', 'pollux' ), $plugin['name'] ), [
53
			'data-name' => $plugin['name'],
54
			'data-plugin' => $plugin['plugin'],
55
			'data-slug' => $plugin['slug'],
56
			'href' => wp_nonce_url( $actionUrl, sprintf( 'activate-plugin_%s', $plugin['plugin'] )),
57
		]);
58
	}
59
60
	/**
61
	 * @param string $title
62
	 * @return string
63
	 */
64
	public function button( $title, array $atts = [] )
65
	{
66
		$atts = wp_parse_args( $atts, [
67
			'class' => '',
68
			'href' => '',
69
		]);
70
		$atts['class'] = trim( $atts['class'] . ' button button-small' );
71
		$attributes = array_reduce( array_keys( $atts ), function( $carry, $key ) use( $atts ) {
72
			return $carry . sprintf( ' %s="%s"', $key, $atts[$key] );
73
		});
74
		return sprintf( '<a%s>%s</a>', $attributes, $title );
75
	}
76
77
	/**
78
	 * @return string
79
	 */
80
	public function generate( array $notice, $unset = true )
81
	{
82
		if( $unset ) {
83
			$index = array_search( $notice, $this->app->notices );
84
			if( $index !== false ) {
85
				unset( $this->app->notices[$index] );
86
			}
87
		}
88
		return $this->buildNotice( $notice );
89
	}
90
91
	/**
92
	 * @return string
93
	 */
94
	public function installButton( array $plugin )
95
	{
96
		$actionUrl = self_admin_url( sprintf( 'update.php?action=install-plugin&plugin=%s', $plugin['slug'] ));
97
		return $this->button( sprintf( '%s %s', __( 'Install', 'pollux' ), $plugin['name'] ), [
98
			'data-name' => $plugin['name'],
99
			'data-plugin' => $plugin['plugin'],
100
			'data-slug' => $plugin['slug'],
101
			'href' => wp_nonce_url( $actionUrl, sprintf( 'install-plugin_%s', $plugin['slug'] )),
102
		]);
103
	}
104
105
	/**
106
	 * @return string
107
	 */
108
	public function updateButton( array $plugin )
109
	{
110
		$actionUrl = self_admin_url( sprintf( 'update.php?action=upgrade-plugin&plugin=%s', $plugin['plugin'] ));
111
		return $this->button( sprintf( '%s %s', __( 'Update', 'pollux' ), $plugin['name'] ), [
112
			'data-name' => $plugin['name'],
113
			'data-plugin' => $plugin['plugin'],
114
			'data-slug' => $plugin['slug'],
115
			'href' => wp_nonce_url( $actionUrl, sprintf( 'upgrade-plugin_%s', $plugin['plugin'] )),
116
		]);
117
	}
118
119
	/**
120
	 * @param string $string
121
	 * @return string
122
	 */
123
	public function title( $string )
124
	{
125
		return sprintf( '<strong>%s</strong>', $string );
126
	}
127
128
	/**
129
	 * @param string $type
130
	 * @param string|array $messages
131
	 * @param bool $dismissible
132
	 * @return void
133
	 */
134
	protected function addNotice( $type, $messages, $dismissible = true )
135
	{
136
		$this->app->notices[] = [
137
			'dismissible' => $dismissible,
138
			'message' => $this->buildMessage( array_filter( (array) $messages )),
139
			'type' => $type,
140
		];
141
		$this->app->notices = array_unique( $this->app->notices, SORT_REGULAR );
142
	}
143
144
	/**
145
	 * @return string
146
	 */
147
	protected function buildMessage( array $messages )
148
	{
149
		foreach( $messages as $key => &$message ) {
150
			if( !is_wp_error( $message ))continue;
151
			$message = $message->get_error_message();
152
		}
153
		return wpautop( implode( PHP_EOL . PHP_EOL, $messages ));
154
	}
155
156
	/**
157
	 * @return string
158
	 */
159
	protected function buildNotice( array $notice )
160
	{
161
		$class = sprintf( 'notice notice-%s', $notice['type'] );
162
		if( $notice['dismissible'] ) {
163
			$class .= ' is-dismissible';
164
		}
165
		return sprintf( '<div class="pollux-notice %s">%s</div>', $class, $notice['message'] );
166
	}
167
}
168