| 1 |
<?php |
| 2 |
|
| 3 |
namespace wpforo\classes; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if( ! defined( 'ABSPATH' ) ) exit; |
| 7 |
|
| 8 |
|
| 9 |
class Notices { |
| 10 |
private $types = []; |
| 11 |
private $notices = []; |
| 12 |
private $timeouts = []; |
| 13 |
|
| 14 |
private function init_types() { |
| 15 |
$this->types = array_merge( |
| 16 |
[ 'neutral', 'error', 'success' ], |
| 17 |
apply_filters( 'wpforo_notice_types', [] ) |
| 18 |
); |
| 19 |
$this->types = array_map( 'strtolower', array_unique( $this->types ) ); |
| 20 |
} |
| 21 |
|
| 22 |
private function init_timeouts() { |
| 23 |
foreach( $this->types as $type ) $this->timeouts[ $type ] = $this->get_timeout( $type ); |
| 24 |
} |
| 25 |
|
| 26 |
private function get_timeout( $type ) { |
| 27 |
switch( $type ) { |
| 28 |
case "success": |
| 29 |
$durr = 4000; |
| 30 |
break; |
| 31 |
case "neutral": |
| 32 |
$durr = 0; |
| 33 |
break; |
| 34 |
default: |
| 35 |
$durr = 8000; |
| 36 |
} |
| 37 |
|
| 38 |
return apply_filters( "wpforo_notice_timeout_{$type}", $durr ); |
| 39 |
} |
| 40 |
|
| 41 |
public function get_timeouts() { |
| 42 |
return $this->timeouts; |
| 43 |
} |
| 44 |
|
| 45 |
private function reset() { |
| 46 |
foreach( $this->types as $type ) $this->notices[ $type ] = []; |
| 47 |
} |
| 48 |
|
| 49 |
public function is_empty() { |
| 50 |
foreach( $this->notices as $notice ) if( ! empty( $notice ) ) return false; |
| 51 |
|
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
public function __construct() { |
| 56 |
$this->init_types(); |
| 57 |
$this->reset(); |
| 58 |
add_action( 'wpforo_before_init', [ $this, 'init' ] ); |
| 59 |
} |
| 60 |
|
| 61 |
public function init() { |
| 62 |
$this->init_timeouts(); |
| 63 |
if( WPF()->session_token ) { |
| 64 |
$sql = "SELECT DISTINCT `key`, `value` FROM `" . WPF()->tables->logs . "` WHERE `sessionid` = %s AND `key` IN('" . implode( "','", $this->types ) . "')"; |
| 65 |
if( $notices = (array) WPF()->db->get_results( WPF()->db->prepare( $sql, WPF()->session_token ), ARRAY_A ) ) { |
| 66 |
foreach( $notices as $notice ) { |
| 67 |
if( trim( (string) $notice['value'] ) ) { |
| 68 |
$this->notices[ $notice['key'] ] = array_merge( $this->notices[ $notice['key'] ], wpforo_is_json( $notice['value'] ) ? json_decode( $notice['value'], true ) : (array) $notice['value'] ); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* |
| 77 |
* @param string|array $args |
| 78 |
* @param string $type (e.g. success|error) |
| 79 |
* @param string|array $s |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
public function add( $args, $type = 'neutral', $s = [] ) { |
| 84 |
if( ! $args ) return false; |
| 85 |
$args = (array) $args; |
| 86 |
if( $s && count( $args ) === 1 && is_array( $s ) && isset( $s[0] ) && ! is_array( $s[0] ) ) { |
| 87 |
$s = [ $s ]; |
| 88 |
} else { |
| 89 |
$s = (array) $s; |
| 90 |
} |
| 91 |
|
| 92 |
if( WPF()->session_token ) { |
| 93 |
$type = strtolower( (string) $type ); |
| 94 |
foreach( $args as $key => $arg ) { |
| 95 |
if( $s && isset( $s[ $key ] ) ) { |
| 96 |
$args[ $key ] = wpforo_sprintf_array( wpforo_phrase( $arg, false ), $s[ $key ] ); |
| 97 |
} else { |
| 98 |
$args[ $key ] = wpforo_phrase( $arg, false ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
$this->notices[ $type ] = array_merge( (array) $this->notices[ $type ], (array) $args ); |
| 103 |
$this->notices[ $type ] = array_unique( $this->notices[ $type ] ); |
| 104 |
|
| 105 |
if( ! wpforo_is_ajax() ) { |
| 106 |
$insert_id_backup = WPF()->db->insert_id; |
| 107 |
WPF()->db->insert( WPF()->tables->logs, [ |
| 108 |
'sessionid' => WPF()->session_token, |
| 109 |
'key' => $type, |
| 110 |
'value' => json_encode( (array) $args ), |
| 111 |
], [ '%s', '%s', '%s' ] ); |
| 112 |
WPF()->db->insert_id = $insert_id_backup; |
| 113 |
} |
| 114 |
|
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* |
| 123 |
* @return bool |
| 124 |
* |
| 125 |
*/ |
| 126 |
public function clear() { |
| 127 |
if( WPF()->session_token ) { |
| 128 |
$this->reset(); |
| 129 |
|
| 130 |
WPF()->db->delete( WPF()->tables->logs, [ 'sessionid' => WPF()->session_token ], [ '%s' ] ); |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* <p class="success">success msg text</p><p class="error">error msg text</p> |
| 140 |
* |
| 141 |
* @return string |
| 142 |
*/ |
| 143 |
public function get_notices() { |
| 144 |
$inner = ''; |
| 145 |
if( ! $this->is_empty() ) { |
| 146 |
foreach( $this->notices as $type => $notice ) { |
| 147 |
$notice = (array) $notice; |
| 148 |
foreach( $notice as $msg ) { |
| 149 |
if( ! is_array( $msg ) ) { |
| 150 |
if( $msg = trim( (string) $msg ) ) $inner .= sprintf( '<p class="%s">%s</p>', sanitize_html_class( $type ), $msg ); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
$this->clear(); |
| 156 |
} |
| 157 |
|
| 158 |
return $inner; |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* |
| 163 |
* show collected wpforo notices |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function show() { |
| 168 |
if( $this->is_empty() ) return; |
| 169 |
if( wpforo_is_admin() ) { |
| 170 |
$this->backend( $this->notices ); |
| 171 |
} else { |
| 172 |
$this->frontend( $this->notices ); |
| 173 |
} |
| 174 |
$this->clear(); |
| 175 |
} |
| 176 |
|
| 177 |
private function backend( $notices ) { |
| 178 |
$inner = ''; |
| 179 |
foreach( $notices as $type => $notice ) { |
| 180 |
$notice = (array) $notice; |
| 181 |
foreach( $notice as $msg ) { |
| 182 |
if( ! is_array( $msg ) && ( $msg = trim( (string) $msg ) ) ) { |
| 183 |
$inner .= sprintf( |
| 184 |
'<div class="notice is-dismissible notice-%s"> |
| 185 |
<p>%s</p> |
| 186 |
</div>', |
| 187 |
sanitize_html_class( $type ), |
| 188 |
wpforo_kses( $msg ) |
| 189 |
); |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
echo '<div class="wpf-backend-notices-wrap">' . $inner . '</div>'; |
| 194 |
} |
| 195 |
|
| 196 |
private function frontend( $notices ) { |
| 197 |
$inner = ''; |
| 198 |
foreach( $notices as $type => $notice ) { |
| 199 |
$notice = (array) $notice; |
| 200 |
foreach( $notice as $msg ) { |
| 201 |
if( ! is_array( $msg ) && ( $msg = trim( (string) $msg ) ) ) { |
| 202 |
$inner .= sprintf( PHP_EOL . 'wpforo_notice_show("%1$s", "%2$s");' . PHP_EOL, addslashes( wpforo_kses( $msg ) ), sanitize_html_class( $type ) ); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
?> |
| 207 |
<script type="text/javascript"> |
| 208 |
window.jQuery(document).ready(function () { |
| 209 |
<?php echo $inner ?> |
| 210 |
}) |
| 211 |
</script> |
| 212 |
<?php |
| 213 |
} |
| 214 |
|
| 215 |
public function addonNote() { |
| 216 |
$lastHash = get_option( 'wpforo_addon_note_dismissed' ); |
| 217 |
if( ! $lastHash ) { |
| 218 |
$hash = $this->addonHash(); |
| 219 |
update_option( 'wpforo_addon_note_dismissed', $hash ); |
| 220 |
update_option( 'wpforo_addon_note_first', 'true' ); |
| 221 |
} else { |
| 222 |
$lastHashArray = explode( ',', $lastHash ); |
| 223 |
$currentHash = $this->addonHash(); |
| 224 |
if( $lastHash != $currentHash ) { |
| 225 |
?> |
| 226 |
<div class="updated notice wpforo_addon_note is-dismissible" style="margin-top:10px;"> |
| 227 |
<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> |
| 228 |
<div style="font-size:14px;"> |
| 229 |
<?php |
| 230 |
foreach( wpforo_get_addons_info() as $addon ) { |
| 231 |
if( in_array( $addon['title'], $lastHashArray ) ) { |
| 232 |
continue; |
| 233 |
} |
| 234 |
?> |
| 235 |
<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> |
| 236 |
<?php |
| 237 |
} |
| 238 |
?> |
| 239 |
<div style="clear:both;"></div> |
| 240 |
</div> |
| 241 |
<p> <a href="<?php echo admin_url( 'admin.php?page=' . wpforo_prefix_slug( 'addons' ) ) ?>"><?php _e( 'View all Addons', 'wpforo' ); ?> »</a></p> |
| 242 |
</div> |
| 243 |
<script>jQuery(document).on('click', '.wpforo_addon_note .notice-dismiss', function () {jQuery.ajax({ url: ajaxurl, data: { action: 'dismiss_wpforo_addon_note' } })})</script> |
| 244 |
<?php |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
public function dismissAddonNote() { |
| 250 |
$hash = $this->addonHash(); |
| 251 |
update_option( 'wpforo_addon_note_dismissed', $hash ); |
| 252 |
exit(); |
| 253 |
} |
| 254 |
|
| 255 |
public function dismissAddonNoteOnPage() { |
| 256 |
$hash = $this->addonHash(); |
| 257 |
update_option( 'wpforo_addon_note_dismissed', $hash ); |
| 258 |
} |
| 259 |
|
| 260 |
public function addonHash() { |
| 261 |
$viewed = ''; |
| 262 |
foreach( wpforo_get_addons_info() as $addon ) { |
| 263 |
$viewed .= $addon['title'] . ','; |
| 264 |
} |
| 265 |
|
| 266 |
return $viewed; |
| 267 |
} |
| 268 |
|
| 269 |
public function refreshAddonPage() { |
| 270 |
$lastHash = get_option( 'wpforo_addon_note_dismissed' ); |
| 271 |
$currentHash = $this->addonHash(); |
| 272 |
if( $lastHash != $currentHash ) { |
| 273 |
?> |
| 274 |
<script language="javascript">jQuery(document).ready(function () { |
| 275 |
location.reload() |
| 276 |
})</script> |
| 277 |
<?php |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
public function dismissCacheConflict(){ |
| 282 |
$excluded = wpforo_get_option( 'wpforo_excluded_cache', '', false ); |
| 283 |
$not_excluded_plugins = WPF()->cache->cache_plugins_status(); |
| 284 |
if( !empty($not_excluded_plugins) ){ |
| 285 |
foreach( $not_excluded_plugins as $plugin ){ |
| 286 |
$excluded .= ',' . $plugin['name']; |
| 287 |
} |
| 288 |
} |
| 289 |
wpforo_update_option( 'wpforo_excluded_cache', trim( (string) $excluded, ',' ) ); |
| 290 |
exit(); |
| 291 |
} |
| 292 |
|
| 293 |
} |
| 294 |
|