=> $currency_symbol,
'total_orders' => $order_count,
'total_revenue_raw' => $gross_sale,
'total_revenue' => str_replace( ' ', '', wc_price( (float) $gross_sale ) ),
'total_bump_revenue' => '0',
'total_offers_revenue' => '0',
'total_visits' => '0',
);
}
/**
* Get orders data for flow.
*
* @since 1.6.15
*
* @param string $start_date start date.
* @param string $end_date end date.
* @param string $flow_id flow id.
* @return wc_order object.
*/
public static function get_orders_by_flow( $start_date, $end_date, $flow_id = '' ) {
global $wpdb;
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) {
// HPOS usage is enabled.
$conditions = array(
'tb1.type' => 'shop_order',
);
$order_date_key = 'date_created_gmt';
$order_status_key = 'status';
$order_id_key = 'order_id';
$order_table = $wpdb->prefix . 'wc_orders';
$order_meta_table = $wpdb->prefix . 'wc_orders_meta';
} else {
// Traditional CPT-based orders are in use.
$conditions = array(
'tb1.post_type' => 'shop_order',
);
$order_date_key = 'post_date';
$order_status_key = 'post_status';
$order_id_key = 'post_id';
$order_table = $wpdb->prefix . 'posts';
$order_meta_table = $wpdb->prefix . 'postmeta';
}
$where = self::get_items_query_where( $conditions );
if ( ! empty( $flow_id ) ) {
$where .= $wpdb->prepare( ' AND tb2.meta_value = %s', $flow_id );
}
// Security: Use $wpdb->prepare() for date parameters to prevent SQL injection.
$where .= $wpdb->prepare(
' AND ( tb1.' . $order_date_key . " BETWEEN IF (tb2.meta_key='wcf-analytics-reset-date'>%s, tb2.meta_key, %s) AND %s )", // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$start_date,
$start_date,
$end_date
);
$where .= " AND ( ( tb2.meta_key = '_wcf_flow_id' ) OR ( tb2.meta_key = '_cartflows_parent_flow_id' ) )";
$where .= ' AND tb1.' . $order_status_key . " IN ( 'wc-completed', 'wc-processing' )";
$query = 'SELECT tb1.ID, DATE( tb1.' . $order_date_key . ' ) date, tb2.meta_value FROM ' . $order_table . ' tb1
INNER JOIN ' . $order_meta_table . ' tb2
ON tb1.ID = tb2.' . $order_id_key . ' '
. $where;
return $wpdb->get_results( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery
}
/**
* Prepare where items for query.
*
* @param array $conditions conditions to prepare WHERE query.
* @return string
*/
public static function get_items_query_where( $conditions ) {
global $wpdb;
$where_conditions = array();
foreach ( $conditions as $key => $condition ) {
if ( false !== stripos( $key, 'IN' ) ) {
$where_conditions[] = $key . $wpdb->prepare( '( %s )', $condition );
} else {
$where_conditions[] = $key . $wpdb->prepare( '= %s', $condition );
}
}
if ( ! empty( $where_conditions ) ) {
return 'WHERE 1 = 1 AND ' . implode( ' AND ', $where_conditions );
} else {
return '';
}
}
/**
* Check is error in the received response.
*
* @param object $response Received API Response.
* @return array $result Error result.
* @since 1.11.8
*/
public static function has_api_error( $response ) {
$result = array(
'error' => false,
'error_message' => __( 'Ooops! Something went wrong. Please open a support ticket from the website.', 'cartflows' ),
'call_to_action' => __( 'No error found.', 'cartflows' ),
'error_code' => 0,
);
if ( is_wp_error( $response ) ) {
$msg = $response->get_error_message();
$error_code = $response->get_error_code();
if ( 'http_request_failed' === $error_code ) {
/* translators: %1$s: HTML, %2$s: HTML */
$msg = $msg . '
' . sprintf( __( 'Request timeout error. Please check if the firewall or any security plugin is blocking the outgoing HTTP/HTTPS requests to templates.cartflows.com or not. %1$1sTo resolve this issue, please check this %2$2sarticle%3$3s.', 'cartflows' ), '
', '', '' );
}
$result['error'] = true;
$result['call_to_action'] = $msg;
$result['error_code'] = $error_code;
} elseif ( ! empty( wp_remote_retrieve_response_code( $response ) ) && ! in_array( wp_remote_retrieve_response_code( $response ), array( 200, 201, 204 ), true ) ) {
$error_message = ! empty( wp_remote_retrieve_response_message( $response ) ) ? wp_remote_retrieve_response_message( $response ) : '';
$error_body = ! empty( wp_remote_retrieve_body( $response ) ) ? wp_kses( wp_remote_retrieve_body( $response ), '
' ) : '';
if ( false !== strpos( $error_body, 'MalCare' ) ) {
/* translators: %1$s: HTML, %2$s: HTML, %3$s: HTML */
$error_message = $error_message . '
' . sprintf( __( 'Sorry for the inconvenience, but your website seems to be having trouble connecting to our server. %1$s Please open a technical %2$ssupport ticket%3$s and share the server\'s outgoing IP address.', 'cartflows' ), '
', '', '' );
$ip_address = self::get_valid_ip_address();
$result['message'] = ! empty( $ip_address ) ? __( 'Server\'s outgoing IP address: ', 'cartflows' ) . $ip_address : '';
}
$result['error'] = true;
$result['call_to_action'] = $error_message;
$result['error_code'] = wp_remote_retrieve_response_code( $response );
} else {
$result['response_code'] = wp_remote_retrieve_response_code( $response );
}
return $result;
}
/**
* Convert the array format in the WooCommerce's expected format for display.
* Received array format is array( 0=>key, 1=>Key_value ) expected is array( key=>key_value )
* This is because of the core rules structure which we have created.
*
* @param array $options Array of key & value in the array format.
* @return array $options
*/
public static function sanitize_array_values( $options ) {
if ( is_array( $options ) ) {
$sanitized_options = array();
foreach ( $options as $option_key => $option_value ) {
$sanitized_options[ trim( $option_value ) ] = trim( $option_value );
}
$options = $sanitized_options;
}
return $options;
}
/**
* Get the IP address of the user's server.
* To check weather the IP is not blocked by any firewall.
*
* Note: This IP address is not stored any where in the database.
*
* @return string A valid IP address.
*/
public static function get_valid_ip_address() {
if ( isset( $_SERVER['HTTP_X_REAL_IP'] ) ) { //phpcs:ignore WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
return self::validate_ip_address( sanitize_text_field( wp_unslash( $_SERVER['HTTP_X_REAL_IP'] ) ) );
} elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) { //phpcs:ignore WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
return self::validate_ip_address( sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) ); //phpcs:ignore WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders, WordPressVIPMinimum.Variables.RestrictedVariables.cache_constraints___SERVER__REMOTE_ADDR__
} elseif ( isset( $_SERVER['SERVER_ADDR'] ) ) { //phpcs:ignore WordPressVIPMinimum.Variables.ServerVariables.UserControlledHeaders
return self::validate_ip_address( sanitize_text_field( wp_unslash( $_SERVER['SERVER_ADDR'] ) ) );
} else {
return '';
}
}
/**
* Validate the provided IP address safe to printing.
*
* @param string $ip_address IP address to validate.
* @return string $ip_address Validated IP address for display/use.
*/
public static function validate_ip_address( $ip_address ) {
$ip_address = filter_var( $ip_address, FILTER_VALIDATE_IP, array() );
return $ip_address ? $ip_address : '';
}
/**
* Track funnel creation method for analytics.
*
* @param string $creation_method The method used to create the funnel (e.g., 'scratch', 'ready_made_template').
* @return void
*/
public static function track_funnel_creation_method( $creation_method ) {
$funnel_creation_stats = get_option(
'cartflows_funnel_creation_method',
array(
'scratch' => 0,
'ready_made_template' => 0,
)
);
if ( isset( $funnel_creation_stats[ $creation_method ] ) ) {
$funnel_creation_stats[ $creation_method ]++;
}
update_option( 'cartflows_funnel_creation_method', $funnel_creation_stats );
}
/**
* Determine whether to show the CodeMirror code editor fields.
*
* Returns true when the custom script migration status is 'completed',
* meaning users should see the new separate JS and CSS code editors
* instead of the old combined textarea.
*
* @since 2.2.2
* @return bool
*/
public static function should_show_code_editor() {
return 'completed' === \CartFlows_Helper::get_script_migration_status();
}
/**
* Get the field type for custom script fields based on migration status.
*
* Returns 'code' after migration is completed, 'textarea' otherwise.
*
* @since 2.2.2
* @return string 'code' or 'textarea'
*/
public static function get_custom_script_field_type() {
return self::should_show_code_editor() ? 'code' : 'textarea';
}
}