Skip to content

Commit

Permalink
Merge pull request #24 from mailchimp/fix/linting
Browse files Browse the repository at this point in the history
Resolves phpcs errors
  • Loading branch information
dkotter authored Jun 7, 2024
2 parents 66aaf7f + cee743d commit fb30ec9
Show file tree
Hide file tree
Showing 9 changed files with 690 additions and 325 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,20 @@ 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.

## Upgrading

If you are upgrading to version 1.2.1 and you used the widget in your sidebar previously, all you need to do is drag the `Mailchimp Widget` back into the sidebar, visit the Mailchimp settings page (which will have maintained your prior settings), click the "Update List" button, and you're done!

If you are upgrading to version 1.6.0, you will need to updated any references to display function `mailchimpSF_signup_form` to `mailchimp_sf_signup_form`.

## Internationalization (i18n)

Currently we have the plugin configured so it can be translated and the following languages supported:
Expand Down
76 changes: 69 additions & 7 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;

/**
* Initialize the class
*
* @param string $api_key The API key.
* @throws Exception If no api key is set
*/
public function __construct( $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 fb30ec9

Please sign in to comment.