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

341 lines 8.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 > 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 // Enqueue Select2 JS.
60 convertkit_select2_enqueue_scripts();
61
62 // Enqueue Preview Output JS.
63 wp_enqueue_script( 'convertkit-admin-preview-output', CONVERTKIT_PLUGIN_URL . 'resources/backend/js/preview-output.js', array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
64
65 /**
66 * Enqueue JavaScript for the Settings Screen at Settings > ConvertKit
67 *
68 * @since 1.9.6
69 */
70 do_action( 'convertkit_admin_settings_enqueue_scripts' );
71
72 }
73
74 /**
75 * Enqueue CSS for the Settings Screens at Settings > ConvertKit
76 *
77 * @since 1.9.6
78 *
79 * @param string $hook Hook.
80 */
81 public function enqueue_styles( $hook ) {
82
83 // Bail if we are not on the Settings screen.
84 if ( $hook !== 'settings_page_' . self::SETTINGS_PAGE_SLUG ) {
85 return;
86 }
87
88 // Enqueue Select2 CSS.
89 convertkit_select2_enqueue_styles();
90
91 // Enqueue Settings CSS.
92 wp_enqueue_style( 'convertkit-admin-settings', CONVERTKIT_PLUGIN_URL . 'resources/backend/css/settings.css', array(), CONVERTKIT_PLUGIN_VERSION );
93
94 /**
95 * Enqueue CSS for the Settings Screen at Settings > ConvertKit
96 *
97 * @since 1.9.6
98 */
99 do_action( 'convertkit_admin_settings_enqueue_styles' );
100
101 }
102
103 /**
104 * Adds the options page
105 *
106 * @since 1.9.6
107 */
108 public function add_settings_page() {
109
110 add_options_page(
111 __( 'ConvertKit', 'convertkit' ),
112 __( 'ConvertKit', 'convertkit' ),
113 'manage_options',
114 self::SETTINGS_PAGE_SLUG,
115 array( $this, 'display_settings_page' )
116 );
117
118 }
119
120 /**
121 * Outputs the settings screen.
122 *
123 * @since 1.9.6
124 */
125 public function display_settings_page() {
126
127 $active_section = $this->get_active_section();
128 ?>
129
130 <header>
131 <h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1>
132 </header>
133
134 <div class="wrap">
135 <?php
136 if ( count( $this->sections ) > 1 ) {
137 $this->display_section_nav( $active_section );
138 }
139 ?>
140
141 <form method="post" action="options.php" enctype="multipart/form-data">
142 <?php
143 // Iterate through sections to find the active section to render.
144 if ( isset( $this->sections[ $active_section ] ) ) {
145 $this->sections[ $active_section ]->render();
146 }
147 ?>
148 </form>
149
150 <p class="description">
151 <?php
152 // Output Documentation link, if it exists.
153 $documentation_url = $this->get_active_section_documentation_url( $active_section );
154 if ( $documentation_url !== false ) {
155 echo sprintf(
156 '%s <a href="%s" target="_blank">%s</a>',
157 esc_html__( 'If you need help setting up the plugin please refer to the', 'convertkit' ),
158 esc_attr( $documentation_url ),
159 esc_html__( 'plugin documentation', 'convertkit' )
160 );
161 }
162 ?>
163 </p>
164 </div>
165 <?php
166
167 }
168
169 /**
170 * Gets the active tab section that the user is viewing on the Plugin Settings screen.
171 *
172 * @since 1.9.6
173 *
174 * @return string Tab Name
175 */
176 private function get_active_section() {
177
178 if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
179 return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
180 }
181
182 // First registered section will be the active section.
183 return current( $this->sections )->name;
184
185 }
186
187 /**
188 * Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen.
189 *
190 * @param array $links Links.
191 * @return array Links
192 */
193 public function add_settings_page_link( $links ) {
194
195 // Add link to Plugin settings screen.
196 $links['settings'] = sprintf(
197 '<a href="%s">%s</a>',
198 convertkit_get_settings_link(),
199 __( 'Settings', 'convertkit' )
200 );
201
202 /**
203 * Define links to display below the Plugin Name on the WP_List_Table at Plugins > Installed Plugins.
204 *
205 * @since 2.1.2
206 *
207 * @param array $links HTML Links.
208 */
209 $links = apply_filters( 'convertkit_plugin_screen_action_links', $links );
210
211 // Return.
212 return $links;
213
214 }
215
216 /**
217 * Output tabs, one for each registered settings section.
218 *
219 * @param string $active_section Currently displayed/selected section.
220 */
221 public function display_section_nav( $active_section ) {
222
223 ?>
224 <ul class="convertkit-tabs">
225 <?php
226 foreach ( $this->sections as $section ) {
227 printf(
228 '<li><a href="?page=%s&tab=%s" class="convertkit-tab %s">%s%s</a></li>',
229 sanitize_text_field( $_REQUEST['page'] ), // phpcs:ignore WordPress.Security.NonceVerification
230 esc_html( $section->name ),
231 $active_section === $section->name ? 'convertkit-tab-active' : '',
232 esc_html( $section->tab_text ),
233 $section->is_beta ? $this->get_beta_tab() : '' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
234 );
235 }
236
237 // Output Documentation link tab, if it exists.
238 $documentation_url = $this->get_active_section_documentation_url( $active_section );
239 if ( $documentation_url !== false ) {
240 printf(
241 '<li class="convertkit-docs"><a href="%s" class="convertkit-tab" target="_blank">%s <span class="dashicons dashicons-external"></span></a></li>',
242 esc_attr( $documentation_url ),
243 esc_html__( 'Documentation', 'convertkit' )
244 );
245 }
246 ?>
247 </ul>
248 <?php
249 // WordPress' JS will automatically move any .notice elements to be immediately below .wp-header-end
250 // or <h2>, whichever comes first.
251 // As our <h2> is inside our .metabox-holder, we output .wp-header-end first to control the notification
252 // placement to be before the white background container/box.
253 ?>
254 <hr class="wp-header-end">
255 <?php
256
257 }
258
259 /**
260 * Returns a 'beta' tab wrapped in a span, using wp_kses to ensure only permitted
261 * HTML elements are included in the output.
262 *
263 * @since 2.1.0
264 *
265 * @return string
266 */
267 private function get_beta_tab() {
268
269 return wp_kses(
270 '<span class="convertkit-beta-label">' . esc_html__( 'Beta', 'convertkit' ) . '</span>',
271 array(
272 'span' => array(
273 'class' => array(),
274 ),
275 )
276 );
277
278 }
279
280 /**
281 * Registers settings sections at Settings > ConvertKit.
282 *
283 * Each section has its own tab.
284 *
285 * @since 1.9.6
286 */
287 public function register_sections() {
288
289 // Register the General and Tools settings sections.
290 $sections = array(
291 'general' => new ConvertKit_Settings_General(),
292 'tools' => new ConvertKit_Settings_Tools(),
293 );
294
295 /**
296 * Registers settings sections at Settings > ConvertKit.
297 *
298 * @since 1.9.6
299 *
300 * @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc.
301 */
302 $sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections );
303
304 // With our sections now registered, assign them to this class.
305 $this->sections = $sections;
306
307 }
308
309 /**
310 * Returns the documentation URL for the active settings section viewed by the user.
311 *
312 * @since 2.0.8
313 *
314 * @param string $active_section Currently displayed/selected section.
315 * @return bool|string
316 */
317 private function get_active_section_documentation_url( $active_section ) {
318
319 // Bail if no sections registered.
320 if ( ! $this->sections ) {
321 return false;
322 }
323
324 // Bail if the active section isn't registered.
325 if ( ! array_key_exists( $active_section, $this->sections ) ) {
326 return false;
327 }
328
329 // Pass request to section's documentation_url() function, including UTM parameters.
330 return add_query_arg(
331 array(
332 'utm_source' => 'wordpress',
333 'utm_content' => 'convertkit',
334 ),
335 $this->sections[ $active_section ]->documentation_url()
336 );
337
338 }
339
340 }
341