| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Classes; |
| 4 |
|
| 5 |
use DOMDocument; |
| 6 |
|
| 7 |
class Notices |
| 8 |
{ |
| 9 |
protected static $notices = []; |
| 10 |
|
| 11 |
public static function init() |
| 12 |
{ |
| 13 |
|
| 14 |
libxml_use_internal_errors(true); |
| 15 |
|
| 16 |
self::removeNotices('admin_notices'); |
| 17 |
self::removeNotices('network_admin_notices'); |
| 18 |
} |
| 19 |
|
| 20 |
public static function removeNotices($hook_name) { |
| 21 |
|
| 22 |
global $wp_filter; |
| 23 |
|
| 24 |
if (isset($wp_filter[$hook_name])) { |
| 25 |
|
| 26 |
foreach ($wp_filter[$hook_name]->callbacks as $hooks) { |
| 27 |
|
| 28 |
foreach ($hooks as $hook) { |
| 29 |
|
| 30 |
ob_start(); |
| 31 |
call_user_func_array($hook["function"], []); |
| 32 |
$notice = ob_get_clean(); |
| 33 |
$notice = trim($notice); |
| 34 |
if (!empty($notice)) { |
| 35 |
self::setNotice($notice); |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
if(in_array($hook_name, ['admin_notices', 'network_admin_notices'])) { |
| 41 |
remove_all_actions($hook_name); |
| 42 |
} |
| 43 |
|
| 44 |
add_action('wpide_inline_scripts', [__CLASS__, 'appendInlineScript'], 1); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public static function appendInlineScript($scripts): string |
| 49 |
{ |
| 50 |
|
| 51 |
$ajaxurl = is_network_admin() ? 'ajaxurl + "?_fs_network_admin=true"' : 'ajaxurl + "?_fs_blog_admin=true"'; |
| 52 |
|
| 53 |
$scripts .= ' |
| 54 |
(function( $ ) { |
| 55 |
$(function() { |
| 56 |
|
| 57 |
$( document ).on("click", ".fs-notice.fs-sticky .fs-close", function() { |
| 58 |
var |
| 59 |
notice = $( this ).parents( ".fs-notice" ), |
| 60 |
container = $( this ).parents( ".nk-notification-item" ), |
| 61 |
index = container.index(), |
| 62 |
id = notice.attr( "data-id" ), |
| 63 |
ajaxActionSuffix = notice.attr( "data-manager-id" ).replace( ":", "-" ); |
| 64 |
|
| 65 |
notice.fadeOut( "fast", function() { |
| 66 |
var data = { |
| 67 |
action : "fs_dismiss_notice_action_" + ajaxActionSuffix, |
| 68 |
// As such we don"t need to use `wp_json_encode` method but using it to follow wp.org guideline. |
| 69 |
_wpnonce : '.wp_json_encode( wp_create_nonce( "fs_dismiss_notice_action" ) ).', |
| 70 |
message_id: id |
| 71 |
}; |
| 72 |
|
| 73 |
// since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php |
| 74 |
$.post('.$ajaxurl.', data); |
| 75 |
|
| 76 |
var event = new CustomEvent("wpide_notice_removed", { detail: index }); |
| 77 |
document.body.dispatchEvent(event); |
| 78 |
|
| 79 |
}); |
| 80 |
}); |
| 81 |
}); |
| 82 |
})( jQuery ); |
| 83 |
'; |
| 84 |
|
| 85 |
return $scripts; |
| 86 |
} |
| 87 |
|
| 88 |
public static function isValid($notice): bool |
| 89 |
{ |
| 90 |
|
| 91 |
if(!str_contains($notice, 'data-slug="wpide"')) { |
| 92 |
return false; |
| 93 |
} |
| 94 |
|
| 95 |
return true; |
| 96 |
} |
| 97 |
public static function setNotice($notice) |
| 98 |
{ |
| 99 |
if(!self::isValid($notice)) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
// check if notice has multiple notices, split them into separate notices. |
| 104 |
preg_match_all('/data-slug\="wpide"/ms', $notice, $matches); |
| 105 |
if(!empty($matches[0]) && count($matches[0]) > 1) { |
| 106 |
|
| 107 |
$dom = new DOMDocument(); |
| 108 |
|
| 109 |
$dom->LoadHTML($notice); |
| 110 |
$body = $dom->getElementsByTagName( 'body' )->item( 0 ); |
| 111 |
foreach( $body->childNodes as $node ) |
| 112 |
{ |
| 113 |
$notice = $dom->saveHTML($node); |
| 114 |
$notice = trim($notice); |
| 115 |
if(!empty($notice)) { |
| 116 |
$notice = self::format($notice); |
| 117 |
self::$notices[] = $notice; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
}else { |
| 122 |
$notice = self::format($notice); |
| 123 |
self::$notices[] = $notice; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
protected static function format($notice): array |
| 128 |
{ |
| 129 |
$type = 'info'; |
| 130 |
if (str_contains($notice, 'error') || str_contains($notice, 'danger')) { |
| 131 |
$type = 'error'; |
| 132 |
} else if (str_contains($notice, 'warning')) { |
| 133 |
$type = 'warning'; |
| 134 |
} else if (str_contains($notice, 'success') || str_contains($notice, 'updated')) { |
| 135 |
$type = 'success'; |
| 136 |
} |
| 137 |
|
| 138 |
$findTagStart = [ |
| 139 |
'<span', |
| 140 |
'<b>' |
| 141 |
]; |
| 142 |
$findTagEnd = [ |
| 143 |
'</span>', |
| 144 |
'</b>' |
| 145 |
]; |
| 146 |
$replaceTagStart = [ |
| 147 |
'<strong', |
| 148 |
'<strong>' |
| 149 |
]; |
| 150 |
$replaceTagEnd = '</strong>'; |
| 151 |
|
| 152 |
$notice = strip_tags($notice, ['a', 'p', 'div', 'span', 'strong', 'br', 'b', 'label', 'ul', 'li']); |
| 153 |
|
| 154 |
$notice = str_replace($findTagStart, $replaceTagStart, $notice); |
| 155 |
$notice = str_replace($findTagEnd, $replaceTagEnd, $notice); |
| 156 |
|
| 157 |
$notice = self::removeEmptyTags($notice); |
| 158 |
|
| 159 |
return [ |
| 160 |
'type' => $type, |
| 161 |
'content' => $notice |
| 162 |
]; |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
* @param string $str String to remove tags. |
| 167 |
* @param string $replace Replace empty string with. |
| 168 |
* @return string Cleaned string. |
| 169 |
*/ |
| 170 |
public static function removeEmptyTags($str, $replace = NULL) |
| 171 |
{ |
| 172 |
//** Return if string not given or empty. |
| 173 |
if (!is_string($str) |
| 174 |
|| trim($str) == '') |
| 175 |
return $str; |
| 176 |
|
| 177 |
//** Recursive empty HTML tags. |
| 178 |
return preg_replace( |
| 179 |
|
| 180 |
'/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/imsU', |
| 181 |
|
| 182 |
//** Replace with nothing if string empty. |
| 183 |
!is_string($replace) ? '' : $replace, |
| 184 |
|
| 185 |
//** Source string |
| 186 |
$str |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
public static function all(): array |
| 191 |
{ |
| 192 |
return self::$notices; |
| 193 |
} |
| 194 |
|
| 195 |
public static function count(): int |
| 196 |
{ |
| 197 |
return count(self::$notices); |
| 198 |
} |
| 199 |
} |
| 200 |
|