| 1 |
<?php |
| 2 |
|
| 3 |
namespace StoreEngine\Models; |
| 4 |
|
| 5 |
use StoreEngine\Classes\AbstractModel; |
| 6 |
use StoreEngine\traits\Subscription as SubscriptionTrait; |
| 7 |
use StoreEngine\Utils\Helper; |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* @deprecated |
| 15 |
*/ |
| 16 |
class Subscription extends AbstractModel { |
| 17 |
use SubscriptionTrait; |
| 18 |
|
| 19 |
protected string $table = 'storeengine_subscriptions'; |
| 20 |
|
| 21 |
public function get_subscription_by_primary_key( $subscription_id ) { |
| 22 |
global $wpdb; |
| 23 |
|
| 24 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_subscription WHERE id = %d", $subscription_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 25 |
} |
| 26 |
|
| 27 |
public function get_user_subscription_by_price_id( $user_id, $price_id ) { |
| 28 |
global $wpdb; |
| 29 |
|
| 30 |
return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}storeengine_subscriptions WHERE user_id = %d AND price_id = %d", $user_id, $price_id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 31 |
} |
| 32 |
|
| 33 |
public function save( array $args = [] ) { |
| 34 |
if ( empty( $args['user_id'] ) || empty( $args['product_id'] || $args['price_id'] ) ) { |
| 35 |
return false; |
| 36 |
} |
| 37 |
|
| 38 |
// Create the subscription data |
| 39 |
global $wpdb; |
| 40 |
$table_name = $wpdb->prefix . 'storeengine_subscriptions'; |
| 41 |
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 42 |
$table_name, |
| 43 |
[ |
| 44 |
'user_id' => $args['user_id'], |
| 45 |
'product_id' => $args['product_id'], |
| 46 |
'price_id' => $args['price_id'], |
| 47 |
'status' => $args['status'], |
| 48 |
'interval' => $args['interval'], |
| 49 |
'interval_type' => $args['interval_type'], |
| 50 |
'hook' => $this->subscription_renewal_hook_name, |
| 51 |
] |
| 52 |
); |
| 53 |
|
| 54 |
return $wpdb->insert_id; |
| 55 |
} |
| 56 |
|
| 57 |
public function update( int $id, array $args ) { |
| 58 |
// TODO: Implement update() method. |
| 59 |
} |
| 60 |
|
| 61 |
public function delete( ?int $id = null ) { |
| 62 |
// TODO: Implement delete() method. |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public function get_subscriptions() { |
| 67 |
global $wpdb; |
| 68 |
$result = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 69 |
"SELECT {$wpdb->users}.user_login as username, |
| 70 |
{$wpdb->posts}.post_title as product, |
| 71 |
s.status, |
| 72 |
wp_postmeta.meta_value as price_name, |
| 73 |
s.created_at |
| 74 |
FROM {$wpdb->prefix}storeengine_subscriptions as s |
| 75 |
JOIN {$wpdb->users} ON s.user_id = wp_users.ID |
| 76 |
JOIN {$wpdb->posts} ON s.product_id = wp_posts.ID |
| 77 |
JOIN {$wpdb->postmeta} ON s.price_id = wp_postmeta.meta_id" |
| 78 |
); |
| 79 |
foreach ( $result as $key => $value ) { |
| 80 |
$value->price_name = unserialize( $value->price_name )['price_name']; // phpcs:ignore |
| 81 |
} |
| 82 |
|
| 83 |
return $result; |
| 84 |
} |
| 85 |
|
| 86 |
public function update_subscription( $subscription_id, $args ) { |
| 87 |
global $wpdb; |
| 88 |
$wpdb->update( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 89 |
$wpdb->prefix . 'storeengine_subscriptions', |
| 90 |
$args, |
| 91 |
[ 'id' => $subscription_id ] |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
|