| 1 |
<?php |
| 2 |
/** |
| 3 |
* CartFlows Flows Stats ajax actions. |
| 4 |
* |
| 5 |
* @package CartFlows |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace CartflowsAdmin\AdminLegacyCore\Ajax; |
| 9 |
|
| 10 |
// Exit if accessed directly. |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
use CartflowsAdmin\AdminLegacyCore\Ajax\AjaxBase; |
| 16 |
use CartflowsAdmin\AdminLegacyCore\Inc\AdminHelper; |
| 17 |
use Automattic\WooCommerce\Utilities\OrderUtil; |
| 18 |
|
| 19 |
/** |
| 20 |
* Class Flows. |
| 21 |
*/ |
| 22 |
class FlowsStats extends AjaxBase { |
| 23 |
|
| 24 |
/** |
| 25 |
* Instance |
| 26 |
* |
| 27 |
* @access private |
| 28 |
* @var object Class object. |
| 29 |
* @since 1.0.0 |
| 30 |
*/ |
| 31 |
private static $instance; |
| 32 |
|
| 33 |
/** |
| 34 |
* Initiator |
| 35 |
* |
| 36 |
* @since 1.0.0 |
| 37 |
* @return object initialized object of class. |
| 38 |
*/ |
| 39 |
public static function get_instance() { |
| 40 |
if ( ! isset( self::$instance ) ) { |
| 41 |
self::$instance = new self(); |
| 42 |
} |
| 43 |
return self::$instance; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Register ajax events. |
| 48 |
* |
| 49 |
* @since 1.0.0 |
| 50 |
* @return void |
| 51 |
*/ |
| 52 |
public function register_ajax_events() { |
| 53 |
|
| 54 |
$ajax_events = array( |
| 55 |
'get_all_flows_stats', |
| 56 |
); |
| 57 |
|
| 58 |
$this->init_ajax_events( $ajax_events ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
/** |
| 63 |
* Get all Flows Stats. |
| 64 |
*/ |
| 65 |
public function get_all_flows_stats() { |
| 66 |
|
| 67 |
$response_data = array( 'messsage' => $this->get_error_msg( 'permission' ) ); |
| 68 |
|
| 69 |
if ( ! current_user_can( 'cartflows_manage_flows_steps' ) ) { |
| 70 |
wp_send_json_error( $response_data ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Nonce verification |
| 75 |
*/ |
| 76 |
if ( ! check_ajax_referer( 'cartflows_get_all_flows_stats', 'security', false ) ) { |
| 77 |
$response_data = array( 'messsage' => $this->get_error_msg( 'nonce' ) ); |
| 78 |
wp_send_json_error( $response_data ); |
| 79 |
} |
| 80 |
|
| 81 |
if ( ! wcf()->is_woo_active ) { |
| 82 |
wp_send_json_success( |
| 83 |
array( |
| 84 |
'flow_stats' => array(), |
| 85 |
'order_data' => array(), |
| 86 |
'last_week_orders' => array(), |
| 87 |
'recent_orders' => array(), |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
$start_date = isset( $_POST['date_from'] ) ? sanitize_text_field( wp_unslash( $_POST['date_from'] ) ) : ''; |
| 93 |
$end_date = isset( $_POST['date_to'] ) ? sanitize_text_field( wp_unslash( $_POST['date_to'] ) ) : ''; |
| 94 |
$flow_id = isset( $_POST['flow_id'] ) ? absint( $_POST['flow_id'] ) : 0; |
| 95 |
$screen_type = isset( $_POST['screen_type'] ) ? sanitize_text_field( wp_unslash( $_POST['screen_type'] ) ) : ''; |
| 96 |
$comparison_range_type = isset( $_POST['comparison_range_type'] ) ? sanitize_text_field( wp_unslash( $_POST['comparison_range_type'] ) ) : ''; |
| 97 |
$dashboard_analytics_data = AdminHelper::get_earnings( $start_date, $end_date, $flow_id, $screen_type, $comparison_range_type ); |
| 98 |
|
| 99 |
$start_date = gmdate( 'Y-m-d H:i:s', strtotime( $start_date . '00:00:00' ) ); |
| 100 |
$end_date = gmdate( 'Y-m-d H:i:s', strtotime( $end_date . '23:59:59' ) ); |
| 101 |
|
| 102 |
global $wpdb; |
| 103 |
|
| 104 |
$decimal_point_pos = wc_get_price_decimals(); |
| 105 |
$order_revenue_by_date = ''; |
| 106 |
|
| 107 |
// Build WHERE clause and prepare values array. |
| 108 |
$where_clause_for_flow_id = ''; |
| 109 |
if ( ! empty( $flow_id ) ) { |
| 110 |
$where_clause_for_flow_id .= $wpdb->prepare( ' AND om.meta_value = %s', $flow_id ); |
| 111 |
} |
| 112 |
|
| 113 |
if ( class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && OrderUtil::custom_orders_table_usage_is_enabled() ) { |
| 114 |
// HPOS usage is enabled. |
| 115 |
$order_date_key = 'date_created_gmt'; |
| 116 |
$order_status_key = 'status'; |
| 117 |
$order_id_key = 'order_id'; |
| 118 |
$order_table = $wpdb->prefix . 'wc_orders'; |
| 119 |
$order_meta_table = $wpdb->prefix . 'wc_orders_meta'; |
| 120 |
$order_type_key = 'type'; |
| 121 |
$order_table_id = 'id'; |
| 122 |
|
| 123 |
//phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 124 |
$order_revenue_by_date = $wpdb->get_results( |
| 125 |
$wpdb->prepare( |
| 126 |
"SELECT DATE_FORMAT(o.$order_date_key, '%%Y-%%m-%%d') AS OrderDate, ROUND(SUM(o.total_amount), $decimal_point_pos) AS Revenue |
| 127 |
FROM $order_table o |
| 128 |
INNER JOIN $order_meta_table om ON o.$order_table_id = om.$order_id_key AND om.meta_key IN ('_wcf_flow_id', '_cartflows_parent_flow_id') |
| 129 |
WHERE o.$order_type_key = 'shop_order' |
| 130 |
AND o.$order_status_key IN ('wc-completed', 'wc-processing') |
| 131 |
AND o.$order_date_key >= %s |
| 132 |
AND o.$order_date_key <= %s |
| 133 |
$where_clause_for_flow_id |
| 134 |
GROUP BY OrderDate |
| 135 |
ORDER BY OrderDate ASC", |
| 136 |
$start_date, |
| 137 |
$end_date |
| 138 |
) |
| 139 |
); |
| 140 |
//phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 141 |
|
| 142 |
} else { |
| 143 |
// Traditional CPT-based orders are in use. |
| 144 |
$order_date_key = 'post_date'; |
| 145 |
$order_status_key = 'post_status'; |
| 146 |
$order_id_key = 'post_id'; |
| 147 |
$order_table = $wpdb->prefix . 'posts'; |
| 148 |
$order_meta_table = $wpdb->prefix . 'postmeta'; |
| 149 |
$order_type_key = 'post_type'; |
| 150 |
$order_table_id = 'ID'; |
| 151 |
|
| 152 |
//phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 153 |
$order_revenue_by_date = $wpdb->get_results( |
| 154 |
$wpdb->prepare( |
| 155 |
"SELECT DATE_FORMAT(o.$order_date_key, '%%Y-%%m-%%d') AS OrderDate, ROUND(SUM(m.meta_value), $decimal_point_pos) AS Revenue |
| 156 |
FROM $order_table o |
| 157 |
INNER JOIN $order_meta_table m ON o.$order_table_id = m.$order_id_key AND m.meta_key = '_order_total' |
| 158 |
INNER JOIN $order_meta_table om ON o.$order_table_id = om.$order_id_key AND om.meta_key IN ('_wcf_flow_id', '_cartflows_parent_flow_id') |
| 159 |
WHERE o.$order_type_key = 'shop_order' |
| 160 |
AND o.$order_status_key IN ('wc-completed', 'wc-processing') |
| 161 |
AND m.meta_key = '_order_total' |
| 162 |
AND o.$order_date_key >= %s |
| 163 |
AND o.$order_date_key <= %s |
| 164 |
$where_clause_for_flow_id |
| 165 |
GROUP BY OrderDate |
| 166 |
ORDER BY OrderDate ASC", |
| 167 |
$start_date, |
| 168 |
$end_date |
| 169 |
) |
| 170 |
); |
| 171 |
//phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 172 |
} |
| 173 |
|
| 174 |
//phpcs:disable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 175 |
$orders_by_date = $wpdb->get_results( |
| 176 |
$wpdb->prepare( |
| 177 |
"SELECT DATE_FORMAT($order_date_key, '%%Y-%%m-%%d') AS OrderDate, COUNT(*) AS OrderCount |
| 178 |
FROM $order_table o |
| 179 |
INNER JOIN $order_meta_table om ON o.$order_table_id = om.$order_id_key AND om.meta_key IN ('_wcf_flow_id', '_cartflows_parent_flow_id') |
| 180 |
WHERE $order_type_key = 'shop_order' |
| 181 |
AND $order_status_key IN ('wc-completed', 'wc-processing') |
| 182 |
AND $order_date_key >= %s |
| 183 |
AND $order_date_key <= %s |
| 184 |
$where_clause_for_flow_id |
| 185 |
GROUP BY OrderDate |
| 186 |
ORDER BY OrderDate ASC |
| 187 |
", |
| 188 |
$start_date, |
| 189 |
$end_date |
| 190 |
) |
| 191 |
); |
| 192 |
//phpcs:enable WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 193 |
|
| 194 |
$dashboard_analytics_data['orders_by_date'] = $orders_by_date; |
| 195 |
$dashboard_analytics_data['revenue_by_date'] = $order_revenue_by_date; |
| 196 |
|
| 197 |
$response = array( |
| 198 |
'flow_stats' => $dashboard_analytics_data, |
| 199 |
'recent_orders' => $this->get_recent_orders(), |
| 200 |
); |
| 201 |
|
| 202 |
wp_send_json_success( $response ); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get all orders placed via CartFlows. |
| 207 |
* |
| 208 |
* @return array $recent_orders_data array of orders. |
| 209 |
*/ |
| 210 |
public function get_recent_orders() { |
| 211 |
|
| 212 |
$recent_orders = wc_get_orders( |
| 213 |
array( |
| 214 |
'limit' => 4, |
| 215 |
'orderby' => 'date', |
| 216 |
'order' => 'DESC', |
| 217 |
'meta_key' => '_wcf_flow_id', //phpcs:ignore |
| 218 |
'meta_compare' => 'EXISTS', |
| 219 |
) |
| 220 |
); |
| 221 |
|
| 222 |
$recent_orders_data = array(); |
| 223 |
|
| 224 |
foreach ( $recent_orders as $recent_order ) { |
| 225 |
$recent_orders_data[] = array( |
| 226 |
'order_id' => $recent_order->get_id(), |
| 227 |
'customer_name' => $recent_order->get_billing_first_name() . ' ' . $recent_order->get_billing_last_name(), |
| 228 |
'customer_email' => $recent_order->get_billing_email(), |
| 229 |
'payment_method' => $recent_order->get_payment_method_title(), |
| 230 |
'order_total' => get_woocommerce_currency_symbol( $recent_order->get_currency() ) . $recent_order->get_total(), |
| 231 |
'order_status' => ucfirst( $recent_order->get_status() ), |
| 232 |
'order_currency' => $recent_order->get_currency(), |
| 233 |
'order_date' => wc_format_datetime( $recent_order->get_date_created(), 'M j, Y' ), |
| 234 |
'order_time' => wc_format_datetime( $recent_order->get_date_created(), 'g:i A' ), |
| 235 |
); |
| 236 |
} |
| 237 |
|
| 238 |
return $recent_orders_data; |
| 239 |
} |
| 240 |
} |
| 241 |
|