, shippingMethods: array, categories: array} */ public static function all(): array { return [ 'paymentMethods' => self::payment_methods(), 'shippingMethods' => self::shipping_methods(), 'categories' => self::categories(), ]; } /** * Installed payment gateways, enabled or not: old orders keep the ID of * a gateway that has since been switched off. * * @return array */ private static function payment_methods(): array { $out = []; foreach ( WC()->payment_gateways()->payment_gateways() as $id => $gateway ) { $label = self::text( (string) $gateway->get_method_title() ); if ( '' === $label ) { $label = self::text( (string) $gateway->get_title() ); } $out[] = [ 'key' => (string) $id, 'label' => '' !== $label ? $label : (string) $id, ]; } return $out; } /** * Shipping method types (Flat rate, Free shipping, Local pickup, ...), * which is what each order's shipping line records as method_id. * * @return array */ private static function shipping_methods(): array { $out = []; foreach ( WC()->shipping()->get_shipping_methods() as $id => $method ) { $label = self::text( (string) $method->get_method_title() ); $out[] = [ 'key' => (string) $id, 'label' => '' !== $label ? $label : (string) $id, ]; } return $out; } /** * Every product category with its path ("Clothing › Hoodies"), sorted so * children follow their parent. * * @return array */ private static function categories(): array { $terms = get_terms( [ 'taxonomy' => 'product_cat', 'hide_empty' => false, ] ); if ( ! is_array( $terms ) ) { return []; } $by_id = []; foreach ( $terms as $term ) { if ( $term instanceof \WP_Term ) { $by_id[ $term->term_id ] = $term; } } $out = []; foreach ( $by_id as $id => $term ) { $path = [ self::text( $term->name ) ]; $parent = $term->parent; $depth = 0; while ( $parent && isset( $by_id[ $parent ] ) && $depth < 10 ) { array_unshift( $path, self::text( $by_id[ $parent ]->name ) ); $parent = $by_id[ $parent ]->parent; ++$depth; } $out[] = [ 'id' => (int) $id, 'label' => implode( ' › ', $path ), ]; } usort( $out, static fn( array $a, array $b ) => strnatcasecmp( $a['label'], $b['label'] ) ); return $out; } /** * Plain text for the app: no tags, entities decoded (React escapes). * * @param string $value Raw text. */ private static function text( string $value ): string { return trim( html_entity_decode( wp_strip_all_tags( $value ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); } }