← All changes
|
integrations/convertkit/includes/ajax-functions.php
+185
-0
5.7.8
→
6.0.3
View file →
| @@ -1,0 +1,185 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\ConvertKit\Ajax_Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | +/** | |
| 12 | + * AJAX handler for the authorize action | |
| 13 | + * | |
| 14 | + * @since 1.0.0 | |
| 15 | + */ | |
| 16 | +function automatorwp_convertkit_ajax_authorize() { | |
| 17 | + // Security check | |
| 18 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 19 | + | |
| 20 | + // Permissions check | |
| 21 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 22 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 23 | + } | |
| 24 | + | |
| 25 | + $prefix = 'automatorwp_convertkit_'; | |
| 26 | + | |
| 27 | + $key = sanitize_text_field( $_POST['key'] ); | |
| 28 | + $secret = sanitize_text_field( $_POST['secret'] ); | |
| 29 | + | |
| 30 | + // Check parameters given | |
| 31 | + if( empty( $key ) || empty( $secret ) ) { | |
| 32 | + wp_send_json_error( array( 'message' => __( 'All fields are required to connect with ConvertKit', 'automatorwp-convertkit' ) ) ); | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + | |
| 36 | + $status_secret = automatorwp_convertkit_check_api_secret( $secret ); | |
| 37 | + $status_key = automatorwp_convertkit_check_api_key( $key ); | |
| 38 | + | |
| 39 | + if ( empty( $status_secret ) || empty ( $status_key ) ) { | |
| 40 | + return; | |
| 41 | + } | |
| 42 | + | |
| 43 | + $settings = get_option( 'automatorwp_settings' ); | |
| 44 | + | |
| 45 | + // Save API key and API secret | |
| 46 | + $settings[$prefix . 'key'] = $key; | |
| 47 | + $settings[$prefix . 'secret'] = $secret; | |
| 48 | + | |
| 49 | + // Update settings | |
| 50 | + update_option( 'automatorwp_settings', $settings ); | |
| 51 | + $admin_url = admin_url( 'admin.php?page=automatorwp_settings&tab=opt-tab-convertkit' ); | |
| 52 | + | |
| 53 | + wp_send_json_success( array( | |
| 54 | + 'message' => __( 'Correct data to connect with ConvertKit', 'automatorwp-convertkit' ), | |
| 55 | + 'redirect_url' => $admin_url | |
| 56 | + ) ); | |
| 57 | + | |
| 58 | +} | |
| 59 | +add_action( 'wp_ajax_automatorwp_convertkit_authorize', 'automatorwp_convertkit_ajax_authorize' ); | |
| 60 | + | |
| 61 | +/** | |
| 62 | + * Ajax function for selecting forms | |
| 63 | + * | |
| 64 | + * @since 1.0.0 | |
| 65 | + */ | |
| 66 | +function automatorwp_convertkit_ajax_get_forms() { | |
| 67 | + // Security check | |
| 68 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 69 | + | |
| 70 | + // Permissions check | |
| 71 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 72 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + global $wpdb; | |
| 76 | + | |
| 77 | + // Pull back the search string | |
| 78 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 79 | + | |
| 80 | + $forms = automatorwp_convertkit_get_forms( ); | |
| 81 | + | |
| 82 | + $results = array(); | |
| 83 | + | |
| 84 | + // Parse form results to match select2 results | |
| 85 | + foreach ( $forms as $form ) { | |
| 86 | + | |
| 87 | + $results[] = array( | |
| 88 | + 'id' => $form['id'], | |
| 89 | + 'text' => $form['name'] | |
| 90 | + ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + // Prepend option none | |
| 94 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 95 | + | |
| 96 | + // Return our results | |
| 97 | + wp_send_json_success( $results ); | |
| 98 | + die; | |
| 99 | + | |
| 100 | +} | |
| 101 | +add_action( 'wp_ajax_automatorwp_convertkit_get_forms', 'automatorwp_convertkit_ajax_get_forms' ); | |
| 102 | + | |
| 103 | +/** | |
| 104 | + * Ajax function for selecting sequences | |
| 105 | + * | |
| 106 | + * @since 1.0.0 | |
| 107 | + */ | |
| 108 | +function automatorwp_convertkit_ajax_get_sequences() { | |
| 109 | + // Security check | |
| 110 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 111 | + | |
| 112 | + // Permissions check | |
| 113 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 114 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 115 | + } | |
| 116 | + | |
| 117 | + global $wpdb; | |
| 118 | + | |
| 119 | + // Pull back the search string | |
| 120 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 121 | + | |
| 122 | + $sequences = automatorwp_convertkit_get_sequences( ); | |
| 123 | + | |
| 124 | + $results = array(); | |
| 125 | + | |
| 126 | + // Parse sequence results to match select2 results | |
| 127 | + foreach ( $sequences as $sequence ) { | |
| 128 | + | |
| 129 | + $results[] = array( | |
| 130 | + 'id' => $sequence['id'], | |
| 131 | + 'text' => $sequence['name'] | |
| 132 | + ); | |
| 133 | + } | |
| 134 | + | |
| 135 | + // Prepend option none | |
| 136 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 137 | + | |
| 138 | + // Return our results | |
| 139 | + wp_send_json_success( $results ); | |
| 140 | + die; | |
| 141 | + | |
| 142 | +} | |
| 143 | +add_action( 'wp_ajax_automatorwp_convertkit_get_sequences', 'automatorwp_convertkit_ajax_get_sequences' ); | |
| 144 | + | |
| 145 | +/** | |
| 146 | + * Ajax function for selecting tags | |
| 147 | + * | |
| 148 | + * @since 1.0.0 | |
| 149 | + */ | |
| 150 | +function automatorwp_convertkit_ajax_get_tags() { | |
| 151 | + // Security check | |
| 152 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 153 | + | |
| 154 | + // Permissions check | |
| 155 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 156 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 157 | + } | |
| 158 | + | |
| 159 | + global $wpdb; | |
| 160 | + | |
| 161 | + // Pull back the search string | |
| 162 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( $_REQUEST['q'] ) : ''; | |
| 163 | + | |
| 164 | + $tags = automatorwp_convertkit_get_tags( ); | |
| 165 | + | |
| 166 | + $results = array(); | |
| 167 | + | |
| 168 | + // Parse tag results to match select2 results | |
| 169 | + foreach ( $tags as $tag ) { | |
| 170 | + | |
| 171 | + $results[] = array( | |
| 172 | + 'id' => $tag['id'], | |
| 173 | + 'text' => $tag['name'] | |
| 174 | + ); | |
| 175 | + } | |
| 176 | + | |
| 177 | + // Prepend option none | |
| 178 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 179 | + | |
| 180 | + // Return our results | |
| 181 | + wp_send_json_success( $results ); | |
| 182 | + die; | |
| 183 | + | |
| 184 | +} | |
| 185 | +add_action( 'wp_ajax_automatorwp_convertkit_get_tags', 'automatorwp_convertkit_ajax_get_tags' ); | |