PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / vendor / priyomukul / wp-notice / src / Utils / CacheBank.php

CacheBank.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at vendor/priyomukul/wp-notice/src/Utils/CacheBank.php

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