Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active November 24, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/b3dcaec5a843c76656d8e0710146de9b to your computer and use it in GitHub Desktop.
Save igorbenic/b3dcaec5a843c76656d8e0710146de9b to your computer and use it in GitHub Desktop.
MailChimp WordPress Plugin with React: The Form and Form Submission | http://www.ibenic.com/mailchimp-wordpress-plugin-with-react-form-subscription
<?php
/**
* Functions for handling everything about the forms
*/
/**
* Add Form error
* @param numeric $form_id POST ID of the form
* @param string $message Message
* @return void
*/
function mwp_add_form_error( $form_id, $message ) {
$mwp = mwp();
$mwp->errors->add( 'form-' . $form_id, $message );
}
/**
* Get Form errors
* @param numeric $form_id
* @return array
*/
function mwp_get_form_errors( $form_id ) {
$mwp = mwp();
return $mwp->errors->get_error_messages( 'form-' . $form_id );
}
<?php
/**
* Renders the form
* @param int $id POST ID of our CPT mwp_form
* @return void
*/
function mwp_render_form( $id ) {
$form = new MWP_Form( $id );
$form->render();
}
/**
* Returns the list ID that is assigned to the form
* @param number $form_id ID of the form
* @return number List ID
*/
function mwp_get_form_list_id( $form_id ) {
return get_post_meta( $form_id, 'mwp_mailchimp_list_id', true );
}
<?php
/**
* Subscribe User on Form Submit
* Redirecting on a successful subscribe
*
* @hooked init
* @return void
*/
function mwp_subscribe_user_on_form_submit() {
if( isset( $_POST['mwp_subscribe_form_submit'] ) && $_POST['mwp_subscribe_form_submit'] != '' ) {
$name = '';
$email = '';
$form_id = '';
if( isset( $_POST['mwp_name'] ) ) {
$name = $_POST['mwp_name'];
}
if( isset( $_POST['mwp_email'] ) ) {
$email = $_POST['mwp_email'];
}
if( isset( $_POST['mwp_subscribe_form_submit'] ) ) {
$form_id = $_POST['mwp_subscribe_form_submit'];
}
$result = mwp_subscribe_user( $form_id, $name, $email );
if( $result['success'] == 1 && isset( $result['redirect'] ) ) {
$redirect = $result['redirect'];
if( filter_var($redirect, FILTER_VALIDATE_URL) !== false && strpos( $redirect, 'http' ) !== false ) {
wp_redirect( $redirect, 301 );
exit;
}
}
}
}
<?php
/**
* Subscribes a user to the Form (List)
* @param numeric $form_id
* @param string $name
* @param string $email
* @return array Contains success, redirect url and error messages
*/
function mwp_subscribe_user( $form_id, $name, $email ) {
// Setting the default $result array
$result = array( 'success' => 1, 'redirect' => '', 'errors' => 0 );
// If empty name, add error
if( $name == '' ) {
mwp_add_form_error( $form_id, __( 'Enter a name', 'mwp' ) );
}
// If empty email, add error
if( $email == '' ) {
mwp_add_form_error( $form_id, __( 'Enter an email', 'mwp' ) );
} else {
// If email is valid
if ( !filter_var( $email, FILTER_VALIDATE_EMAIL ) === false ) {
// Get the List ID
$list_id = mwp_get_form_list_id( $form_id );
if( $list_id != '' ) {
// Subscribe User
$subscribe = mwp_subscribe_user_to_list( $list_id, $name, $email );
// If subscribed, get the redirect url
if( $subscribe == true ) {
$result['redirect'] = get_post_meta( $form_id, 'mwp_mailchimp_list_redirect', true );
} else {
// If not subscribed, get the error
mwp_add_form_error( $form_id, $subscribe['title'] );
}
} else {
// If no list ID, add a generic error message
mwp_add_form_error( $form_id, __( 'We can\' subscribe you right now. Please contact the administrator.', 'socialnano' ) );
}
} else {
// If email is not valid, add error
mwp_add_form_error( $form_id, __( 'Enter a valid email', 'socialnano' ) );
}
}
$form_errors = mwp_get_form_errors( $form_id );
// If there are errors, it was not successful
if( count( $form_errors ) > 0 ) {
$result['success'] = 0;
$result['errors'] = $form_errors;
}
return $result;
}
<?php
// wp-mailchimp-plugin.php
/**
* Various hooks where we hook our functions
*/
public function hooks() {
add_action( 'admin_menu', 'mwp_settings' );
add_action( 'init', 'mwp_cpt_form', 0 );
add_action( 'init', 'mwp_subscribe_user_on_form_submit' );
}
<?php
// wp-mailchimp-plugin
/**
* Includes that are essential for this plugin
*/
public function includes(){
/** Core **/
require_once MWP_PATH . 'inc/settings.php';
require_once MWP_PATH . 'inc/cpt-form.php';
require_once MWP_PATH . 'inc/class-mwp-form.php';
/** Functions **/
require_once MWP_PATH . 'inc/functions-mailchimp.php';
require_once MWP_PATH . 'inc/functions-form.php';
// ...
}
<?php
// inc/functions-mailchimp.php
/**
* Subscribe the user to a list
* When this function is called, we have already checked everything
* @param number $list_id
* @param string $name
* @param string $email
* @return mixed True if it's a new one or a subscribed one, if not the result array
*/
function mwp_subscribe_user_to_list( $list_id, $name, $email ) {
$api_key = get_option( 'mwp_mailchimp_api_key', false );
if( $api_key ) {
$MailChimp = new MailChimp( $api_key );
$result = $MailChimp->post("lists/" . $list_id . "/members", array(
'email_address' => $email,
'status' => 'pending', // We want them to confirm
'merge_fields' => array( 'FNAME' => $name )
));
// New subscriber
if( isset( $result['id'] ) && isset( $result['email_address'] ) ) {
return true;
}
if( isset( $result[ 'title' ] ) && $result[ 'title' ] == 'Member Exists' ) {
return true;
}
return $result;
}
return false;
}
<?php
//inc/admin/metabox-form.php
/**
* Renders the meta box.
*/
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'form_nonce_save', 'form_nonce' );
$redirect_url = get_post_meta( $post->ID, 'mwp_mailchimp_list_redirect', true );
$list_id_saved = get_post_meta( $post->ID, 'mwp_mailchimp_list_id', true );
// ...
}
<?php
// inc/admin/metabox-form.php
/**
* Renders the meta box.
*/
public function render_metabox( $post ) {
// ...
?>
<!-- ... --->
<tr>
<th>
<?php _e( 'Redirect URL', 'mwp'); ?>
</th>
<td>
<input type="url" name="mwp_mailchimp_list_redirect" placeholder="<?php _e( 'Enter an URL (http://...)', 'mwp' ); ?>" value="<?php echo $redirect_url; ?>"
</td>
</tr>
<!-- ... --->
<?php
}
<?php
// inc/admin/metabox-form.php
/**
* Handles saving the meta box.
*
* @param int $post_id Post ID.
* @param WP_Post $post Post object.
* @return null
*/
public function save_metabox( $post_id, $post ) {
// ...
if( isset( $_POST[ 'mwp_mailchimp_list_redirect' ] ) ) {
update_post_meta( $post_id, 'mwp_mailchimp_list_redirect', esc_url_raw( $_POST[ 'mwp_mailchimp_list_redirect' ], array( 'http', 'https') ) );
}
}
<?php
// inc/class-mwp-form.php
/**
* Class used to render the form
*/
class MWP_Form {
/**
* Form ID
* @var integer
*/
public $form_id = 0;
public function __construct( $id = 0 ) {
$this->form_id = $id;
}
}
<?php
// inc/class-mwp-form.php
/**
* Class used to render the form
*/
class MWP_Form {
// ...
/**
* Rendering the form
* @return void
*/
public function render() {
if( $this->form_id == 0 ) {
return;
}
?>
<div class="mwp-subscribe-form" data-id="<?php echo $this->form_id; ?>">
<form action="" method="POST">
<?php $this->render_errors(); ?>
<input type="text" name="mwp_name" placeholder="<?php _e( 'Your Name', 'mwp' ); ?>"/>
<input type="email" name="mwp_email" placeholder="<?php _e( 'Your Email', 'mwp'); ?>"/>
<button class="button" type="submit" name="mwp_subscribe_form_submit" value="<?php echo $this->form_id; ?>" ><?php _e( 'Subscribe', 'mwp' ); ?></button>
</form>
</div>
<?php
}
}
<?php
// inc/class-mwp-form.php
/**
* Class used to render the form
*/
class MWP_Form {
// ...
/**
* Renders errors if any
* @return void
*/
public function render_errors() {
$errors = mwp_get_form_errors( $this->form_id );
if( is_array( $errors ) && count( $errors ) > 0 ) {
echo '<ul class="errors">';
foreach ($errors as $error) {
echo '<li>' . $error . '</li>';
}
echo '</ul>';
}
}
// ...
}
<?php
// wp-mailchimp-plugin.php
/**
* Create objects, call functions to start various features
*/
public function create() {
// Create only in admin
if( is_admin() ) {
new MWP_Form_Metabox();
}
$this->errors = new WP_Error();
}
<?php
mwp_render_form( 304 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment