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

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

256 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 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 // AJAX callback for TinyMCE button to get list of tags
48 add_action( 'wp_ajax_convertkit_get_tags', array( $this, 'get_tags' ) );
49 // Function to output
50 add_action( 'admin_footer', array( $this, 'add_tags_footer' ) );
51
52 if ( WP_DEBUG ) {
53 add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) );
54 add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) );
55 }
56 }
57
58 /**
59 * Add the options page
60 */
61 public function add_settings_page() {
62 add_options_page(
63 __( 'ConvertKit', 'convertkit' ),
64 __( 'ConvertKit', 'convertkit' ),
65 'manage_options',
66 $this->settings_key,
67 array( $this, 'display_settings_page' )
68 );
69
70 add_action( 'admin_print_styles', array( $this, 'admin_styles' ) );
71 }
72
73 /**
74 * Options page callback
75 */
76 public function display_settings_page() {
77 if ( isset( $_GET['tab'] ) ) { // WPCS: CSRF ok.
78 $active_section = sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // WPCS: CSRF ok.
79 } else {
80 $active_section = $this->sections[0]->name;
81 }
82
83 ?>
84 <div class="wrap convertkit-settings-wrap">
85 <?php
86 if ( count( $this->sections ) > 1 ) {
87 $this->display_section_nav( $active_section );
88 } else {
89 ?>
90 <h2><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h2>
91 <?php
92 }
93 ?>
94
95 <form method="post" action="options.php">
96 <?php
97 foreach ( $this->sections as $section ) :
98 if ( $active_section === $section->name ) :
99 $section->render();
100 endif;
101 endforeach;
102
103 // Check for Multibyte string PHP extension.
104 if ( ! extension_loaded( 'mbstring' ) ) {
105 ?><p><strong><?php
106 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>';
107 ?></strong></p><?php
108 }
109 ?><p class="description"><?php
110 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>
111 </form>
112 </div>
113 <?php
114 }
115
116 /**
117 * Queue up the admin styles
118 */
119 public function admin_styles() {
120 wp_enqueue_style( 'wp-convertkit-admin' );
121 }
122
123 /**
124 * Render a tab for each section
125 *
126 * @param string $active_section The currently active section.
127 */
128 public function display_section_nav( $active_section ) {
129 ?>
130 <h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1>
131 <h2 class="nav-tab-wrapper">
132 <?php
133 foreach ( $this->sections as $section ) :
134 printf(
135 '<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>',
136 esc_html( $this->settings_key ),
137 esc_html( $section->name ),
138 $active_section === $section->name ? 'nav-tab-active' : '',
139 esc_html( $section->tab_text )
140 );
141 endforeach;
142 ?>
143 </h2>
144 <?php
145 }
146
147 /**
148 * Adds a section to be displayed
149 *
150 * @param string $section A section class name.
151 */
152 public function register_section( $section ) {
153 $section_instance = new $section();
154
155 if ( $section_instance->is_registerable ) {
156 array_push( $this->sections, $section_instance );
157 }
158 }
159
160 /**
161 * Register each section
162 */
163 public function register_sections() {
164 wp_register_style( 'wp-convertkit-admin', plugins_url( '../resources/backend/wp-convertkit.css', __FILE__ ) );
165 $this->register_section( 'ConvertKit_Settings_General' );
166 $this->register_section( 'ConvertKit_Settings_Wishlist' );
167 $this->register_section( 'ConvertKit_Settings_ContactForm7' );
168 }
169
170 /**
171 * Ajax callback to return formatted list of available tags
172 *
173 * Since 1.5.0
174 */
175 public function get_tags() {
176 check_ajax_referer( 'convertkit-tinymce', 'security' );
177
178 $tags = $this->api->get_resources( 'tags' );
179 $values = array();
180 foreach ( $tags as $tag ) {
181 $values[] = array(
182 'value' => $tag['id'],
183 'text' => $tag['name'],
184 );
185 }
186 wp_send_json( $values );
187 }
188
189 /**
190 * Add tags to the footer
191 *
192 * @since 1.5.0
193 */
194 public function add_tags_footer() {
195 // create nonce
196 global $pagenow;
197 if ( $pagenow !== 'admin.php' ) {
198 $nonce = wp_create_nonce( 'convertkit-tinymce' );
199 ?><script type="text/javascript">
200 jQuery( document ).ready( function( $ ) {
201 var data = {
202 'action' : 'convertkit_get_tags', // wp ajax action
203 'security' : '<?php echo $nonce; ?>' // nonce value created earlier
204 };
205 jQuery.post( ajaxurl, data, function( response ) {
206 if( response === '-1' ){
207 console.log( 'error convertkit_get_tags' );
208 } else {
209 if ( typeof( tinyMCE ) != 'undefined' ) {
210 if (tinyMCE.activeEditor != null) {
211 tinyMCE.activeEditor.settings.ckTags = response;
212 console.log('added tags');
213 }
214 }
215 }
216 });
217 });
218 </script>
219 <?php
220 }
221 }
222
223 /**
224 * Show customer's tags
225 * @param $user
226 */
227 public function add_customer_meta_fields( $user ) {
228
229 $tags = get_user_meta( $user->ID, 'convertkit_tags', true );
230 ?>
231 <h2><?php esc_attr_e( 'ConvertKit Tags', 'convertkit' ) ?></h2>
232 <table class="form-table" id="<?php echo esc_attr( 'fieldset-convertkit' ); ?>">
233 <?php
234 ?>
235 <tr>
236 <th><label for="tags"><?php esc_attr_e( 'Tags', 'convertkit' ) ?></label></th>
237 <td><textarea id="tags" name="tags" disabled="disabled">
238 <?php
239 if ( empty( $tags ) ) {
240 esc_html_e( 'No ConvertKit Tags assigned to this user.' ,'convertkit' );
241 } else {
242 $tags = json_decode( $tags );
243 foreach ( $tags as $key => $tag ) {
244 echo $tag . ' (' . $key . ')' . "\n";
245 }
246 }
247 ?></textarea>
248 </td>
249 </tr>
250 <?php
251 ?>
252 </table>
253 <?php
254 }
255 }
256