| 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__( 'ConvertKit: 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' ) ) ), |
| 73 |
esc_html__( 'connect your ConvertKit 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 echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 100 |
</p> |
| 101 |
</div> |
| 102 |
<?php |
| 103 |
} |
| 104 |
|
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Add a persistent notice for output in the WordPress Administration. |
| 109 |
* |
| 110 |
* @since 2.0.9 |
| 111 |
* |
| 112 |
* @param string $notice Notice name. |
| 113 |
* @return bool Notice saved successfully |
| 114 |
*/ |
| 115 |
public function add( $notice ) { |
| 116 |
|
| 117 |
// If no other persistent notices exist, add one now. |
| 118 |
if ( ! $this->exist() ) { |
| 119 |
return update_option( $this->key_prefix, array( $notice ) ); |
| 120 |
} |
| 121 |
|
| 122 |
// Fetch existing persistent notices. |
| 123 |
$notices = $this->get(); |
| 124 |
|
| 125 |
// Add notice to existing notices. |
| 126 |
$notices[] = $notice; |
| 127 |
|
| 128 |
// Remove any duplicate notices. |
| 129 |
$notices = array_values( array_unique( $notices ) ); |
| 130 |
|
| 131 |
// Update and return. |
| 132 |
return update_option( $this->key_prefix, $notices ); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Returns all notices stored in the options table. |
| 138 |
* |
| 139 |
* @since 2.0.9 |
| 140 |
* |
| 141 |
* @return array |
| 142 |
*/ |
| 143 |
public function get() { |
| 144 |
|
| 145 |
// Fetch all notices from the options table. |
| 146 |
return get_option( $this->key_prefix ); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Whether any persistent notices are stored in the option table. |
| 152 |
* |
| 153 |
* @since 2.0.9 |
| 154 |
* |
| 155 |
* @return bool |
| 156 |
*/ |
| 157 |
public function exist() { |
| 158 |
|
| 159 |
if ( ! $this->get() ) { |
| 160 |
return false; |
| 161 |
} |
| 162 |
|
| 163 |
return true; |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Delete all persistent notices. |
| 169 |
* |
| 170 |
* @since 2.0.9 |
| 171 |
* |
| 172 |
* @param string $notice Notice name. |
| 173 |
* @return bool Success |
| 174 |
*/ |
| 175 |
public function delete( $notice ) { |
| 176 |
|
| 177 |
// If no persistent notices exist, there's nothing to delete. |
| 178 |
if ( ! $this->exist() ) { |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
// Fetch existing persistent notices. |
| 183 |
$notices = $this->get(); |
| 184 |
|
| 185 |
// Remove notice from existing notices. |
| 186 |
$index = array_search( $notice, $notices, true ); |
| 187 |
if ( $index !== false ) { |
| 188 |
unset( $notices[ $index ] ); |
| 189 |
} |
| 190 |
|
| 191 |
// Update and return. |
| 192 |
return update_option( $this->key_prefix, $notices ); |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
} |
| 197 |
|