← All changes
|
integrations/activecampaign/includes/ajax-functions.php
+155
-0
5.7.3
→
6.0.3
View file →
| @@ -1,0 +1,155 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Ajax Functions | |
| 4 | + * | |
| 5 | + * @package AutomatorWP\ActiveCampaign\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_activecampaign_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_activecampaign_'; | |
| 26 | + | |
| 27 | + $url = sanitize_text_field( $_POST['url'] ); | |
| 28 | + $key = sanitize_text_field( $_POST['key'] ); | |
| 29 | + | |
| 30 | + // Check parameters given | |
| 31 | + if( empty( $url ) || empty( $key ) ) { | |
| 32 | + wp_send_json_error( array( 'message' => __( 'All fields are required to connect with ActiveCampaign', 'automatorwp' ) ) ); | |
| 33 | + return; | |
| 34 | + } | |
| 35 | + | |
| 36 | + // To get first answer and check the connection | |
| 37 | + $response = wp_remote_get( $url . '/api/3', array( | |
| 38 | + 'headers' => array( | |
| 39 | + 'Accept' => 'application/json', | |
| 40 | + 'Api-Token' => $key, | |
| 41 | + 'Content-Type' => 'application/json' | |
| 42 | + ), | |
| 43 | + 'sslverify' => false | |
| 44 | + ) ); | |
| 45 | + | |
| 46 | + // Incorrect URL or API key | |
| 47 | + if ( isset( $response->errors ) ){ | |
| 48 | + wp_send_json_error (array( 'message' => __( 'Please, check your credentials', 'automatorwp' ) ) ); | |
| 49 | + return; | |
| 50 | + } | |
| 51 | + | |
| 52 | + $settings = get_option( 'automatorwp_settings' ); | |
| 53 | + | |
| 54 | + // Save client url and API key | |
| 55 | + $settings[$prefix . 'url'] = $url; | |
| 56 | + $settings[$prefix . 'key'] = $key; | |
| 57 | + | |
| 58 | + // Update settings | |
| 59 | + update_option( 'automatorwp_settings', $settings ); | |
| 60 | + $admin_url = admin_url( 'admin.php?page=automatorwp_settings&tab=opt-tab-activecampaign' ); | |
| 61 | + | |
| 62 | + wp_send_json_success( array( | |
| 63 | + 'message' => __( 'Correct data to connect with ActiveCampaign', 'automatorwp' ), | |
| 64 | + 'redirect_url' => $admin_url | |
| 65 | + ) ); | |
| 66 | + | |
| 67 | +} | |
| 68 | +add_action( 'wp_ajax_automatorwp_activecampaign_authorize', 'automatorwp_activecampaign_ajax_authorize' ); | |
| 69 | + | |
| 70 | + | |
| 71 | +/** | |
| 72 | + * Set the default URL value | |
| 73 | + * | |
| 74 | + * @since 1.0.0 | |
| 75 | + * | |
| 76 | + * @return string | |
| 77 | + */ | |
| 78 | +function automatorwp_activecampaign_ajax_refresh( ) { | |
| 79 | + // Security check | |
| 80 | + check_ajax_referer( 'automatorwp_admin', 'nonce' ); | |
| 81 | + | |
| 82 | + // Permissions check | |
| 83 | + if( ! current_user_can( automatorwp_get_manager_capability() ) ) { | |
| 84 | + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); | |
| 85 | + } | |
| 86 | + | |
| 87 | + $prefix = 'automatorwp_activecampaign_'; | |
| 88 | + | |
| 89 | + // Get random characters for slug | |
| 90 | + $slug = strtolower( wp_generate_password( 8, false ) ); | |
| 91 | + | |
| 92 | + $settings = get_option( 'automatorwp_settings' ); | |
| 93 | + | |
| 94 | + $settings[$prefix . 'webhook'] = get_rest_url() . 'activecampaign/webhooks/' . $slug; | |
| 95 | + $settings[$prefix . 'slug'] = $slug; | |
| 96 | + update_option( 'automatorwp_settings', $settings); | |
| 97 | + | |
| 98 | + $admin_url = str_replace( 'http://', 'http://', get_admin_url() ) . 'admin.php?page=automatorwp_settings&tab=opt-tab-activecampaign'; | |
| 99 | + | |
| 100 | + wp_send_json_success( array( | |
| 101 | + 'message' => __( 'Webhook URL refreshed', 'automatorwp' ), | |
| 102 | + 'redirect_url' => $admin_url | |
| 103 | + ) ); | |
| 104 | + | |
| 105 | +} | |
| 106 | + | |
| 107 | +add_action( 'wp_ajax_automatorwp_activecampaign_refresh', 'automatorwp_activecampaign_ajax_refresh' ); | |
| 108 | + | |
| 109 | +/** | |
| 110 | + * Ajax function for selecting tags | |
| 111 | + * | |
| 112 | + * @since 1.0.0 | |
| 113 | + */ | |
| 114 | +function automatorwp_activecampaign_ajax_get_tags() { | |
| 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( $_REQUEST['q'] ) : ''; | |
| 127 | + $page = isset( $_REQUEST['page'] ) ? absint( $_REQUEST['page'] ) : 1; | |
| 128 | + | |
| 129 | + $tags = automatorwp_activecampaign_get_tags( $search, $page ); | |
| 130 | + | |
| 131 | + $results = array(); | |
| 132 | + | |
| 133 | + // Parse tag results to match select2 results | |
| 134 | + foreach ( $tags as $tag ) { | |
| 135 | + | |
| 136 | + $results[] = array( | |
| 137 | + 'id' => $tag['id'], | |
| 138 | + 'text' => $tag['name'] | |
| 139 | + ); | |
| 140 | + } | |
| 141 | + | |
| 142 | + // Prepend option none | |
| 143 | + $results = automatorwp_ajax_parse_extra_options( $results ); | |
| 144 | + | |
| 145 | + $response = array( | |
| 146 | + 'results' => $results, | |
| 147 | + 'more_results' => count( $results ), | |
| 148 | + ); | |
| 149 | + | |
| 150 | + // Return our results | |
| 151 | + wp_send_json_success( $response ); | |
| 152 | + die; | |
| 153 | + | |
| 154 | +} | |
| 155 | +add_action( 'wp_ajax_automatorwp_activecampaign_get_tags', 'automatorwp_activecampaign_ajax_get_tags' ); | |