CartRepository.php
7 months ago
ContactListRepository.php
8 months ago
FormRepository.php
8 months ago
Repository.php
8 months ago
RepositoryInterface.php
8 months ago
CartRepository.php
245 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Hostinger\Reach\Repositories; |
| 4 | |
| 5 | use Exception; |
| 6 | use Hostinger\Reach\Admin\Database\CartsTable; |
| 7 | use Hostinger\Reach\Models\Cart; |
| 8 | use Hostinger\Reach\Setup\Encrypt; |
| 9 | use WC_Customer; |
| 10 | use wpdb; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | die; |
| 14 | } |
| 15 | |
| 16 | class CartRepository extends Repository { |
| 17 | |
| 18 | public const ABANDONED_CART_TIME_IN_SECONDS = HOUR_IN_SECONDS; |
| 19 | public const OLD_CART_TIME_IN_SECONDS = MONTH_IN_SECONDS; |
| 20 | |
| 21 | public function __construct( wpdb $db, CartsTable $table ) { |
| 22 | parent::__construct( $db ); |
| 23 | $this->table = $table; |
| 24 | } |
| 25 | |
| 26 | public function all( array $where = array(), int $limit = 100 ): array { |
| 27 | $query = $this->build_query( $where, $limit ); |
| 28 | $results = $this->db->get_results( $query, ARRAY_A ); |
| 29 | |
| 30 | $carts = array(); |
| 31 | foreach ( $results as $result ) { |
| 32 | $carts[] = ( new Cart( $result ) )->to_array(); |
| 33 | } |
| 34 | |
| 35 | return $carts; |
| 36 | } |
| 37 | |
| 38 | public function old_carts( int $limit = 100, array $columns = array() ): array { |
| 39 | $where_conditions = array( |
| 40 | 'updated_at' => array( |
| 41 | 'operator' => '<', |
| 42 | 'value' => $this->get_updated_at_date( self::OLD_CART_TIME_IN_SECONDS ), |
| 43 | ), |
| 44 | ); |
| 45 | |
| 46 | return $this->get_cart_columns( $this->all( $where_conditions, $limit ), $columns ); |
| 47 | } |
| 48 | |
| 49 | public function active_abandoned_carts( int $limit = 100, array $columns = array() ): array { |
| 50 | $where_conditions = array( |
| 51 | 'status' => Cart::STATUS_ACTIVE, |
| 52 | 'updated_at' => array( |
| 53 | 'operator' => '<', |
| 54 | 'value' => $this->get_updated_at_date( self::ABANDONED_CART_TIME_IN_SECONDS ), |
| 55 | ), |
| 56 | ); |
| 57 | |
| 58 | return $this->get_cart_columns( $this->all( $where_conditions, $limit ), $columns ); |
| 59 | } |
| 60 | |
| 61 | public function exists( string $hash ): bool { |
| 62 | try { |
| 63 | $this->get( $hash ); |
| 64 | |
| 65 | return true; |
| 66 | } catch ( Exception $e ) { |
| 67 | return false; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @throws Exception |
| 73 | */ |
| 74 | public function get( string $hash = '' ): array { |
| 75 | $query = $this->db->prepare( 'SELECT * FROM %i WHERE hash = %s', $this->table->table_name(), $hash ); |
| 76 | $results = $this->db->get_results( $query, ARRAY_A ); |
| 77 | if ( ! empty( $results ) ) { |
| 78 | return ( new Cart( $results[0] ) )->to_array(); |
| 79 | } |
| 80 | |
| 81 | throw new Exception( 'Cart not found' ); |
| 82 | } |
| 83 | |
| 84 | public function get_by_customer( WC_Customer $customer, string $fallback_email = '' ): array { |
| 85 | $customer_email = $customer->get_email(); |
| 86 | $customer_id = $customer->get_id(); |
| 87 | |
| 88 | if ( ! $customer_email ) { |
| 89 | $customer_email = $customer->get_billing_email(); |
| 90 | } |
| 91 | |
| 92 | if ( ! $customer_email ) { |
| 93 | $customer_email = $fallback_email; |
| 94 | } |
| 95 | |
| 96 | if ( $customer_email ) { |
| 97 | $carts = $this->all( array( 'customer_email' => $this->encrypt_email( $customer_email ) ) ); |
| 98 | } elseif ( $customer_id > 0 ) { |
| 99 | $carts = $this->all( array( 'customer_id' => $customer_id ) ); |
| 100 | } |
| 101 | |
| 102 | if ( ! empty( $carts ) ) { |
| 103 | return $carts[0]; |
| 104 | } |
| 105 | |
| 106 | return array(); |
| 107 | } |
| 108 | |
| 109 | public function get_by_customer_id( int $customer_id ): array { |
| 110 | $carts = $this->all( array( 'customer_id' => $customer_id ) ); |
| 111 | if ( ! empty( $carts ) ) { |
| 112 | return $carts[0]; |
| 113 | } |
| 114 | |
| 115 | return array(); |
| 116 | } |
| 117 | |
| 118 | public function insert( array $fields ): bool { |
| 119 | |
| 120 | if ( ! isset( $fields['hash'] ) ) { |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | if ( $this->exists( $fields['hash'] ) ) { |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if ( isset( $fields['customer_email'] ) ) { |
| 129 | $fields['customer_email'] = $this->encrypt_email( $fields['customer_email'] ); |
| 130 | } |
| 131 | |
| 132 | return $this->db->insert( $this->table->table_name(), $fields ); |
| 133 | } |
| 134 | |
| 135 | public function delete( string $hash ): bool { |
| 136 | if ( ! $this->exists( $hash ) ) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return $this->db->delete( $this->table->table_name(), array( 'hash' => $hash ) ); |
| 141 | } |
| 142 | |
| 143 | public function update( array $fields ): bool { |
| 144 | if ( ! isset( $fields['hash'] ) ) { |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | if ( isset( $fields['customer_email'] ) ) { |
| 149 | $fields['customer_email'] = $this->encrypt_email( $fields['customer_email'] ); |
| 150 | } |
| 151 | |
| 152 | $data = array_diff_key( $fields, array_flip( array( 'hash' ) ) ); |
| 153 | if ( empty( $data ) ) { |
| 154 | return false; |
| 155 | } |
| 156 | |
| 157 | $has_changes = ! empty( $this->get_cart_diff( $fields['hash'], $data ) ); |
| 158 | if ( ! $has_changes ) { |
| 159 | return false; |
| 160 | } |
| 161 | |
| 162 | return $this->db->update( |
| 163 | $this->table->table_name(), |
| 164 | $data, |
| 165 | array( 'hash' => $fields['hash'] ) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | public function get_cart_diff( string $hash, array $data ): array { |
| 170 | $changes = array(); |
| 171 | try { |
| 172 | $current_data = $this->get( $hash ); |
| 173 | foreach ( $data as $key => $value ) { |
| 174 | if ( $key === 'hash' || $key === 'updated_at' ) { |
| 175 | continue; |
| 176 | } |
| 177 | |
| 178 | if ( ! isset( $current_data[ $key ] ) ) { |
| 179 | $changes[ $key ] = $value; |
| 180 | continue; |
| 181 | } |
| 182 | |
| 183 | if ( is_array( $current_data[ $key ] ) ) { |
| 184 | $current_value = wp_json_encode( $current_data[ $key ] ); |
| 185 | } else { |
| 186 | $current_value = $current_data[ $key ]; |
| 187 | } |
| 188 | |
| 189 | if ( $current_value !== $value ) { |
| 190 | $changes[ $key ] = $value; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | return $changes; |
| 195 | } catch ( Exception $e ) { |
| 196 | return $changes; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | public function get_cart_item( array $item ): array { |
| 201 | return array( |
| 202 | 'product_id' => $item['variation_id'] ?: $item['product_id'], |
| 203 | 'quantity' => $item['quantity'], |
| 204 | ); |
| 205 | } |
| 206 | |
| 207 | public function set_status( string $hash, string $status ): bool { |
| 208 | if ( ! in_array( $status, Cart::STATUSES, true ) ) { |
| 209 | return false; |
| 210 | } |
| 211 | |
| 212 | try { |
| 213 | return $this->update( |
| 214 | array( |
| 215 | 'hash' => $hash, |
| 216 | 'status' => $status, |
| 217 | ) |
| 218 | ); |
| 219 | } catch ( Exception $e ) { |
| 220 | return false; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | private function encrypt_email( string $email ): string { |
| 225 | return Encrypt::encrypt( sanitize_email( $email ) ); |
| 226 | } |
| 227 | |
| 228 | protected function get_cart_columns( array $carts, array $columns = array() ): array { |
| 229 | if ( empty( $columns ) ) { |
| 230 | return $carts; |
| 231 | } |
| 232 | |
| 233 | return array_map( |
| 234 | function ( $cart ) use ( $columns ) { |
| 235 | return array_intersect_key( $cart, array_flip( $columns ) ); |
| 236 | }, |
| 237 | $carts |
| 238 | ); |
| 239 | } |
| 240 | |
| 241 | protected function get_updated_at_date( int $threshold ): string { |
| 242 | return gmdate( 'Y-m-d H:i:s', time() - $threshold ); |
| 243 | } |
| 244 | } |
| 245 |