| 1 |
<?php |
| 2 |
|
| 3 |
namespace PriyoMukul\WPNotice\Utils; |
| 4 |
|
| 5 |
use PriyoMukul\WPNotice\Notices; |
| 6 |
|
| 7 |
#[\AllowDynamicProperties] |
| 8 |
class CacheBank { |
| 9 |
use Helper; |
| 10 |
|
| 11 |
private static $instance; |
| 12 |
|
| 13 |
private static $accounts = []; |
| 14 |
|
| 15 |
private static $notices = []; |
| 16 |
|
| 17 |
private $priority_key = 'wpnotice_priority_time_expired'; |
| 18 |
|
| 19 |
public static function get_instance() { |
| 20 |
if ( self::$instance === null ) { |
| 21 |
self::$instance = new self(); |
| 22 |
} |
| 23 |
|
| 24 |
return self::$instance; |
| 25 |
} |
| 26 |
|
| 27 |
public function __construct() { |
| 28 |
add_action( 'admin_notices', [ $this, 'notices' ] ); |
| 29 |
add_action( 'admin_footer', [ $this, 'scripts' ] ); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
public function create_account( $app ) { |
| 34 |
$priority = isset( $app->options['priority'] ) ? $app->priority : count( self::$accounts ); |
| 35 |
|
| 36 |
if ( isset( $app->args['version'] ) && $app->args['version'] === '1.0.0' ) { |
| 37 |
$priority = 999 + count( self::$accounts ); |
| 38 |
} |
| 39 |
|
| 40 |
if ( isset( self::$accounts[ $priority ] ) ) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
self::$accounts[ $priority ] = $app; |
| 45 |
|
| 46 |
ksort( self::$accounts ); |
| 47 |
} |
| 48 |
|
| 49 |
public function calculate_deposits( $app ) { |
| 50 |
if ( ! $app instanceof Notices ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
foreach ( $app->notices as $id => $notice ) { |
| 55 |
$this->deposit( $app->id, $id, $notice ); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function deposit( $account, $id, $value ) { |
| 60 |
self::$notices[ $account ][ $id ] = $value; |
| 61 |
} |
| 62 |
|
| 63 |
private function get_current_account() { |
| 64 |
if ( ! empty( self::$accounts ) ) { |
| 65 |
/** |
| 66 |
* @var Notices $account |
| 67 |
*/ |
| 68 |
foreach ( self::$accounts as $account ) { |
| 69 |
$notices = $this->eligible_notices( $account->notices, $account->queue ); |
| 70 |
|
| 71 |
$notices = array_filter( $notices, function ( $notice_key ) use ( $account ) { |
| 72 |
$notice = self::$notices[ $account->id ][ $notice_key ]; |
| 73 |
|
| 74 |
return $notice->show(); |
| 75 |
} ); |
| 76 |
|
| 77 |
if ( ! empty( $notices ) ) { |
| 78 |
return $account; |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* @return Notices |
| 88 |
*/ |
| 89 |
public function get() { |
| 90 |
/** |
| 91 |
* @var Notices $current_notice ; |
| 92 |
*/ |
| 93 |
return $this->get_current_account(); |
| 94 |
} |
| 95 |
|
| 96 |
public function notices() { |
| 97 |
$notice = $this->get(); |
| 98 |
|
| 99 |
if( $notice && ! $notice->dev_mode ) { |
| 100 |
if ( get_transient( $this->priority_key ) ) { |
| 101 |
return; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ( $notice instanceof Notices ) { |
| 106 |
$notice->notices(); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* @return void |
| 112 |
*/ |
| 113 |
public function scripts() { |
| 114 |
$notice = $this->get(); |
| 115 |
|
| 116 |
if( $notice && ! $notice->dev_mode ) { |
| 117 |
if ( get_transient( $this->priority_key ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if ( $notice instanceof Notices ) { |
| 123 |
$notice->scripts(); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* This is a fallback method of Notices::eligible_notices. |
| 129 |
* Please make sure changes are done in both classes. |
| 130 |
* |
| 131 |
* @param $notices |
| 132 |
* @param $queue |
| 133 |
* |
| 134 |
* @return array |
| 135 |
*/ |
| 136 |
private function eligible_notices( $notices = [], $queue = [] ) { |
| 137 |
return $this->get_sorted_queue($notices, $queue); |
| 138 |
} |
| 139 |
|
| 140 |
public function clear_notices_in_( $screens, $app, $re_initiate = false ) { |
| 141 |
add_action( 'in_admin_header', function() use( $screens, $app, $re_initiate ) { |
| 142 |
$this->clear( $screens, $app, $re_initiate ); |
| 143 |
} ); |
| 144 |
} |
| 145 |
|
| 146 |
public function clear( $screens, $app, $re_initiate = false ) { |
| 147 |
$current_screen = get_current_screen(); |
| 148 |
|
| 149 |
if( in_array( $current_screen->id, $screens, true ) ) { |
| 150 |
remove_all_actions( 'admin_notices' ); |
| 151 |
|
| 152 |
if( $re_initiate ) { |
| 153 |
self::$accounts = []; |
| 154 |
self::$instance = new self(); |
| 155 |
self::$instance->create_account( $app ); |
| 156 |
self::$instance->calculate_deposits( $app ); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |