Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolves phpcs errors #24

Merged
merged 6 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ WordPress v2.8 or higher:

If you are adding it inside a php code block, pop this in:

` mailchimpSF_signup_form(); `
` mailchimp_sf_signup_form(); `

Or, if you are dropping it in between a bunch of HTML, use this:

`<?php mailchimpSF_signup_form(); ?>`
`<?php mailchimp_sf_signup_form(); ?>`

Where ever you want it to show up.

Expand Down
78 changes: 70 additions & 8 deletions lib/mailchimp/mailchimp.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,77 @@
<?php

/**
* Handles Mailchimp API authorization.
*
* @package Mailchimp
*/

/**
* Handles Mailchimp API authorization.
*/
class MailChimp_API {

/**
* The API key
*
* @var string
*/
public $key;

/**
* The API key
*
* @var string
*/
public $api_key;

/**
* The API url
*
* @var string
*/
public $api_url;

/**
* The datacenter.
*
* @var string
*/
public $datacenter;

public function __construct( $api_key ) {
/**
* Initialize the class
*
* @param string $api_key The API key.
* @throws Exception If no api key is set
*/
public function __construct( string $api_key ) {
dkotter marked this conversation as resolved.
Show resolved Hide resolved
$api_key = trim( $api_key );
if ( ! $api_key ) {
throw new Exception( __( 'Invalid API Key: ' . $api_key ) );
throw new Exception(
esc_html(
sprintf(
// translators: placeholder is an api key
__( 'Invalid API Key: %s', 'mailchimp_i18n' ),
$api_key
)
)
);
}

$this->key = $api_key;
$dc = explode( '-', $api_key );
$this->datacenter = empty( $dc[1] ) ? 'us1' : $dc[1];
$this->api_url = 'https://' . $this->datacenter . '.api.mailchimp.com/3.0/';
return;
}

/**
* Get endpoint.
*
* @param string $endpoint The Mailchimp endpoint.
* @param integer $count The count to retrieve.
* @param array $fields The fields to retrieve.
* @return mixed
*/
public function get( $endpoint, $count = 10, $fields = array() ) {
$query_params = '';

Expand Down Expand Up @@ -47,7 +101,7 @@ public function get( $endpoint, $count = 10, $fields = array() ) {

$request = wp_remote_get( $url, $args );

if ( is_array( $request ) && 200 == $request['response']['code'] ) {
if ( is_array( $request ) && 200 === $request['response']['code'] ) {
return json_decode( $request['body'], true );
} elseif ( is_array( $request ) && $request['response']['code'] ) {
$error = json_decode( $request['body'], true );
Expand All @@ -58,6 +112,14 @@ public function get( $endpoint, $count = 10, $fields = array() ) {
}
}

/**
* Sends request to Mailchimp endpoint.
*
* @param string $endpoint The endpoint to send the request.
* @param string $body The body of the request
* @param string $method The request method.
* @return mixed
*/
public function post( $endpoint, $body, $method = 'POST' ) {
$url = $this->api_url . $endpoint;

Expand All @@ -68,11 +130,11 @@ public function post( $endpoint, $body, $method = 'POST' ) {
'httpversion' => '1.1',
'user-agent' => 'Mailchimp WordPress Plugin/' . get_bloginfo( 'url' ),
'headers' => array( 'Authorization' => 'apikey ' . $this->key ),
'body' => json_encode( $body ),
'body' => wp_json_encode( $body ),
);
$request = wp_remote_post( $url, $args );

if ( is_array( $request ) && 200 == $request['response']['code'] ) {
if ( is_array( $request ) && 200 === $request['response']['code'] ) {
return json_decode( $request['body'], true );
} else {
if ( is_wp_error( $request ) ) {
Expand All @@ -86,7 +148,7 @@ public function post( $endpoint, $body, $method = 'POST' ) {
// Email address doesn't come back from the API, so if something's wrong, it's that.
$field_name = 'Email Address';
$body['errors'][0]['message'] = 'Please fill out a valid email address.';
} elseif ( $merge['tag'] == $body['errors'][0]['field'] ) {
} elseif ( $merge['tag'] === $body['errors'][0]['field'] ) {
$field_name = $merge['name'];
}
}
Expand Down
Loading
Loading