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

303 lines 6.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 // Enqueue Select2 JS.
60 convertkit_select2_enqueue_scripts();
61
62 /**
63 * Enqueue JavaScript for the Settings Screen at Settings > ConvertKit
64 *
65 * @since 1.9.6
66 */
67 do_action( 'convertkit_admin_settings_enqueue_scripts' );
68
69 }
70
71 /**
72 * Enqueue CSS for the Settings Screens at Settings > ConvertKit
73 *
74 * @since 1.9.6
75 *
76 * @param string $hook Hook.
77 */
78 public function enqueue_styles( $hook ) {
79
80 // Bail if we are not on the Settings screen.
81 if ( $hook !== 'settings_page_' . self::SETTINGS_PAGE_SLUG ) {
82 return;
83 }
84
85 // Enqueue Select2 CSS.
86 convertkit_select2_enqueue_styles();
87
88 // Enqueue Settings CSS.
89 wp_enqueue_style( 'convertkit-admin-settings', CONVERTKIT_PLUGIN_URL . '/resources/backend/css/settings.css', array(), CONVERTKIT_PLUGIN_VERSION );
90
91 /**
92 * Enqueue CSS for the Settings Screen at Settings > ConvertKit
93 *
94 * @since 1.9.6
95 */
96 do_action( 'convertkit_admin_settings_enqueue_styles' );
97
98 }
99
100 /**
101 * Adds the options page
102 *
103 * @since 1.9.6
104 */
105 public function add_settings_page() {
106
107 add_options_page(
108 __( 'ConvertKit', 'convertkit' ),
109 __( 'ConvertKit', 'convertkit' ),
110 'manage_options',
111 self::SETTINGS_PAGE_SLUG,
112 array( $this, 'display_settings_page' )
113 );
114
115 }
116
117 /**
118 * Outputs the settings screen.
119 *
120 * @since 1.9.6
121 */
122 public function display_settings_page() {
123
124 $active_section = $this->get_active_section();
125 ?>
126
127 <header>
128 <h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1>
129 </header>
130
131 <?php
132 $this->maybe_display_notices();
133 ?>
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">
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
150 <hr />
151
152 <p class="description">
153 <?php
154 printf(
155 'If you need help setting up the plugin please refer to the %s plugin documentation.</a>',
156 '<a href="https://help.convertkit.com/en/articles/2502591-the-convertkit-wordpress-plugin" target="_blank">'
157 );
158 ?>
159 </p>
160 </form>
161 </div>
162 <?php
163
164 }
165
166 /**
167 * Gets the active tab section that the user is viewing on the Plugin Settings screen.
168 *
169 * @since 1.9.6
170 *
171 * @return string Tab Name
172 */
173 private function get_active_section() {
174
175 if ( isset( $_GET['tab'] ) ) { // phpcs:ignore
176 return sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // phpcs:ignore
177 }
178
179 // First registered section will be the active section.
180 return current( $this->sections )->name;
181
182 }
183
184 /**
185 * Display notice(s) immediately after the settings screen header.
186 *
187 * @since 1.9.6
188 */
189 private function maybe_display_notices() {
190
191 $notices = array();
192
193 // Check the mbstring extension is loaded.
194 if ( ! extension_loaded( 'mbstring' ) ) {
195 $notices[] = array(
196 'type' => 'warning',
197 'message' => sprintf(
198 /* translators: link to php.net manual */
199 __( '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' ),
200 '<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>'
201 ),
202 );
203 }
204
205 // Bail if no notices exist.
206 if ( ! count( $notices ) ) {
207 return;
208 }
209 ?>
210 <div class="notices">
211 <?php
212 // Output inline notices.
213 foreach ( $notices as $notice ) {
214 ?>
215 <div class="inline notice notice-<?php echo esc_attr( $notice['type'] ); ?>">
216 <p>
217 <?php echo esc_attr( $notice['message'] ); ?>
218 </p>
219 </div>
220 <?php
221 }
222 ?>
223 </div>
224 <?php
225
226 }
227
228 /**
229 * Define links to display below the Plugin Name on the WP_List_Table at in the Plugins screen.
230 *
231 * @param array $links Links.
232 * @return array Links
233 */
234 public static function add_settings_page_link( $links ) {
235
236 return array_merge(
237 array(
238 'settings' => sprintf(
239 '<a href="%s">%s</a>',
240 convertkit_get_settings_link(),
241 __( 'Settings', 'convertkit' )
242 ),
243 ),
244 $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 <h2 class="nav-tab-wrapper">
257 <?php
258 foreach ( $this->sections as $section ) {
259 printf(
260 '<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>',
261 sanitize_text_field( $_REQUEST['page'] ), // phpcs:ignore
262 esc_html( $section->name ),
263 $active_section === $section->name ? 'nav-tab-active' : '',
264 esc_html( $section->tab_text )
265 );
266 }
267 ?>
268 </h2>
269 <?php
270
271 }
272
273 /**
274 * Registers settings sections at Settings > ConvertKit.
275 *
276 * Each section has its own tab.
277 *
278 * @since 1.9.6
279 */
280 public function register_sections() {
281
282 // Register the General and Tools settings sections.
283 $sections = array(
284 'general' => new ConvertKit_Settings_General(),
285 'tools' => new ConvertKit_Settings_Tools(),
286 );
287
288 /**
289 * Registers settings sections at Settings > ConvertKit.
290 *
291 * @since 1.9.6
292 *
293 * @param array $sections Array of settings classes that handle individual tabs e.g. General, Tools etc.
294 */
295 $sections = apply_filters( 'convertkit_admin_settings_register_sections', $sections );
296
297 // With our sections now registered, assign them to this class.
298 $this->sections = $sections;
299
300 }
301
302 }
303