'product',
'post_status' => array( 'publish', 'draft', 'private', 'future' ),
'posts_per_page' => 500,
'orderby' => 'title',
'order' => 'ASC',
)
);
}
/* ------------------------------------------------------------------
* Render
* ------------------------------------------------------------------ */
public static function render() {
// The finish screen shows right after a drop was saved (its result is kept for this user).
if ( get_transient( 'hyperdrop_wizard_done_' . get_current_user_id() ) ) {
self::render_done();
return;
}
$cats = get_terms(
array(
'taxonomy' => 'product_cat',
'hide_empty' => false,
)
);
$cats = is_wp_error( $cats ) ? array() : $cats;
$products = self::products();
$steps = array(
__( 'What to lock', 'hyperdrop' ),
__( 'Products', 'hyperdrop' ),
__( 'Access', 'hyperdrop' ),
__( 'Limits', 'hyperdrop' ),
__( 'Codes', 'hyperdrop' ),
__( 'Review', 'hyperdrop' ),
);
$steps = (array) apply_filters( 'hyperdrop_wizard_steps', $steps );
?>
value (data-show etc.).
* @param int $value Starting value; 0 = off.
*/
protected static function limit_row( $key, $icon, $title, $desc, $suggest, $attrs = array(), $value = 0 ) {
$attrs = is_array( $attrs ) ? $attrs : array();
$id = 'hdw_lim_' . $key;
?>
$s['visibility'],
'hide_price' => $s['hide_price'],
'signup' => $s['signup'],
'message' => $s['message'],
'start' => HyperDrop_Schedule::input_value( $sch['start'] ),
'release' => HyperDrop_Schedule::input_value( $sch['release'] ),
'end' => HyperDrop_Schedule::input_value( $sch['end'] ),
'notify' => $sch['notify'],
'limits' => HyperDrop_Limits::get( $scope ),
'testing' => HyperDrop_Drop::is_testing_scope( $scope ),
);
return (array) apply_filters( 'hyperdrop_wizard_settings', $out, $scope );
}
/**
* Translated strings for assets/wizard.js.
*/
public static function script_data() {
$i18n = array(
'pickProducts' => __( 'Pick at least one product.', 'hyperdrop' ),
'pickProduct' => __( 'Pick the product for this drop.', 'hyperdrop' ),
'catName' => __( 'Give your collection a name.', 'hyperdrop' ),
'codeBad' => __( 'The code needs at least 4 letters or numbers.', 'hyperdrop' ),
/* translators: 1: how many people can use the code, 2: number of emails */
'usesLow' => __( 'The code can be used by %1$d people, but you are emailing %2$d. Raise the number, or set it to 0 for no limit.', 'hyperdrop' ),
'alphabet' => HyperDrop_Codes::ALPHABET,
/* translators: %d: number of email addresses */
'vipCount' => __( '%d emails.', 'hyperdrop' ),
'vipOne' => __( '1 email.', 'hyperdrop' ),
'limitNum' => __( 'Enter a number for each limit that is on, or turn it off.', 'hyperdrop' ),
'noMedia' => __( 'The Media Library could not load. Reload the page and try again.', 'hyperdrop' ),
'mediaTitle' => __( 'Choose a category image', 'hyperdrop' ),
'mediaButton' => __( 'Use this image', 'hyperdrop' ),
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'selected' => __( '%d selected', 'hyperdrop' ),
'code' => __( 'Enter the shared code.', 'hyperdrop' ),
'emails' => __( 'Add at least one email, or untick the VIP list.', 'hyperdrop' ),
'pickDate' => __( 'Pick a date and time, or untick that option.', 'hyperdrop' ),
'order1' => __( 'Opening to everyone must be after the start.', 'hyperdrop' ),
'order2' => __( 'The end must be after the start.', 'hyperdrop' ),
'order3' => __( 'The end must be after opening to everyone.', 'hyperdrop' ),
'review' => array(
'what' => __( 'Exclusive', 'hyperdrop' ),
'products' => __( 'Products', 'hyperdrop' ),
'image' => __( 'Category image', 'hyperdrop' ),
/* translators: %d: number of people */
'mailShared' => __( 'emailed to %d people', 'hyperdrop' ),
/* translators: %d: number of people */
'mailOwn' => __( 'personal codes emailed to %d people', 'hyperdrop' ),
'auto' => __( 'sign-ups get their own code by email', 'hyperdrop' ),
'imgNew' => __( 'New image', 'hyperdrop' ),
'imgNone' => __( 'Removed', 'hyperdrop' ),
'access' => __( 'Access', 'hyperdrop' ),
'visible' => __( 'Before unlocking', 'hyperdrop' ),
'vHidden' => __( 'Hidden β secret until unlocked', 'hyperdrop' ),
'vPreview' => __( 'Preview β visible, buying needs a code', 'hyperdrop' ),
'vNoPrice' => __( ', prices hidden', 'hyperdrop' ),
'release' => __( 'Opens to everyone', 'hyperdrop' ),
'start' => __( 'Drop starts', 'hyperdrop' ),
'end' => __( 'Drop ends', 'hyperdrop' ),
'now' => __( 'Right away', 'hyperdrop' ),
'notify' => __( '"It\'s live!" email', 'hyperdrop' ),
'limits' => __( 'Limits', 'hyperdrop' ),
'codes' => __( 'Codes', 'hyperdrop' ),
'none' => __( 'None', 'hyperdrop' ),
'never' => __( 'Not scheduled (stays exclusive)', 'hyperdrop' ),
'newCol' => __( 'New collection: ', 'hyperdrop' ),
'cat' => __( 'Category: ', 'hyperdrop' ),
'prod' => __( 'Product: ', 'hyperdrop' ),
/* translators: 1: first value, 2: second value (filled in by the wizard). */
'shared' => __( 'Shared code %1$s (%2$s people)', 'hyperdrop' ),
'signup' => HyperDrop_Drop::signup_modes(),
'lim' => array(
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'max_qty' => __( '%s of each item per order', 'hyperdrop' ),
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'max_items' => __( '%s exclusive items per order', 'hyperdrop' ),
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'max_orders' => __( '%s orders per customer', 'hyperdrop' ),
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'max_signups' => __( '%s sign-up spots', 'hyperdrop' ),
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'activations' => __( '%s devices per code', 'hyperdrop' ),
/* translators: %s/%d: value filled in by the wizard (name, number or date). */
'code_uses' => __( '%s orders per code', 'hyperdrop' ),
),
// Wording when the number is 1.
'lim1' => array(
'max_items' => __( '1 exclusive item per order', 'hyperdrop' ),
'max_orders' => __( '1 order per customer', 'hyperdrop' ),
'max_signups' => __( '1 sign-up spot', 'hyperdrop' ),
'activations' => __( '1 device per code', 'hyperdrop' ),
'code_uses' => __( '1 order per code', 'hyperdrop' ),
),
),
);
return (array) apply_filters( 'hyperdrop_wizard_script_data', $i18n );
}
/* ------------------------------------------------------------------
* Save
* ------------------------------------------------------------------ */
public static function handle() {
if ( ! current_user_can( 'manage_woocommerce' ) ) {
wp_die( esc_html__( 'Not allowed.', 'hyperdrop' ) );
}
check_admin_referer( 'hyperdrop_wizard' );
$back = HyperDrop_Admin::url( array( 'tab' => 'wizard' ) );
$kind = isset( $_POST['kind'] ) ? sanitize_key( wp_unslash( $_POST['kind'] ) ) : '';
$signup = isset( $_POST['signup'] ) ? sanitize_key( wp_unslash( $_POST['signup'] ) ) : 'auto';
$signup = array_key_exists( $signup, HyperDrop_Drop::signup_modes() ) ? $signup : 'auto';
$message = isset( $_POST['message'] ) ? sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : '';
$sch = HyperDrop_Schedule::from_input(
isset( $_POST['sch_start'] ) ? sanitize_text_field( wp_unslash( $_POST['sch_start'] ) ) : '',
isset( $_POST['sch_release'] ) ? sanitize_text_field( wp_unslash( $_POST['sch_release'] ) ) : '',
isset( $_POST['sch_end'] ) ? sanitize_text_field( wp_unslash( $_POST['sch_end'] ) ) : '',
! empty( $_POST['sch_notify'] )
);
if ( ! is_array( $sch ) ) {
HyperDrop_Admin::flash( $sch, 'error' );
wp_safe_redirect( $back );
exit;
}
// Whole numbers only, for the limit names HyperDrop knows.
$limits = isset( $_POST['limits'] ) && is_array( $_POST['limits'] ) ? map_deep( wp_unslash( $_POST['limits'] ), 'absint' ) : array();
$limits = array_intersect_key( $limits, array_merge( HyperDrop_Limits::defaults( 't' ), HyperDrop_Limits::defaults( 'p' ) ) );
$summary = array();
if ( 'product' === $kind ) {
$pid = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0;
$product = $pid ? wc_get_product( $pid ) : null;
if ( ! $product ) {
HyperDrop_Admin::flash( __( 'Please pick a product.', 'hyperdrop' ), 'error' );
wp_safe_redirect( $back );
exit;
}
$product->update_meta_data( '_hyperdrop_enabled', 'yes' );
$product->update_meta_data( '_hyperdrop_testing', empty( $_POST['testing'] ) ? 'no' : 'yes' );
$product->update_meta_data( '_hyperdrop_signup', $signup );
$product->update_meta_data( '_hyperdrop_message', $message );
$product->save();
$scope = 'p:' . $pid;
$name = $product->get_name();
$view = get_permalink( $pid );
$edit = get_edit_post_link( $pid, 'raw' );
} else {
if ( 'new_cat' === $kind ) {
$cat_name = isset( $_POST['cat_name'] ) ? sanitize_text_field( wp_unslash( $_POST['cat_name'] ) ) : '';
if ( '' === $cat_name ) {
HyperDrop_Admin::flash( __( 'Please name the collection.', 'hyperdrop' ), 'error' );
wp_safe_redirect( $back );
exit;
}
$existing = get_term_by( 'name', $cat_name, 'product_cat' );
if ( $existing ) {
$term_id = (int) $existing->term_id;
} else {
$res = wp_insert_term( $cat_name, 'product_cat' );
if ( is_wp_error( $res ) ) {
HyperDrop_Admin::flash( $res->get_error_message(), 'error' );
wp_safe_redirect( $back );
exit;
}
$term_id = (int) $res['term_id'];
}
} else {
$term_id = isset( $_POST['cat_id'] ) ? absint( $_POST['cat_id'] ) : 0;
}
$term = get_term( $term_id, 'product_cat' );
if ( ! $term || is_wp_error( $term ) ) {
HyperDrop_Admin::flash( __( 'Category not found.', 'hyperdrop' ), 'error' );
wp_safe_redirect( $back );
exit;
}
update_term_meta( $term_id, '_hyperdrop_protected', 'yes' );
update_term_meta( $term_id, '_hyperdrop_testing', empty( $_POST['testing'] ) ? 'no' : 'yes' );
update_term_meta( $term_id, '_hyperdrop_visibility', isset( $_POST['visibility'] ) && 'preview' === sanitize_key( wp_unslash( $_POST['visibility'] ) ) ? 'preview' : 'hidden' );
update_term_meta( $term_id, '_hyperdrop_hide_price', ! empty( $_POST['hide_price'] ) && isset( $_POST['visibility'] ) && 'preview' === sanitize_key( wp_unslash( $_POST['visibility'] ) ) ? 'yes' : 'no' );
update_term_meta( $term_id, '_hyperdrop_signup', $signup );
update_term_meta( $term_id, '_hyperdrop_message', $message );
$img_note = self::save_image( $term_id, ! empty( $_POST['cat_image_set'] ), isset( $_POST['cat_image'] ) ? absint( $_POST['cat_image'] ) : 0 );
$pids = isset( $_POST['products'] ) && is_array( $_POST['products'] ) ? array_filter( map_deep( wp_unslash( $_POST['products'] ), 'absint' ) ) : array();
$added = 0;
foreach ( array_filter( $pids ) as $pid ) {
if ( 'product' === get_post_type( $pid ) ) {
wp_set_object_terms( $pid, $term_id, 'product_cat', true );
++$added;
}
}
if ( function_exists( 'wc_delete_product_transients' ) ) {
wc_delete_product_transients();
}
HyperDrop_Drop::flush_cache();
$scope = 't:' . $term_id;
$name = $term->name;
$view = get_term_link( $term );
$edit = get_edit_term_link( $term_id, 'product_cat' );
/* translators: %d: number of products */
$summary[] = sprintf( _n( '%d product in the collection', '%d products in the collection', $added, 'hyperdrop' ), $added );
if ( $img_note ) {
$summary[] = $img_note;
}
}
HyperDrop_Schedule::set( $scope, $sch[0], $sch[1], $sch[2], $sch[3] );
if ( $sch[0] && $sch[0] > time() ) {
/* translators: %s: date */
$summary[] = sprintf( __( 'Drop starts %s', 'hyperdrop' ), HyperDrop_Schedule::fmt( $sch[0] ) ) . ( $sch[3] ? ' β ' . __( '"It\'s live!" email scheduled', 'hyperdrop' ) : '' );
}
if ( $sch[2] ) {
/* translators: %s: date */
$summary[] = sprintf( __( 'Drop ends %s', 'hyperdrop' ), HyperDrop_Schedule::fmt( $sch[2] ) );
}
HyperDrop_Limits::set( $scope, $limits );
/**
* Add-ons save their wizard fields here, before any codes are created.
* Messages they return are added to the summary.
*/
$summary = (array) apply_filters( 'hyperdrop_wizard_saved', $summary, $scope );
list( $s_type, $s_id ) = HyperDrop_Limits::split( $scope );
$base = array(
'product_id' => 'p' === $s_type ? $s_id : 0,
'term_id' => 't' === $s_type ? $s_id : 0,
'source' => 'manual',
);
// Every drop gets one shared code. If the code typed is taken, make a random one instead.
$shared = '';
$shared_row = null;
$want = isset( $_POST['shared_code'] ) ? HyperDrop_Codes::normalize( sanitize_text_field( wp_unslash( $_POST['shared_code'] ) ) ) : '';
$uses = isset( $_POST['shared_uses'] ) ? absint( $_POST['shared_uses'] ) : 0;
$r = HyperDrop_Codes::create( array_merge( $base, array( 'code' => strlen( $want ) >= 4 ? $want : '', 'max_uses' => $uses ) ) );
if ( is_wp_error( $r ) && 'hyperdrop_exists' === $r->get_error_code() ) {
$r = HyperDrop_Codes::create( array_merge( $base, array( 'max_uses' => $uses ) ) );
if ( ! is_wp_error( $r ) ) {
/* translators: 1: code that was taken, 2: new code */
$summary[] = '!' . sprintf( __( '%1$s was already taken, so your drop code is %2$s', 'hyperdrop' ), $want, $r->code );
}
}
if ( is_wp_error( $r ) ) {
/* translators: %s: error */
$summary[] = '!' . sprintf( __( 'Shared code not created: %s', 'hyperdrop' ), $r->get_error_message() );
} else {
$shared_row = $r;
$shared = $r->code;
/* translators: %s: code */
$summary[] = sprintf( __( 'Shared code %s created', 'hyperdrop' ), $r->code );
}
if ( ! empty( $_POST['vip_on'] ) && ! empty( $_POST['vip_emails'] ) ) {
$raw = sanitize_textarea_field( wp_unslash( $_POST['vip_emails'] ) );
$emails = array_values( array_filter( array_unique( array_map( 'sanitize_email', preg_split( '/[\s,;]+/', $raw ) ) ), 'is_email' ) );
$personal = ! ( isset( $_POST['vip_mode'] ) && 'shared' === sanitize_key( wp_unslash( $_POST['vip_mode'] ) ) && $shared_row );
$sent = 0;
$per_uses = 't' === $s_type ? HyperDrop_Limits::get( $scope )['activations'] : HyperDrop_Limits::get( $scope )['code_uses'];
foreach ( $emails as $email ) {
if ( $personal ) {
$row = HyperDrop_Codes::create( array_merge( $base, array( 'email' => $email, 'max_uses' => $per_uses ) ) );
} else {
// Same shared code, addressed to this person.
$row = clone $shared_row;
$row->email = $email;
}
if ( ! is_wp_error( $row ) && HyperDrop_Mailer::send_code( $row ) ) {
++$sent;
}
}
$summary[] = $personal
/* translators: 1: sent, 2: total */
? sprintf( __( 'Emailed personal codes to %1$d of %2$d people', 'hyperdrop' ), $sent, count( $emails ) )
/* translators: 1: code, 2: sent, 3: total */
: sprintf( __( 'Emailed %1$s to %2$d of %3$d people', 'hyperdrop' ), $shared, $sent, count( $emails ) );
if ( ! $personal && $uses && count( $emails ) > $uses ) {
/* translators: 1: number of people who can use the code, 2: number emailed */
$summary[] = '!' . sprintf( __( 'Heads up: the code works for %1$d people but you emailed %2$d.', 'hyperdrop' ), $uses, count( $emails ) );
}
}
if ( ! empty( $_POST['testing'] ) ) {
$summary[] = __( 'π§ͺ In test mode β only admins can see it. Click "Go live" on the dashboard when ready.', 'hyperdrop' );
}
set_transient(
'hyperdrop_wizard_done_' . get_current_user_id(),
array(
'name' => $name,
'scope' => $scope,
'view' => is_wp_error( $view ) ? home_url( '/' ) : $view,
'edit' => $edit,
'slug' => 't' === $s_type ? get_term( $s_id, 'product_cat' )->slug : '',
'shared' => $shared,
'summary' => $summary,
),
HOUR_IN_SECONDS
);
wp_safe_redirect( HyperDrop_Admin::url( array( 'tab' => 'wizard' ) ) );
exit;
}
protected static function render_done() {
$d = get_transient( 'hyperdrop_wizard_done_' . get_current_user_id() );
// Show the finish screen once; opening the wizard again starts a new drop.
delete_transient( 'hyperdrop_wizard_done_' . get_current_user_id() );
if ( ! $d ) {
echo '';
return;
}
$shortcode = $d['slug'] ? '[hyperdrop category="' . $d['slug'] . '"]' : '';
?>