/
llms.functions.notice.php
155 lines (126 loc) · 3.2 KB
/
llms.functions.notice.php
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* Notice Functions
*
* Functions for managing front end notices (alert messages).
*
* @package LifterLMS/Functions
*
* @since unknown
* @version 3.14.7
*/
defined( 'ABSPATH' ) || exit;
/**
* Stores notice in llms_notices session
*
* @param string $message [The notice message]
* @param string $notice_type [notice type]
* @return void
* @since 1.0.0
* @version 1.0.0
*/
function llms_add_notice( $message, $notice_type = 'success' ) {
$notices = llms()->session->get( 'llms_notices', array() );
if ( 'success' === $notice_type ) {
$message = apply_filters( 'lifterlms_add_message', $message );
}
$notices[ $notice_type ][] = apply_filters( 'lifterlms_add_' . $notice_type, $message );
llms()->session->set( 'llms_notices', $notices );
}
/**
* Clears all notices from session
*
* @return void
* @since 1.0.0
* @version 3.12.0
*/
function llms_clear_notices() {
llms()->session->set( 'llms_notices', array() );
}
/**
* Retrieve an array of notice types
*
* @return array
* @since 1.0.0
* @version 1.0.0
*/
function llms_get_notice_types() {
return apply_filters( 'lifterlms_notice_types', array( 'debug', 'error', 'notice', 'success' ) );
}
/**
* Gets messages and errors which are stored in the session, then clears them.
*
* @package LifterLMS/Functions
*
* @return string
* @since 3.0.0
* @version 3.12.0
*/
function llms_get_notices() {
$all_notices = apply_filters( 'lifterlms_print_notices', llms()->session->get( 'llms_notices', array() ) );
$notice_types = llms_get_notice_types();
ob_start();
foreach ( $notice_types as $notice_type ) {
if ( llms_notice_count( $notice_type ) > 0 ) {
llms_get_template(
"notices/{$notice_type}.php",
array(
'messages' => $all_notices[ $notice_type ],
)
);
}
}
add_action( 'shutdown', 'llms_clear_notices', 1 ); // Prior to shutdown functions executed by session manager.
return ob_get_clean();
}
/**
* Returns a count of all current notices by type.
*
* @param string $notice_type Type of notice passed. IE: error, success, warning
* @return int
* @since 1.0.0
* @version 1.0.0
*/
function llms_notice_count( $notice_type = '' ) {
$notice_count = 0;
$all_notices = llms()->session->get( 'llms_notices', array() );
if ( isset( $all_notices[ $notice_type ] ) ) {
$notice_count = absint( count( $all_notices[ $notice_type ] ) );
} elseif ( empty( $notice_type ) ) {
foreach ( $all_notices as $notices ) {
$notice_count += absint( count( $all_notices ) );
}
}
return $notice_count;
}
/**
* Prints a single notice
*
* @param string $message [The notice message]
* @param string $notice_type [notice type]
* @return void
* @since 1.0.0
* @version 1.0.0
*/
function llms_print_notice( $message, $notice_type = 'success' ) {
if ( 'success' === $notice_type ) {
$message = apply_filters( 'lifterlms_add_message', $message );
}
llms_get_template(
"notices/{$notice_type}.php",
array(
'messages' => array( apply_filters( 'lifterlms_add_' . $notice_type, $message ) ),
)
);
}
/**
* Prints all notices
*
* @return void
* @since 1.0.0
* @version 3.14.7
*/
function llms_print_notices() {
echo llms_get_notices();
llms_clear_notices();
}