| 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 string|array $args |
| 36 |
* @param string $type (e.g. success|error) |
| 37 |
* @param string|array $s |
| 38 |
* |
| 39 |
* @return bool |
| 40 |
*/ |
| 41 |
public function add( $args, $type = '', $s = array() ){ |
| 42 |
if(!$args) return FALSE; |
| 43 |
$args = (array) $args; |
| 44 |
if( $s && count($args) == 1 && is_array($s) && isset($s[0]) && !is_array($s[0]) ){ |
| 45 |
$s = array($s); |
| 46 |
}else{ |
| 47 |
$s = (array) $s; |
| 48 |
} |
| 49 |
|
| 50 |
if( $this->is_session_started() ){ |
| 51 |
$_SESSION['wpforo_notice_type'] = $type; |
| 52 |
if( !isset($_SESSION['wpforo_notices']) ) $_SESSION['wpforo_notices'] = array(); |
| 53 |
|
| 54 |
foreach($args as $key => $arg){ |
| 55 |
if( $s && isset($s[$key]) ){ |
| 56 |
$args[$key] = wpforo_sprintf_array( wpforo_phrase($arg, FALSE), $s[$key] ); |
| 57 |
}else{ |
| 58 |
$args[$key] = wpforo_phrase($arg, FALSE); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$_SESSION['wpforo_notices'] = array_merge( (array) $_SESSION['wpforo_notices'], (array) $args); |
| 63 |
$_SESSION['wpforo_notices'] = array_unique($_SESSION['wpforo_notices']); |
| 64 |
return TRUE; |
| 65 |
} |
| 66 |
|
| 67 |
return FALSE; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* |
| 72 |
* @return bool |
| 73 |
* |
| 74 |
*/ |
| 75 |
public function clear(){ |
| 76 |
if( $this->is_session_started() ){ |
| 77 |
unset($_SESSION['wpforo_notice_type'], $_SESSION['wpforo_notices']); |
| 78 |
return TRUE; |
| 79 |
} |
| 80 |
|
| 81 |
return FALSE; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* get notices $notices['type'], $notices['text'] |
| 86 |
* |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function get_notices(){ |
| 90 |
if(!isset($_SESSION['wpforo_notices'])) return array( 'type' => '', 'text' => '' ); |
| 91 |
if($_SESSION['wpforo_notice_type'] == 'success'){ |
| 92 |
$class = 'success'; |
| 93 |
}elseif($_SESSION['wpforo_notice_type'] == 'error'){ |
| 94 |
$class = 'error'; |
| 95 |
}else{ |
| 96 |
$class = ''; |
| 97 |
} |
| 98 |
|
| 99 |
$notices = array(); |
| 100 |
$notices['type'] = $class; |
| 101 |
$notices['text'] = ''; |
| 102 |
foreach($_SESSION['wpforo_notices'] as $notice) if( !is_array($notice) ) $notices['text'] .= wpforo_phrase($notice, FALSE) . '<br/>'; |
| 103 |
|
| 104 |
$this->clear(); |
| 105 |
return $notices; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* |
| 110 |
* @param bool $frontend (default TRUE) |
| 111 |
* |
| 112 |
* @return void |
| 113 |
*/ |
| 114 |
public function show( $frontend = TRUE ){ |
| 115 |
if(!isset($_SESSION['wpforo_notices'])) return; |
| 116 |
if($_SESSION['wpforo_notice_type'] == 'success'){ |
| 117 |
$class = 'success'; |
| 118 |
}elseif($_SESSION['wpforo_notice_type'] == 'error'){ |
| 119 |
$class = 'error'; |
| 120 |
}else{ |
| 121 |
$class = ''; |
| 122 |
} |
| 123 |
$inner = ''; |
| 124 |
foreach($_SESSION['wpforo_notices'] as $notice) if( !is_array($notice) ) $inner .= wpforo_phrase($notice, FALSE) . '<br/><br/>'; |
| 125 |
$inner = preg_replace('#(<br\s*/?\s*>)*$#is', '', $inner); |
| 126 |
?> |
| 127 |
<?php if($frontend) : ?> |
| 128 |
<script type="text/javascript"> |
| 129 |
jQuery(document).ready(function($){ |
| 130 |
var msg_box = $("#wpf-msg-box-p"); |
| 131 |
<?php if($class) : ?> |
| 132 |
msg_box.removeClass("success").removeClass("error").addClass("<?php echo sanitize_html_class($class) ?>"); |
| 133 |
<?php endif ?> |
| 134 |
msg_box.html("<span><?php echo addslashes(wpforo_kses($inner)) ?></span>"); |
| 135 |
$("#wpf-msg-box").show(150).delay(1000); |
| 136 |
<?php if($class) : ?> |
| 137 |
setTimeout(function(){ $("#wpf-msg-box").hide(); }, <?php echo ($class == 'error' ? 6000 : 3000 ) ?>); |
| 138 |
<?php endif ?> |
| 139 |
}); |
| 140 |
</script> |
| 141 |
<?php else : ?> |
| 142 |
<div class="notice is-dismissible<?php if($class) echo ' notice-' . sanitize_html_class($class) ?>"> |
| 143 |
<p><?php echo wpforo_kses($inner) ?></p> |
| 144 |
<button type="button" class="notice-dismiss"><span class="screen-reader-text"><?php _e('Dismiss this notice.', 'wpforo') ?></span></button> |
| 145 |
</div> |
| 146 |
<?php endif ?> |
| 147 |
<?php |
| 148 |
$this->clear(); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
public function addonNote() { |
| 153 |
global $wpforo; |
| 154 |
$lastHash = get_option('wpforo-addon-note-dismissed'); |
| 155 |
$lastHashArray = explode(',', $lastHash); |
| 156 |
$currentHash = $this->addonHash(); |
| 157 |
if ($lastHash != $currentHash) { |
| 158 |
?> |
| 159 |
<div class="updated notice wpforo_addon_note is-dismissible" style="margin-top:10px;"> |
| 160 |
<p style="font-weight:normal; font-size:15px; border-bottom:1px dotted #DCDCDC; padding-bottom:10px; width:95%;"><strong><?php _e('New Addons for Your Forum!', 'wpforo'); ?></strong><br><span style="font-size:14px;"><?php _e('Extend your forum with wpForo addons', 'wpforo'); ?></span></p> |
| 161 |
<div style="font-size:14px;"> |
| 162 |
<?php |
| 163 |
foreach ($wpforo->addons as $key => $addon) { |
| 164 |
if (in_array($addon['title'], $lastHashArray)) |
| 165 |
continue; |
| 166 |
?> |
| 167 |
<div style="display:inline-block; min-width:27%; padding-right:10px; margin-bottom:1px;border-bottom:1px dotted #DCDCDC; border-right:1px dotted #DCDCDC; padding-bottom:10px;"><img src="<?php echo $addon['thumb'] ?>" style="height:40px; width:auto; vertical-align:middle; margin:0 10px; text-decoration:none;" /> <a href="<?php echo $addon['url'] ?>" style="text-decoration:none;" target="_blank">wpForo <?php echo $addon['title']; ?></a></div> |
| 168 |
<?php |
| 169 |
} |
| 170 |
?> |
| 171 |
<div style="clear:both;"></div> |
| 172 |
</div> |
| 173 |
<p> <a href="<?php echo admin_url('admin.php?page=wpforo-addons') ?>"><?php _e('View all Addons', 'wpforo'); ?> »</a></p> |
| 174 |
</div> |
| 175 |
<script>jQuery(document).on( 'click', '.wpforo_addon_note .notice-dismiss', function() {jQuery.ajax({url: ajaxurl, data: { action: 'dismiss_wpforo_addon_note'}})})</script> |
| 176 |
<?php |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
public function dismissAddonNote() { |
| 181 |
$hash = $this->addonHash(); |
| 182 |
update_option('wpforo-addon-note-dismissed', $hash); |
| 183 |
exit(); |
| 184 |
} |
| 185 |
|
| 186 |
public function dismissAddonNoteOnPage() { |
| 187 |
$hash = $this->addonHash(); |
| 188 |
update_option('wpforo-addon-note-dismissed', $hash); |
| 189 |
} |
| 190 |
|
| 191 |
public function addonHash() { |
| 192 |
global $wpforo; $viewed = ''; |
| 193 |
foreach ($wpforo->addons as $key => $addon) { |
| 194 |
$viewed .= $addon['title'] . ','; |
| 195 |
} |
| 196 |
$hash = $viewed; |
| 197 |
return $hash; |
| 198 |
} |
| 199 |
|
| 200 |
public function refreshAddonPage() { |
| 201 |
$lastHash = get_option('wpforo-addon-note-dismissed'); |
| 202 |
$currentHash = $this->addonHash(); |
| 203 |
if ($lastHash != $currentHash) { |
| 204 |
?> |
| 205 |
<script language="javascript">jQuery(document).ready(function () { |
| 206 |
location.reload(); |
| 207 |
});</script> |
| 208 |
<?php |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
} |