*/ private const REST_MAP = array( array( '#^orders/(\d+)/refunds(?:/\d+)?$#i', 'refund', null, 1 ), array( '#^refunds(?:/\d+)?$#i', 'refund', null, -1, 'order_id' ), array( '#^orders/(\d+)/notes(?:/\d+)?$#i', 'order_note', null, 1 ), array( '#^order-notes(?:/\d+)?$#i', 'order_note', null, -1, 'order_id' ), array( '#^orders/(\d+)/fulfillments(?:/\d+)?$#i', 'order_fulfillment', null, 1 ), array( '#^fulfillments(?:/\d+)?$#i', 'order_fulfillment', null, -1, 'entity_id' ), array( '#^orders/batch$#i', 'order', 'update', -1 ), array( '#^orders(?:/(\d+))?$#i', 'order', null, 1 ), array( '#^products/\d+/variations/batch$#i', 'product_variation', 'update', -1 ), array( '#^products/\d+/variations(?:/(\d+))?$#i', 'product_variation', null, 1 ), array( '#^products/(categories|tags|attributes|brands|shipping_classes)(?:/.*)?$#i', 'product_taxonomy', null, -1 ), array( '#^products/reviews(?:/.*)?$#i', 'product_review', null, 0 ), array( '#^products/batch$#i', 'product', 'update', -1 ), array( '#^products/(\d+)/duplicate$#i', 'product', 'create', 1 ), array( '#^products(?:/(\d+))?$#i', 'product', null, 1 ), array( '#^coupons/batch$#i', 'coupon', 'update', -1 ), array( '#^coupons(?:/(\d+))?$#i', 'coupon', null, 1 ), array( '#^customers/batch$#i', 'customer', 'update', -1 ), array( '#^customers(?:/(\d+))?$#i', 'customer', null, 1 ), array( '#^settings/(payment-gateways|payments)(?:/.*)?$#i', 'payment_gateway', 'update', -1 ), array( '#^settings(?:/.*)?$#i', 'wc_setting', 'update', -1 ), array( '#^payment_gateways(?:/.*)?$#i', 'payment_gateway', null, -1 ), array( '#^(shipping/zones|shipping-zones|shipping-zone-method)(?:/.*)?$#i', 'shipping', null, 0 ), array( '#^taxes(?:/.*)?$#i', 'tax', null, 0 ), array( '#^webhooks(?:/.*)?$#i', 'webhook', null, 0 ), array( '#^system_status/tools(?:/.*)?$#i', 'system_tool', 'update', -1 ), ); /** * The wc-admin map, in the same shape. * * @var list */ private const ADMIN_MAP = array( array( '#^options$#i', 'wc_setting', 'update', -1 ), array( '#^plugins(?:/.*)?$#i', 'wc_plugin', null, -1 ), array( '#^settings/payments(?:/.*)?$#i', 'payment_gateway', 'update', -1 ), ); /** * Entity types targeted at their order. */ private const ORDER_CHILDREN = array( 'refund', 'order_note', 'order_fulfillment' ); /** * What any other write under these namespaces is: a WooCommerce write * this map does not know, held by the default rules rather than allowed * by the firewall's built-in defaults. */ public const OTHER = 'wc_other'; /** * Classify a REST action, or leave it to the core. * * @param ActionDescriptor $action The core's descriptor. */ public function __invoke( ActionDescriptor $action ): ?Classification { if ( 'rest' !== $action->source || null === $action->route || 1 !== preg_match( self::ROUTE_PATTERN, $action->route, $namespace ) ) { return null; } $path = trim( substr( $action->route, strlen( $namespace[0] ) ), '/' ); $map = str_starts_with( strtolower( $namespace[1] ), 'wc/' ) ? self::REST_MAP : ( 'wc-admin' === strtolower( $namespace[1] ) ? self::ADMIN_MAP : array() ); foreach ( $map as $row ) { if ( 1 !== preg_match( $row[0], $path, $matches ) ) { continue; } return new Classification( $row[1], $row[2] ?? $action->action_cat, self::target( $action, $row, $matches ), self::destructive( $action, $row[1] ) ); } return new Classification( self::OTHER, $action->action_cat, $action->entity_id, self::destructive( $action, self::OTHER ) ); } /** * The target id a map row names. * * @param ActionDescriptor $action The core's descriptor. * @param array{0: string, 1: string, 2: string|null, 3: int, 4?: string} $row The matching row. * @param array $matches Its captures. */ private static function target( ActionDescriptor $action, array $row, array $matches ): ?int { // A body or query order_id overrides the one in the route for // WooCommerce, which reads merged parameters: /orders/1/refunds with // {"order_id": 2} refunds order 2, so order 2 is the target. if ( in_array( $row[1], self::ORDER_CHILDREN, true ) ) { $named = WcBatchExpander::id( $action->input['order_id'] ?? null ); if ( null !== $named ) { return $named; } } if ( isset( $row[4] ) ) { return WcBatchExpander::id( $action->input[ $row[4] ] ?? null ); } if ( 0 === $row[3] ) { return $action->entity_id; } return $row[3] > 0 && isset( $matches[ $row[3] ] ) ? (int) $matches[ $row[3] ] : null; } /** * Whether the write cannot be taken back, which the core cannot see. * * @param ActionDescriptor $action The core's descriptor. * @param string $entity_type The type it was given. */ private static function destructive( ActionDescriptor $action, string $entity_type ): ?bool { return self::destructive_write( $entity_type, $action->action_cat, $action->input ); } /** * Whether a store write is destructive by its input: a delete with * `force` skips the trash, and a note to the customer is an email. Null * leaves the core's reading. Also how the onboarding dry run reads * ledger rows. * * @param string $entity_type Entity type. * @param string $action_cat Category. * @param array $input The write's input. */ public static function destructive_write( string $entity_type, string $action_cat, array $input ): ?bool { if ( 'delete' === $action_cat && self::truthy( $input['force'] ?? null ) ) { return true; } if ( 'order_note' === $entity_type && 'create' === $action_cat && self::truthy( $input['customer_note'] ?? null ) ) { return true; } return null; } /** * Whether a request parameter is true as WordPress's REST API reads a * boolean: true, 1, "1", "true". * * @param mixed $value Parameter. */ public static function truthy( mixed $value ): bool { return true === filter_var( $value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE ); } }