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 | } |