ActiveDEMAND tracking script to your website. As well this plugin gives you the ability to add custom popups and use shortcodes to embed ActiveDEMAND webforms and dynamic content blocks into your widgets, pages, posts, and sidebars. * Version: 0.1.13 * Author: JumpDEMAND Inc. * Author URI: https://www.activedemand.com/small-business-marketing-software/ * License:GPL-2.0+ * License URI:http://www.gnu.org/licenses/gpl-2.0.txt */ //--------------- AD Server calls ------------------------------------------------------------------------- function activedemand_getHTML($url, $timeout, $args = array()) { $fields_string = activedemand_field_string($args); if (in_array('curl', get_loaded_extensions())) { $ch = curl_init($url . "?" . $fields_string); // initialize curl with given url curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);//force IP4 $result = curl_exec($ch); curl_close($ch); } elseif (function_exists('file_get_contents')){ $result = file_get_contents($url); } return $result; } function activedemand_postHTML($url, $args, $timeout) { $fields_string = activedemand_field_string($args); error_log("ad: Field STring: ".$fields_string); if (in_array('curl', get_loaded_extensions())) { $ch = curl_init($url); // initialize curl with given url curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // set useragent curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // write the response to a variable // curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // follow redirects if any curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); // max. seconds to execute curl_setopt($ch, CURLOPT_FAILONERROR, 1); // stop when it encounters an error curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);//force IP4 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); $result = curl_exec($ch); if (curl_exec($ch) === false) { error_log('ActiveDEMAND Web Form error: ' . curl_error($ch)); } curl_close($ch); } return $result; } function activedemand_field_string($args) { $options = get_option('activedemand_options_field'); if (array_key_exists('activedemand_appkey', $options)) { $activedemand_appkey = $options["activedemand_appkey"]; } else { $activedemand_appkey = ""; } if ("" != $activedemand_appkey) { $cookievalue = activedemand_get_cookie_value(); $url="http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; if(isset($_SERVER['HTTP_REFERER'])) { $referrer=$_SERVER['HTTP_REFERER']; } else { $referrer=""; } if ($cookievalue != "") { $fields = array( 'api-key' => $activedemand_appkey, 'activedemand_session_guid' => activedemand_get_cookie_value(), 'url' => $url, 'ip_address' => activedemand_get_ip_address(), 'referer' => $referrer ); } else { $fields = array( 'api-key' => $activedemand_appkey, 'url' =>$url, 'ip_address' => activedemand_get_ip_address(), 'referer' => $referrer ); } if (is_array($args)) { $fields = array_merge($fields, $args); } $fields_string = http_build_query($fields); } return $fields_string; } function activedemand_get_cookie_value() { $cookieValue = ""; if (false == strpos($_SERVER['REQUEST_URI'], "wp-admin")) { //not editing an options page etc. if (isset($_COOKIE['activedemand_session_guid'])) { $cookieValue = $_COOKIE['activedemand_session_guid']; } else { $urlParms = $_SERVER['HTTP_HOST']; if (NULL != $urlParms) { $cookieValue = activedemand_get_GUID(); $basedomain = str_replace('www.', "", $urlParms); setcookie('activedemand_session_guid', $cookieValue, time() + (60 * 60 * 24 * 365 * 10), "/", $basedomain); } } } return $cookieValue; } // create a session if one doesn't exist function activedemand_get_GUID() { if (function_exists('com_create_guid')) { return com_create_guid(); } else { mt_srand((double)microtime() * 10000);//optional for php 4.2.0 and up. $charid = strtoupper(md5(uniqid(rand(), true))); $hyphen = chr(45);// "-" $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12); return $uuid; } } // get the ip address function activedemand_get_ip_address() { if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } //--------------- Admin Menu ------------------------------------------------------------------------- function activedemand_menu() { global $activedemand_plugin_hook; $activedemand_plugin_hook = add_options_page('ActiveDEMAND options', 'ActiveDEMAND', 'manage_options', 'activedemand_options', 'activedemand_plugin_options'); add_action('admin_init', 'register_activedemand_settings'); } function register_activedemand_settings() { register_setting('activedemand_options', 'activedemand_options_field'); } function activedemand_enqueue_scripts() { wp_enqueue_script('ActiveDEMAND-Track', 'https://activedemand-static.s3.amazonaws.com/public/javascript/jquery.tracker.compiled.js.gz'); } function activedemand_admin_enqueue_scripts() { global $pagenow; if ('post.php' == $pagenow || 'post-new.php' == $pagenow) { wp_enqueue_script('jquery-ui-dialog'); wp_enqueue_style('wp-jquery-ui-dialog'); } } function activedemand_process_form_shortcode($atts, $content = null) { //[activedemand_form id='123'] $id = ""; //$id exists after this call. extract(shortcode_atts(array('id' => ''), $atts)); $options = get_option('activedemand_options_field'); if (array_key_exists('activedemand_ignore_form_style', $options)) { $activedemand_ignore_form_style = $options['activedemand_ignore_form_style']; } $form_str = ""; if (is_numeric($id)) { $form_str = activedemand_getHTML("https://api.activedemand.com/v1/forms/" . $id, 10, array('form_id' => $id)); if ($activedemand_ignore_form_style) { //strip out the style tag $dom = new DOMDocument(); //avoid the whitespace after removing the node $dom->preserveWhiteSpace = false; //parse html dom elements $dom->loadHTML($form_str); //get the Style from dom if ($style = $dom->getElementsByTagName('style')->item(0)) { //remove the node by telling the parent node to remove the child $style->parentNode->removeChild($style); //remove the class reference from the form element $form = $dom->getElementsByTagName('form')->item(0); $form->setAttribute('class', ''); //save the new document $form_str = $dom->saveHTML(); } } } if ($form_str != "") { //replace \n to ensure WP auto format does not mess with our CSS $form_str = str_replace("\n", "", $form_str); } return $form_str; } function activedemand_process_block_shortcode($atts, $content = null) { //[activedemand_block id='123'] $id = ""; //$id exists after this call. extract(shortcode_atts(array('id' => ''), $atts)); $options = get_option('activedemand_options_field'); if (array_key_exists('activedemand_appkey', $options)) { $activedemand_appkey = $options['activedemand_appkey']; } $block_str = ""; if (is_numeric($id)) { $block_str = activedemand_getHTML("https://api.activedemand.com/v1/smart_blocks/" . $id, 10, array('block_id' => $id)); } if ($block_str != "") { //replace \n to ensure WP auto format does not mess with our CSS $block_str = str_replace("\n", "", $block_str); } return $block_str; } function activedemand_plugin_action_links($links, $file) { static $this_plugin; if (!$this_plugin) { $this_plugin = plugin_basename(__FILE__); } if ($file == $this_plugin) { $settings_link = 'Settings'; array_unshift($links, $settings_link); } return $links; } function activedemand_plugin_options() { $options = get_option('activedemand_options_field'); if (!array_key_exists('activedemand_appkey', $options)) { $options['activedemand_appkey'] = ""; } $activedemand_appkey = $options['activedemand_appkey']; if (!array_key_exists('activedemand_ignore_form_style', $options)) { $options['activedemand_ignore_form_style'] = 0; } if (!array_key_exists('activedemand_defer_script', $options)) { $options['activedemand_defer_script'] = 0; } 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; } if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { $woo_commerce_installed = true; } if (array_key_exists('activedemand_woo_commerce_use_status', $options)) { $activedemand_woo_commerce_use_status = $options["activedemand_woo_commerce_use_status"]; if (!array_key_exists("pending", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["pending"] = FALSE; } if (!array_key_exists("processing", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["processing"] = FALSE; } if (!array_key_exists("on-hold", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["on-hold"] = FALSE; } if (!array_key_exists("completed", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["completed"] = FALSE; } if (!array_key_exists("refunded", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["refunded"] = FALSE; } if (!array_key_exists("cancelled", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["cancelled"] = FALSE; } if (!array_key_exists("failed", $activedemand_woo_commerce_use_status)) { $activedemand_woo_commerce_use_status["failed"] = FALSE; } } else { $activedemand_woo_commerce_use_status = array( "pending" => FALSE, "processing" => FALSE, "on-hold" => FALSE, "completed" => TRUE, "refunded" => FALSE, "cancelled" => FALSE, "failed" => FALSE ); $options["activedemand_woo_commerce_use_status"] = $activedemand_woo_commerce_use_status; } update_option('activedemand_options_field', $options); ?>

Settings

Your ActiveDEMAND Account


You will require an ActiveDEMAND account to use this plugin. With an ActiveDEMAND account you will be able to:

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:

ActiveDEMAND Plugin Options

ActiveDEMAND API Key
Style Forms in WordPress />
Defer Script Loading? />
On WooCommerce Order: "; echo ""; foreach ($form_xml->children() as $child) { echo ""; } echo ""; if (0 != $activedemand_woo_commerce_order_form_id) { ?>
Note: The selected ActiveDEMAND Form must have [First Name]-[Last Name]-[Email Address*]-[Order Value]-[Order State Change]-[Order ID] as the first 6 fields. Ensure that only the [Email Address*] field is required.

Submit Forms to ActiveDEMAND when an WooCommerce order status changes to:
Pending />
Processing />
On Hold />
Completed />
Refunded />
Cancelled />
Failed />

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:
.

Available Web Form Shortcodes

children() as $child) { echo ""; echo ""; } ?>
Form Name Shortcode
"; echo $child->name; echo "[activedemand_form id='"; echo $child->id; echo "']

No Web Forms Configured

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.

Available Dynamic Content Block Shortcodes

children() as $child) { echo ""; echo ""; } ?>
Block Name Shortcode
"; echo $child->name; echo "[activedemand_block id='"; echo $child->id; echo "']

No Dynamic Blocks Configured

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; } 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); } } //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'); ?>