| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791 |
- <?php
- /**
- * Plugin Name: EKSRelay API
- * Description: Dedykowane REST API dla EKSRelay – get_car_data i get_product_compatibility.
- * Otwarte endpointy REST (bez autoryzacji) – odpowiedniki nopriv AJAX.
- * Wdróż ten plik na serwer WP jako: /wp-content/mu-plugins/eksrelay_api.php
- * Version: 1.0.0
- */
- if ( ! defined( 'ABSPATH' ) ) {
- exit;
- }
- // ═══════════════════════════════════════════════════════════════════
- // Rejestracja tras REST
- // ═══════════════════════════════════════════════════════════════════
- add_action( 'rest_api_init', function () {
- register_rest_route( 'eksrelay/v1', '/car-data', [
- 'methods' => 'POST',
- 'callback' => 'eksrelay_car_data',
- 'permission_callback' => '__return_true',
- ] );
- register_rest_route( 'eksrelay/v1', '/product-compatibility', [
- 'methods' => 'POST',
- 'callback' => 'eksrelay_product_compatibility',
- 'permission_callback' => '__return_true',
- ] );
- /**
- * GET /wp-json/eksrelay/v1/shipping-costs?zone_id=X¤cy=EUR
- *
- * Returns shipping methods for a zone with costs in the requested currency.
- * WCML stores per-currency costs in wp_options under the key
- * woocommerce_{method_id}_{instance_id}_settings as cost_EUR, cost_CZK, etc.
- */
- register_rest_route( 'eksrelay/v1', '/shipping-costs', [
- 'methods' => 'GET',
- 'callback' => 'eksrelay_shipping_costs',
- 'permission_callback' => '__return_true',
- ] );
- } );
- // ═══════════════════════════════════════════════════════════════════
- // Handler: POST /wp-json/eksrelay/v1/car-data
- //
- // Body JSON: { car_year, car_brand?, car_model?, car_engine? }
- // ═══════════════════════════════════════════════════════════════════
- function eksrelay_car_data( WP_REST_Request $request ) {
- $body = $request->get_json_params() ?: [];
- if ( empty( $body['car_year'] ) ) {
- return [ 'error' => 'car_year jest wymagany' ];
- }
- $car_year = intval( sanitize_text_field( $body['car_year'] ) );
- $has_brand = ! empty( $body['car_brand'] );
- $has_model = ! empty( $body['car_model'] );
- $car_brand = false;
- $car_model = false;
- if ( ! $has_brand && ! $has_model ) {
- return [ 'error' => 'car_brand lub car_model jest wymagany' ];
- }
- // ── Tylko brand, bez modelu ──────────────────────────────────────
- if ( $has_brand && ! $has_model ) {
- $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
- $termIds = get_terms( [ 'name__like' => $car_brand, 'parent' => 0, 'fields' => 'ids' ] );
- if ( count( $termIds ) === 1 ) {
- $car_models = eksrelay_models_for_brand_year( $termIds, $car_year );
- return [ 'error' => 'options', 'field' => 'car_model', 'options' => $car_models, 'filtered' => false ];
- }
- return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
- }
- // ── Tylko model, bez brandu ─────────────────────────────────────
- if ( $has_model && ! $has_brand ) {
- $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
- $termIds = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
- // exact match → auto-wybierz
- foreach ( $termIds as $tid ) {
- $term = get_term_by( 'id', $tid, 'car_model' );
- if ( $term && eksrelay_replace_ascii( $term->name ) === $car_model ) {
- $termIds = [ $term->term_id ];
- break;
- }
- }
- if ( count( $termIds ) === 1 ) {
- $term = get_term( $termIds[0] );
- if ( ! is_wp_error( $term ) && $term->parent !== 0 ) {
- $parent = get_term( $term->parent );
- if ( ! is_wp_error( $parent ) ) {
- $car_brand = $parent->name;
- }
- } else {
- return [ 'error' => 'nie znaleziono takiego modelu', 'car_model' => $car_model ];
- }
- } else {
- $models = [];
- foreach ( $termIds as $tid ) {
- $t = get_term_by( 'id', $tid, 'car_model' );
- if ( $t ) {
- $models[] = $t->name;
- }
- }
- if ( $models ) {
- return [ 'error' => 'options', 'field' => 'car_model', 'options' => $models, 'filtered' => true ];
- }
- return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
- }
- }
- // ── Mamy oba ────────────────────────────────────────────────────
- if ( ! $car_brand ) {
- $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
- }
- if ( ! $car_model ) {
- $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
- }
- $car_engine = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_engine'] ?? '' ) ) );
- $termIds = get_terms( [ 'name__like' => $car_brand, 'fields' => 'ids' ] );
- $termIds2 = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
- if ( ! count( $termIds ) ) {
- return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
- }
- if ( ! count( $termIds2 ) ) {
- if ( count( $termIds ) === 1 ) {
- $car_models = eksrelay_models_for_brand_year( $termIds, $car_year );
- return [ 'error' => 'options', 'field' => 'car_model', 'options' => $car_models, 'filtered' => false ];
- }
- return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
- }
- $query = new WP_Query( [
- 'post_type' => 'car',
- 'posts_per_page' => -1,
- 'tax_query' => [
- 'relation' => 'AND',
- [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds ],
- [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds2 ],
- [ 'taxonomy' => 'car_production_year', 'field' => 'slug', 'terms' => (string) $car_year ],
- ],
- ] );
- $cars = $query->get_posts();
- $engine_info = eksrelay_get_engines_info( $cars, true, $car_year );
- if ( ! $cars ) {
- return [
- 'error' => 'nie znaleziono auta, ' . get_option(
- 'aiac_tool_get_car_data_no_car',
- 'Orientacyjnie do 1994 roku włącznie powinien pasować gaz R12, dla aut 1995-2016 gaz R134A a dla aut od 2016 roku gaz R1234YF'
- ),
- 'car_brand' => $car_brand,
- 'car_model' => $car_model,
- ];
- }
- $engine_names = array_column( $engine_info, 'name' );
- if ( count( $engine_info ) === 1 ) {
- $car_engine = $engine_info[0]['name'];
- }
- if ( count( $engine_info ) > 1 && ! $car_engine ) {
- return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_names, 'filtered' => false ];
- }
- foreach ( $engine_info as $eng ) {
- if ( strtolower( trim( $eng['name'] ) ) !== strtolower( trim( $car_engine ) ) ) {
- continue;
- }
- $car_id = intval( $eng['id'] );
- $maybe_diff = get_field( 'custom_ac', $car_id );
- $gas_types = array_unique( array_filter( [
- get_field( 'ac_gas_type', $car_id ),
- $maybe_diff ? get_field( 'ac_gas_type_2', $car_id ) : '',
- ] ) );
- $year_term = get_terms( [ 'taxonomy' => 'car_production_year', 'name__like' => $car_year ] );
- $brand_term = $termIds ? get_term( $termIds[0] ) : false;
- $model_term = $termIds2 ? get_term( $termIds2[0] ) : false;
- $data = [
- 'success' => true,
- 'selected_car' => [
- 'ek_ac_gas_type' => implode( ',', $gas_types ),
- 'ek_brand_id' => $termIds[0] ?? '',
- 'ek_brand' => ( $brand_term && ! is_wp_error( $brand_term ) ) ? $brand_term->name : $car_brand,
- 'ek_car_id' => $car_id,
- 'ek_engine_type' => $eng['name'],
- 'ek_info' => '',
- 'ek_model_id' => $termIds2[0] ?? '',
- 'ek_model' => ( $model_term && ! is_wp_error( $model_term ) ) ? $model_term->name : $car_model,
- 'ek_year_id' => ( ! is_wp_error( $year_term ) && $year_term ) ? $year_term[0]->term_id : '',
- 'ek_year' => $car_year,
- ],
- 'shop_url' => $eng['url'],
- 'adapters' => get_field( 'adapters', $car_id ) ?: false,
- 'ac_gas_type' => get_field( 'ac_gas_type', $car_id ),
- 'ac_gas_amount' => ( get_field( 'ac_gas_amount', $car_id ) ?: '-' ) . ' g',
- 'ac_ports_count' => intval( get_field( 'ac_ports_count', $car_id ) ),
- 'ac_oil' => get_field( 'ac_oil', $car_id ),
- 'ac_oil_amount' => ( get_field( 'ac_oil_amount', $car_id ) ?: '-' ) . ' cm3',
- 'ac_clutch' => get_field( 'ac_clutch', $car_id ),
- 'maybe_different_layout' => $maybe_diff,
- ];
- if ( $maybe_diff ) {
- $data['different_layout_info'] = get_field( 'custom_ac_info', $car_id ) ?: '';
- $data['different_layout_adapters'] = get_field( 'adapters_2', $car_id ) ?: false;
- $data['different_layout_ac_gas_type'] = get_field( 'ac_gas_type_2', $car_id );
- $data['different_layout_ac_gas_amount'] = ( get_field( 'ac_gas_amount_2', $car_id ) ?: '-' ) . ' g';
- $data['different_layout_ac_ports_count'] = get_field( 'ac_ports_count_2', $car_id );
- $data['different_layout_ac_oil'] = get_field( 'ac_oil_2', $car_id );
- $data['different_layout_ac_oil_amount'] = ( get_field( 'ac_oil_amount_2', $car_id ) ?: '-' ) . ' cm3';
- $data['different_layout_ac_clutch'] = get_field( 'ac_clutch_2', $car_id );
- }
- return $data;
- }
- // podany silnik nie pasuje do żadnego na liście → pokaż opcje
- return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_names, 'filtered' => false ];
- }
- // ═══════════════════════════════════════════════════════════════════
- // Handler: POST /wp-json/eksrelay/v1/product-compatibility
- //
- // Body JSON: { car_year, car_brand?, car_model?, car_engine?, product_name? }
- // ═══════════════════════════════════════════════════════════════════
- function eksrelay_product_compatibility( WP_REST_Request $request ) {
- $body = $request->get_json_params() ?: [];
- if ( empty( $body['car_year'] ) ) {
- return [ 'error' => 'car_year jest wymagany' ];
- }
- $lang = sanitize_text_field( $body['lang'] ?? 'pl' );
- $currency = strtoupper( sanitize_text_field( $body['currency'] ?? 'PLN' ) );
- $car_year = intval( sanitize_text_field( $body['car_year'] ) );
- $has_brand = ! empty( $body['car_brand'] );
- $has_model = ! empty( $body['car_model'] );
- $car_brand = false;
- $car_model = false;
- // ── Tylko brand ─────────────────────────────────────────────────
- if ( $has_brand && ! $has_model ) {
- $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
- $termIds = get_terms( [ 'name__like' => $car_brand, 'parent' => 0, 'fields' => 'ids' ] );
- if ( count( $termIds ) === 1 ) {
- $car_models = eksrelay_models_for_brand_year( $termIds, $car_year );
- return [ 'error' => 'options', 'field' => 'car_model', 'options' => $car_models, 'filtered' => false ];
- }
- return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
- }
- // ── Tylko model ─────────────────────────────────────────────────
- if ( $has_model && ! $has_brand ) {
- $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
- $termIds = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
- if ( count( $termIds ) === 1 ) {
- $term = get_term( $termIds[0] );
- if ( ! is_wp_error( $term ) && $term->parent !== 0 ) {
- $parent = get_term( $term->parent );
- if ( ! is_wp_error( $parent ) ) {
- $car_brand = $parent->name;
- }
- } else {
- return [ 'error' => 'nie znaleziono takiego modelu', 'car_model' => $car_model ];
- }
- } else {
- $models = [];
- foreach ( $termIds as $tid ) {
- $t = get_term_by( 'id', $tid, 'car_model' );
- if ( $t ) {
- $models[] = $t->name;
- }
- }
- if ( $models ) {
- return [ 'error' => 'options', 'field' => 'car_model', 'options' => $models, 'filtered' => true ];
- }
- return [ 'error' => 'options', 'field' => 'car_brand', 'options' => eksrelay_all_brands(), 'filtered' => false ];
- }
- }
- if ( ! $has_brand && ! $has_model ) {
- return [ 'error' => 'car_brand lub car_model jest wymagany' ];
- }
- if ( ! $car_brand ) {
- $car_brand = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_brand'] ) ) );
- }
- if ( ! $car_model ) {
- $car_model = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_model'] ) ) );
- }
- $car_engine = eksrelay_replace_ascii( trim( sanitize_text_field( $body['car_engine'] ?? '' ) ) );
- $car_engine_index = isset( $body['car_engine_index'] ) ? intval( $body['car_engine_index'] ) : -1;
- $product_name = eksrelay_replace_ascii( trim( sanitize_text_field( $body['product_name'] ?? '' ) ) );
- $product_id = intval( $body['product_id'] ?? 0 );
- // ── Znajdź produkt ──────────────────────────────────────────────
- $product = false;
- if ( $product_id > 0 ) {
- $p = wc_get_product( $product_id );
- if ( $p && ! is_wp_error( $p ) ) {
- $product = $p;
- }
- } elseif ( $product_name ) {
- $products = eksrelay_search_products_by_name( $product_name );
- if ( count( $products ) > 1 ) {
- // exact match → auto-wybierz
- foreach ( $products as $p ) {
- if ( eksrelay_replace_ascii( strtolower( trim( $p->get_title() ) ) ) === strtolower( $product_name ) ) {
- $product = $p;
- break;
- }
- }
- if ( ! $product ) {
- return [
- 'error' => 'options',
- 'field' => 'product_name',
- 'options' => array_map( fn( $p ) => [ 'id' => $p->get_id(), 'title' => $p->get_title() ], $products ),
- 'filtered' => true,
- ];
- }
- } elseif ( count( $products ) === 1 ) {
- $product = $products[0];
- }
- }
- // ── Znajdź auto ─────────────────────────────────────────────────
- $termIds = get_terms( [ 'name__like' => $car_brand, 'fields' => 'ids' ] );
- $termIds2 = get_terms( [ 'name__like' => $car_model, 'fields' => 'ids' ] );
- // Uwaga: celowo bez filtra roku – engine_info filtruje rok wewnętrznie
- $query = new WP_Query( [
- 'post_type' => 'car',
- 'posts_per_page' => -1,
- 'tax_query' => [
- 'relation' => 'AND',
- [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds ],
- [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds2 ],
- ],
- ] );
- $cars = $query->get_posts();
- $engine_info = eksrelay_get_engines_info( $cars, false, $car_year );
- if ( ! $cars ) {
- return [ 'error' => 'nie znaleziono takiego samochodu', 'car_brand' => $car_brand, 'car_model' => $car_model ];
- }
- // If year filter returns nothing, fall back to all engines for this model (year=0 disables filter).
- // This handles cases where the user provides an approximate or incorrect year.
- if ( empty( $engine_info ) && $car_year ) {
- $engine_info = eksrelay_get_engines_info( $cars, false, 0 );
- }
- $engine_names = array_column( $engine_info, 'name' );
- if ( count( $engine_info ) === 1 ) {
- $car_engine = $engine_info[0]['name'];
- }
- if ( count( $engine_info ) > 1 && ! $car_engine && $car_engine_index < 0 ) {
- // Return options as {index, name} objects so the caller can select by numeric index,
- // avoiding string-matching issues (e.g. LLM stripping engine-type prefixes like "B ", "D ").
- $engine_options = array_map(
- fn( $i, $eng ) => [ 'index' => $i, 'name' => $eng['name'] ],
- array_keys( $engine_info ),
- array_values( $engine_info )
- );
- return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_options, 'filtered' => false ];
- }
- foreach ( $engine_info as $idx => $eng ) {
- // Prefer index-based selection (unambiguous) over name-based (fragile string match).
- if ( $car_engine_index >= 0 ) {
- if ( $idx !== $car_engine_index ) continue;
- } else {
- if ( strtolower( trim( $eng['name'] ) ) !== strtolower( trim( $car_engine ) ) ) continue;
- }
- $engine_gas = array_values( array_filter( [ $eng['ac_gas_type'] ?? '', $eng['ac_gas_type_2'] ?? '' ] ) );
- $engine_adapters = array_values( array_filter( [ $eng['adapters'] ?? '', $eng['adapters_2'] ?? '' ] ) );
- if ( ! $engine_gas ) $engine_gas = [ 'no-gas' ];
- if ( ! $engine_adapters ) $engine_adapters = [ 'no' ];
- if ( $product ) {
- // ── Sprawdzenie kompatybilności konkretnego produktu ─────────
- $attributes = eksrelay_product_attributes( $product );
- foreach ( $attributes as $k => $arr ) {
- $attributes[ $k ] = array_map( fn( $slug ) => str_replace( "-{$lang}", '', $slug ), $arr );
- }
- if ( ! isset( $attributes['pa_rg'] ) || empty( $attributes['pa_rg'] ) ) $attributes['pa_rg'] = [ 'no-gas' ];
- if ( ! isset( $attributes['pa_ra'] ) || empty( $attributes['pa_ra'] ) ) $attributes['pa_ra'] = [ 'no' ];
- $gas_fit = count( array_intersect( $engine_gas, $attributes['pa_rg'] ) ) > 0;
- $adapter_fit = count( array_intersect( $engine_adapters, $attributes['pa_ra'] ) ) > 0;
- return [
- 'success' => true,
- 'fit' => $gas_fit && $adapter_fit,
- 'gas_fit' => $gas_fit,
- 'adapter_fit' => $adapter_fit,
- 'selected_engine' => $eng,
- 'selected_product' => [
- 'id' => $product->get_id(),
- 'title' => $product->get_title(),
- 'type' => $product->get_type(),
- 'attributes' => $attributes,
- ],
- ];
- }
- // ── Brak konkretnego produktu – lista wszystkich kompatybilnych ─
- // Switch WPML language so product titles and WCML prices are returned
- // in the requested language/currency. Restored in finally block below.
- if ( $lang && $lang !== 'pl' ) {
- do_action( 'wpml_switch_language', $lang );
- }
- try {
- $all_products = wc_get_products( [
- 'status' => 'publish',
- 'visibility' => 'visible',
- 'limit' => -1,
- 'orderby' => 'name',
- ] );
- $compatible = [];
- foreach ( $all_products as $p ) {
- $attrs = eksrelay_product_attributes( $p );
- foreach ( $attrs as $k => $arr ) {
- $attrs[ $k ] = array_map( fn( $slug ) => str_replace( "-{$lang}", '', $slug ), $arr );
- }
- $p_gas = ( isset( $attrs['pa_rg'] ) && ! empty( $attrs['pa_rg'] ) ) ? $attrs['pa_rg'] : [ 'no-gas' ];
- $p_adapters = ( isset( $attrs['pa_ra'] ) && ! empty( $attrs['pa_ra'] ) ) ? $attrs['pa_ra'] : [ 'no' ];
- if (
- count( array_intersect( $engine_gas, $p_gas ) ) > 0 &&
- count( array_intersect( $engine_adapters, $p_adapters ) ) > 0
- ) {
- $compatible[] = [
- 'id' => $p->get_id(),
- 'title' => $p->get_title(),
- 'price' => $p->get_price(),
- 'permalink' => get_permalink( $p->get_id() ),
- ];
- }
- }
- } finally {
- if ( $lang && $lang !== 'pl' ) {
- do_action( 'wpml_switch_language', null );
- }
- }
- return [
- 'success' => true,
- 'compatible_products' => $compatible,
- 'selected_engine' => $eng,
- ];
- }
- // No engine matched — return options again so user can pick
- $engine_options = array_map(
- fn( $i, $eng ) => [ 'index' => $i, 'name' => $eng['name'] ],
- array_keys( $engine_info ),
- array_values( $engine_info )
- );
- return [ 'error' => 'options', 'field' => 'car_engine', 'options' => $engine_options, 'filtered' => false ];
- }
- // ═══════════════════════════════════════════════════════════════════
- // Pomocnicze funkcje wewnętrzne
- // ═══════════════════════════════════════════════════════════════════
- /**
- * Zwraca listę modeli dla danej marki (termIds) i roku produkcji.
- */
- function eksrelay_models_for_brand_year( array $termIds, int $car_year ): array {
- $query = new WP_Query( [
- 'post_type' => 'car',
- 'posts_per_page' => -1,
- 'tax_query' => [
- 'relation' => 'AND',
- [ 'taxonomy' => 'car_model', 'field' => 'id', 'terms' => $termIds ],
- [ 'taxonomy' => 'car_production_year', 'field' => 'slug', 'terms' => [ (string) $car_year ] ],
- ],
- ] );
- $car_models = [];
- foreach ( $query->get_posts() as $car ) {
- foreach ( wp_get_post_terms( $car->ID, 'car_model' ) as $term ) {
- if ( ! in_array( $term->name, $car_models, true ) && $term->parent !== 0 ) {
- $car_models[] = $term->name;
- }
- }
- }
- return $car_models;
- }
- /**
- * Zwraca listę wszystkich marek aut (terminy car_model z parent=0).
- */
- function eksrelay_all_brands(): array {
- return (array) get_terms( [
- 'taxonomy' => 'car_model',
- 'parent' => 0,
- 'fields' => 'names',
- 'hide_empty' => true,
- ] );
- }
- /**
- * Zwraca info o silnikach dla podanych postów 'car'.
- *
- * Parametr $prefiltered:
- * true – WP_Query już przefiltrował po roku przez taxonomy (get_car_data)
- * false – rok musi być sprawdzony wewnętrznie przez pola ACF year_from / year_to (get_product_compatibility)
- *
- * Nazwa silnika jest budowana z pól ACF: engine_type, engine_size (ccm), engine_power_kw, engine_power_km.
- * Format: "B 1.8 - 141 kW / 192 KM"
- *
- * Zakres roku pochodzi z pól ACF year_from / year_to (przechowywane jako term_id taksonomii car_production_year).
- *
- * Dodatkowe warianty silnika mogą być przechowywane w ACF repeaterze 'engines' na poście car.
- */
- function eksrelay_get_engines_info( array $cars, bool $prefiltered, int $car_year ): array {
- $engines = [];
- $seen = [];
- foreach ( $cars as $car ) {
- $car_id = $car->ID;
- // ── Filtrowanie zakresu roku przez pola ACF year_from / year_to ─
- if ( $car_year ) {
- $year_from_id = get_field( 'year_from', $car_id );
- $year_to_id = get_field( 'year_to', $car_id );
- $year_from = 0;
- $year_to = 9999;
- if ( $year_from_id ) {
- $t = get_term( (int) $year_from_id, 'car_production_year' );
- if ( $t && ! is_wp_error( $t ) ) {
- $year_from = (int) $t->slug;
- }
- }
- if ( $year_to_id ) {
- $t = get_term( (int) $year_to_id, 'car_production_year' );
- if ( $t && ! is_wp_error( $t ) ) {
- $year_to = (int) $t->slug;
- }
- }
- if ( $car_year < $year_from || $car_year > $year_to ) {
- continue;
- }
- }
- // ── Budowanie nazwy silnika z pól ACF ────────────────────────────
- $engine_type = (string) ( get_field( 'engine_type', $car_id ) ?: '' );
- $engine_size_cc = (float) ( get_field( 'engine_size', $car_id ) ?: 0 );
- $power_kw = (string) ( get_field( 'engine_power_kw', $car_id ) ?: '' );
- $power_km = (string) ( get_field( 'engine_power_km', $car_id ) ?: '' );
- $engine_size = number_format( $engine_size_cc / 1000, 1 );
- $name = "{$engine_type} {$engine_size} - {$power_kw} kW / {$power_km} KM";
- // ── Typ gazu i adaptery ──────────────────────────────────────────
- $ac_gas_type = (string) ( get_field( 'ac_gas_type', $car_id ) ?: '' );
- $ac_gas_type_2 = (string) ( get_field( 'ac_gas_type_2', $car_id ) ?: '' );
- $adapters_raw = get_field( 'adapters', $car_id );
- $adapters_raw2 = get_field( 'adapters_2', $car_id );
- $adapters = is_array( $adapters_raw ) ? ( implode( ',', $adapters_raw ) ?: 'no' ) : (string) ( $adapters_raw ?: 'no' );
- $adapters_2 = is_array( $adapters_raw2 ) ? implode( ',', $adapters_raw2 ) : (string) ( $adapters_raw2 ?: '' );
- // ── Korekty gazu na podstawie roku (sanity check) ────────────────
- if ( $car_year ) {
- if ( $car_year <= 1994 && $ac_gas_type === '' ) {
- $ac_gas_type = 'r12';
- } elseif ( $car_year >= 2017 && $ac_gas_type === '' ) {
- $ac_gas_type = 'r1234yf';
- } elseif ( $car_year >= 1995 && $car_year <= 2016 && $ac_gas_type === '' ) {
- $ac_gas_type = 'r134a';
- }
- }
- // ── Główny silnik ────────────────────────────────────────────────
- $key = mb_strtolower( $name );
- if ( ! isset( $seen[ $key ] ) ) {
- $seen[ $key ] = true;
- $engines[] = [
- 'name' => $name,
- 'id' => $car_id,
- 'url' => get_permalink( $car_id ),
- 'ac_gas_type' => $ac_gas_type,
- 'ac_gas_type_2' => $ac_gas_type_2,
- 'adapters' => $adapters,
- 'adapters_2' => $adapters_2,
- ];
- }
- // ── ACF repeater 'engines' – dodatkowe warianty silnika ──────────
- $extra_engines = get_field( 'engines', $car_id );
- if ( is_array( $extra_engines ) ) {
- foreach ( $extra_engines as $extra ) {
- $e_type = (string) ( $extra['engine_type'] ?? '' );
- $e_size_cc = (float) ( $extra['engine_size'] ?? 0 );
- $e_kw = (string) ( $extra['engine_power_kw'] ?? '' );
- $e_km = (string) ( $extra['engine_power_km'] ?? '' );
- $e_size = number_format( $e_size_cc / 1000, 1 );
- $e_name = "{$e_type} {$e_size} - {$e_kw} kW / {$e_km} KM";
- $e_gas = (string) ( $extra['ac_gas_type'] ?? $ac_gas_type );
- $e_gas_2 = (string) ( $extra['ac_gas_type_2'] ?? $ac_gas_type_2 );
- $e_adp_r = $extra['adapters'] ?? null;
- $e_adp_r2 = $extra['adapters_2'] ?? null;
- $e_adp = is_array( $e_adp_r ) ? implode( ',', $e_adp_r ) : (string) ( $e_adp_r ?: $adapters );
- $e_adp_2 = is_array( $e_adp_r2 ) ? implode( ',', $e_adp_r2 ) : (string) ( $e_adp_r2 ?: $adapters_2 );
- $e_key = mb_strtolower( $e_name );
- if ( ! isset( $seen[ $e_key ] ) ) {
- $seen[ $e_key ] = true;
- $engines[] = [
- 'name' => $e_name,
- 'id' => $car_id,
- 'url' => get_permalink( $car_id ),
- 'ac_gas_type' => $e_gas,
- 'ac_gas_type_2' => $e_gas_2,
- 'adapters' => $e_adp,
- 'adapters_2' => $e_adp_2,
- ];
- }
- }
- }
- }
- return $engines;
- }
- /**
- * Zwraca atrybuty produktu WC jako [ slug_atrybutu => [ slug_wartości, ... ] ].
- * Używane do sprawdzania pa_rg (typ gazu) i pa_ra (adapter).
- *
- * Implementacja wzorowana na aiac_get_product_attributes_as_str_arr() z aiac_chat_api.php:
- * – dla wariantów (variation): [ $attr_val ] (pojedyncza wartość ze zmiennej produktowej)
- * – dla pozostałych typów: $attr_obj->get_slugs()
- */
- function eksrelay_product_attributes( WC_Product $product ): array {
- $result = [];
- $attributes = $product->get_attributes();
- if ( $product->is_type( 'variation' ) ) {
- foreach ( $attributes as $attr_key => $attr_val ) {
- $result[ $attr_key ] = [ $attr_val ];
- }
- } else {
- foreach ( $attributes as $attr_key => $attr_obj ) {
- $result[ $attr_key ] = $attr_obj->get_slugs();
- }
- }
- return $result;
- }
- /**
- * Wyszukuje produkty WooCommerce po fragmencie nazwy (fulltext search).
- */
- function eksrelay_search_products_by_name( string $product_name ): array {
- add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'eksrelay_wc_query_like', 10, 2 );
- $products = wc_get_products( [
- 'status' => 'publish',
- 'limit' => -1,
- 'like' => $product_name,
- ] );
- remove_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'eksrelay_wc_query_like', 10 );
- return $products;
- }
- function eksrelay_wc_query_like( array $query, array $query_vars ): array {
- if ( ! empty( $query_vars['like'] ) ) {
- $query['s'] = esc_attr( $query_vars['like'] );
- }
- return $query;
- }
- // ═══════════════════════════════════════════════════════════════════
- // Handler: GET /wp-json/eksrelay/v1/shipping-costs?zone_id=X¤cy=EUR
- //
- // Returns shipping methods for a zone with costs in the requested currency.
- // WCML stores per-currency costs in wp_options:
- // woocommerce_{method_id}_{instance_id}_settings → cost_EUR, cost_CZK, etc.
- // ═══════════════════════════════════════════════════════════════════
- function eksrelay_shipping_costs( WP_REST_Request $request ) {
- $zone_id = intval( $request->get_param( 'zone_id' ) );
- $currency = strtoupper( sanitize_text_field( $request->get_param( 'currency' ) ?: 'PLN' ) );
- $zone = new WC_Shipping_Zone( $zone_id );
- $methods = $zone->get_shipping_methods( true ); // true = enabled only
- $result = [];
- foreach ( $methods as $method ) {
- $instance_id = $method->instance_id;
- $method_id = $method->id; // e.g. 'flat_rate', 'free_shipping'
- // WCML stores per-currency costs in wp_options
- $option_key = "woocommerce_{$method_id}_{$instance_id}_settings";
- $settings = get_option( $option_key, [] );
- // Base cost is the default (PLN) cost
- $base_cost = $settings['cost'] ?? '0';
- $cost_key = "cost_{$currency}";
- if ( $currency !== 'PLN' && isset( $settings[ $cost_key ] ) && $settings[ $cost_key ] !== '' ) {
- $cost = $settings[ $cost_key ];
- $cost_currency = $currency;
- } else {
- $cost = $base_cost;
- $cost_currency = 'PLN';
- }
- $result[] = [
- 'instance_id' => $instance_id,
- 'method_id' => $method_id,
- 'title' => $method->get_title(),
- 'cost' => $cost,
- 'cost_currency' => $cost_currency,
- ];
- }
- return [
- 'zone_id' => $zone_id,
- 'currency' => $currency,
- 'methods' => $result,
- ];
- }
- /**
- * Usuwa znaki diakrytyczne z ciągu znaków.
- * Mapowanie identyczne z replace_ascii() w aiac_chat_api.php.
- */
- function eksrelay_replace_ascii( string $string ): string {
- $map = [
- 'Š' => 'S', 'š' => 's', 'ë' => 'e', 'Ë' => 'E',
- 'ä' => 'a', 'Ä' => 'A', 'ö' => 'o', 'Ö' => 'O',
- 'ü' => 'u', 'Ü' => 'U', 'ß' => 'ss',
- 'ó' => 'o', 'Ó' => 'O', 'ł' => 'l', 'Ł' => 'L',
- 'ń' => 'n', 'Ń' => 'N', 'ć' => 'c', 'Ć' => 'C',
- 'ę' => 'e', 'Ę' => 'E', 'ź' => 'z', 'Ź' => 'Z',
- 'ż' => 'z', 'Ż' => 'Z', 'á' => 'a', 'Á' => 'A',
- 'č' => 'c', 'Č' => 'C', 'ď' => 'd', 'Ď' => 'D',
- 'é' => 'e', 'É' => 'E', 'ě' => 'e', 'Ě' => 'E',
- 'í' => 'i', 'Í' => 'I', 'ň' => 'n', 'Ň' => 'N',
- 'ř' => 'r', 'Ř' => 'R', 'ś' => 's', 'Ś' => 'S',
- 'ť' => 't', 'Ť' => 'T', 'ů' => 'u', 'Ů' => 'U',
- 'ý' => 'y', 'Ý' => 'Y', 'ą' => 'a', 'Ą' => 'A',
- 'ș' => 's', 'Ș' => 'S', 'î' => 'i', 'Î' => 'I',
- 'â' => 'a', 'Â' => 'A', 'ț' => 't', 'Ț' => 'T',
- 'ğ' => 'g', 'Ğ' => 'G', 'İ' => 'I', 'ı' => 'i',
- 'ç' => 'c', 'Ç' => 'C',
- ];
- return str_replace( array_keys( $map ), array_values( $map ), $string );
- }
|