PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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