Skip to content

Commit

Permalink
Merge branch 'release/4.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
remcotolsma committed Apr 20, 2018
2 parents e21b35b + d882ec6 commit 2dca369
Show file tree
Hide file tree
Showing 23 changed files with 481 additions and 326 deletions.
521 changes: 266 additions & 255 deletions change_log.txt

Large diffs are not rendered by default.

166 changes: 141 additions & 25 deletions class-gf-mailchimp.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

// don't load directly
if ( ! defined( 'ABSPATH' ) ) {
die();
}

GFForms::include_feed_addon_framework();

/**
Expand All @@ -17,7 +22,7 @@ class GFMailChimp extends GFFeedAddOn {
*
* @since 3.0
* @access private
* @var object $_instance If available, contains an instance of this class.
* @var GFMailChimp $_instance If available, contains an instance of this class.
*/
private static $_instance = null;

Expand Down Expand Up @@ -147,14 +152,23 @@ class GFMailChimp extends GFFeedAddOn {
*/
protected $merge_var_name = '';

/**
* Defines the MailChimp merge fields used in the current request.
*
* @since 4.2.4
* @access protected
* @var array $merge_fields The MailChimp merge fields used in the current request.
*/
protected $merge_fields = array();

/**
* Contains an instance of the Mailchimp API library, if available.
*
* @since 1.0
* @access protected
* @var object $api If available, contains an instance of the Mailchimp API library.
*/
private $api = null;
public $api = null;

/**
* Get an instance of this class.
Expand Down Expand Up @@ -188,7 +202,7 @@ public function pre_init() {

if ( $this->is_gravityforms_supported() ) {

// Load the Mailgun API library.
// Load the MailChimp API library.
if ( ! class_exists( 'GF_MailChimp_API' ) ) {
require_once( 'includes/class-gf-mailchimp-api.php' );
}
Expand Down Expand Up @@ -248,7 +262,9 @@ public function styles() {
'handle' => $this->_slug . '_form_settings',
'src' => $this->get_base_url() . '/css/form_settings.css',
'version' => $this->_version,
'enqueue' => array( 'admin_page' => array( 'form_settings' ) ),
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
),
),
);

Expand Down Expand Up @@ -547,19 +563,8 @@ public function merge_vars_field_map() {
// Get current list ID.
$list_id = $this->get_setting( 'mailchimpList' );

try {

// Get merge fields.
$merge_fields = $this->api->get_list_merge_fields( $list_id );

} catch ( Exception $e ) {

// Log error.
$this->log_error( __METHOD__ . '(): Unable to get merge fields for MailChimp list; ' . $e->getMessage() );

return $field_map;

}
// Get merge fields.
$merge_fields = $this->get_list_merge_fields( $list_id );

// If merge fields exist, add to field map.
if ( ! empty( $merge_fields['merge_fields'] ) ) {
Expand Down Expand Up @@ -985,6 +990,38 @@ public function process_feed( $feed, $entry, $form ) {
continue;
}

// Get merge field.
$merge_field = $this->get_list_merge_field( $feed['meta']['mailchimpList'], $name );

// Format date field.
if ( ! empty( $field_value ) && ! empty( $merge_field ) && in_array( $merge_field['type'], array( 'date', 'birthday' ) ) ) {

// Get date format.
$date_format = $merge_field['options']['date_format'];

// Convert field value to timestamp.
$field_value_timestamp = strtotime( $field_value );

// Format date.
switch( $date_format ) {

case 'DD/MM':
$field_value = date( 'd/m', $field_value_timestamp );
break;

case 'MM/DD':
$field_value = date( 'm/d', $field_value_timestamp );
break;

case 'DD/MM/YYYY':
case 'MM/DD/YYYY':
$field_value = date( 'm/d/Y', $field_value_timestamp );
break;

}

}

$merge_vars[ $name ] = $field_value;

}
Expand Down Expand Up @@ -1045,9 +1082,6 @@ public function process_feed( $feed, $entry, $form ) {
return;
}

// Initialize interests array.
$interests = $member_found ? $member['interests'] : array();

/**
* Modify whether a user that is already subscribed to your list has their groups replaced when submitting the form a second time.
*
Expand All @@ -1063,8 +1097,8 @@ public function process_feed( $feed, $entry, $form ) {
// Initialize interests to keep array.
$interests_to_keep = array();

// Get existing interests.
$existing_interests = rgar( $member, 'interests' );
// Initialize interests array.
$interests = $existing_interests = rgar( $member, 'interests', array() );

// If member was found, has existing interests and we are not keeping existing interest categories, remove them.
if ( $member_found && $existing_interests ) {
Expand Down Expand Up @@ -1116,8 +1150,8 @@ public function process_feed( $feed, $entry, $form ) {

}

// If member status is not defined, set to subscribed.
$member_status = isset( $member_status ) ? $member_status : 'subscribed';
// If member status is not defined or is anything other than pending, set to subscribed.
$member_status = isset( $member_status ) && $member_status === 'pending' ? $member_status : 'subscribed';

// Prepare subscription arguments.
$subscription = array(
Expand Down Expand Up @@ -1264,7 +1298,7 @@ public function process_feed( $feed, $entry, $form ) {
* @uses GFMailChimp::get_full_address()
* @uses GFMailChimp::maybe_override_field_value()
*
* @return array
* @return array|string
*/
public function get_field_value( $form, $entry, $field_id ) {

Expand Down Expand Up @@ -1676,6 +1710,88 @@ public function get_full_address( $entry, $field_id ) {

}

/**
* Get MailChimp merge fields for list.
*
* @since 4.2.4
* @access public
*
* @param string $list_id List ID to get merge fields for.
*
* @uses GFMailChimp::initialize_api()
* @uses GF_MailChimp_API::get_list_merge_fields()
*
* @return array
*/
public function get_list_merge_fields( $list_id = '' ) {

// If no list ID was provided or if API cannot be initialized, return.
if ( rgblank( $list_id ) || ! $this->initialize_api() ) {
return array();
}

// If merge fields have already been retrieved, return.
if ( isset( $this->merge_fields[ $list_id ] ) ) {
return $this->merge_fields[ $list_id ];
}

try {

// Get merge fields.
$this->merge_fields[ $list_id ] = $this->api->get_list_merge_fields( $list_id );

} catch ( Exception $e ) {

// Log error.
$this->log_error( __METHOD__ . '(): Unable to get merge fields for MailChimp list; ' . $e->getMessage() );

$this->merge_fields[ $list_id ] = array();

}

return $this->merge_fields[ $list_id ];

}

/**
* Get specific MailChimp merge field by tag.
*
* @since 4.2.4
* @access public
*
* @param string $list_id List ID to get merge fields for.
* @param string $tag Merge field tag.
*
* @uses GFMailChimp::get_list_merge_fields()
*
* @return array
*/
public function get_list_merge_field( $list_id = '', $tag = '' ) {

// Get the merge fields for list.
$merge_fields = $this->get_list_merge_fields( $list_id );

// If no merge fields were provided, return.
if ( empty( $merge_fields ) || ! isset( $merge_fields['merge_fields'] ) ) {
return;
}

// Loop through merge fields.
foreach ( $merge_fields['merge_fields'] as $merge_field ) {

// If this is not the merge field we are looking for, skip.
if ( $tag !== $merge_field['tag'] ) {
continue;
}

return $merge_field;

}

return array();

}




Expand Down
25 changes: 23 additions & 2 deletions includes/class-gf-mailchimp-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public function add_member_note( $list_id, $email_address, $note ) {
* @param string $method Request method. Defaults to GET.
* @param string $return_key Array key from response to return. Defaults to null (return full response).
*
* @throws Exception if API request returns an error, exception is thrown.
* @throws GF_MailChimp_Exception If API request returns an error, exception is thrown.
*
* @return array
*/
Expand All @@ -261,7 +261,21 @@ private function process_request( $path = '', $data = array(), $method = 'GET',
'Authorization' => 'Basic ' . base64_encode( ':' . $this->api_key ),
'Content-Type' => 'application/json',
),
/**
* Filters if SSL verification should occur.
*
* @param bool false If the SSL certificate should be verified. Defalts to false.
*
* @return bool
*/
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
/**
* Sets the HTTP timeout, in seconds, for the request.
*
* @param int 30 The timeout limit, in seconds. Defalts to 30.
*
* @return int
*/
'timeout' => apply_filters( 'http_request_timeout', 30 ),
);

Expand All @@ -270,7 +284,14 @@ private function process_request( $path = '', $data = array(), $method = 'GET',
$args['body'] = json_encode( $data );
}

// Filter request arguments.
/**
* Filters the MailChimp request arguments.
*
* @param array $args The request arguments sent to MailChimp.
* @param string $path The request path.
*
* @return array
*/
$args = apply_filters( 'gform_mailchimp_request_args', $args, $path );

// Get request response.
Expand Down
Binary file modified languages/gravityformsmailchimp-ca.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-da_DK.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-de_DE.mo
Binary file not shown.
Binary file added languages/gravityformsmailchimp-de_DE_formal.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-en.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-en_AU.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-en_GB.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-es_ES.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-fi.mo
Binary file not shown.
Binary file added languages/gravityformsmailchimp-fr_CA.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-fr_FR.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-it_IT.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-nb_NO.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-nl_NL.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-pt_BR.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-pt_PT.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-ru_RU.mo
Binary file not shown.
Binary file modified languages/gravityformsmailchimp-zh_CN.mo
Binary file not shown.
Loading

0 comments on commit 2dca369

Please sign in to comment.