' . esc_html__( 'HyperDrop schedule not saved:', 'hyperdrop' ) . ' ' . esc_html( $msg ) . '
';
}
}
/**
* Build a schedule from form values the caller has already nonce-checked and sanitized.
*
* @param string $start Start time as typed (Y-m-d\TH:i, site time) or ''.
* @param string $release "Open to everyone" time or ''.
* @param string $end End time or ''.
* @param bool $notify Email everyone when it starts.
* @return array|string [start, release, end, notify] timestamps, or an error message.
*/
public static function from_input( $start, $release, $end, $notify ) {
$start = self::parse( (string) $start );
$release = self::parse( (string) $release );
$end = self::parse( (string) $end );
$err = self::validate( $start, $release, $end );
return $err ? $err : array( $start, $release, $end, (bool) $notify );
}
/**
* Schedule inputs for admin forms. $layout: 'table' (category edit), 'div' (category add), 'wc' (product tab).
*/
public static function fields_html( $prefix, $scope, $layout ) {
$s = $scope ? self::get( $scope ) : array( 'start' => 0, 'release' => 0, 'end' => 0, 'notify' => true );
$rows = array(
array( 'start', __( 'Drop starts', 'hyperdrop' ), __( 'Optional. Before this, the page shows "Coming soon" with a countdown. People can sign up and enter codes early. Empty = live now.', 'hyperdrop' ) ),
array( 'release', __( 'Open to everyone', 'hyperdrop' ), __( 'Optional. From this time anyone can see and buy — no code needed.', 'hyperdrop' ) ),
array( 'end', __( 'Drop ends', 'hyperdrop' ), __( 'Optional. After this nobody can buy, and the page says the drop has ended.', 'hyperdrop' ) ),
);
$out = '';
foreach ( $rows as $r ) {
$input = '';
if ( 'table' === $layout ) {
$out .= ' | ' . $input . ' ' . esc_html( $r[2] ) . ' |
';
} elseif ( 'wc' === $layout ) {
$out .= '' . $input . wc_help_tip( $r[2] ) . '
';
} else {
$out .= '';
}
}
/* translators: 1: timezone, 2: current site time */
$hint = '' . esc_html( sprintf( __( 'Times use the site clock: %1$s — now %2$s.', 'hyperdrop' ), wp_timezone_string(), wp_date( 'M j, g:ia' ) ) ) . '
';
$out .= 'table' === $layout ? ' | ' . $hint . ' |
' : $hint;
$chk = '';
if ( 'table' === $layout ) {
$out .= ' | ' . $chk . ' |
';
} elseif ( 'wc' === $layout ) {
$out .= '' . $chk . '
';
} else {
$out .= '' . $chk . '
';
}
return $out;
}
protected static function meta( $scope, $key ) {
list( $type, $id ) = HyperDrop_Limits::split( $scope );
return 't' === $type ? get_term_meta( $id, '_hyperdrop_' . $key, true ) : get_post_meta( $id, '_hyperdrop_' . $key, true );
}
protected static function update( $scope, $key, $value ) {
list( $type, $id ) = HyperDrop_Limits::split( $scope );
if ( 't' === $type ) {
update_term_meta( $id, '_hyperdrop_' . $key, $value );
} else {
update_post_meta( $id, '_hyperdrop_' . $key, $value );
}
}
public static function get( $scope ) {
return array(
'start' => (int) self::meta( $scope, 'start_at' ),
'release' => (int) self::meta( $scope, 'release_at' ),
'end' => (int) self::meta( $scope, 'end_at' ),
'notify' => 'yes' === self::meta( $scope, 'notify_start' ),
);
}
public static function phase( $scope, $now = null ) {
$s = self::get( $scope );
$now = null === $now ? time() : $now;
if ( $s['end'] && $now >= $s['end'] ) {
return 'ended';
}
if ( $s['start'] && $now < $s['start'] ) {
return 'upcoming';
}
if ( $s['release'] && $now >= $s['release'] ) {
return 'public';
}
return 'live';
}
/**
* Save a schedule. Timestamps (0 = not set).
*/
public static function set( $scope, $start, $release, $end, $notify = null ) {
self::update( $scope, 'start_at', (int) $start );
self::update( $scope, 'release_at', (int) $release );
self::update( $scope, 'end_at', (int) $end );
if ( null !== $notify ) {
self::update( $scope, 'notify_start', $notify ? 'yes' : 'no' );
}
self::reschedule( $scope );
}
/**
* Convert a datetime-local value (site timezone) to a timestamp.
*/
public static function parse( $value ) {
$value = trim( (string) $value );
if ( '' === $value ) {
return 0;
}
$dt = date_create_immutable( $value, wp_timezone() );
return $dt ? $dt->getTimestamp() : 0;
}
public static function input_value( $ts ) {
return $ts ? wp_date( 'Y-m-d\TH:i', $ts ) : '';
}
public static function fmt( $ts ) {
return HyperDrop_I18n::date( $ts );
}
/**
* Check order (start < release < end). Returns an error message or ''.
*/
public static function validate( $start, $release, $end ) {
if ( $start && $release && $release <= $start ) {
return __( 'The public release must be after the start time.', 'hyperdrop' );
}
if ( $end && $start && $end <= $start ) {
return __( 'The end time must be after the start time.', 'hyperdrop' );
}
if ( $end && $release && $end <= $release ) {
return __( 'The end time must be after the public release.', 'hyperdrop' );
}
return '';
}
public static function phase_label( $phase ) {
$l = array(
'upcoming' => __( 'Coming soon', 'hyperdrop' ),
'live' => __( 'Live — code holders', 'hyperdrop' ),
'public' => __( 'Open to everyone', 'hyperdrop' ),
'ended' => __( 'Ended', 'hyperdrop' ),
);
return isset( $l[ $phase ] ) ? $l[ $phase ] : $phase;
}
/**
* One-line description for admin lists: "Starts Dec 1, 9:00am · ends Dec 3, 9:00am".
*/
public static function summary( $scope ) {
$s = self::get( $scope );
$bits = array();
$short = 'M j, g:ia';
if ( $s['start'] ) {
/* translators: %s: date */
$bits[] = sprintf( __( 'starts %s', 'hyperdrop' ), wp_date( $short, $s['start'] ) );
}
if ( $s['release'] ) {
/* translators: %s: date */
$bits[] = sprintf( __( 'public %s', 'hyperdrop' ), wp_date( $short, $s['release'] ) );
}
if ( $s['end'] ) {
/* translators: %s: date */
$bits[] = sprintf( __( 'ends %s', 'hyperdrop' ), wp_date( $short, $s['end'] ) );
}
return ucfirst( implode( ' · ', $bits ) );
}
/**
* Countdown HTML (JS fills in the live timer and reloads the page at zero).
*/
public static function countdown( $ts, $label ) {
return '' . esc_html( $label ) . '
';
}
/* ------------------------------------------------------------------
* "It's live" emails
* ------------------------------------------------------------------ */
public static function reschedule( $scope ) {
wp_clear_scheduled_hook( self::CRON_HOOK, array( $scope ) );
$s = self::get( $scope );
if ( $s['notify'] && $s['start'] && $s['start'] > time() ) {
wp_schedule_single_event( $s['start'], self::CRON_HOOK, array( $scope ) );
}
}
/**
* Email every active code holder (with an email) for this scope.
*/
public static function send_live_emails( $scope ) {
$s = self::get( $scope );
if ( ! $s['notify'] || ! $s['start'] || HyperDrop_Drop::is_testing_scope( $scope ) ) {
return 0; // Test mode never emails real customers.
}
// Only once per start time.
$flag = 'hyperdrop_live_sent_' . md5( $scope . '|' . $s['start'] );
if ( get_option( $flag ) ) {
return 0;
}
update_option( $flag, time(), false );
list( $type, $id ) = HyperDrop_Limits::split( $scope );
$res = HyperDrop_Codes::query(
array(
'product_id' => 'p' === $type ? $id : 0,
'term_id' => 't' === $type ? $id : 0,
'status' => 'active',
'per_page' => 0,
)
);
$sent = 0;
$seen = array();
foreach ( $res['rows'] as $row ) {
$email = strtolower( (string) $row->email );
if ( ! $email || isset( $seen[ $email ] ) ) {
continue;
}
$seen[ $email ] = true;
if ( HyperDrop_Mailer::send_live( $row ) ) {
++$sent;
}
}
do_action( 'hyperdrop_live_emails_sent', $scope, $sent );
return $sent;
}
}