Skip to content

Commit

Permalink
refactors, form improvements, cached data improvements, etc..
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Mar 17, 2014
1 parent 455c74a commit 9b9a641
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 80 deletions.
4 changes: 4 additions & 0 deletions assets/css/form-reset.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
margin-top: 10px;
}

.mc4wp-form input {
vertical-align: baseline;
}

.mc4wp-form input.placeholdersjs {
color: #aaa !important;
}
Expand Down
6 changes: 1 addition & 5 deletions includes/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,18 +239,14 @@ public function call( $method, array $data = array() )
}

// dirty fix for older WP version
if($method == 'helper/ping' && isset( $response['headers']['content-length'] ) && (int) $response['headers']['content-length'] == '44' ) {
if($method == 'helper/ping' && isset( $response['headers']['content-length'] ) && (int) $response['headers']['content-length'] == 44 ) {
return (object) array( 'msg' => "Everything's Chimpy!");
}

$body = wp_remote_retrieve_body( $response );
return json_decode( $body );
}

public function show_http_error() {

}

/**
* Checks if an error occured in the most recent request
* @return boolean
Expand Down
131 changes: 104 additions & 27 deletions includes/class-form.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,11 @@ public function ensure_backwards_compatibility() {
return;
}

/**
* Act on posted data
*
* @var array $data
*/
public function subscribe( array $data ) {

$email = null;
Expand All @@ -271,8 +276,8 @@ public function subscribe( array $data ) {
foreach ( $data as $name => $value ) {

// uppercase all variables
$name = trim(strtoupper($name));
$value = (is_scalar($value)) ? trim($value) : $value;
$name = trim( strtoupper( $name ) );
$value = ( is_scalar( $value ) ) ? trim( $value ) : $value;

if( $name === 'EMAIL' && is_email($value) ) {
// set the email address
Expand All @@ -282,7 +287,9 @@ public function subscribe( array $data ) {
$groupings = $value;

// malformed
if ( !is_array( $groupings ) ) { continue; }
if ( ! is_array( $groupings ) ) {
continue;
}

// setup groupings array
$merge_vars['GROUPINGS'] = array();
Expand All @@ -297,26 +304,29 @@ public function subscribe( array $data ) {
$grouping['name'] = $grouping_id_or_name;
}

if ( !is_array( $groups ) ) {
$grouping['groups'] = explode( ',', $groups );
} else {
$grouping['groups'] = $groups;
// comma separated list should become an array
if( ! is_array( $groups ) ) {
$groups = explode( ',', $groups );
}

$grouping['groups'] = array_map( 'stripslashes', $groups );

// add grouping to array
$merge_vars['GROUPINGS'][] = $grouping;
}

if ( empty( $merge_vars['GROUPINGS'] ) ) { unset( $merge_vars['GROUPINGS'] ); }
if ( empty( $merge_vars['GROUPINGS'] ) ) {
unset( $merge_vars['GROUPINGS'] );
}

} else if($name === 'BIRTHDAY') {
// format birthdays in the DD/MM format required by MailChimp
$merge_vars['BIRTHDAY'] = date('d/m', strtotime( $value ) );
$merge_vars['BIRTHDAY'] = date( 'd/m', strtotime( $value ) );
} else if($name === 'ADDRESS') {

if(!isset($value['addr1'])) {
if( ! isset( $value['addr1'] ) ) {
// addr1, addr2, city, state, zip, country
$addr_pieces = explode(',', $value);
$addr_pieces = explode( ',', $value );

// try to fill it.... this is a long shot
$merge_vars['ADDRESS'] = array(
Expand All @@ -338,17 +348,17 @@ public function subscribe( array $data ) {
}

// check if an email address has been found
if( !$email ) {
if( ! $email ) {
$this->error = 'invalid_email';
return false;
}

// Try to guess FNAME and LNAME if they are not given, but NAME is
if(isset($merge_vars['NAME']) && !isset($merge_vars['FNAME']) && !isset($merge_vars['LNAME'])) {
if( isset( $merge_vars['NAME'] ) && !isset( $merge_vars['FNAME'] ) && ! isset( $merge_vars['LNAME'] ) ) {

$strpos = strpos($merge_vars['NAME'], ' ');

if($strpos) {
if( $strpos !== false ) {
$merge_vars['FNAME'] = substr($merge_vars['NAME'], 0, $strpos);
$merge_vars['LNAME'] = substr($merge_vars['NAME'], $strpos);
} else {
Expand All @@ -359,39 +369,106 @@ public function subscribe( array $data ) {
$api = mc4wp_get_api();
$opts = mc4wp_get_options('form');

$lists = $opts['lists'];
$lists = $this->get_lists();

if ( empty( $lists ) ) {
$this->error = 'no_lists_selected';
return false;
}

do_action('mc4wp_before_subscribe', $email, $merge_vars, 0);

$result = false;
$email_type = apply_filters('mc4wp_email_type', 'html');
$lists = apply_filters('mc4wp_lists', $lists, $merge_vars);

$email_type = $this->get_email_type();

foreach ( $lists as $list_id ) {
// allow plugins to alter merge vars for each individual list
$list_merge_vars = apply_filters('mc4wp_merge_vars', $merge_vars, 0, $list_id);

// send a subscribe request to MailChimp for each list
$result = $api->subscribe( $list_id, $email, $list_merge_vars, $email_type, $opts['double_optin'] );
}

do_action('mc4wp_after_subscribe', $email, $merge_vars, 0, $result);

// flawed, will only check the result of the last list
if ( $result === true ) {

// do not use... will be removed in 2.0
$from_url = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
do_action('mc4wp_subscribe_form', $email, $list_id, 0, $merge_vars, $from_url);

$this->success = true;
} else {
if ( $result !== true ) {
// subscribe request failed, store error.
$this->success = false;
$this->error = $result;
}

// store user email in a cookie
$this->set_email_cookie( $email );

/**
* @deprecated Don't use, will be removed in v2.0
*/
$from_url = (isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : '';
do_action('mc4wp_subscribe_form', $email, $list_id, 0, $merge_vars, $from_url);

// Store success result
$this->success = true;

return true;
}

/**
* Gets the email_type
*
* @return string The email type to use for subscription coming from this form
*/
public function get_email_type( ) {

$email_type = 'html';

// get email type from form
if( isset( $_POST['_mc4wp_email_type'] ) ) {
$email_type = trim( $_POST['_mc4wp_email_type'] );
}

return $this->success;
// allow plugins to override this email type
$email_type = apply_filters( 'mc4wp_email_type', $email_type );

return $email_type;
}

/**
* Get MailChimp Lists to subscribe to
*
* @return array Array of selected MailChimp lists
*/
public function get_lists( ) {

$opts = mc4wp_get_options('form');

$lists = $opts['lists'];

// get lists from form, if set.
if( isset( $_POST['_mc4wp_lists'] ) && ! empty( $_POST['_mc4wp_lists'] ) ) {

$lists = $_POST['_mc4wp_lists'];

// make sure lists is an array
if( ! is_array( $lists ) ) {
$lists = array( $lists );
}

}

// allow plugins to alter the lists to subscribe to
$lists = apply_filters( 'mc4wp_lists', $lists );

return $lists;
}

/**
* Stores the given email in a cookie for 30 days
*
* @param string $email
*/
public function set_email_cookie( $email ) {
setcookie( 'mc4wp_email', $email, strtotime( '+30 days' ), '/' );
}

}
49 changes: 45 additions & 4 deletions includes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,36 @@

class MC4WP_Lite {

/**
* @var MC4WP_Lite_Form
*/
private $form_manager;

/**
* @var MC4WP_Lite_Checkbox
*/
private $checkbox_manager;

/**
* @var MC4WP_Lite_API
*/
private $api = null;

/**
* @var string Code version number
*/
private $code_version = "1.0";

/**
* Constructor
*/
public function __construct() {

$this->backwards_compatibility();
// Check whether to run the upgrade routine
$db_code_version = get_option( 'mc4wp_code_version', 0 );
if( version_compare( $this->code_version, $db_code_version, "<" ) ) {
$this->upgrade();
}

// checkbox
require_once MC4WP_LITE_PLUGIN_DIR . 'includes/class-checkbox.php';
Expand All @@ -37,20 +61,34 @@ public function __construct() {
}

/**
* @return MC4WP_Checkbox
* @return MC4WP_Lite_Checkbox
*/
public function get_checkbox_manager() {
return $this->checkbox_manager;
}

/**
* @return MC4WP_Form
* @return MC4WP_Lite_Form
*/
public function get_form_manager() {
return $this->form_manager;
}

private function backwards_compatibility() {
/**
* @return MC4WP_Lite_API
*/
public function get_api() {

if( $this->api === null ) {
require_once MC4WP_LITE_PLUGIN_DIR . 'includes/class-api.php';
$opts = mc4wp_get_options();
$this->api = new MC4WP_Lite_API( $opts['general']['api_key'] );
}

return $this->api;
}

private function upgrade() {
$options = get_option( 'mc4wp_lite' );

// transfer widget to new id?
Expand Down Expand Up @@ -119,6 +157,9 @@ private function backwards_compatibility() {
update_option( 'mc4wp_lite_checkbox', $new_options['checkbox'] );
update_option( 'mc4wp_lite_form', $new_options['form'] );
} // end transfer options

// update code version
update_option( 'mc4wp_code_version', $this->code_version );
}

public function register_widget()
Expand Down
14 changes: 4 additions & 10 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,10 @@ function mc4wp_get_options( $key = null ) {

/**
* Gets the MailChimp for WP API class and injects it with the given API key
* @return MC4WP_API
*
* @return MC4WP_Lite_API
*/
function mc4wp_get_api() {
static $api;

if( ! $api ) {
require_once MC4WP_LITE_PLUGIN_DIR . 'includes/class-api.php';
$opts = mc4wp_get_options();
$api = new MC4WP_Lite_API( $opts['general']['api_key'] );
}

return $api;
global $mc4wp;
return $mc4wp->get_api();
}
5 changes: 4 additions & 1 deletion includes/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ function mc4wp_get_subscriber_count( $list_ids ) {
} else {
// use fallback transient
$list_counts = get_transient( 'mc4wp_list_counts_fallback' );
if ( ! $list_counts ) { return 0; }

if ( ! $list_counts ) {
return 0;
}
}
}

Expand Down
Loading

0 comments on commit 9b9a641

Please sign in to comment.