| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Classes; |
| 4 |
|
| 5 |
use StoreEngine\Utils\Helper; |
| 6 |
|
| 7 |
class DownloadPermission { |
| 8 |
|
| 9 |
protected string $table; |
| 10 |
|
| 11 |
protected int $id; |
| 12 |
|
| 13 |
protected array $data = [ |
| 14 |
'user_id' => 0, |
| 15 |
'order_id' => 0, |
| 16 |
'download_id' => '', |
| 17 |
'product_id' => 0, |
| 18 |
'price_id' => null, |
| 19 |
'variation_id' => null, |
| 20 |
'downloads_remaining' => null, |
| 21 |
'access_granted' => null, |
| 22 |
'access_expires' => null, |
| 23 |
'download_count' => 0, |
| 24 |
]; |
| 25 |
protected array $new_data = []; |
| 26 |
protected array $extra_data = [ |
| 27 |
'order_key' => '', |
| 28 |
'billing_email' => '', |
| 29 |
]; |
| 30 |
|
| 31 |
public const CACHE_KEY = 'storeengine_download_permission_'; |
| 32 |
public const CACHE_GROUP = 'storeengine_download_permissions'; |
| 33 |
|
| 34 |
public function __construct( int $id = 0 ) { |
| 35 |
global $wpdb; |
| 36 |
$this->table = $wpdb->prefix . Helper::DB_PREFIX . 'downloadable_product_permissions'; |
| 37 |
$this->id = $id; |
| 38 |
} |
| 39 |
|
| 40 |
public function get() { |
| 41 |
$has_cache = wp_cache_get( self::CACHE_KEY . $this->id, self::CACHE_GROUP ); |
| 42 |
if ( $has_cache ) { |
| 43 |
return $this->set_data( $has_cache ); |
| 44 |
} |
| 45 |
|
| 46 |
global $wpdb; |
| 47 |
//phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 48 |
$result = $wpdb->get_row( |
| 49 |
$wpdb->prepare( "SELECT |
| 50 |
dp.*, |
| 51 |
oa.email AS billing_email, |
| 52 |
op.order_key AS order_key |
| 53 |
FROM |
| 54 |
$this->table dp |
| 55 |
INNER JOIN {$wpdb->prefix}storeengine_orders o ON o.id = dp.order_id |
| 56 |
INNER JOIN {$wpdb->prefix}storeengine_order_operational_data op ON op.order_id = o.id |
| 57 |
INNER JOIN {$wpdb->prefix}storeengine_order_addresses oa ON oa.order_id = o.id |
| 58 |
AND oa.address_type = 'billing' |
| 59 |
WHERE |
| 60 |
dp.id = %d", $this->id ) |
| 61 |
); |
| 62 |
//phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 63 |
if ( ! $result ) { |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
return $this->set_data( $result ); |
| 68 |
} |
| 69 |
|
| 70 |
public function set_data( $data ): self { |
| 71 |
wp_cache_set( self::CACHE_KEY . $this->id, $data, self::CACHE_GROUP ); |
| 72 |
|
| 73 |
$this->id = (int) $data->id; |
| 74 |
$this->data = [ |
| 75 |
'user_id' => (int) $data->user_id, |
| 76 |
'order_id' => (int) $data->order_id, |
| 77 |
'download_id' => $data->download_id, |
| 78 |
'product_id' => (int) $data->product_id, |
| 79 |
'price_id' => $data->price_id ? (int) $data->price_id : null, |
| 80 |
'variation_id' => $data->variation_id ? (int) $data->variation_id : null, |
| 81 |
'downloads_remaining' => $data->downloads_remaining, |
| 82 |
'access_granted' => $data->access_granted, |
| 83 |
'access_expires' => $data->access_expires, |
| 84 |
'download_count' => (int) $data->download_count, |
| 85 |
]; |
| 86 |
|
| 87 |
if ( $data->order_key ) { |
| 88 |
$this->extra_data['order_key'] = $data->order_key; |
| 89 |
} |
| 90 |
|
| 91 |
if ( $data->billing_email ) { |
| 92 |
$this->extra_data['billing_email'] = $data->billing_email; |
| 93 |
} |
| 94 |
|
| 95 |
return $this; |
| 96 |
} |
| 97 |
|
| 98 |
public function save() { |
| 99 |
if ( empty( $this->new_data ) ) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
global $wpdb; |
| 104 |
$data = array_merge( $this->data, $this->new_data ); |
| 105 |
$placeholder = [ '%d', '%d', '%s', '%d', '%d', '%d', '%d', '%s', '%s', '%d' ]; |
| 106 |
|
| 107 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 108 |
if ( 0 === $this->id ) { |
| 109 |
$wpdb->insert( $this->table, $data, $placeholder ); |
| 110 |
$this->id = $wpdb->insert_id; |
| 111 |
} else { |
| 112 |
$wpdb->update( $this->table, $data, [ 'id' => $this->get_id() ], $placeholder, [ '%d' ] ); |
| 113 |
} |
| 114 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 115 |
wp_cache_delete( self::CACHE_KEY . $this->id, self::CACHE_GROUP ); |
| 116 |
} |
| 117 |
|
| 118 |
public function delete(): bool { |
| 119 |
if ( 0 === $this->get_id() ) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
global $wpdb; |
| 124 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 125 |
$wpdb->delete( $this->table, [ 'id' => $this->get_id() ], [ '%d' ] ); |
| 126 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 127 |
wp_cache_delete( self::CACHE_KEY . $this->id, self::CACHE_GROUP ); |
| 128 |
wp_cache_flush_group( self::CACHE_GROUP ); |
| 129 |
|
| 130 |
return true; |
| 131 |
} |
| 132 |
|
| 133 |
public function get_id(): int { |
| 134 |
return $this->id; |
| 135 |
} |
| 136 |
|
| 137 |
public function get_user_id(): int { |
| 138 |
return $this->get_prop( 'product_id', 0 ); |
| 139 |
} |
| 140 |
|
| 141 |
public function get_order_id(): int { |
| 142 |
return $this->get_prop( 'order_id', 0 ); |
| 143 |
} |
| 144 |
|
| 145 |
public function get_download_id(): string { |
| 146 |
return $this->get_prop( 'download_id' ); |
| 147 |
} |
| 148 |
|
| 149 |
public function get_product_id() { |
| 150 |
return $this->get_prop( 'product_id', null ); |
| 151 |
} |
| 152 |
|
| 153 |
public function get_product_title(): string { |
| 154 |
return get_the_title( $this->get_product_id() ); |
| 155 |
} |
| 156 |
|
| 157 |
public function get_price_id() { |
| 158 |
return $this->get_prop( 'price_id', null ); |
| 159 |
} |
| 160 |
|
| 161 |
public function get_variation_id() { |
| 162 |
return $this->get_prop( 'variation_id', null ); |
| 163 |
} |
| 164 |
|
| 165 |
public function get_downloads_remaining() { |
| 166 |
return $this->get_prop( 'downloads_remaining', null ); |
| 167 |
} |
| 168 |
|
| 169 |
public function get_file_name(): string { |
| 170 |
$downloadable_files = get_post_meta( $this->get_product_id(), '_storeengine_product_downloadable_files', true ); |
| 171 |
if ( empty( $downloadable_files ) ) { |
| 172 |
return ''; |
| 173 |
} |
| 174 |
$downloadable_files = maybe_unserialize( $downloadable_files ); |
| 175 |
if ( ! is_array( $downloadable_files ) ) { |
| 176 |
return ''; |
| 177 |
} |
| 178 |
|
| 179 |
$downloadable_files = array_filter( $downloadable_files, fn( $file ) => $file['id'] === $this->get_download_id() ); |
| 180 |
|
| 181 |
return empty( $downloadable_files ) ? '' : reset( $downloadable_files )['name']; |
| 182 |
} |
| 183 |
|
| 184 |
public function get_order_key() { |
| 185 |
return $this->extra_data['order_key'] ?? ''; |
| 186 |
} |
| 187 |
|
| 188 |
public function get_billing_email() { |
| 189 |
return $this->extra_data['billing_email'] ?? ''; |
| 190 |
} |
| 191 |
|
| 192 |
public function get_download_url(): string { |
| 193 |
$args = [ |
| 194 |
'download_file' => $this->get_product_id(), |
| 195 |
'order' => $this->get_order_key(), |
| 196 |
'uid' => hash( 'sha256', $this->get_billing_email() ), |
| 197 |
'key' => $this->get_download_id(), |
| 198 |
]; |
| 199 |
|
| 200 |
return add_query_arg( $args, home_url( '/' ) ); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* @throws \Exception |
| 205 |
*/ |
| 206 |
public function get_access_granted_date(): ?StoreengineDatetime { |
| 207 |
$access_granted = $this->get_prop( 'access_granted', null ); |
| 208 |
|
| 209 |
return ! empty( $access_granted ) ? new StoreengineDatetime( $access_granted ) : null; |
| 210 |
} |
| 211 |
|
| 212 |
protected function get_prop( string $name, $default = '' ) { |
| 213 |
if ( array_key_exists( $name, $this->new_data ) ) { |
| 214 |
return $this->new_data[ $name ]; |
| 215 |
} |
| 216 |
|
| 217 |
return $this->data[ $name ] ?? $default; |
| 218 |
} |
| 219 |
|
| 220 |
public function set_user_id( int $value ) { |
| 221 |
$this->new_data['user_id'] = $value; |
| 222 |
} |
| 223 |
|
| 224 |
public function set_order_id( int $value ) { |
| 225 |
$this->new_data['order_id'] = $value; |
| 226 |
} |
| 227 |
|
| 228 |
public function set_download_id( string $value ) { |
| 229 |
$this->new_data['download_id'] = $value; |
| 230 |
} |
| 231 |
|
| 232 |
public function set_product_id( int $value ) { |
| 233 |
$this->new_data['product_id'] = $value; |
| 234 |
} |
| 235 |
|
| 236 |
public function set_price_id( $value ) { |
| 237 |
$this->new_data['price_id'] = $value; |
| 238 |
} |
| 239 |
|
| 240 |
public function set_variation_id( $value ) { |
| 241 |
$this->new_data['variation_id'] = $value; |
| 242 |
} |
| 243 |
|
| 244 |
public function set_downloads_remaining( $value ) { |
| 245 |
$this->new_data['downloads_remaining'] = $value; |
| 246 |
} |
| 247 |
|
| 248 |
public function set_access_granted( $value ) { |
| 249 |
$this->new_data['access_granted'] = $value; |
| 250 |
} |
| 251 |
} |
| 252 |
|