PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/events-manager/classes/em-notices.php

https://gitlab.com/pankajmohale/chef2go
PHP | 251 lines | 212 code | 16 blank | 23 comment | 35 complexity | a73be590570e8cce2106df9ebc8e9394 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @author marcus
  5. *
  6. */
  7. class EM_Notices implements Iterator {
  8. /**
  9. * If object has been displayed, this gets set to true, can be checked to avoid duplicates.
  10. * @var boolean
  11. * @since 5.5.7
  12. */
  13. public $displayed = false;
  14. public $set_cookies = true;
  15. public $notices = array('errors'=>array(), 'infos'=>array(), 'alerts'=>array(), 'confirms'=>array());
  16. function __construct( $set_cookies = true ){
  17. //Grab from cookie, if it exists
  18. $this->set_cookies = $set_cookies == true;
  19. if( $this->set_cookies ){
  20. if( !empty($_COOKIE['em_notices']) ) {
  21. $notices = json_decode(base64_decode($_COOKIE['em_notices']), true);
  22. if( is_array($notices) ){
  23. $this->notices = $notices;
  24. setcookie('em_notices', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true); //unset the cookie
  25. }
  26. }
  27. }
  28. add_filter('wp_redirect', array(&$this,'destruct'), 1,1);
  29. }
  30. function destruct($redirect = false){
  31. //Flush notices that weren't made to stay cross-requests, we can do this if initialized immediately.
  32. foreach($this->notices as $notice_type => $notices){
  33. foreach ($notices as $key => $notice){
  34. if( empty($notice['static']) ){
  35. unset($this->notices[$notice_type][$key]);
  36. }else{
  37. unset($this->notices[$notice_type][$key]['static']); //so it gets removed next request
  38. $has_static = true;
  39. }
  40. }
  41. }
  42. if( $this->set_cookies ){
  43. if(count($this->notices['errors']) > 0 || count($this->notices['alerts']) > 0 || count($this->notices['infos']) > 0 || count($this->notices['confirms']) > 0){
  44. setcookie('em_notices', base64_encode(json_encode($this->notices)), time() + 30, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true); //sets cookie for 30 seconds, which may be too much
  45. }
  46. }
  47. return $redirect;
  48. }
  49. function __toString(){
  50. $string = false;
  51. if(count($this->notices['errors']) > 0){
  52. $string .= "<div class='em-warning em-warning-errors error'>{$this->get_errors()}</div>";
  53. }
  54. if(count($this->notices['alerts']) > 0){
  55. $string .= "<div class='em-warning em-warning-alerts updated'>{$this->get_alerts()}</div>";
  56. }
  57. if(count($this->notices['infos']) > 0){
  58. $string .= "<div class='em-warning em-warning-infos updated'>{$this->get_infos()}</div>";
  59. }
  60. if(count($this->notices['confirms']) > 0){
  61. $string .= "<div class='em-warning em-warning-confirms updated'>{$this->get_confirms()}</div>";
  62. }
  63. $this->displayed = true;
  64. return ($string !== false) ? "<div class='statusnotice'>".$string."</div>" : '';
  65. }
  66. /* General */
  67. function add($string, $type, $static = false){
  68. if( is_array($string) ){
  69. $result = true;
  70. foreach($string as $key => $string_item){
  71. if( !is_array($string_item) ){
  72. if( $this->add($string_item, $type, $static) === false ){ $result = false; }
  73. }else{
  74. if( $this->add_item($string_item, $type, $static) === false ){ $result = false; }
  75. }
  76. }
  77. return $result;
  78. }
  79. if($string != ''){
  80. return $this->add_item($string, $type, $static);
  81. }else{
  82. return false;
  83. }
  84. }
  85. function add_item($string, $type, $static = false){
  86. if( isset($this->notices[$type]) ){
  87. $notice_key = 0;
  88. foreach( $this->notices[$type] as $notice_key => $notice ){
  89. if($string == $notice['string']){
  90. return $notice_key;
  91. }elseif( is_array($string) && !empty($notice['title']) && $this->get_array_title($string) == $notice['title'] ){
  92. return $notice_key;
  93. }
  94. }
  95. $i = $notice_key+1;
  96. if( is_array($string) ){
  97. $this->notices[$type][$i]['title'] = $this->get_array_title($string);
  98. $this->notices[$type][$i]['string'] = array_shift($string);
  99. }else{
  100. $this->notices[$type][$i]['string'] = $string;
  101. }
  102. if( $static ){
  103. $this->notices[$type][$i]['static'] = true;
  104. }
  105. return $i;
  106. }else{
  107. return false;
  108. }
  109. }
  110. /**
  111. * Returns title of an array, assumes a assoc array with one item containing title => messages
  112. * @param unknown_type $array
  113. * @return unknown
  114. */
  115. function get_array_title($array){
  116. foreach($array as $title => $msgs)
  117. return $title;
  118. }
  119. function remove($key, $type){
  120. if( isset($this->notices[$type]) ){
  121. unset($this->notices[$type][$key]);
  122. return true;
  123. }else{
  124. return false;
  125. }
  126. }
  127. function remove_all(){
  128. $this->notices = array('errors'=>array(), 'infos'=>array(), 'alerts'=>array(), 'confirms'=>array());
  129. }
  130. function get($type){
  131. if( isset($this->notices[$type]) ){
  132. $string = '';
  133. foreach ($this->notices[$type] as $message){
  134. if( !is_array($message['string']) ){
  135. $string .= "<p>{$message['string']}</p>";
  136. }else{
  137. $string .= "<p><strong>".$message['title']."</strong><ul>";
  138. foreach($message['string'] as $msg){
  139. if( trim($msg) != '' ){
  140. $string .= "<li>$msg</li>";
  141. }
  142. }
  143. $string .= "</ul></p>";
  144. }
  145. }
  146. return $string;
  147. }
  148. return false;
  149. }
  150. function count($type){
  151. if( isset($this->notices[$type]) ){
  152. return count($this->notices[$type]);
  153. }
  154. return 0;
  155. }
  156. /* Errors */
  157. function add_error($string, $static=false){
  158. return $this->add($string, 'errors', $static);
  159. }
  160. function remove_error($key){
  161. return $this->remove($key, 'errors');
  162. }
  163. function get_errors(){
  164. return $this->get('errors');
  165. }
  166. function count_errors(){
  167. return $this->count('errors');
  168. }
  169. /* Alerts */
  170. function add_alert($string, $static=false){
  171. return $this->add($string, 'alerts', $static);
  172. }
  173. function remove_alert($key){
  174. return $this->remove($key, 'alerts');
  175. }
  176. function get_alerts(){
  177. return $this->get('alerts');
  178. }
  179. function count_alerts(){
  180. return $this->count('alerts');
  181. }
  182. /* Info */
  183. function add_info($string, $static=false){
  184. return $this->add($string, 'infos', $static);
  185. }
  186. function remove_info($key){
  187. return $this->remove($key, 'infos');
  188. }
  189. function get_infos(){
  190. return $this->get('infos');
  191. }
  192. function count_infos(){
  193. return $this->count('infos');
  194. }
  195. /* Confirms */
  196. function add_confirm($string, $static=false){
  197. return $this->add($string, 'confirms', $static);
  198. }
  199. function remove_confirm($key){
  200. return $this->remove($key, 'confirms');
  201. }
  202. function get_confirms(){
  203. return $this->get('confirms');
  204. }
  205. function count_confirms(){
  206. return $this->count('confirms');
  207. }
  208. //Iterator Implementation
  209. function rewind(){
  210. reset($this->bookings);
  211. }
  212. function current(){
  213. $var = current($this->bookings);
  214. return $var;
  215. }
  216. function key(){
  217. $var = key($this->bookings);
  218. return $var;
  219. }
  220. function next(){
  221. $var = next($this->bookings);
  222. return $var;
  223. }
  224. function valid(){
  225. $key = key($this->bookings);
  226. $var = ($key !== NULL && $key !== FALSE);
  227. return $var;
  228. }
  229. }
  230. function em_notices_init(){
  231. global $EM_Notices;
  232. $EM_Notices = new EM_Notices();
  233. }
  234. add_action('plugins_loaded', 'em_notices_init');
  235. ?>