AlkantarClanX12

Your IP : 216.73.217.24


Current Path : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/includes/
Upload File :
Current File : /www/capitalgmcbuickregina_830/public/wp-content/plugins/leadbox/includes/dealer-data.php

<?php

/**
 * Dealer Data REST API Endpoint
 * Provides dealer hours information via REST API
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class DealerDataEndpoint
{
    public function __construct()
    {
        // Register REST API endpoint
        add_action('rest_api_init', array($this, 'register_data_endpoint'));
    }

    /**
     * Register the dealer data REST endpoint
     */
    public function register_data_endpoint()
    {
        register_rest_route('lbx/v1', '/dealer-data', array(
            'methods' => 'GET',
            'callback' => array($this, 'get_dealer_data'),
            'permission_callback' => '__return_true'
        ));
    }

    /**
     * Get dealer data callback
     */
    public function get_dealer_data($request)
    {
        try {
            // Get dealer code from settings
            $leadbox_settings = get_option('leadbox-settings', array());
            $dealer_code = $this->get_dealer_code($leadbox_settings);
            
            // Get hours from WordPress options
            $hours_data = $this->get_hours_from_settings();
            
            // Get phones from WordPress options
            $phones_data = $this->get_phones_from_settings();
            
            // Get tagged pages
            $tagged_pages = $this->get_tagged_pages();

            // Get service specials
            $service_specials = $this->get_service_specials($dealer_code, $leadbox_settings);

            $data = array(
                'dealer_code' => array(
                    $dealer_code => array(
                        'hours' => $hours_data,
                        'phones' => $phones_data,
                        'tagged-pages' => $tagged_pages,
                        'service_specials' => $service_specials
                    )
                )
            );
            
            return new WP_REST_Response($data, 200);
        } catch (Exception $e) {
            return new WP_REST_Response(array(
                'error' => 'Internal server error',
                'message' => $e->getMessage()
            ), 500);
        }
    }

    /**
     * Get dealer code from settings
     */
    private function get_dealer_code($settings)
    {
        // Try to get dealer code from different possible sources
        if (!empty($settings['ford_dealer_code'])) {
            return $settings['ford_dealer_code'];
        }
        
        if (!empty($settings['jlr_dealer_code'])) {
            return $settings['jlr_dealer_code'];
        }
        
        // Fallback to site name or default
        return sanitize_title(get_bloginfo('name'));
    }

    /**
     * Get hours from WordPress settings
     */
    private function get_hours_from_settings()
    {
        $hours_options = get_option('dealer-settings-hours-options', array());
        
        $departments = array(
            'Sales' => 'sale_hours',
            'Service' => 'service_hours',
            'Parts' => 'parts_hours',
            'Body_Shop' => 'additionalone_hours'
        );
        
        $result = array();
        
        foreach ($departments as $dept_name => $prefix) {
            $result[$dept_name] = $this->get_department_hours($hours_options, $prefix);
        }
        
        return $result;
    }

    /**
     * Get hours for a specific department
     */
    private function get_department_hours($hours_options, $prefix)
    {
        $days = array('sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday');
        $result = array();
        
        foreach ($days as $day) {
            $day_capitalized = ucfirst($day);
            $is_closed = isset($hours_options["{$prefix}_{$day}_closed"]) && $hours_options["{$prefix}_{$day}_closed"] === 'on';
            
            if ($is_closed) {
                $result[$day_capitalized] = array(
                    'Open' => '',
                    'Close' => ''
                );
            } else {
                $start = isset($hours_options["{$prefix}_{$day}_start"]) ? $hours_options["{$prefix}_{$day}_start"] : '';
                $end = isset($hours_options["{$prefix}_{$day}_end"]) ? $hours_options["{$prefix}_{$day}_end"] : '';
                
                // Convert 24h format to 12h format with AM/PM
                $start_formatted = $this->format_time($start);
                $end_formatted = $this->format_time($end);
                
                $result[$day_capitalized] = array(
                    'Open' => $start_formatted,
                    'Close' => $end_formatted
                );
            }
        }
        
        return $result;
    }

    /**
     * Get phones from WordPress settings
     */
    private function get_phones_from_settings()
    {
        $contact_options = get_option('dealer-settings-contact-options', array());
        
        return array(
            'Sales' => isset($contact_options['sale_phone']) ? $contact_options['sale_phone'] : '',
            'Service' => isset($contact_options['service_phone']) ? $contact_options['service_phone'] : '',
            'Parts' => isset($contact_options['parts_phone']) ? $contact_options['parts_phone'] : '',
            'Body_Shop' => isset($contact_options['body_shop']) ? $contact_options['body_shop'] : '',
            'Collision' => isset($contact_options['collision_phone']) ? $contact_options['collision_phone'] : '',
            'Accessories' => isset($contact_options['accessories_phone']) ? $contact_options['accessories_phone'] : '',
            'Fax' => isset($contact_options['fax']) ? $contact_options['fax'] : '',
            'SMS' => isset($contact_options['sms']) ? $contact_options['sms'] : ''
        );
    }

    /**
     * Get tagged pages
     */
    private function get_tagged_pages()
    {
        // Get all pages that have a tag assigned
        $pages = get_posts(array(
            'post_type' => 'page',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'meta_query' => array(
                array(
                    'key' => 'lbx_page_tag',
                    'value' => '',
                    'compare' => '!='
                )
            )
        ));

        $tagged_pages = array();

        foreach ($pages as $page) {
            $tag = get_post_meta($page->ID, 'lbx_page_tag', true);

            if (!empty($tag)) {
                $tagged_pages[] = array(
                    'tag' => $tag,
                    'title' => $page->post_title,
                    'url' => get_permalink($page->ID)
                );
            }
        }

        return $tagged_pages;
    }

    /**
     * Find service special term IDs by matching slug variations
     */
    private function find_service_special_term_ids()
    {
        global $wpdb;

        $results = $wpdb->get_col(
            "SELECT t.term_id
             FROM {$wpdb->terms} t
             INNER JOIN {$wpdb->term_taxonomy} tt ON t.term_id = tt.term_id
             WHERE tt.taxonomy = 'offer_category'
             AND t.slug REGEXP '^services?[^a-z]*specials?$'"
        );

        return array_map('intval', $results);
    }

    /**
     * Get active service specials from offer categories matching service-special variations
     */
    private function get_service_specials($dealer_code, $leadbox_settings)
    {
        $term_ids = $this->find_service_special_term_ids();

        if (empty($term_ids)) {
            return array();
        }

        $today = current_time('Y-m-d');
        $brand = !empty($leadbox_settings['manufacturer']) ? $leadbox_settings['manufacturer'] : '';

        $args = array(
            'post_type' => 'offer',
            'post_status' => 'publish',
            'posts_per_page' => -1,
            'tax_query' => array(
                array(
                    'taxonomy' => 'offer_category',
                    'field' => 'term_id',
                    'terms' => $term_ids
                )
            ),
            'meta_query' => array(
                'relation' => 'AND',
                array(
                    'key' => '_offer_start_date',
                    'value' => $today,
                    'compare' => '<=',
                    'type' => 'DATE'
                ),
                array(
                    'relation' => 'OR',
                    array(
                        'key' => '_offer_end_date',
                        'value' => $today,
                        'compare' => '>=',
                        'type' => 'DATE'
                    ),
                    array(
                        'key' => '_offer_end_date',
                        'value' => '',
                        'compare' => '='
                    ),
                    array(
                        'key' => '_offer_end_date',
                        'compare' => 'NOT EXISTS'
                    )
                )
            )
        );

        $offers = get_posts($args);
        $result = array();

        foreach ($offers as $offer) {
            $language = get_post_meta($offer->ID, '_offer_language', true);
            $start_date = get_post_meta($offer->ID, '_offer_start_date', true);
            $end_date = get_post_meta($offer->ID, '_offer_end_date', true);

            $result[] = array(
                'date' => $today,
                'dealerCode' => $dealer_code,
                'language' => $this->convert_language_code($language),
                'brand' => $brand,
                'owner' => 'Dealer',
                'startdate' => !empty($start_date) ? $start_date : null,
                'endDate' => !empty($end_date) ? $end_date : null,
                'title' => $offer->post_title,
                'subText' => '',
                'description' => $this->sanitize_for_export(get_post_meta($offer->ID, '_offer_description', true) ?: ''),
                'disclaimer' => $this->sanitize_for_export(get_post_meta($offer->ID, '_offer_disclaimer', true) ?: ''),
                'mainValue' => null,
                'secondaryValue' => '',
                'additionalMainValue' => null,
                'additionalSecondaryValue' => '',
                'couponCode' => ''
            );
        }

        return $result;
    }

    /**
     * Sanitize text for export: strip HTML (keeping visible text), decode entities,
     * normalize Unicode special characters, and clean up whitespace.
     */
    private function sanitize_for_export($text)
    {
        if (empty($text)) {
            return '';
        }

        $text = wp_strip_all_tags($text);

        $text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8');

        // Normalize Unicode special characters to ASCII equivalents
        $patterns = array(
            '/\x{2014}/u',  // em-dash
            '/\x{2013}/u',  // en-dash
            '/[\x{2018}\x{2019}]/u',  // single curly quotes
            '/[\x{201C}\x{201D}]/u',  // double curly quotes
            '/\x{2026}/u',  // ellipsis
            '/\x{00A0}/u',  // non-breaking space
        );
        $replacements = array('--', '-', "'", '"', '...', ' ');
        $text = preg_replace($patterns, $replacements, $text);

        // Normalize line breaks and collapse excessive whitespace
        $text = str_replace(array("\r\n", "\r"), "\n", $text);
        $text = preg_replace("/\n{3,}/", "\n\n", $text);
        $text = trim($text);

        return $text;
    }

    /**
     * Convert internal language code to standard short code
     */
    private function convert_language_code($language)
    {
        $map = array(
            'eng' => 'en',
            'fr' => 'fr'
        );

        return isset($map[$language]) ? $map[$language] : $language;
    }

    /**
     * Format time from 24h to 12h format with AM/PM
     */
    private function format_time($time)
    {
        if (empty($time)) {
            return '';
        }
        
        // If already in 12h format, return as is
        if (stripos($time, 'AM') !== false || stripos($time, 'PM') !== false) {
            return $time;
        }
        
        // Try to parse the time
        $timestamp = strtotime($time);
        if ($timestamp === false) {
            return $time; // Return original if can't parse
        }
        
        // Format to 12h with AM/PM
        return date('g:i:s A', $timestamp);
    }
}

// Initialize the dealer data endpoint
new DealerDataEndpoint();

Home - Capital GMC Buick Regina

No data

Welcome to Capital GMC BUICK – REGINA

Thank you for choosing Capital GMC Buick | Regina, your premier certified Buick and GMC dealership proudly serving drivers in Regina and the surrounding communities. Whether you’re searching for a brand-new Buick or GMC vehicle or a meticulously inspected pre-owned model, we have a diverse selection to match your needs and lifestyle.

Beyond our impressive inventory, we offer a seamless and stress-free financing experience through our well-connected finance centre, where our team of experts is dedicated to securing the best loan or lease options for you, quickly, transparently, and hassle-free.

But our commitment to you doesn’t stop at the sale. Our state-of-the-art service centre is staffed with skilled Buick and GMC technicians who use the latest equipment and genuine OEM parts to keep your vehicle running at its best. From routine maintenance to complex repairs, we’ve got you covered.

Experience top-tier customer service, quality vehicles, and expert care, all in one place. Visit Capital GMC Buick | Regina today or call us at 306-205-8072 with any questions. We’re here to help!

Ask a Question