1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Feedback class |
4
|
|
|
* |
5
|
|
|
* Depending on the location of the page (CMS or Website) show a notification to the user |
6
|
|
|
* |
7
|
|
|
* @package Stencil |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Feedback |
12
|
|
|
*/ |
13
|
|
|
class Stencil_Feedback { |
14
|
|
|
/** |
15
|
|
|
* Admin notice queue |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
private static $notices = array(); |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Display a notification |
23
|
|
|
* |
24
|
|
|
* @param string $type Type of message. |
25
|
|
|
* @param string $message Message to show. |
26
|
|
|
*/ |
27
|
|
|
public static function notification( $type, $message ) { |
28
|
|
|
if ( is_admin() ) { |
29
|
|
|
self::admin_notification( $type, $message ); |
30
|
|
|
} else { |
31
|
|
|
self::site_notification( $type, $message ); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Display a notification on the site |
37
|
|
|
* |
38
|
|
|
* Using trigger_error so developers see this message but visitors don't. |
39
|
|
|
* If the server has been configured properly ofcourse.. |
40
|
|
|
* |
41
|
|
|
* @param string $type Type of notification. |
42
|
|
|
* @param string $message Message to show. |
43
|
|
|
*/ |
44
|
|
|
private static function site_notification( $type, $message ) { |
45
|
|
|
switch ( $type ) { |
46
|
|
|
case 'error': |
47
|
|
|
$error_type = E_USER_ERROR; |
48
|
|
|
break; |
49
|
|
|
|
50
|
|
|
case 'warning': |
51
|
|
|
case 'notification': |
52
|
|
|
$error_type = E_USER_WARNING; |
53
|
|
|
break; |
54
|
|
|
|
55
|
|
|
default: |
56
|
|
|
$error_type = E_USER_NOTICE; |
57
|
|
|
break; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sanitize for logfile |
62
|
|
|
*/ |
63
|
|
|
$message = sanitize_text_field( $message ); |
64
|
|
|
|
65
|
|
|
trigger_error( $message, $error_type ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Display a notification on the CMS |
70
|
|
|
* |
71
|
|
|
* @param string $type Type of mesesage. |
72
|
|
|
* @param string $message Message to show. |
73
|
|
|
*/ |
74
|
|
|
private static function admin_notification( $type, $message ) { |
75
|
|
|
static $admin_notices_hooked = false; |
76
|
|
|
if ( ! $admin_notices_hooked ) { |
77
|
|
|
$admin_notices_hooked = true; |
78
|
|
|
|
79
|
|
|
add_action( 'admin_notices', array( __CLASS__, 'show_admin_notices' ) ); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
self::$notices[] = array( 'type' => $type, 'message' => $message ); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Display queued notices |
87
|
|
|
*/ |
88
|
|
|
public static function show_admin_notices() { |
89
|
|
|
while ( array() !== self::$notices ) { |
90
|
|
|
$notice = array_shift( self::$notices ); |
91
|
|
|
echo '<div class="' . esc_attr( $notice['type'] ) . '"><p>Stencil: ' . $notice['message'] . '</p></div>'; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|