| 1 |
<?php // phpcs:ignore Generic.Commenting.DocComment.MissingShort Squiz.Commenting.FileComment.MissingPackageTag |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'Access Denied.' ); |
| 5 |
} |
| 6 |
|
| 7 |
if ( ! function_exists( 'mailrelay_woo_commerce_installed' ) ) { |
| 8 |
function mailrelay_woo_commerce_installed() { |
| 9 |
return is_plugin_active( 'woocommerce/woocommerce.php' ); |
| 10 |
} |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! function_exists( 'mailrelay_api_request' ) ) { |
| 14 |
function mailrelay_api_request( $method, $url, $args = array(), $mailrelay_data = null ) { |
| 15 |
if ( is_null( $mailrelay_data ) ) { |
| 16 |
$mailrelay_data = mailrelay_data(); |
| 17 |
} |
| 18 |
|
| 19 |
$url = 'https://' . $mailrelay_data['host'] . '.ipzmarketing.com/api/v1/' . $url; |
| 20 |
|
| 21 |
$args = array_merge_recursive( |
| 22 |
$args, |
| 23 |
array( |
| 24 |
'method' => $method, |
| 25 |
'headers' => array( |
| 26 |
'x-auth-token' => $mailrelay_data['api_key'], |
| 27 |
'x-request-origin' => 'Wordpress|'. MAILRELAY_PLUGIN_VERSION .'|'. get_bloginfo('version'), |
| 28 |
), |
| 29 |
) |
| 30 |
); |
| 31 |
|
| 32 |
$response = wp_remote_request( $url, $args ); |
| 33 |
|
| 34 |
if ( is_wp_error( $response ) ) { |
| 35 |
return array( |
| 36 |
'wp_error' => true, |
| 37 |
'error_message' => $response->get_error_message(), |
| 38 |
'response' => $response, |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
$code = wp_remote_retrieve_response_code( $response ); |
| 43 |
|
| 44 |
$body = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 45 |
|
| 46 |
return array( |
| 47 |
'wp_error' => false, |
| 48 |
'code' => $code, |
| 49 |
'body' => $body, |
| 50 |
'response' => $response, |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! function_exists( 'mailrelay_get_groups' ) ) { |
| 56 |
function mailrelay_get_groups() { |
| 57 |
$response = mailrelay_api_request( 'GET', 'groups' ); |
| 58 |
|
| 59 |
if ( $response['wp_error'] ) { |
| 60 |
/* translators: %s error message */ |
| 61 |
wp_die( sprintf( esc_html__( 'Something went wrong: %s', 'mailrelay' ), esc_html( $response['error_message'] ) ) ); |
| 62 |
} |
| 63 |
|
| 64 |
if ( 200 === $response['code'] ) { |
| 65 |
return $response['body']; |
| 66 |
} else { |
| 67 |
/* translators: %s error message */ |
| 68 |
wp_die( sprintf( esc_html__( 'Something went wrong: %s', 'mailrelay' ), esc_html( $response['body'] ) ) ); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
if ( ! function_exists( 'mailrelay_sync_user' ) ) { |
| 74 |
function mailrelay_sync_user( $user, $groups, $mailrelay_data = null ) { |
| 75 |
if ( is_null( $mailrelay_data ) ) { |
| 76 |
$mailrelay_data = mailrelay_data(); |
| 77 |
} |
| 78 |
|
| 79 |
$full_name = $user->first_name . ' ' . $user->last_name; |
| 80 |
$data = array( |
| 81 |
'email' => $user->user_email, |
| 82 |
'name' => $full_name, |
| 83 |
'replace_groups' => false, |
| 84 |
'restore_if_deleted' => false, |
| 85 |
'status' => 'active', |
| 86 |
'group_ids' => (array) $groups, |
| 87 |
); |
| 88 |
$data = wp_json_encode( $data ); |
| 89 |
|
| 90 |
$response = mailrelay_api_request( |
| 91 |
'POST', |
| 92 |
'subscribers/sync', |
| 93 |
array( |
| 94 |
'post_data' => 'body', |
| 95 |
'body' => $data, |
| 96 |
'headers' => array( |
| 97 |
'content-type' => 'application/json', |
| 98 |
), |
| 99 |
), |
| 100 |
$mailrelay_data |
| 101 |
); |
| 102 |
|
| 103 |
if ( $response['wp_error'] ) { |
| 104 |
/* translators: %s error message */ |
| 105 |
wp_die( sprintf( esc_html__( 'Something went wrong: %s', 'mailrelay' ), esc_html( $response['error_message'] ) ) ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( 200 === $response['code'] ) { |
| 109 |
return array( |
| 110 |
'status' => 'updated', |
| 111 |
); |
| 112 |
} elseif ( 201 === $response['code'] ) { |
| 113 |
return array( |
| 114 |
'status' => 'created', |
| 115 |
); |
| 116 |
} else { |
| 117 |
return array( |
| 118 |
'status' => 'failed', |
| 119 |
); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
if ( ! function_exists( 'mailrelay_ping' ) ) { |
| 125 |
function mailrelay_ping( $mailrelay_data ) { |
| 126 |
$response = mailrelay_api_request( 'GET', 'ping', array(), $mailrelay_data ); |
| 127 |
|
| 128 |
if ( $response['wp_error'] ) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
return $response['code']; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if ( ! function_exists( 'mailrelay_data' ) ) { |
| 137 |
function mailrelay_data() { |
| 138 |
$api_key = get_option( 'mailrelay_api_key' ); |
| 139 |
|
| 140 |
if ( $api_key ) { |
| 141 |
return array( |
| 142 |
'host' => get_option( 'mailrelay_host' ), |
| 143 |
'api_key' => $api_key, |
| 144 |
'auto_sync' => get_option( 'mailrelay_auto_sync' ), |
| 145 |
'groups_sync' => get_option( 'mailrelay_auto_sync_groups' ), |
| 146 |
); |
| 147 |
} |
| 148 |
|
| 149 |
return false; |
| 150 |
} |
| 151 |
} |
| 152 |
|