PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | integrations/mailerlite/includes/ajax-functions.php +118 -0 5.7.2 → 6.0.3 View file →
@@ -1,0 +1,118 @@
1 +<?php
2 +/**
3 + * Ajax Functions
4 + *
5 + * @package AutomatorWP\MailerLite\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_mailerlite_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_mailerlite_';
26 +
27 + $url = automatorwp_mailerlite_get_url();
28 + $token = sanitize_text_field( $_POST['token'] );
29 +
30 + // Check parameters given
31 + if( empty( $token ) ) {
32 + wp_send_json_error( array( 'message' => __( 'API Token is required to connect with MailerLite', 'automatorwp-pro' ) ) );
33 + return;
34 + }
35 +
36 + // To get first answer and check the connection
37 + $response = wp_remote_get( $url . '/api/subscribers', array(
38 + 'headers' => array(
39 + 'Authorization' => 'Bearer ' . $token,
40 + 'Accept' => 'application/json',
41 + 'Content-Type' => 'application/json'
42 + ),
43 + 'body' => array(
44 + 'limit' => 1,
45 + )
46 + ) );
47 +
48 + // Incorrect API token
49 + if ( isset( $response['response']['code'] ) && $response['response']['code'] !== 200 ){
50 + wp_send_json_error (array( 'message' => __( 'Please, check your credentials', 'automatorwp-pro' ) ) );
51 + return;
52 + }
53 +
54 + $settings = get_option( 'automatorwp_settings' );
55 +
56 + // Save client url and API key
57 + $settings[$prefix . 'token'] = $token;
58 +
59 + // Update settings
60 + update_option( 'automatorwp_settings', $settings );
61 + $admin_url = str_replace( 'http://', 'https://', get_admin_url() ) . 'admin.php?page=automatorwp_settings&tab=opt-tab-mailerlite';
62 +
63 + wp_send_json_success( array(
64 + 'message' => __( 'Correct data to connect with MailerLite', 'automatorwp-pro' ),
65 + 'redirect_url' => $admin_url
66 + ) );
67 +
68 +}
69 +add_action( 'wp_ajax_automatorwp_mailerlite_authorize', 'automatorwp_mailerlite_ajax_authorize' );
70 +
71 +
72 +/**
73 + * Ajax function for selecting groups
74 + *
75 + * @since 1.0.0
76 + */
77 +function automatorwp_mailerlite_ajax_get_groups() {
78 + // Security check, forces to die if not security passed
79 + check_ajax_referer( 'automatorwp_admin', 'nonce' );
80 +
81 + // Permissions check
82 + if( ! current_user_can( automatorwp_get_manager_capability() ) ) {
83 + wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'automatorwp' ) );
84 + }
85 +
86 + global $wpdb;
87 +
88 + // Pull back the search string
89 + $search = isset( $_REQUEST['q'] ) ? $wpdb->esc_like( sanitize_text_field( $_REQUEST['q'] ) ) : '';
90 +
91 + $groups = automatorwp_mailerlite_get_groups();
92 +
93 + $results = array();
94 +
95 + // Parse groups results to match select2 results
96 + foreach ( $groups as $group ) {
97 +
98 + if( ! empty( $search ) ) {
99 + if( strpos( strtolower( $group['name'] ), strtolower( $search ) ) === false ) {
100 + continue;
101 + }
102 + }
103 +
104 + $results[] = array(
105 + 'id' => strval($group['id']),
106 + 'text' => $group['name']
107 + );
108 + }
109 +
110 + // Prepend option none
111 + $results = automatorwp_ajax_parse_extra_options( $results );
112 +
113 + // Return our results
114 + wp_send_json_success( $results );
115 + die;
116 +
117 +}
118 +add_action( 'wp_ajax_automatorwp_mailerlite_get_groups', 'automatorwp_mailerlite_ajax_get_groups' );