1
|
|
|
<?php |
|
|
|
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Sensei Notices Class |
6
|
|
|
* |
7
|
|
|
* All functionality pertaining to displaying of various notices on the frontend. |
8
|
|
|
* |
9
|
|
|
* @package Core |
10
|
|
|
* @author Automattic |
11
|
|
|
* |
12
|
|
|
* @since 1.6.3 |
13
|
|
|
*/ |
14
|
|
|
class Sensei_Notices{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var $notices |
18
|
|
|
*/ |
19
|
|
|
protected $notices; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var boolean $has_printed |
23
|
|
|
*/ |
24
|
|
|
protected $has_printed; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* constructor |
28
|
|
|
*/ |
29
|
|
|
public function __construct(){ |
30
|
|
|
//initialize the notices variable |
31
|
|
|
$this->notices = array(); |
32
|
|
|
$this->has_printed = false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Add a notice to the array of notices for display at a later stage. |
37
|
|
|
* |
38
|
|
|
* |
39
|
|
|
* @param string $content |
40
|
|
|
* @param string $type defaults to alert options( alert, tick , download , info ) |
41
|
|
|
* |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
public function add_notice( $content , $type = 'alert' ){ |
46
|
|
|
// append the new notice |
47
|
|
|
$this->notices[] = array('content' => $content , 'type'=> $type ); |
48
|
|
|
|
49
|
|
|
// if a notice is added after we've printed print it immediately. |
50
|
|
|
if( $this->has_printed ){ |
51
|
|
|
$this->maybe_print_notices(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
} // end add_notice() |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Output all notices added |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
|
62
|
|
|
public function maybe_print_notices(){ |
63
|
|
|
if( count( $this->notices ) > 0 ){ |
64
|
|
|
foreach ($this->notices as $notice) { |
65
|
|
|
|
66
|
|
|
$classes = 'sensei-message '. $notice['type']; |
67
|
|
|
$html = '<div class="'. $classes . '">'. $notice['content'] . '</div>'; |
68
|
|
|
|
69
|
|
|
echo $html; |
70
|
|
|
} |
71
|
|
|
// empty the notice queue to avoid reprinting the same notices |
72
|
|
|
$this->clear_notices(); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// set this to print immediately if notices are added after the notices were printed |
77
|
|
|
$this->has_printed = true; |
78
|
|
|
|
79
|
|
|
} // end print_notice() |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Clear all notices |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function clear_notices(){ |
87
|
|
|
// assign an empty array to clear all existing notices |
88
|
|
|
$this->notices = array(); |
89
|
|
|
} // end clear_notices() |
90
|
|
|
|
91
|
|
|
} // end Woothemes_Sensei_Notices |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Class Woothemes_Sensei_Notices |
95
|
|
|
* @ignore only for backward compatibility |
96
|
|
|
* @since 1.9.0 |
97
|
|
|
*/ |
98
|
|
|
class Woothemes_Sensei_Notices extends Sensei_Notices{} |
99
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.