*/ private static $counted = array(); /** * Hooks the filters in. */ public static function register() { /** * Filters which WooCommerce emails are held for attack traffic during lockdown. * * @param string[] $ids WooCommerce email ids. */ $ids = (array) apply_filters( 'brickmoat_held_email_ids', array( 'failed_order', 'customer_failed_order', 'cancelled_order', 'customer_cancelled_order', 'customer_invoice', 'customer_note' ) ); foreach ( $ids as $id ) { add_filter( 'woocommerce_email_enabled_' . sanitize_key( $id ), array( __CLASS__, 'maybe_hold' ), 20, 3 ); } } /** * Returns false for an email about an attack order while lockdown is on. * * @param bool $enabled Whether the email is enabled. * @param mixed $order The object the email is about. * @param \WC_Email $email The email (may be missing on old callers). * @return bool */ public static function maybe_hold( $enabled, $order = null, $email = null ) { try { if ( ! $enabled || ! ( $order instanceof \WC_Order ) || ! Lockdown::is_on() ) { return $enabled; } if ( ! self::is_attack_order( $order ) ) { return $enabled; } $key = $order->get_id() . ':' . ( is_object( $email ) && isset( $email->id ) ? $email->id : 'email' ); if ( ! isset( self::$counted[ $key ] ) ) { self::$counted[ $key ] = true; Attacks::add( Lockdown::attack_id(), 'emails_held', 1 ); } return false; } catch ( \Throwable $error ) { Util::log_error( 'Holding an email failed, email sent', $error ); return $enabled; } } /** * Whether an order is attack traffic: unpaid, and its attempt was logged as declined or blocked * for a visitor who is not on any always-allow list. * * @param \WC_Order $order Order. * @return bool */ public static function is_attack_order( $order ) { if ( null !== $order->get_date_paid() || ! $order->has_status( array( 'failed', 'pending', 'cancelled' ) ) ) { return false; } $current = Decider::current(); if ( $current && (int) $current['order_id'] === $order->get_id() ) { return '' === $current['trusted'] && in_array( $current['outcome'], array( 'declined', 'blocked' ), true ); } $row = Events::latest_for_order( $order->get_id() ); if ( ! $row || ! in_array( $row['outcome'], array( 'declined', 'blocked' ), true ) ) { return false; } return false === strpos( (string) $row['reasons'], ',trusted_' ); } }