tab_text ) ) {
$this->tab_text = $this->title;
}
// Register the settings section.
$this->register_section();
}
/**
* Helper method to determine if we're viewing the current settings screen.
*
* @since 2.5.0
*
* @param string $tab Current settings tab (general|tools|restrict-content|broadcasts).
* @return bool
*/
public function on_settings_screen( $tab ) {
// phpcs:disable WordPress.Security.NonceVerification
// Bail if we're not on the settings screen.
if ( ! array_key_exists( 'page', $_REQUEST ) ) {
return false;
}
if ( sanitize_text_field( $_REQUEST['page'] ) !== '_wp_convertkit_settings' ) {
return false;
}
// Define current settings tab.
// General screen won't always be loaded with a `tab` parameter.
$current_tab = ( array_key_exists( 'tab', $_REQUEST ) ? sanitize_text_field( $_REQUEST['tab'] ) : 'general' );
// Return whether the request is for the current settings tab.
return ( $current_tab === $tab );
// phpcs:enable
}
/**
* Register settings section.
*/
public function register_section() {
add_settings_section(
$this->name,
$this->title,
array( $this, 'print_section_info' ),
$this->settings_key
);
$this->register_fields();
register_setting(
$this->settings_key,
$this->settings_key,
array( $this, 'sanitize_settings' )
);
}
/**
* Register fields for this section
*/
abstract public function register_fields();
/**
* Prints help info for this section
*/
abstract public function print_section_info();
/**
* Returns the URL for the ConvertKit documentation for this setting section.
*/
abstract public function documentation_url();
/**
* Outputs success and/or error notices if required.
*
* @since 2.0.0
*/
public function maybe_output_notices() {
// Define notices that might be displayed as a notification.
$notices = array();
/**
* Register success and error notices for settings screens.
*
* @since 2.5.1
*
* @param array $notices Regsitered success and error notices.
* @return array
*/
$notices = apply_filters( 'convertkit_settings_base_register_notices', $notices );
// Output the verbose error description if supplied (e.g. OAuth).
if ( isset( $_REQUEST['error_description'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$this->output_error( sanitize_text_field( $_REQUEST['error_description'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
}
// Output error notification if defined.
if ( isset( $_REQUEST['error'] ) && array_key_exists( sanitize_text_field( $_REQUEST['error'] ), $notices ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$this->output_error( $notices[ sanitize_text_field( $_REQUEST['error'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification
}
// Output success notification if defined.
if ( isset( $_REQUEST['success'] ) && array_key_exists( sanitize_text_field( $_REQUEST['success'] ), $notices ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$this->output_success( $notices[ sanitize_text_field( $_REQUEST['success'] ) ] ); // phpcs:ignore WordPress.Security.NonceVerification
}
}
/**
* Renders the section
*/
public function render() {
/**
* Performs actions prior to rendering the settings form.
*
* @since 1.9.6
*/
do_action( 'convertkit_settings_base_render_before' );
$this->render_container_start();
do_settings_sections( $this->settings_key );
settings_fields( $this->settings_key );
if ( ! $this->save_disabled ) {
submit_button();
}
$this->render_container_end();
/**
* Performs actions after rendering of the settings form.
*
* @since 1.9.6
*/
do_action( 'convertkit_settings_base_render_after' );
}
/**
* Outputs .metabox-holder and .postbox container div elements,
* used before beginning a setting screen's output.
*
* @since 2.0.0
*/
public function render_container_start() {
?>
'_wp_convertkit_settings',
'tab' => $this->name,
),
'options-general.php'
)
);
exit();
}
/**
* Redirects to the settings screen with an error notice key.
*
* The function maybe_output_notices() will then output the translated error notice
* based on the supplied key.
*
* @since 2.5.1
*
* @param string $error The error notice key, registered using `convertkit_settings_base_register_notices`.
*/
public function redirect_with_error_notice( $error ) {
wp_safe_redirect(
add_query_arg(
array(
'page' => '_wp_convertkit_settings',
'tab' => $this->name,
'error' => $error,
),
'options-general.php'
)
);
exit();
}
/**
* Redirects to the settings screen with the verbose error description.
*
* @since 2.5.1
*
* @param string $error_description The error description.
*/
public function redirect_with_error_description( $error_description ) {
wp_safe_redirect(
add_query_arg(
array(
'page' => '_wp_convertkit_settings',
'tab' => $this->name,
'error_description' => $error_description,
),
'options-general.php'
)
);
exit();
}
/**
* Redirects to the settings screen with a success notice key.
*
* The function maybe_output_notices() will then output the translated success notice
* based on the supplied key.
*
* @since 2.5.1
*
* @param string $success The success notice key, registered using `convertkit_settings_base_register_notices`.
*/
public function redirect_with_success_notice( $success ) {
wp_safe_redirect(
add_query_arg(
array(
'page' => '_wp_convertkit_settings',
'tab' => $this->name,
'success' => $success,
),
'options-general.php'
)
);
exit();
}
/**
* Outputs the given success message in an inline notice.
*
* @since 2.0.0
*
* @param string $success_message Success Message.
*/
public function output_success( $success_message ) {
?>
%s',
str_repeat( '*', strlen( $value ) - 4 ) . substr( $value, - 4 )
);
if ( $description ) {
$html .= $this->get_description( $description );
}
return $html;
}
/**
* Returns a text field.
*
* @since 1.9.6
*
* @param string $name Name.
* @param string $value Value.
* @param bool|string|array $description Description (false|string|array).
* @param bool|array $css_classes CSS Classes (false|array).
* @return string HTML Field
*/
public function get_text_field( $name, $value = '', $description = false, $css_classes = false ) {
$html = sprintf(
'',
( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ),
$name,
$this->settings_key,
$name,
$value
);
return $html . $this->get_description( $description );
}
/**
* Returns a textarea field.
*
* @since 2.3.5
*
* @param string $name Name.
* @param string $value Value.
* @param bool|string|array $description Description (false|string|array).
* @param bool|array $css_classes CSS Classes (false|array).
* @return string HTML Field
*/
public function get_textarea_field( $name, $value = '', $description = false, $css_classes = false ) {
$html = sprintf(
'',
( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ),
$name,
$this->settings_key,
$name,
$value
);
return $html . $this->get_description( $description );
}
/**
* Returns a date field.
*
* @since 2.2.8
*
* @param string $name Name.
* @param string $value Value.
* @param bool|string|array $description Description (false|string|array).
* @param bool|array $css_classes CSS Classes (false|array).
* @return string HTML Field
*/
public function get_date_field( $name, $value = '', $description = false, $css_classes = false ) {
$html = sprintf(
'',
( is_array( $css_classes ) ? implode( ' ', $css_classes ) : 'regular-text' ),
$name,
$this->settings_key,
$name,
$value
);
return $html . $this->get_description( $description );
}
/**
* Returns a select dropdown field.
*
* @since 1.9.6
*
* @param string $name Name.
* @param string $value Value.
* @param array $options Options / Choices.
* @param bool|string $description Description.
* @param bool|array $css_classes