Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: Solana Jewelry Email Subscription
- * Plugin URI: http://www.woothemes.com/woocommerce/
- * Description: An email subscription toolkit that helps users subscribe to storefront.
- * Version: 1.0
- * Author: Allen Rodriguez
- * Author URI: http://psieinteractive.com
- * Requires at least: 4.1
- * Tested up to: 4.4
- * text-domain: solana-jewelry-email-subscribers
- * */
- /* !0. TABLE OF CONTENTS */
- /*
- 1. HOOKS
- 2. SHORTCODES
- 3. FILTERS
- 4. EXTERNAL SCRIPTS
- 5. ACTIONS
- 6. HELPERS
- 7. CUSTOM POST TYPES
- 8. ADMIN PAGES
- 9. SETTINGS
- 10. MISCELLANEOUS
- */
- /* !1. HOOKS */
- add_action('init', 'solj_register_shortcodes');
- //register custom admin column headers
- add_filter('manage_edit-solj_subscriber_columns', 'solj_subscriber_column_headers');
- //register custom admin column data
- add_filter('manage_edit-solj_subscriber_posts_custom_columns', 'solj_subscriber_column_data',1,2);
- //register custom admin list olumn headers
- add_filter('manage_edit-solj_list_columns', 'solj_list_column_headers');
- //register custom admin list column data
- add_filter('manage_edit-solj_list_posts_custom_columns', 'solj_list_column_data',1,2);
- // 1.4
- // hint: register ajax actions
- add_action('wp_ajax_nopriv_solj_save_subscription', 'solj_save_subscription'); // regular website visitor
- add_action('wp_ajax_solj_save_subscription', 'solj_save_subscription'); // admin user
- /* !2. SHORTCODES */
- // registers custom shortcode
- function solj_register_shortcodes() {
- add_shortcode('solj-subscribe-form', 'solj_form_shortcode');
- }
- function solj_form_shortcode ($args, $content="") {
- // get the list id
- $list_id = 0;
- if( isset($args['id'])) $list_id = (int)$args['id'];
- // setup output variable - the form html
- $output = '
- <div class="solj">
- <form id="solj_form" name="solj_form" action="/wp-admin/admin-ajax.php?action=solj_save_subscription" method="post" >
- <input type="hidden" name="solj_list" value = "' . $list_id .'" >
- <p class="solj-input-container" >
- <label>your name</label><br />
- <input type="text" name="solj_fname" placeholder="first name" />
- <input type="text" name="solj_lname" placeholder="last name" />
- </p>
- <p class="solj-input-container" >
- <label>email</label><br />
- <input type="email" name="solj_email" placeholder="ex. you@email.com" />
- </p>';
- if( strlen($content) ):
- $output .= '<div class="solj_content">' . wpautop($content) . '</div>';
- endif;
- //completing form html
- $output .= '<p class="solj-input-container" >
- <input type="submit" name="solj_submit" value="Sign Me Up!" />
- </p>
- </form>
- </div>
- ';
- // return our result/html
- return $output;
- }
- /* !3. FILTERS */
- // 3.6
- function solj_subscriber_column_headers ( $columns ) {
- //creating custom column header data
- $columns = array (
- 'cb'=>'<input type="checkbox" />',
- 'title'=>__('Subscriber Name'),
- 'email'=>__('Email Address'),
- );
- //returning new columns
- return $columns;
- }
- // 3.2
- function solj_subscriber_column_data( $column, $post_id ) {
- // setup our return text
- $output = '';
- switch( $column ) {
- case 'title':
- // get the custom name data
- $fname = get_field('solj_fname', $post_id );
- $lname = get_field('solj_lname', $post_id );
- $output .= $fname .' '. $lname;
- break;
- case 'email':
- // get the custom email data
- $email = get_field('solj_email', $post_id );
- $output .= $email;
- break;
- }
- // echo the output
- echo $output;
- }
- // 3.3
- function solj_list_column_headers ( $columns ) {
- //creating custom column header data
- $columns = array (
- 'cb'=>'<input type="checkbox" />',
- 'title'=>__('List Name'),
- 'shortcode'=>__('Shortcode'),
- );
- //returning new columns
- return $columns;
- }
- //3.4
- function solj_list_column_data( $column, $post_id ) {
- // setup our return text
- $output = '';
- switch( $column ) {
- case 'shortcode':
- $output .= '[solj_form id="'. $post_id .'"]';
- break;
- }
- // echo the output
- echo $output;
- }
- /* !4. EXTERNAL SCRIPTS */
- /* !5. ACTIONS */
- //save subscription data to existing or new subscriber
- function solj_save_subscription() {
- //setup default result data
- $result = array (
- 'status' => 0,
- 'message'=> 'Subscription was not saved. ',
- );
- // array fo storing errors
- $errors = array();
- try {
- // get the list id
- $list_id = (int)$_POST['solj_list'];
- // prepare subscriber data
- $subscriber_data = array (
- 'fname' => esc_attr( $_POST[ 'solj_fname' ] ),
- 'lname' => esc_attr( $_POST[ 'solj_lname' ] ),
- 'email' => esc_attr( $_POST[ 'solj_email' ] ),
- );
- // attempt to create/save object
- $subscriber_id = solj_save_subscription($subscriber_data);
- // IF subscriber was saved successfully $subscriber_id will be greater than 0
- if ($subscriber_id) :
- // IF subscriber already has this subscription
- if (solj_subscriber_has_subscription( $subscriber_id, $list_id)) :
- //get list object
- $list = get_post( $list_id );
- //return detailed error
- $resut['message'] .= esc_attr( $subscriber_data['email'] . ' is already subscribed to ' . $list>post_title . '.');
- else:
- //save new subscription
- $subscription_saved = solj_add_subscription( $subscriber_id, $list_id );
- // IF subscription was saved successfully
- if ( $subscription_saved) :
- // subscription saved
- $result['status']=1;
- $result['message']='subscription saved';
- endif;
- endif;
- endif;
- } catch ( Exception $e) {
- // a php error occurred
- $result['error']= 'Caught exception' . $e->getMessage();
- }
- // return result as json
- solj_return_json($result);
- }
- // 5.2
- // hint: creates a new subscriber or updates and existing one
- function solj_save_subscriber( $subscriber_data ) {
- // setup default subscriber id
- // 0 means the subscriber was not saved
- $subscriber_id = 0;
- try {
- $subscriber_id = solj_get_subscriber_id( $subscriber_data['email'] );
- // IF the subscriber does not already exists...
- if( !$subscriber_id ):
- // add new subscriber to database
- $subscriber_id = wp_insert_post(
- array(
- 'post_type'=>'solj_subscriber',
- 'post_title'=>$subscriber_data['fname'] .' '. $subscriber_data['lname'],
- 'post_status'=>'publish',
- ),
- true
- );
- endif;
- // add/update custom meta data
- update_field(solj_get_acf_key('solj_fname'), $subscriber_data['fname'], $subscriber_id);
- update_field(solj_get_acf_key('solj_lname'), $subscriber_data['lname'], $subscriber_id);
- update_field(solj_get_acf_key('solj_email'), $subscriber_data['email'], $subscriber_id);
- } catch( Exception $e ) {
- // a php error occurred
- }
- // return subscriber_id
- return $subscriber_id;
- }
- // 5.3
- // hint: adds list to subscribers subscriptions
- function solj_add_subscription( $subscriber_id, $list_id ) {
- // setup default return value
- $subscription_saved = false;
- // IF the subscriber does NOT have the current list subscription
- if( !solj_subscriber_has_subscription( $subscriber_id, $list_id ) ):
- // get subscriptions and append new $list_id
- $subscriptions = solj_get_subscriptions( $subscriber_id );
- $subscriptions[]=$list_id;
- // update solj_subscriptions
- update_field( solj_get_acf_key('solj_subscriptions'), $subscriptions, $subscriber_id );
- // subscriptions updated!
- $subscription_saved = true;
- endif;
- // return result
- return $subscription_saved;
- }
- /* !6. HELPERS */
- // 6.1
- // hint: returns true or false
- function solj_subscriber_has_subscription( $subscriber_id, $list_id ) {
- // setup default return value
- $has_subscription = false;
- // get subscriber
- $subscriber = get_post($subscriber_id);
- // get subscriptions
- $subscriptions = solj_get_subscriptions( $subscriber_id );
- // check subscriptions for $list_id
- if( in_array($list_id, $subscriptions) ):
- // found the $list_id in $subscriptions
- // this subscriber is already subscribed to this list
- $has_subscription = true;
- else:
- // did not find $list_id in $subscriptions
- // this subscriber is not yet subscribed to this list
- endif;
- return $has_subscription;
- }
- // 6.2
- // hint: retrieves a subscriber_id from an email address
- function solj_get_subscriber_id( $email ) {
- $subscriber_id = 0;
- try {
- // check if subscriber already exists
- $subscriber_query = new WP_Query(
- array(
- 'post_type' => 'solj_subscriber',
- 'posts_per_page' => 1,
- 'meta_key' => 'solj_email',
- 'meta_query' => array(
- array(
- 'key' => 'solj_email',
- 'value' => $email, // or whatever it is you're using here
- 'compare' => '=',
- ),
- ),
- )
- );
- // IF the subscriber exists...
- if( $subscriber_query->have_posts() ):
- // get the subscriber_id
- $subscriber_query->the_post();
- $subscriber_id = get_the_ID();
- endif;
- } catch( Exception $e ) {
- // a php error occurred
- }
- // reset the Wordpress post object
- wp_reset_query();
- return (int)$subscriber_id;
- }
- // 6.3
- // hint: returns an array of list_id's
- function solj_get_subscriptions( $subscriber_id ) {
- $subscriptions = array();
- // get subscriptions (returns array of list objects)
- $lists = get_field( solj_get_acf_key('solj_subscriptions'), $subscriber_id );
- // IF $lists returns something
- if( $lists ):
- // IF $lists is an array and there is one or more items
- if( is_array($lists) && count($lists) ):
- // build subscriptions: array of list id's
- foreach( $lists as &$list):
- $subscriptions[]= (int)$list->ID;
- endforeach;
- elseif( is_numeric($lists) ):
- // single result returned
- $subscriptions[]= $lists;
- endif;
- endif;
- return (array)$subscriptions;
- }
- // 6.4
- function solj_return_json( $php_array ) {
- // encode result as json string
- $json_result = json_encode( $php_array );
- // return result
- die( $json_result );
- // stop all other processing
- exit;
- }
- //6.5
- // hint: gets the unique act field key from the field name
- function solj_get_acf_key( $field_name ) {
- $field_key = $field_name;
- switch( $field_name ) {
- case 'solj_fname':
- $field_key = 'field_568f22de20e31';
- break;
- case 'solj_lname':
- $field_key = 'field_568f232520e32';
- break;
- case 'solj_email':
- $field_key = 'field_568f233920e33';
- break;
- case 'solj_subscriptions':
- $field_key = 'field_568f235b20e34';
- break;
- }
- return $field_key;
- }
- // 6.6
- // hint: returns an array of subscriber data including subscriptions
- function solj_get_subscriber_data( $subscriber_id ) {
- // setup subscriber_data
- $subscriber_data = array();
- // get subscriber object
- $subscriber = get_post( $subscriber_id );
- // IF subscriber object is valid
- if( isset($subscriber->post_type) && $subscriber->post_type == 'solj_subscriber' ):
- $fname = get_field( solj_get_acf_key('solj_fname'), $subscriber_id);
- $lname = get_field( solj_get_acf_key('solj_lname'), $subscriber_id);
- // build subscriber_data for return
- $subscriber_data = array(
- 'name'=> $fname .' '. $lname,
- 'fname'=>$fname,
- 'lname'=>$lname,
- 'email'=>get_field( solj_get_acf_key('solj_email'), $subscriber_id),
- 'subscriptions'=>solj_get_subscriptions( $subscriber_id )
- );
- endif;
- // return subscriber_data
- return $subscriber_data;
- }
- /* !7. CUSTOM POST TYPES */
- /* !8. ADMIN PAGES */
- /* !9. SETTINGS */
- /* !10. MISCELLANEOUS */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement