PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 3.3.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v3.3.6
3.4.2 3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 All 195 releases
convertkit / admin / class-convertkit-admin-settings.php

class-convertkit-admin-settings.php in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages 3.3.6, at admin/class-convertkit-admin-settings.php

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