PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / 1.9.0
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages v1.9.0
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-settings.php

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

345 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ConvertKit Settings class
4 *
5 * @package ConvertKit
6 * @author ConvertKit
7 */
8
9 /**
10 * Class ConvertKit_Settings
11 */
12 class ConvertKit_Settings {
13 /**
14 * ConvertKit API instance
15 *
16 * @var ConvertKit_API
17 */
18 public $api;
19
20 /**
21 * Settings sections
22 *
23 * @var array
24 */
25 public $sections = array();
26
27 /**
28 * Page slug
29 *
30 * @var string
31 */
32 public $settings_key = WP_ConvertKit::SETTINGS_PAGE_SLUG;
33
34 /**
35 * Constructor
36 */
37 public function __construct() {
38 $general_options = get_option( $this->settings_key );
39 $api_key = $general_options && array_key_exists( 'api_key', $general_options ) ? $general_options['api_key'] : null;
40 $api_secret = $general_options && array_key_exists( 'api_secret', $general_options ) ? $general_options['api_secret'] : null;
41 $debug = $general_options && array_key_exists( 'debug', $general_options ) ? $general_options['debug'] : null;
42 $this->api = new ConvertKit_API( $api_key, $api_secret, $debug );
43
44 add_action( 'admin_menu', array( $this, 'add_settings_page' ) );
45 add_action( 'admin_init', array( $this, 'register_sections' ) );
46
47 add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
48
49 // AJAX callback for TinyMCE button to get list of tags
50 add_action( 'wp_ajax_convertkit_get_tags', array( $this, 'get_tags' ) );
51 // Function to output
52 add_action( 'admin_footer', array( $this, 'add_tags_footer' ) );
53
54 // Category default forms
55 add_action( 'edit_category_form_fields', array( $this, 'category_form_fields' ), 20 );
56 add_action( 'edited_category', array( $this, 'save_category_fields' ), 20 );
57
58 if ( WP_DEBUG ) {
59 add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) );
60 add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) );
61 }
62 }
63
64 /**
65 * Enqueue Scripts in Admin
66 *
67 * @param $hook
68 */
69 public function enqueue_scripts( $hook ) {
70 if ( 'settings_page__wp_convertkit_settings' === $hook ) {
71 wp_enqueue_script( 'ck-admin-js', plugins_url( '../resources/backend/wp-convertkit.js', __FILE__ ), array( 'jquery' ), CONVERTKIT_PLUGIN_VERSION, true );
72 wp_localize_script( 'ck-admin-js', 'ck_admin', array(
73 'option_none' => __( 'None', 'convertkit' ),
74 ));
75 }
76 }
77
78 /**
79 * Add the options page
80 */
81 public function add_settings_page() {
82 add_options_page(
83 __( 'ConvertKit', 'convertkit' ),
84 __( 'ConvertKit', 'convertkit' ),
85 'manage_options',
86 $this->settings_key,
87 array( $this, 'display_settings_page' )
88 );
89
90 add_action( 'admin_print_styles', array( $this, 'admin_styles' ) );
91 }
92
93 /**
94 * Options page callback
95 */
96 public function display_settings_page() {
97 if ( isset( $_GET['tab'] ) ) { // WPCS: CSRF ok.
98 $active_section = sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // WPCS: CSRF ok.
99 } else {
100 $active_section = $this->sections[0]->name;
101 }
102 ?>
103 <div class="wrap convertkit-settings-wrap">
104 <?php
105 if ( count( $this->sections ) > 1 ) {
106 $this->display_section_nav( $active_section );
107 } else {
108 ?>
109 <h2><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h2>
110 <?php
111 }
112 ?>
113
114 <form method="post" action="options.php">
115 <?php
116 foreach ( $this->sections as $section ) :
117 if ( $active_section === $section->name ) :
118 $section->render();
119 endif;
120 endforeach;
121
122 // Check for Multibyte string PHP extension.
123 if ( ! extension_loaded( 'mbstring' ) ) {
124 ?>
125 <div class="inline notice notice-warning">
126 <p>
127 <strong>
128 <?php
129 echo sprintf( __( 'Notice: Your server does not support the %s function - this is required for better character encoding. Please contact your webhost to have it installed.',
130 'convertkit' ),
131 '<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>' );
132 ?>
133 </strong>
134 </p>
135 </div>
136 <?php
137 }
138 ?><p class="description"><?php
139 printf( 'If you need help setting up the plugin please refer to the %s plugin documentation.</a>',
140 '<a href="http://help.convertkit.com/article/99-the-convertkit-wordpress-plugin" target="_blank">' ); ?></p>
141 </form>
142 </div>
143 <?php
144 }
145
146 /**
147 * Queue up the admin styles
148 */
149 public function admin_styles() {
150 wp_enqueue_style( 'wp-convertkit-admin' );
151 }
152
153 /**
154 * Render a tab for each section
155 *
156 * @param string $active_section The currently active section.
157 */
158 public function display_section_nav( $active_section ) {
159 ?>
160 <h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1>
161 <h2 class="nav-tab-wrapper">
162 <?php
163 foreach ( $this->sections as $section ) :
164 printf(
165 '<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>',
166 esc_html( $this->settings_key ),
167 esc_html( $section->name ),
168 $active_section === $section->name ? 'nav-tab-active' : '',
169 esc_html( $section->tab_text )
170 );
171 endforeach;
172 ?>
173 </h2>
174 <?php
175 }
176
177 /**
178 * Adds a section to be displayed
179 *
180 * @param string $section A section class name.
181 */
182 public function register_section( $section ) {
183 $section_instance = new $section();
184
185 if ( $section_instance->is_registerable ) {
186 array_push( $this->sections, $section_instance );
187 }
188 }
189
190 /**
191 * Register each section
192 */
193 public function register_sections() {
194 wp_register_style( 'wp-convertkit-admin', plugins_url( '../resources/backend/wp-convertkit.css', __FILE__ ) );
195 $this->register_section( 'ConvertKit_Settings_General' );
196 $this->register_section( 'ConvertKit_Settings_Tools' );
197 $this->register_section( 'ConvertKit_Settings_Wishlist' );
198 $this->register_section( 'ConvertKit_Settings_ContactForm7' );
199 }
200
201 /**
202 * Ajax callback to return formatted list of available tags
203 *
204 * Since 1.5.0
205 */
206 public function get_tags() {
207 check_ajax_referer( 'convertkit-tinymce', 'security' );
208
209 $tags = get_option( 'convertkit_tags' );
210 $values = array();
211 if ( is_array( $tags ) ) {
212 foreach ( $tags as $tag ) {
213 $values[] = array(
214 'value' => $tag['id'],
215 'text' => $tag['name'],
216 );
217 }
218 }
219 wp_send_json( $values );
220 }
221
222 /**
223 * Add tags to the footer
224 *
225 * @since 1.5.0
226 */
227 public function add_tags_footer() {
228 // create nonce
229 global $pagenow;
230 if ( $pagenow !== 'admin.php' ) {
231 $nonce = wp_create_nonce( 'convertkit-tinymce' );
232 ?><script type="text/javascript">
233 jQuery( document ).ready( function( $ ) {
234 var data = {
235 'action' : 'convertkit_get_tags', // wp ajax action
236 'security' : '<?php echo $nonce; ?>' // nonce value created earlier
237 };
238 jQuery.post( ajaxurl, data, function( response ) {
239 if( response === '-1' ){
240 console.log( 'error convertkit_get_tags' );
241 } else {
242 if ( typeof( tinyMCE ) != 'undefined' ) {
243 if (tinyMCE.activeEditor != null) {
244 tinyMCE.activeEditor.settings.ckTags = response;
245 console.log('added tags');
246 }
247 }
248 }
249 });
250 });
251 </script>
252 <?php
253 }
254 }
255
256 /**
257 * Show customer's tags
258 * @param $user
259 */
260 public function add_customer_meta_fields( $user ) {
261
262 $tags = get_user_meta( $user->ID, 'convertkit_tags', true );
263 ?>
264 <h2><?php esc_attr_e( 'ConvertKit Tags', 'convertkit' ) ?></h2>
265 <table class="form-table" id="<?php echo esc_attr( 'fieldset-convertkit' ); ?>">
266 <?php
267 ?>
268 <tr>
269 <th><label for="tags"><?php esc_attr_e( 'Tags', 'convertkit' ) ?></label></th>
270 <td><textarea id="tags" name="tags" disabled="disabled">
271 <?php
272 if ( empty( $tags ) ) {
273 esc_html_e( 'No ConvertKit Tags assigned to this user.' ,'convertkit' );
274 } else {
275 $tags = json_decode( $tags );
276 foreach ( $tags as $key => $tag ) {
277 echo $tag . ' (' . $key . ')' . "\n";
278 }
279 }
280 ?></textarea>
281 </td>
282 </tr>
283 <?php
284 ?>
285 </table>
286 <?php
287 }
288
289 /**
290 * Display the ConvertKit forms dropdown
291 *
292 * @since 1.5.3
293 * @param WP_Term $tag
294 */
295 public function category_form_fields( $tag ) {
296
297 $forms = get_option( 'convertkit_forms' );
298 $default_form = get_term_meta( $tag->term_id, 'ck_default_form', true );
299
300 echo '<tr class="form-field"><th scope="row"><label for="description">ConvertKit Form</label></th><td>';
301
302 // Check for error in response.
303 if ( isset( $forms[0]['id'] ) && '-2' === $forms[0]['id'] ) {
304 $html = '<p class="error">' . __( 'Error connecting to API. Please verify your site can connect to <code>https://api.convertkit.com</code>','convertkit' ) . '</p>';
305 } else {
306 $html = '<select id="ck_default_form" name="ck_default_form">';
307 $html .= '<option value="default">' . __( 'None', 'convertkit' ) . '</option>';
308 foreach ( $forms as $form ) {
309 $html .= sprintf(
310 '<option value="%s" %s>%s</option>',
311 esc_attr( $form['id'] ),
312 selected( $default_form, $form['id'], false ),
313 esc_html( $form['name'] )
314 );
315 }
316 $html .= '</select>';
317 }
318
319 if ( empty( $forms ) ) {
320 $html .= '<p class="description">' . __( 'There are no forms setup in your account. You can go <a href="https://app.convertkit.com/landing_pages/new" target="_blank">here</a> to create one.', 'convertkit' ) . '</p>';
321 }
322
323 $html .= '<p class="description">' . __( 'This form will be automatically added to posts in this category.', 'convertkit' ) . '</p></td></tr>';
324
325 echo $html; // WPCS: XSS ok.
326
327 }
328
329 /**
330 * Set the default ConvertKit form for
331 *
332 * @param int $tag_id
333 */
334 public function save_category_fields( $tag_id ) {
335 $ck_default_form = isset( $_POST['ck_default_form'] ) ? intval( $_POST['ck_default_form'] ) : 0;
336 if ( $ck_default_form ) {
337 update_term_meta( $tag_id, 'ck_default_form', $ck_default_form );
338 } else {
339 update_term_meta( $tag_id, 'ck_default_form', 'default' );
340 }
341
342 }
343
344 }
345