PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 4.6.3
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v4.6.3
4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 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.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / vendor / priyomukul / wp-notice / src / Notice.php
embedpress / vendor / priyomukul / wp-notice / src Last commit date
Utils 11 months ago Dismiss.php 2 years ago Notice.php 11 months ago Notices.php 2 years ago
Notice.php
301 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 $output = '<ul style="display: flex; width: 100%; align-items: center;" class="notice-links ' . $this->app->id . '-notice-links">';
118 foreach ( $links as $link ) {
119 $_attributes = '';
120 $class = ! empty( $link['class'] ) ? $link['class'] : '';
121
122 if ( ! empty( $link['attributes'] ) ) {
123 $_attributes = $this->attributes( $link['attributes'] );
124 }
125
126 if( empty( $link['link'] ) ) {
127 $link['link'] = '#';
128 }
129
130 $output .= '<li style="margin: 0 15px 0 0;" class="notice-link-item ' . $class . '">';
131 $output .= ! empty( $link['link'] ) ? '<a href="' . esc_url( $link['link'] ) . '" ' . $_attributes . '>' : '';
132 if ( isset( $link['icon_class'] ) ) {
133 $output .= '<span style="margin-right: 5px" class="' . esc_attr( $link['icon_class'] ) . '"></span>';
134 }
135 $output .= $link['label'];
136 $output .= ! empty( $link['link'] ) ? '</a>' : '';
137 $output .= '</li>';
138 }
139
140 $output .= '</ul>';
141
142 return $output;
143 }
144
145 public function attributes( $params = [] ) {
146 $_attr = [];
147 if ( ! empty( $params ) ) {
148 foreach ( $params as $key => $value ) {
149 $_attr[ $key ] = $value;
150
151 if( $key === 'class' ) {
152 $_attr[ $key ] = [ $value ];
153 }
154
155 if( in_array( $key, [ 'data-later', 'data-dismiss' ] ) ) {
156 $_attr[ 'class' ][] = 'dismiss-btn';
157 }
158 }
159 }
160
161 $_attrs = [];
162 if( ! empty( $_attr ) ) {
163 foreach ( $_attr as $property => $value ) {
164 $_attrs[] = "$property=" . '"' . ( is_array( $value ) ? esc_attr( implode(' ', $value ) ) : esc_attr( $value ) ) . '"';
165 }
166 }
167
168 return implode( ' ', $_attrs );
169 }
170
171 public function url( $params = [] ) {
172 $nonce = wp_create_nonce( 'wpnotice_dismiss_notice_' . $this->id );
173
174 return esc_url( add_query_arg( [
175 'action' => 'wpnotice_dismiss_notice',
176 'id' => $this->id,
177 'nonce' => $nonce,
178 ], admin_url( '/' ) ) );
179 }
180
181 /**
182 * Get the notice classes.
183 *
184 * @access public
185 * @return string
186 * @since 1.0
187 */
188 public function get_classes() {
189 $classes = [ 'wpnotice-wrapper notice', $this->app->id ];
190
191 // Add the class for notice-type.
192 $classes[] = $this->options['classes'];
193 $classes[] = 'notice-' . $this->options['type'];
194 $classes[] = 'notice-' . $this->app->id . '-' . $this->id;
195
196 if ( $this->options['dismissible'] ) {
197 $classes[] = 'is-dismissible';
198 }
199
200 // Combine classes to a string.
201 return implode( ' ', $classes );
202 }
203
204 /**
205 * Determine if the notice should be shown or not.
206 *
207 * @access public
208 * @return bool
209 * @since 1.0
210 */
211 public function show() {
212 // External Condition Check
213 if ( isset( $this->options['display_if'] ) && ! $this->options['display_if'] ) {
214 return false;
215 }
216 // Don't show if the user doesn't have the required capability.
217 if ( ! is_null( $this->options['capability'] ) && ! current_user_can( $this->options['capability'] ) ) {
218 return false;
219 }
220
221 // Don't show if we're not on the right screen.
222 if ( ! $this->is_screen() ) {
223 return false;
224 }
225
226 // Don't show if notice has been dismissed.
227 if ( $this->dismiss->is_dismissed() ) {
228 return false;
229 }
230
231 // Start and Expiration Check.
232 if ( $this->time() <= $this->options['start'] ) {
233 return false;
234 }
235
236 if ( $this->is_expired() ) {
237 if ( $this->options['recurrence'] ) {
238 $_recurrence = intval( $this->options['recurrence'] );
239 $this->queue[ $this->id ]['start'] = $this->strtotime( "+$_recurrence days" );
240 $this->queue[ $this->id ]['expire'] = $this->strtotime( "+" . ( $_recurrence + 3 ) . " days" );
241 $this->app->storage()->save( $this->queue );
242 }
243
244 return false;
245 }
246
247 return true;
248 }
249
250 /**
251 * Evaluate if we're on the right place depending on the "screens" argument.
252 *
253 * @access private
254 * @return bool
255 * @since 1.0
256 */
257 private function is_screen() {
258 // Make sure the get_current_screen function exists.
259 if ( ! function_exists( 'get_current_screen' ) ) {
260 require_once ABSPATH . 'wp-admin/includes/screen.php';
261 }
262
263 /** @var WP_Screen $current_screen */
264 $current_screen = get_current_screen();
265
266 if ( $current_screen->id === 'update' ) {
267 return false;
268 }
269
270 // If screen is empty we want this shown on all screens.
271 if ( empty( $this->options['screens'] ) ) {
272 return true;
273 }
274
275 return ( in_array( $current_screen->id, $this->options['screens'], true ) );
276 }
277
278 public function is_expired() {
279 if ( isset( $this->options['expire'] ) && $this->time() >= $this->options['expire'] ) {
280 return true;
281 }
282
283 return false;
284 }
285
286 public function __call( $name, $args ) {
287 if ( property_exists( $this, $name ) ) {
288 return $this->{$name}[ $args[0] ];
289 }
290
291 return null;
292 }
293
294 public function get_thumbnail( $image ) {
295 $output = '<div class="wpnotice-thumbnail-wrapper">';
296 $output .= '<img style="max-width: 100%;" src="' . esc_url( $image ) . '">';
297 $output .= '</div>';
298
299 return wp_kses_post( $output );
300 }
301 }