Skip to content

Commit

Permalink
Resolves phpcs errors
Browse files Browse the repository at this point in the history
  • Loading branch information
nateconley committed Jun 5, 2024
1 parent 66aaf7f commit de25910
Show file tree
Hide file tree
Showing 9 changed files with 669 additions and 324 deletions.
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 ) {
$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

0 comments on commit de25910

Please sign in to comment.