| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Admin Settings class. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Registers a screen at Settings > Kit 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 |
// Get active settings section / tab that has been requested. |
| 60 |
$section = $this->get_active_section(); |
| 61 |
|
| 62 |
/** |
| 63 |
* Enqueue JavaScript for the Settings Screen at Settings > Kit |
| 64 |
* |
| 65 |
* @since 1.9.6 |
| 66 |
* |
| 67 |
* @param string $section Settings section / tab (general|tools|restrict-content). |
| 68 |
*/ |
| 69 |
do_action( 'convertkit_admin_settings_enqueue_scripts', $section ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Enqueue CSS for the Settings Screens at Settings > Kit |
| 75 |
* |
| 76 |
* @since 1.9.6 |
| 77 |
* |
| 78 |
* @param string $hook Hook. |
| 79 |
*/ |
| 80 |
public function enqueue_styles( $hook ) { |
| 81 |
|
| 82 |
// Bail if we are not on the Settings screen. |
| 83 |
if ( $hook !== 'settings_page_' . self::SETTINGS_PAGE_SLUG ) { |
| 84 |
return; |
| 85 |
} |
| 86 |
|
| 87 |
// Get active settings section / tab that has been requested. |
| 88 |
$section = $this->get_active_section(); |
| 89 |
|
| 90 |
// Always enqueue Settings CSS, as this is used for the UI across all settings sections. |
| 91 |
wp_enqueue_style( 'convertkit-admin-settings', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/settings.css', array(), CONVERTKIT_PLUGIN_VERSION ); |
| 92 |
|
| 93 |
/** |
| 94 |
* Enqueue CSS for the Settings Screen at Settings > Kit |
| 95 |
* |
| 96 |
* @since 1.9.6 |
| 97 |
* |
| 98 |
* @param string $section Settings section / tab (general|tools|restrict-content). |
| 99 |
*/ |
| 100 |
do_action( 'convertkit_admin_settings_enqueue_styles', $section ); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Adds the options page |
| 106 |
* |
| 107 |
* @since 1.9.6 |
| 108 |
*/ |
| 109 |
public function add_settings_page() { |
| 110 |
|
| 111 |
add_options_page( |
| 112 |
__( 'Kit', 'convertkit' ), |
| 113 |
__( 'Kit', 'convertkit' ), |
| 114 |
'manage_options', |
| 115 |
self::SETTINGS_PAGE_SLUG, |
| 116 |
array( $this, 'display_settings_page' ) |
| 117 |
); |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Outputs the settings screen. |
| 123 |
* |
| 124 |
* @since 1.9.6 |
| 125 |
*/ |
| 126 |
public function display_settings_page() { |
| 127 |
|
| 128 |
$active_section = $this->get_active_section(); |
| 129 |
?> |
| 130 |
|
| 131 |
<header> |
| 132 |
<h1><?php esc_html_e( 'Kit', 'convertkit' ); ?></h1> |
| 133 |
|
| 134 |
<?php |
| 135 |
// Output Help link tab, if it exists. |
| 136 |
$documentation_url = $this->get_active_section_documentation_url( $active_section ); |
| 137 |
if ( $documentation_url !== false ) { |
| 138 |
printf( |
| 139 |
'<a href="%s" class="convertkit-docs" target="_blank">%s</a>', |
| 140 |
esc_attr( $documentation_url ), |
| 141 |
esc_html__( 'Help', 'convertkit' ) |
| 142 |
); |
| 143 |
} |
| 144 |
?> |
| 145 |
</header> |
| 146 |
|
| 147 |
<div class="wrap"> |
| 148 |
<?php |
| 149 |
if ( count( $this->sections ) > 1 ) { |
| 150 |
$this->display_section_nav( $active_section ); |
| 151 |
} |
| 152 |
?> |
| 153 |
|
| 154 |
<form method="post" action="options.php" enctype="multipart/form-data"> |
| 155 |
<?php |
| 156 |
// Iterate through sections to find the active section to render. |
| 157 |
if ( isset( $this->sections[ $active_section ] ) ) { |
| 158 |
$this->sections[ $active_section ]->render(); |
| 159 |
} |
| 160 |
?> |
| 161 |
</form> |
| 162 |
|
| 163 |
<p class="description"> |
| 164 |
<?php |
| 165 |
// Output Help link, if it exists. |
| 166 |
$documentation_url = $this->get_active_section_documentation_url( $active_section ); |
| 167 |
if ( $documentation_url !== false ) { |
| 168 |
printf( |
| 169 |
'%s <a href="%s" target="_blank">%s</a>', |
| 170 |
esc_html__( 'If you need help setting up the plugin please refer to the', 'convertkit' ), |
| 171 |
esc_attr( $documentation_url ), |
| 172 |
esc_html__( 'plugin documentation', 'convertkit' ) |
| 173 |
); |
| 174 |
} |
| 175 |
?> |
| 176 |
</p> |
| 177 |
</div> |
| 178 |
<?php |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Gets the active tab section that the user is viewing on the Plugin Settings screen. |
| 184 |
* |
| 185 |
* @since 1.9.6 |
| 186 |
* |
| 187 |
* @return string Tab Name |
| 188 |
*/ |
| 189 |
private function get_active_section() { |
| 190 |
|
| 191 |
if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 192 |
return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 193 |
} |
| 194 |
|
| 195 |
// First registered section will be the active section. |
| 196 |
return current( $this->sections )->name; |
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen. |
| 202 |
* |
| 203 |
* @param array $links Links. |
| 204 |
* @return array Links |
| 205 |
*/ |
| 206 |
public function add_settings_page_link( $links ) { |
| 207 |
|
| 208 |
// Add link to Plugin settings screen. |
| 209 |
$links['settings'] = sprintf( |
| 210 |
'<a href="%s">%s</a>', |
| 211 |
convertkit_get_settings_link(), |
| 212 |
__( 'Settings', 'convertkit' ) |
| 213 |
); |
| 214 |
|
| 215 |
/** |
| 216 |
* Define links to display below the Plugin Name on the WP_List_Table at Plugins > Installed Plugins. |
| 217 |
* |
| 218 |
* @since 2.1.2 |
| 219 |
* |
| 220 |
* @param array $links HTML Links. |
| 221 |
*/ |
| 222 |
$links = apply_filters( 'convertkit_plugin_screen_action_links', $links ); |
| 223 |
|
| 224 |
// Return. |
| 225 |
return $links; |
| 226 |
|
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Output tabs, one for each registered settings section. |
| 231 |
* |
| 232 |
* @param string $active_section Currently displayed/selected section. |
| 233 |
*/ |
| 234 |
public function display_section_nav( $active_section ) { |
| 235 |
|
| 236 |
?> |
| 237 |
<ul class="convertkit-tabs"> |
| 238 |
<?php |
| 239 |
foreach ( $this->sections as $section ) { |
| 240 |
printf( |
| 241 |
'<li><a href="%s" class="convertkit-tab %s">%s%s</a></li>', |
| 242 |
esc_url( |
| 243 |
add_query_arg( |
| 244 |
array( |
| 245 |
'page' => self::SETTINGS_PAGE_SLUG, |
| 246 |
'tab' => $section->name, |
| 247 |
), |
| 248 |
admin_url( 'options-general.php' ) |
| 249 |
) |
| 250 |
), |
| 251 |
( $active_section === $section->name ? 'convertkit-tab-active' : '' ), |
| 252 |
esc_html( $section->tab_text ), |
| 253 |
$section->is_beta ? $this->get_beta_tab() : '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 254 |
); |
| 255 |
} |
| 256 |
?> |
| 257 |
</ul> |
| 258 |
<?php |
| 259 |
// WordPress' JS will automatically move any .notice elements to be immediately below .wp-header-end |
| 260 |
// or <h2>, whichever comes first. |
| 261 |
// As our <h2> is inside our .metabox-holder, we output .wp-header-end first to control the notification |
| 262 |
// placement to be before the white background container/box. |
| 263 |
?> |
| 264 |
<hr class="wp-header-end"> |
| 265 |
<?php |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Returns a 'beta' tab wrapped in a span, using wp_kses to ensure only permitted |
| 271 |
* HTML elements are included in the output. |
| 272 |
* |
| 273 |
* @since 2.1.0 |
| 274 |
* |
| 275 |
* @return string |
| 276 |
*/ |
| 277 |
private function get_beta_tab() { |
| 278 |
|
| 279 |
return wp_kses( |
| 280 |
'<span class="convertkit-beta-label">' . esc_html__( 'Beta', 'convertkit' ) . '</span>', |
| 281 |
array( |
| 282 |
'span' => array( |
| 283 |
'class' => array(), |
| 284 |
), |
| 285 |
) |
| 286 |
); |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Registers settings sections at Settings > Kit. |
| 292 |
* |
| 293 |
* Each section has its own tab. |
| 294 |
* |
| 295 |
* @since 1.9.6 |
| 296 |
*/ |
| 297 |
public function register_sections() { |
| 298 |
|
| 299 |
// If no Access Token exists, register a settings section that shows a button |
| 300 |
// to start the OAuth authentication flow. |
| 301 |
$settings = new ConvertKit_Settings(); |
| 302 |
if ( ! $settings->has_access_and_refresh_token() ) { |
| 303 |
// Just register the OAuth screen. |
| 304 |
$sections = array( |
| 305 |
'oauth' => new ConvertKit_Admin_Section_OAuth(), |
| 306 |
); |
| 307 |
|
| 308 |
// Assign them to this class. |
| 309 |
$this->sections = $sections; |
| 310 |
|
| 311 |
return; |
| 312 |
} |
| 313 |
|
| 314 |
// Register the General and Tools settings sections. |
| 315 |
$sections = array( |
| 316 |
'general' => new ConvertKit_Admin_Section_General(), |
| 317 |
'tools' => new ConvertKit_Admin_Section_Tools(), |
| 318 |
); |
| 319 |
|
| 320 |
/** |
| 321 |
* Registers settings sections at Settings > Kit. |
| 322 |
* |
| 323 |
* @since 1.9.6 |
| 324 |
* |
| 325 |
* @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc. |
| 326 |
*/ |
| 327 |
$sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections ); |
| 328 |
|
| 329 |
// With our sections now registered, assign them to this class. |
| 330 |
$this->sections = $sections; |
| 331 |
|
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Returns the documentation URL for the active settings section viewed by the user. |
| 336 |
* |
| 337 |
* @since 2.0.8 |
| 338 |
* |
| 339 |
* @param string $active_section Currently displayed/selected section. |
| 340 |
* @return bool|string |
| 341 |
*/ |
| 342 |
private function get_active_section_documentation_url( $active_section ) { |
| 343 |
|
| 344 |
// Bail if no sections registered. |
| 345 |
if ( ! $this->sections ) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
|
| 349 |
// Bail if the active section isn't registered. |
| 350 |
if ( ! array_key_exists( $active_section, $this->sections ) ) { |
| 351 |
return false; |
| 352 |
} |
| 353 |
|
| 354 |
// Pass request to section's documentation_url() function, including UTM parameters. |
| 355 |
return add_query_arg( |
| 356 |
array( |
| 357 |
'utm_source' => 'wordpress', |
| 358 |
'utm_term' => get_locale(), |
| 359 |
'utm_content' => 'convertkit', |
| 360 |
), |
| 361 |
$this->sections[ $active_section ]->documentation_url() |
| 362 |
); |
| 363 |
|
| 364 |
} |
| 365 |
|
| 366 |
} |
| 367 |
|