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

181 lines 3.6 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 ?>
66 <div class="notice notice-error">
67 <p>
68 <?php
69 // Depending on the notice, output the applicable error message.
70 switch ( $notice ) {
71 case 'authorization_failed':
72 echo sprintf(
73 '%s %s',
74 esc_html__( 'ConvertKit: Authorization failed. Please enter valid API credentials on the', 'convertkit' ),
75 sprintf(
76 '<a href="%s">%s</a>',
77 esc_url( convertkit_get_settings_link() ),
78 esc_html__( 'settings screen.', 'convertkit' )
79 )
80 );
81 break;
82 }
83 ?>
84 </p>
85 </div>
86 <?php
87 }
88
89 }
90
91 /**
92 * Add a persistent notice for output in the WordPress Administration.
93 *
94 * @since 2.0.9
95 *
96 * @param string $notice Notice name.
97 * @return bool Notice saved successfully
98 */
99 public function add( $notice ) {
100
101 // If no other persistent notices exist, add one now.
102 if ( ! $this->exist() ) {
103 return update_option( $this->key_prefix, array( $notice ) );
104 }
105
106 // Fetch existing persistent notices.
107 $notices = $this->get();
108
109 // Add notice to existing notices.
110 $notices[] = $notice;
111
112 // Remove any duplicate notices.
113 $notices = array_values( array_unique( $notices ) );
114
115 // Update and return.
116 return update_option( $this->key_prefix, $notices );
117
118 }
119
120 /**
121 * Returns all notices stored in the options table.
122 *
123 * @since 2.0.9
124 *
125 * @return array
126 */
127 public function get() {
128
129 // Fetch all notices from the options table.
130 return get_option( $this->key_prefix );
131
132 }
133
134 /**
135 * Whether any persistent notices are stored in the option table.
136 *
137 * @since 2.0.9
138 *
139 * @return bool
140 */
141 public function exist() {
142
143 if ( ! $this->get() ) {
144 return false;
145 }
146
147 return true;
148
149 }
150
151 /**
152 * Delete all persistent notices.
153 *
154 * @since 2.0.9
155 *
156 * @param string $notice Notice name.
157 * @return bool Success
158 */
159 public function delete( $notice ) {
160
161 // If no persistent notices exist, there's nothing to delete.
162 if ( ! $this->exist() ) {
163 return false;
164 }
165
166 // Fetch existing persistent notices.
167 $notices = $this->get();
168
169 // Remove notice from existing notices.
170 $index = array_search( $notice, $notices, true );
171 if ( $index !== false ) {
172 unset( $notices[ $index ] );
173 }
174
175 // Update and return.
176 return update_option( $this->key_prefix, $notices );
177
178 }
179
180 }
181