'dashboard' ) ) ) . '">' . esc_html__( 'Dashboard', 'hyperdrop' ) . '' ); return $links; } /** * Styles and scripts for HyperDrop's own admin screens only. */ public static function assets() { if ( ! in_array( self::current_page(), self::slugs(), true ) ) { return; } wp_enqueue_style( 'hyperdrop-admin', HYPERDROP_URL . 'assets/admin.css', array(), hyperdrop_asset_ver( 'assets/admin.css' ) ); wp_enqueue_script( 'hyperdrop-admin', HYPERDROP_URL . 'assets/admin.js', array(), hyperdrop_asset_ver( 'assets/admin.js' ), true ); if ( 'wizard' === self::current_tab() ) { $deps = array(); if ( current_user_can( 'upload_files' ) ) { // Media Library for the category image picker. wp_enqueue_media(); $deps[] = 'media-editor'; } wp_enqueue_script( 'hyperdrop-wizard', HYPERDROP_URL . 'assets/wizard.js', $deps, hyperdrop_asset_ver( 'assets/wizard.js' ), true ); wp_add_inline_script( 'hyperdrop-wizard', 'window.hyperdropWizard = ' . wp_json_encode( HyperDrop_Wizard::script_data() ) . ';', 'before' ); } } /** * "Go live" / "Back to test mode" button for a drop. */ public static function testing_button( $scope, $to_testing ) { ?>
>
'dashboard' ) ); } self::done( '', array( 'tab' => 'dashboard' ) ); } /** * Admin bar: switch between admin view (see everything) and shopper view (locks apply). */ public static function do_shopper_view() { if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'Not allowed.', 'hyperdrop' ) ); } check_admin_referer( 'hyperdrop_shopper_view' ); $on = isset( $_GET['on'] ) && '1' === sanitize_key( wp_unslash( $_GET['on'] ) ); update_user_meta( get_current_user_id(), 'hyperdrop_shopper_view', $on ? 'yes' : 'no' ); $back = wp_get_referer(); wp_safe_redirect( $back ? $back : home_url( '/' ) ); exit; } /** * @param WP_Admin_Bar $bar */ public static function admin_bar( $bar ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { return; } $shopper = HyperDrop_Drop::shopper_view(); $testing = count( HyperDrop_Drop::testing_terms() ) + count( HyperDrop_Drop::testing_products() ); $bar->add_node( array( 'id' => 'hyperdrop', 'title' => $shopper ? esc_html__( 'πŸ‘ HyperDrop: Shopper view', 'hyperdrop' ) : ( $testing ? esc_html__( 'πŸ§ͺ HyperDrop', 'hyperdrop' ) : esc_html__( 'πŸ”’ HyperDrop', 'hyperdrop' ) ), 'href' => self::url( array( 'tab' => 'dashboard' ) ), ) ); $bar->add_node( array( 'parent' => 'hyperdrop', 'id' => 'hyperdrop-view', 'title' => $shopper ? esc_html__( 'Back to admin view (see everything)', 'hyperdrop' ) : esc_html__( 'View the store as a shopper (locks apply to me)', 'hyperdrop' ), 'href' => wp_nonce_url( admin_url( 'admin-post.php?action=hyperdrop_shopper_view&on=' . ( $shopper ? '0' : '1' ) ), 'hyperdrop_shopper_view' ), ) ); if ( $testing ) { $bar->add_node( array( 'parent' => 'hyperdrop', 'id' => 'hyperdrop-testing', /* translators: %d: number of drops */ 'title' => esc_html( sprintf( _n( '%d drop in test mode (hidden from the public)', '%d drops in test mode (hidden from the public)', $testing, 'hyperdrop' ), $testing ) ), 'href' => self::url( array( 'tab' => 'dashboard' ) ), ) ); } } /** Admin page slug for each screen. */ public static function slugs() { $slugs = array( 'dashboard' => 'hyperdrop', 'wizard' => 'hyperdrop-new', 'codes' => 'hyperdrop-codes', 'generate' => 'hyperdrop-create', 'limits' => 'hyperdrop-limits', 'design' => 'hyperdrop-design', 'settings' => 'hyperdrop-settings', ); if ( ! defined( 'HYPERDROP_PRO_VERSION' ) ) { $slugs['pro'] = 'hyperdrop-pro'; } /** Add-ons can add screens: key => page slug (render with the hyperdrop_admin_tab_{key} action). */ return (array) apply_filters( 'hyperdrop_admin_slugs', $slugs ); } /** Tab labels. */ public static function tab_labels() { $labels = array( 'dashboard' => __( 'Dashboard', 'hyperdrop' ), 'wizard' => __( '+ New exclusive drop', 'hyperdrop' ), 'codes' => __( 'Codes', 'hyperdrop' ), 'generate' => __( 'Create codes', 'hyperdrop' ), 'limits' => __( 'Limits', 'hyperdrop' ), 'design' => __( 'Design', 'hyperdrop' ), 'settings' => __( 'Settings', 'hyperdrop' ), 'pro' => __( 'Pro', 'hyperdrop' ), ); return (array) apply_filters( 'hyperdrop_admin_tab_labels', $labels ); } public static function menu() { $labels = self::tab_labels(); /** Add-ons can show a count next to the menu (e.g. sign-ups waiting for review). */ $count = (int) apply_filters( 'hyperdrop_menu_count', 0 ); $badge = $count ? ' ' . $count . '' : ''; add_menu_page( __( 'HyperDrop', 'hyperdrop' ), __( 'HyperDrop', 'hyperdrop' ) . $badge, self::CAP, 'hyperdrop', array( __CLASS__, 'page' ), 'dashicons-lock', 56 // Right below WooCommerce / Products. ); foreach ( self::slugs() as $tab => $slug ) { $label = isset( $labels[ $tab ] ) ? $labels[ $tab ] : $tab; add_submenu_page( 'hyperdrop', $label . ' – ' . __( 'HyperDrop', 'hyperdrop' ), $label, self::CAP, $slug, array( __CLASS__, 'page' ) ); } } protected static function drop_products() { return get_posts( array( 'post_type' => 'product', 'post_status' => array( 'publish', 'draft', 'private', 'future' ), 'posts_per_page' => 200, 'meta_key' => '_hyperdrop_enabled', // phpcs:ignore 'meta_value' => 'yes', // phpcs:ignore 'orderby' => 'title', 'order' => 'ASC', ) ); } protected static function protected_terms() { $terms = get_terms( array( 'taxonomy' => 'product_cat', 'hide_empty' => false, 'meta_query' => array( array( 'key' => '_hyperdrop_protected', 'value' => 'yes' ) ), // phpcs:ignore ) ); return is_wp_error( $terms ) ? array() : $terms; } /** * Scope strings: '' = everything / any, 'p:ID' = product drop, 't:ID' = protected category. * * @return int[] [ product_id, term_id ] */ public static function parse_scope( $scope ) { if ( preg_match( '/^([pt]):(\d+)$/', (string) $scope, $m ) ) { return 'p' === $m[1] ? array( (int) $m[2], 0 ) : array( 0, (int) $m[2] ); } return array( 0, 0 ); } public static function current_scope() { $scope = ''; if ( self::$verified_action ) { // Inside an admin action that guard() checked: confirm the same nonce here, then read the form. $nonce = isset( $_REQUEST['_wpnonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['_wpnonce'] ) ) : ''; if ( false !== wp_verify_nonce( $nonce, self::$verified_action ) ) { if ( isset( $_REQUEST['scope'] ) ) { $scope = sanitize_text_field( wp_unslash( $_REQUEST['scope'] ) ); } elseif ( ! empty( $_REQUEST['product_id'] ) ) { $scope = 'p:' . absint( $_REQUEST['product_id'] ); } } } else { $scope = self::nav( 'scope' ); if ( '' === $scope && absint( self::nav( 'product_id' ) ) ) { $scope = 'p:' . absint( self::nav( 'product_id' ) ); } } // Only "p:" (product) or "t:" (collection) is valid. return preg_match( '/^[pt]:\d+$/', $scope ) ? $scope : ''; } public static function scope_options( $selected, $any_label ) { echo ''; $terms = self::protected_terms(); if ( $terms ) { echo ''; foreach ( $terms as $t ) { echo ''; } echo ''; } $drops = self::drop_products(); if ( $drops ) { echo ''; foreach ( $drops as $d ) { echo ''; } echo ''; } } public static function scope_label( $r, $html = true ) { if ( (int) $r->term_id ) { $t = get_term( (int) $r->term_id, 'product_cat' ); $name = ( $t && ! is_wp_error( $t ) ) ? $t->name : '#' . $r->term_id; /* translators: %s: category name */ $label = sprintf( __( 'Category: %s', 'hyperdrop' ), $name ); $link = $html ? get_edit_term_link( (int) $r->term_id, 'product_cat' ) : ''; return $link ? '' . esc_html( $label ) . '' : ( $html ? esc_html( $label ) : $label ); } if ( (int) $r->product_id ) { $name = get_the_title( $r->product_id ); $link = $html ? get_edit_post_link( $r->product_id ) : ''; return $link ? '' . esc_html( $name ) . '' : ( $html ? esc_html( $name ) : $name ); } $any = __( 'Any drop or category', 'hyperdrop' ); return $html ? esc_html( $any ) : $any; } /** * URL of a screen. args['tab'] picks the screen (default: codes list). */ public static function url( $args = array() ) { $slugs = self::slugs(); $key = isset( $args['tab'] ) ? (string) $args['tab'] : 'codes'; unset( $args['tab'] ); if ( 'products' === $key ) { // The collection products screen lives under the dashboard page. $args = array_merge( array( 'tab' => 'products' ), $args ); $key = 'dashboard'; } $slug = isset( $slugs[ $key ] ) ? $slugs[ $key ] : $slugs['codes']; // Every HyperDrop link carries a navigation nonce, checked in nav(). $args['_hdnav'] = wp_create_nonce( 'hyperdrop_nav' ); return add_query_arg( $args, admin_url( 'admin.php?page=' . $slug ) ); } protected static function current_tab() { $tab = array_search( self::current_page(), self::slugs(), true ); if ( 'dashboard' === $tab ) { $sub = sanitize_key( self::nav( 'tab' ) ); if ( 'products' === $sub || ( $sub && isset( self::slugs()[ $sub ] ) ) ) { return $sub; } } return $tab ? $tab : 'dashboard'; } /** Nonce action that the current admin-post handler was checked against (see guard()). */ protected static $verified_action = ''; /** * Permission + nonce check for HyperDrop's admin actions (admin-post handlers). * * @param string $nonce_action Nonce action of the form or link. */ public static function require_cap() { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'Not allowed.', 'hyperdrop' ) ); } } /** * Permission + nonce check in one call (kept for add-ons; HyperDrop's own handlers check inline). * * @param string $nonce_action Nonce action of the form or link. */ public static function guard( $nonce_action ) { if ( ! current_user_can( self::CAP ) ) { wp_die( esc_html__( 'Not allowed.', 'hyperdrop' ) ); } check_admin_referer( $nonce_action ); self::$verified_action = $nonce_action; } /* ------------------------------------------------------------------ * Page * ------------------------------------------------------------------ */ public static function page() { $tab = self::current_tab(); $flash = self::take_flash(); $msg = $flash ? $flash[0] : ''; $type = $flash ? $flash[1] : 'success'; if ( 'wizard' === $tab ) { echo '
'; if ( $msg ) { echo '

' . esc_html( $msg ) . '

'; } HyperDrop_Wizard::render(); echo '
'; return; } $tabs = array_intersect_key( self::tab_labels(), self::slugs() ); unset( $tabs['wizard'] ); ?>

v


__( 'Give a code a percentage or a fixed amount off each exclusive item.', 'hyperdrop' ), __( 'Waitlist you approve', 'hyperdrop' ) => __( 'Shoppers join a list; you pick who gets in, one by one or all at once.', 'hyperdrop' ), __( 'Background images', 'hyperdrop' ) => __( 'A photo behind the unlock card, inside it or full width, with a different image per drop.', 'hyperdrop' ), __( 'CSV export', 'hyperdrop' ) => __( 'Download your codes and sign-ups.', 'hyperdrop' ), ); ?>

    $desc ) : ?>
  • β€”

get_var( $wpdb->prepare( 'SELECT COUNT(DISTINCT order_id) FROM %i WHERE scope <> %s', $reds, '' ) ); // Exclusive sales: line totals of locked items on those orders (most recent 500 orders). $order_ids = $wpdb->get_col( $wpdb->prepare( 'SELECT DISTINCT order_id FROM %i WHERE scope <> %s ORDER BY order_id DESC LIMIT %d', $reds, '', 500 ) ); $sales = 0; $units = 0; foreach ( $order_ids as $oid ) { $o = wc_get_order( $oid ); if ( ! $o || in_array( $o->get_status(), array( 'cancelled', 'failed', 'refunded' ), true ) ) { continue; } foreach ( $o->get_items() as $item ) { if ( $item->get_meta( '_hyperdrop_scopes' ) ) { $sales += (float) $item->get_total(); $units += (int) $item->get_quantity(); } } } $rows = array(); foreach ( self::protected_terms() as $t ) { $rows[ 't:' . $t->term_id ] = array( $t->name, 'preview' === HyperDrop_Drop::term_settings( $t->term_id )['visibility'] ? __( 'Category (preview)', 'hyperdrop' ) : __( 'Category (hidden)', 'hyperdrop' ), HyperDrop_Drop::term_settings( $t->term_id ), get_edit_term_link( $t->term_id, 'product_cat' ), get_term_link( $t ), HyperDrop_Drop::is_term_gated( $t->term_id ), 0, $t->term_id ); } foreach ( self::drop_products() as $d ) { $rows[ 'p:' . $d->ID ] = array( $d->post_title, __( 'Product drop', 'hyperdrop' ), HyperDrop_Drop::settings( $d->ID ), get_edit_post_link( $d->ID ), get_permalink( $d->ID ), HyperDrop_Drop::is_product_gated( $d->ID ), $d->ID, 0 ); } $recent = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE source = %s ORDER BY id DESC LIMIT %d', HyperDrop_DB::codes_table(), 'signup', 8 ) ); ?>

$r ) : list( $name, $type, $set, $edit, $view, $locked, $pid, $tid ) = $r; $limits = HyperDrop_Limits::get( $scope ); $used = HyperDrop_Limits::signups_used( $scope ); $codes = HyperDrop_Codes::query( array( 'product_id' => $pid, 'term_id' => $tid, 'per_page' => 1 ) )['total']; $orders = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(DISTINCT order_id) FROM %i WHERE scope = %s', $reds, $scope ) ); $modes = HyperDrop_Drop::signup_modes(); ?>

array( '⏳', 'pending' ), 'live' => array( 'πŸ”’', 'active' ), 'public' => array( 'πŸ”“', '' ), 'ended' => array( 'β›”', 'disabled' ), ); ?>


Β· Β· Β· Β·

email ); ?> status ) ); ?> created_at ), strtotime( current_time( 'mysql' ) ) ) ) ); ?>

β†’

  1. [hyperdrop category="exclusive"] to the home page.', 'hyperdrop' ) ); ?>
$product_id, 'term_id' => $term_id, 'status' => $status, 'search' => $search, 'page' => $paged, 'per_page' => 50, ) ); $drops = self::drop_products(); $pages = (int) ceil( $result['total'] / 50 ); ?>
' . wp_kses( $hyperdrop_toolbar, hyperdrop_allowed_html() ) . '

'; } ?> status && $r->expires_at && strtotime( $r->expires_at ) < strtotime( current_time( 'mysql' ) ); ?>
code ); ?>
source ? esc_html__( 'Requested by email', 'hyperdrop' ) : esc_html__( 'Created by admin', 'hyperdrop' ); ?>
email ? $r->email : '–' ); ?> status ) ); ?> uses . ' / ' . ( (int) $r->max_uses ? $r->max_uses : '∞' ) ); ?> expires_at ? mysql2date( 'M j, Y g:ia', $r->expires_at ) : '–' ); ?> sent_at ? mysql2date( 'M j g:ia', $r->sent_at ) : '–' ); ?> status && $r->email ) { self::action_button( $r, 'send', $r->sent_at ? __( 'Resend', 'hyperdrop' ) : __( 'Email code', 'hyperdrop' ) ); } if ( 'disabled' === $r->status ) { self::action_button( $r, 'enable', __( 'Enable', 'hyperdrop' ) ); } else { self::action_button( $r, 'disable', __( 'Disable', 'hyperdrop' ) ); } self::action_button( $r, 'delete', __( 'Delete', 'hyperdrop' ) ); ?>
1 ) : ?>

' . esc_html( sprintf( __( 'Site time (%1$s) β€” now %2$s', 'hyperdrop' ), wp_timezone_string(), wp_date( 'M j, g:ia' ) ) ) . ''; } /** * Warn when the site still runs on UTC β€” every date in the plugin uses the site clock. */ public static function timezone_notice() { $tz = wp_timezone_string(); if ( ! in_array( $tz, array( 'UTC', '+00:00', 'UTC+0' ), true ) ) { return; } echo '

' . wp_kses_post( sprintf( /* translators: 1: current site time, 2: settings link */ __( 'Your site clock is set to UTC (it thinks it is %1$s now). Code expiry dates and drop schedules use this clock, so times you enter may be hours off. Set your timezone (pick your city, e.g. Chicago).', 'hyperdrop' ), esc_html( wp_date( 'g:ia' ) ), esc_url( admin_url( 'options-general.php#timezone_string' ) ) ) ) . '

'; } protected static function status_label( $s ) { $l = array( 'active' => __( 'Active', 'hyperdrop' ), 'pending' => __( 'Waiting', 'hyperdrop' ), 'disabled' => __( 'Disabled', 'hyperdrop' ), ); return isset( $l[ $s ] ) ? $l[ $s ] : $s; } public static function action_button( $r, $act, $label ) { $confirm = 'delete' === $act ? ' data-hyperdrop-confirm="' . esc_attr__( 'Delete this code?', 'hyperdrop' ) . '"' : ''; ?>
>

–'; } $extra = ''; if ( 'max_signups' === $key ) { /* translators: %d: number of sign-ups */ $extra = '
' . esc_html( sprintf( _n( '%d signed up', '%d signed up', HyperDrop_Limits::signups_used( $scope ), 'hyperdrop' ), HyperDrop_Limits::signups_used( $scope ) ) ) . ''; } return '' . $extra . ''; } protected static function limits_table( $rows, $type, $title, $empty ) { $fields = array_filter( HyperDrop_Limits::fields(), function ( $f ) use ( $type ) { return in_array( $type, $f[1], true ); } ); echo '

' . esc_html( $title ) . '

'; if ( ! $rows ) { echo '

' . esc_html( $empty ) . '

'; return; } echo ''; foreach ( $fields as $f ) { echo ''; } echo ''; foreach ( $rows as $scope => $r ) { $l = HyperDrop_Limits::get( $scope ); echo ''; foreach ( $fields as $k => $f ) { echo wp_kses( self::limit_cell( $scope, $k, isset( $l[ $k ] ) ? $l[ $k ] : 0, $f[1] ), hyperdrop_allowed_html() ); } echo ''; } echo '
' . esc_html__( 'Name', 'hyperdrop' ) . '' . esc_html( $f[0] ) . '' . esc_html( $f[2] ) . '
' . esc_html( $r['name'] ) . '
'; echo $r['locked'] ? '' . esc_html( HyperDrop_Schedule::phase_label( HyperDrop_Schedule::phase( $scope ) ) ) . ' β€” ' . esc_html__( 'limits active', 'hyperdrop' ) . '' : '' . esc_html__( 'Open to everyone β€” limits paused', 'hyperdrop' ) . ''; echo '
'; } protected static function tab_limits() { $cats = array(); foreach ( self::protected_terms() as $t ) { $cats[ 't:' . $t->term_id ] = array( 'name' => $t->name, 'edit' => get_edit_term_link( $t->term_id, 'product_cat' ), 'locked' => HyperDrop_Drop::is_term_gated( $t->term_id ), ); } $prods = array(); foreach ( self::drop_products() as $d ) { $prods[ 'p:' . $d->ID ] = array( 'name' => $d->post_title, 'edit' => get_edit_post_link( $d->ID ), 'locked' => HyperDrop_Drop::is_product_gated( $d->ID ), ); } ?>

$g ) : ?>

$vals ) { $scope = sanitize_text_field( (string) $scope ); // Only "t:" or "p:" that really exist. if ( ! preg_match( '/^[tp]:\d+$/', $scope ) || ! is_array( $vals ) ) { continue; } list( $type, $id ) = HyperDrop_Limits::split( $scope ); $valid = 't' === $type ? (bool) get_term( $id, 'product_cat' ) : 'product' === get_post_type( $id ); if ( $valid ) { // Only the limit names this kind of drop has; HyperDrop_Limits::set() ignores anything else. HyperDrop_Limits::set( $scope, array_intersect_key( $vals, HyperDrop_Limits::defaults( $type ) ) ); } } foreach ( array_keys( HyperDrop_Limits::globals() ) as $k ) { if ( isset( $globals[ $k ] ) ) { update_option( 'hyperdrop_limit_' . $k, absint( $globals[ $k ] ) ); } } self::done( __( 'Limits saved.', 'hyperdrop' ), array( 'tab' => 'limits' ) ); } /* ------------------------------------------------------------------ * Actions * ------------------------------------------------------------------ */ public static function done( $msg, $args = array() ) { self::flash( $msg ); wp_safe_redirect( self::url( $args ) ); exit; } /** * Keep a message for the current user's next HyperDrop screen ("Saved", "3 codes created"…), * instead of passing it in the URL. * * @param string $msg Message. * @param string $type success | error. */ public static function flash( $msg, $type = 'success' ) { if ( '' !== (string) $msg ) { set_transient( 'hyperdrop_flash_' . get_current_user_id(), array( (string) $msg, 'error' === $type ? 'error' : 'success' ), 5 * MINUTE_IN_SECONDS ); } } /** * Take (and clear) the current user's pending message. * * @return array{0:string,1:string}|null */ public static function take_flash() { $key = 'hyperdrop_flash_' . get_current_user_id(); $f = get_transient( $key ); if ( ! is_array( $f ) ) { return null; } delete_transient( $key ); return array( (string) $f[0], (string) $f[1] ); } /** * The HyperDrop admin page being shown, from WordPress itself (not from the URL). * * @return string Page slug, or ''. */ public static function current_page() { global $plugin_page; return is_string( $plugin_page ) ? sanitize_key( $plugin_page ) : ''; } /** * Read a navigation value (tab, drop, filter, search, page number) from a HyperDrop * admin link. Links and filter forms carry a "hyperdrop_nav" nonce (see url() and * nav_field()); without a valid nonce and the right permission, nothing is read. * * @param string $key Query argument. * @param string $default Value when missing or not verified. * @return string Sanitized text. */ public static function nav( $key, $default = '' ) { static $ok = null; if ( null === $ok ) { $ok = current_user_can( self::CAP ) && isset( $_GET['_hdnav'] ) && false !== wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_hdnav'] ) ), 'hyperdrop_nav' ); } if ( ! $ok || ! isset( $_GET[ $key ] ) || ! is_string( $_GET[ $key ] ) ) { return $default; } return sanitize_text_field( wp_unslash( $_GET[ $key ] ) ); } /** * Hidden nonce field for HyperDrop's GET filter forms (search, status filter). */ public static function nav_field() { echo ''; } protected static function parse_dt( $v ) { $v = sanitize_text_field( $v ); if ( ! $v ) { return ''; } $dt = date_create_immutable( $v, wp_timezone() ); return $dt ? $dt->format( 'Y-m-d H:i:s' ) : ''; } public static function do_generate() { self::require_cap(); check_admin_referer( 'hyperdrop_generate' ); self::$verified_action = 'hyperdrop_generate'; $scope = self::current_scope(); list( $product_id, $term_id ) = self::parse_scope( $scope ); $max_uses = isset( $_POST['max_uses'] ) ? absint( $_POST['max_uses'] ) : 1; $expires = isset( $_POST['expires'] ) ? self::parse_dt( sanitize_text_field( wp_unslash( $_POST['expires'] ) ) ) : ''; $custom = isset( $_POST['custom'] ) ? HyperDrop_Codes::normalize( sanitize_text_field( wp_unslash( $_POST['custom'] ) ) ) : ''; $count = isset( $_POST['count'] ) ? min( 1000, max( 1, absint( $_POST['count'] ) ) ) : 1; if ( $expires && strtotime( $expires ) <= strtotime( current_time( 'mysql' ) ) ) { /* translators: %s: current site time */ self::done( sprintf( __( 'That expiry time has already passed on the site clock (it is %s now). Codes not created.', 'hyperdrop' ), wp_date( 'M j, g:ia' ) ), array( 'tab' => 'generate' ) ); } $emails_raw = isset( $_POST['emails'] ) ? sanitize_textarea_field( wp_unslash( $_POST['emails'] ) ) : ''; $base = array( 'product_id' => $product_id, 'term_id' => $term_id, 'max_uses' => $max_uses, 'expires_at' => $expires, 'source' => 'manual', ); /** Add-ons can add their own fields from the "Create codes" form (nonce already checked). */ $base = (array) apply_filters( 'hyperdrop_generate_args', $base ); if ( $custom ) { $r = HyperDrop_Codes::create( array_merge( $base, array( 'code' => $custom ) ) ); if ( is_wp_error( $r ) ) { self::done( $r->get_error_message(), array( 'tab' => 'generate' ) ); } /* translators: %s: code */ self::done( sprintf( __( 'Created code %s.', 'hyperdrop' ), $r->code ), array( 'scope' => $scope ) ); } $emails = array_filter( array_unique( array_map( 'sanitize_email', preg_split( '/[\s,;]+/', $emails_raw ) ) ), 'is_email' ); if ( $emails ) { $sent = 0; foreach ( $emails as $email ) { $r = HyperDrop_Codes::create( array_merge( $base, array( 'email' => $email ) ) ); if ( ! is_wp_error( $r ) && HyperDrop_Mailer::send_code( $r ) ) { ++$sent; } } /* translators: 1: emails sent, 2: total */ self::done( sprintf( __( 'Emailed %1$d of %2$d codes.', 'hyperdrop' ), $sent, count( $emails ) ), array( 'scope' => $scope ) ); } $made = 0; for ( $i = 0; $i < $count; $i++ ) { if ( ! is_wp_error( HyperDrop_Codes::create( $base ) ) ) { ++$made; } } /* translators: %d: number of codes */ self::done( sprintf( _n( 'Created %d code.', 'Created %d codes.', $made, 'hyperdrop' ), $made ), array( 'scope' => $scope ) ); } public static function do_code_action() { self::require_cap(); check_admin_referer( 'hyperdrop_code_action' ); self::$verified_action = 'hyperdrop_code_action'; $id = isset( $_POST['id'] ) ? absint( $_POST['id'] ) : 0; $act = isset( $_POST['act'] ) ? sanitize_key( wp_unslash( $_POST['act'] ) ) : ''; $row = HyperDrop_Codes::get( $id ); if ( ! $row ) { self::done( __( 'Code not found.', 'hyperdrop' ) ); } switch ( $act ) { case 'send': $ok = HyperDrop_Mailer::send_code( $row ); self::done( $ok ? __( 'Code emailed.', 'hyperdrop' ) : __( 'Email could not be sent. Check your site email setup.', 'hyperdrop' ) ); break; case 'disable': HyperDrop_Codes::update( $id, array( 'status' => 'disabled' ) ); self::done( __( 'Code disabled.', 'hyperdrop' ) ); break; case 'enable': HyperDrop_Codes::update( $id, array( 'status' => 'active' ) ); self::done( __( 'Code enabled.', 'hyperdrop' ) ); break; case 'delete': HyperDrop_Codes::delete( $id ); self::done( __( 'Code deleted.', 'hyperdrop' ) ); break; } /** Add-ons handle their own row actions and return a message. */ self::done( (string) apply_filters( 'hyperdrop_code_action', '', $act, $row ) ); } public static function do_settings() { self::require_cap(); check_admin_referer( 'hyperdrop_settings' ); self::$verified_action = 'hyperdrop_settings'; update_option( 'hyperdrop_code_prefix', isset( $_POST['prefix'] ) ? HyperDrop_Codes::normalize( sanitize_text_field( wp_unslash( $_POST['prefix'] ) ) ) : '' ); foreach ( array( 'subject', 'heading', 'button' ) as $k ) { update_option( 'hyperdrop_email_' . $k, isset( $_POST[ $k ] ) ? sanitize_text_field( wp_unslash( $_POST[ $k ] ) ) : '' ); } update_option( 'hyperdrop_email_body', isset( $_POST['body'] ) ? sanitize_textarea_field( wp_unslash( $_POST['body'] ) ) : '' ); $lang = isset( $_POST['store_language'] ) ? sanitize_key( wp_unslash( $_POST['store_language'] ) ) : 'auto'; update_option( HyperDrop_I18n::OPTION, in_array( $lang, array( 'auto', 'en', 'es', 'visitor' ), true ) ? $lang : 'auto' ); self::done( __( 'Settings saved.', 'hyperdrop' ), array( 'tab' => 'settings' ) ); } }