PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 2.3.6
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v2.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 2.3.6, at admin/class-convertkit-admin-settings.php

350 lines 8.7 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 > ConvertKit 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 > ConvertKit
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 > ConvertKit
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 > ConvertKit
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 __( 'ConvertKit', 'convertkit' ),
113 __( 'ConvertKit', '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( 'ConvertKit', 'convertkit' ); ?></h1>
133 </header>
134
135 <div class="wrap">
136 <?php
137 if ( count( $this->sections ) > 1 ) {
138 $this->display_section_nav( $active_section );
139 }
140 ?>
141
142 <form method="post" action="options.php" enctype="multipart/form-data">
143 <?php
144 // Iterate through sections to find the active section to render.
145 if ( isset( $this->sections[ $active_section ] ) ) {
146 $this->sections[ $active_section ]->render();
147 }
148 ?>
149 </form>
150
151 <p class="description">
152 <?php
153 // Output Documentation link, if it exists.
154 $documentation_url = $this->get_active_section_documentation_url( $active_section );
155 if ( $documentation_url !== false ) {
156 printf(
157 '%s <a href="%s" target="_blank">%s</a>',
158 esc_html__( 'If you need help setting up the plugin please refer to the', 'convertkit' ),
159 esc_attr( $documentation_url ),
160 esc_html__( 'plugin documentation', 'convertkit' )
161 );
162 }
163 ?>
164 </p>
165 </div>
166 <?php
167
168 }
169
170 /**
171 * Gets the active tab section that the user is viewing on the Plugin Settings screen.
172 *
173 * @since 1.9.6
174 *
175 * @return string Tab Name
176 */
177 private function get_active_section() {
178
179 if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
180 return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
181 }
182
183 // First registered section will be the active section.
184 return current( $this->sections )->name;
185
186 }
187
188 /**
189 * Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen.
190 *
191 * @param array $links Links.
192 * @return array Links
193 */
194 public function add_settings_page_link( $links ) {
195
196 // Add link to Plugin settings screen.
197 $links['settings'] = sprintf(
198 '<a href="%s">%s</a>',
199 convertkit_get_settings_link(),
200 __( 'Settings', 'convertkit' )
201 );
202
203 /**
204 * Define links to display below the Plugin Name on the WP_List_Table at Plugins > Installed Plugins.
205 *
206 * @since 2.1.2
207 *
208 * @param array $links HTML Links.
209 */
210 $links = apply_filters( 'convertkit_plugin_screen_action_links', $links );
211
212 // Return.
213 return $links;
214
215 }
216
217 /**
218 * Output tabs, one for each registered settings section.
219 *
220 * @param string $active_section Currently displayed/selected section.
221 */
222 public function display_section_nav( $active_section ) {
223
224 ?>
225 <ul class="convertkit-tabs">
226 <?php
227 foreach ( $this->sections as $section ) {
228 printf(
229 '<li><a href="%s" class="convertkit-tab %s">%s%s</a></li>',
230 esc_url(
231 add_query_arg(
232 array(
233 'page' => self::SETTINGS_PAGE_SLUG,
234 'tab' => $section->name,
235 ),
236 admin_url( 'options-general.php' )
237 )
238 ),
239 ( $active_section === $section->name ? 'convertkit-tab-active' : '' ),
240 esc_html( $section->tab_text ),
241 $section->is_beta ? $this->get_beta_tab() : '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
242 );
243 }
244
245 // Output Documentation link tab, if it exists.
246 $documentation_url = $this->get_active_section_documentation_url( $active_section );
247 if ( $documentation_url !== false ) {
248 printf(
249 '<li class="convertkit-docs"><a href="%s" class="convertkit-tab" target="_blank">%s <span class="dashicons dashicons-external"></span></a></li>',
250 esc_attr( $documentation_url ),
251 esc_html__( 'Documentation', 'convertkit' )
252 );
253 }
254 ?>
255 </ul>
256 <?php
257 // WordPress' JS will automatically move any .notice elements to be immediately below .wp-header-end
258 // or <h2>, whichever comes first.
259 // As our <h2> is inside our .metabox-holder, we output .wp-header-end first to control the notification
260 // placement to be before the white background container/box.
261 ?>
262 <hr class="wp-header-end">
263 <?php
264
265 }
266
267 /**
268 * Returns a 'beta' tab wrapped in a span, using wp_kses to ensure only permitted
269 * HTML elements are included in the output.
270 *
271 * @since 2.1.0
272 *
273 * @return string
274 */
275 private function get_beta_tab() {
276
277 return wp_kses(
278 '<span class="convertkit-beta-label">' . esc_html__( 'Beta', 'convertkit' ) . '</span>',
279 array(
280 'span' => array(
281 'class' => array(),
282 ),
283 )
284 );
285
286 }
287
288 /**
289 * Registers settings sections at Settings > ConvertKit.
290 *
291 * Each section has its own tab.
292 *
293 * @since 1.9.6
294 */
295 public function register_sections() {
296
297 // Register the General and Tools settings sections.
298 $sections = array(
299 'general' => new ConvertKit_Settings_General(),
300 'tools' => new ConvertKit_Settings_Tools(),
301 );
302
303 /**
304 * Registers settings sections at Settings > ConvertKit.
305 *
306 * @since 1.9.6
307 *
308 * @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc.
309 */
310 $sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections );
311
312 // With our sections now registered, assign them to this class.
313 $this->sections = $sections;
314
315 }
316
317 /**
318 * Returns the documentation URL for the active settings section viewed by the user.
319 *
320 * @since 2.0.8
321 *
322 * @param string $active_section Currently displayed/selected section.
323 * @return bool|string
324 */
325 private function get_active_section_documentation_url( $active_section ) {
326
327 // Bail if no sections registered.
328 if ( ! $this->sections ) {
329 return false;
330 }
331
332 // Bail if the active section isn't registered.
333 if ( ! array_key_exists( $active_section, $this->sections ) ) {
334 return false;
335 }
336
337 // Pass request to section's documentation_url() function, including UTM parameters.
338 return add_query_arg(
339 array(
340 'utm_source' => 'wordpress',
341 'utm_term' => get_locale(),
342 'utm_content' => 'convertkit',
343 ),
344 $this->sections[ $active_section ]->documentation_url()
345 );
346
347 }
348
349 }
350