| 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 |
// Category default forms |
| 53 |
add_action( 'edit_category_form_fields', array( $this, 'category_form_fields' ), 20 ); |
| 54 |
add_action( 'edited_category', array( $this, 'save_category_fields' ), 20 ); |
| 55 |
|
| 56 |
if ( WP_DEBUG ) { |
| 57 |
add_action( 'show_user_profile', array( $this, 'add_customer_meta_fields' ) ); |
| 58 |
add_action( 'edit_user_profile', array( $this, 'add_customer_meta_fields' ) ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Add the options page |
| 64 |
*/ |
| 65 |
public function add_settings_page() { |
| 66 |
add_options_page( |
| 67 |
__( 'ConvertKit', 'convertkit' ), |
| 68 |
__( 'ConvertKit', 'convertkit' ), |
| 69 |
'manage_options', |
| 70 |
$this->settings_key, |
| 71 |
array( $this, 'display_settings_page' ) |
| 72 |
); |
| 73 |
|
| 74 |
add_action( 'admin_print_styles', array( $this, 'admin_styles' ) ); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Options page callback |
| 79 |
*/ |
| 80 |
public function display_settings_page() { |
| 81 |
if ( isset( $_GET['tab'] ) ) { // WPCS: CSRF ok. |
| 82 |
$active_section = sanitize_text_field( wp_unslash( $_GET['tab'] ) ); // WPCS: CSRF ok. |
| 83 |
} else { |
| 84 |
$active_section = $this->sections[0]->name; |
| 85 |
} |
| 86 |
|
| 87 |
?> |
| 88 |
<div class="wrap convertkit-settings-wrap"> |
| 89 |
<?php |
| 90 |
if ( count( $this->sections ) > 1 ) { |
| 91 |
$this->display_section_nav( $active_section ); |
| 92 |
} else { |
| 93 |
?> |
| 94 |
<h2><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h2> |
| 95 |
<?php |
| 96 |
} |
| 97 |
?> |
| 98 |
|
| 99 |
<form method="post" action="options.php"> |
| 100 |
<?php |
| 101 |
foreach ( $this->sections as $section ) : |
| 102 |
if ( $active_section === $section->name ) : |
| 103 |
$section->render(); |
| 104 |
endif; |
| 105 |
endforeach; |
| 106 |
|
| 107 |
// Check for Multibyte string PHP extension. |
| 108 |
if ( ! extension_loaded( 'mbstring' ) ) { |
| 109 |
?><p><strong><?php |
| 110 |
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>'; |
| 111 |
?></strong></p><?php |
| 112 |
} |
| 113 |
?><p class="description"><?php |
| 114 |
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> |
| 115 |
</form> |
| 116 |
</div> |
| 117 |
<?php |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Queue up the admin styles |
| 122 |
*/ |
| 123 |
public function admin_styles() { |
| 124 |
wp_enqueue_style( 'wp-convertkit-admin' ); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Render a tab for each section |
| 129 |
* |
| 130 |
* @param string $active_section The currently active section. |
| 131 |
*/ |
| 132 |
public function display_section_nav( $active_section ) { |
| 133 |
?> |
| 134 |
<h1><?php esc_html_e( 'ConvertKit', 'convertkit' ); ?></h1> |
| 135 |
<h2 class="nav-tab-wrapper"> |
| 136 |
<?php |
| 137 |
foreach ( $this->sections as $section ) : |
| 138 |
printf( |
| 139 |
'<a href="?page=%s&tab=%s" class="nav-tab right %s">%s</a>', |
| 140 |
esc_html( $this->settings_key ), |
| 141 |
esc_html( $section->name ), |
| 142 |
$active_section === $section->name ? 'nav-tab-active' : '', |
| 143 |
esc_html( $section->tab_text ) |
| 144 |
); |
| 145 |
endforeach; |
| 146 |
?> |
| 147 |
</h2> |
| 148 |
<?php |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Adds a section to be displayed |
| 153 |
* |
| 154 |
* @param string $section A section class name. |
| 155 |
*/ |
| 156 |
public function register_section( $section ) { |
| 157 |
$section_instance = new $section(); |
| 158 |
|
| 159 |
if ( $section_instance->is_registerable ) { |
| 160 |
array_push( $this->sections, $section_instance ); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Register each section |
| 166 |
*/ |
| 167 |
public function register_sections() { |
| 168 |
wp_register_style( 'wp-convertkit-admin', plugins_url( '../resources/backend/wp-convertkit.css', __FILE__ ) ); |
| 169 |
$this->register_section( 'ConvertKit_Settings_General' ); |
| 170 |
$this->register_section( 'ConvertKit_Settings_Wishlist' ); |
| 171 |
$this->register_section( 'ConvertKit_Settings_ContactForm7' ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Ajax callback to return formatted list of available tags |
| 176 |
* |
| 177 |
* Since 1.5.0 |
| 178 |
*/ |
| 179 |
public function get_tags() { |
| 180 |
check_ajax_referer( 'convertkit-tinymce', 'security' ); |
| 181 |
|
| 182 |
$tags = $this->api->get_resources( 'tags' ); |
| 183 |
$values = array(); |
| 184 |
foreach ( $tags as $tag ) { |
| 185 |
$values[] = array( |
| 186 |
'value' => $tag['id'], |
| 187 |
'text' => $tag['name'], |
| 188 |
); |
| 189 |
} |
| 190 |
wp_send_json( $values ); |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Add tags to the footer |
| 195 |
* |
| 196 |
* @since 1.5.0 |
| 197 |
*/ |
| 198 |
public function add_tags_footer() { |
| 199 |
// create nonce |
| 200 |
global $pagenow; |
| 201 |
if ( $pagenow !== 'admin.php' ) { |
| 202 |
$nonce = wp_create_nonce( 'convertkit-tinymce' ); |
| 203 |
?><script type="text/javascript"> |
| 204 |
jQuery( document ).ready( function( $ ) { |
| 205 |
var data = { |
| 206 |
'action' : 'convertkit_get_tags', // wp ajax action |
| 207 |
'security' : '<?php echo $nonce; ?>' // nonce value created earlier |
| 208 |
}; |
| 209 |
jQuery.post( ajaxurl, data, function( response ) { |
| 210 |
if( response === '-1' ){ |
| 211 |
console.log( 'error convertkit_get_tags' ); |
| 212 |
} else { |
| 213 |
if ( typeof( tinyMCE ) != 'undefined' ) { |
| 214 |
if (tinyMCE.activeEditor != null) { |
| 215 |
tinyMCE.activeEditor.settings.ckTags = response; |
| 216 |
console.log('added tags'); |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
}); |
| 221 |
}); |
| 222 |
</script> |
| 223 |
<?php |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* Show customer's tags |
| 229 |
* @param $user |
| 230 |
*/ |
| 231 |
public function add_customer_meta_fields( $user ) { |
| 232 |
|
| 233 |
$tags = get_user_meta( $user->ID, 'convertkit_tags', true ); |
| 234 |
?> |
| 235 |
<h2><?php esc_attr_e( 'ConvertKit Tags', 'convertkit' ) ?></h2> |
| 236 |
<table class="form-table" id="<?php echo esc_attr( 'fieldset-convertkit' ); ?>"> |
| 237 |
<?php |
| 238 |
?> |
| 239 |
<tr> |
| 240 |
<th><label for="tags"><?php esc_attr_e( 'Tags', 'convertkit' ) ?></label></th> |
| 241 |
<td><textarea id="tags" name="tags" disabled="disabled"> |
| 242 |
<?php |
| 243 |
if ( empty( $tags ) ) { |
| 244 |
esc_html_e( 'No ConvertKit Tags assigned to this user.' ,'convertkit' ); |
| 245 |
} else { |
| 246 |
$tags = json_decode( $tags ); |
| 247 |
foreach ( $tags as $key => $tag ) { |
| 248 |
echo $tag . ' (' . $key . ')' . "\n"; |
| 249 |
} |
| 250 |
} |
| 251 |
?></textarea> |
| 252 |
</td> |
| 253 |
</tr> |
| 254 |
<?php |
| 255 |
?> |
| 256 |
</table> |
| 257 |
<?php |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Display the ConvertKit forms dropdown |
| 262 |
* |
| 263 |
* @since 1.5.3 |
| 264 |
* @param WP_Term $tag |
| 265 |
*/ |
| 266 |
public function category_form_fields( $tag ) { |
| 267 |
global $convertkit_settings; |
| 268 |
|
| 269 |
$forms = $convertkit_settings->api->get_resources( 'forms' ); |
| 270 |
$default_form = get_term_meta( $tag->term_id, 'ck_default_form', true ); |
| 271 |
|
| 272 |
echo '<tr class="form-field term-description-wrap"><th scope="row"><label for="description">ConvertKit Form</label></th><td>'; |
| 273 |
|
| 274 |
// Check for error in response. |
| 275 |
if ( isset( $forms[0]['id'] ) && '-2' === $forms[0]['id'] ) { |
| 276 |
$html = '<p class="error">' . __( 'Error connecting to API. Please verify your site can connect to <code>https://api.convertkit.com</code>','convertkit' ) . '</p>'; |
| 277 |
} else { |
| 278 |
$html = '<select id="ck_default_form" name="ck_default_form">'; |
| 279 |
$html .= '<option value="default">' . __( 'None', 'convertkit' ) . '</option>'; |
| 280 |
foreach ( $forms as $form ) { |
| 281 |
$html .= sprintf( |
| 282 |
'<option value="%s" %s>%s</option>', |
| 283 |
esc_attr( $form['id'] ), |
| 284 |
selected( $default_form, $form['id'], false ), |
| 285 |
esc_html( $form['name'] ) |
| 286 |
); |
| 287 |
} |
| 288 |
$html .= '</select>'; |
| 289 |
} |
| 290 |
|
| 291 |
if ( empty( $forms ) ) { |
| 292 |
$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>'; |
| 293 |
} |
| 294 |
|
| 295 |
$html .= '<p class="description">' . __( 'This form will be automatically added to posts in this category.', 'convertkit' ) . '</p></td></tr>'; |
| 296 |
|
| 297 |
echo $html; // WPCS: XSS ok. |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
/** |
| 302 |
* Set the default ConvertKit form for |
| 303 |
* |
| 304 |
* @param int $tag_id |
| 305 |
*/ |
| 306 |
public function save_category_fields( $tag_id ) { |
| 307 |
$ck_default_form = isset( $_POST['ck_default_form'] ) ? intval( $_POST['ck_default_form'] ) : 0; |
| 308 |
if ( $ck_default_form ) { |
| 309 |
update_term_meta( $tag_id, 'ck_default_form', $ck_default_form ); |
| 310 |
} |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
} |
| 315 |
|