| 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 |
<div class="wrap"> |
| 135 |
<?php |
| 136 |
if ( count( $this->sections ) > 1 ) { |
| 137 |
$this->display_section_nav( $active_section ); |
| 138 |
} |
| 139 |
?> |
| 140 |
|
| 141 |
<form method="post" action="options.php" enctype="multipart/form-data"> |
| 142 |
<?php |
| 143 |
// Iterate through sections to find the active section to render. |
| 144 |
if ( isset( $this->sections[ $active_section ] ) ) { |
| 145 |
$this->sections[ $active_section ]->render(); |
| 146 |
} |
| 147 |
?> |
| 148 |
</form> |
| 149 |
|
| 150 |
<p class="description"> |
| 151 |
<?php |
| 152 |
// Output Documentation link, if it exists. |
| 153 |
$documentation_url = $this->get_active_section_documentation_url( $active_section ); |
| 154 |
if ( $documentation_url !== false ) { |
| 155 |
echo sprintf( |
| 156 |
'%s <a href="%s" target="_blank">%s</a>', |
| 157 |
esc_html__( 'If you need help setting up the plugin please refer to the', 'convertkit' ), |
| 158 |
esc_attr( $documentation_url ), |
| 159 |
esc_html__( 'plugin documentation', 'convertkit' ) |
| 160 |
); |
| 161 |
} |
| 162 |
?> |
| 163 |
</p> |
| 164 |
</div> |
| 165 |
<?php |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Gets the active tab section that the user is viewing on the Plugin Settings screen. |
| 171 |
* |
| 172 |
* @since 1.9.6 |
| 173 |
* |
| 174 |
* @return string Tab Name |
| 175 |
*/ |
| 176 |
private function get_active_section() { |
| 177 |
|
| 178 |
if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification |
| 179 |
return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification |
| 180 |
} |
| 181 |
|
| 182 |
// First registered section will be the active section. |
| 183 |
return current( $this->sections )->name; |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen. |
| 189 |
* |
| 190 |
* @param array $links Links. |
| 191 |
* @return array Links |
| 192 |
*/ |
| 193 |
public function add_settings_page_link( $links ) { |
| 194 |
|
| 195 |
// Add link to Plugin settings screen. |
| 196 |
$links['settings'] = sprintf( |
| 197 |
'<a href="%s">%s</a>', |
| 198 |
convertkit_get_settings_link(), |
| 199 |
__( 'Settings', 'convertkit' ) |
| 200 |
); |
| 201 |
|
| 202 |
/** |
| 203 |
* Define links to display below the Plugin Name on the WP_List_Table at Plugins > Installed Plugins. |
| 204 |
* |
| 205 |
* @since 2.1.2 |
| 206 |
* |
| 207 |
* @param array $links HTML Links. |
| 208 |
*/ |
| 209 |
$links = apply_filters( 'convertkit_plugin_screen_action_links', $links ); |
| 210 |
|
| 211 |
// Return. |
| 212 |
return $links; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
/** |
| 217 |
* Output tabs, one for each registered settings section. |
| 218 |
* |
| 219 |
* @param string $active_section Currently displayed/selected section. |
| 220 |
*/ |
| 221 |
public function display_section_nav( $active_section ) { |
| 222 |
|
| 223 |
?> |
| 224 |
<ul class="convertkit-tabs"> |
| 225 |
<?php |
| 226 |
foreach ( $this->sections as $section ) { |
| 227 |
printf( |
| 228 |
'<li><a href="%s" class="convertkit-tab %s">%s%s</a></li>', |
| 229 |
esc_url( |
| 230 |
add_query_arg( |
| 231 |
array( |
| 232 |
'page' => self::SETTINGS_PAGE_SLUG, |
| 233 |
'tab' => $section->name, |
| 234 |
), |
| 235 |
admin_url( 'options-general.php' ) |
| 236 |
) |
| 237 |
), |
| 238 |
( $active_section === $section->name ? 'convertkit-tab-active' : '' ), |
| 239 |
esc_html( $section->tab_text ), |
| 240 |
$section->is_beta ? $this->get_beta_tab() : '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 241 |
); |
| 242 |
} |
| 243 |
|
| 244 |
// Output Documentation link tab, if it exists. |
| 245 |
$documentation_url = $this->get_active_section_documentation_url( $active_section ); |
| 246 |
if ( $documentation_url !== false ) { |
| 247 |
printf( |
| 248 |
'<li class="convertkit-docs"><a href="%s" class="convertkit-tab" target="_blank">%s <span class="dashicons dashicons-external"></span></a></li>', |
| 249 |
esc_attr( $documentation_url ), |
| 250 |
esc_html__( 'Documentation', 'convertkit' ) |
| 251 |
); |
| 252 |
} |
| 253 |
?> |
| 254 |
</ul> |
| 255 |
<?php |
| 256 |
// WordPress' JS will automatically move any .notice elements to be immediately below .wp-header-end |
| 257 |
// or <h2>, whichever comes first. |
| 258 |
// As our <h2> is inside our .metabox-holder, we output .wp-header-end first to control the notification |
| 259 |
// placement to be before the white background container/box. |
| 260 |
?> |
| 261 |
<hr class="wp-header-end"> |
| 262 |
<?php |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Returns a 'beta' tab wrapped in a span, using wp_kses to ensure only permitted |
| 268 |
* HTML elements are included in the output. |
| 269 |
* |
| 270 |
* @since 2.1.0 |
| 271 |
* |
| 272 |
* @return string |
| 273 |
*/ |
| 274 |
private function get_beta_tab() { |
| 275 |
|
| 276 |
return wp_kses( |
| 277 |
'<span class="convertkit-beta-label">' . esc_html__( 'Beta', 'convertkit' ) . '</span>', |
| 278 |
array( |
| 279 |
'span' => array( |
| 280 |
'class' => array(), |
| 281 |
), |
| 282 |
) |
| 283 |
); |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Registers settings sections at Settings > ConvertKit. |
| 289 |
* |
| 290 |
* Each section has its own tab. |
| 291 |
* |
| 292 |
* @since 1.9.6 |
| 293 |
*/ |
| 294 |
public function register_sections() { |
| 295 |
|
| 296 |
// Register the General and Tools settings sections. |
| 297 |
$sections = array( |
| 298 |
'general' => new ConvertKit_Settings_General(), |
| 299 |
'tools' => new ConvertKit_Settings_Tools(), |
| 300 |
); |
| 301 |
|
| 302 |
/** |
| 303 |
* Registers settings sections at Settings > ConvertKit. |
| 304 |
* |
| 305 |
* @since 1.9.6 |
| 306 |
* |
| 307 |
* @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc. |
| 308 |
*/ |
| 309 |
$sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections ); |
| 310 |
|
| 311 |
// With our sections now registered, assign them to this class. |
| 312 |
$this->sections = $sections; |
| 313 |
|
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Returns the documentation URL for the active settings section viewed by the user. |
| 318 |
* |
| 319 |
* @since 2.0.8 |
| 320 |
* |
| 321 |
* @param string $active_section Currently displayed/selected section. |
| 322 |
* @return bool|string |
| 323 |
*/ |
| 324 |
private function get_active_section_documentation_url( $active_section ) { |
| 325 |
|
| 326 |
// Bail if no sections registered. |
| 327 |
if ( ! $this->sections ) { |
| 328 |
return false; |
| 329 |
} |
| 330 |
|
| 331 |
// Bail if the active section isn't registered. |
| 332 |
if ( ! array_key_exists( $active_section, $this->sections ) ) { |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
// Pass request to section's documentation_url() function, including UTM parameters. |
| 337 |
return add_query_arg( |
| 338 |
array( |
| 339 |
'utm_source' => 'wordpress', |
| 340 |
'utm_term' => get_locale(), |
| 341 |
'utm_content' => 'convertkit', |
| 342 |
), |
| 343 |
$this->sections[ $active_section ]->documentation_url() |
| 344 |
); |
| 345 |
|
| 346 |
} |
| 347 |
|
| 348 |
} |
| 349 |
|