BRICKMOAT_VERSION, 'mode' => Lockdown::is_on() ? 'lockdown' : 'normal', 'lockdown' => Lockdown::to_rest(), 'licence' => Licence::to_rest(), 'network' => Network::to_rest(), 'historyDays' => Settings::history_days(), 'onboarding' => Settings::onboarding(), 'cleanupPending' => Cleanup::pending_count(), 'protectedRoutes' => self::protected_routes(), 'currency' => array( 'code' => function_exists( 'get_woocommerce_currency' ) ? get_woocommerce_currency() : '', 'symbol' => $symbol, ), 'timezone' => wp_timezone_string(), 'urls' => self::urls(), // Only used if the administrator presses "Chat with support"; until then nothing is sent. 'supportChat' => array( 'websiteId' => Config::SUPPORT_CHAT_ID, 'siteUrl' => home_url(), ), 'development' => Config::is_development(), ); } /** * Links the admin screens point to. * * @return array */ private static function urls() { $policy_page = (int) get_option( 'wp_page_for_privacy_policy' ); $policy_edit = $policy_page > 0 ? get_edit_post_link( $policy_page, 'raw' ) : ''; $hpos = class_exists( '\Automattic\WooCommerce\Utilities\OrderUtil' ) && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); $site = (string) preg_replace( '#/api/v1$#', '', Config::api_base() ); /** * Filters the address of the Brickmoat website used for the upgrade and account links. * * @param string $site Address without a trailing slash. */ $site = untrailingslashit( (string) apply_filters( 'brickmoat_site_url', $site ) ); return array( 'privacyPolicyEdit' => $policy_edit ? $policy_edit : admin_url( 'options-privacy.php' ), 'exportPersonalData' => admin_url( 'export-personal-data.php' ), 'erasePersonalData' => admin_url( 'erase-personal-data.php' ), 'orders' => $hpos ? admin_url( 'admin.php?page=wc-orders' ) : admin_url( 'edit.php?post_type=shop_order' ), 'upgrade' => $site . '/pricing', 'account' => $site . '/account', 'support' => Config::SUPPORT_CHAT_URL, ); } /** * The ways to pay found on this store, as short labels. * * @return string[] */ public static function protected_routes() { $cached = get_transient( 'brickmoat_protected_routes' ); if ( is_array( $cached ) ) { return $cached; } $labels = array( __( 'Checkout page', 'brickmoat' ) ); try { $stripe = false; $paypal = false; $saved = false; foreach ( WC()->payment_gateways()->payment_gateways() as $gateway ) { if ( 'yes' !== $gateway->enabled ) { continue; } $id = strtolower( (string) $gateway->id ); $stripe = $stripe || false !== strpos( $id, 'stripe' ) || 'woocommerce_payments' === $id; $paypal = $paypal || false !== strpos( $id, 'ppcp' ) || false !== strpos( $id, 'paypal' ); $saved = $saved || $gateway->supports( 'add_payment_method' ) || $gateway->supports( 'tokenization' ); } if ( $stripe ) { $labels[] = __( 'Stripe card form', 'brickmoat' ); } if ( $paypal ) { $labels[] = __( 'PayPal button', 'brickmoat' ); } if ( $saved ) { $labels[] = __( 'Saved cards', 'brickmoat' ); } } catch ( \Throwable $error ) { unset( $error ); } $labels[] = __( 'Pay-by-link orders', 'brickmoat' ); set_transient( 'brickmoat_protected_routes', $labels, 5 * MINUTE_IN_SECONDS ); return $labels; } /** * The fee a payment provider charges per card attempt, used for "card fees avoided". * * @return float */ public static function fee_per_attempt() { /** * Filters the per-attempt card fee used to estimate "card fees avoided". * * @param float $fee Default 0.30, in the store currency. */ return max( 0.0, (float) apply_filters( 'brickmoat_fee_per_attempt', 0.30 ) ); } /** * GET /overview. * * @param string $range 24h, 7d or 30d. * @return array Overview. */ public static function overview( $range ) { $now = time(); $zone = wp_timezone(); $fee = self::fee_per_attempt(); // Bucket starts: hours for 24h, local days for 7d and 30d. $starts = array(); if ( '24h' === $range ) { $first = intdiv( $now, HOUR_IN_SECONDS ) * HOUR_IN_SECONDS - 23 * HOUR_IN_SECONDS; for ( $i = 0; $i < 24; $i++ ) { $starts[] = $first + $i * HOUR_IN_SECONDS; } } else { $days = '30d' === $range ? 30 : 7; $today = new \DateTimeImmutable( 'today', $zone ); for ( $i = $days - 1; $i >= 0; $i-- ) { $starts[] = $today->modify( '-' . $i . ' days' )->getTimestamp(); } } $from = $starts[0]; $period = $now - $from; $empty = array( 'allow' => 0, 'challenge' => 0, 'block' => 0, ); $hours = array(); $passed = 0; foreach ( Events::hourly( $from, $now + 1 ) as $row ) { $h = (int) $row['h'] * HOUR_IN_SECONDS; if ( ! isset( $hours[ $h ] ) ) { $hours[ $h ] = $empty; } if ( isset( $hours[ $h ][ $row['decision'] ] ) ) { $hours[ $h ][ $row['decision'] ] += (int) $row['n']; } $passed += (int) $row['paid_passed']; } ksort( $hours ); // Put each hour in its bucket. $series = array(); foreach ( $starts as $start ) { $series[] = array( 't' => Util::iso( $start ), 'allowed' => 0, 'challenged' => 0, 'blocked' => 0, ); } $last = count( $starts ) - 1; $index = 0; foreach ( $hours as $h => $counts ) { while ( $index < $last && $h >= $starts[ $index + 1 ] ) { ++$index; } $series[ $index ]['allowed'] += $counts['allow']; $series[ $index ]['challenged'] += $counts['challenge']; $series[ $index ]['blocked'] += $counts['block']; } $attempts = 0; $blocked = 0; $challenged = 0; foreach ( $series as $point ) { $attempts += $point['allowed'] + $point['challenged'] + $point['blocked']; $blocked += $point['blocked']; $challenged += $point['challenged']; } $previous = Events::totals( $from - $period, $from - 1 ); // Twelve points per number for the small trend lines. $span = max( 1, $period / 12 ); $sparks = array( 'attempts' => array_fill( 0, 12, 0 ), 'blocked' => array_fill( 0, 12, 0 ), 'challenged' => array_fill( 0, 12, 0 ), 'feesAvoided' => array_fill( 0, 12, 0 ), ); foreach ( $hours as $h => $counts ) { $slot = (int) min( 11, max( 0, floor( ( $h - $from ) / $span ) ) ); $sparks['attempts'][ $slot ] += $counts['allow'] + $counts['challenge'] + $counts['block']; $sparks['blocked'][ $slot ] += $counts['block']; $sparks['challenged'][ $slot ] += $counts['challenge']; } $running = 0; foreach ( $sparks['blocked'] as $slot => $value ) { $running += $value; $sparks['feesAvoided'][ $slot ] = $running; } foreach ( $sparks as $name => $values ) { $max = max( $values ); foreach ( $values as $slot => $value ) { $sparks[ $name ][ $slot ] = $max > 0 ? round( $value / $max, 3 ) : 0; } } $windows = array(); $latest = null; foreach ( Attacks::all( 50, $from ) as $attack ) { $windows[] = array( 'id' => (int) $attack['id'], 'start' => Util::iso( (int) $attack['started'] ), 'end' => Util::iso( (int) $attack['ended'] ), ); if ( null === $latest ) { $latest = Attacks::summary( $attack ); } } // Why attempts were stopped. One attempt can show several signs, so shares overlap. $stopped = 0; $groups = array(); foreach ( Events::stopped_reasons( $from, $now ) as $row ) { $n = (int) $row['n']; $stopped += $n; $seen = array(); foreach ( array_filter( explode( ',', (string) $row['reasons'] ) ) as $code ) { $group = Signals::group( $code ); if ( 'normal_shopper' === $code || isset( $seen[ $group ] ) ) { continue; } $seen[ $group ] = true; if ( ! isset( $groups[ $group ] ) ) { $groups[ $group ] = array( 'code' => $group, 'label' => Signals::general_label( $code ), 'n' => 0, ); } $groups[ $group ]['n'] += $n; } } usort( $groups, static function ( $a, $b ) { return $b['n'] - $a['n']; } ); $reasons = array(); foreach ( array_slice( $groups, 0, 5 ) as $group ) { $reasons[] = array( 'code' => $group['code'], 'label' => $group['label'], 'share' => $stopped > 0 ? round( $group['n'] / $stopped, 4 ) : 0, ); } return array( 'range' => $range, 'kpis' => array( 'attempts' => $attempts, 'attemptsChange' => $previous['attempts'] > 0 ? round( ( $attempts - $previous['attempts'] ) / $previous['attempts'], 4 ) : null, 'blocked' => $blocked, 'challenged' => $challenged, 'challengePassedPaid' => $passed, 'feesAvoided' => round( $blocked * $fee, 2 ), 'feePerAttempt' => $fee, ), 'sparks' => $sparks, 'series' => $series, 'attackWindows' => $windows, 'lastAttack' => $latest, 'recent' => Events::recent( 8, $from ), 'reasons' => $reasons, ); } }