PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.5.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.5.6
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 / admin / class-convertkit-admin-notices.php

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

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