| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
/** |
| 6 |
* @deprecated |
| 7 |
*/ |
| 8 |
class Orders extends AbstractWpdb { |
| 9 |
|
| 10 |
protected string $table = 'storeengine_orders'; |
| 11 |
|
| 12 |
protected int $page; |
| 13 |
protected int $limit; |
| 14 |
|
| 15 |
public function __construct( int $page = 1, int $per_page = 10 ) { |
| 16 |
$this->page = max( 1, $page ); |
| 17 |
$this->limit = $per_page; |
| 18 |
parent::__construct(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* @param $conditions |
| 23 |
* |
| 24 |
* @return array |
| 25 |
* @throws Exceptions\StoreEngineException |
| 26 |
* @deprecated |
| 27 |
*/ |
| 28 |
public function get( $conditions = array() ): array { |
| 29 |
global $wpdb; |
| 30 |
|
| 31 |
$conditions = array_merge( array( |
| 32 |
'status' => array( |
| 33 |
'condition' => '!=', |
| 34 |
'formatter' => '%s', |
| 35 |
'value' => 'draft', |
| 36 |
), |
| 37 |
'type' => array( |
| 38 |
'condition' => '=', |
| 39 |
'formatter' => '%s', |
| 40 |
'value' => 'order', |
| 41 |
), |
| 42 |
), $conditions ); |
| 43 |
|
| 44 |
$offset = ( $this->page - 1 ) * $this->limit; |
| 45 |
|
| 46 |
// @TODO cache (must be same as Abstract Entity (order object format) to reduce multiple query. |
| 47 |
|
| 48 |
$sql_conditions = $this->generate_conditions( $conditions ); |
| 49 |
$results = $wpdb->get_results( $wpdb->prepare( "{$this->query()} WHERE {$sql_conditions} GROUP BY o.id ORDER BY o.id DESC LIMIT %d OFFSET %d;", $this->limit, $offset ), ARRAY_A ); // phpcs:ignore |
| 50 |
|
| 51 |
if ( ! $results ) { |
| 52 |
return []; |
| 53 |
} |
| 54 |
|
| 55 |
$orders = []; |
| 56 |
|
| 57 |
foreach ( $results as $result ) { |
| 58 |
if ( ! empty( $result['o_id'] ) ) { |
| 59 |
$order = new Order( $result['o_id'] ); |
| 60 |
$orders[] = $order; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
return $orders; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* @param $conditions |
| 69 |
* |
| 70 |
* @return int |
| 71 |
* @deprecated |
| 72 |
*/ |
| 73 |
public function get_total_orders_count( $conditions = array() ) { |
| 74 |
global $wpdb; |
| 75 |
|
| 76 |
$conditions = array_merge( array( |
| 77 |
'status' => array( |
| 78 |
'condition' => '!=', |
| 79 |
'formatter' => '%s', |
| 80 |
'value' => 'draft', |
| 81 |
), |
| 82 |
'type' => array( |
| 83 |
'condition' => '=', |
| 84 |
'formatter' => '%s', |
| 85 |
'value' => 'order', |
| 86 |
), |
| 87 |
), $conditions ); |
| 88 |
|
| 89 |
$sql_conditions = $this->generate_conditions( $conditions ); |
| 90 |
|
| 91 |
// phpcs:disable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- query prepared above. |
| 92 |
$result = $wpdb->get_row( "SELECT COUNT(DISTINCT o.id) as order_totals FROM {$wpdb->prefix}storeengine_orders o |
| 93 |
LEFT JOIN {$wpdb->prefix}storeengine_order_addresses b ON b.order_id = o.id AND b.address_type = 'billing' |
| 94 |
LEFT JOIN {$wpdb->prefix}storeengine_order_addresses s ON s.order_id = o.id AND s.address_type = 'shipping' |
| 95 |
LEFT JOIN {$wpdb->prefix}storeengine_order_operational_data p ON p.order_id = o.id |
| 96 |
LEFT JOIN {$wpdb->prefix}storeengine_orders_meta m ON m.order_id = o.id WHERE $sql_conditions;" ); |
| 97 |
// phpcs:enable PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 98 |
|
| 99 |
if ( ! $result ) { |
| 100 |
return 0; |
| 101 |
} |
| 102 |
|
| 103 |
return $result->order_totals; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* @param $conditions |
| 108 |
* @param $implode |
| 109 |
* |
| 110 |
* @return array|string |
| 111 |
* @deprecated |
| 112 |
*/ |
| 113 |
protected function generate_conditions( $conditions, $implode = true ) { |
| 114 |
$sql = []; |
| 115 |
foreach ( $conditions as $operator => $condition ) { |
| 116 |
if ( in_array( $operator, array( 'and', 'or' ), true ) ) { |
| 117 |
$operator = strtoupper( $operator ); |
| 118 |
$sql[ $operator ] = '(' . implode( " $operator ", $this->generate_conditions( $condition, false ) ) . ')'; |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
$arr = []; |
| 123 |
$arr[ $operator ] = $condition; |
| 124 |
$sql[] = $this->generate_sql_from_conditions( $arr ); |
| 125 |
} |
| 126 |
|
| 127 |
return $implode ? implode( ' AND ', $sql ) : $sql; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* @param array $condition |
| 132 |
* |
| 133 |
* @return string |
| 134 |
* |
| 135 |
* @deprecated |
| 136 |
*/ |
| 137 |
protected function generate_sql_from_conditions( array $condition ): string { |
| 138 |
global $wpdb; |
| 139 |
|
| 140 |
$column = array_keys( $condition ); |
| 141 |
$column = reset( $column ); |
| 142 |
|
| 143 |
$condition = array_values( $condition )[0]; |
| 144 |
|
| 145 |
$place_holder = "$column {$condition['condition']} {$condition['formatter']}"; |
| 146 |
|
| 147 |
return $wpdb->prepare( $place_holder, $condition['value'] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared -- returning prepared statements. |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* @return string |
| 152 |
* |
| 153 |
* @deprecated |
| 154 |
*/ |
| 155 |
protected function query(): string { |
| 156 |
global $wpdb; |
| 157 |
|
| 158 |
return " |
| 159 |
SELECT |
| 160 |
o.id as o_id, |
| 161 |
o.parent_order_id as parent_order_id, |
| 162 |
o.*, |
| 163 |
b.id as b_id, |
| 164 |
s.id as s_id, |
| 165 |
p.id as operational_id, |
| 166 |
b.first_name as b_first_name, |
| 167 |
b.last_name as b_last_name, |
| 168 |
b.company as b_company, |
| 169 |
b.address_1 as b_address_1, |
| 170 |
b.address_2 as b_address_2, |
| 171 |
b.city as b_city, |
| 172 |
b.state as b_state, |
| 173 |
b.postcode as b_postcode, |
| 174 |
b.country as b_country, |
| 175 |
b.email as b_email, |
| 176 |
b.phone as b_phone, |
| 177 |
s.first_name as s_first_name, |
| 178 |
s.last_name as s_last_name, |
| 179 |
s.company as s_company, |
| 180 |
s.address_1 as s_address_1, |
| 181 |
s.address_2 as s_address_2, |
| 182 |
s.city as s_city, |
| 183 |
s.state as s_state, |
| 184 |
s.postcode as s_postcode, |
| 185 |
s.country as s_country, |
| 186 |
s.email as s_email, |
| 187 |
s.phone as s_phone, |
| 188 |
p.* |
| 189 |
FROM {$wpdb->prefix}storeengine_orders o |
| 190 |
LEFT JOIN {$wpdb->prefix}storeengine_order_addresses b ON b.order_id = o.id AND b.address_type = 'billing' |
| 191 |
LEFT JOIN {$wpdb->prefix}storeengine_order_addresses s ON s.order_id = o.id AND s.address_type = 'shipping' |
| 192 |
LEFT JOIN {$wpdb->prefix}storeengine_order_operational_data p ON p.order_id = o.id |
| 193 |
LEFT JOIN {$wpdb->prefix}storeengine_orders_meta m ON m.order_id = o.id |
| 194 |
"; |
| 195 |
} |
| 196 |
} |
| 197 |
|