__( 'Your access code for {product}', 'hyperdrop' ), 'heading' => __( "You're in 🔓", 'hyperdrop' ), 'body' => __( "Here's your exclusive access code for {product}:\n\n{code}\n\nTap the button below to unlock it instantly, or enter the code on the product page.", 'hyperdrop' ), 'button' => __( 'Unlock now', 'hyperdrop' ), ); } public static function get( $key ) { $d = self::defaults(); $v = get_option( 'hyperdrop_email_' . $key, '' ); return '' !== $v ? $v : $d[ $key ]; } /** * One-tap unlock link for emails: the drop's page with the code after "#". * The code never reaches the server in the URL; hyperdrop.js puts it into the * page's unlock form and submits that (a normal nonce-checked POST). * * @param object $row Code row. * @return string */ public static function magic_link( $row ) { $url = ''; if ( ! empty( $row->term_id ) ) { $link = get_term_link( (int) $row->term_id, 'product_cat' ); $url = is_wp_error( $link ) ? '' : $link; } elseif ( ! empty( $row->product_id ) ) { $url = (string) get_permalink( (int) $row->product_id ); } if ( ! $url ) { return home_url( '/' ); } return $url . '#hyperdrop-code=' . rawurlencode( $row->code ); } public static function row_scope( $row ) { if ( ! empty( $row->term_id ) ) { return 't:' . (int) $row->term_id; } return ! empty( $row->product_id ) ? 'p:' . (int) $row->product_id : ''; } protected static function button( $link, $label ) { return '
'; } /** * "It's live!" email sent when a scheduled drop starts. */ public static function send_live( $row ) { return HyperDrop_I18n::as_shopper( function () use ( $row ) { return self::build_and_send_live( $row ); }, isset( $row->lang ) ? $row->lang : '' ); } protected static function build_and_send_live( $row ) { if ( ! $row || ! is_email( $row->email ) ) { return false; } $name = self::scope_name( $row ); /* translators: %s: collection or product name */ $subject = apply_filters( 'hyperdrop_live_subject', sprintf( __( '%s is live now 🔥', 'hyperdrop' ), $name ), $row ); $heading = __( "It's live!", 'hyperdrop' ); /* translators: %s: collection or product name */ $body = '' . esc_html( sprintf( __( 'The exclusive drop you signed up for — %s — just opened. Tap below to get in before it sells out.', 'hyperdrop' ), $name ) ) . '
'; $body .= '' . esc_html__( 'Your access code:', 'hyperdrop' ) . '
'; $body .= '' . esc_html( sprintf( __( 'Ends %s.', 'hyperdrop' ), HyperDrop_Schedule::fmt( $end ) ) ) . '
'; } $body .= self::button( self::magic_link( $row ), __( 'Shop the drop', 'hyperdrop' ) ); $mailer = WC()->mailer(); $message = apply_filters( 'hyperdrop_live_email_message', $mailer->wrap_message( $heading, $body ), $row ); return $mailer->send( $row->email, $subject, $message, "Content-Type: text/html\r\n" ); } /** * Name of what the code unlocks. */ public static function scope_name( $row ) { if ( ! empty( $row->term_id ) ) { $t = get_term( (int) $row->term_id, 'product_cat' ); return ( $t && ! is_wp_error( $t ) ) ? $t->name : get_bloginfo( 'name' ); } return $row->product_id ? get_the_title( $row->product_id ) : get_bloginfo( 'name' ); } /** * @param object $row Code row (must have email). */ public static function send_code( $row ) { return HyperDrop_I18n::as_shopper( function () use ( $row ) { return self::build_and_send_code( $row ); }, isset( $row->lang ) ? $row->lang : '' ); } protected static function build_and_send_code( $row ) { if ( ! $row || ! is_email( $row->email ) ) { return false; } $product_name = self::scope_name( $row ); $link = self::magic_link( $row ); $repl = array( '{product}' => $product_name, '{site}' => get_bloginfo( 'name' ), '{link}' => $link, ); $subject = strtr( self::get( 'subject' ), $repl ); $heading = strtr( self::get( 'heading' ), $repl ); $code_html = '' . esc_html( sprintf( __( 'The drop opens on %s — your code works from then. Save this email!', 'hyperdrop' ), HyperDrop_Schedule::fmt( HyperDrop_Schedule::get( $scope )['start'] ) ) ) . '
'; } $body .= self::button( $link, self::get( 'button' ) ); $mailer = WC()->mailer(); $message = $mailer->wrap_message( $heading, $body ); $message = apply_filters( 'hyperdrop_email_message', $message, $row ); $sent = $mailer->send( $row->email, $subject, $message, "Content-Type: text/html\r\n" ); if ( $sent ) { HyperDrop_Codes::update( $row->id, array( 'sent_at' => current_time( 'mysql' ) ) ); } do_action( 'hyperdrop_code_sent', $row, $sent ); return $sent; } }