'', 'product_id' => 0, 'term_id' => 0, 'email' => '', 'status' => 'active', 'source' => 'manual', 'max_uses' => 1, 'expires_at' => '', 'lang' => null, ) ); /** * Add-ons can attach extra data to new codes (e.g. a discount). * Recognised extra keys: discount_type, discount_amount. */ $args = (array) apply_filters( 'hyperdrop_create_code_args', $args ); $code = $args['code'] ? self::normalize( $args['code'] ) : ''; if ( $code && self::find( $code ) ) { return new WP_Error( 'hyperdrop_exists', __( 'That code already exists.', 'hyperdrop' ) ); } for ( $try = 0; $try < 10; $try++ ) { $candidate = $code ? $code : self::random_code(); $ok = $wpdb->insert( HyperDrop_DB::codes_table(), array( 'code' => $candidate, 'product_id' => absint( $args['product_id'] ), 'term_id' => absint( $args['term_id'] ), 'email' => sanitize_email( $args['email'] ), 'status' => in_array( $args['status'], array( 'active', 'pending', 'disabled' ), true ) ? $args['status'] : 'active', 'source' => sanitize_key( $args['source'] ), 'max_uses' => max( 0, (int) $args['max_uses'] ), 'uses' => 0, 'expires_at' => $args['expires_at'] ? $args['expires_at'] : null, 'discount_type' => isset( $args['discount_type'] ) ? (string) $args['discount_type'] : '', 'discount_amount' => isset( $args['discount_amount'] ) ? (float) $args['discount_amount'] : 0, 'lang' => in_array( $args['lang'], array( 'es', 'en' ), true ) ? $args['lang'] : ( null === $args['lang'] ? HyperDrop_I18n::lang_for_new_code() : '' ), 'created_at' => current_time( 'mysql' ), ) ); if ( $ok ) { return self::get( $wpdb->insert_id ); } if ( $code ) { break; // A fixed code failed; don't loop. } } return new WP_Error( 'hyperdrop_insert', __( 'Could not create the code.', 'hyperdrop' ) ); } public static function get( $id ) { global $wpdb; $id = (int) $id; if ( ! array_key_exists( $id, self::$cache ) ) { self::$cache[ $id ] = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE id = %d', HyperDrop_DB::codes_table(), $id ) ); } return self::$cache[ $id ]; } public static function find( $code ) { global $wpdb; $code = self::normalize( $code ); if ( '' === $code ) { return null; } return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE code = %s', HyperDrop_DB::codes_table(), $code ) ); } public static function find_for_email( $email, $product_id, $term_id = 0 ) { global $wpdb; return $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM %i WHERE email = %s AND product_id = %d AND term_id = %d ORDER BY id DESC LIMIT 1', HyperDrop_DB::codes_table(), sanitize_email( $email ), $product_id, $term_id ) ); } public static function update( $id, $data ) { global $wpdb; unset( self::$cache[ (int) $id ] ); return $wpdb->update( HyperDrop_DB::codes_table(), $data, array( 'id' => (int) $id ) ); } public static function delete( $id ) { global $wpdb; unset( self::$cache[ (int) $id ] ); return $wpdb->delete( HyperDrop_DB::codes_table(), array( 'id' => (int) $id ) ); } /** * Status and expiry checks shared by product and category codes. */ protected static function check_common( $row ) { if ( ! $row ) { return new WP_Error( 'hyperdrop_invalid', __( 'That code is not valid.', 'hyperdrop' ) ); } if ( 'pending' === $row->status ) { return new WP_Error( 'hyperdrop_pending', __( 'That code has not been activated yet.', 'hyperdrop' ) ); } if ( 'active' !== $row->status ) { return new WP_Error( 'hyperdrop_disabled', __( 'That code is no longer active.', 'hyperdrop' ) ); } if ( $row->expires_at && strtotime( $row->expires_at ) < strtotime( current_time( 'mysql' ) ) ) { return new WP_Error( 'hyperdrop_expired', __( 'That code has expired.', 'hyperdrop' ) ); } return true; } /** * Is this code valid for a protected category? * For categories, "uses" counts activations (devices/people), not orders. * * @param bool $activating True when the code is being entered; enforces the activation limit. */ public static function check_term( $row, $term_id, $activating = false ) { $c = self::check_common( $row ); if ( is_wp_error( $c ) ) { return $c; } if ( (int) $row->product_id || ( (int) $row->term_id && (int) $row->term_id !== (int) $term_id ) ) { return new WP_Error( 'hyperdrop_wrong_scope', __( 'That code is for a different collection.', 'hyperdrop' ) ); } if ( $activating && (int) $row->max_uses > 0 && (int) $row->uses >= (int) $row->max_uses ) { return new WP_Error( 'hyperdrop_used', __( 'That code has reached its limit.', 'hyperdrop' ) ); } return true; } /** * Count one activation of a category code. */ public static function add_activation( $code_id ) { global $wpdb; unset( self::$cache[ (int) $code_id ] ); $wpdb->query( $wpdb->prepare( 'UPDATE %i SET uses = uses + 1 WHERE id = %d', HyperDrop_DB::codes_table(), $code_id ) ); } /** * Is the code row valid for this product right now? true or WP_Error. */ public static function check( $row, $product_id ) { $c = self::check_common( $row ); if ( is_wp_error( $c ) ) { return $c; } if ( (int) $row->term_id || ( (int) $row->product_id && (int) $row->product_id !== (int) $product_id ) ) { return new WP_Error( 'hyperdrop_wrong_product', __( 'That code is for a different product.', 'hyperdrop' ) ); } if ( (int) $row->max_uses > 0 && (int) $row->uses >= (int) $row->max_uses ) { return new WP_Error( 'hyperdrop_used', __( 'That code has already been used.', 'hyperdrop' ) ); } return true; } /** * Log a code used on an order. $count_use: product-drop codes are limited * per order; category codes are limited per activation, so orders don't count. */ public static function record_redemption( $code_id, $order_id, $product_id, $qty, $count_use = true, $who = array() ) { global $wpdb; $wpdb->insert( HyperDrop_DB::redemptions_table(), array( 'code_id' => (int) $code_id, 'order_id' => (int) $order_id, 'product_id' => (int) $product_id, 'qty' => (int) $qty, 'counted' => $count_use ? 1 : 0, 'scope' => '', 'email' => isset( $who['email'] ) ? $who['email'] : '', 'user_id' => isset( $who['user_id'] ) ? (int) $who['user_id'] : 0, 'created_at' => current_time( 'mysql' ), ) ); if ( ! $count_use ) { return; } unset( self::$cache[ (int) $code_id ] ); $wpdb->query( $wpdb->prepare( 'UPDATE %i SET uses = uses + 1 WHERE id = %d', HyperDrop_DB::codes_table(), $code_id ) ); } public static function count_signups( $product_id, $term_id ) { global $wpdb; return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(DISTINCT email) FROM %i WHERE source = 'signup' AND product_id = %d AND term_id = %d", HyperDrop_DB::codes_table(), $product_id, $term_id ) ); } /** * Remember that a customer ordered from a locked scope (for "orders per customer"). */ public static function record_scope_order( $scope, $order_id, $who ) { global $wpdb; $wpdb->insert( HyperDrop_DB::redemptions_table(), array( 'code_id' => 0, 'order_id' => (int) $order_id, 'product_id' => 0, 'qty' => 0, 'counted' => 0, 'scope' => (string) $scope, 'email' => isset( $who['email'] ) ? $who['email'] : '', 'user_id' => isset( $who['user_id'] ) ? (int) $who['user_id'] : 0, 'created_at' => current_time( 'mysql' ), ) ); } /** * Admin listing. */ public static function query( $args = array() ) { global $wpdb; $args = wp_parse_args( $args, array( 'product_id' => 0, 'term_id' => 0, 'status' => '', 'search' => '', 'per_page' => 50, 'page' => 1, ) ); $product_id = (int) $args['product_id']; $term_id = (int) $args['term_id']; $status = (string) $args['status']; $like = '' !== (string) $args['search'] ? '%' . $wpdb->esc_like( (string) $args['search'] ) . '%' : ''; $limit = max( 0, (int) $args['per_page'] ); $offset = ( max( 1, (int) $args['page'] ) - 1 ) * $limit; // One fixed query: a filter that is 0 / '' matches every row. $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE ( %d = 0 OR product_id = %d ) AND ( %d = 0 OR term_id = %d ) AND ( %s = '' OR status = %s ) AND ( %s = '' OR code LIKE %s OR email LIKE %s )", HyperDrop_DB::codes_table(), $product_id, $product_id, $term_id, $term_id, $status, $status, $like, $like, $like ) ); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE ( %d = 0 OR product_id = %d ) AND ( %d = 0 OR term_id = %d ) AND ( %s = '' OR status = %s ) AND ( %s = '' OR code LIKE %s OR email LIKE %s ) ORDER BY id DESC LIMIT %d OFFSET %d", HyperDrop_DB::codes_table(), $product_id, $product_id, $term_id, $term_id, $status, $status, $like, $like, $like, $limit > 0 ? $limit : PHP_INT_MAX, $offset ) ); return array( 'total' => $total, 'rows' => $rows, ); } public static function stats() { global $wpdb; $t = HyperDrop_DB::codes_table(); return array( 'total' => (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $t ) ), 'signups' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE source = 'signup'", $t ) ), 'pending' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE status = 'pending'", $t ) ), 'redeemed' => (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i WHERE uses > 0", $t ) ), ); } }