Note: The selected ActiveDEMAND Form must
have [First
Name]-[Last Name]-[Email
Address*]-[Product Data]
as the first 4 fields.
Ensure that the [Product Data] field is a textarea.
Send Stale carts to ActiveDEMAND after it has sat for:
hours
You will require an ActiveDEMAND account to use this plugin. With an
ActiveDEMAND account you will be able
to:
Build Webforms for your pages, posts, sidebars, etc
Build Dynamic Content Blocks for your pages, posts, sidebars, etc
Dynamically swap content based on GEO-IP data
Automatically change banners based on campaign duration
Stop showing forms to people who have already subscribed
Deploy Popups and Subscriber bars
Automatically send emails to those who fill out your web forms
Automatically send emails to you when a form is filled out
Send email campaigns to your subscribers
Build your individual blog posts and have them automatically be posted on a schedule
Bulk import blog posts and have them post on a defined set of times and days
To sign up for your ActiveDEMAND account, click here
You will need to enter your application key in order to enable the form shortcodes. Your can find
your
ActiveDEMAND API key in your account settings:
Using ActiveDEMAND Web Forms and Dynamic Content Blocks
The ActiveDEMAND plugin adds a
tracking script to your
WordPress
pages. This plugin offers the ability to use web form and content block shortcodes on your pages,
posts, and
sidebars
that
will render an ActiveDEMAND Web Form/Dynamic Content block. This allows you to maintain your dynamic
content, form styling, and
configuration
within
ActiveDEMAND.
In your visual editor, look for the 'Insert ActiveDEMAND Shortcode' button: .
To use the ActiveDEMAND web form shortcodes, you will first have to add some Web
Forms
to
your
account in ActiveDEMAND. Once you do have Web Forms configured, the available
shortcodes
will
be
displayed here.
To use the ActiveDEMAND Dynamic Content Block shortcodes, you will first have to add
some Dynamic Content Blocks
to
your
account in ActiveDEMAND. Once you do have Dynamic Blocks configured, the available
shortcodes
will
be
displayed here.
Need help with the ActiveDEMAND plugin?";
$text .= "
Check out the documentation and support forums for help with this plugin.
";
$text .= "Documentation Support forums ActiveDEMAND Support portal";
}
return $text;
}
function get_base_url()
{
return plugins_url(null, __FILE__);
}
function activedemand_register_tinymce_javascript($plugin_array)
{
$plugin_array['activedemand'] = plugins_url('/js/tinymce-plugin.js', __FILE__);
return $plugin_array;
}
function activedemand_buttons()
{
add_filter("mce_external_plugins", "activedemand_add_buttons");
add_filter('mce_buttons', 'activedemand_register_buttons');
}
function activedemand_add_buttons($plugin_array)
{
$plugin_array['activedemand'] = get_base_url() . '/includes/activedemand-plugin.js';
return $plugin_array;
}
function activedemand_register_buttons($buttons)
{
array_push($buttons, 'insert_form_shortcode');
return $buttons;
}
function add_editor()
{
global $pagenow;
// Add html for shortcodes popup
if ('post.php' == $pagenow || 'post-new.php' == $pagenow) {
include 'partials/tinymce-editor.php';
}
}
function activedemand_clean_url($url)
{
$options = get_option('activedemand_options_field');
$defer_script = FALSE;
if(FALSE!=$options)
{
if (array_key_exists('activedemand_defer_script', $options)) {
$defer_script = $options['activedemand_defer_script'];
}
}
if (FALSE === strpos($url, 'jquery.tracker.compiled.js.gz')) { // not our file
return $url;
}
if (TRUE == $defer_script) {
// Must be a ', not "!
return "$url' defer='defer";
}
return $url;
}
//Constant used to track stale carts
define('AD_CARTTIMEKEY', 'ad_last_cart_update');
/**
* Adds cart timestamp to usermeta
*/
function activedemand_woocommerce_cart_update(){
$user_id=get_current_user_id();
update_user_meta($user_id, AD_CARTTIMEKEY, time());
}
add_action('woocommerce_cart_updated', 'activedemand_coocommerce_cart_update');
/**
* Deletes timestamp from current user meta
*/
function activedemand_woocommerce_cart_emptied(){
$user_id=get_current_user_id();
delete_user_meta($user_id, AD_CARTTIMEKEY);
}
add_action('woocommerce_cart_emptied', 'activedemand_woocommerce_cart_emptied');
/**Periodically scans, and sends stale carts to active demand
*
* @global object $wpdb
*
* @uses activedemand_send_stale_carts function to process and send
*/
function activedemand_woocommerce_scan_stale_carts(){
global $wpdb;
$options=get_option('activedemand_options_field');
$hours=$options['woocommerce_stalecart_hours'];
$stale_secs=$hours*60*60;
$carts=$wpdb->get_results('SELECT * FROM '.$wpdb->usermeta.' WHERE meta_key='.AD_CARTTIMEKEY);
$stale_carts=array();
$i=0;
foreach($carts as $cart){
if((time()-(int) $cart->meta_value)>$stale_secs){
$stale_carts[$i]['user_id']=$cart->user_id;
$stale_carts[$i]['cart']= get_user_meta($cart->user_id, '_woocommerce_persistent_cart', TRUE);
}
}
activedemand_send_stale_carts($stale_carts);
}
add_action('activedemand_hourly', 'activedemand_woocommerce_scan_stale_carts');
register_activation_hook(__FILE__,'activedemand_plugin_activation');
function activedemand_plugin_activation(){
if(!wp_next_scheduled('activedemand_hourly')) wp_schedule_event(time(), 'hourly', 'activedemand_hourly');
}
register_deactivation_hook(__FILE__, 'activedemand_plugin_deactivation');
function activedemand_plugin_deactivation(){
wp_clear_scheduled_hook('activedemand_hourly');
}
/**Processes and send stale carts
* Delete the timestamp so carts are only used once
*
* @param array $stale_carts
*
* @used-by activedemand_woocommerce_scan_stale_carts
* @uses function _activedemand_send_stale cart to send each cart individually
*/
function activedemand_send_stale_carts($stale_carts){
foreach($stale_carts as $cart){
$form_data=array();
$user=new WP_User($cart['user_id']);
$form_data['first_name'] = $user->user_firstname;
$form_data['last_name'] = $user->user_lastname;
$form_data['email_address'] = $user->user_email;
$products=$cart['cart']['cart'];
$form_data['product_data']='';
foreach($products as $product){
$product_name=get_the_title($product['product_id']);
$form_data['product_data'].="Product Name: $product_name \n"
. "Product price: ".$product['price'].'\n'
. 'Product Qty: '.$product['quantity'].'\n'
. 'Total: '.$product['line_total'].'\n\n';
}
_activedemand_send_stale_cart($form_data);
delete_user_meta($user->ID, AD_CARTTIMEKEY);
}
}
/**Sends individual carts to activedemand form
*
* @param array $form_data
*/
function _activedemand_send_stale_cart($form_data){
$options = get_option('activedemand_options_field');
$activedemand_form_id = $options["activedemand_woocommerce_stalecart_form_id"];
$form_str = activedemand_getHTML("https://api.activedemand.com/v1/forms/fields.xml", 10, array('form_id' => $activedemand_form_id));
$form_xml = simplexml_load_string($form_str);
if ($form_xml->children()->count() >= 4) {
$fields = array();
$i = 0;
foreach ($form_xml->children() as $child) {
if (!array_key_exists(urlencode($child->key), $fields)) {
$fields[urlencode($child->key)] = array();
}
switch ($i) {
case 0:
array_push($fields[urlencode($child->key)], $form_data['first_name']);
break;
case 1:
array_push($fields[urlencode($child->key)], $form_data['last_name']);
break;
case 2:
array_push($fields[urlencode($child->key)], $form_data['email_address']);
break;
case 3:
array_push($fields[urlencode($child->key)], $form_data['product_data']);
break;
}
$i++;
}
activedemand_postHTML("https://api.activedemand.com/v1/forms/" . $activedemand_form_id, $fields, 5);
}
}
function activedemand_woocommerce_order_status_changed($order_id, $order_status_old, $order_status_new)
{
//post that this person has reviewed their account page.
$options = get_option('activedemand_options_field');
if (array_key_exists('activedemand_appkey', $options)) {
$activedemand_appkey = $options["activedemand_appkey"];
}
if (array_key_exists('activedemand_woo_commerce_use_status', $options)) {
$activedemand_woo_commerce_use_status = $options["activedemand_woo_commerce_use_status"];
} else {
$activedemand_woo_commerce_use_status = array('none' => 'none');
}
if (array_key_exists('activedemand_woo_commerce_order_form_id', $options)) {
$activedemand_woo_commerce_order_form_id = $options["activedemand_woo_commerce_order_form_id"];
} else {
$activedemand_woo_commerce_order_form_id = "0";
}
$execute_form_submit = ("" != $activedemand_appkey) && ("0" != $activedemand_woo_commerce_order_form_id) && ("" != $activedemand_woo_commerce_order_form_id) && array_key_exists($order_status_new, $activedemand_woo_commerce_use_status);
if ($execute_form_submit) {
$execute_form_submit = $activedemand_woo_commerce_use_status[$order_status_new];
}
//we need an email address and a form ID
if ($execute_form_submit) {
$order = new WC_Order($order_id);
$user_id = (int)$order->get_user_id();
if (0 == $user_id) {
$first_name = $order->billing_first_name;
$last_name = $order->billing_last_name;
$email_address = $order->billing_email;
} else {
$guest = FALSE;
$current_user = get_userdata($user_id);
$first_name = $current_user->user_firstname;
$last_name = $current_user->user_lastname;
$email_address = $current_user->user_email;
}
if (("" != $email_address) && ('0' != $activedemand_woo_commerce_order_form_id)) {
$form_str = $form_str = activedemand_getHTML("https://api.activedemand.com/v1/forms/fields.xml", 10, array('form_id' => $activedemand_woo_commerce_order_form_id));
$form_xml = simplexml_load_string($form_str);
if ("" != $form_xml) {
if ($form_xml->children()->count() >= 6) {
$fields = array();
$i = 0;
foreach ($form_xml->children() as $child) {
if (!array_key_exists(urlencode($child->key), $fields)) {
$fields[urlencode($child->key)] = array();
}
switch ($i) {
case 0:
array_push($fields[urlencode($child->key)], $first_name);
break;
case 1:
array_push($fields[urlencode($child->key)], $last_name);
break;
case 2:
array_push($fields[urlencode($child->key)], $email_address);
break;
case 3:
array_push($fields[urlencode($child->key)], $order->get_total());
break;
case 4:
array_push($fields[urlencode($child->key)], $order_status_new);
break;
case 5:
array_push($fields[urlencode($child->key)], $order_id);
break;
}
$i++;
}
activedemand_postHTML("https://api.activedemand.com/v1/forms/" . $activedemand_woo_commerce_order_form_id, $fields, 5);
}
} else {
// error_log("no form fields");
}
//$order_status_new;
}
} else {
// error_log("Not Processing ADForm Submit");
}//execute form submit
}
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
$options = get_option('activedemand_options_field');
//check to see if we have an API key, if we do not, zero integration is possible
$activedemand_appkey = "";
if (is_array($options) && array_key_exists('activedemand_appkey', $options)) {
$activedemand_appkey = $options["activedemand_appkey"];
}
if ("" != $activedemand_appkey) {
add_action('woocommerce_order_status_changed', 'activedemand_woocommerce_order_status_changed', 10, 3);
}
}
function activedemand_prefilter_sc($content){
if(!defined('W3TC_DYNAMIC_SECURITY') || !function_exists('mfunc')) return $content;
$output=$content;
$shortcodes=array('activedemand_form','activedemand_block','fakecode');
foreach($shortcodes as $sc){
$output=preg_replace("/\[$sc(.*?)?\]/", "'
. '', $content);
}
return $output;
}
add_filter('the_content', 'activedemand_prefilter_sc',1);
add_filter('widget_text', 'activedemand_prefilter_sc');
//defer our script loading
add_filter('clean_url', 'activedemand_clean_url', 11, 1);
add_filter('contextual_help', 'activedemand_plugin_help', 10, 3);
add_action('wp_enqueue_scripts', 'activedemand_enqueue_scripts');
add_action('admin_enqueue_scripts', 'activedemand_admin_enqueue_scripts');
add_shortcode('activedemand_form', 'activedemand_process_form_shortcode');
add_shortcode('activedemand_block', 'activedemand_process_block_shortcode');
add_action('admin_menu', 'activedemand_menu');
add_filter('plugin_action_links', 'activedemand_plugin_action_links', 10, 2);
//widgets
// add new buttons
add_action('init', 'activedemand_buttons');
add_action('in_admin_footer', 'add_editor');
?>