| 1 |
<?php |
| 2 |
|
| 3 |
class ConvertKitSettings { |
| 4 |
public $api; |
| 5 |
public $sections = array(); |
| 6 |
|
| 7 |
public $settings_key = WP_ConvertKit::SETTINGS_PAGE_SLUG; |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
$general_options = get_option($this->settings_key); |
| 11 |
$this->api = new ConvertKitAPI($general_options['api_key']); |
| 12 |
|
| 13 |
add_action('admin_menu', array($this, 'add_settings_page')); |
| 14 |
add_action('admin_init', array($this, 'register_sections')); |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Add the options page |
| 19 |
*/ |
| 20 |
public function add_settings_page() { |
| 21 |
$settings = add_options_page( |
| 22 |
__('ConvertKit'), |
| 23 |
__('ConvertKit'), |
| 24 |
'manage_options', |
| 25 |
$this->settings_key, |
| 26 |
array($this, 'display_settings_page') |
| 27 |
); |
| 28 |
|
| 29 |
add_action('admin_print_styles', array($this, 'admin_styles')); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Options page callback |
| 34 |
*/ |
| 35 |
public function display_settings_page() { |
| 36 |
$active_section = (isset($_GET['tab'])) ? $_GET['tab'] : $this->sections[0]->name; |
| 37 |
|
| 38 |
?> |
| 39 |
<div class="wrap convertkit-settings-wrap"> |
| 40 |
<?php |
| 41 |
screen_icon(); |
| 42 |
if(count($this->sections) > 1) { |
| 43 |
$this->display_section_nav($active_section); |
| 44 |
} else { |
| 45 |
?> |
| 46 |
<h2><?php _e('ConvertKit', 'wp_convertkit'); ?></h2> |
| 47 |
<?php |
| 48 |
} |
| 49 |
?> |
| 50 |
|
| 51 |
<form method="post" action="options.php"> |
| 52 |
<?php |
| 53 |
foreach($this->sections as $section): |
| 54 |
if ($active_section == $section->name): |
| 55 |
$section->render(); |
| 56 |
endif; |
| 57 |
endforeach; |
| 58 |
?> |
| 59 |
<p class="description"> |
| 60 |
If you have questions or problems, please contact |
| 61 |
<a href="mailto:support@convertkit.com">support@convertkit.com</a> |
| 62 |
</p> |
| 63 |
</form> |
| 64 |
</div> |
| 65 |
<?php |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Queue up the admin styles |
| 70 |
*/ |
| 71 |
public function admin_styles() { |
| 72 |
wp_enqueue_style('wp-convertkit-admin'); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Render a tab for each section |
| 77 |
* |
| 78 |
* @param string $active_section The currently active section |
| 79 |
*/ |
| 80 |
public function display_section_nav($active_section) { |
| 81 |
?> |
| 82 |
<h2 class="nav-tab-wrapper"> |
| 83 |
<span class="nav-tab-title"> |
| 84 |
<?php _e('ConvertKit', 'wp_convertkit'); ?> |
| 85 |
</span> |
| 86 |
<?php |
| 87 |
foreach($this->sections as $section): |
| 88 |
printf( |
| 89 |
'<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>', |
| 90 |
$this->settings_key, |
| 91 |
$section->name, |
| 92 |
$active_section == $section->name ? 'nav-tab-active' : '', |
| 93 |
esc_html($section->tab_text) |
| 94 |
); |
| 95 |
endforeach; |
| 96 |
?> |
| 97 |
</h2> |
| 98 |
<?php |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Adds a section to be displayed |
| 103 |
* |
| 104 |
* @param string $section A section class name |
| 105 |
*/ |
| 106 |
public function register_section($section) { |
| 107 |
$section_instance = new $section(); |
| 108 |
|
| 109 |
if ($section_instance->is_registerable) { |
| 110 |
array_push($this->sections, $section_instance); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* Register each section |
| 116 |
*/ |
| 117 |
public function register_sections() { |
| 118 |
wp_register_style( 'wp-convertkit-admin', plugins_url('../resources/backend/wp-convertkit.css', __FILE__) ); |
| 119 |
|
| 120 |
$this->register_section('ConvertKitSettingsGeneral'); |
| 121 |
$this->register_section('ConvertKitSettingsWishlistMember'); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if( is_admin() ) { |
| 126 |
$convertkit_settings = new ConvertKitSettings(); |
| 127 |
|
| 128 |
include 'section/general.php'; |
| 129 |
include 'section/wishlist_member.php'; |
| 130 |
} |
| 131 |
|