PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents / 3.5.3
EmbedPress – PDF Embedder, 3D PDF FlipBook, Google Reviews, YouTube Videos, Upload & Embed PDF documents v3.5.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 4 years ago Dismiss.php 4 years ago Notice.php 4 years ago Notices.php 4 years ago
Notice.php
290 lines
1 <?php
2
3 namespace PriyoMukul\WPNotice;
4
5 use PriyoMukul\WPNotice\Utils\Base;
6 use PriyoMukul\WPNotice\Utils\Storage;
7 use PriyoMukul\WPNotice\Utils\Helper;
8
9 use function property_exists;
10
11 class Notice extends Base {
12 use Helper;
13
14 private $app = null;
15
16 private $id = null;
17 private $content = null;
18 public $dismiss = null;
19
20 private $queue = [];
21
22 /**
23 * @var int start
24 * @var int expire
25 * @var int recurrence Meaning this notice will appear after 10, 20 days.
26 * @var string scope
27 * @var array screens
28 * @var string type Notice type
29 * @var string capability
30 * @var bool dismissible
31 */
32 private $options = [
33 // 'start' => 192933, // timestamp
34 // 'expire' => 1029339, // timestamp
35 'classes' => '',
36 'recurrence' => false,
37 'scope' => 'user',
38 'screens' => null,
39 'type' => 'info',
40 'capability' => null,
41 'dismissible' => false,
42 ];
43
44 public function __construct( ...$args ){
45 list( $id, $content, $options, $queue, $app ) = $args;
46
47 $this->app = $app;
48 $this->id = $id;
49 $this->content = $content;
50 $this->queue = $queue;
51 $this->options = wp_parse_args( $options, $this->options );
52
53 $this->dismiss = new Dismiss( $this->id, $this->options, $this->app );
54
55 if( ! isset( $queue[ $id ] ) ) {
56 $queue[ $id ] = [];
57 $_eligible_keys = ['start', 'expire', 'recurrence'];
58 array_walk($options, function( $value, $key ) use( $id, &$queue, $_eligible_keys ) {
59 if( in_array( $key, $_eligible_keys, true ) ) {
60 $queue[ $id ][ $key ] = $value;
61 }
62 });
63
64 $this->queue = $queue;
65 $this->app->storage()->save( $queue ); // saved in queue
66 } else {
67 $this->options = wp_parse_args( $queue[ $id ], $this->options );
68 }
69
70 if( isset( $this->options['do_action'] ) ) {
71 add_action( 'admin_init', [ $this, 'do_action' ] );
72 }
73 }
74
75 public function do_action(){
76 do_action( $this->options['do_action'], $this );
77 }
78
79 private function get_content(){
80 if( is_callable( $this->content ) ) {
81 ob_start();
82 call_user_func( $this->content );
83 return ob_get_clean();
84 }
85 return $this->content;
86 }
87
88 public function display( $force = false ){
89 if ( ! $force && ! $this->show() ) {
90 return;
91 }
92
93 $content = $this->get_content();
94 if( empty( $content ) ) {
95 return; // Return if notice is empty.
96 }
97
98 $links = $this->get_links();
99
100 // Print the notice.
101 printf(
102 '<div style="display: grid; grid-template-columns: 50px 1fr; align-items: center;" id="%1$s" class="%2$s">%3$s<div class="wpnotice-content-wrapper">%4$s%5$s</div></div>',
103 'wpnotice-' . esc_attr( $this->app->app ) . '-' . esc_attr( $this->id ), // The ID.
104 esc_attr( $this->get_classes() ), // The classes.
105 ! empty( $content['thumbnail'] ) ? $this->get_thumbnail( $content['thumbnail'] ) : '',
106 ! empty( $content['html'] ) ? $content['html'] : $content,
107 ! empty( $links ) ? $this->links( $links ) : ''
108 );
109 }
110
111 public function get_links(){
112 return ! empty( $this->content['links'] ) ? $this->content['links'] : ( ! empty( $this->options['links'] ) ? $this->options['links'] : []);
113 }
114
115 // 'later' => array(
116 // 'link' => 'https://wpdeveloper.com/review-notificationx',
117 // 'target' => '_blank',
118 // 'label' => __( 'Ok, you deserve it!', 'notificationx' ),
119 // 'icon_class' => 'dashicons dashicons-external',
120 // ),
121 public function links( $links ){
122 $_attributes = '';
123 $output = '<ul style="display: flex; width: 100%; align-items: center;" class="notice-links '. $this->app->app .'-notice-links">';
124 foreach( $links as $_key => $link ) {
125 $class = ! empty( $link['class'] ) ? $link['class'] : '';
126
127 if( ! empty( $link['attributes'] ) ) {
128 $link['attributes']['target'] = '_top';
129 $_attributes = $this->attributes( $link['attributes'] );
130 $link['link'] = '#';
131 }
132
133 $output .= '<li style="margin: 0 15px 0 0;" class="notice-link-item '. $class .'">';
134 $output .= ! empty( $link['link'] ) ? '<a href="' . esc_url( $link['link'] ) . '" '. $_attributes .'>' : '';
135 if ( isset( $link['icon_class'] ) ) {
136 $output .= '<span style="margin-right: 5px" class="' . esc_attr( $link['icon_class'] ) . '"></span>';
137 }
138 $output .= $link['label'];
139 $output .= ! empty( $link['link'] ) ? '</a>' : '';
140 $output .= '</li>';
141 }
142
143 $output .= '</ul>';
144
145 return $output;
146 }
147
148 public function attributes( $params = [] ){
149 $_attr = [];
150 $classname = 'dismiss-btn ';
151
152 if( ! empty( $params['class'] ) ) {
153 $classname .= $params['class'];
154 unset( $params['class'] );
155 }
156
157 $_attr[] = 'class="' . esc_attr( $classname ) . '"';
158
159 $_attr[] = 'target="_blank"';
160 if( ! empty( $params ) ) {
161 foreach( $params as $key => $value ) {
162 $_attr[] = "$key='$value'";
163 }
164 }
165
166 return \implode(' ', $_attr);
167 }
168
169 public function url( $params = [] ){
170 $nonce = wp_create_nonce( 'wpnotice_dismiss_notice_' . $this->id );
171
172 return esc_url( add_query_arg( [
173 'action' => 'wpnotice_dismiss_notice',
174 'id' => $this->id,
175 'nonce' => $nonce,
176 ], admin_url( '/' ) ) );
177 }
178 /**
179 * Get the notice classes.
180 *
181 * @access public
182 * @since 1.0
183 * @return string
184 */
185 public function get_classes() {
186 $classes = [ 'wpnotice-wrapper notice', $this->app->app ];
187
188 // Add the class for notice-type.
189 $classes[] = $this->options['classes'];
190 $classes[] = 'notice-' . $this->options['type'];
191 $classes[] = 'notice-' . $this->app->app . '-' . $this->id;
192
193 if( $this->options['dismissible'] ) {
194 $classes[] = 'is-dismissible';
195 }
196
197 // Combine classes to a string.
198 return implode( ' ', $classes );
199 }
200
201 /**
202 * Determine if the notice should be shown or not.
203 *
204 * @access public
205 * @since 1.0
206 * @return bool
207 */
208 public function show() {
209 // External Condition Check
210 if( isset( $this->options['display_if'] ) && ! $this->options['display_if'] ) {
211 return false;
212 }
213 // Don't show if the user doesn't have the required capability.
214 if ( ! is_null( $this->options['capability'] ) && ! current_user_can( $this->options['capability'] ) ) {
215 return false;
216 }
217
218 // Don't show if we're not on the right screen.
219 if ( ! $this->is_screen() ) {
220 return false;
221 }
222
223 // Don't show if notice has been dismissed.
224 if ( $this->dismiss->is_dismissed() ) {
225 return false;
226 }
227
228 // Start and Expiration Check.
229 if( $this->time() <= $this->options['start'] ) {
230 return false;
231 }
232
233 if( $this->is_expired() ) {
234 if( $this->options['recurrence'] ) {
235 $_recurrence = intval( $this->options['recurrence'] );
236 $this->queue[ $this->id ]['start'] = $this->strtotime( "+$_recurrence days" );
237 $this->queue[ $this->id ]['expire'] = $this->strtotime( "+". ($_recurrence + 3) ." days" );
238 $this->app->storage()->save( $this->queue );
239 }
240
241 return false;
242 }
243
244 return true;
245 }
246
247 /**
248 * Evaluate if we're on the right place depending on the "screens" argument.
249 *
250 * @access private
251 * @since 1.0
252 * @return bool
253 */
254 private function is_screen() {
255 // If screen is empty we want this shown on all screens.
256 if ( empty( $this->options['screens'] ) ) {
257 return true;
258 }
259
260 // Make sure the get_current_screen function exists.
261 if ( ! function_exists( 'get_current_screen' ) ) {
262 require_once ABSPATH . 'wp-admin/includes/screen.php';
263 }
264
265 /** @var \WP_Screen $current_screen */
266 $current_screen = get_current_screen();
267 return ( in_array( $current_screen->id, $this->options['screens'], true ) );
268 }
269
270 public function is_expired(){
271 if( isset( $this->options['expire'] ) && $this->time() >= $this->options['expire'] ) {
272 return true;
273 }
274
275 return false;
276 }
277
278 public function __call( $name, $args ){
279 if( property_exists( $this, $name ) ) {
280 return $this->{$name}[ $args[0] ];
281 }
282 }
283
284 public function get_thumbnail( $image ) {
285 $output = '<div style="padding: 10px 10px 10px 0px; box-sizing: border-box;" class="wpnotice-thumbnail-wrapper">';
286 $output .= '<img style="max-width: 100%;" src="' . esc_url( $image ) . '">';
287 $output .= '</div>';
288 return wp_kses_post( $output );
289 }
290 }