PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
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 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 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 trunk, at includes/class-convertkit-admin-notices.php

202 lines 4.1 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 $api = new ConvertKit_API_V4( CONVERTKIT_OAUTH_CLIENT_ID, CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI );
67 $output = sprintf(
68 '%s %s',
69 esc_html__( 'Kit: Authorization failed. Please', 'convertkit' ),
70 sprintf(
71 '<a href="%s">%s</a>',
72 esc_url( $api->get_oauth_url( admin_url( 'options-general.php?page=_wp_convertkit_settings' ), get_site_url() ) ),
73 esc_html__( 'connect your Kit account.', 'convertkit' )
74 )
75 );
76 break;
77
78 default:
79 $output = '';
80
81 /**
82 * Define the text to output in an admin error notice.
83 *
84 * @since 2.2.1
85 *
86 * @param string $notice Admin notice name.
87 */
88 $output = apply_filters( 'convertkit_admin_notices_output_' . $notice, $output );
89 break;
90 }
91
92 // If no output defined, skip.
93 if ( empty( $output ) ) {
94 continue;
95 }
96 ?>
97 <div class="notice notice-error">
98 <p>
99 <?php
100 echo wp_kses(
101 $output,
102 convertkit_kses_allowed_html()
103 );
104 ?>
105 </p>
106 </div>
107 <?php
108 }
109
110 }
111
112 /**
113 * Add a persistent notice for output in the WordPress Administration.
114 *
115 * @since 2.0.9
116 *
117 * @param string $notice Notice name.
118 * @return bool Notice saved successfully
119 */
120 public function add( $notice ) {
121
122 // If no other persistent notices exist, add one now.
123 if ( ! $this->exist() ) {
124 return update_option( $this->key_prefix, array( $notice ) );
125 }
126
127 // Fetch existing persistent notices.
128 $notices = $this->get();
129
130 // Add notice to existing notices.
131 $notices[] = $notice;
132
133 // Remove any duplicate notices.
134 $notices = array_values( array_unique( $notices ) );
135
136 // Update and return.
137 return update_option( $this->key_prefix, $notices );
138
139 }
140
141 /**
142 * Returns all notices stored in the options table.
143 *
144 * @since 2.0.9
145 *
146 * @return array
147 */
148 public function get() {
149
150 // Fetch all notices from the options table.
151 return get_option( $this->key_prefix );
152
153 }
154
155 /**
156 * Whether any persistent notices are stored in the option table.
157 *
158 * @since 2.0.9
159 *
160 * @return bool
161 */
162 public function exist() {
163
164 if ( ! $this->get() ) {
165 return false;
166 }
167
168 return true;
169
170 }
171
172 /**
173 * Delete all persistent notices.
174 *
175 * @since 2.0.9
176 *
177 * @param string $notice Notice name.
178 * @return bool Success
179 */
180 public function delete( $notice ) {
181
182 // If no persistent notices exist, there's nothing to delete.
183 if ( ! $this->exist() ) {
184 return false;
185 }
186
187 // Fetch existing persistent notices.
188 $notices = $this->get();
189
190 // Remove notice from existing notices.
191 $index = array_search( $notice, $notices, true );
192 if ( $index !== false ) {
193 unset( $notices[ $index ] );
194 }
195
196 // Update and return.
197 return update_option( $this->key_prefix, $notices );
198
199 }
200
201 }
202