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:
<?php
if ( !class_exists( 'Inbound_Automation_Action_Relay_Data' ) ) {
class Inbound_Automation_Action_Relay_Data {
function __construct() {
add_filter( 'inbound_automation_actions' , array( __CLASS__ , 'define_action' ) , 1 , 1);
}
public static function define_action( $actions ) {
$actions['relay_data'] = array (
'class_name' => get_class(),
'id' => 'relay_data',
'label' => __( 'Relay Data', 'marketing-automation') ,
'description' => __('Send data intercepted by Trigger to extenal URL using POST or GET methods.' , 'inbound-pro' ),
'settings' => array (
array (
'id' => 'send_method',
'label' => 'Send Method',
'type' => 'dropdown',
'default' => 'POST',
'description' => __( 'Select which protocol we want to use when sending data to a remote location.' , 'inbound-pro' ),
'options' => array (
'POST' => __('POST (post variables directly to URL)', 'inbound-pro' ),
'GET' => __('GET (append parameters to URL)','inbound-pro' ),
'JSON-POST' => __('JSON (Post JSON packet directly to URL) ', 'inbound-pro' ),
)
),
array (
'id' => 'destination',
'label' => __('Destination URL:', 'inbound-pro' ),
'type' => 'text',
'description' => __( 'Input the remote API or remote webhook here. ' , 'inbound-pro' ),
'default' => 'http://www.somesite.com/somescript.php'
)
)
);
return $actions;
}
public static function run_action( $action , $arguments ) {
$final_args = $arguments;
if (isset($arguments['object_id'])) {
$ID = $arguments['object_id'];
$final_args['postmeta'] = get_post_meta($ID);
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $ID ), 'single-post-thumbnail' );
$final_args['featured_image'] = (isset($image[0])) ? $image[0] : '';
$final_args['the_content'] = do_shortcode(get_the_content($ID));
$final_args['post'] = get_post($ID , ARRAY_A );
}
$ch = curl_init();
switch ($action['send_method']) {
case 'POST' :
curl_setopt($ch, CURLOPT_URL, $action['destination'] );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS , http_build_query($final_args) );
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
break;
case 'GET' :
$query = http_build_query($final_args, '', '&');
curl_setopt($ch, CURLOPT_URL, $action['destination'] . '?' . $query );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
break;
case 'JSON-POST' :
$data_string = json_encode( $final_args );
curl_setopt($ch, CURLOPT_URL, $action['destination'] );
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
break;
}
$return = curl_exec($ch);
if(!$return) {
$action_encoded = json_encode($action) ;
inbound_record_log( 'Action Event - Sending Data to External URL - Error' , '<h2>Settings</h2><pre>'.'Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch).'</pre> <h2>Settings</h2><pre>'. $action_encoded.'</pre> <h2>Arguments</h2><pre>' . json_encode($arguments) . '</pre>', $action['rule_id'] , 'action_event' );
}
curl_close($ch);
$action_encoded = json_encode($action) ;
inbound_record_log( 'Action Event - Sending Data to External URL' , '<h2>Server Response</h2><pre>'. $return .'</pre> <h2>Settings</h2><pre>'. $action_encoded .'</pre> <h2>Arguments</h2><pre>' . json_encode($arguments) . '</pre>', $action['rule_id'] , 'action_event' );
}
}
new Inbound_Automation_Action_Relay_Data();
}