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