134 lines
4.0 KiB
PHP
134 lines
4.0 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Elementor Widget: Carei Branches — grid oddziałów z adresami.
|
|
*/
|
|
class Carei_Branches_Widget extends \Elementor\Widget_Base {
|
|
|
|
const NAME_FIXES = array(
|
|
'BYDGOSZC' => 'BYDGOSZCZ',
|
|
'GORZÓW WIE' => 'GORZÓW WIELKOPOLSKI',
|
|
'RZSZÓW' => 'RZESZÓW',
|
|
'SK-KAM' => '',
|
|
);
|
|
|
|
public function get_name() {
|
|
return 'carei-branches';
|
|
}
|
|
|
|
public function get_title() {
|
|
return 'Carei Branches';
|
|
}
|
|
|
|
public function get_icon() {
|
|
return 'eicon-posts-grid';
|
|
}
|
|
|
|
public function get_categories() {
|
|
return array( 'general' );
|
|
}
|
|
|
|
public function get_style_depends() {
|
|
return array( 'carei-reservation-css' );
|
|
}
|
|
|
|
protected function register_controls() {}
|
|
|
|
/**
|
|
* Clean branch name and return base city.
|
|
*/
|
|
private static function clean_name( $raw ) {
|
|
$name = trim( $raw );
|
|
$name = preg_replace( '/\s+[DL]$/u', '', $name );
|
|
if ( isset( self::NAME_FIXES[ $name ] ) ) {
|
|
$name = self::NAME_FIXES[ $name ];
|
|
}
|
|
if ( empty( $name ) || preg_match( '/^[A-Z]{2,4}-[A-Z]{2,4}$/u', $name ) ) {
|
|
return '';
|
|
}
|
|
return $name;
|
|
}
|
|
|
|
/**
|
|
* Get unique branches with addresses.
|
|
*/
|
|
private function get_branches_data() {
|
|
$api = Carei_Softra_API::get_instance();
|
|
if ( null === $api ) {
|
|
return array();
|
|
}
|
|
|
|
$branches = $api->get_branches_cached();
|
|
if ( is_wp_error( $branches ) || ! is_array( $branches ) ) {
|
|
return array();
|
|
}
|
|
|
|
$result = array();
|
|
$seen = array();
|
|
|
|
foreach ( $branches as $b ) {
|
|
$raw_name = isset( $b['name'] ) ? $b['name'] : '';
|
|
$city = self::clean_name( $raw_name );
|
|
if ( empty( $city ) ) {
|
|
continue;
|
|
}
|
|
|
|
$norm = mb_strtolower( $city, 'UTF-8' );
|
|
if ( isset( $seen[ $norm ] ) ) {
|
|
continue;
|
|
}
|
|
$seen[ $norm ] = true;
|
|
|
|
$display_city = mb_convert_case( $city, MB_CASE_TITLE, 'UTF-8' );
|
|
$street = isset( $b['street'] ) ? trim( $b['street'] ) : '';
|
|
$zip = isset( $b['zipCode'] ) ? trim( (string) $b['zipCode'] ) : '';
|
|
$api_city = isset( $b['city'] ) ? trim( $b['city'] ) : '';
|
|
$api_city_tc = mb_convert_case( $api_city, MB_CASE_TITLE, 'UTF-8' );
|
|
|
|
// Format street
|
|
if ( $street ) {
|
|
$street_lower = mb_strtolower( $street, 'UTF-8' );
|
|
$has_prefix = preg_match( '/^(ul\.|al\.|pl\.|os\.)/u', $street_lower );
|
|
$street = $has_prefix ? $street : 'ul. ' . $street;
|
|
}
|
|
|
|
$result[] = array(
|
|
'name' => 'Oddział ' . $display_city,
|
|
'street' => $street,
|
|
'zipCity' => trim( $zip . ' ' . $api_city_tc ),
|
|
);
|
|
}
|
|
|
|
usort( $result, function ( $a, $b ) {
|
|
return strcmp( $a['name'], $b['name'] );
|
|
} );
|
|
|
|
return $result;
|
|
}
|
|
|
|
protected function render() {
|
|
$branches = $this->get_branches_data();
|
|
if ( empty( $branches ) ) {
|
|
return;
|
|
}
|
|
?>
|
|
<div class="carei-branches">
|
|
<?php foreach ( $branches as $branch ) : ?>
|
|
<div class="carei-branches__item">
|
|
<div class="carei-branches__name"><?php echo esc_html( $branch['name'] ); ?></div>
|
|
<?php if ( $branch['street'] ) : ?>
|
|
<div class="carei-branches__street"><?php echo esc_html( $branch['street'] ); ?></div>
|
|
<?php endif; ?>
|
|
<?php if ( $branch['zipCity'] ) : ?>
|
|
<div class="carei-branches__zip-city"><?php echo esc_html( $branch['zipCity'] ); ?></div>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php
|
|
}
|
|
}
|