| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if( !defined( 'ABSPATH' ) ) exit; |
| 4 |
|
| 5 |
|
| 6 |
class wpForoNotices{ |
| 7 |
|
| 8 |
function __construct(){ |
| 9 |
$this->init(); |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
private function init(){ |
| 16 |
if(!$this->is_session_started()) session_start(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* @return bool |
| 21 |
*/ |
| 22 |
private function is_session_started(){ |
| 23 |
if ( php_sapi_name() !== 'cli' ) { |
| 24 |
if ( version_compare(phpversion(), '5.4.0', '>=') ) { |
| 25 |
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE; |
| 26 |
} else { |
| 27 |
return session_id() === '' ? FALSE : TRUE; |
| 28 |
} |
| 29 |
} |
| 30 |
return FALSE; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* |
| 35 |
* @param mixed $args |
| 36 |
* @param string $type (e.g. success|error) |
| 37 |
* |
| 38 |
* @return bool |
| 39 |
*/ |
| 40 |
public function add( $args, $type = '' ){ |
| 41 |
if(!$args) return FALSE; |
| 42 |
if(!is_array($args)) $args = array($args); |
| 43 |
|
| 44 |
if( $this->is_session_started() ){ |
| 45 |
$_SESSION['wpforo_notice_type'] = $type; |
| 46 |
if( !empty($_SESSION['wpforo_notices']) ){ |
| 47 |
foreach($args as $arg) $_SESSION['wpforo_notices'][] = $arg; |
| 48 |
}else{ |
| 49 |
$_SESSION['wpforo_notices'] = $args; |
| 50 |
} |
| 51 |
$_SESSION['wpforo_notices'] = array_unique($_SESSION['wpforo_notices']); |
| 52 |
return TRUE; |
| 53 |
} |
| 54 |
|
| 55 |
return FALSE; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* |
| 60 |
* @return bool |
| 61 |
* |
| 62 |
*/ |
| 63 |
public function clear(){ |
| 64 |
if( $this->is_session_started() ){ |
| 65 |
unset($_SESSION['wpforo_notice_type'], $_SESSION['wpforo_notices']); |
| 66 |
return TRUE; |
| 67 |
} |
| 68 |
|
| 69 |
return FALSE; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* get notices $notices['type'], $notices['text'] |
| 74 |
* |
| 75 |
* @return array |
| 76 |
*/ |
| 77 |
public function get_notices(){ |
| 78 |
if(!isset($_SESSION['wpforo_notices'])) return array( 'type' => '', 'text' => '' ); |
| 79 |
if($_SESSION['wpforo_notice_type'] == 'success'){ |
| 80 |
$class = 'success'; |
| 81 |
}elseif($_SESSION['wpforo_notice_type'] == 'error'){ |
| 82 |
$class = 'error'; |
| 83 |
}else{ |
| 84 |
$class = ''; |
| 85 |
} |
| 86 |
|
| 87 |
$notices = array(); |
| 88 |
$notices['type'] = $class; |
| 89 |
$notices['text'] = ''; |
| 90 |
foreach($_SESSION['wpforo_notices'] as $notice) if( !is_array($notice) ) $notices['text'] .= wpforo_phrase($notice, FALSE) . '<br/>'; |
| 91 |
|
| 92 |
$this->clear(); |
| 93 |
return $notices; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* |
| 98 |
* @param bool $frontend (default TRUE) |
| 99 |
* |
| 100 |
* @return void |
| 101 |
*/ |
| 102 |
public function show( $frontend = TRUE ){ |
| 103 |
if(!isset($_SESSION['wpforo_notices'])) return; |
| 104 |
if($_SESSION['wpforo_notice_type'] == 'success'){ |
| 105 |
$class = 'success'; |
| 106 |
}elseif($_SESSION['wpforo_notice_type'] == 'error'){ |
| 107 |
$class = 'error'; |
| 108 |
}else{ |
| 109 |
$class = ''; |
| 110 |
} |
| 111 |
$inner = ''; |
| 112 |
foreach($_SESSION['wpforo_notices'] as $notice) if( !is_array($notice) ) $inner .= wpforo_phrase($notice, FALSE) . '<br/>'; |
| 113 |
?> |
| 114 |
<?php if($frontend) : ?> |
| 115 |
<script type="text/javascript"> |
| 116 |
jQuery(document).ready(function($){ |
| 117 |
<?php if($class) : ?> |
| 118 |
$("#wpf-msg-box p.wpf-msg-box-triangle-right").removeClass("success").removeClass("error").addClass("<?php echo sanitize_html_class($class) ?>"); |
| 119 |
<?php endif ?> |
| 120 |
$("#wpf-msg-box p.wpf-msg-box-triangle-right").html("<span><?php echo wpforo_kses($inner) ?></span>"); |
| 121 |
$("#wpf-msg-box").show(150).delay(1000); |
| 122 |
<?php if($class) : ?> |
| 123 |
setTimeout(function(){ $("#wpf-msg-box").hide(); }, <?php echo ($class == 'error' ? 6000 : 3000 ) ?>); |
| 124 |
<?php endif ?> |
| 125 |
}); |
| 126 |
</script> |
| 127 |
<?php else : ?> |
| 128 |
<div class="notice is-dismissible<?php if($class) echo ' notice-' . sanitize_html_class($class) ?>"> |
| 129 |
<p><?php echo wpforo_kses($inner) ?></p> |
| 130 |
<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e('Dismiss this notice.', 'wpforo') ?></span></button> |
| 131 |
</div> |
| 132 |
<?php endif ?> |
| 133 |
<?php |
| 134 |
$this->clear(); |
| 135 |
} |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
?> |