Skip to content

Commit

Permalink
initial attempt (#898)
Browse files Browse the repository at this point in the history
merging into dev to test functionality.
  • Loading branch information
jaspercroome authored Apr 16, 2024
1 parent ce9e9f9 commit 237427d
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 5 deletions.
63 changes: 63 additions & 0 deletions client-mu-plugins/goodbids/src/classes/Auctions/Auctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ class Auctions {
*/
const AUCTION_CLOSE_META_KEY = '_auction_close';

/**
* @since 1.0.0
* @var string
*/
const CRON_AUCTION_ENDING_SOON_CHECK_HOOK = 'goodbids_auction_ending_soon_event'

/**
* @since 1.0.0
* @var Wizard
Expand Down Expand Up @@ -390,6 +396,63 @@ public function get_unclaimed_reward_auction_emails(): array {
return $reminder_emails;
}

/**
* Get any live auctions that are within a given range of their extension time of closing.
*
* @since 1.0.0
*
* @param Date $threshold_start
* @param Date $threshold_end
*
*
* @return array
*/
public function get_auctions_ending_soon($threshold_start, $threshold_end): array {
$query_args =
[
'meta_query'=>[
[
'key'=> self::AUCTION_CLOSED_META_KEY,
'value'=>'0',
'compare'=>'='
],
[
'key'=> self::AUCTION_CLOSE_META_KEY,
'value'=> $threshold_start,
'compare'=> '>='
],
['key'=> self::AUCTION_CLOSE_META_KEY,
'value'=> $threshold_end ,
'compare'=>'<='
],
]
]
$ending_soon = $this->get_all( $query_args );

return $ending_soon-> posts;

}

/**
* Send emails for any auctions ending in a certain window of time
*
* @since 1.0.0
*
* @return array
*/
public function get_auctions_ending_soon_emails(): array {
$threshold_start = new DateTime()->modify('+3 hours') // TODO update to dynamic values (1/3 of auction extension window)
$threshold_end = new DateTime()->modify('+4 hours') // TODO update to dynamic values (1/3 of auction extension window)
$ending_soon_emails = [];

$ending_soon = $this->get_auctions_ending_soon($threshold_start, $threshold_end)

if (count($ending_soon)){
array_push( $ending_soon_emails, ...$ending_soon );
}
return $ending_soon_emails;
}

/**
* Get the default template for Auctions.
*
Expand Down
41 changes: 36 additions & 5 deletions client-mu-plugins/goodbids/src/classes/Auctions/Cron.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,24 @@ public function __construct() {
}

// Set up Cron Schedules.
$this->cron_intervals['1min'] = [
'interval' => MINUTE_IN_SECONDS,
'name' => '1min',
'display' => __( 'Once Every Minute', 'goodbids' ),
];
$this->cron_intervals['30sec'] = [
'interval' => 30,
'name' => '30sec',
'display' => __( 'Every 30 Seconds', 'goodbids' ),
];
$this->cron_intervals['1min'] = [
'interval' => MINUTE_IN_SECONDS,
'name' => '1min',
'display' => __( 'Once Every Minute', 'goodbids' ),
];
$this->cron_intervals['30min'] = [
'interval'=> 30 * MINUTE_IN_SECONDS,
'name' => '30min',
'display'=> __('Once Every 30 Minutes', 'goodbids')];
$this->cron_intervals['1hr'] = [
'interval'=> 60 * MINUTE_IN_SECONDS,
'name' => '1hr',
'display'=> __('Every Hour', 'goodbids')];
$this->cron_intervals['daily'] = [
'interval' => DAY_IN_SECONDS,
'name' => 'Daily',
Expand Down Expand Up @@ -192,6 +200,29 @@ function (): void {
);
}

/**
* Schedule a cron job that runs every 30 minutes to see if there are any auctions ending soon
*
* @since 1.0.0
*
* @return void
*/
private function schedule_auction_ending_soon_check(): void {
add_action(
'init',
function (): void {
if (wp_next_scheduled(Auctions::CRON_AUCTION_ENDING_SOON_CHECK_HOOK)){
return;
}
wp_schedule_event(
strtotime( current_time( 'mysql') ),
$this->cron_intervals['1hr']['name'],
Auctions::CRON_AUCTION_ENDING_SOON_CHECK_HOOK
);
}
);
}

/**
* Check for Auctions that are starting during cron hook.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Auction is Ending Soon: Email the Watchers when an Auction is 1 hour from closing.
*
* @since 1.0.0
* @package GoodBids
*/

namespace GoodBids\Plugins\WooCommerce\Emails;

defined( 'ABSPATH' ) || exit;

/**
* Auction Ending Soon Email
*
* @since 1.0.0
* @extends Email
*/
class AuctionIsEndingSoon extends Email {

/**
* Set the unique Email ID
*
* @since 1.0.0
* @var string
*/
public $id = 'goodbids_auction_ending_soon';

/**
* Set email defaults
*
* @since 1.0.0
*
* @return void
*/
public function __construct() {
parent::__construct();

$this->title = __( 'Auction is Ending Soon', 'goodbids' );
$this->description = __( 'Notification email sent to all Bidders & Watchers when an Auction is near closing.', 'goodbids' );
$this->template_html = 'emails/auction-is-ending-soon.php';
$this->template_plain = 'emails/plain/auction-is-ending-soon.php';
$this->bidder_email = true;
$this->watcher_email = true;

$this->cron_check_for_auctions_ending_soon();
}

/**
* Trigger this email when an Auction is within 4 hours of closing
*
* @since 1.0.0
*
* @return void
*/
private function cron_check_for_auctions_ending_soon(): void {
add_action(
Auctions::CRON_AUCTION_ENDING_SOON_CHECK_HOOK,
function (): void {
$auctions = goodbids()->auctions->get_auctions_ending_soon_emails();
if (! $auctions){
return;
}
foreach ($auctions as $auction_id){
$auction = goodbids()->auctions->get( $auction_id );
$this->send_to_bidders( $auction );
$this->send_to_watchers( $auction );
}
}
)
}

/**
* Get email subject.
*
* @since 1.0.0
* @return string
*/
public function get_default_subject(): string {
return sprintf(
// translators: %1$s: Site Name, %2$s: Auction Title
__( '[%1$s] %2$s auction is ending soon', 'goodbids' ),
'{site_title}',
'{auction.title}'
);
}

/**
* Get email heading.
*
* @since 1.0.0
* @return string
*/
public function get_default_heading(): string {
return __( 'Don\'t miss out!', 'goodbids' );
}

/**
* Get button text
*
* @since 1.0.0
* @return string
*/
public function get_default_button_text(): string {
return __( 'Bid Now', 'goodbids' );
}

/**
* Set Button URL
*
* @since 1.0.0
*
* @return string
*/
public function get_button_url(): string {
return '{auction.url}';
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Auction Ending Soon email
*
* @since 1.0.0
* @version 1.0.0
* @package GoodBids
*
* @var string $email_heading
*
* @var AuctionIsEndingSoon $instance
*/

use GoodBids\Plugins\WooCommerce\Emails\AuctionIsEndingSoon;

defined( 'ABSPATH' ) || exit;

/*
* @hooked WC_Emails::email_header() Output the email header
*/
do_action( 'woocommerce_email_header', $email_heading );
?>

<p>
<?php
printf(
'%s <a href="%s">%s</a> %s',

// Before
sprintf(
/* translators: %1$s: Auction Title */
esc_html__( 'The %1$s auction is ending soon.', 'goodbids' ),
'{auction.title}'
),

// URL
'{auction.url}',

// Link Text
esc_html__( 'Visit the auction', 'goodbids' ),

// After.
sprintf(
/* translators: %1$s: Site Title, Reward Product Title */
esc_html__( 'to support %1$s\'s mission and place a bid for your chance to win the %2$s!', 'goodbids' ),
'{site_title}',
'{reward.title}'
)
);
?>
</p>

<p>
<?php
printf(
'%s %s %s <strong>%s</strong> %s',
/* translators: %1$s: Auction total raised, %2$s: current high bid */
esc_html__( 'We\ve already raised', 'goodbids' ),
'{auction.total_raised}',
esc_html__( 'and the', 'goodbids' ),
esc_html__( 'current high bid is ', 'goodbids' ),
'{auction.high_bid}'
);
?>
</p>

<p>
<?php
printf(
/* translators: %1$s: Site Name */
esc_html__( 'Every GOODBID on this auction is a donation to %1$s.', 'goodbids' ),
'{site_title}'
);
?>
</p>

<?php

/* * @hooked WC_Emails::email_footer() Output the email footer */
do_action( 'woocommerce_email_footer' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Auction Ending Soon email (plain text)
*
* @since 1.0.0
* @version 1.0.0
* @package GoodBids
*
* @var AuctionIsEndingSoon $instance
*/

use GoodBids\Plugins\WooCommerce\Emails\AuctionIsEndingSoon;

defined( 'ABSPATH' ) || exit;

/** @var WC_Order $bid_order */
$bid_order = $instance->object;

$instance->plain_text_header();

printf(
/* translators: %1$s: Auction Title, %2$s: total raised, %3$s: Site title, %4$s: High bid, %5$s: auction url */
esc_html__( 'The auction you\'re watching, %1$s, is ending soon. It\'s already raised %2$s for %3$s, with a current high bid of %4$s. Let\'s see how much more can we raise: %5$s', 'goodbids' ),
'{auction.title}',
'{auction.total_raised}',
'{site_title}',
'{auction.high_bid}',
'{auction.url}'
);

echo "\n\n----------------------------------------\n\n";

esc_html_e( 'Auction', 'goodbids' ) . ': ' . '{auction.title}' . "\n";
esc_html_e( 'Total Raised', 'goodbids' ) . ': ' . '{auction.total_raised}' . "\n";
esc_html_e( 'High Bid', 'goodbids' ) . ': ' . '{auction.high_bid}' . "\n";

echo "\n\n----------------------------------------\n\n";

$instance->plain_text_footer();

0 comments on commit 237427d

Please sign in to comment.