| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\models; |
| 4 |
|
| 5 |
use StoreEngine\Classes\AbstractModel; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; |
| 9 |
} |
| 10 |
|
| 11 |
class ShippingZoneMethods extends AbstractModel { |
| 12 |
protected string $table = 'storeengine_shipping_zones'; |
| 13 |
|
| 14 |
|
| 15 |
public function all() { |
| 16 |
global $wpdb; |
| 17 |
|
| 18 |
return $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}storeengine_shipping_zones" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 19 |
} |
| 20 |
|
| 21 |
public function get( int $id ) { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_shipping_zone_methods WHERE id = %d", $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 25 |
} |
| 26 |
|
| 27 |
public function save( array $args = [] ) { |
| 28 |
$defaults = [ |
| 29 |
'name' => '', |
| 30 |
'zone_id' => 0, |
| 31 |
'cost' => 0, |
| 32 |
'is_enabled' => 0, |
| 33 |
'type' => '', |
| 34 |
'tax' => 0, |
| 35 |
'description' => '', |
| 36 |
]; |
| 37 |
|
| 38 |
$args = wp_parse_args( $args, $defaults ); |
| 39 |
|
| 40 |
global $wpdb; |
| 41 |
|
| 42 |
$wpdb->insert( $this->prefix . 'storeengine_shipping_zone_methods', $args ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 43 |
|
| 44 |
return $this->get( $wpdb->insert_id ); |
| 45 |
} |
| 46 |
|
| 47 |
public function update( int $id, array $args ) { |
| 48 |
$defaults = [ |
| 49 |
'name' => '', |
| 50 |
'zone_id' => 0, |
| 51 |
'cost' => 0, |
| 52 |
'is_enabled' => 0, |
| 53 |
'type' => '', |
| 54 |
'tax' => 0, |
| 55 |
'description' => '', |
| 56 |
]; |
| 57 |
|
| 58 |
$args = wp_parse_args( $args, $defaults ); |
| 59 |
|
| 60 |
global $wpdb; |
| 61 |
|
| 62 |
$wpdb->update( $this->prefix . 'storeengine_shipping_zone_methods', $args, [ 'id' => $id ] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 63 |
|
| 64 |
return $this->get( $id ); |
| 65 |
} |
| 66 |
|
| 67 |
public function delete( ?int $id = null ) { |
| 68 |
global $wpdb; |
| 69 |
|
| 70 |
$item_to_delete = $this->get( $id ); |
| 71 |
|
| 72 |
$result = $wpdb->delete( $this->prefix . 'storeengine_shipping_zone_methods', [ 'id' => $id ] ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 73 |
|
| 74 |
if ( ! $result ) { |
| 75 |
return new \WP_Error( 'db-delete-failed', __( 'Unable to delete shipping zone method.', 'storeengine' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
return $item_to_delete; |
| 79 |
} |
| 80 |
|
| 81 |
public function get_by_zone_id( $zone_id ) { |
| 82 |
global $wpdb; |
| 83 |
|
| 84 |
return $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_shipping_zone_methods WHERE zone_id = %d", $zone_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 85 |
} |
| 86 |
} |
| 87 |
|