update
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Elementor Widget: Carei Cities — siatka miast oddziałów.
|
||||
*/
|
||||
class Carei_Cities_Widget extends \Elementor\Widget_Base {
|
||||
|
||||
public function get_name() {
|
||||
return 'carei-cities';
|
||||
}
|
||||
|
||||
public function get_title() {
|
||||
return 'Carei Cities';
|
||||
}
|
||||
|
||||
public function get_icon() {
|
||||
return 'eicon-bullet-list';
|
||||
}
|
||||
|
||||
public function get_categories() {
|
||||
return array( 'general' );
|
||||
}
|
||||
|
||||
public function get_style_depends() {
|
||||
return array( 'carei-reservation-css' );
|
||||
}
|
||||
|
||||
public function get_script_depends() {
|
||||
return array( 'carei-reservation-js' );
|
||||
}
|
||||
|
||||
protected function register_controls() {}
|
||||
|
||||
/**
|
||||
* Truncated / misspelled names from Softra API → correct full city names.
|
||||
*/
|
||||
const NAME_FIXES = array(
|
||||
'BYDGOSZC' => 'BYDGOSZCZ',
|
||||
'GORZÓW WIE' => 'GORZÓW WIELKOPOLSKI',
|
||||
'RZSZÓW' => 'RZESZÓW',
|
||||
'SK-KAM' => '',
|
||||
);
|
||||
|
||||
/**
|
||||
* Clean branch name: handle D(Dworzec)/L suffixes, fix truncated names, title-case.
|
||||
* API returns names like "GDAŃSK D", "GDAŃSK L", "BYDGOSZC D", "SK-KAM", "GORZÓW WIE".
|
||||
* D-suffix branches without a base variant → "Oddział [City]".
|
||||
*/
|
||||
private static function clean_city_name( $raw ) {
|
||||
$name = trim( $raw );
|
||||
// Detect D (Dworzec) suffix before stripping
|
||||
$is_dworzec = (bool) preg_match( '/\s+D$/u', $name );
|
||||
// Strip trailing single-letter suffixes (D=Dworzec, L=Lotnisko/inne)
|
||||
$name = preg_replace( '/\s+[DL]$/u', '', $name );
|
||||
// Fix known truncated/misspelled names
|
||||
if ( isset( self::NAME_FIXES[ $name ] ) ) {
|
||||
$name = self::NAME_FIXES[ $name ];
|
||||
}
|
||||
// Skip empty or non-city codes
|
||||
if ( empty( $name ) || preg_match( '/^[A-Z]{2,4}-[A-Z]{2,4}$/u', $name ) ) {
|
||||
return '';
|
||||
}
|
||||
// Title-case: "GDAŃSK" → "Gdańsk"
|
||||
$name = mb_convert_case( $name, MB_CASE_TITLE, 'UTF-8' );
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract unique city names from branches.
|
||||
*/
|
||||
private function get_city_names() {
|
||||
$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();
|
||||
}
|
||||
|
||||
$cities = array();
|
||||
$seen = array();
|
||||
|
||||
foreach ( $branches as $b ) {
|
||||
$name = isset( $b['name'] ) ? $b['name'] : '';
|
||||
$city = self::clean_city_name( $name );
|
||||
if ( empty( $city ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$norm = mb_strtolower( $city, 'UTF-8' );
|
||||
if ( isset( $seen[ $norm ] ) ) {
|
||||
continue;
|
||||
}
|
||||
$seen[ $norm ] = true;
|
||||
$cities[] = $city;
|
||||
}
|
||||
|
||||
sort( $cities );
|
||||
return $cities;
|
||||
}
|
||||
|
||||
protected function render() {
|
||||
$cities = $this->get_city_names();
|
||||
if ( empty( $cities ) ) {
|
||||
return;
|
||||
}
|
||||
?>
|
||||
<div class="carei-cities">
|
||||
<?php foreach ( $cities as $i => $city ) : ?>
|
||||
<span class="carei-cities__item"><?php echo esc_html( $city ); ?></span>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user