90 lines
2.5 KiB
PHP
90 lines
2.5 KiB
PHP
<?php
|
|
/**
|
|
* Helpers Opt-in Forms
|
|
*
|
|
* @package Powerkit
|
|
* @subpackage Modules/Helper
|
|
*/
|
|
|
|
/**
|
|
* Get privacy text
|
|
*/
|
|
function powerkit_mailchimp_get_privacy_text() {
|
|
return get_option( 'powerkit_mailchimp_privacy', esc_html__( 'By checking this box, you confirm that you have read and are agreeing to our terms of use regarding the storage of the data submitted through this form.', 'powerkit' ) );
|
|
}
|
|
|
|
/**
|
|
* Get headers for mailchimp request.
|
|
*/
|
|
function powerkit_mailchimp_headers() {
|
|
global $wp_version;
|
|
|
|
$token = get_option( 'powerkit_mailchimp_token' );
|
|
|
|
$headers = array();
|
|
$headers['Authorization'] = 'apikey: ' . $token;
|
|
$headers['Accept'] = 'application/json';
|
|
$headers['Content-Type'] = 'application/json';
|
|
$headers['User-Agent'] = 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)';
|
|
|
|
return $headers;
|
|
}
|
|
|
|
/**
|
|
* Performs the underlying HTTP request. Not very exciting.
|
|
*
|
|
* @param string $http_verb The HTTP verb to use: get, post, put, patch, delete.
|
|
* @param string $method The API method to be called.
|
|
* @param array $data Assoc array of parameters to be passed.
|
|
*/
|
|
function powerkit_mailchimp_request( $http_verb = 'GET', $method = 'lists', $data = array() ) {
|
|
$token = get_option( 'powerkit_mailchimp_token' );
|
|
|
|
if ( ! $token || false === strpos( $token, '-' ) ) {
|
|
return false;
|
|
}
|
|
|
|
$url = 'https://<dc>.api.mailchimp.com/3.0/' . ltrim( $method, '/' );
|
|
|
|
list(, $data_center ) = explode( '-', $token );
|
|
|
|
$url = str_replace( '<dc>', $data_center, $url );
|
|
|
|
$args = array(
|
|
'url' => $url,
|
|
'method' => $http_verb,
|
|
'headers' => powerkit_mailchimp_headers(),
|
|
'timeout' => 10,
|
|
'sslverify' => apply_filters( 'powerkit_mailchimp_sslverify', true ),
|
|
);
|
|
|
|
if ( ! empty( $data ) ) {
|
|
if ( in_array( $http_verb, array( 'GET', 'DELETE' ), true ) ) {
|
|
$url = add_query_arg( $data, $url );
|
|
} else {
|
|
$args['body'] = wp_json_encode( $data );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Filter the request arguments for all requests generated by this class.
|
|
*
|
|
* @param array $args
|
|
*/
|
|
$args = apply_filters( 'powerkit_mailchimp_http_args', $args, $url );
|
|
|
|
// Perform request.
|
|
$response = wp_remote_request( $url, $args );
|
|
|
|
// Instagram response.
|
|
if ( ! is_wp_error( $response ) ) {
|
|
$response = wp_remote_retrieve_body( $response );
|
|
|
|
$response = json_decode( $response, true );
|
|
} else {
|
|
powerkit_alert_warning( esc_html__( 'This client has not been approved to access this resource.', 'powerkit' ) );
|
|
}
|
|
|
|
return $response;
|
|
}
|