| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
use StoreEngine\Utils\Helper; |
| 6 |
|
| 7 |
class Integration { |
| 8 |
|
| 9 |
protected string $table; |
| 10 |
|
| 11 |
protected int $id; |
| 12 |
|
| 13 |
protected array $data = [ |
| 14 |
'product_id' => 0, |
| 15 |
'price_id' => 0, |
| 16 |
'integration_id' => 0, |
| 17 |
'provider' => '', |
| 18 |
]; |
| 19 |
protected array $new_data = []; |
| 20 |
|
| 21 |
public const CACHE_KEY = 'storeengine_integration_'; |
| 22 |
public const CACHE_GROUP = 'storeengine_integrations'; |
| 23 |
|
| 24 |
public function __construct( int $id = 0 ) { |
| 25 |
global $wpdb; |
| 26 |
$this->table = $wpdb->prefix . Helper::DB_PREFIX . 'integrations'; |
| 27 |
$this->id = $id; |
| 28 |
} |
| 29 |
|
| 30 |
public function get() { |
| 31 |
$has_cache = wp_cache_get( self::CACHE_KEY . $this->id, self::CACHE_GROUP ); |
| 32 |
if ( $has_cache ) { |
| 33 |
return $this->set_data( $has_cache ); |
| 34 |
} |
| 35 |
|
| 36 |
global $wpdb; |
| 37 |
//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 38 |
$result = $wpdb->get_row( |
| 39 |
$wpdb->prepare( "SELECT * FROM $this->table WHERE id = %d", $this->id ) |
| 40 |
); |
| 41 |
//phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 42 |
if ( ! $result ) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
return $this->set_data( $result ); |
| 47 |
} |
| 48 |
|
| 49 |
public function set_data( $data ): Integration { |
| 50 |
wp_cache_set( self::CACHE_KEY . $this->id, $data, self::CACHE_GROUP ); |
| 51 |
|
| 52 |
$this->id = (int) $data->id; |
| 53 |
$this->data = [ |
| 54 |
'product_id' => (int) $data->product_id, |
| 55 |
'price_id' => (int) $data->price_id, |
| 56 |
'integration_id' => (int) $data->integration_id, |
| 57 |
'provider' => $data->provider, |
| 58 |
]; |
| 59 |
|
| 60 |
return $this; |
| 61 |
} |
| 62 |
|
| 63 |
public function get_data(): array { |
| 64 |
return array_merge( [ 'id' => $this->id ], $this->data ); |
| 65 |
} |
| 66 |
|
| 67 |
public function save() { |
| 68 |
if ( empty( $this->new_data ) ) { |
| 69 |
return; |
| 70 |
} |
| 71 |
|
| 72 |
global $wpdb; |
| 73 |
$data = array_merge( $this->data, $this->new_data ); |
| 74 |
$placeholder = [ '%d', '%d', '%d', '%s' ]; |
| 75 |
|
| 76 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 77 |
if ( 0 === $this->id ) { |
| 78 |
$wpdb->insert( $this->table, $data, $placeholder ); |
| 79 |
$this->id = $wpdb->insert_id; |
| 80 |
do_action( 'storeengine/integrations/created', $this ); |
| 81 |
} else { |
| 82 |
$wpdb->update( $this->table, $data, [ 'id' => $this->get_id() ], $placeholder, [ '%d' ] ); |
| 83 |
} |
| 84 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 85 |
$this->delete_cache(); |
| 86 |
} |
| 87 |
|
| 88 |
public function delete(): bool { |
| 89 |
if ( 0 === $this->get_id() ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
global $wpdb; |
| 94 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 95 |
$wpdb->delete( $this->table, [ 'id' => $this->get_id() ], [ '%d' ] ); |
| 96 |
$this->delete_cache(); |
| 97 |
|
| 98 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
public function delete_by_price_and_integration(): bool { |
| 103 |
if ( 0 === $this->get_price_id() || empty( $this->get_provider() ) || 0 === $this->get_integration_id() ) { |
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
global $wpdb; |
| 108 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 109 |
$wpdb->delete( $this->table, [ |
| 110 |
'price_id' => $this->get_price_id(), |
| 111 |
'provider' => $this->get_provider(), |
| 112 |
'integration_id' => $this->get_integration_id(), |
| 113 |
], [ '%d', '%s', '%d' ] ); |
| 114 |
$this->delete_cache(); |
| 115 |
|
| 116 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
public function get_id(): int { |
| 121 |
return $this->id; |
| 122 |
} |
| 123 |
|
| 124 |
public function get_product_id(): int { |
| 125 |
return $this->get_prop( 'product_id', 0 ); |
| 126 |
} |
| 127 |
|
| 128 |
public function get_price_id(): int { |
| 129 |
return $this->get_prop( 'price_id', 0 ); |
| 130 |
} |
| 131 |
|
| 132 |
public function get_integration_id(): int { |
| 133 |
return $this->get_prop( 'integration_id', 0 ); |
| 134 |
} |
| 135 |
|
| 136 |
public function get_provider(): string { |
| 137 |
return $this->get_prop( 'provider' ); |
| 138 |
} |
| 139 |
|
| 140 |
public function set_product_id( int $value ) { |
| 141 |
$this->new_data['product_id'] = $value; |
| 142 |
} |
| 143 |
|
| 144 |
public function set_price_id( int $value ) { |
| 145 |
$this->new_data['price_id'] = $value; |
| 146 |
} |
| 147 |
|
| 148 |
public function set_integration_id( int $value ) { |
| 149 |
$this->new_data['integration_id'] = $value; |
| 150 |
} |
| 151 |
|
| 152 |
public function set_provider( string $value ) { |
| 153 |
$this->new_data['provider'] = $value; |
| 154 |
} |
| 155 |
|
| 156 |
protected function get_prop( string $name, $default = '' ) { |
| 157 |
if ( array_key_exists( $name, $this->new_data ) ) { |
| 158 |
return $this->new_data[ $name ]; |
| 159 |
} |
| 160 |
|
| 161 |
return $this->data[ $name ] ?? $default; |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
private function delete_cache(): void { |
| 168 |
wp_cache_delete( self::CACHE_KEY . $this->id, self::CACHE_GROUP ); |
| 169 |
wp_cache_delete( AbstractProduct::CACHE_KEY . $this->get_product_id() . '_integrations', AbstractProduct::CACHE_GROUP ); |
| 170 |
wp_cache_delete( 'storeengine_price_' . $this->get_price_id() . '_integrations_integrations', 'storeengine_product_price' ); |
| 171 |
wp_cache_flush_group( IntegrationRepository::CACHE_GROUP ); |
| 172 |
} |
| 173 |
|
| 174 |
} |
| 175 |
|