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

333 lines 9.3 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 ?>
104 <div class="wrap convertkit-settings-wrap">
105 <?php
106 if ( count( $this->sections ) > 1 ) {
107 $this->display_section_nav( $active_section );
108 } else {
109 ?>
110 <h2><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h2>
111 <?php
112 }
113 ?>
114
115 <form method="post" action="options.php">
116 <?php
117 foreach ( $this->sections as $section ) :
118 if ( $active_section === $section->name ) :
119 $section->render();
120 endif;
121 endforeach;
122
123 // Check for Multibyte string PHP extension.
124 if ( ! extension_loaded( 'mbstring' ) ) {
125 ?><p><strong><?php
126 echo sprintf( __( 'Note: Your server does not support the %s functions - this is required for better character encoding. Please contact your webhost to have it installed.', 'woocommerce' ), '<a href="https://php.net/manual/en/mbstring.installation.php">mbstring</a>' ) . '</mark>';
127 ?></strong></p><?php
128 }
129 ?><p class="description"><?php
130 printf( 'If you need help setting up the plugin please refer to the %s plugin documentation.</a>', '<a href="http://help.convertkit.com/article/99-the-convertkit-wordpress-plugin" target="_blank">' ); ?></p>
131 </form>
132 </div>
133 <?php
134 }
135
136 /**
137 * Queue up the admin styles
138 */
139 public function admin_styles() {
140 wp_enqueue_style( 'wp-convertkit-admin' );
141 }
142
143 /**
144 * Render a tab for each section
145 *
146 * @param string $active_section The currently active section.
147 */
148 public function display_section_nav( $active_section ) {
149 ?>
150 <h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1>
151 <h2 class="nav-tab-wrapper">
152 <?php
153 foreach ( $this->sections as $section ) :
154 printf(
155 '<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>',
156 esc_html( $this->settings_key ),
157 esc_html( $section->name ),
158 $active_section === $section->name ? 'nav-tab-active' : '',
159 esc_html( $section->tab_text )
160 );
161 endforeach;
162 ?>
163 </h2>
164 <?php
165 }
166
167 /**
168 * Adds a section to be displayed
169 *
170 * @param string $section A section class name.
171 */
172 public function register_section( $section ) {
173 $section_instance = new $section();
174
175 if ( $section_instance->is_registerable ) {
176 array_push( $this->sections, $section_instance );
177 }
178 }
179
180 /**
181 * Register each section
182 */
183 public function register_sections() {
184 wp_register_style( 'wp-convertkit-admin', plugins_url( '../resources/backend/wp-convertkit.css', __FILE__ ) );
185 $this->register_section( 'ConvertKit_Settings_General' );
186 $this->register_section( 'ConvertKit_Settings_Tools' );
187 $this->register_section( 'ConvertKit_Settings_Wishlist' );
188 $this->register_section( 'ConvertKit_Settings_ContactForm7' );
189 }
190
191 /**
192 * Ajax callback to return formatted list of available tags
193 *
194 * Since 1.5.0
195 */
196 public function get_tags() {
197 check_ajax_referer( 'convertkit-tinymce', 'security' );
198
199 $tags = get_option( 'convertkit_tags' );
200 $values = array();
201 foreach ( $tags as $tag ) {
202 $values[] = array(
203 'value' => $tag['id'],
204 'text' => $tag['name'],
205 );
206 }
207 wp_send_json( $values );
208 }
209
210 /**
211 * Add tags to the footer
212 *
213 * @since 1.5.0
214 */
215 public function add_tags_footer() {
216 // create nonce
217 global $pagenow;
218 if ( $pagenow !== 'admin.php' ) {
219 $nonce = wp_create_nonce( 'convertkit-tinymce' );
220 ?><script type="text/javascript">
221 jQuery( document ).ready( function( $ ) {
222 var data = {
223 'action' : 'convertkit_get_tags', // wp ajax action
224 'security' : '<?php echo $nonce; ?>' // nonce value created earlier
225 };
226 jQuery.post( ajaxurl, data, function( response ) {
227 if( response === '-1' ){
228 console.log( 'error convertkit_get_tags' );
229 } else {
230 if ( typeof( tinyMCE ) != 'undefined' ) {
231 if (tinyMCE.activeEditor != null) {
232 tinyMCE.activeEditor.settings.ckTags = response;
233 console.log('added tags');
234 }
235 }
236 }
237 });
238 });
239 </script>
240 <?php
241 }
242 }
243
244 /**
245 * Show customer's tags
246 * @param $user
247 */
248 public function add_customer_meta_fields( $user ) {
249
250 $tags = get_user_meta( $user->ID, 'convertkit_tags', true );
251 ?>
252 <h2><?php esc_attr_e( 'ConvertKit Tags', 'convertkit' ) ?></h2>
253 <table class="form-table" id="<?php echo esc_attr( 'fieldset-convertkit' ); ?>">
254 <?php
255 ?>
256 <tr>
257 <th><label for="tags"><?php esc_attr_e( 'Tags', 'convertkit' ) ?></label></th>
258 <td><textarea id="tags" name="tags" disabled="disabled">
259 <?php
260 if ( empty( $tags ) ) {
261 esc_html_e( 'No ConvertKit Tags assigned to this user.' ,'convertkit' );
262 } else {
263 $tags = json_decode( $tags );
264 foreach ( $tags as $key => $tag ) {
265 echo $tag . ' (' . $key . ')' . "\n";
266 }
267 }
268 ?></textarea>
269 </td>
270 </tr>
271 <?php
272 ?>
273 </table>
274 <?php
275 }
276
277 /**
278 * Display the ConvertKit forms dropdown
279 *
280 * @since 1.5.3
281 * @param WP_Term $tag
282 */
283 public function category_form_fields( $tag ) {
284
285 $forms = get_option( 'convertkit_forms' );
286 $default_form = get_term_meta( $tag->term_id, 'ck_default_form', true );
287
288 echo '<tr class="form-field"><th scope="row"><label for="description">ConvertKit Form</label></th><td>';
289
290 // Check for error in response.
291 if ( isset( $forms[0]['id'] ) && '-2' === $forms[0]['id'] ) {
292 $html = '<p class="error">' . __( 'Error connecting to API. Please verify your site can connect to <code>https://api.convertkit.com</code>','convertkit' ) . '</p>';
293 } else {
294 $html = '<select id="ck_default_form" name="ck_default_form">';
295 $html .= '<option value="default">' . __( 'None', 'convertkit' ) . '</option>';
296 foreach ( $forms as $form ) {
297 $html .= sprintf(
298 '<option value="%s" %s>%s</option>',
299 esc_attr( $form['id'] ),
300 selected( $default_form, $form['id'], false ),
301 esc_html( $form['name'] )
302 );
303 }
304 $html .= '</select>';
305 }
306
307 if ( empty( $forms ) ) {
308 $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>';
309 }
310
311 $html .= '<p class="description">' . __( 'This form will be automatically added to posts in this category.', 'convertkit' ) . '</p></td></tr>';
312
313 echo $html; // WPCS: XSS ok.
314
315 }
316
317 /**
318 * Set the default ConvertKit form for
319 *
320 * @param int $tag_id
321 */
322 public function save_category_fields( $tag_id ) {
323 $ck_default_form = isset( $_POST['ck_default_form'] ) ? intval( $_POST['ck_default_form'] ) : 0;
324 if ( $ck_default_form ) {
325 update_term_meta( $tag_id, 'ck_default_form', $ck_default_form );
326 } else {
327 update_term_meta( $tag_id, 'ck_default_form', 'default' );
328 }
329
330 }
331
332 }
333