| @@ -1,0 +1,158 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\Integrations\AWeber\Ajax_Functions | |
| 6 | + * @since 1.0.0 | |
| 7 | + */ | |
| 8 | +// Exit if accessed directly | |
| 9 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 10 | + | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * AJAX handler for the authorize action | |
| 14 | + * | |
| 15 | + * @since 1.0.0 | |
| 16 | + */ | |
| 17 | +function automatorwp_aweber_ajax_authorize() { | |
| 18 | + // Security check | |
| 19 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 20 | + | |
| 21 | + // Permissions check | |
| 22 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 23 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 24 | + } | |
| 25 | + | |
| 26 | + $prefix = 'automatorwp_aweber_'; | |
| 27 | + | |
| 28 | + $client_id = sanitize_text_field( $_POST['client_id'] ); | |
| 29 | + $client_secret = sanitize_text_field( $_POST['client_secret'] ); | |
| 30 | + | |
| 31 | + // Check parameters given | |
| 32 | + if( empty( $client_id ) || empty( $client_secret ) ) { | |
| 33 | + wp_send_json_error( array( 'message' => __( 'All fields are required to connect with AWeber', 'automatorwp' ) ) ); | |
| 34 | + } | |
| 35 | + | |
| 36 | + $settings = get_option( 'automatorwp_settings' ); | |
| 37 | + | |
| 38 | + // Save client id and secret | |
| 39 | + $settings[$prefix . 'client_id'] = $client_id; | |
| 40 | + $settings[$prefix . 'client_secret'] = $client_secret; | |
| 41 | + | |
| 42 | + | |
| 43 | + // Update settings | |
| 44 | + update_option( 'automatorwp_settings', $settings ); | |
| 45 | + | |
| 46 | + // Scopes to read and write | |
| 47 | + $scope = 'account.read list.read list.write subscriber.read subscriber.write'; | |
| 48 | + | |
| 49 | + $admin_url = admin_url('admin.php?page=automatorwp_settings&tab=opt-tab-aweber'); | |
| 50 | + $redirect_url = 'https://auth.aweber.com/oauth2/authorize?response_type=code&client_id=' . $client_id . '&redirect_uri=' . urlencode( $admin_url ) . '&scope=' . urlencode( $scope ); | |
| 51 | + | |
| 52 | + // Return the redirect URL | |
| 53 | + wp_send_json_success( array( | |
| 54 | + 'message' => __( 'Settings saved successfully, redirecting to AWeber...', 'automatorwp' ), | |
| 55 | + 'redirect_url' => $redirect_url | |
| 56 | + ) ); | |
| 57 | + | |
| 58 | +} | |
| 59 | +add_action( 'wp_ajax_automatorwp_aweber_authorize', 'automatorwp_aweber_ajax_authorize' ); | |
| 60 | + | |
| 61 | +/** | |
| 62 | + * Ajax function for selecting accounts | |
| 63 | + * | |
| 64 | + * @since 1.0.0 | |
| 65 | + */ | |
| 66 | +function automatorwp_aweber_ajax_get_accounts() { | |
| 67 | + // Security check, forces to die if not security passed | |
| 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( sanitize_text_field( $_REQUEST['q'] ) ) : ''; | |
| 79 | + | |
| 80 | + $accounts = automatorwp_aweber_get_accounts(); | |
| 81 | + | |
| 82 | + $results = array(); | |
| 83 | + | |
| 84 | + // Parse accounts results to match select2 results | |
| 85 | + foreach ( $accounts as $account ) { | |
| 86 | + | |
| 87 | + if( ! empty( $search ) ) { | |
| 88 | + if( strpos( strtolower( $account['company'] ), strtolower( $search ) ) === false ) { | |
| 89 | + continue; | |
| 90 | + } | |
| 91 | + } | |
| 92 | + | |
| 93 | + $results[] = array( | |
| 94 | + 'id' => $account['id'], | |
| 95 | + 'text' => $account['company'] | |
| 96 | + ); | |
| 97 | + } | |
| 98 | + | |
| 99 | + // Prepend option none | |
| 100 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 101 | + | |
| 102 | + // Return our results | |
| 103 | + wp_send_json_success( $results ); | |
| 104 | + die; | |
| 105 | + | |
| 106 | +} | |
| 107 | +add_action( 'wp_ajax_automatorwp_aweber_get_accounts', 'automatorwp_aweber_ajax_get_accounts' ); | |
| 108 | + | |
| 109 | +/** | |
| 110 | + * Ajax function for selecting lists | |
| 111 | + * | |
| 112 | + * @since 1.0.0 | |
| 113 | + */ | |
| 114 | +function automatorwp_aweber_ajax_get_lists() { | |
| 115 | + // Security check, forces to die if not security passed | |
| 116 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 117 | + | |
| 118 | + // Permissions check | |
| 119 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 120 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 121 | + } | |
| 122 | + | |
| 123 | + global $wpdb; | |
| 124 | + | |
| 125 | + // Pull back the search string | |
| 126 | + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; | |
| 127 | + | |
| 128 | + // Get account ID | |
| 129 | + $account_id = isset( $_REQUEST['table'] ) ? sanitize_text_field( $_REQUEST['table'] ) : ''; | |
| 130 | + | |
| 131 | + $lists = automatorwp_aweber_get_lists( $account_id ); | |
| 132 | + | |
| 133 | + $results = array(); | |
| 134 | + | |
| 135 | + // Parse accounts results to match select2 results | |
| 136 | + foreach ( $lists as $list ) { | |
| 137 | + | |
| 138 | + if( ! empty( $search ) ) { | |
| 139 | + if( strpos( strtolower( $list['name'] ), strtolower( $search ) ) === false ) { | |
| 140 | + continue; | |
| 141 | + } | |
| 142 | + } | |
| 143 | + | |
| 144 | + $results[] = array( | |
| 145 | + 'id' => $list['id'], | |
| 146 | + 'text' => $list['name'] | |
| 147 | + ); | |
| 148 | + } | |
| 149 | + | |
| 150 | + // Prepend option none | |
| 151 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 152 | + | |
| 153 | + // Return our results | |
| 154 | + wp_send_json_success( $results ); | |
| 155 | + die; | |
| 156 | + | |
| 157 | +} | |
| 158 | +add_action( 'wp_ajax_automatorwp_aweber_get_lists', 'automatorwp_aweber_ajax_get_lists' ); | |