PluginProbe
Gianism / trunk
Gianism vtrunk
4.3.0 4.3.1 4.3.2 4.3.3 4.3.4 4.4.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.1 5.2.2 5.3.0 6.0.0 6.0.1 trunk 1.0 1.1 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 All 65 releases
gianism / app / Gianism / Pattern / AbstractNotice.php

AbstractNotice.php in Gianism trunk, at app/Gianism/Pattern/AbstractNotice.php

196 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Gianism\Pattern;
4 use Gianism\Helper\Input;
5 use Gianism\Helper\Option;
6
7 /**
8 * Notice utility
9 *
10 * @package Gianism
11 * @since 3.0.4
12 * @property Input $input
13 * @property Option $option
14 */
15 abstract class AbstractNotice {
16
17 const DEPRECATED = false;
18
19 /**
20 * @var bool If duplicated, never executed.
21 */
22 public static $deprecated = false;
23
24 /**
25 * @var bool
26 */
27 private static $initialized = false;
28
29 /**
30 * @var array
31 */
32 private static $notices = [];
33
34 /**
35 * Constructor
36 */
37 final public function __construct() {
38 self::$notices[ $this->get_key() ] = $this;
39 // Register Ajax action
40 if ( ! self::$initialized ) {
41 self::$initialized = true;
42 // Register notices
43 add_action( 'wp_ajax_gianism_admin_notice', [ $this, 'admin_notice_handler' ] );
44 }
45 // Check notice and register them if exists.
46 add_action( 'admin_init', [ $this, 'register_notice' ] );
47 // Run constructor alternatives.
48 $this->init();
49 }
50
51 /**
52 * Init function
53 */
54 protected function init() {
55 // Do something on constructor.
56 }
57
58 /**
59 * Register notice if exists.
60 */
61 public function register_notice() {
62 if ( wp_doing_ajax() ) {
63 // Do nothing.
64 return;
65 }
66 if ( ! current_user_can( $this->role ) || ! $this->has_notice() ) {
67 // Setting is O.K.
68 return;
69 }
70 if ( $this->notice_dismissed() ) {
71 // Already dismissed.
72 return;
73 }
74 // Print notice
75 add_action( 'admin_notices', [ $this, 'invalid_option_notices' ] );
76 }
77
78 /**
79 * Should return key
80 *
81 * @return string
82 */
83 abstract public function get_key();
84
85 /**
86 * If this function returns true, notice will be rendered.
87 *
88 * @return bool
89 */
90 abstract protected function has_notice();
91
92 /**
93 * Should return message string.
94 *
95 * @return string
96 */
97 abstract protected function message();
98
99 /**
100 * Role or capability to display notice.
101 *
102 * @var string
103 */
104 protected $role = 'manage_options';
105
106 /**
107 * Get options
108 *
109 * @return array
110 */
111 protected function dismissed_notices() {
112 return (array) Option::get_instance()->get( 'gianism_notice_log', [] );
113 }
114
115 /**
116 * Detect if notice is dismissed.
117 *
118 * @return bool
119 */
120 protected function notice_dismissed() {
121 $key = $this->get_key();
122 $option = $this->dismissed_notices();
123 return isset( $option[ $key ] ) && $option[ $key ];
124 }
125
126 /**
127 * Update notice
128 */
129 protected function update_notice() {
130 $option = $this->dismissed_notices();
131 $option[ $this->get_key() ] = 1;
132 update_option( 'gianism_notice_log', $option );
133 }
134
135 /**
136 * Show setting error on screen.
137 */
138 public function invalid_option_notices() {
139 $endpoint = wp_nonce_url( admin_url( 'admin-ajax.php?action=gianism_admin_notice&key=' . $this->get_key() ), 'gianism_notice' );
140 ?>
141 <div class="error" style="position: relative;">
142 <p>
143 <button data-endpoint="<?php echo esc_url( $endpoint ); ?>" class="gianism-admin-notice notice-dismiss"></button>
144 <?php echo wp_kses_post( $this->message() ); ?>
145 </p>
146 </div>
147 <?php
148 }
149
150
151 /**
152 * Handle admin notice
153 */
154 final public function admin_notice_handler() {
155 try {
156 if ( ! $this->input->verify_nonce( 'gianism_notice' ) ) {
157 throw new \Exception( __( 'You have no permission.', 'wp-gianism' ), 401 );
158 }
159 $key = $this->input->get( 'key' );
160 if ( ! isset( self::$notices[ $key ] ) ) {
161 throw new \Exception( __( 'This request type is not allowed.', 'wp-gianism' ), 400 );
162 }
163 /** @var AbstractNotice $instance */
164 $instance = self::$notices[ $key ];
165 if ( $instance->notice_dismissed() ) {
166 throw new \Exception( __( 'This notice is already dismissed.', 'wp-gianism' ), 400 );
167 }
168 // Update option
169 $instance->update_notice();
170 wp_send_json_success( 'O,K.' );
171 } catch ( \Exception $e ) {
172 wp_send_json_error( $e->getMessage(), $e->getCode() );
173 }
174 }
175
176 /**
177 * Getter
178 *
179 * @param string $name
180 *
181 * @return mixed
182 */
183 public function __get( $name ) {
184 switch ( $name ) {
185 case 'input':
186 return Input::get_instance();
187 break;
188 case 'option':
189 return Option::get_instance();
190 default:
191 return null;
192 break;
193 }
194 }
195 }
196