PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.4.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.4.6
3.4.6 3.4.5 3.4.4 3.4.3 3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 All 199 releases
convertkit / includes / class-convertkit-admin-notices.php

class-convertkit-admin-notices.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.4.6, at includes/class-convertkit-admin-notices.php

201 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Admin Notices class.
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Add and remove persistent error messages across all
11 * WordPress Administration screens.
12 *
13 * @package ConvertKit
14 * @author ConvertKit
15 */
16 class ConvertKit_Admin_Notices {
17
18 /**
19 * The key prefix to use for stored notices
20 *
21 * @since 2.0.9
22 *
23 * @var string
24 */
25 private $key_prefix = 'convertkit-admin-notices';
26
27 /**
28 * Register output function to display persistent notices
29 * in the WordPress Administration, if any exist.
30 *
31 * @since 2.0.9
32 */
33 public function __construct() {
34
35 add_action( 'admin_notices', array( $this, 'output' ) );
36
37 }
38
39 /**
40 * Output persistent notices in the WordPress Administration
41 *
42 * @since 2.0.9
43 */
44 public function output() {
45
46 // Don't output if we're on a settings screen.
47 if ( convertkit_get_current_screen( 'base' ) === 'settings_page__wp_convertkit_settings' ) {
48 return;
49 }
50
51 // Don't output if we don't have the required capabilities to fix the issue.
52 if ( ! current_user_can( 'manage_options' ) ) {
53 return;
54 }
55
56 // Bail if no notices exist.
57 $notices = get_option( $this->key_prefix );
58 if ( ! $notices ) {
59 return;
60 }
61
62 // Output notices.
63 foreach ( $notices as $notice ) {
64 switch ( $notice ) {
65 case 'authorization_failed':
66 $output = sprintf(
67 '%s %s',
68 esc_html__( 'Kit: Authorization failed. Please', 'convertkit' ),
69 sprintf(
70 '<a href="%s">%s</a>',
71 esc_url( convertkit_get_oauth_url() ),
72 esc_html__( 'connect your Kit account.', 'convertkit' )
73 )
74 );
75 break;
76
77 default:
78 $output = '';
79
80 /**
81 * Define the text to output in an admin error notice.
82 *
83 * @since 2.2.1
84 *
85 * @param string $notice Admin notice name.
86 */
87 $output = apply_filters( 'convertkit_admin_notices_output_' . $notice, $output );
88 break;
89 }
90
91 // If no output defined, skip.
92 if ( empty( $output ) ) {
93 continue;
94 }
95 ?>
96 <div class="notice notice-error">
97 <p>
98 <?php
99 echo wp_kses(
100 $output,
101 convertkit_kses_allowed_html()
102 );
103 ?>
104 </p>
105 </div>
106 <?php
107 }
108
109 }
110
111 /**
112 * Add a persistent notice for output in the WordPress Administration.
113 *
114 * @since 2.0.9
115 *
116 * @param string $notice Notice name.
117 * @return bool Notice saved successfully
118 */
119 public function add( $notice ) {
120
121 // If no other persistent notices exist, add one now.
122 if ( ! $this->exist() ) {
123 return update_option( $this->key_prefix, array( $notice ) );
124 }
125
126 // Fetch existing persistent notices.
127 $notices = $this->get();
128
129 // Add notice to existing notices.
130 $notices[] = $notice;
131
132 // Remove any duplicate notices.
133 $notices = array_values( array_unique( $notices ) );
134
135 // Update and return.
136 return update_option( $this->key_prefix, $notices );
137
138 }
139
140 /**
141 * Returns all notices stored in the options table.
142 *
143 * @since 2.0.9
144 *
145 * @return array
146 */
147 public function get() {
148
149 // Fetch all notices from the options table.
150 return get_option( $this->key_prefix );
151
152 }
153
154 /**
155 * Whether any persistent notices are stored in the option table.
156 *
157 * @since 2.0.9
158 *
159 * @return bool
160 */
161 public function exist() {
162
163 if ( ! $this->get() ) {
164 return false;
165 }
166
167 return true;
168
169 }
170
171 /**
172 * Delete all persistent notices.
173 *
174 * @since 2.0.9
175 *
176 * @param string $notice Notice name.
177 * @return bool Success
178 */
179 public function delete( $notice ) {
180
181 // If no persistent notices exist, there's nothing to delete.
182 if ( ! $this->exist() ) {
183 return false;
184 }
185
186 // Fetch existing persistent notices.
187 $notices = $this->get();
188
189 // Remove notice from existing notices.
190 $index = array_search( $notice, $notices, true );
191 if ( $index !== false ) {
192 unset( $notices[ $index ] );
193 }
194
195 // Update and return.
196 return update_option( $this->key_prefix, $notices );
197
198 }
199
200 }
201