. */ /** * If this file is called directly, abort. */ if (!defined('WPINC')) { die; } if (!defined('ABSPATH')) { exit; } /** * Plugin versionnumber, please do not override. * Define some constants */ define( 'WOOCOMMERCESEA_PLUGIN_VERSION', '13.1.2' ); define( 'WOOCOMMERCESEA_PLUGIN_NAME', 'woocommerce-product-feed-pro' ); define( 'WOOCOMMERCESEA_PLUGIN_NAME_SHORT', 'woo-product-feed-pro' ); if ( ! defined( 'WOOCOMMERCESEA_FILE' ) ) { define( 'WOOCOMMERCESEA_FILE', __FILE__ ); } if ( ! defined( 'WOOCOMMERCESEA_PATH' ) ) { define( 'WOOCOMMERCESEA_PATH', plugin_dir_path( WOOCOMMERCESEA_FILE ) ); } if ( ! defined( 'WOOCOMMERCESEA_BASENAME' ) ) { define( 'WOOCOMMERCESEA_BASENAME', plugin_basename( WOOCOMMERCESEA_FILE ) ); } if ( ! defined( 'WOOCOMMERCESEA_PLUGIN_URL' ) ) { define( 'WOOCOMMERCESEA_PLUGIN_URL', plugins_url() . '/' . WOOCOMMERCESEA_PLUGIN_NAME_SHORT ); } /** * Enqueue css assets */ function woosea_styles($hook) { // Only register and enqueue CSS scripts from within the plugin itself if (preg_match("/product-feed-pro/i",$hook)){ wp_register_style( 'woosea_admin-css', plugins_url( '/css/woosea_admin.css', __FILE__ ), '',WOOCOMMERCESEA_PLUGIN_VERSION ); wp_enqueue_style( 'woosea_admin-css' ); wp_register_style( 'woosea_jquery_ui-css', plugins_url( '/css/jquery-ui.css', __FILE__ ), '',WOOCOMMERCESEA_PLUGIN_VERSION ); wp_enqueue_style( 'woosea_jquery_ui-css' ); wp_register_style( 'woosea_jquery_typeahead-css', plugins_url( '/css/jquery.typeahead.css', __FILE__ ), '',WOOCOMMERCESEA_PLUGIN_VERSION ); wp_enqueue_style( 'woosea_jquery_typeahead-css' ); } } add_action( 'admin_enqueue_scripts' , 'woosea_styles' ); /** * Strip slashes from POST requests */ function stripslashes_recursive($object) { return is_array($object) ? array_map('stripslashes_recursive', $object) : stripslashes($object); } /** * Enqueue js assets admin pages */ function woosea_scripts($hook) { // Enqueue Jquery wp_enqueue_script('jquery'); wp_enqueue_script('jquery-ui-dialog'); wp_enqueue_script('jquery-ui-calender'); wp_enqueue_script('jquery-ui-datepicker'); // Only register and enqueue JS scripts from within the plugin itself if (preg_match("/product-feed-pro/i",$hook)){ // Bootstrap typeahead wp_register_script( 'typeahead-js', plugin_dir_url( __FILE__ ) . 'js/woosea_typeahead.js', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'typeahead-js' ); // JS for adding input field validation wp_register_script( 'woosea_validation-js', plugin_dir_url( __FILE__ ) . 'js/woosea_validation.js', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_validation-js' ); // JS for autocomplete wp_register_script( 'woosea_autocomplete-js', plugin_dir_url( __FILE__ ) . 'js/woosea_autocomplete.js', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_autocomplete-js' ); // JS for adding table rows to the rules page wp_register_script( 'woosea_rules-js', plugin_dir_url( __FILE__ ) . 'js/woosea_rules.js', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_rules-js' ); // JS for adding table rows to the field mappings page wp_register_script( 'woosea_field_mapping-js', plugin_dir_url( __FILE__ ) . 'js/woosea_field_mapping.js', '', WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_field_mapping-js' ); // JS for getting channels wp_register_script( 'woosea_channel-js', plugin_dir_url( __FILE__ ) . 'js/woosea_channel.js', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_channel-js' ); // JS for managing keys wp_register_script( 'woosea_key-js', plugin_dir_url( __FILE__ ) . 'js/woosea_key.js', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_key-js' ); } // JS for manage projects page wp_register_script( 'woosea_manage-js', plugin_dir_url( __FILE__ ) . 'js/woosea_manage.js?yo=12', '',WOOCOMMERCESEA_PLUGIN_VERSION, true ); wp_enqueue_script( 'woosea_manage-js' ); } add_action( 'admin_enqueue_scripts' , 'woosea_scripts' ); /** * Internationalisation of plugin */ function woosea_load_plugin_textdomain() { load_plugin_textdomain( 'woo-product-feed-pro', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' ); } add_action( 'plugins_loaded', 'woosea_load_plugin_textdomain' ); /** * Required classes */ require plugin_dir_path(__FILE__) . 'classes/class-admin-pages-template.php'; require plugin_dir_path(__FILE__) . 'classes/class-cron.php'; require plugin_dir_path(__FILE__) . 'classes/class-get-products.php'; require plugin_dir_path(__FILE__) . 'classes/class-admin-notifications.php'; require plugin_dir_path(__FILE__) . 'classes/class-update-channel.php'; require plugin_dir_path(__FILE__) . 'classes/class-attributes.php'; require plugin_dir_path(__FILE__) . 'classes/class-google-remarketing.php'; require plugin_dir_path(__FILE__) . 'classes/class-caching.php'; /** * Add links to the plugin page */ function woosea_plugin_action_links($links, $file) { if (!isset($this_plugin)) { $this_plugin = plugin_basename(__FILE__); } // check to make sure we are on the correct plugin if ($file == $this_plugin) { $host = ''; if (!empty($_SERVER['HTTP_HOST'])) { $host = $_SERVER['HTTP_HOST']; $host = sanitize_text_field($_SERVER['HTTP_HOST']); } $plugin_links[] = 'Support'; $plugin_links[] = 'Tutorials'; $plugin_links[] = 'Go Elite'; $plugin_links[] = 'Premium Support'; // add the links to the list of links already there foreach($plugin_links as $link) { if(is_array($links)){ array_unshift($links, $link); } } } return $links; } add_filter('plugin_action_links', 'woosea_plugin_action_links', 10, 2); /** * Get category path for Facebook pixel */ function woosea_get_term_parents( $id, $taxonomy, $project_taxonomy, $link = false, $nicename = false, $visited = array() ) { // Only add Home to the beginning of the chain when we start buildin the chain if(empty($visited)){ $chain = 'Home'; } else { $chain = ''; } $parent = get_term( $id, $taxonomy ); $separator = ' > '; if ( is_wp_error( $parent ) ) return $parent; if($parent){ if ($nicename){ $name = $parent->slug; } else { $name = $parent->name; } if ($parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited, TRUE )){ $visited[] = $parent->parent; $chain .= woosea_get_term_parents( $parent->parent, $taxonomy, $separator, $link = false, $nicename, $visited ); } if ($link){ $chain .= $separator.$name; } else { $chain .= $separator.$name; } } return $chain; } /** * Add Facebook pixel */ function woosea_add_facebook_pixel( $product = null ){ if ( ! is_object( $product ) ) { global $product; } $fb_pagetype = WooSEA_Google_Remarketing::woosea_google_remarketing_pagetype(); $add_facebook_pixel = get_option ('add_facebook_pixel'); $add_facebook_capi = get_option ('add_facebook_capi'); $viewContent = ""; $event_id = uniqid (rand (),true); $currency = get_woocommerce_currency(); if($add_facebook_pixel == "yes"){ $facebook_pixel_id = get_option("woosea_facebook_pixel_id"); $facebook_capi_token = get_option("woosea_facebook_capi_token"); // Add vulnerability check if(!is_numeric($facebook_pixel_id)){ unset($facebook_pixel_id); } if(isset($facebook_pixel_id) AND ($facebook_pixel_id > 0)){ // Set Facebook conversion API data define('FACEBOOK_APP_ACCESS_TOKEN', $facebook_capi_token); define('FACEBOOK_PIXEL_OFFLINE_EVENT_SET_ID', $facebook_pixel_id); $fb_capi_data["match_keys"] = array(); $fb_capi_data["event_time"] = time(); $fb_capi_data["event_id"] = $event_id; $fb_capi_data["user_data"]["client_ip_address"] = WC_Geolocation::get_ip_address(); if(!isset( $_SERVER['HTTP_USER_AGENT'] ) ) { $_SERVER['HTTP_USER_AGENT'] = "Unknown"; } $fb_capi_data["user_data"]["client_user_agent"] = sanitize_text_field($_SERVER['HTTP_USER_AGENT']); $fb_capi_data["action_source"] = "website"; $fb_capi_data["event_source_url"] = sanitize_text_field(home_url($_SERVER['REQUEST_URI'])); if ($fb_pagetype == "product"){ if (!empty($product->get_price())) { $fb_prodid = get_the_id(); $product_name = $product->get_name(); $product_name = str_replace("\"","",$product_name); $product_name = str_replace("'","",$product_name); $cats = ""; $all_cats = get_the_terms( $fb_prodid, 'product_cat' ); if(!empty($all_cats)){ foreach ($all_cats as $key => $category) { $cats .= $category->name.","; } } // strip last comma $cats = rtrim($cats, ","); $cats = str_replace("&","&", $cats); $cats = str_replace("\"","",$cats); $cats = str_replace("'","",$cats); if(!empty($fb_prodid)){ if(!$product) { return -1; } if ( $product->is_type( 'variable' ) ) { // We should first check if there are any _GET parameters available // When there are not we are on a variable product page but not on a specific variable one // In that case we need to put in the AggregateOffer structured data $variation_id = woosea_find_matching_product_variation( $product, sanitize_text_field($_GET) ); $nr_get = count($_GET); // This is a variant product if(($nr_get > 0) AND ($variation_id > 0)){ $variable_product = wc_get_product($variation_id); // for variants use the variation_id and not the item_group_id // otherwise Google will disapprove the items due to itemID mismatches $fb_prodid = $variation_id; if(is_object( $variable_product ) ) { $product_price = $variable_product->get_price(); $fb_price = $product_price; } else { // AggregateOffer $prices = $product->get_variation_prices(); $lowest = reset( $prices['price'] ); $highest = end( $prices['price'] ); if ( $lowest === $highest ) { $fb_price = wc_format_localized_price( $lowest ); } else { $fb_lowprice = wc_format_localized_price( $lowest ); $fb_highprice = wc_format_localized_price( $highest ); $fb_price = $fb_lowprice; } } $fb_price = floatval(str_replace(',', '.', str_replace(',', '.', $fb_price))); $viewContent = "fbq(\"track\",\"ViewContent\",{content_category:\"$cats\", content_name:\"$product_name\", content_type:\"product\", content_ids:[\"$fb_prodid\"], value:\"$fb_price\", currency:\"$currency\"},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "ViewContent"; $fb_capi_data["custom_data"]["content_ids"] = $fb_prodid; $fb_capi_data["custom_data"]["content_name"] = $product_name; $fb_capi_data["custom_data"]["content_category"] = $cats; $fb_capi_data["custom_data"]["currency"] = $currency; $fb_capi_data["custom_data"]["value"] = $fb_price; $fb_capi_data["custom_data"]["content_type"] = "product"; } else { // This is a parent variable product // Since these are not allowed in the feed, at the variations product ID's // Get children product variation IDs in an array $woosea_content_ids = "variation"; $woosea_content_ids = get_option( 'add_facebook_pixel_content_ids' ); if($woosea_content_ids == "variation"){ $children_ids = $product->get_children(); $content = ""; foreach ($children_ids as $id){ $content .= '\''.$id.'\','; //$content .= $id.','; } } else { $content = '\''.$fb_prodid.'\''; } $content = rtrim($content, ","); $prices = $product->get_variation_prices(); $lowest = reset( $prices['price'] ); $highest = end( $prices['price'] ); if ( $lowest === $highest ) { $fb_price = wc_format_localized_price( $lowest ); } else { $fb_lowprice = wc_format_localized_price( $lowest ); $fb_highprice = wc_format_localized_price( $highest ); $fb_price = $fb_lowprice; } $fb_price = floatval(str_replace(',', '.', str_replace(',', '.', $fb_price))); $viewContent = "fbq(\"track\",\"ViewContent\",{content_category:\"$cats\", content_name:\"$product_name\", content_type:\"product_group\", content_ids:[$content], value:\"$fb_price\", currency:\"$currency\"},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "ViewContent"; $fb_capi_data["custom_data"]["content_ids"] = $fb_prodid; $fb_capi_data["custom_data"]["content_name"] = $product_name; $fb_capi_data["custom_data"]["content_category"] = $cats; $fb_capi_data["custom_data"]["currency"] = $currency; $fb_capi_data["custom_data"]["value"] = $fb_price; $fb_capi_data["custom_data"]["content_type"] = "product_group"; } } else { // This is a simple product page $fb_price = wc_format_localized_price( $product->get_price() ); $fb_price = floatval(str_replace(',', '.', str_replace(',', '.', $fb_price))); $viewContent = "fbq(\"track\",\"ViewContent\",{content_category:\"$cats\", content_name:\"$product_name\", content_type:\"product\", content_ids:[\"$fb_prodid\"], value:\"$fb_price\", currency:\"$currency\"},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "ViewContent"; $fb_capi_data["custom_data"]["content_ids"] = $fb_prodid; $fb_capi_data["custom_data"]["content_name"] = $product_name; $fb_capi_data["custom_data"]["content_category"] = $cats; $fb_capi_data["custom_data"]["currency"] = $currency; $fb_capi_data["custom_data"]["value"] = $fb_price; $fb_capi_data["custom_data"]["content_type"] = "product"; } } } } elseif ($fb_pagetype == "cart"){ // This is on the order thank you page if( isset( $_GET['key'] ) && is_wc_endpoint_url( 'order-received' ) ) { $order_string = sanitize_text_field($_GET['key']); if(!empty($order_string)){ $order_id = wc_get_order_id_by_order_key( $order_string ); $order = wc_get_order( $order_id ); $order_items = $order->get_items(); $currency = get_woocommerce_currency(); $contents = ""; $order_real = wc_format_localized_price($order->get_total()); if ( !is_wp_error( $order_items )) { foreach( $order_items as $item_id => $order_item) { $prod_id = $order_item->get_product_id(); $variation_id = $order_item->get_variation_id(); if($variation_id > 0){ $prod_id = $variation_id; } $prod_quantity = $order_item->get_quantity(); $contents .= "{'id': '$prod_id', 'quantity': $prod_quantity},"; } } $contents = rtrim($contents, ","); $order_real = floatval(str_replace(',', '.', str_replace(',', '.', $order_real))); $viewContent = "fbq('track','Purchase',{currency:'$currency', value:'$order_real', content_type:'product', contents:[$contents]},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "Purchase"; $fb_capi_data["custom_data"]["content_ids"] = $prod_id; $fb_capi_data["custom_data"]["currency"] = $currency; $fb_capi_data["custom_data"]["value"] = $order_real; $fb_capi_data["custom_data"]["content_type"] = "product"; } } else { // This is on the cart page itself $currency = get_woocommerce_currency(); $cart_items = WC()->cart->get_cart(); $cart_quantity = count($cart_items); $cart_real = 0; $contents = ""; $cart_total_amount = wc_format_localized_price(WC()->cart->get_cart_contents_total()); $cart_total_amount = floatval(str_replace(',', '.', str_replace(',', '.', $cart_total_amount))); $checkoutpage = wc_get_checkout_url(); $current_url = get_permalink(get_the_ID()); if(!empty($cart_items)){ if( !is_wp_error( $cart_items )) { foreach( $cart_items as $cart_id => $cart_item) { $prod_id = $cart_item['product_id']; $product = $cart_item['data']; $product_name = $product->get_name(); if($cart_item['variation_id'] > 0){ $prod_id = $cart_item['variation_id']; } $contents .= '\''.$prod_id.'\','; //$contents .= "$prod_id,"; $cart_real = wc_format_localized_price( $cart_item['line_total'] ); } $contents = rtrim($contents, ","); // User is on the billing pages if($checkoutpage == $current_url){ $viewContent = "fbq(\"track\",\"InitiateCheckout\",{currency:\"$currency\", value:\"$cart_total_amount\", content_type:\"product\", content_ids:[$contents]},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "InitiateCheckout"; $fb_capi_data["custom_data"]["content_ids"] = $contents; $fb_capi_data["custom_data"]["content_name"] = $product_name; $fb_capi_data["custom_data"]["currency"] = $currency; $fb_capi_data["custom_data"]["value"] = $cart_total_amount; $fb_capi_data["custom_data"]["content_type"] = "product"; } else { // User is on the basket page $viewContent = "fbq(\"track\",\"AddToCart\",{currency:\"$currency\", value:\"$cart_total_amount\", content_type:\"product\", content_ids:[$contents]},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "AddToCart"; $fb_capi_data["custom_data"]["content_ids"] = $contents; $fb_capi_data["custom_data"]["content_name"] = $product_name; $fb_capi_data["custom_data"]["currency"] = $currency; $fb_capi_data["custom_data"]["value"] = $cart_total_amount; $fb_capi_data["custom_data"]["content_type"] = "product"; } } } } } elseif ($fb_pagetype == "category"){ $term = get_queried_object(); global $wp_query; $ids = wp_list_pluck( $wp_query->posts, "ID" ); $fb_prodid = ""; foreach ($ids as $id){ $_product = wc_get_product($id); if(!$_product) { return -1; } if($_product->is_type('simple')){ // Add the simple product ID $fb_prodid .= '\''.$id.'\','; } else { // This is a variable product, add variation product ID's $children_ids = $_product->get_children(); foreach ($children_ids as $id){ $fb_prodid .= '\''.$id.'\','; $fb_prodid .= '\''.$id.'\','; } } } $fb_prodid = rtrim($fb_prodid, ","); $category_name = $term->name; $category_path = woosea_get_term_parents( $term->term_id, 'product_cat', $project_taxonomy = false, $link = false, $nicename = false, $visited = array() ); $viewContent = "fbq(\"track\",\"ViewCategory\",{content_category:'$category_path', content_name:'$category_name', content_type:\"product\", content_ids:\"[$fb_prodid]\"},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "ViewCategory"; $fb_capi_data["custom_data"]["content_ids"] = $ids; $fb_capi_data["custom_data"]["content_type"] = "product"; } elseif ($fb_pagetype == "searchresults"){ $term = get_queried_object(); $search_string = sanitize_text_field($_GET['s']); global $wp_query; $ids = wp_list_pluck( $wp_query->posts, "ID" ); $fb_prodid = ""; foreach ($ids as $id){ $_product = wc_get_product($id); if(!$_product) { return -1; } $ptype = $_product->get_type(); if($ptype == "simple"){ // Add the simple product ID $fb_prodid .= '\''.$id.'\','; } else { // This is a variable product, add variation product ID's $children_ids = $_product->get_children(); foreach ($children_ids as $id){ $fb_prodid .= '\''.$id.'\','; //$fb_prodid .= $id.','; } } } $fb_prodid = rtrim($fb_prodid, ","); $viewContent = "fbq(\"trackCustom\",\"Search\",{search_string:\"$search_string\", content_type:\"product\", content_ids:\"[$fb_prodid]\"},{eventID:\"$event_id\"});"; // Facebook CAPI data $fb_capi_data["event_name"] = "Search"; $fb_capi_data["custom_data"]["content_ids"] = $ids; $fb_capi_data["custom_data"]["content_type"] = "product"; } else { // This is another page than a product page $fb_capi_data["event_name"] = "ViewContent"; $viewContent = ""; } ?> 30, 'redirection' => 10, 'httpversion' => '1.0', 'blocking' => true, 'headers' => array( "cache-control: no-cache", "Accept: application/json" ), 'body' => $fields, 'cookies' => array() ); $response = wp_remote_post( $url, $args ); } } } } add_action('wp_footer', 'woosea_add_facebook_pixel'); /** * Add Google Adwords Remarketing code to footer */ function woosea_add_remarketing_tags( $product = null ){ if ( ! is_object( $product ) ) { $product = wc_get_product(get_the_ID()); } $ecomm_pagetype = WooSEA_Google_Remarketing::woosea_google_remarketing_pagetype(); $add_remarketing = get_option ('add_remarketing'); if($add_remarketing == "yes"){ $adwords_conversion_id = get_option("woosea_adwords_conversion_id"); // Add vulnerability check, unset when no proper comversion ID was inserted if(!is_numeric($adwords_conversion_id)){ unset($adwords_conversion_id); } if(!empty($adwords_conversion_id)){ ?> get_price()) { $ecomm_prodid = get_the_id(); if(!empty($ecomm_prodid)){ if(!$product) { return -1; } if ( $product->is_type( 'variable' ) ) { // We should first check if there are any _GET parameters available // When there are not we are on a variable product page but not on a specific variable one // In that case we need to put in the AggregateOffer structured data $variation_id = woosea_find_matching_product_variation( $product, sanitize_text_field($_GET) ); $nr_get = count($_GET); if($nr_get > 0){ $variable_product = wc_get_product($variation_id); // for variants use the variation_id and not the item_group_id // otherwise Google will disapprove the items due to itemID mismatches $ecomm_prodid = $variation_id; if(is_object( $variable_product ) ) { $product_price = $variable_product->get_price(); // ----- remove HTML TAGs ----- //$product_price = preg_replace ('/<[^>]*>/', ' ', $product_price); $ecomm_price = $product_price; } else { // AggregateOffer $prices = $product->get_variation_prices(); $lowest = reset( $prices['price'] ); $highest = end( $prices['price'] ); if ( $lowest === $highest ) { $ecomm_price = wc_format_decimal( $lowest, wc_get_price_decimals() ); } else { $ecomm_lowprice = wc_format_decimal( $lowest, wc_get_price_decimals() ); $ecomm_highprice = wc_format_decimal( $highest, wc_get_price_decimals() ); } } } else { // When there are no parameters in the URL (so for normal users, not coming via Google Shopping URL's) show the old WooCommwerce JSON $prices = $product->get_variation_prices(); $lowest = reset( $prices['price'] ); $highest = end( $prices['price'] ); if ( $lowest === $highest ) { $ecomm_price = wc_format_decimal( $lowest, wc_get_price_decimals()); } else { $ecomm_lowprice = wc_format_decimal( $lowest, wc_get_price_decimals() ); $ecomm_highprice = wc_format_decimal( $highest, wc_get_price_decimals() ); $ecomm_price = $ecomm_lowprice; } } } else { $ecomm_price = wc_format_decimal( $product->get_price(), wc_get_price_decimals() ); } } if( isset($ecomm_price) ){ ?> get_items(); $currency = get_woocommerce_currency(); $contents = ""; $order_real = wc_format_localized_price($order->get_total()); if ( !is_wp_error( $order_items )) { foreach( $order_items as $item_id => $order_item) { $prod_id = $order_item->get_product_id(); $variation_id = $order_item->get_variation_id(); if($variation_id > 0){ $prod_id = $variation_id; } $prod_quantity = $order_item->get_quantity(); } } $order_real = floatval(str_replace(',', '.', str_replace(',', '.', $order_real))); ?> cart->get_cart() as $cart_item ){ $ecomm_prodid = $cart_item['product_id']; break; } if( isset($ecomm_prodid) ){ $currency = get_woocommerce_currency(); $cart_items = WC()->cart->get_cart(); $cart_quantity = count($cart_items); $cart_total_amount = wc_format_localized_price(WC()->cart->get_cart_contents_total()+WC()->cart->tax_total); $cart_total_amount = floatval(str_replace(',', '.', str_replace(',', '.', $cart_total_amount))); ?> roles ) ) { $get_elite_notice = array( "show" => "no", "timestamp" => date( 'd-m-Y' ) ); update_option('woosea_getelite_notification',$get_elite_notice); } } add_action( 'wp_ajax_woosea_getelite_notification', 'woosea_getelite_notification' ); /** * Close the get Elite activation notification **/ function woosea_getelite_active_notification(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $get_elite_notice = array( "show" => "no", "timestamp" => date( 'd-m-Y' ) ); update_option('woosea_getelite_active_notification',$get_elite_notice); } } add_action( 'wp_ajax_woosea_getelite_active_notification', 'woosea_getelite_active_notification' ); /** * Add some JS and mark-up code on every front-end page in order to get the conversion tracking to work */ function woosea_hook_header() { $marker = sprintf(''); $allowed_tags = array('' => array()); echo wp_kses("\n{$marker}\n",$allowed_tags); } add_action('wp_head','woosea_hook_header'); /** * We need to be able to make an AJAX call on the thank you page */ function woosea_inject_ajax( $order_id ){ // Last order details $order = new WC_Order( $order_id ); $order_id = $order->get_id(); $customer_id = $order->get_user_id(); $total = $order->get_total(); update_option('last_order_id', $order_id); } add_action( 'woocommerce_thankyou', 'woosea_inject_ajax' ); /** * Register own cron hook(s), it will execute the woosea_create_all_feeds that will generate all feeds on scheduled event */ add_action( 'woosea_cron_hook', 'woosea_create_all_feeds'); // create a cron hook /** * Add WooCommerce SEA plugin to Menu */ function woosea_menu_addition(){ add_menu_page(__( 'Product Feed PRO for WooCommerce', 'woo-product-feed-pro' ), __( 'Product Feed Pro','woo-product-feed-pro' ), apply_filters( 'woosea_user_cap', 'manage_options' ), __FILE__, 'woosea_generate_pages', esc_url( WOOCOMMERCESEA_PLUGIN_URL . '/images/icon-16x16.png'),99); add_submenu_page(__FILE__, __( 'Feed configuration', 'woo-product-feed-pro' ), __( 'Create feed', 'woo-product-feed-pro' ), apply_filters( 'woosea_user_cap', 'manage_options' ), __FILE__, 'woosea_generate_pages'); add_submenu_page(__FILE__, __( 'Manage feeds', 'woo-product-feed-pro' ), __( 'Manage feeds', 'woo-product-feed-pro' ), apply_filters( 'woosea_user_cap', 'manage_options' ), 'woosea_manage_feed', 'woosea_manage_feed'); add_submenu_page(__FILE__, __( 'Settings', 'woo-product-feed-pro' ), __( 'Settings', 'woo-product-feed-pro' ), apply_filters( 'woosea_user_cap', 'manage_options' ), 'woosea_manage_settings', 'woosea_manage_settings'); } /** * Get the attributes for displaying the attributes dropdown on the rules page * Gets all attributes, product, image and attributes */ function woosea_ajax() { check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $rowCount = absint(esc_attr(sanitize_text_field($_POST['rowCount']))); $attributes_dropdown = get_option('attributes_dropdown'); if (!is_array($attributes_dropdown)){ $attributes_obj = new WooSEA_Attributes; $attributes_dropdown = $attributes_obj->get_product_attributes_dropdown(); update_option( 'attributes_dropdown', $attributes_dropdown, 'yes'); } $data = array ( 'rowCount' => $rowCount, 'dropdown' => $attributes_dropdown ); echo json_encode($data); wp_die(); } } add_action( 'wp_ajax_woosea_ajax', 'woosea_ajax' ); /** * Get a list of categories for the drop-down */ function woosea_categories_dropdown() { $rowCount = absint(esc_attr(sanitize_text_field($_POST['rowCount']))); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $orderby = 'name'; $order = 'asc'; $hide_empty = false ; $cat_args = array( 'orderby' => $orderby, 'order' => $order, 'hide_empty' => $hide_empty, ); $categories_dropdown = ""; $data = array ( 'rowCount' => $rowCount, 'dropdown' => $categories_dropdown ); echo json_encode($data); wp_die(); } } add_action( 'wp_ajax_woosea_categories_dropdown', 'woosea_categories_dropdown' ); /** * Sanitize XSS */ function woosea_sanitize_xss($value) { return htmlspecialchars(strip_tags($value)); } /** * Recursive sanitation for an array */ function woosea_recursive_sanitize_text_field($array) { foreach ( $array as $key => &$value ) { if ( is_array( $value ) ) { $value = woosea_recursive_sanitize_text_field( $value ); } else { $value = sanitize_text_field( $value ); } } return $array; } /** * Save Google Dynamic Remarketing Conversion Tracking ID */ function woosea_save_adwords_conversion_id() { check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $adwords_conversion_id = sanitize_text_field($_POST['adwords_conversion_id']); $adwords_conversion_id = woosea_sanitize_xss($adwords_conversion_id); update_option("woosea_adwords_conversion_id", $adwords_conversion_id); } } add_action( 'wp_ajax_woosea_save_adwords_conversion_id', 'woosea_save_adwords_conversion_id' ); /** * Save batch size */ function woosea_save_batch_size() { check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $batch_size = sanitize_text_field($_POST['batch_size']); update_option("woosea_batch_size", $batch_size); } } add_action( 'wp_ajax_woosea_save_batch_size', 'woosea_save_batch_size' ); /** * Save Facebook Pixel ID */ function woosea_save_facebook_pixel_id() { check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $facebook_pixel_id = sanitize_text_field($_POST['facebook_pixel_id']); $facebook_pixel_id = woosea_sanitize_xss($facebook_pixel_id); update_option("woosea_facebook_pixel_id", $facebook_pixel_id); } } add_action( 'wp_ajax_woosea_save_facebook_pixel_id', 'woosea_save_facebook_pixel_id' ); /** * Save Facebook Conversion API Token */ function woosea_save_facebook_capi_token() { check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $facebook_capi_token = sanitize_text_field($_POST['facebook_capi_token']); $facebook_capi_token = woosea_sanitize_xss($facebook_capi_token); update_option("woosea_facebook_capi_token", $facebook_capi_token); } } add_action( 'wp_ajax_woosea_save_facebook_capi_token', 'woosea_save_facebook_capi_token' ); /** * Mass map categories to the correct Google Shopping category taxonomy */ function woosea_add_mass_cat_mapping(){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $project_hash = sanitize_text_field($_POST['project_hash']); $catMappings = woosea_recursive_sanitize_text_field($_POST['catMappings']); // I need to sanitize the catMappings Array $mappings = array(); foreach ($catMappings as $mKey => $mVal){ $mKey = sanitize_text_field($mKey); $mVal = sanitize_text_field($mVal); $piecesVal = explode("||", $mVal); $mappings[$piecesVal[1]] = array( "rowCount" => $piecesVal[1], "categoryId" => $piecesVal[1], "criteria" => $piecesVal[0], "map_to_category" => $piecesVal[2], ); } $project = WooSEA_Update_Project::get_project_data(sanitize_text_field($project_hash)); // This happens during configuration of a new feed if(empty($project)){ $project_temp = get_option( 'channel_project' ); if(array_key_exists('mappings', $project_temp)){ $project_temp['mappings'] = $mappings + $project_temp['mappings']; } else { $project_temp['mappings'] = $mappings; } update_option( 'channel_project',$project_temp,'yes'); } else { // Only update the ones that changed foreach ($mappings as $categoryId => $catArray){ if(is_array($project['mappings'])){ if (array_key_exists($categoryId, $project['mappings'])){ $project['mappings'][$categoryId] = $catArray; } else { $project['mappings'][$categoryId] = $catArray; } } } $project_updated = WooSEA_Update_Project::update_project_data($project); } $data = array ( 'status_mapping' => "true", ); echo json_encode($data); wp_die(); } } add_action( 'wp_ajax_woosea_add_mass_cat_mapping', 'woosea_add_mass_cat_mapping' ); /** * Map categories to the correct Google Shopping category taxonomy */ function woosea_add_cat_mapping() { $rowCount = absint(esc_attr(sanitize_text_field($_POST['rowCount']))); $className = sanitize_text_field($_POST['className']); $map_to_category = sanitize_text_field($_POST['map_to_category']); $project_hash = sanitize_text_field($_POST['project_hash']); $criteria = sanitize_text_field($_POST['criteria']); $status_mapping = "false"; $project = WooSEA_Update_Project::get_project_data(sanitize_text_field($project_hash)); // This is during the configuration of a new feed if(empty($project)){ $project_temp = get_option( 'channel_project' ); $project_temp['mappings'][$rowCount]['rowCount'] = $rowCount; $project_temp['mappings'][$rowCount]['categoryId'] = $rowCount; $project_temp['mappings'][$rowCount]['criteria'] = $criteria; $project_temp['mappings'][$rowCount]['map_to_category'] = $map_to_category; update_option( 'channel_project',$project_temp,'yes'); $status_mapping = "true"; // This is updating an existing product feed } else { $project['mappings'][$rowCount]['rowCount'] = $rowCount; $project['mappings'][$rowCount]['categoryId'] = $rowCount; $project['mappings'][$rowCount]['criteria'] = $criteria; $project['mappings'][$rowCount]['map_to_category'] = $map_to_category; $project_updated = WooSEA_Update_Project::update_project_data($project); $status_mapping = "true"; } $data = array ( 'rowCount' => $rowCount, 'className' => $className, 'map_to_category' => $map_to_category, 'status_mapping' => $status_mapping, ); echo json_encode($data); wp_die(); } add_action( 'wp_ajax_woosea_add_cat_mapping', 'woosea_add_cat_mapping' ); /** * Retrieve variation product id based on it attributes **/ function woosea_find_matching_product_variation( $product, $attributes ) { if(is_array( $attributes )) { foreach( $attributes as $key => $value ) { if( strpos( $key, 'attribute_' ) === 0 ) { continue; } unset( $attributes[ $key ] ); $attributes[ sprintf( 'attribute_%s', $key ) ] = $value; } if( class_exists('WC_Data_Store') ) { $data_store = WC_Data_Store::load( 'product' ); return $data_store->find_matching_product_variation( $product, $attributes ); } else { return $product->get_matching_variation( $attributes ); } } } /** * Remove the price from the JSON-LD on variant product pages * As WooCommerce shows the wrong price and it causes items * to disapproved in Google's Merchant center because of it */ function woosea_product_delete_meta_price( $product = null ) { $markup_offer = array(); $structured_data_fix = get_option ('structured_data_fix'); if ( ! is_object( $product ) ) { global $product; } if ( ! is_a( $product, 'WC_Product' ) ) { return; } $shop_name = get_bloginfo( 'name' ); $shop_url = home_url(); $shop_currency = get_woocommerce_currency(); // Sisplay URL of current page. if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') { $link = "https"; } else { $link = "http"; } // Here append the common URL characters. $link .= "://"; // Append the host(domain name, ip) to the URL. $link .= sanitize_text_field($_SERVER['HTTP_HOST']); // Append the requested resource location to the URL $link .= sanitize_text_field($_SERVER['REQUEST_URI']); if($structured_data_fix == "yes"){ $pr_woo = wc_get_price_to_display($product); // if ( '' !== $product->get_regular_price() ) { $product_id = get_the_id(); // Get product MPN $mpn = get_post_meta( $product_id, '_woosea_mpn', true ); // Get product condition $first_condition = get_post_meta( $product_id, '_woosea_condition', true ); if(is_string($first_condition)){ $condition = ucfirst( $first_condition ); } if(!$condition){ $json_condition = "NewCondition"; } else { $json_condition = $condition."Condition"; } // Assume prices will be valid until the end of next year, unless on sale and there is an end date. $price_valid_until = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS ); if(!$product) { return -1; } if ( $product->is_type( 'variable' ) ) { // We should first check if there are any _GET parameters available // When there are not we are on a variable product page but not on a specific variable one // In that case we need to put in the AggregateOffer structured data $nr_get = count($_GET); if($nr_get > 0){ $mother_id = wc_get_product()->get_id(); $children_ids = $product->get_children(); foreach ($children_ids as &$child_val) { $product_variations = new WC_Product_Variation( $child_val ); $variations = array_filter($product_variations->get_variation_attributes()); $from_url = str_replace("\\","",sanitize_text_field($_GET),$i); if(is_array($from_url)){ $intersect = @array_intersect($from_url, $variations); if($variations == $intersect){ $variation_id = $child_val; } } else { $variation_id = $mother_id; } } if(isset($variation_id )){ $variable_product = wc_get_product($variation_id); } if( (isset($variation_id)) AND ( is_object( $variable_product ) ) ) { $qty = 1; // on default show prices including tax $product_price = wc_get_price_including_tax($variable_product); $structured_vat = get_option ('structured_vat'); // user requested to have prices without tax if($structured_vat == "yes"){ $tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class() ); // Workaround for price caching issues if(!empty($tax_rates)){ $tax_rates[1]['rate'] = 0; } // Make sure tax rates are numeric if( is_numeric($tax_rates[1]['rate']) ){ if( is_numeric($variable_product->get_price()) ){ $product_price = wc_get_price_excluding_tax($variable_product,array('price'=> $variable_product->get_price())) * (100+$tax_rates[1]['rate'])/100; } } } // Force rounding to two decimals if(!empty($product_price)){ $product_price = round($product_price, 2); } // Get product MPN $mpn = get_post_meta( $variation_id, '_woosea_mpn', true ); // Get product condition $condition = ucfirst( get_post_meta( $variation_id, '_woosea_condition', true ) ); if(!$condition){ $json_condition = "NewCondition"; } else { $json_condition = $condition."Condition"; } // Get stock status $stock_status = $variable_product->get_stock_status(); if ($stock_status == "outofstock"){ $availability = "OutOfStock"; } else { $availability = "InStock"; } if ( $variable_product->is_on_sale() && $variable_product->get_date_on_sale_to() ) { $price_valid_until = date( 'Y-m-d', $variable_product->get_date_on_sale_to()->getTimestamp() ); } $markup_offer = array( '@type' => 'Offer', 'price' => $product_price, 'priceValidUntil' => $price_valid_until, 'priceSpecification' => array( '@type' => 'PriceSpecification', 'price' => $product_price, 'priceCurrency' => $shop_currency, 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', ), 'priceCurrency' => $shop_currency, 'itemCondition' => 'https://schema.org/'.$json_condition.'', 'availability' => 'https://schema.org/'.$availability.'', 'sku' => $variable_product->get_sku(), 'image' => wp_get_attachment_url( $product->get_image_id() ), 'description' => $product->get_description(), 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), 'url' => $link ); } else { // AggregateOffer $prices = $product->get_variation_prices(); $lowest = reset( $prices['price'] ); $highest = end( $prices['price'] ); $price_valid_until = date( 'Y-m-d', $variable_product->get_date_on_sale_to()->getTimestamp() ); if ( $lowest === $highest ) { $markup_offer = array( '@type' => 'Offer', 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'priceCurrency' => $shop_currency, 'priceSpecification' => array( '@type' => 'PriceSpecification', 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'priceCurrency' => $shop_currency, 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', ), 'itemCondition' => 'https://schema.org/'.$json_condition.'', 'availability' => 'https://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'sku' => $product->get_sku(), 'image' => wp_get_attachment_url( $product->get_image_id() ), 'description' => $product->get_description(), 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), 'url' => $link ); } else { $markup_offer = array( '@type' => 'AggregateOffer', 'lowPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'highPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ), 'priceCurrency' => $shop_currency, 'priceValidUntil' => $price_valid_until, 'itemCondition' => 'https://schema.org/'.$json_condition.'', 'availability' => 'https://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'sku' => $product->get_sku(), 'image' => wp_get_attachment_url( $product->get_image_id() ), 'description' => $product->get_description(), 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), 'url' => $link ); } } } else { // This is a variation product page but no variation has been selected. WooCommerce always shows the price of the lowest priced // variation product. That is why we also put this in the JSON // When there are no parameters in the URL (so for normal users, not coming via Google Shopping URL's) show the old WooCommwerce JSON $product_price = wc_get_price_to_display($product); if(!is_string($product_price)){ $product_price = round($product_price,2); } $price_valid_until = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS ); $markup_offer += array( '@type' => 'Offer', 'price' => $product_price, 'priceCurrency' => $shop_currency, 'priceValidUntil' => $price_valid_until, 'availability' => 'https://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'sku' => $product->get_sku(), 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), 'url' => $link ); } } else { // This is a simple product // By default show prices including tax $product_price = wc_get_price_including_tax($product); $structured_vat = get_option ('structured_vat'); // Use prices excluding tax if($structured_vat == "yes"){ $tax_rates = WC_Tax::get_base_tax_rates( $product->get_tax_class() ); if(!empty($tax_rates)){ $tax_rates[1]['rate'] = 0; } $product_price = wc_get_price_excluding_tax($product,array('price'=> $product->get_price())) * (100+$tax_rates[1]['rate'])/100; } $product_price = round($product_price, 2); // Assume prices will be valid until the end of next year, unless on sale and there is an end date. $price_valid_until = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS ); $markup_offer = array( '@type' => 'Offer', 'price' => $product_price, 'priceValidUntil' => $price_valid_until, 'priceCurrency' => $shop_currency, 'priceSpecification' => array( '@type' => 'PriceSpecification', 'price' => $product_price, 'priceCurrency' => $shop_currency, 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', ), 'itemCondition' => 'https://schema.org/'.$json_condition.'', 'availability' => 'https://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'sku' => $product->get_sku(), 'image' => wp_get_attachment_url( $product->get_image_id() ), 'description' => $product->get_description(), 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), 'url' => $link ); } // } } else { // Just use the old WooCommerce buggy setting if ( '' !== $product->get_price() ) { $price_valid_until = date( 'Y-12-31', current_time( 'timestamp', true ) + YEAR_IN_SECONDS ); if(!$product) { return -1; } if ( $product->is_type( 'variable' ) ) { $prices = $product->get_variation_prices(); $lowest = reset( $prices['price'] ); $highest = end( $prices['price'] ); $permalink = get_permalink( $product->get_id() ); $price_valid_until = date( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() ); if ( $lowest === $highest ) { $markup_offer = array( '@type' => 'Offer', 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'priceValidUntil' => $price_valid_until, 'priceCurrency' => $shop_currency, 'availability' => 'https://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'url' => $permalink, 'priceSpecification' => array( 'price' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'priceCurrency' => $shop_currency, 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', ), ); } else { $markup_offer = array( '@type' => 'AggregateOffer', 'lowPrice' => wc_format_decimal( $lowest, wc_get_price_decimals() ), 'highPrice' => wc_format_decimal( $highest, wc_get_price_decimals() ), 'priceValidUntil' => $price_valid_until, 'priceCurrency' => $shop_currency, 'availability' => 'https://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'url' => $permalink, 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), ); } } else { if ( $product->is_on_sale() && $product->get_date_on_sale_to() ) { $price_valid_until = date( 'Y-m-d', $product->get_date_on_sale_to()->getTimestamp() ); } $permalink = get_permalink( $product->get_id() ); $markup_offer = array( '@type' => 'Offer', 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ), 'priceValidUntil' => $price_valid_until, 'priceCurrency' => $shop_currency, 'priceSpecification' => array( 'price' => wc_format_decimal( $product->get_price(), wc_get_price_decimals() ), 'priceCurrency' => $shop_currency, 'valueAddedTaxIncluded' => wc_prices_include_tax() ? 'true' : 'false', ), 'availability' => 'https://schema.org/' . ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ), 'url' => $permalink, 'seller' => array( '@type' => 'Organization', 'name' => $shop_name, 'url' => $shop_url, ), ); } } } return $markup_offer; } //add_filter( 'woocommerce_structured_data_product_offer', 'woosea_product_delete_meta_price', 1000, 1 ); /** * Fix the WooCommerce schema markup bug for variation prices */ function woosea_product_fix_structured_data( $product = null ) { $markup = array(); if ( ! is_object( $product ) ) { global $product; } if ( ! is_a( $product, 'WC_Product' ) ) { return; } $shop_name = get_bloginfo( 'name' ); $shop_url = home_url(); $currency = get_woocommerce_currency(); // Sisplay URL of current page. if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') { $link = "https"; } else { $link = "http"; } // Here append the common URL characters. $link .= "://"; // Append the host(domain name, ip) to the URL. $link .= sanitize_text_field($_SERVER['HTTP_HOST']); // Append the requested resource location to the URL $link .= sanitize_text_field($_SERVER['REQUEST_URI']); $structured_data_fix = get_option ('structured_data_fix'); // This is an Elite user who enababled the structured data fix if($structured_data_fix == "yes"){ // We should first check if there are any _GET parameters available // When there are not we are on a variable product page but not on a specific variable one // In that case we need to put in the AggregateOffer structured data $nr_get = count($_GET); if($nr_get > 0){ // This is a variable product $mother_id = wc_get_product()->get_id(); $children_ids = $product->get_children(); $prod_type = $product->get_type(); foreach ($children_ids as &$child_val) { $product_variations = new WC_Product_Variation( $child_val ); $variations = array_filter($product_variations->get_variation_attributes()); $from_url = str_replace("\\","",sanitize_text_field($_GET),$i); if(is_array($from_url)){ $intersect = array_intersect($from_url, $variations); if($variations == $intersect){ $variation_id = $child_val; } } } if(isset($variation_id )){ $variable_product = wc_get_product($variation_id); } if( (isset($variation_id)) AND ( is_object( $variable_product ) ) ) { $markup = array( '@type' => 'Product', '@id' => $link . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist. 'name' => $variable_product->get_name(), 'url' => $link, 'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ), ); $image = wp_get_attachment_url( $variable_product->get_image_id() ); if ( $image ) { $markup['image'] = $image; } // Get product brand $brand = get_post_meta( $variation_id, '_woosea_brand', true ); if ( $brand ) { $markup['brand'] = array ( '@type' => 'Brand', 'name' => $brand, ); } // Get product MPN $mpn = get_post_meta( $variation_id, '_woosea_mpn', true ); if ( $mpn ) { $markup['mpn'] = $mpn; } // Get product GTIM $gtin = get_post_meta( $variation_id, '_woosea_gtin', true ); if ( $gtin ) { $gtin_length = strlen($gtin); if ( $gtin_length == 14){ $markup['gtin14'] = $gtin; } elseif ( $gtin_length == 13){ $markup['gtin13'] = $gtin; } elseif ( $gtin_length == 12){ $markup['gtin12'] = $gtin; } elseif ( $gtin_length == 8){ $markup['gtin8'] = $gtin; } else { // do not add GTIN to markup } } // Declare SKU or fallback to ID. if ( $variable_product->get_sku() ) { $markup['sku'] = $variable_product->get_sku(); } else { $markup['sku'] = $variable_product->get_id(); } // Get the offers structured data schema markup $markup['offers'][0] = woosea_product_delete_meta_price($product); // This only works for WooCommerce 3.6 and higher (wc_review_ratings_enabled function) if ( ($product->get_rating_count()) OR (function_exists(wc_review_ratings_enabled()))) { $markup['aggregateRating'] = array( '@type' => 'AggregateRating', 'ratingValue' => $product->get_average_rating(), 'reviewCount' => $product->get_review_count(), ); // Markup 5 most recent rating/review. $comments = get_comments( array( 'number' => 5, 'post_id' => $product->get_id(), 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product', 'parent' => 0, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => 'rating', 'type' => 'NUMERIC', 'compare' => '>', 'value' => 0, ), ), ) ); if ( $comments ) { $markup['review'] = array(); foreach ( $comments as $comment ) { $markup['review'][] = array( '@type' => 'Review', 'reviewRating' => array( '@type' => 'Rating', 'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ), ), 'author' => array( '@type' => 'Person', 'name' => get_comment_author( $comment ), ), 'reviewBody' => get_comment_text( $comment ), 'datePublished' => get_comment_date( 'c', $comment ), ); } } } } } else { // This is a simple product $markup = array( '@type' => 'Product', '@id' => $link . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist. 'name' => $product->get_name(), 'url' => $link, 'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ), ); $brand = get_post_meta( $product->get_id(), '_woosea_brand', true ); if ( $brand ) { $markup['brand'] = array ( '@type' => 'Brand', 'name' => $brand, ); } // Get product MPN $mpn = get_post_meta( $product->get_id(), '_woosea_mpn', true ); if ( $mpn ) { $markup['mpn'] = $mpn; } // Get product GTIM $gtin = get_post_meta( $product->get_id(), '_woosea_gtin', true ); if ( $gtin ) { $gtin_length = strlen($gtin); if ( $gtin_length == 14){ $markup['gtin14'] = $gtin; } elseif ( $gtin_length == 13){ $markup['gtin13'] = $gtin; } elseif ( $gtin_length == 12){ $markup['gtin12'] = $gtin; } elseif ( $gtin_length == 8){ $markup['gtin8'] = $gtin; } else { // do not add GTIN to markup } } $image = wp_get_attachment_url( $product->get_image_id() ); if ( $image ) { $markup['image'] = $image; } // Declare SKU or fallback to ID. if ( $product->get_sku() ) { $markup['sku'] = $product->get_sku(); } else { $markup['sku'] = $product->get_id(); } // Get the offers structured data schema markup $markup['offers'][0] = woosea_product_delete_meta_price($product); // if(!class_exists('WPSEO_WooCommerce_Schema')){ if ( ($product->get_rating_count()) OR (function_exists(wc_review_ratings_enabled()))) { $markup['aggregateRating'] = array( '@type' => 'AggregateRating', 'ratingValue' => $product->get_average_rating(), 'reviewCount' => $product->get_review_count(), ); // Markup 5 most recent rating/review. $comments = get_comments( array( 'number' => 5, 'post_id' => $product->get_id(), 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product', 'parent' => 0, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => 'rating', 'type' => 'NUMERIC', 'compare' => '>', 'value' => 0, ), ), ) ); if ( $comments ) { $markup['review'] = array(); foreach ( $comments as $comment ) { $markup['review'][] = array( '@type' => 'Review', 'reviewRating' => array( '@type' => 'Rating', 'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ), ), 'author' => array( '@type' => 'Person', 'name' => get_comment_author( $comment ), ), 'reviewBody' => get_comment_text( $comment ), 'datePublished' => get_comment_date( 'c', $comment ), ); } } } // } } } else { // Structured data fix is not enabled $markup = array( '@type' => 'Product', '@id' => $link . '#product', // Append '#product' to differentiate between this @id and the @id generated for the Breadcrumblist. 'name' => $product->get_name(), 'url' => $link, 'description' => wp_strip_all_tags( do_shortcode( $product->get_short_description() ? $product->get_short_description() : $product->get_description() ) ), ); $image = wp_get_attachment_url( $product->get_image_id() ); if ( $image ) { $markup['image'] = $image; } // Declare SKU or fallback to ID. if ( $product->get_sku() ) { $markup['sku'] = $product->get_sku(); } else { $markup['sku'] = $product->get_id(); } // Get the offers structured data schema markup $markup['offers'][0] = woosea_product_delete_meta_price($product); // Check if Yoast SEO WooCommerce plugin is enabled // if(!class_exists('WPSEO_WooCommerce_Schema')){ if ( ($product->get_rating_count() > 0) OR (function_exists(wc_review_ratings_enabled()))) { $markup['aggregateRating'] = array( '@type' => 'AggregateRating', 'ratingValue' => $product->get_average_rating(), 'reviewCount' => $product->get_review_count(), ); // Markup 5 most recent rating/review. $comments = get_comments( array( 'number' => 5, 'post_id' => $product->get_id(), 'status' => 'approve', 'post_status' => 'publish', 'post_type' => 'product', 'parent' => 0, 'meta_query' => array( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query array( 'key' => 'rating', 'type' => 'NUMERIC', 'compare' => '>', 'value' => 0, ), ), ) ); if ( $comments ) { $markup['review'] = array(); foreach ( $comments as $comment ) { $markup['review'][] = array( '@type' => 'Review', 'reviewRating' => array( '@type' => 'Rating', 'ratingValue' => get_comment_meta( $comment->comment_ID, 'rating', true ), ), 'author' => array( '@type' => 'Person', 'name' => get_comment_author( $comment ), ), 'reviewBody' => get_comment_text( $comment ), 'datePublished' => get_comment_date( 'c', $comment ), ); } } } //} } return $markup; } // Only execute this filter when the structured data fix feature is enabled $structured_data_fix = get_option ('structured_data_fix'); if($structured_data_fix == "yes"){ add_filter( 'woocommerce_structured_data_product', 'woosea_product_fix_structured_data', 10, 2 ); } /** * Get the shipping zone countries and ID's */ function woosea_shipping_zones(){ $shipping_options = ""; $shipping_zones = WC_Shipping_Zones::get_zones(); $shipping_options = ""; foreach ( $shipping_zones as $zone){ $shipping_options .= ""; } $data = array ( 'dropdown' => $shipping_options, ); echo json_encode($data); wp_die(); } add_action( 'wp_ajax_woosea_shipping_zones', 'woosea_shipping_zones' ); /** * Determine if any of the feeds is updating */ function woosea_check_processing(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $processing = "false"; $feed_config = get_option( 'cron_projects' ); $found = false; foreach ( $feed_config as $key => $val ) { if(array_key_exists('running', $val)){ if( in_array($val['running'], array( "true", "processing", "stopped", "not run yet" ) ) ){ $processing = "true"; } } } $data = array ( 'processing' => $processing, ); echo json_encode($data); wp_die(); } } add_action( 'wp_ajax_woosea_check_processing', 'woosea_check_processing' ); /** * Get the dynamic attributes */ function woosea_special_attributes(){ $attributes_obj = new WooSEA_Attributes; $special_attributes = $attributes_obj->get_special_attributes_dropdown(); $special_attributes_clean = $attributes_obj->get_special_attributes_clean(); $data = array ( 'dropdown' => $special_attributes, 'clean' => $special_attributes_clean, ); echo json_encode($data); wp_die(); } add_action( 'wp_ajax_woosea_special_attributes', 'woosea_special_attributes' ); /** * Get the available channels for a specific country */ function woosea_channel() { $country = sanitize_text_field($_POST['country']); $channel_obj = new WooSEA_Attributes; $data = $channel_obj->get_channels($country); echo json_encode($data); wp_die(); } add_action( 'wp_ajax_woosea_channel', 'woosea_channel' ); /** * Delete a project from cron */ function woosea_project_delete(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $project_hash = sanitize_text_field($_POST['project_hash']); $feed_config = get_option( 'cron_projects' ); $found = false; foreach ( $feed_config as $key => $val ) { if (isset($val['project_hash']) AND ($val['project_hash'] == $project_hash)){ $found = true; $found_key = $key; $upload_dir = wp_upload_dir(); $base = $upload_dir['basedir']; $path = $base . "/woo-product-feed-pro/" . $val['fileformat']; $file = $path . "/" . sanitize_file_name($val['filename']) . "." . $val['fileformat']; } } if ($found == "true"){ # Remove project from project array unset($feed_config[$found_key]); # Update cron update_option('cron_projects', $feed_config, 'no'); # Remove project file @unlink($file); } } } add_action( 'wp_ajax_woosea_project_delete', 'woosea_project_delete' ); /** * Stop processing of a project */ function woosea_project_cancel(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $project_hash = sanitize_text_field($_POST['project_hash']); $feed_config = get_option( 'cron_projects' ); foreach ( $feed_config as $key => $val ) { if ($val['project_hash'] == $project_hash){ $batch_project = "batch_project_".$project_hash; delete_option( $batch_project ); $feed_config[$key]['nr_products_processed'] = 0; // Set processing status on ready $feed_config[$key]['running'] = "stopped"; $feed_config[$key]['last_updated'] = date("d M Y H:i"); // Delete processed product array for preventing duplicates delete_option('woosea_duplicates'); // In 1 minute from now check the amount of products in the feed and update the history count wp_schedule_single_event( time() + 60, 'woosea_update_project_stats', array($val['project_hash']) ); } } update_option( 'cron_projects', $feed_config, 'no'); } } add_action( 'wp_ajax_woosea_project_cancel', 'woosea_project_cancel' ); /** * Get the processing status of a project feed */ function woosea_project_processing_status(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $project_hash = sanitize_text_field($_POST['project_hash']); $feed_config = get_option( 'cron_projects' ); $proc_perc = 0; foreach ( $feed_config as $key => $val ) { if (isset($val['project_hash']) AND ($val['project_hash'] === $project_hash)){ $this_feed = $val; } } if($this_feed['running'] == "ready"){ $proc_perc = 100; } elseif($this_feed['running'] == "not run yet"){ $proc_perc = 999; } else { $proc_perc = round(($this_feed['nr_products_processed']/$this_feed['nr_products'])*100); } $data = array ( 'project_hash' => $project_hash, 'running' => $this_feed['running'], 'proc_perc' => $proc_perc, ); echo json_encode($data); wp_die(); } } add_action( 'wp_ajax_woosea_project_processing_status', 'woosea_project_processing_status' ); /** * Copy a project */ function woosea_project_copy(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $project_hash = sanitize_text_field($_POST['project_hash']); $feed_config = get_option( 'cron_projects' ); $max_key = max(array_keys($feed_config)); $add_project = array(); $upload_dir = wp_upload_dir(); $external_base = $upload_dir['baseurl']; foreach ( $feed_config as $key => $val ) { if ($val['project_hash'] == $project_hash){ $val['projectname'] = "Copy ". $val['projectname']; // New code to create the project hash so dependency on openSSL is removed $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $pieces = []; $length = 32; $max = mb_strlen($keyspace, '8bit') - 1; for ($i = 0; $i < $length; ++$i) { $pieces []= $keyspace[random_int(0, $max)]; } $val['project_hash'] = implode('', $pieces); $val['filename'] = $val['project_hash']; $val['utm_campaign'] = "Copy ". $val['utm_campaign']; $val['last_updated'] = ""; $val['running'] = "not run yet"; // Construct product feed URL $external_path = $external_base . "/woo-product-feed-pro/" . $val['fileformat']; $val['external_file'] = $external_path . "/" . sanitize_file_name($val['filename']) . "." . $val['fileformat']; // To build the new project row on the manage feed page $projecthash = $val['project_hash']; $projectname = $val['projectname']; $channel = $val['name']; $fileformat = $val['fileformat']; $interval = $val['cron']; $external_file = $val['external_file']; // Save the copied project $new_key = $max_key+1; $add_project[$new_key] = $val; array_push($feed_config, $add_project[$new_key]); update_option( 'cron_projects', $feed_config, 'no'); // Do not start processing, user wants to make changes to the copied project $copy_status = "true"; } } $data = array ( 'project_hash' => $projecthash, 'channel' => $channel, 'projectname' => $projectname, 'fileformat' => $fileformat, 'interval' => $interval, 'external_file' => $external_file, 'copy_status' => $copy_status ); echo json_encode($data); wp_die(); } } add_action( 'wp_ajax_woosea_project_copy', 'woosea_project_copy' ); /** * Refresh a project */ function woosea_project_refresh(){ check_ajax_referer( 'woosea_ajax_nonce', 'security' ); // Force garbage collection dump gc_enable(); gc_collect_cycles(); $project_hash = sanitize_text_field($_POST['project_hash']); $feed_config = get_option( 'cron_projects' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { // Make sure content of feeds is not being cached // Make sure feeds are not being cached $no_caching = new WooSEA_Caching(); // LiteSpeed Caching if (class_exists( 'LiteSpeed\Core' ) || defined( 'LSCWP_DIR' ) ) { $no_caching->litespeed_cache(); } // WP Fastest Caching if (class_exists( 'WpFastestCache' ) ) { $no_caching->wp_fastest_cache(); } // WP Super Caching if (function_exists( 'wpsc_init' ) ) { $no_caching->wp_super_cache(); } // Breeze Caching if (class_exists( 'Breeze_Admin' ) ) { $no_caching->breeze_cache(); } // WP Optimize Caching if (class_exists( 'WP_Optimize' ) ) { $no_caching->wp_optimize_cache(); } // Cache Enabler if (class_exists( 'Cache_Enabler' ) ) { $no_caching->cache_enabler_cache(); } // Swift Performance Lite if (class_exists( 'Swift_Performance_Lite' ) ) { $no_caching->swift_performance_cache(); } // Comet Cache if (is_plugin_active( 'comet-cache/comet-cache.php' ) ) { $no_caching->comet_cache(); } // HyperCache if (class_exists( 'HyperCache' ) ) { $no_caching->hyper_cache(); } foreach ( $feed_config as $key => $val ) { if (isset($val['project_hash']) AND ($val['project_hash'] == $project_hash)){ $batch_project = "batch_project_".$project_hash; if (!get_option( $batch_project )){ update_option( $batch_project, $val, 'no'); $final_creation = woosea_continue_batch($project_hash); } else { $final_creation = woosea_continue_batch($project_hash); } } } } } add_action( 'wp_ajax_woosea_project_refresh', 'woosea_project_refresh' ); /** * Add or remove custom attributes to the feed configuration drop-downs */ function woosea_add_attributes() { check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $attribute_name = sanitize_text_field($_POST['attribute_name']); $attribute_value = sanitize_text_field($_POST['attribute_value']); $active = sanitize_text_field($_POST['active']); if(!get_option( 'woosea_extra_attributes' )){ if($active == "true"){ $extra_attributes = array( $attribute_value => $attribute_name ); update_option ( 'woosea_extra_attributes', $extra_attributes, 'no'); } } else { $extra_attributes = get_option( 'woosea_extra_attributes' ); if(!in_array($attribute_name, $extra_attributes,TRUE)){ if($active == "true"){ $add_attribute = array ( $attribute_value => $attribute_name ); $extra_attributes = array_merge ($extra_attributes, $add_attribute); update_option ( 'woosea_extra_attributes', $extra_attributes, 'no'); } } else { if($active == "false"){ // remove from extra attributes array $extra_attributes = array_diff($extra_attributes, array($attribute_value => $attribute_name)); update_option ( 'woosea_extra_attributes', $extra_attributes, 'no'); } } } $extra_attributes = get_option( 'woosea_extra_attributes' ); } } add_action( 'wp_ajax_woosea_add_attributes', 'woosea_add_attributes' ); /** * Change status of a project from active to inactive or visa versa */ function woosea_project_status() { check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $project_hash = sanitize_text_field($_POST['project_hash']); $active = sanitize_text_field($_POST['active']); $feed_config = get_option( 'cron_projects' ); $number_feeds = count($feed_config); if ($number_feeds > 0){ foreach ( $feed_config as $key => $val ) { if ($val['project_hash'] == $project_hash){ $feed_config[$key]['active'] = $active; $upload_dir = wp_upload_dir(); $base = $upload_dir['basedir']; $path = $base . "/woo-product-feed-pro/" . $val['fileformat']; $file = $path . "/" . sanitize_file_name($val['filename']) . "." . $val['fileformat']; } } } // When project is put on inactive, delete the product feed if($active == "false"){ @unlink($file); } // Regenerate product feed if($active == "true"){ $update_project = woosea_project_refresh($project_hash); } // Update cron with new project status update_option( 'cron_projects', $feed_config, 'no'); } } add_action( 'wp_ajax_woosea_project_status', 'woosea_project_status' ); /** * Register interaction with the review request notification * We do not want to keep bothering our users with the notification */ function woosea_review_notification() { // Update review notification status check_ajax_referer( 'woosea_ajax_nonce', 'security' ); $user = wp_get_current_user(); $allowed_roles = array( 'administrator','editor','author' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { update_option( 'woosea_review_interaction', 'yes', 'yes'); } } add_action( 'wp_ajax_woosea_review_notification', 'woosea_review_notification' ); /** * This function enables the setting to fix the * WooCommerce structured data bug */ function woosea_enable_structured_data (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $status = sanitize_text_field($_POST['status']); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { if ($status == "off"){ update_option( 'structured_data_fix', 'no', 'yes'); } else { update_option( 'structured_data_fix', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_enable_structured_data', 'woosea_enable_structured_data' ); /** * This function enables the setting to remove VAT from * structured data prices */ function woosea_structured_vat (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'structured_vat', 'no', 'yes'); } else { update_option( 'structured_vat', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_structured_vat', 'woosea_structured_vat' ); /** * This function enables the setting to add * Product data manipulation support */ function woosea_add_manipulation (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_manipulation_support', 'no', 'yes'); } else { update_option( 'add_manipulation_support', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_manipulation', 'woosea_add_manipulation' ); /** * This function enables the setting to add * WPML support */ function woosea_add_wpml (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_wpml_support', 'no', 'yes'); } else { update_option( 'add_wpml_support', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_wpml', 'woosea_add_wpml' ); /** * This function enables the setting to add * Aelia support */ function woosea_add_aelia (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_aelia_support', 'no', 'yes'); } else { update_option( 'add_aelia_support', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_aelia', 'woosea_add_aelia' ); /** * This function enables the setting to use * Mother main image for all product variations */ function woosea_add_mother_image (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_mother_image', 'no', 'yes'); } else { update_option( 'add_mother_image', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_mother_image', 'woosea_add_mother_image' ); /** * This function enables the setting to use * Shipping costs for all countries */ function woosea_add_all_shipping (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_all_shipping', 'no', 'yes'); } else { update_option( 'add_all_shipping', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_all_shipping', 'woosea_add_all_shipping' ); /** * This function enables the setting to respect * the free shipping class */ function woosea_free_shipping (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'free_shipping', 'no', 'yes'); } else { update_option( 'free_shipping', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_free_shipping', 'woosea_free_shipping' ); /** * This function enables the setting to remove * local pickup shipping zones */ function woosea_local_pickup_shipping (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'local_pickup_shipping', 'no', 'yes'); } else { update_option( 'local_pickup_shipping', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_local_pickup_shipping', 'woosea_local_pickup_shipping' ); /** * This function enables the setting to remove * free shipping zones */ function woosea_remove_free_shipping (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'remove_free_shipping', 'no', 'yes'); } else { update_option( 'remove_free_shipping', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_remove_free_shipping', 'woosea_remove_free_shipping' ); /** * This function enables the setting to use logging */ function woosea_add_woosea_logging (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_woosea_logging', 'no', 'yes'); } else { update_option( 'add_woosea_logging', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_woosea_logging', 'woosea_add_woosea_logging' ); /** * This function enables the setting to use only the basic attributes in drop-downs */ function woosea_add_woosea_basic (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_woosea_basic', 'no', 'yes'); } else { update_option( 'add_woosea_basic', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_woosea_basic', 'woosea_add_woosea_basic' ); /** * This function enables the setting to add CDATA to title and descriptions */ function woosea_add_woosea_cdata (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_woosea_cdata', 'no', 'yes'); } else { update_option( 'add_woosea_cdata', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_woosea_cdata', 'woosea_add_woosea_cdata' ); /** * This function enables the setting to add * the Faceook pixel */ function woosea_add_facebook_pixel_setting (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_facebook_pixel', 'no', 'yes'); } else { update_option( 'add_facebook_pixel', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_facebook_pixel_setting', 'woosea_add_facebook_pixel_setting' ); /** * This function enables the setting to enable * the Faceook Conversion API (CAPI) */ function woosea_add_facebook_capi_setting (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_facebook_capi', 'no', 'yes'); } else { update_option( 'add_facebook_capi', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_facebook_capi_setting', 'woosea_add_facebook_capi_setting' ); /** * This function saves the value that needs to be used in the Facebook pixel content_ids parameter */ function woosea_facebook_content_ids (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $content_ids = sanitize_text_field($_POST['content_ids']); if ($content_ids == "variable"){ update_option( 'add_facebook_pixel_content_ids', 'variable', 'yes'); } else { update_option( 'add_facebook_pixel_content_ids', 'variation', 'yes'); } } } add_action( 'wp_ajax_woosea_facebook_content_ids', 'woosea_facebook_content_ids' ); /** * This function enables the setting to add * Google's Dynamic Remarketing */ function woosea_add_remarketing (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_remarketing', 'no', 'yes'); } else { update_option( 'add_remarketing', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_remarketing', 'woosea_add_remarketing' ); /** * This function enables the setting to add * a new batch size */ function woosea_add_batch (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_batch', 'no', 'yes'); } else { update_option( 'add_batch', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_batch', 'woosea_add_batch' ); /** * This function enables the setting to add * identifiers GTIN, MPN, EAN, UPC, Brand and Condition */ function woosea_add_identifiers (){ check_ajax_referer('woosea_ajax_nonce', 'security'); $user = wp_get_current_user(); $allowed_roles = array( 'administrator' ); if ( array_intersect( $allowed_roles, $user->roles ) ) { $status = sanitize_text_field($_POST['status']); if ($status == "off"){ update_option( 'add_unique_identifiers', 'no', 'yes'); } else { update_option( 'add_unique_identifiers', 'yes', 'yes'); } } } add_action( 'wp_ajax_woosea_add_identifiers', 'woosea_add_identifiers' ); /** * This function add the actual fields to the edit product page for single products * identifiers GTIN, MPN, EAN, UPC, Brand and Condition */ function woosea_custom_general_fields() { global $woocommerce, $post; $extra_attributes = array(); $extra_attributes = get_option( 'woosea_extra_attributes' ); // Check if the option is enabled or not in the pluggin settings if( (get_option('add_unique_identifiers') == "yes") AND (!empty($extra_attributes)) ){ echo '
'; echo ''; } } add_action( 'woocommerce_product_options_general_product_data', 'woosea_custom_general_fields' ); /** * This function saves the input from the extra fields on the single product edit page */ function woosea_save_custom_general_fields($post_id){ $woocommerce_brand = empty($_POST['_woosea_brand']) ? '' : sanitize_text_field($_POST['_woosea_brand']); $woocommerce_gtin = empty($_POST['_woosea_gtin']) ? '' : sanitize_text_field($_POST['_woosea_gtin']); $woocommerce_upc = empty($_POST['_woosea_upc']) ? '' : sanitize_text_field($_POST['_woosea_upc']); $woocommerce_mpn = empty($_POST['_woosea_mpn']) ? '' : sanitize_text_field($_POST['_woosea_mpn']); $woocommerce_ean = empty($_POST['_woosea_ean']) ? '' : sanitize_text_field($_POST['_woosea_ean']); $woocommerce_title = empty($_POST['_woosea_optimized_title']) ? '' : sanitize_text_field($_POST['_woosea_optimized_title']); $woocommerce_color = empty($_POST['_woosea_color']) ? '' : sanitize_text_field($_POST['_woosea_color']); $woocommerce_size = empty($_POST['_woosea_size']) ? '' : sanitize_text_field($_POST['_woosea_size']); $woocommerce_gender = empty($_POST['_woosea_gender']) ? '' : sanitize_text_field($_POST['_woosea_gender']); $woocommerce_material = empty($_POST['_woosea_material']) ? '' : sanitize_text_field($_POST['_woosea_material']); $woocommerce_pattern = empty($_POST['_woosea_pattern']) ? '' : sanitize_text_field($_POST['_woosea_pattern']); $woocommerce_unit_pricing_measure = empty($_POST['_woosea_unit_pricing_measure']) ? '' : sanitize_text_field($_POST['_woosea_unit_pricing_measure']); $woocommerce_unit_pricing_base_measure = empty($_POST['_woosea_unit_pricing_base_measure']) ? '' : sanitize_text_field($_POST['_woosea_unit_pricing_base_measure']); $woocommerce_installment_months = empty($_POST['_woosea_installment_months']) ? '' : sanitize_text_field($_POST['_woosea_installment_months']); $woocommerce_installment_amount = empty($_POST['_woosea_installment_amount']) ? '' : sanitize_text_field($_POST['_woosea_installment_amount']); $woocommerce_condition = empty($_POST['_woosea_condition']) ? '' : sanitize_text_field($_POST['_woosea_condition']); $woocommerce_age_group = empty($_POST['_woosea_age_group']) ? '' : sanitize_text_field($_POST['_woosea_age_group']); $woocommerce_cost_of_good_sold = empty($_POST['_woosea_cost_of_good_sold']) ? '' : sanitize_text_field($_POST['_woosea_cost_of_good_sold']); $woocommerce_multipack = empty($_POST['_woosea_multipack']) ? '' : sanitize_text_field($_POST['_woosea_multipack']); $woocommerce_is_bundle = empty($_POST['_woosea_is_bundle']) ? '' : sanitize_text_field($_POST['_woosea_is_bundle']); $woocommerce_energy_efficiency_class = empty($_POST['_woosea_energy_efficiency_class']) ? '' : sanitize_text_field($_POST['_woosea_energy_efficiency_class']); $woocommerce_min_energy_efficiency_class = empty($_POST['_woosea_min_energy_efficiency_class']) ? '' : sanitize_text_field($_POST['_woosea_min_energy_efficiency_class']); $woocommerce_max_energy_efficiency_class = empty($_POST['_woosea_max_energy_efficiency_class']) ? '' : sanitize_text_field($_POST['_woosea_max_energy_efficiency_class']); $woocommerce_is_promotion = empty($_POST['_woosea_is_promotion']) ? '' : sanitize_text_field($_POST['_woosea_is_promotion']); $woocommerce_custom_field_0 = empty($_POST['_woosea_custom_field_0']) ? '' : sanitize_text_field($_POST['_woosea_custom_field_0']); $woocommerce_custom_field_1 = empty($_POST['_woosea_custom_field_1']) ? '' : sanitize_text_field($_POST['_woosea_custom_field_1']); $woocommerce_custom_field_2 = empty($_POST['_woosea_custom_field_2']) ? '' : sanitize_text_field($_POST['_woosea_custom_field_2']); $woocommerce_custom_field_3 = empty($_POST['_woosea_custom_field_3']) ? '' : sanitize_text_field($_POST['_woosea_custom_field_3']); $woocommerce_custom_field_4 = empty($_POST['_woosea_custom_field_4']) ? '' : sanitize_text_field($_POST['_woosea_custom_field_4']); if(!empty($_POST['_woosea_exclude_product'])){ $woocommerce_exclude_product = sanitize_text_field($_POST['_woosea_exclude_product']); } else { $woocommerce_exclude_product = "no";; } if(!empty($woocommerce_brand)) update_post_meta( $post_id, '_woosea_brand', $woocommerce_brand); if(!empty($woocommerce_mpn)) update_post_meta( $post_id, '_woosea_mpn', esc_attr($woocommerce_mpn)); if(!empty($woocommerce_upc)) update_post_meta( $post_id, '_woosea_upc', esc_attr($woocommerce_upc)); if(!empty($woocommerce_ean)) update_post_meta( $post_id, '_woosea_ean', esc_attr($woocommerce_ean)); if(!empty($woocommerce_gtin)) update_post_meta( $post_id, '_woosea_gtin', esc_attr($woocommerce_gtin)); if(!empty($woocommerce_color)) update_post_meta( $post_id, '_woosea_color', esc_attr($woocommerce_color)); if(!empty($woocommerce_size)) update_post_meta( $post_id, '_woosea_size', esc_attr($woocommerce_size)); if(!empty($woocommerce_gender)) update_post_meta( $post_id, '_woosea_gender', esc_attr($woocommerce_gender)); if(!empty($woocommerce_material)) update_post_meta( $post_id, '_woosea_material', esc_attr($woocommerce_material)); if(!empty($woocommerce_pattern)) update_post_meta( $post_id, '_woosea_pattern', esc_attr($woocommerce_pattern)); if(!empty($woocommerce_title)) update_post_meta( $post_id, '_woosea_optimized_title', $woocommerce_title); if(!empty($woocommerce_unit_pricing_measure)) update_post_meta( $post_id, '_woosea_unit_pricing_measure', $woocommerce_unit_pricing_measure); if(!empty($woocommerce_unit_pricing_base_measure)) update_post_meta( $post_id, '_woosea_unit_pricing_base_measure', $woocommerce_unit_pricing_base_measure); if(!empty($woocommerce_condition)) update_post_meta( $post_id, '_woosea_condition', $woocommerce_condition); if(!empty($woocommerce_age_group)) update_post_meta( $post_id, '_woosea_age_group', $woocommerce_age_group); if(!empty($woocommerce_installment_months)) update_post_meta( $post_id, '_woosea_installment_months', esc_attr($woocommerce_installment_months)); if(!empty($woocommerce_installment_amount)) update_post_meta( $post_id, '_woosea_installment_amount', esc_attr($woocommerce_installment_amount)); if(!empty($woocommerce_exclude_product)) update_post_meta( $post_id, '_woosea_exclude_product', esc_attr($woocommerce_exclude_product)); if(!empty($woocommerce_cost_of_good_sold)) update_post_meta( $post_id, '_woosea_cost_of_good_sold', esc_attr($woocommerce_cost_of_good_sold)); if(!empty($woocommerce_multipack)) update_post_meta( $post_id, '_woosea_multipack', esc_attr($woocommerce_multipack)); if(!empty($woocommerce_is_bundle)) update_post_meta( $post_id, '_woosea_is_bundle', esc_attr($woocommerce_is_bundle)); if(!empty($woocommerce_energy_efficiency_class)) update_post_meta( $post_id, '_woosea_energy_efficiency_class', esc_attr($woocommerce_energy_efficiency_class)); if(!empty($woocommerce_min_energy_efficiency_class)) update_post_meta( $post_id, '_woosea_min_energy_efficiency_class', esc_attr($woocommerce_min_energy_efficiency_class)); if(!empty($woocommerce_max_energy_efficiency_class)) update_post_meta( $post_id, '_woosea_max_energy_efficiency_class', esc_attr($woocommerce_max_energy_efficiency_class)); if(!empty($woocommerce_is_promotion)) update_post_meta( $post_id, '_woosea_is_promotion', $woocommerce_is_promotion); if(!empty($woocommerce_custom_field_0)) update_post_meta( $post_id, '_woosea_custom_field_0', $woocommerce_custom_field_0); if(!empty($woocommerce_custom_field_1)) update_post_meta( $post_id, '_woosea_custom_field_1', $woocommerce_custom_field_1); if(!empty($woocommerce_custom_field_2)) update_post_meta( $post_id, '_woosea_custom_field_2', $woocommerce_custom_field_2); if(!empty($woocommerce_custom_field_3)) update_post_meta( $post_id, '_woosea_custom_field_3', $woocommerce_custom_field_3); if(!empty($woocommerce_custom_field_4)) update_post_meta( $post_id, '_woosea_custom_field_4', $woocommerce_custom_field_4); } add_action( 'woocommerce_process_product_meta', 'woosea_save_custom_general_fields' ); /** * Create the unique identifier fields for variation products */ function woosea_custom_variable_fields( $loop, $variation_id, $variation ) { // Check if the option is enabled or not in the pluggin settings if( get_option('add_unique_identifiers') == "yes" ){ $extra_attributes = get_option( 'woosea_extra_attributes' ); // Variation Brand field if(array_key_exists('custom_attributes__woosea_brand', $extra_attributes) OR (!empty(get_post_meta( $variation->ID, '_woosea_brand', true )))){ woocommerce_wp_text_input( array( 'id' => '_woosea_variable_brand['.$loop.']', 'label' => __( '