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