order|refund|order_note|order_fulfillment|product|product_variation|coupon)/(?P\d+)', array( 'methods' => WP_REST_Server::READABLE, 'callback' => array( $this, 'context' ), 'permission_callback' => array( self::class, 'allowed' ), ) ); } /** * Store managers, never an agent. */ public static function allowed(): bool { return current_user_can( 'manage_woocommerce' ) && null === Api::current_agent(); } /** * The target's context. * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function context( $request ) { $type = (string) $request['type']; $id = (int) $request['id']; $context = match ( true ) { in_array( $type, self::ORDER_TYPES, true ) => self::order( $id ), 'coupon' === $type => self::coupon( $id ), default => self::product( $id ), }; return null === $context ? new WP_Error( 'adafwc_not_found', __( 'It no longer exists.', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ), array( 'status' => 404 ) ) : new WP_REST_Response( $context, 200 ); } /** * An order. * * @param int $id Order id. * @return array|null */ private static function order( int $id ): ?array { $order = wc_get_order( $id ); if ( ! $order instanceof \WC_Order ) { return null; } $customer = trim( $order->get_formatted_billing_full_name() ); return array( 'kind' => 'order', 'number' => $order->get_order_number(), 'edit_link' => $order->get_edit_order_url(), 'customer' => '' === $customer ? __( 'Guest', 'agentic-daisy-ai-agent-firewall-for-woocommerce' ) : $customer, 'total' => self::price( $order->get_total(), $order->get_currency() ), 'refunded' => self::price( $order->get_total_refunded(), $order->get_currency() ), 'gateway' => '' === $order->get_payment_method_title() ? $order->get_payment_method() : $order->get_payment_method_title(), 'status' => $order->get_status(), ); } /** * A product or variation. * * @param int $id Product id. * @return array|null */ private static function product( int $id ): ?array { $product = wc_get_product( $id ); if ( ! $product instanceof \WC_Product ) { return null; } return array( 'kind' => 'product', 'name' => $product->get_name(), 'edit_link' => get_edit_post_link( $product->get_parent_id() > 0 ? $product->get_parent_id() : $id, 'raw' ), 'fields' => self::fields( $product, self::PRODUCT_FIELDS ), ); } /** * A coupon. * * @param int $id Coupon id. * @return array|null */ private static function coupon( int $id ): ?array { $coupon = new \WC_Coupon( $id ); if ( $coupon->get_id() !== $id || 0 === $id ) { return null; } return array( 'kind' => 'coupon', 'name' => $coupon->get_code(), 'edit_link' => get_edit_post_link( $id, 'raw' ), 'fields' => self::fields( $coupon, self::COUPON_FIELDS ), ); } /** * Stored values by field, as the REST API names them; dates as ISO. * * @param \WC_Data $data The object. * @param list $fields Field names. * @return array */ private static function fields( \WC_Data $data, array $fields ): array { $values = array(); foreach ( $fields as $field ) { $getter = 'get_' . $field; $value = is_callable( array( $data, $getter ) ) ? $data->{$getter}( 'edit' ) : null; $values[ $field ] = $value instanceof \WC_DateTime ? $value->format( DATE_ATOM ) : $value; } return $values; } /** * An amount as the store prints it, as text. * * @param float|string $amount Amount. * @param string $currency Currency code. */ private static function price( $amount, string $currency ): string { return html_entity_decode( wp_strip_all_tags( wc_price( (float) $amount, array( 'currency' => $currency ) ) ), ENT_QUOTES, 'UTF-8' ); } }