1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239:
<?php
class Inbound_Automation_Trigger_inbound_store_lead_post {
static $trigger;
function __construct() {
self::$trigger = 'inbound_store_lead_post';
add_filter( 'inbound_automation_triggers' , array( __CLASS__ , 'define_trigger' ) , 1 , 1);
add_action( 'activate/automation' , array( __CLASS__ , 'create_dummy_event' ) );
add_action( 'wp_insert_post', array( __CLASS__ , 'simulate_new_lead' ) , 10, 3 );
}
public static function simulate_new_lead( $post_id ) {
global $post_id, $post;
if ( wp_is_post_revision( $post_id )
|| (defined('DOING_AJAX') && DOING_AJAX )
|| ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
) {
return;
}
if (get_post_status($post_id) != 'publish') {
return;
}
remove_action( 'wp_insert_post', array( __CLASS__ , 'simulate_new_lead' ) , 10, 3 );
if ( !isset($post) || $post->post_type != 'wp-lead' ) {
return;
}
$lead = get_post_custom( $post_id );
foreach ( $lead as $key => $value ) {
if (isset($value[0])) {
$lead[$key] = $value[0];
}
}
$lead['id'] = $post_id;
$lead_lists = Inbound_Leads::get_lead_lists_by_lead_id($post_id);
$lead['lead_lists'] = json_encode(array_keys( $lead_lists));
$lead['form_id'] = 'wp-core';
do_action( 'inbound_store_lead_post' , $lead );
}
public static function define_trigger( $triggers ) {
$arguments = apply_filters('trigger/inbound_store_lead_post/args' , array(
'lead_data' => array(
'id' => 'lead_data',
'label' => __( 'Lead Data' , 'inbound-pro' ),
'callback' => array(
get_class() , 'enrich_lead_data'
)
)
) );
$db_lookup_filters = apply_filters( 'trigger/inbound_store_lead_post/db_filters' , array (
array(
'id' => 'lead_data',
'label' => __( 'Validate Lead Data' , 'inbound-pro' ),
'class_name' => 'Inbound_Automation_Query_Lead'
)
));
$actions = apply_filters('trigger/inbound_store_lead_post/actions' , array(
'send_email' ,
'wait' ,
'relay_data' ,
'add_remove_lead_list',
'add_remove_lead_tag',
'create_user',
'kill_lead_tasks',
) );
$triggers[self::$trigger] = array (
'label' => __( 'On add/update lead' , 'inbound-pro' ),
'description' => __( 'This trigger fires whenever a lead added into the Leads database.' , 'inbound-pro' ),
'action_hook' => self::$trigger,
'arguments' => $arguments,
'db_lookup_filters' => $db_lookup_filters,
'actions' => $actions
);
return $triggers;
}
public static function enrich_lead_data( $args ) {
$new_args = array();
$args['form_name'] = isset($args['form_name']) ? $args['form_name'] : '';
$args['inbound_form_name'] = isset($args['form_name']) ? $args['form_name'] : '';
$args['form_id'] = isset($args['form_id']) ? $args['form_id'] : '';
$args['inbound_form_id'] = isset($args['inbound_form_id']) ? $args['inbound_form_id'] : '';
$args['inbound_form_values'] = isset($args['inbound_form_values']) ? $args['inbound_form_values'] : '';
$args['raw_params'] = isset($args['raw_params']) ? $args['raw_params'] : array();
$args['mapped_params'] = isset($args['mapped_params']) ? $args['mapped_params'] : '';
$args['wp_lead_status'] = isset($args['wp_lead_status']) ? $args['wp_lead_status'] : '';
$args['wpleads_raw_post_data'] = '';
foreach ($args as $arg_key => $arg_value) {
if (is_array($arg_value) || is_numeric($arg_key)) {
$arg_value = json_encode($arg_value);
}
if ($arg_key == 'raw_params') {
if (self::is_json($arg_value)) {
$arg_value_array = json_decode($arg_value,true);
} else {
parse_str($arg_value , $arg_value_array);
}
$arg_value_array = (is_array($arg_value_array)) ? $arg_value_array : array();
foreach ($arg_value_array as $raw_key => $raw_value) {
if (is_array($raw_value)) {
$raw_value = json_encode($raw_value);
}
$new_args[$raw_key] = $raw_value;
}
$new_args['raw_params'] = '[parsed]';
}
else if ($arg_key == 'mapped_params') {
parse_str($arg_value , $arg_value_array);
$arg_value_array = (is_array($arg_value_array)) ? $arg_value_array : array();
foreach ($arg_value_array as $mapped_key => $mapped_value) {
if (is_array($mapped_value)) {
$mapped_value = json_encode($mapped_value);
}
$new_args[$mapped_key] = $mapped_value;
}
}
$new_args[$arg_key] = $arg_value;
}
return $new_args;
}
public static function create_dummy_event() {
parse_str( 'wpleads_first_name=Example%20User&wpleads_email_address=test%40inboundnow.com&country-dropdown=US&wpleads_notes=Please%20contact%20me!%20&inbound_form_n=Auto%20Responder%20Form&inbound_form_lists=108&inbound_form_id=2741&inbound_current_page_url=http%3A%2F%2Finboundsoon.dev%2Fcall-to-action-testing%2F&inbound_notify=YXR3ZWxsLnB1Ymxpc2hpbmdAZ21haWwuY29t' , $raw_params );
parse_str( 'wpleads_first_name=Example%20User&wpleads_email_address=test%40inboundnow.com&wpleads_notes=Please%20contact%20me!%20&inbound_form_lists=108' , $mapped_params );
$pageviews = json_encode( stripslashes('{\"95897\":[\"2015/06/01 20:01:49\"]}' ) , true );
$lead = array (
'user_ID' => 2,
'wordpress_date_time' => '2015-06-01 18:03:31 UTC',
'wpleads_email_address' => 'test@inboundnow.com',
'wpleads_first_name' => 'Example',
'wpleads_last_name' => 'User',
'page_id' => 95897,
'page_views' => $pageviews,
'raw_params' => $raw_params ,
'mapped_params' => $mapped_params ,
'url_params' => '{}',
'form_id' => 10,
'variation' => 0,
'source' => 'http://www.inboundnow.com/',
'ip_address' => '127.0.0.1',
'lead_lists' => Array (
'0' => 108
),
'post_type' => 'post',
'id' => 97623
);
$inbound_arguments = Inbound_Options_API::get_option( 'inbound_automation' , 'arguments' );
$inbound_arguments = ( is_array($inbound_arguments) ) ? $inbound_arguments : array();
$inbound_arguments[self::$trigger]['lead_data'] = $lead;
Inbound_Options_API::update_option( 'inbound_automation' , 'arguments' , $inbound_arguments );
}
public static function is_json($string) {
$array = array('{','[');
foreach ($array as $v) {
if (substr($string, 0, 1) === $v) {
return true;
}
}
return false;
}
}
new Inbound_Automation_Trigger_inbound_store_lead_post;