PluginProbe
WPIDE – File Manager & Code Editor / 3.1
WPIDE – File Manager & Code Editor v3.1
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Classes / Notices.php

Notices.php in WPIDE – File Manager & Code Editor 3.1, at App/Classes/Notices.php

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