__( 'API Token is required to connect with MailerLite', 'automatorwp-pro' ) ) ); return; } // To get first answer and check the connection $response = wp_remote_get( $url . '/api/subscribers', array( 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json', 'Content-Type' => 'application/json' ), 'body' => array( 'limit' => 1, ) ) ); // Incorrect API token if ( isset( $response['response']['code'] ) && $response['response']['code'] !== 200 ){ wp_send_json_error (array( 'message' => __( 'Please, check your credentials', 'automatorwp-pro' ) ) ); return; } $settings = get_option( 'automatorwp_settings' ); // Save client url and API key $settings[$prefix . 'token'] = $token; // Update settings update_option( 'automatorwp_settings', $settings ); $admin_url = str_replace( 'http://', 'https://', get_admin_url() ) . 'admin.php?page=automatorwp_settings&tab=opt-tab-mailerlite'; wp_send_json_success( array( 'message' => __( 'Correct data to connect with MailerLite', 'automatorwp-pro' ), 'redirect_url' => $admin_url ) ); } add_action( 'wp_ajax_automatorwp_mailerlite_authorize', 'automatorwp_mailerlite_ajax_authorize' ); /** * Ajax function for selecting groups * * @since 1.0.0 */ function automatorwp_mailerlite_ajax_get_groups() { // Security check, forces to die if not security passed check_ajax_referer( 'automatorwp_admin', 'nonce' ); // Permissions check if( ! current_user_can( automatorwp_get_manager_capability() ) ) { wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) ); } global $wpdb; // Pull back the search string $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : ''; $groups = automatorwp_mailerlite_get_groups(); $results = array(); // Parse groups results to match select2 results foreach ( $groups as $group ) { if( ! empty( $search ) ) { if( strpos( strtolower( $group['name'] ), strtolower( $search ) ) === false ) { continue; } } $results[] = array( 'id' => strval($group['id']), 'text' => $group['name'] ); } // Prepend option none $results = automatorwp_ajax_parse_extra_options( $results ); // Return our results wp_send_json_success( $results ); die; } add_action( 'wp_ajax_automatorwp_mailerlite_get_groups', 'automatorwp_mailerlite_ajax_get_groups' );