initialize(); $this->buildMenu(); } /** * */ public function initialize() { $this->pluginDir = plugin_dir_path(__FILE__); /** * Adds 8digits tracking code to page */ add_action('wp_footer', array($this, 'add8digitsCode')); /** * Product view */ add_action('the_post', array($this, 'view')); register_activation_hook(__FILE__, array($this, 'activate')); register_deactivation_hook(__FILE__, array($this, 'deactivate')); } /** * */ private function buildMenu() { add_action('admin_init', array($this, 'adminInit')); add_action('admin_menu', array($this, 'pluginMenu')); } /** * */ public function activate() { } /** * */ public function deactivate() { } /** * */ public function adminInit() { add_settings_section( 'eightdigits_setting_section', 'Account Settings', array($this, 'accountSettingsSectionRenderer'), '8digits' ); add_settings_field( 'eightdigits_tracking_code', 'Tracking Code', array($this, 'textFieldRenderer'), '8digits', 'eightdigits_setting_section', array( 'id' => 'eightdigits_tracking_code', 'label' => 'Type your Tracking Code here.' ) ); add_settings_field( 'eightdigits_installation_notified', '', array($this, 'hiddenFieldRenderer'), '8digits', 'eightdigits_setting_section', array( 'name' => 'eightdigits_installation_notified', 'default' => '1' ) ); register_setting('8digits', 'eightdigits_tracking_code'); register_setting('8digits', 'eightdigits_installation_notified'); } /** * */ public function pluginMenu() { add_menu_page('8digits', '8digits', 'manage_options', '8digits', array($this, 'optionsPage')); } /** * */ public function optionsPage() { echo '
'; echo '

8digits Options

'; echo '
'; settings_fields('8digits'); do_settings_sections('8digits'); submit_button(); echo '
'; echo '
'; } /** * Renders section header for settings */ public function accountSettingsSectionRenderer() { $trackingCode = get_option('eightdigits_tracking_code'); $output = ''; if (!($trackingCode)) { $output .= '

To activate 8digits, type in your tracking code and save changes.

'; $output .= ''; } else { $output .= '

Run Campaigns

'; $output .= '

Track Campaigns

'; } /** * Renders notification script * This is for 8digits to be notified on new installations * and offer support to find the best ways to run solutions * for your site. */ $notified = get_option('eightdigits_installation_notified'); if (!($notified)) { $adminEmail = get_option('admin_email'); $siteUrl = get_option('siteurl'); $output .= << if(typeof jQuery!="undefined") { addLoadEvent(function() { jQuery.ajax({ url: '$this->_8digitsInterface/wordpress/installed', method: 'POST', dataType: 'jsonp', data: { siteurl: '$siteUrl', email: '$adminEmail' } }) }()); } EOD; update_option('eightdigits_installation_notified', true); } echo $output; } /** * Renders input box for option */ public function textFieldRenderer() { $args = func_get_args(); $args = $args[0]; $id = $args['id']; $label = $args['label']; echo ' ' . $label; } /** * Renders hidden input box for option */ public function hiddenFieldRenderer() { $args = func_get_args(); $args = $args[0]; $name = $args['name']; $defaultVal = $args['default']; $optVal = get_option($name); echo ' '; } /** * Adds 8digits integration code to footer. Also, renders scraping code to get called when 8digits JS SDK is ready. */ public function add8digitsCode() { $trackingCode = get_option('eightdigits_tracking_code'); $output = ''; if($trackingCode) { if($this->_extraCodeBefore) { $output .= $this->_extraCodeBefore; } $version = self::$version; $output .= << var _trackingCode = '$trackingCode'; (function() { var wa = document.createElement('script'); wa.type = 'text/javascript'; wa.async = true; wa.src = '$this->_8digitsStaticInterface/automation.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(wa, s); })(); EOD; } echo $output; } /** * Creates scraping code according to page. Currently, we are handling cart, checkout and congrats pages. * * @param $post */ public function view($post) { global $woocommerce; if(is_shop()) { } else if(is_product_category()) { } else if(is_product_tag()) { } else if(is_product()) { } else if(is_cart()) { $cartItems = $woocommerce->cart->get_cart(); if (empty($cartItems) || !is_array($cartItems)) break; $dataLayer = array( 'price' => "".$woocommerce->cart->total, 'itemCount' => "".$woocommerce->cart->get_cart_contents_count(), 'products' => array() ); $products = array(); foreach($cartItems AS $key => $item) { $product = $item['data']; if (empty($products[$product->get_sku()])) { $terms = get_the_terms( $product->id, 'product_cat' ); $categories = array(); foreach ( $terms as $term ){ $categories[] = $term->name; } // Build all fields the first time we encounter this item. $products[$product->get_sku()] = array( 'name' => $product->get_title(), 'sku' => $product->get_sku(), 'category' => implode('|',$categories), 'price' => (double)number_format($product->get_sale_price(),2,'.',''), 'quantity' => (int)$item['quantity'] ); } else { // If we already have the item, update quantity. $products[$product->get_sku()]['quantity'] += (int)$item['quantity']; } } // Push products into main data array. foreach ($products as $product) { $dataLayer['products'][] = $product; } // Trim empty fields from the final output. foreach ($dataLayer as $key => $value) { if (!is_numeric($value) && empty($value)) unset($dataLayer[$key]); } if (!empty($dataLayer)) { $attributeList = json_encode($dataLayer); $this->_extraCodeBefore = << var EightDigitsData = $attributeList; function EightDigitsReady() { EightDigits.setAttributes($attributeList); setTimeout(function() { EightDigits.event({ key: 'CartDisplayed', noPath: true }); }, 500); } EOF; } } else if(is_checkout()) { $this->_extraCodeBefore = << function EightDigitsReady() { EightDigits.event({ key: 'CheckoutDisplayed', noPath: true }); } var attributeNamesMap = { 'billing_first_name': 'firstName', 'billing_last_name': 'lastName', 'billing_company': 'company', 'billing_email': 'email', 'billing_phone': 'phone' }; jQuery(function() { jQuery('.woocommerce-billing-fields').find('input[type="text"]').on('blur', function() { var el = jQuery(this); var id = el.attr('id'); var value = el.val(); if(attributeNamesMap.hasOwnProperty(id)) { var attributeName = attributeNamesMap[id]; EightDigits.setAttribute({ name: attributeName, value: value }); } }) }) EOF; } else if(is_account_page()) { } else if(is_order_received_page()) { $this->_extraCodeBefore = << function EightDigitsReady() { EightDigits.event({ key: 'OrderReceivedDisplayed', noPath: true }); } EOF; } } } EightDigits::instance(); } ?>