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

311 lines 7.4 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 <?php
135 $this->maybe_display_notices();
136 ?>
137
138 <div class="wrap">
139 <?php
140 if ( count( $this->sections ) > 1 ) {
141 $this->display_section_nav( $active_section );
142 }
143 ?>
144
145 <form method="post" action="options.php" enctype="multipart/form-data">
146 <?php
147 // Iterate through sections to find the active section to render.
148 if ( isset( $this->sections[ $active_section ] ) ) {
149 $this->sections[ $active_section ]->render();
150 }
151 ?>
152 </form>
153
154 <p class="description">
155 <?php
156 printf(
157 'If you need help setting up the plugin please refer to the %s plugin documentation.</a>',
158 '<a href="https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin" target="_blank">'
159 );
160 ?>
161 </p>
162 </div>
163 <?php
164
165 }
166
167 /**
168 * Gets the active tab section that the user is viewing on the Plugin Settings screen.
169 *
170 * @since 1.9.6
171 *
172 * @return string Tab Name
173 */
174 private function get_active_section() {
175
176 if ( isset( $_GET['tab'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
177 return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
178 }
179
180 // First registered section will be the active section.
181 return current( $this->sections )->name;
182
183 }
184
185 /**
186 * Display notice(s) immediately after the settings screen header.
187 *
188 * @since 1.9.6
189 */
190 private function maybe_display_notices() {
191
192 $notices = array();
193
194 // Check the mbstring extension is loaded.
195 if ( ! extension_loaded( 'mbstring' ) ) {
196 $notices[] = array(
197 'type' => 'warning',
198 'message' => sprintf(
199 /* translators: link to php.net manual */
200 __( 'Notice: Your server does not support the %s function - this is required for better character encoding. Please contact your webhost to have it installed.', 'convertkit' ),
201 '<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>'
202 ),
203 );
204 }
205
206 // Bail if no notices exist.
207 if ( ! count( $notices ) ) {
208 return;
209 }
210 ?>
211 <div class="notices">
212 <?php
213 // Output inline notices.
214 foreach ( $notices as $notice ) {
215 ?>
216 <div class="inline notice notice-<?php echo esc_attr( $notice['type'] ); ?>">
217 <p>
218 <?php echo esc_attr( $notice['message'] ); ?>
219 </p>
220 </div>
221 <?php
222 }
223 ?>
224 </div>
225 <?php
226
227 }
228
229 /**
230 * Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen.
231 *
232 * @param array $links Links.
233 * @return array Links
234 */
235 public static function add_settings_page_link( $links ) {
236
237 return array_merge(
238 array(
239 'settings' => sprintf(
240 '<a href="%s">%s</a>',
241 convertkit_get_settings_link(),
242 __( 'Settings', 'convertkit' )
243 ),
244 ),
245 $links
246 );
247
248 }
249 /**
250 * Output tabs, one for each registered settings section.
251 *
252 * @param string $active_section Currently displayed/selected section.
253 */
254 public function display_section_nav( $active_section ) {
255
256 ?>
257 <ul class="convertkit-tabs">
258 <?php
259 foreach ( $this->sections as $section ) {
260 printf(
261 '<li><a href="?page=%s&tab=%s" class="convertkit-tab %s">%s</a></li>',
262 sanitize_text_field( $_REQUEST['page'] ), // phpcs:ignore WordPress.Security.NonceVerification
263 esc_html( $section->name ),
264 $active_section === $section->name ? 'convertkit-tab-active' : '',
265 esc_html( $section->tab_text )
266 );
267 }
268 ?>
269 </ul>
270 <?php
271 // WordPress' JS will automatically move any .notice elements to be immediately below .wp-header-end
272 // or <h2>, whichever comes first.
273 // As our <h2> is inside our .metabox-holder, we output .wp-header-end first to control the notification
274 // placement to be before the white background container/box.
275 ?>
276 <hr class="wp-header-end">
277 <?php
278
279 }
280
281 /**
282 * Registers settings sections at Settings > ConvertKit.
283 *
284 * Each section has its own tab.
285 *
286 * @since 1.9.6
287 */
288 public function register_sections() {
289
290 // Register the General and Tools settings sections.
291 $sections = array(
292 'general' => new ConvertKit_Settings_General(),
293 'tools' => new ConvertKit_Settings_Tools(),
294 );
295
296 /**
297 * Registers settings sections at Settings > ConvertKit.
298 *
299 * @since 1.9.6
300 *
301 * @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc.
302 */
303 $sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections );
304
305 // With our sections now registered, assign them to this class.
306 $this->sections = $sections;
307
308 }
309
310 }
311