| 1 |
<?php |
| 2 |
|
| 3 |
namespace BetterDocs\WPNotice; |
| 4 |
|
| 5 |
use BetterDocs\WPNotice\Utils\Base; |
| 6 |
use BetterDocs\WPNotice\Utils\Helper; |
| 7 |
use BetterDocs\WPNotice\Utils\Storage; |
| 8 |
|
| 9 |
final class Notices extends Base { |
| 10 |
use Helper; |
| 11 |
|
| 12 |
public $system_id = 'wpnotice_system'; |
| 13 |
public $app = 'wpnotice'; |
| 14 |
public $version = '1.0.0'; |
| 15 |
|
| 16 |
private $args = []; |
| 17 |
|
| 18 |
private $storage = null; |
| 19 |
|
| 20 |
private $notices = []; |
| 21 |
private $queue = []; |
| 22 |
|
| 23 |
private $scripts = null; |
| 24 |
|
| 25 |
public function __get( $name ){ |
| 26 |
if( property_exists( $this, $name ) ) { |
| 27 |
return $this->$name; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public function __construct( $args ){ |
| 32 |
$this->system_id = ! empty( $args['id'] ) ? $args['id'] . '-notice-system' : $this->system_id; |
| 33 |
$this->app = ! empty( $args['id'] ) ? $args['id'] : $this->app; |
| 34 |
$this->version = ! empty( $args['version'] ) ? $args['version'] : '1.0.0'; |
| 35 |
$this->dev_mode = ! empty( $args['dev_mode'] ) ? $args['dev_mode'] : false; |
| 36 |
|
| 37 |
$this->args = $args; |
| 38 |
|
| 39 |
if( ! empty( $args['styles'] ) ) { |
| 40 |
$this->scripts = $args['styles']; |
| 41 |
unset( $args['styles'] ); |
| 42 |
} |
| 43 |
|
| 44 |
$this->queue = $this->storage()->get( '', [] ); |
| 45 |
} |
| 46 |
|
| 47 |
public function storage(){ |
| 48 |
return $this->database( $this->args ); |
| 49 |
} |
| 50 |
|
| 51 |
public function init(){ |
| 52 |
add_action( 'admin_notices', [ $this, 'notices' ] ); |
| 53 |
add_action( 'admin_footer', [ $this, 'scripts' ] ); |
| 54 |
} |
| 55 |
|
| 56 |
public function notices(){ |
| 57 |
wp_enqueue_style( $this->system_id, $this->scripts ); |
| 58 |
|
| 59 |
if( ! $this->dev_mode ) { |
| 60 |
$current_notice = current( $this->eligible_notices() ); |
| 61 |
if( isset( $this->notices[ $current_notice ] ) ) { |
| 62 |
$this->notices[ $current_notice ]->display(); |
| 63 |
} |
| 64 |
} else { |
| 65 |
foreach( $this->notices as $key => $notice ) { |
| 66 |
$notice->display( true ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
protected function eligible_notices(){ |
| 73 |
$_sorted_quque = []; |
| 74 |
$_queue = empty( $this->queue ) ? $this->notices : $this->queue; |
| 75 |
|
| 76 |
if( ! empty ( $_queue ) ) { |
| 77 |
array_walk( $_queue, function( $value, $key ) use( &$_sorted_quque ) { |
| 78 |
$notice = isset( $this->notices[ $key ] ) ? $this->notices[ $key ] : null; |
| 79 |
if( ! is_null( $notice ) ) { |
| 80 |
if( ! $notice->dismiss->is_dismissed() && ! $notice->is_expired() ) { |
| 81 |
$_sorted_quque[ $notice->options('start') ] = $key; |
| 82 |
} |
| 83 |
} |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
ksort( $_sorted_quque ); |
| 88 |
|
| 89 |
return $_sorted_quque; |
| 90 |
} |
| 91 |
|
| 92 |
public function scripts(){ |
| 93 |
$current_notice = current( $this->eligible_notices() ); |
| 94 |
|
| 95 |
if( isset( $this->notices[ $current_notice ] ) && ! $this->dev_mode ) { |
| 96 |
$notice = $this->notices[ $current_notice ]; |
| 97 |
if( $notice->show() ) { |
| 98 |
$notice->dismiss->print_script(); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if( $this->dev_mode ) { |
| 103 |
foreach( $this->notices as $key => $notice ) { |
| 104 |
$notice->dismiss->print_script(); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
public function add( $id, $content, $options = [] ){ |
| 111 |
$this->notices[ $id ] = new Notice( $id, $content, $options, $this->queue, $this ); |
| 112 |
} |
| 113 |
} |