<?php
/**
* Utils.
*
* @package CARTFLOWS
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Class Cartflows_Pro_Utils.
*/
class Cartflows_Pro_Utils {
/**
* Member Variable
*
* @var instance
*/
private static $instance;
/**
* Initiator
*/
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Clone step by ID
*
* @param int $post_id Step id.
*
* @return int|bool
*/
public function clone_step( $post_id ) {
global $wpdb;
/**
* And all the original post data then
*/
$post = get_post( $post_id );
/**
* If post data exists, create the post duplicate
*/
if ( isset( $post ) && null !== $post ) {
/**
* Assign current user to be the new post author
*/
$current_user = wp_get_current_user();
$new_post_author = $current_user->ID;
/**
* New post data array
*/
$args = array(
'comment_status' => $post->comment_status,
'ping_status' => $post->ping_status,
'post_author' => $new_post_author,
'post_content' => $post->post_content,
'post_excerpt' => $post->post_excerpt,
'post_name' => $post->post_name,
'post_parent' => $post->post_parent,
'post_password' => $post->post_password,
'post_status' => $post->post_status,
'post_title' => $post->post_title . ' Clone',
'post_type' => $post->post_type,
'to_ping' => $post->to_ping,
'menu_order' => $post->menu_order,
);
/**
* Insert the post
*/
$new_step_id = wp_insert_post( $args );
/**
* Get all current post terms ad set them to the new post
*/
// returns array of taxonomy names for post type, ex array("category", "post_tag");.
$taxonomies = get_object_taxonomies( $post->post_type );
foreach ( $taxonomies as $taxonomy ) {
$post_terms = wp_get_object_terms( $post_id, $taxonomy, array( 'fields' => 'slugs' ) );
wp_set_object_terms( $new_step_id, $post_terms, $taxonomy, false );
}
/**
* Duplicate all post meta just in two SQL queries
*/
// @codingStandardsIgnoreStart
$post_meta_infos = $wpdb->get_results(
"SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE post_id=$post_id"
);
// @codingStandardsIgnoreEnd
if ( ! empty( $post_meta_infos ) ) {
$sql_query = "INSERT INTO $wpdb->postmeta (post_id, meta_key, meta_value) VALUES ";
$sql_query_sel = array();
foreach ( $post_meta_infos as $meta_info ) {
$meta_key = $meta_info->meta_key;
if ( '_wp_old_slug' === $meta_key ) {
continue;
}
$meta_value = addslashes( $meta_info->meta_value );
$sql_query_sel[] = "($new_step_id, '$meta_key', '$meta_value')";
}
$sql_query .= implode( ',', $sql_query_sel );
// @codingStandardsIgnoreStart
$wpdb->query( $sql_query );
// @codingStandardsIgnoreEnd
}
/* Clear Page Builder Cache */
wcf()->utils->clear_cache();
/**
* Return new step id
*/
return $new_step_id;
}
return false;
}
/**
* We are using this function mostly in ajax on checkout page
*
* @param array $post_data post data.
* @return bool
*/
public function get_checkout_id_from_data( $post_data ) {
if ( isset( $post_data['_wcf_checkout_id'] ) ) { //phpcs:ignore
$checkout_id = filter_var( wp_unslash( $post_data['_wcf_checkout_id'] ), FILTER_SANITIZE_NUMBER_INT ); //phpcs:ignore
return intval( $checkout_id );
}
return false;
}
/**
* We are using this function mostly in ajax on checkout page
*
* @param array $post_data post data.
* @return bool
*/
public function get_flow_id_from_data( $post_data ) {
if ( isset( $post_data['_wcf_flow_id'] ) ) { //phpcs:ignore
$flow_id = filter_var( wp_unslash( $post_data['_wcf_flow_id'] ), FILTER_SANITIZE_NUMBER_INT ); //phpcs:ignore
return intval( $flow_id );
}
return false;
}
/**
* Fetch updated fragments after cart update.
*
* @param string $new_key product key.
* @param array $data extra data.
* @return array.
*/
public static function get_fragments( $new_key, $data = array() ) {
ob_start();
woocommerce_order_review();
$woocommerce_order_review = ob_get_clean();
$response = array(
'cart_total' => WC()->cart->total,
'cart_item_key' => $new_key,
'fragments' => apply_filters(
'woocommerce_update_order_review_fragments',
array(
'.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
)
),
);
if ( ! empty( $data ) ) {
$response['cartflows_data'] = $data;
}
return $response;
}
/**
* Prepare response for facebook.
*
* @param integer $product_id product id.
* @return array
*/
public static function prepare_fb_response( $product_id ) {
$response = array();
$product = wc_get_product( $product_id );
$add_to_cart['content_type'] = 'product';
$add_to_cart['content_category'][] = wp_strip_all_tags( wc_get_product_category_list( $product->get_id() ) );
$add_to_cart['currency'] = get_woocommerce_currency();
$add_to_cart['language'] = get_bloginfo( 'language' );
$add_to_cart['userAgent'] = isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '';
$add_to_cart['value'] = $product->get_price();
$add_to_cart['content_name'] = $product->get_title();
$add_to_cart['content_ids'][] = $product->get_id();
$response['added_to_cart'] = $add_to_cart;
$response['current_cart'] = Cartflows_Helper::prepare_cart_data_fb_response();
return $response;
}
/**
* Prepare response for Google Analytics.
*
* @param integer $product_id product id.
* @return array
*/
public static function prepare_ga_response( $product_id ) {
$response = array();
$product = wc_get_product( $product_id );
$product_data['id'] = $product_id;
$product_data['name'] = $product->get_title();
$product_data['category'][] = wp_strip_all_tags( wc_get_product_category_list( $product->get_id() ) );
$product_data['price'] = $product->get_price();
$response['add_to_cart'] = $product_data;
$response['remove_from_cart'] = $product_data;
return $response;
}
/**
* Check is offer page
*
* @param int $step_id step ID.
* @return bool
*/
public function check_is_offer_page( $step_id ) {
$step_type = $this->get_step_type( $step_id );
if ( 'upsell' === $step_type || 'downsell' === $step_type ) {
return true;
}
return false;
}
/**
* Get offer data
*
* @param int $step_id step ID.
* @param int $selected_product_id product ID.
* @param int $input_qty qty.
* @param int $order_id parent order id.
* @return array
*/
public function get_offer_data( $step_id, $selected_product_id = '', $input_qty = '', $order_id = 0 ) {
$data = array();
$subscription_types = array( 'subscription', 'variable-subscription', 'subscription_variation' );
$amount_diff = 0;
$cancel_main_order = false;
$order = '';
$product_id = 0;
if ( empty( $selected_product_id ) ) {
$offer_product = wcf_pro()->options->get_offers_meta_value( $step_id, 'wcf-offer-product' );
if ( isset( $offer_product[0] ) ) {
$product_id = $offer_product[0];
}
} else {
$product_id = $selected_product_id;
}
$product = wc_get_product( $product_id );
if ( $product ) {
$product_type = $product->get_type();
$original_price = $product->get_price( 'edit' );
$custom_price = $original_price;
if ( ! empty( $input_qty ) ) {
/* Product Quantity */
$product_qty = intval( $input_qty );
} else {
$product_qty = intval( wcf_pro()->options->get_offers_meta_value( $step_id, 'wcf-offer-quantity' ) );
}
/* Offer Discount */
$discount_type = wcf_pro()->options->get_offers_meta_value( $step_id, 'wcf-offer-discount' );
if ( ! empty( $discount_type ) ) {
$discount_value = floatval( wcf_pro()->options->get_offers_meta_value( $step_id, 'wcf-offer-discount-value' ) );
if ( 'discount_percent' === $discount_type ) {
if ( $discount_value > 0 ) {
$custom_price = $custom_price - ( ( $custom_price * $discount_value ) / 100 );
}
} elseif ( 'discount_price' === $discount_type ) {
if ( $discount_value > 0 ) {
$custom_price = $custom_price - $discount_value;
}
}
}
/* Set unit discount price */
$unit_price = $custom_price;
$unit_price_tax = $custom_price;
Loading ...