class-yoast-notification.php
2.26 KB
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
<?php
/**
* @package WPSEO\Admin
* @since 1.5.3
*/
/**
* Implements individual notification.
*/
class Yoast_Notification {
/**
* Contains optional arguments:
*
* - type: The notification type, i.e. 'updated' or 'error'
* - id: The ID of the notification
* - nonce: Security nonce to use in case of dismissible notice.
*
* @var array
*/
private $options;
/**
* Contains default values for the optional arguments
*
* @var array
*/
private $defaults = array(
'type' => 'updated',
'id' => '',
'nonce' => null,
'data_json' => array(),
);
/**
* The Constructor
*
* @param string $message
* @param array $options
*/
public function __construct( $message, $options = array() ) {
$this->options = wp_parse_args( $options, $this->defaults );
$this->message = $message;
}
/**
* Return the object properties as an array
*
* @return array
*/
public function to_array() {
return array(
'message' => $this->message,
'options' => $this->options,
);
}
/**
* Adds string (view) behaviour to the Notification
*
* @return string
*/
public function __toString() {
return '<div class="yoast-notice notice is-dismissible ' . esc_attr( $this->options['type'] ) . '" id="' . esc_attr( $this->options['id'] ) . '"' . $this->parse_data_attributes() . '>' . wpautop( $this->message ) . '</div>' . PHP_EOL;
}
/**
* Parsing the data attributes
*
* @return string
*/
private function parse_data_attributes() {
return $this->parse_nonce_attribute() . '' . $this->parse_data_json_attribute();
}
/**
* Returns a data attribute containing the nonce if present
*
* @return string
*/
private function parse_nonce_attribute() {
return ( ! empty( $this->options['nonce'] ) ? ' data-nonce="' . $this->options['nonce'] . '"' : '' );
}
/**
* Make it possible to pass some JSON data
*
* @return string
*/
private function parse_data_json_attribute() {
if ( empty( $this->options['data_json'] ) ) {
return '';
}
// @codingStandardsIgnoreStart
$data = ( function_exists( 'wp_json_encode' ) ) ? wp_json_encode( $this->options['data_json'] ) : json_encode( $this->options['data_json'] );
// @codingStandardsIgnoreEnd
return " data-json='" . $data . "'";
}
}