Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 20 additions & 35 deletions includes/classes/class-cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,14 @@ public function atbdp_custom_schedule_cron() {
*/

private function featured_listing_followup() {
if ( directorist_is_force_disabled_featured_listings() ) {
return;
}

if ( directorist_is_monetization_enabled() && directorist_is_featured_listing_enabled() ) {
$featured_days = get_directorist_option( 'featured_listing_time', 30 );
$featured_days = (int) get_directorist_option( 'featured_listing_time', 30 );
$orders = directorist_order_repository();
$current_time = directorist_now()->getTimestamp();
// Define the query
$args = [
'post_type' => ATBDP_POST_TYPE,
Expand All @@ -120,44 +126,23 @@ private function featured_listing_followup() {
// Start the Loop
if ( $listings->found_posts ) {
foreach ( $listings->posts as $listing ) {
$order = $this->get_order_by_listing( $listing->ID );
if ( $order ) {
$days = round( abs( strtotime( current_time( 'mysql' ) ) - strtotime( $order[0]->post_date ) ) / 86400 );
if ( $days > $featured_days ) {
do_action( 'atbdp_listing_featured_to_general', $listing->ID );
update_post_meta( $listing->ID, '_featured', '' );
}
}
}
}
}
}
$order = $orders->get_latest_paid_featured_order_by_listing_id( $listing->ID );

private function get_order_by_listing( $listing_id ) {
$args = [
'post_type' => ATBDP_ORDER_POST_TYPE,
'posts_per_page' => 1,
'post_status' => 'publish',
'meta_query' => [
'relation' => 'AND',
[
'key' => '_listing_id',
'value' => $listing_id,
],
[
'key' => '_payment_status',
'value' => 'completed',
],
],
];
if ( ! $order ) {
continue;
}

$listings = new WP_Query( $args );
$expiration = ! empty( $order->expires_at )
? new \Directorist\Helpers\DateTime( $order->expires_at )
: ( new \Directorist\Helpers\DateTime( $order->created_at ) )->add_days( $featured_days );

// Start the Loop
if ( $listings->found_posts ) {
return $listings->posts;
if ( $expiration->getTimestamp() <= $current_time ) {
do_action( 'atbdp_listing_featured_to_general', $listing->ID );
update_post_meta( $listing->ID, '_featured', '' );
}
}
}
}
return '';
}

/**
Expand Down
44 changes: 39 additions & 5 deletions includes/classes/class-featured-listing-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,20 @@
class FeaturedListingCheckout {
const CHECKOUT_TYPE = 'featured_listing';

public function __construct() {
add_filter( 'directorist_checkout_types', [$this, 'add_checkout_type'] );
public function __construct() {
add_filter( 'directorist_order_data', [ $this, 'handle_order_data' ] );
add_action( 'directorist_after_order_update', [$this, 'handle_after_order_update'] );
add_filter( 'directorist_payment_receipt_order_items', [$this, 'handle_payment_receipt_order_items'], 10, 2 );

add_action( 'plugins_loaded', [ $this, 'register_hooks' ], 20 );
}

public function register_hooks() {
if ( directorist_is_force_disabled_featured_listings() ) {
return;
}

add_filter( 'directorist_checkout_types', [$this, 'add_checkout_type'] );
add_action( 'directorist_after_order_update', [$this, 'handle_after_order_update'] );
add_filter( 'directorist_checkout_validation', [$this, 'validate_checkout'], 10, 2 );
add_action( 'directorist_checkout_table', [$this, 'handle_checkout_table'], 10, 4 );
add_filter( 'directorist_checkout_subtotal', [$this, 'handle_checkout_subtotal'], 10, 3 );
Expand Down Expand Up @@ -156,7 +165,11 @@ public function handle_after_order_update( OrderDTO $dto ) {

// Publish the listing if it's pending
$listing = get_post( $dto->get_listing_id() );


if ( $listing ) {
$this->updated_listing_expiration( $listing, $dto );
}

if ( $listing && 'publish' !== $listing->post_status ) {
directorist_set_listing_status( $dto->get_listing_id(), 'publish' );
}
Expand All @@ -165,6 +178,27 @@ public function handle_after_order_update( OrderDTO $dto ) {
}
}

private function updated_listing_expiration( $listing, OrderDTO $order ) {
if ( ! $order->is_initialized( 'expires_at' ) || get_post_meta( $listing->ID, '_never_expire', true ) ) {
return;
}

$order_expiration = $order->get_expires_at();
$listing_expiration = get_post_meta( $listing->ID, '_expiry_date', true );
$listing_expiration_date = $listing_expiration
? date_create_from_format( 'Y-m-d H:i:s', $listing_expiration, wp_timezone() )
: false;

if ( $listing_expiration_date
&& $listing_expiration === $listing_expiration_date->format( 'Y-m-d H:i:s' )
&& $listing_expiration_date->getTimestamp() >= $order_expiration->getTimestamp()
) {
return;
}

update_post_meta( $listing->ID, '_expiry_date', $order_expiration->format( 'Y-m-d H:i:s' ) );
}

public function handle_payment_receipt_order_items( array $order_items, OrderDTO $order ) {
if ( ! $this->is_featured_order( $order ) ) {
return $order_items;
Expand Down Expand Up @@ -206,7 +240,7 @@ public function handle_checkout_product_name( string $product_name, OrderDTO $dt
}

public function is_featured_order( OrderDTO $order_dto ): bool {
if ( ! $order_dto->is_initialized( 'ref_type' ) || ! $order_dto->is_initialized( 'ref' ) || ! $order_dto->is_initialized( 'listing_id' ) ) {
if ( ! $order_dto->is_initialized( 'ref_type' ) || ! $order_dto->is_initialized( 'listing_id' ) ) {
return false;
}

Expand Down
7 changes: 7 additions & 0 deletions includes/directorist-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ function directorist_is_guest_submission_enabled() {
return (bool) get_directorist_option( 'guest_listings', false );
}

/**
* @return bool
*/
function directorist_is_force_disabled_featured_listings() {
return (bool) apply_filters( 'directorist_is_force_disabled_featured_listings', false );
}

function directorist_is_featured_listing_enabled( array $context = [] ) {
return (bool) apply_filters( 'directorist_is_featured_listing_enabled', get_directorist_option( 'enable_featured_listing' ), $context );
}
Expand Down
11 changes: 11 additions & 0 deletions includes/repositories/order-repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ public function listing_has_paid_featured_order( int $listing_id ): bool {
return $order ? true : false;
}

public function get_latest_paid_featured_order_by_listing_id( int $listing_id ) {
return $this->get_query_builder()
->select( 'd_order.created_at', 'd_order.expires_at' )
->where( 'd_order.listing_id', $listing_id )
->where( 'd_order.is_featured_listing', 1 )
->where( 'd_order.ref_type', 'featured_listing' )
->where( 'd_order.status', OrderStatus::PAID )
->order_by_desc( 'd_order.id' )
->first();
}

protected function get_orders( Builder $query, Read $dto ) {
$query = apply_filters( 'directorist_order_list_query', $query, $dto );

Expand Down
Loading