| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers a screen at Settings > ConvertKit in the WordPress Administration |
| 11 |
* interface, and handles saving its data. |
| 12 |
* |
| 13 |
* @package ConvertKit |
| 14 |
* @author ConvertKit |
| 15 |
*/ |
| 16 |
class ConvertKit_Admin_Settings { |
| 17 |
|
| 18 |
/** |
| 19 |
* Settings sections |
| 20 |
* |
| 21 |
* @var array |
| 22 |
*/ |
| 23 |
public $sections = array(); |
| 24 |
|
| 25 |
/** |
| 26 |
* Holds the Settings Page Slug |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
const SETTINGS_PAGE_SLUG = '_wp_convertkit_settings'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Constructor |
| 34 |
*/ |
| 35 |
public function __construct() { |
| 36 |
|
| 37 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 38 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) ); |
| 39 |
add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); |
| 40 |
add_action( 'admin_init', array( $this, 'register_sections' ) ); |
| 41 |
add_filter( 'plugin_action_links_' . CONVERTKIT_PLUGIN_FILE, array( $this, 'add_settings_page_link' ) ); |
| 42 |
|
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Enqueue JavaScript in Admin |
| 47 |
* |
| 48 |
* @since 1.9.6 |
| 49 |
* |
| 50 |
* @param string $hook Hook. |
| 51 |
*/ |
| 52 |
public function enqueue_scripts( $hook ) { |
| 53 |
|
| 54 |
// Bail if we are not on the Settings screen. |
| 55 |
if ( $hook !== 'settings_page_' . self::SETTINGS_PAGE_SLUG ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// Enqueue Select2 JS. |
| 60 |
convertkit_select2_enqueue_scripts(); |
| 61 |
|
| 62 |
// Enqueue Preview Output JS. |
| 63 |
wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true ); |
| 64 |
|
| 65 |
/** |
| 66 |
* Enqueue JavaScript for the Settings Screen at Settings > ConvertKit |
| 67 |
* |
| 68 |
* @since 1.9.6 |
| 69 |
*/ |
| 70 |
do_action( 'convertkit_admin_settings_enqueue_scripts' ); |
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Enqueue CSS for the Settings Screens at Settings > ConvertKit |
| 76 |
* |
| 77 |
* @since 1.9.6 |
| 78 |
* |
| 79 |
* @param string $hook Hook. |
| 80 |
*/ |
| 81 |
public function enqueue_styles( $hook ) { |
| 82 |
|
| 83 |
// Bail if we are not on the Settings screen. |
| 84 |
if ( $hook !== 'settings_page_' . self::SETTINGS_PAGE_SLUG ) { |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
// Enqueue Select2 CSS. |
| 89 |
convertkit_select2_enqueue_styles(); |
| 90 |
|
| 91 |
// Enqueue Settings CSS. |
| 92 |
wp_enqueue_style( 'convertkit-admin-settings', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/settings.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 93 |
|
| 94 |
/** |
| 95 |
* Enqueue CSS for the Settings Screen at Settings > ConvertKit |
| 96 |
* |
| 97 |
* @since 1.9.6 |
| 98 |
*/ |
| 99 |
do_action( 'convertkit_admin_settings_enqueue_styles' ); |
| 100 |
|
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Adds the options page |
| 105 |
* |
| 106 |
* @since 1.9.6 |
| 107 |
*/ |
| 108 |
public function add_settings_page() { |
| 109 |
|
| 110 |
add_options_page( |
| 111 |
__( 'ConvertKit', 'convertkit' ), |
| 112 |
__( 'ConvertKit', 'convertkit' ), |
| 113 |
'manage_options', |
| 114 |
self::SETTINGS_PAGE_SLUG, |
| 115 |
array( $this, 'display_settings_page' ) |
| 116 |
); |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Outputs the settings screen. |
| 122 |
* |
| 123 |
* @since 1.9.6 |
| 124 |
*/ |
| 125 |
public function display_settings_page() { |
| 126 |
|
| 127 |
$active_section = $this->get_active_section(); |
| 128 |
?> |
| 129 |
|
| 130 |
<header> |
| 131 |
<h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1> |
| 132 |
</header> |
| 133 |
|
| 134 |
<?php |
| 135 |
$this->maybe_display_notices(); |
| 136 |
?> |
| 137 |
|
| 138 |
<div class="wrap"> |
| 139 |
<?php |
| 140 |
if ( count( $this->sections ) > 1 ) { |
| 141 |
$this->display_section_nav( $active_section ); |
| 142 |
} |
| 143 |
?> |
| 144 |
|
| 145 |
<form method="post" action="options.php" enctype="multipart/form-data"> |
| 146 |
<?php |
| 147 |
// Iterate through sections to find the active section to render. |
| 148 |
if ( isset( $this->sections[ $active_section ] ) ) { |
| 149 |
$this->sections[ $active_section ]->render(); |
| 150 |
} |
| 151 |
?> |
| 152 |
</form> |
| 153 |
|
| 154 |
<p class="description"> |
| 155 |
<?php |
| 156 |
// Output Documentation link, if it exists. |
| 157 |
$documentation_url = $this->get_active_section_documentation_url( $active_section ); |
| 158 |
if ( $documentation_url !== false ) { |
| 159 |
echo sprintf( |
| 160 |
'%s <a href="%s" target="_blank">%s</a>', |
| 161 |
esc_html__( 'If you need help setting up the plugin please refer to the', 'convertkit' ), |
| 162 |
esc_attr( $documentation_url ), |
| 163 |
esc_html__( 'plugin documentation', 'convertkit' ) |
| 164 |
); |
| 165 |
} |
| 166 |
?> |
| 167 |
</p> |
| 168 |
</div> |
| 169 |
<?php |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Gets the active tab section that the user is viewing on the Plugin Settings screen. |
| 175 |
* |
| 176 |
* @since 1.9.6 |
| 177 |
* |
| 178 |
* @return string Tab Name |
| 179 |
*/ |
| 180 |
private function get_active_section() { |
| 181 |
|
| 182 |
if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 183 |
return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 184 |
} |
| 185 |
|
| 186 |
// First registered section will be the active section. |
| 187 |
return current( $this->sections )->name; |
| 188 |
|
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Display notice(s) immediately after the settings screen header. |
| 193 |
* |
| 194 |
* @since 1.9.6 |
| 195 |
*/ |
| 196 |
private function maybe_display_notices() { |
| 197 |
|
| 198 |
$notices = array(); |
| 199 |
|
| 200 |
// Check the mbstring extension is loaded. |
| 201 |
if ( ! extension_loaded( 'mbstring' ) ) { |
| 202 |
$notices[] = array( |
| 203 |
'type' => 'warning', |
| 204 |
'message' => sprintf( |
| 205 |
/* translators: link to php.net manual */ |
| 206 |
__( 'Notice: Your server does not support the %s function - this is required for better character encoding. Please contact your webhost to have it installed.', 'convertkit' ), |
| 207 |
'<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>' |
| 208 |
), |
| 209 |
); |
| 210 |
} |
| 211 |
|
| 212 |
// Bail if no notices exist. |
| 213 |
if ( ! count( $notices ) ) { |
| 214 |
return; |
| 215 |
} |
| 216 |
?> |
| 217 |
<div class="notices"> |
| 218 |
<?php |
| 219 |
// Output inline notices. |
| 220 |
foreach ( $notices as $notice ) { |
| 221 |
?> |
| 222 |
<div class="inline notice notice-<?php echo esc_attr( $notice['type'] ); ?>"> |
| 223 |
<p> |
| 224 |
<?php echo esc_attr( $notice['message'] ); ?> |
| 225 |
</p> |
| 226 |
</div> |
| 227 |
<?php |
| 228 |
} |
| 229 |
?> |
| 230 |
</div> |
| 231 |
<?php |
| 232 |
|
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen. |
| 237 |
* |
| 238 |
* @param array $links Links. |
| 239 |
* @return array Links |
| 240 |
*/ |
| 241 |
public static function add_settings_page_link( $links ) { |
| 242 |
|
| 243 |
return array_merge( |
| 244 |
array( |
| 245 |
'settings' => sprintf( |
| 246 |
'<a href="%s">%s</a>', |
| 247 |
convertkit_get_settings_link(), |
| 248 |
__( 'Settings', 'convertkit' ) |
| 249 |
), |
| 250 |
), |
| 251 |
$links |
| 252 |
); |
| 253 |
|
| 254 |
} |
| 255 |
/** |
| 256 |
* Output tabs, one for each registered settings section. |
| 257 |
* |
| 258 |
* @param string $active_section Currently displayed/selected section. |
| 259 |
*/ |
| 260 |
public function display_section_nav( $active_section ) { |
| 261 |
|
| 262 |
?> |
| 263 |
<ul class="convertkit-tabs"> |
| 264 |
<?php |
| 265 |
foreach ( $this->sections as $section ) { |
| 266 |
printf( |
| 267 |
'<li><a href="?page=%s&tab=%s" class="convertkit-tab %s">%s</a></li>', |
| 268 |
sanitize_text_field( $_REQUEST['page'] ), // phpcs:ignore WordPress.Security.NonceVerification |
| 269 |
esc_html( $section->name ), |
| 270 |
$active_section === $section->name ? 'convertkit-tab-active' : '', |
| 271 |
esc_html( $section->tab_text ) |
| 272 |
); |
| 273 |
} |
| 274 |
|
| 275 |
// Output Documentation link tab, if it exists. |
| 276 |
$documentation_url = $this->get_active_section_documentation_url( $active_section ); |
| 277 |
if ( $documentation_url !== false ) { |
| 278 |
printf( |
| 279 |
'<li class="convertkit-docs"><a href="%s" class="convertkit-tab" target="_blank">%s <span class="dashicons dashicons-external"></span></a></li>', |
| 280 |
esc_attr( $documentation_url ), |
| 281 |
esc_html__( 'Documentation', 'convertkit' ) |
| 282 |
); |
| 283 |
} |
| 284 |
?> |
| 285 |
</ul> |
| 286 |
<?php |
| 287 |
// WordPress' JS will automatically move any .notice elements to be immediately below .wp-header-end |
| 288 |
// or <h2>, whichever comes first. |
| 289 |
// As our <h2> is inside our .metabox-holder, we output .wp-header-end first to control the notification |
| 290 |
// placement to be before the white background container/box. |
| 291 |
?> |
| 292 |
<hr class="wp-header-end"> |
| 293 |
<?php |
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
/** |
| 298 |
* Registers settings sections at Settings > ConvertKit. |
| 299 |
* |
| 300 |
* Each section has its own tab. |
| 301 |
* |
| 302 |
* @since 1.9.6 |
| 303 |
*/ |
| 304 |
public function register_sections() { |
| 305 |
|
| 306 |
// Register the General and Tools settings sections. |
| 307 |
$sections = array( |
| 308 |
'general' => new ConvertKit_Settings_General(), |
| 309 |
'tools' => new ConvertKit_Settings_Tools(), |
| 310 |
); |
| 311 |
|
| 312 |
/** |
| 313 |
* Registers settings sections at Settings > ConvertKit. |
| 314 |
* |
| 315 |
* @since 1.9.6 |
| 316 |
* |
| 317 |
* @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc. |
| 318 |
*/ |
| 319 |
$sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections ); |
| 320 |
|
| 321 |
// With our sections now registered, assign them to this class. |
| 322 |
$this->sections = $sections; |
| 323 |
|
| 324 |
} |
| 325 |
|
| 326 |
/** |
| 327 |
* Returns the documentation URL for the active settings section viewed by the user. |
| 328 |
* |
| 329 |
* @since 2.0.8 |
| 330 |
* |
| 331 |
* @param string $active_section Currently displayed/selected section. |
| 332 |
* @return bool|string |
| 333 |
*/ |
| 334 |
private function get_active_section_documentation_url( $active_section ) { |
| 335 |
|
| 336 |
// Bail if no sections registered. |
| 337 |
if ( ! $this->sections ) { |
| 338 |
return false; |
| 339 |
} |
| 340 |
|
| 341 |
// Bail if the active section isn't registered. |
| 342 |
if ( ! array_key_exists( $active_section, $this->sections ) ) { |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
// Pass request to section's documentation_url() function, including UTM parameters. |
| 347 |
return add_query_arg( |
| 348 |
array( |
| 349 |
'utm_source' => 'wordpress', |
| 350 |
'utm_content' => 'convertkit', |
| 351 |
), |
| 352 |
$this->sections[ $active_section ]->documentation_url() |
| 353 |
); |
| 354 |
|
| 355 |
} |
| 356 |
|
| 357 |
} |
| 358 |
|