| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit Settings class |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class ConvertKit_Settings |
| 11 |
*/ |
| 12 |
class ConvertKit_Settings { |
| 13 |
/** |
| 14 |
* ConvertKit API instance |
| 15 |
* |
| 16 |
* @var ConvertKit_API |
| 17 |
*/ |
| 18 |
public $api; |
| 19 |
|
| 20 |
/** |
| 21 |
* Settings sections |
| 22 |
* |
| 23 |
* @var array |
| 24 |
*/ |
| 25 |
public $sections = array(); |
| 26 |
|
| 27 |
/** |
| 28 |
* Page slug |
| 29 |
* |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
public $settings_key = WP_ConvertKit::SETTINGS_PAGE_SLUG; |
| 33 |
|
| 34 |
/** |
| 35 |
* Constructor |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
$general_options = get_option( $this->settings_key ); |
| 39 |
$api_key = $general_options && array_key_exists( 'api_key', $general_options ) ? $general_options['api_key'] : null; |
| 40 |
$api_secret = $general_options && array_key_exists( 'api_secret', $general_options ) ? $general_options['api_secret'] : null; |
| 41 |
$debug = $general_options && array_key_exists( 'debug', $general_options ) ? $general_options['debug'] : null; |
| 42 |
$this->api = new ConvertKit_API( $api_key, $api_secret, $debug ); |
| 43 |
|
| 44 |
add_action( 'admin_menu', array( $this, 'add_settings_page' ) ); |
| 45 |
add_action( 'admin_init', array( $this, 'register_sections' ) ); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Add the options page |
| 50 |
*/ |
| 51 |
public function add_settings_page() { |
| 52 |
add_options_page( |
| 53 |
__( 'ConvertKit', 'convertkit' ), |
| 54 |
__( 'ConvertKit', 'convertkit' ), |
| 55 |
'manage_options', |
| 56 |
$this->settings_key, |
| 57 |
array( $this, 'display_settings_page' ) |
| 58 |
); |
| 59 |
|
| 60 |
add_action( 'admin_print_styles', array( $this, 'admin_styles' ) ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Options page callback |
| 65 |
*/ |
| 66 |
public function display_settings_page() { |
| 67 |
if ( isset( $_GET['tab'] ) ) { // WPCS: CSRF ok. |
| 68 |
$active_section = sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // WPCS: CSRF ok. |
| 69 |
} else { |
| 70 |
$active_section = $this->sections[0]->name; |
| 71 |
} |
| 72 |
|
| 73 |
?> |
| 74 |
<div class="wrap convertkit-settings-wrap"> |
| 75 |
<?php |
| 76 |
if ( count( $this->sections ) > 1 ) { |
| 77 |
$this->display_section_nav( $active_section ); |
| 78 |
} else { |
| 79 |
?> |
| 80 |
<h2><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h2> |
| 81 |
<?php |
| 82 |
} |
| 83 |
?> |
| 84 |
|
| 85 |
<form method="post" action="options.php"> |
| 86 |
<?php |
| 87 |
foreach ( $this->sections as $section ) : |
| 88 |
if ( $active_section === $section->name ) : |
| 89 |
$section->render(); |
| 90 |
endif; |
| 91 |
endforeach; |
| 92 |
|
| 93 |
// Check for Multibyte string PHP extension. |
| 94 |
if ( ! extension_loaded( 'mbstring' ) ) { |
| 95 |
?><p><strong><?php |
| 96 |
echo sprintf( __( 'Note: Your server does not support the %s functions - this is required for better character encoding. Please contact your webhost to have it installed.', 'woocommerce' ), '<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>' ) . '</mark>'; |
| 97 |
?></strong></p><?php |
| 98 |
} |
| 99 |
?><p class="description"><?php |
| 100 |
printf( 'If you need help setting up the plugin please refer to the %s plugin documentation.</a>', '<a href="http://help.convertkit.com/article/99-the-convertkit-wordpress-plugin" target="_blank">' ); ?></p> |
| 101 |
</form> |
| 102 |
</div> |
| 103 |
<?php |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Queue up the admin styles |
| 108 |
*/ |
| 109 |
public function admin_styles() { |
| 110 |
wp_enqueue_style( 'wp-convertkit-admin' ); |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Render a tab for each section |
| 115 |
* |
| 116 |
* @param string $active_section The currently active section. |
| 117 |
*/ |
| 118 |
public function display_section_nav( $active_section ) { |
| 119 |
?> |
| 120 |
<h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1> |
| 121 |
<h2 class="nav-tab-wrapper"> |
| 122 |
<?php |
| 123 |
foreach ( $this->sections as $section ) : |
| 124 |
printf( |
| 125 |
'<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>', |
| 126 |
esc_html( $this->settings_key ), |
| 127 |
esc_html( $section->name ), |
| 128 |
$active_section === $section->name ? 'nav-tab-active' : '', |
| 129 |
esc_html( $section->tab_text ) |
| 130 |
); |
| 131 |
endforeach; |
| 132 |
?> |
| 133 |
</h2> |
| 134 |
<?php |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Adds a section to be displayed |
| 139 |
* |
| 140 |
* @param string $section A section class name. |
| 141 |
*/ |
| 142 |
public function register_section( $section ) { |
| 143 |
$section_instance = new $section(); |
| 144 |
|
| 145 |
if ( $section_instance->is_registerable ) { |
| 146 |
array_push( $this->sections, $section_instance ); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Register each section |
| 152 |
*/ |
| 153 |
public function register_sections() { |
| 154 |
wp_register_style( 'wp-convertkit-admin', plugins_url( '../resources/backend/wp-convertkit.css', __FILE__ ) ); |
| 155 |
$this->register_section( 'ConvertKit_Settings_General' ); |
| 156 |
$this->register_section( 'ConvertKit_Settings_Wishlist' ); |
| 157 |
$this->register_section( 'ConvertKit_Settings_ContactForm7' ); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
if ( is_admin() ) { |
| 162 |
$convertkit_settings = new ConvertKit_Settings(); |
| 163 |
|
| 164 |
include plugin_dir_path( __FILE__ ) . '../lib/class-multi-value-field-table.php'; |
| 165 |
include 'section/class-convertkit-settings-base.php'; |
| 166 |
include 'section/class-convertkit-settings-general.php'; |
| 167 |
include 'section/class-convertkit-settings-wishlist.php'; |
| 168 |
include 'section/class-convertkit-settings-contactform7.php'; |
| 169 |
} |
| 170 |
|