PluginProbe
گیت‌لند | درگاه پرداخت هوشمند گیت‌لند / 2.3.1
گیت‌لند | درگاه پرداخت هوشمند گیت‌لند v2.3.1
trunk 1.9.9 2.1.0 2.1.1 2.3.1
gateland / src / Notice.php

Notice.php in گیت‌لند | درگاه پرداخت هوشمند گیت‌لند 2.3.1, at src/Notice.php

263 lines 6.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Nabik\Gateland;
4
5 use DOMDocument;
6
7 defined( 'ABSPATH' ) || exit;
8
9 class Notice {
10
11 public function __construct() {
12 add_action( 'admin_notices', [ $this, 'admin_notices' ], 5 );
13 add_action( 'wp_ajax_gateland_dismiss_notice', [ $this, 'dismiss_notice' ] );
14 add_action( 'wp_ajax_gateland_update_notice', [ $this, 'update_notice' ] );
15 }
16
17 public function admin_notices() {
18
19 if ( ! current_user_can( 'manage_options' ) && ! current_user_can( 'manage_woocommerce' ) ) {
20 return;
21 }
22
23 if ( $this->is_dismiss( 'all' ) ) {
24 return;
25 }
26
27 foreach ( $this->notices() as $notice ) {
28
29 if ( ! $notice['condition'] || $this->is_dismiss( $notice['id'] ) ) {
30 continue;
31 }
32
33 $dismissible = $notice['dismiss'] ? 'is-dismissible' : '';
34 $notice_content = strip_tags( $notice['content'], '<p><a><b><img><ul><ol><li>' );
35
36 printf(
37 '<div class="notice gateland_notice notice-success %s" id="gateland_%s"><p>%s</p></div>',
38 esc_attr( $dismissible ),
39 esc_attr( $notice['id'] ),
40 $notice_content
41 );
42
43 break;
44 }
45
46 ?>
47 <script type="text/javascript">
48 jQuery(document).ready(function ($) {
49
50 $(document.body).on('click', '.notice-dismiss', function () {
51
52 let notice = $(this).closest('.gateland_notice');
53 notice = notice.attr('id');
54
55 if (notice !== undefined && notice.indexOf('gateland_') !== -1) {
56
57 notice = notice.replace('gateland_', '');
58
59 $.ajax({
60 url: "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>",
61 type: 'post',
62 data: {
63 notice: notice,
64 action: 'gateland_dismiss_notice',
65 nonce: "<?php echo esc_attr( wp_create_nonce( 'gateland_dismiss_notice' ) ); ?>"
66 }
67 });
68 }
69
70 });
71
72 });
73 </script>
74 <?php
75
76 if ( get_transient( 'gateland_update_notices' ) ) {
77 return;
78 }
79
80 ?>
81 <script type="text/javascript">
82 jQuery(document).ready(function ($) {
83
84 jQuery.ajax({
85 url: "<?php echo esc_url( admin_url( 'admin-ajax.php' ) ) ?>",
86 type: 'post',
87 data: {
88 action: 'gateland_update_notice',
89 nonce: '<?php echo esc_attr( wp_create_nonce( 'gateland_update_notice' ) ); ?>'
90 }
91 });
92
93 });
94 </script>
95 <?php
96 }
97
98 public function notices(): array {
99 global $pagenow;
100
101 $page = sanitize_text_field( $_GET['page'] ?? null );
102 $tab = sanitize_text_field( $_GET['tab'] ?? null );
103
104 $notices = [
105
106 // [
107 // 'id' => 'gateland_video',
108 // 'content' => '<b>آ�
109 وزش:</b> برای پیکربندی گیت‌لند �
110 ی توانید از <a href="https://yun.ir/gatelandvideo" target="_blank">اینجا</a> فیل�
111 های آ�
112 وزشی افزونه را �
113 شاهده کنید.',
114 // 'condition' => 1,
115 // 'dismiss' => 6 * MONTH_IN_SECONDS,
116 // ],
117
118 ];
119
120 $_notices = get_option( 'gateland_notices', [] );
121
122 foreach ( $_notices['notices'] ?? [] as $_notice ) {
123
124 $_notice['condition'] = 1;
125
126 $rules = $_notice['rules'];
127
128 if ( isset( $rules['pagenow'] ) && $rules['pagenow'] != $pagenow ) {
129 $_notice['condition'] = 0;
130 }
131
132 if ( isset( $rules['page'] ) && $rules['page'] != $page ) {
133 $_notice['condition'] = 0;
134 }
135
136 if ( isset( $rules['tab'] ) && $rules['tab'] != $tab ) {
137 $_notice['condition'] = 0;
138 }
139
140 if ( isset( $rules['active'] ) && is_plugin_inactive( $rules['active'] ) ) {
141 $_notice['condition'] = 0;
142 }
143
144 if ( isset( $rules['inactive'] ) && is_plugin_active( $rules['inactive'] ) ) {
145 $_notice['condition'] = 0;
146 }
147
148 unset( $_notice['rules'] );
149
150 array_unshift( $notices, $_notice );
151 }
152
153 return $notices;
154 }
155
156 public function dismiss_notice() {
157
158 check_ajax_referer( 'gateland_dismiss_notice', 'nonce' );
159
160 $this->set_dismiss( sanitize_text_field( $_POST['notice'] ) );
161
162 die();
163 }
164
165 public function update_notice() {
166
167 $update = get_transient( 'gateland_update_notices' );
168
169 if ( $update ) {
170 return;
171 }
172
173 set_transient( 'gateland_update_notices', 1, DAY_IN_SECONDS / 10 );
174
175 check_ajax_referer( 'gateland_update_notice', 'nonce' );
176
177 $notices = wp_remote_get( 'https://wpnotice.ir/gateland.json', [ 'timeout' => 5, ] );
178 $sign = wp_remote_get( 'https://wphash.ir/gateland.hash', [ 'timeout' => 5, ] );
179
180 if ( is_wp_error( $notices ) || is_wp_error( $sign ) ) {
181 die();
182 }
183
184 if ( ! is_array( $notices ) || ! is_array( $sign ) ) {
185 die();
186 }
187
188 $notices = trim( $notices['body'] );
189 $sign = trim( $sign['body'] );
190
191 if ( sha1( $notices ) !== $sign ) {
192 die();
193 }
194
195 $notices = json_decode( $notices, JSON_OBJECT_AS_ARRAY );
196
197 if ( empty( $notices ) || ! is_array( $notices ) ) {
198 die();
199 }
200
201 foreach ( $notices['notices'] as &$_notice ) {
202
203 $doc = new DOMDocument();
204 $content = strip_tags( $_notice['content'], '<p><a><b><img><ul><ol><li>' );
205 $content = str_replace( [ 'javascript', 'java', 'script' ], '', $content );
206 $doc->loadHTML( mb_convert_encoding( $content, 'HTML-ENTITIES', 'UTF-8' ) );
207
208 foreach ( $doc->getElementsByTagName( '*' ) as $element ) {
209
210 $href = null;
211 $src = null;
212 $style = $element->getAttribute( 'style' );
213
214 if ( $element->nodeName == 'a' ) {
215 $href = $element->getAttribute( 'href' );
216 }
217
218 if ( $element->nodeName == 'img' ) {
219 $src = $element->getAttribute( 'src' );
220 }
221
222 foreach ( $element->attributes as $attribute ) {
223 $element->removeAttribute( $attribute->name );
224 }
225
226 if ( $href && filter_var( $href, FILTER_VALIDATE_URL ) ) {
227 $element->setAttribute( 'href', $href );
228 $element->setAttribute( 'target', '_blank' );
229 }
230
231 if ( $src && filter_var( $src, FILTER_VALIDATE_URL ) && strpos( $src, 'https://repo.nabik.net' ) === 0 ) {
232 $element->setAttribute( 'src', $src );
233 }
234
235 if ( $style ) {
236 $element->setAttribute( 'style', $style );
237 }
238 }
239
240 $_notice['content'] = $doc->saveHTML();
241 }
242
243 update_option( 'gateland_notices', $notices );
244
245 die();
246 }
247
248 public function set_dismiss( string $notice_id ) {
249
250 $notices = wp_list_pluck( $this->notices(), 'dismiss', 'id' );
251
252 if ( isset( $notices[ $notice_id ] ) && $notices[ $notice_id ] ) {
253 update_option( 'gateland_dismiss_notice_' . $notice_id, time() + intval( $notices[ $notice_id ] ), 'yes' );
254 update_option( 'gateland_dismiss_notice_all', time() + DAY_IN_SECONDS );
255 }
256 }
257
258 public function is_dismiss( $notice_id ): bool {
259 return intval( get_option( 'gateland_dismiss_notice_' . $notice_id ) ) >= time();
260 }
261
262 }
263