Last active
August 29, 2015 14:17
-
-
Save atwellpub/b1267b209f8150c21da4 to your computer and use it in GitHub Desktop.
This document shows an example plugin that will extend Inbound Forms to be able to add a lead to a custom taxonomy on form submission. It also shows how to create a custom field type that provides a dropdown of available taxonomy terms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Inbound Now - Customizations (Do not disable) | |
Plugin URI: http://www.inboundnow.com/ | |
Description: Helps add leads to custom taxonomies | |
Author: Hudson Atwell | |
Version: 1.0.0 | |
Author URI: http://www.twitter.com/atwellpub | |
Text Domain: inbound-pro | |
Domain Path: /lang/ | |
*/ | |
class Inbound_Customizations { | |
/** | |
* Initialize Class | |
*/ | |
public function __construct() { | |
self::add_hooks(); | |
} | |
/** | |
* Load hooks and filters | |
*/ | |
public static function add_hooks() { | |
/* add taxonomies */ | |
add_action('init', array( __CLASS__ , 'add_taxonomies' ), 10 ); | |
/* extend form settings */ | |
add_filter('inboundnow_forms_settings' , array( __CLASS__ , 'extend_form_settings' ) ); | |
/* Add handler to process unit type field type */ | |
add_filter( 'inbound_form_custom_field' , array( __CLASS__ , 'inbound_form_custom_field' ), 10, 3 ); | |
/* Add form processing hander */ | |
add_action('inboundnow_form_submit_actions' , array( __CLASS__ , 'process_forms' ) , 10 , 2 ); | |
/* Filter by taxonomy */ | |
add_filter('parse_query', array( __CLASS__ , 'filter_leads' ) ); | |
} | |
/** | |
* Add taxonomy | |
* | |
*/ | |
function add_taxonomies() { | |
self::register_agents(); | |
self::register_sources(); | |
self::register_status(); | |
self::register_type(); | |
self::register_unit_type(); | |
} | |
/** | |
* Register agents taxonomy | |
*/ | |
public static function register_agents() { | |
/* Agents */ | |
register_taxonomy('wplead_list_agents','wp-lead', array( | |
'hierarchical' => true, | |
'label' => __( 'Agents' , 'cta' ), | |
'singular_label' => __( 'Agents' , 'cta' ), | |
'show_ui' => true, | |
'show_in_nav_menus' => false, | |
'query_var' => true, | |
"rewrite" => true | |
)); | |
} | |
/** | |
* Register sources taxonomy | |
*/ | |
public static function register_sources() { | |
register_taxonomy('wplead_list_source','wp-lead', array( | |
'hierarchical' => true, | |
'label' => __( 'Sources' , 'cta' ), | |
'singular_label' => __( 'Source' , 'cta' ), | |
'show_ui' => true, | |
'show_in_nav_menus' => false, | |
'query_var' => true, | |
"rewrite" => true | |
)); | |
} | |
/** | |
* Register status taxonomy | |
*/ | |
public static function register_status() { | |
register_taxonomy('wplead_list_status','wp-lead', array( | |
'hierarchical' => true, | |
'label' => __( 'Statuses' , 'cta' ), | |
'singular_label' => __( 'Status' , 'cta' ), | |
'show_ui' => true, | |
'show_in_nav_menus' => false, | |
'query_var' => true, | |
"rewrite" => true | |
)); | |
} | |
/** | |
* Register type taxonomy | |
*/ | |
public static function register_type() { | |
register_taxonomy('wplead_list_types','wp-lead', array( | |
'hierarchical' => true, | |
'label' => __( 'Types' , 'cta' ), | |
'singular_label' => __( 'Type' , 'cta' ), | |
'show_ui' => true, | |
'show_in_nav_menus' => false, | |
'query_var' => true, | |
"rewrite" => true | |
)); | |
} | |
/** | |
* Register status taxonomy | |
*/ | |
public static function register_unit_type() { | |
register_taxonomy('wplead_list_unit_types','wp-lead', array( | |
'hierarchical' => true, | |
'label' => __( 'Unit Types' , 'cta' ), | |
'singular_label' => __( 'Unit Type' , 'cta' ), | |
'show_ui' => true, | |
'show_in_nav_menus' => false, | |
'query_var' => true, | |
"rewrite" => true | |
)); | |
} | |
/** | |
* Extend Form Settings | |
* | |
*/ | |
function extend_form_settings( $settings ) { | |
$settings['forms']['options']['add_to_agents'] = array( | |
'name' => __('Add to Agent', 'leads'), | |
'desc' => __('Assign lead to agent', 'leads'), | |
'type' => 'select', | |
'options' => self::get_agents(), | |
'class' => 'main-form-settings', | |
); | |
$settings['forms']['options']['add_to_source'] = array( | |
'name' => __('Define Source', 'leads'), | |
'desc' => __('Assign source to lead', 'leads'), | |
'type' => 'select', | |
'options' => self::get_sources(), | |
'class' => 'main-form-settings', | |
); | |
$settings['forms']['options']['add_to_status'] = array( | |
'name' => __('Set Initial Status', 'leads'), | |
'desc' => __('Set initial status of lead', 'leads'), | |
'type' => 'select', | |
'options' => self::get_statuses(), | |
'class' => 'main-form-settings', | |
); | |
/* Adding taxonomy dropdown field type */ | |
$settings['forms']['child']['options']['field_type']['options']['dropdown_unit_type'] = __("Dropdown - Unit Type Taxonomy", "inbound-pro"); | |
return $settings; | |
} | |
/** | |
* Listens for attachment uploader field type and renders it | |
*/ | |
public static function inbound_form_custom_field( $form, $field, $form_id) { | |
/* only render field if 'attachments' type is selected. */ | |
if ( !$field || $field['type'] != 'dropdown_unit_type'){ | |
return; | |
} | |
/* get attachment settings */ | |
$args = array( 'orderby'=>'asc' , 'hide_empty'=> false ); | |
$unit_types = get_terms( array('wplead_list_unit_types'), $args); | |
$options = array(); | |
foreach($unit_types as $type){ | |
$options[$type->term_id] = $type->name; | |
} | |
$form .= '<select name="unit-type" class="custom-dropdown" '.$field['required'].'>'; | |
$form .= '<option value="" disabled selected>Please select a unit type</option>'; | |
foreach ($options as $key => $value) { | |
$form .= '<option value="'. $key .'">'. $value .'</option>'; | |
} | |
$form .= '</select>'; | |
return $form; | |
} | |
/** | |
* Get Agents | |
*/ | |
function get_agents() { | |
$args = array( 'hide_empty' => false ); | |
$terms = get_terms( 'wplead_list_agents' , $args ); | |
$array = array(); | |
$array[''] = 'Please select option'; | |
foreach ($terms as $term) { | |
$array[$term->term_id] = $term->name; | |
} | |
return $array; | |
} | |
/** | |
* Get Sourcess | |
*/ | |
function get_sources() { | |
$args = array( 'hide_empty' => false ); | |
$terms = get_terms( 'wplead_list_source' , $args ); | |
$array = array(); | |
$array[''] = 'Please select option'; | |
foreach ($terms as $term) { | |
$array[$term->term_id] = $term->name; | |
} | |
return $array; | |
} | |
/** | |
* Get Statuses | |
*/ | |
function get_statuses() { | |
$args = array( 'hide_empty' => false ); | |
$terms = get_terms( 'wplead_list_status' , $args ); | |
$array = array(); | |
$array[''] = 'Please select option'; | |
foreach ($terms as $term) { | |
$array[$term->term_id] = $term->name; | |
} | |
return $array; | |
} | |
/** | |
* Get Types | |
*/ | |
function get_types() { | |
$args = array( 'hide_empty' => false ); | |
$terms = get_terms( 'wplead_list_types' , $args ); | |
$array = array(); | |
$array[''] = 'Please select option'; | |
foreach ($terms as $term) { | |
$array[$term->term_id] = $term->name; | |
} | |
return $array; | |
} | |
/** | |
* Get Statuses | |
*/ | |
function get_unit_types() { | |
$args = array( 'hide_empty' => false ); | |
$terms = get_terms( 'wplead_list_unit_types' , $args ); | |
$array = array(); | |
$array[''] = 'Please select option'; | |
foreach ($terms as $term) { | |
$array[$term->term_id] = $term->name; | |
} | |
return $array; | |
} | |
/** | |
* Process Form | |
* | |
*/ | |
function process_forms ( $form_post_data , $form_meta_data ) { | |
$form_settings = $form_meta_data['inbound_form_values'][0]; | |
parse_str($form_settings, $form_settings); | |
/* get lead by email */ | |
$lead_id = Inbound_API::leads_get_id_from_email( $form_post_data['wpleads_email_address'] ); | |
/* assign lead to agent */ | |
if (isset($form_settings['inbound_shortcode_add_to_agents'])) { | |
/* Add agent to lead */ | |
wp_set_post_terms( $lead_id , $form_settings['inbound_shortcode_add_to_agents'] , 'wplead_list_agents' , true ); | |
} | |
/* assign lead to Source */ | |
if (isset($form_settings['inbound_shortcode_add_to_source'])) { | |
/* Add agent to lead */ | |
wp_set_post_terms( $lead_id , $form_settings['inbound_shortcode_add_to_source'] , 'wplead_list_source' , true ); | |
} | |
/* assign lead to Status */ | |
if (isset($form_settings['inbound_shortcode_add_to_status'])) { | |
/* Add agent to lead */ | |
wp_set_post_terms( $lead_id , $form_settings['inbound_shortcode_add_to_status'] , 'wplead_list_status' , true ); | |
} | |
/* assign lead to Unit Type - legacy method | |
if (isset($form_settings['inbound_shortcode_add_to_unit_type'])) { | |
wp_set_post_terms( $lead_id , $form_settings['inbound_shortcode_add_to_unit_type'] , 'wplead_list_unit_types' , true ); | |
} | |
*/ | |
/* determin unit type if applicable */ | |
print_r($form_post_data);exit; | |
if (isset($form_post_data['unit-type'])) { | |
/* Add agent to unit type */ | |
wp_set_post_terms( $lead_id , $form_post_data['unit-type'] , 'wplead_list_unit_types' , true ); | |
} | |
//print_r($form_post_data);exit; | |
} | |
/** | |
* Filters leads by custom taxonomy | |
*/ | |
public static function filter_leads( $query ) { | |
global $pagenow; | |
$qv = &$query->query_vars; | |
if ( $pagenow != 'edit.php' || !isset($qv['post_type']) || $qv['post_type'] != 'wp-lead' ) { | |
return; | |
} | |
$taxonomy = array('wplead_list_agents','wplead_list_source','wplead_list_status','wplead_list_types','wplead_list_unit_types'); // change to your taxonomy | |
foreach ($taxonomy as $tax) { | |
if ( isset($_GET[ $tax ]) && $_GET[ $tax ] ) { | |
$term = get_term_by('id',$qv[$tax],$tax); | |
$qv[$tax] = $term->slug; | |
} | |
} | |
} | |
} | |
new Inbound_Customizations; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment