Sensei_Notices   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0
Metric Value
wmc 7
lcom 1
cbo 0
dl 0
loc 78
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add_notice() 0 10 2
A maybe_print_notices() 0 18 3
A clear_notices() 0 4 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 14 and the first side effect is on line 2.

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.

Loading history...
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