WP_REST_Server::READABLE, 'callback' => array( $this, 'preset' ), 'permission_callback' => array( KeyBindingsController::class, 'allowed' ), 'args' => self::args(), ) ); register_rest_route( KeyBindingsController::NAMESPACE, '/recommended-rules/dry-run', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'dry_run' ), 'permission_callback' => array( KeyBindingsController::class, 'allowed' ), 'args' => self::args(), ) ); register_rest_route( KeyBindingsController::NAMESPACE, '/recommended-rules/apply', array( 'methods' => WP_REST_Server::CREATABLE, 'callback' => array( $this, 'apply' ), 'permission_callback' => array( KeyBindingsController::class, 'allowed' ), 'args' => self::args(), ) ); } /** * The refund threshold the owner may choose, in minor units. * * @return array> */ private static function args(): array { return array( 'refund_threshold_minor' => array( 'type' => 'integer', 'minimum' => 1, // A trillion minor units: a typo cannot make a rule that never fires. 'maximum' => 1000000000000, 'validate_callback' => 'rest_validate_request_arg', ), ); } /** * The threshold a request chose, or null for the default. * * @param WP_REST_Request|null $request Request. */ private static function threshold( $request ): ?int { $chosen = null === $request ? null : $request->get_param( 'refund_threshold_minor' ); return null === $chosen ? null : (int) $chosen; } /** * The preset and what to know before applying it. * * @param WP_REST_Request|null $request Request. */ public function preset( $request = null ): WP_REST_Response { $currency = new StoreCurrency(); return new WP_REST_Response( array( 'rules' => RecommendedRules::rules( $currency, self::threshold( $request ) ), 'priority' => RecommendedRules::PRIORITY, 'currency' => $currency->base(), 'decimals' => $currency->decimals(), 'refund_threshold_minor' => RecommendedRules::REFUND_THRESHOLD * 10 ** $currency->decimals(), 'suggested_threshold_minor' => RecommendedRules::suggested_threshold( self::recent_totals( $currency ) ), 'applied_policy_id' => self::applied(), ), 200 ); } /** * What the preset would have done to the last week's agent activity, * read through the firewall's own ledger API. * * @param WP_REST_Request $request Request. */ public function dry_run( $request ): WP_REST_Response { $rows = array(); $truncated = false; $incomplete = false; $since = gmdate( 'Y-m-d\\TH:i:s\\Z', time() - self::DRY_RUN_DAYS * DAY_IN_SECONDS ); for ( $page = 1; $page <= self::DRY_RUN_PAGES; $page++ ) { $ledger = new WP_REST_Request( 'GET', '/agenticdaisy-firewall/v1/ledger' ); $ledger->set_query_params( array( 'since' => $since, 'per_page' => 100, 'page' => $page, ) ); $response = rest_do_request( $ledger ); if ( $response->is_error() ) { // Said, never counted as no activity. $incomplete = true; break; } $batch = $response->get_data(); $rows = array_merge( $rows, is_array( $batch ) ? array_values( array_filter( $batch, 'is_array' ) ) : array() ); $pages = (int) ( $response->get_headers()['X-WP-TotalPages'] ?? 0 ); if ( $page >= $pages ) { break; } $truncated = self::DRY_RUN_PAGES === $page; } return new WP_REST_Response( array( 'days' => self::DRY_RUN_DAYS, 'truncated' => $truncated, 'incomplete' => $incomplete, ) + PresetDryRun::run( RecommendedRules::rules( new StoreCurrency(), self::threshold( $request ) ), $rows ), 200 ); } /** * Create the preset as a firewall policy. * * @param WP_REST_Request|null $request Request. * @return WP_REST_Response|WP_Error */ public function apply( $request = null ) { $existing = self::applied(); if ( null !== $existing ) { return new WP_Error( 'adafwc_recommended_rules_applied', /* translators: %d: policy id. */ sprintf( __( 'The recommended rules are already applied as policy %d. Edit them there, or delete that policy to apply them again.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), $existing ), array( 'status' => 409 ) ); } $create = new WP_REST_Request( 'POST', '/agenticdaisy-firewall/v1/policies' ); $create->set_body_params( array( 'rules' => RecommendedRules::rules( new StoreCurrency(), self::threshold( $request ) ), 'priority' => RecommendedRules::PRIORITY, ) ); $response = rest_do_request( $create ); $refused = $response->as_error(); if ( null !== $refused ) { return $refused; } $policy = $response->get_data(); $id = is_array( $policy ) && isset( $policy['id'] ) ? (int) $policy['id'] : 0; update_option( self::APPLIED_OPTION, $id, false ); return new WP_REST_Response( $policy, 201 ); } /** * The policy the preset was applied as, if it still exists. */ private static function applied(): ?int { $id = (int) get_option( self::APPLIED_OPTION, 0 ); if ( $id <= 0 ) { return null; } $response = rest_do_request( new WP_REST_Request( 'GET', '/agenticdaisy-firewall/v1/policies' ) ); $policies = $response->is_error() ? array() : $response->get_data(); foreach ( is_array( $policies ) ? $policies : array() as $policy ) { if ( is_array( $policy ) && (int) ( $policy['id'] ?? 0 ) === $id ) { return $id; } } delete_option( self::APPLIED_OPTION ); return null; } /** * Recent paid orders' totals, in base-currency minor units. * * @param StoreCurrency $currency The store's money. * @return list */ private static function recent_totals( StoreCurrency $currency ): array { $orders = wc_get_orders( array( 'status' => array( 'processing', 'completed' ), 'limit' => self::SUGGESTION_ORDERS, 'date_created' => '>' . ( time() - self::SUGGESTION_DAYS * 86400 ), 'type' => 'shop_order', ) ); $totals = array(); foreach ( is_array( $orders ) ? $orders : array() as $order ) { if ( ! $order instanceof \WC_Order ) { continue; } $minor = Money::to_minor( (string) $order->get_total(), $currency->decimals() ); $base = null === $minor ? null : $currency->to_base( $minor, $order ); if ( null !== $base ) { $totals[] = $base; } } return $totals; } }