Passed
Push — master ( d47355...85ec42 )
by Paul
04:15
created

Notice::addWarning()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Modules\Html\Builder;
7
use WP_Error;
8
9
class Notice
10
{
11
	/**
12
	 * @param string $type
13
	 * @param string|array|WP_Error $message
14
	 * @return void
15
	 */
16
	public function add( $type, $message, array $args = [] )
17
	{
18
		if( empty( array_filter( [$message, $type] )))return;
19
		$args['message'] = $message;
20
		$args['type'] = $type;
21
		add_settings_error( Application::ID, '', json_encode( $this->normalize( $args )));
22
	}
23
24
	/**
25
	 * @param string|array|WP_Error $message
26
	 * @return void
27
	 */
28
	public function addError( $message, array $args = [] )
29
	{
30
		$this->add( 'error', $message, $args );
31
	}
32
33
	/**
34
	 * @param string|array|WP_Error $message
35
	 * @return void
36
	 */
37
	public function addSuccess( $message, array $args = [] )
38
	{
39
		$this->add( 'success', $message, $args );
40
	}
41
42
	/**
43
	 * @param string|array|WP_Error $message
44
	 * @return void
45
	 */
46
	public function addWarning( $message, array $args = [] )
47
	{
48
		$this->add( 'warning', $message, $args );
49
	}
50
51
	/**
52
	 * @return string
53
	 */
54
	public function get()
55
	{
56
		$notices = array_map( 'unserialize',
57
			array_unique( array_map( 'serialize', get_settings_errors( Application::ID )))
58
		);
59
		if( empty( $notices ))return;
60
		return array_reduce( $notices, function( $carry, $notice ) {
61
			return $carry.$this->buildNotice( json_decode( $notice['message'], true ));
62
		});
63
	}
64
65
	/**
66
	 * @return string
67
	 */
68
	protected function buildNotice( array $args )
69
	{
70
		$messages = array_reduce( $args['messages'], function( $carry, $message ) {
71
			return $carry.glsr( Builder::class )->p( $message );
72
		});
73
		$class = 'notice notice-'.$args['type'];
74
		if( $args['inline'] ) {
75
			$class .= ' inline';
76
		}
77
		if( $args['dismissible'] ) {
78
			$class .= ' is-dismissible';
79
		}
80
		return glsr( Builder::class )->div( $messages, [
81
			'class' => $class,
82
		]);
83
	}
84
85
	/**
86
	 * @return array
87
	 */
88
	protected function normalize( array $args )
89
	{
90
		$defaults = [
91
			'dismissible' => true,
92
			'inline' => true,
93
			'message' => '',
94
			'type' => '',
95
		];
96
		$args = shortcode_atts( $defaults, $args );
97
		if( !in_array( $args['type'], ['error', 'warning', 'success'] )) {
98
			$args['type'] = 'success';
99
		}
100
		$args['messages'] = is_wp_error( $args['message'] )
101
			? (array)$args['message']->get_error_message()
102
			: (array)$args['message'];
103
		unset( $args['message'] );
104
		return $args;
105
	}
106
}
107