PluginProbe ʕ •ᴥ•ʔ
Essential Addons for Elementor – Popular Elementor Templates & Widgets / 6.2.3
Essential Addons for Elementor – Popular Elementor Templates & Widgets v6.2.3
6.6.9 6.6.8 6.6.7 6.6.6 6.6.5 6.6.4 6.6.3 5.7.3 5.7.4 5.8.0 5.8.1 5.8.10 5.8.11 5.8.12 5.8.13 5.8.14 5.8.15 5.8.16 5.8.18 5.8.2 5.8.3 5.8.4 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.10 5.9.11 5.9.12 5.9.13 5.9.14 5.9.15 5.9.16 5.9.17 5.9.18 5.9.19 5.9.2 5.9.20 5.9.21 5.9.22 5.9.23 5.9.24 5.9.25 5.9.26 5.9.27 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 6.0.1 6.0.10 6.0.11 6.0.12 6.0.13 6.0.14 6.0.15 6.0.2 6.0.3 6.0.4 6.0.5 6.0.6 6.0.7 6.0.8 6.0.9 6.1.0 6.1.1 6.1.10 6.1.11 trunk 6.1.12 1.0.0 6.1.13 1.0.1 6.1.14 1.1.0 6.1.15 2.0 6.1.17 2.1 6.1.18 2.10.0 6.1.19 2.10.1 6.1.2 2.10.2 6.1.20 2.10.3 6.1.3 2.10.4 6.1.4 2.10.5 6.1.5 2.2.0 6.1.6 2.2.1 6.1.7 2.2.2 6.1.8 2.2.3 6.1.9 2.2.4 6.2.0 2.2.5 6.2.1 2.3.0 6.2.2 2.3.1 6.2.3 2.4.0 6.2.4 2.4.1 6.3.0 2.4.2 6.3.1 2.4.3 6.3.2 2.5.0 6.3.3 2.6.0 6.4.0 2.7.0 6.5.0 2.7.1 6.5.1 2.7.10 6.5.10 2.7.11 6.5.11 2.7.2 6.5.12 2.7.3 6.5.13 2.7.4 6.5.2 2.7.5 6.5.3 2.7.6 6.5.4 2.7.7 6.5.5 2.7.8 6.5.6 2.7.9 6.5.7 2.8.0 6.5.8 2.8.1 6.5.9 2.8.2 6.6.0 2.8.3 6.6.1 2.8.4 6.6.2 2.8.5 2.8.6 2.8.7 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 2.9.8 2.9.9 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.4.1 3.1.5 3.2.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.5.0 3.5.1 3.5.2 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.8 4.3.9 4.4.0 4.4.1 4.5.0 4.5.1 4.5.2 4.5.3 4.5.4 4.5.5 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 5.0.0 5.0.1 5.0.10 5.0.11 5.0.12 5.0.13 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.0.9 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.1.8 5.1.9 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.4.6 5.5.5 5.6.5 5.7.2
essential-addons-for-elementor-lite / vendor / priyomukul / wp-notice / src / Notice.php
essential-addons-for-elementor-lite / vendor / priyomukul / wp-notice / src Last commit date
Utils 2 years ago Dismiss.php 2 years ago Notice.php 2 years ago Notices.php 2 years ago
Notice.php
294 lines
1 <?php
2
3 namespace PriyoMukul\WPNotice;
4
5 use PriyoMukul\WPNotice\Utils\Base;
6 use PriyoMukul\WPNotice\Utils\Helper;
7 use WP_Screen;
8 use function property_exists;
9
10 #[\AllowDynamicProperties]
11 class Notice extends Base {
12 use Helper;
13
14 private $app = null;
15
16 private $id = null;
17 private $content = null;
18
19 /**
20 * @var Dismiss
21 */
22 public $dismiss;
23
24 private $queue = [];
25
26 /**
27 * @var int start
28 * @var int expire
29 * @var int recurrence Meaning this notice will appear after 10, 20 days.
30 * @var string scope
31 * @var array screens
32 * @var string type Notice type
33 * @var string capability
34 * @var bool dismissible
35 */
36 private $options = [
37 // 'start' => 192933, // timestamp
38 // 'expire' => 1029339, // timestamp
39 'classes' => '',
40 'recurrence' => false,
41 'scope' => 'user',
42 'screens' => null,
43 'type' => 'info',
44 'capability' => null,
45 'dismissible' => false,
46 ];
47
48 public function __construct( ...$args ) {
49 list( $id, $content, $options, $queue, $app ) = $args;
50
51 $this->app = $app;
52 $this->id = $id;
53 $this->content = $content;
54 $this->queue = $queue;
55 $this->options = wp_parse_args( $options, $this->options );
56
57 $this->dismiss = new Dismiss( $this->id, $this->options, $this->app );
58
59 if ( ! isset( $queue[ $id ] ) || ( ! empty( $this->options['refresh'] ) && ( empty( $queue[ $id ]['refresh'] ) || $queue[ $id ]['refresh'] != $this->options['refresh'] ) ) ) {
60 $queue[ $id ] = [];
61 $_eligible_keys = [ 'start', 'expire', 'recurrence', 'refresh' ];
62 array_walk( $options, function ( $value, $key ) use ( $id, &$queue, $_eligible_keys ) {
63 if ( in_array( $key, $_eligible_keys, true ) ) {
64 $queue[ $id ][ $key ] = $value;
65 }
66 } );
67
68 $this->queue = $queue;
69 $this->app->storage()->save( $queue ); // saved in queue
70 } else {
71 $this->options = wp_parse_args( $queue[ $id ], $this->options );
72 }
73
74 if ( isset( $this->options['do_action'] ) ) {
75 add_action( 'admin_init', [ $this, 'do_action' ] );
76 }
77 }
78
79 public function do_action() {
80 do_action( $this->options['do_action'], $this );
81 }
82
83 private function get_content() {
84 if ( is_callable( $this->content ) ) {
85 ob_start();
86 call_user_func( $this->content );
87
88 return ob_get_clean();
89 }
90
91 return $this->content;
92 }
93
94 public function display( $force = false ) {
95 if ( ! $force && ! $this->show() ) {
96 return;
97 }
98
99 $content = $this->get_content();
100 if ( empty( $content ) ) {
101 return; // Return if notice is empty.
102 }
103
104 $links = $this->get_links();
105
106 // Print the notice.
107 printf( '<div style="display: flex; flex-wrap: nowrap; gap: 15px; align-items: center;" id="%1$s" class="%2$s">%3$s<div class="wpnotice-content-wrapper">%4$s%5$s</div></div>', 'wpnotice-' . esc_attr( $this->app->app ) . '-' . esc_attr( $this->id ), // The ID.
108 esc_attr( $this->get_classes() ), // The classes.
109 ! empty( $content['thumbnail'] ) ? $this->get_thumbnail( $content['thumbnail'] ) : '', ! empty( $content['html'] ) ? $content['html'] : $content, ! empty( $links ) ? $this->links( $links ) : '' );
110 }
111
112 public function get_links() {
113 return ! empty( $this->content['links'] ) ? $this->content['links'] : ( ! empty( $this->options['links'] ) ? $this->options['links'] : [] );
114 }
115
116 public function links( $links ) {
117 $_attributes = '';
118 $output = '<ul style="display: flex; width: 100%; align-items: center;" class="notice-links ' . $this->app->id . '-notice-links">';
119 foreach ( $links as $link ) {
120 $class = ! empty( $link['class'] ) ? $link['class'] : '';
121
122 if ( ! empty( $link['attributes'] ) ) {
123 $link['attributes']['target'] = '_top';
124 $_attributes = $this->attributes( $link['attributes'] );
125 $link['link'] = '#';
126 }
127
128 $output .= '<li style="margin: 0 15px 0 0;" class="notice-link-item ' . $class . '">';
129 $output .= ! empty( $link['link'] ) ? '<a href="' . esc_url( $link['link'] ) . '" ' . $_attributes . '>' : '';
130 if ( isset( $link['icon_class'] ) ) {
131 $output .= '<span style="margin-right: 5px" class="' . esc_attr( $link['icon_class'] ) . '"></span>';
132 }
133 $output .= $link['label'];
134 $output .= ! empty( $link['link'] ) ? '</a>' : '';
135 $output .= '</li>';
136 }
137
138 $output .= '</ul>';
139
140 return $output;
141 }
142
143 public function attributes( $params = [] ) {
144 $_attr = [];
145 $classname = 'dismiss-btn ';
146
147 if ( ! empty( $params['class'] ) ) {
148 $classname .= $params['class'];
149 unset( $params['class'] );
150 }
151
152 $_attr[] = 'class="' . esc_attr( $classname ) . '"';
153
154 $_attr[] = 'target="_blank"';
155 if ( ! empty( $params ) ) {
156 foreach ( $params as $key => $value ) {
157 $_attr[] = "$key='$value'";
158 }
159 }
160
161 return \implode( ' ', $_attr );
162 }
163
164 public function url( $params = [] ) {
165 $nonce = wp_create_nonce( 'wpnotice_dismiss_notice_' . $this->id );
166
167 return esc_url( add_query_arg( [
168 'action' => 'wpnotice_dismiss_notice',
169 'id' => $this->id,
170 'nonce' => $nonce,
171 ], admin_url( '/' ) ) );
172 }
173
174 /**
175 * Get the notice classes.
176 *
177 * @access public
178 * @return string
179 * @since 1.0
180 */
181 public function get_classes() {
182 $classes = [ 'wpnotice-wrapper notice', $this->app->id ];
183
184 // Add the class for notice-type.
185 $classes[] = $this->options['classes'];
186 $classes[] = 'notice-' . $this->options['type'];
187 $classes[] = 'notice-' . $this->app->id . '-' . $this->id;
188
189 if ( $this->options['dismissible'] ) {
190 $classes[] = 'is-dismissible';
191 }
192
193 // Combine classes to a string.
194 return implode( ' ', $classes );
195 }
196
197 /**
198 * Determine if the notice should be shown or not.
199 *
200 * @access public
201 * @return bool
202 * @since 1.0
203 */
204 public function show() {
205 // External Condition Check
206 if ( isset( $this->options['display_if'] ) && ! $this->options['display_if'] ) {
207 return false;
208 }
209 // Don't show if the user doesn't have the required capability.
210 if ( ! is_null( $this->options['capability'] ) && ! current_user_can( $this->options['capability'] ) ) {
211 return false;
212 }
213
214 // Don't show if we're not on the right screen.
215 if ( ! $this->is_screen() ) {
216 return false;
217 }
218
219 // Don't show if notice has been dismissed.
220 if ( $this->dismiss->is_dismissed() ) {
221 return false;
222 }
223
224 // Start and Expiration Check.
225 if ( $this->time() <= $this->options['start'] ) {
226 return false;
227 }
228
229 if ( $this->is_expired() ) {
230 if ( $this->options['recurrence'] ) {
231 $_recurrence = intval( $this->options['recurrence'] );
232 $this->queue[ $this->id ]['start'] = $this->strtotime( "+$_recurrence days" );
233 $this->queue[ $this->id ]['expire'] = $this->strtotime( "+" . ( $_recurrence + 3 ) . " days" );
234 $this->app->storage()->save( $this->queue );
235 }
236
237 return false;
238 }
239
240 return true;
241 }
242
243 /**
244 * Evaluate if we're on the right place depending on the "screens" argument.
245 *
246 * @access private
247 * @return bool
248 * @since 1.0
249 */
250 private function is_screen() {
251 // Make sure the get_current_screen function exists.
252 if ( ! function_exists( 'get_current_screen' ) ) {
253 require_once ABSPATH . 'wp-admin/includes/screen.php';
254 }
255
256 /** @var WP_Screen $current_screen */
257 $current_screen = get_current_screen();
258
259 if ( $current_screen->id === 'update' ) {
260 return false;
261 }
262
263 // If screen is empty we want this shown on all screens.
264 if ( empty( $this->options['screens'] ) ) {
265 return true;
266 }
267
268 return ( in_array( $current_screen->id, $this->options['screens'], true ) );
269 }
270
271 public function is_expired() {
272 if ( isset( $this->options['expire'] ) && $this->time() >= $this->options['expire'] ) {
273 return true;
274 }
275
276 return false;
277 }
278
279 public function __call( $name, $args ) {
280 if ( property_exists( $this, $name ) ) {
281 return $this->{$name}[ $args[0] ];
282 }
283
284 return null;
285 }
286
287 public function get_thumbnail( $image ) {
288 $output = '<div class="wpnotice-thumbnail-wrapper">';
289 $output .= '<img style="max-width: 100%;" src="' . esc_url( $image ) . '">';
290 $output .= '</div>';
291
292 return wp_kses_post( $output );
293 }
294 }