Untitled

 avatar
unknown
plain_text
a month ago
4.5 kB
6
Indexable
<?php
// [location_selector] — Initiate selected location details or location search interface in the website header
add_shortcode('location_selector', 'fsm_location_selector');
function fsm_location_selector() {
    // Start output buffering
    ob_start();

    // Generate a unique identifier for this instance
    $instance_id = uniqid('location-selector-');

    // Check if cookies are set
    if (isset($_COOKIE['first_result_web_address']) && isset($_COOKIE['first_result_title']) && isset($_COOKIE['first_result_phone'])) {
        // Sanitize phone number for the href attribute
        $sanitized_phone = preg_replace('/[^+\d]/', '', $_COOKIE['first_result_phone']);
        ?>
        <div class="header-location-selector" data-instance="<?php echo esc_attr($instance_id); ?>">
            <p class="company-info">
                <a href="<?php echo esc_url($_COOKIE['first_result_web_address']); ?>">
                    <?php echo esc_html($_COOKIE['first_result_title']); ?>
                </a>
            </p>
            <p class="owner-phone-number">
                <a href="tel:<?php echo esc_attr($sanitized_phone); ?>">
                    <?php echo esc_html($_COOKIE['first_result_phone']); ?>
                </a>
            </p>
            <a href="#" class="toggle-location-selector">Choose Another Location</a>
            <div class="professional-location-search et_pb_module et_pb_text et_pb_text_0 et_pb_text_align_left et_pb_bg_layout_light">
                <div class="et_pb_text_inner">
                    <div class="location-search-wrapper">
                        <div class="search-form-wrapper" style="display:none;">
                            <?php echo do_shortcode('[facetwp facet="nav_proximity_facet"]'); ?>
                            <br />
                            <a class="fwp-proximity-submit">Search</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
    } else {
        // If cookies are not set, display the search form
        ?>
        <div class="header-location-selector" data-instance="<?php echo esc_attr($instance_id); ?>">
            <p>Find a Blue Moon Location Near You!</p>
            <a href="#" class="toggle-location-selector">Search Locations</a>
            <div class="professional-location-search et_pb_module et_pb_text et_pb_text_0 et_pb_text_align_left et_pb_bg_layout_light">
                <div class="et_pb_text_inner">
                    <div class="location-search-wrapper">
                        <div class="search-form-wrapper" style="display:none;">
                            <?php echo do_shortcode('[facetwp facet="nav_proximity_facet"]'); ?>
                            <br />
                            <a class="fwp-proximity-submit">Search</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <script>
        (function($) {
            // Function to initialize location selector functionality
            function initLocationSelector(instance) {
                const $container = $(`.header-location-selector[data-instance="${instance}"]`);
                const $toggle = $container.find('.toggle-location-selector');
                const $searchForm = $container.find('.search-form-wrapper');
                const $submitBtn = $container.find('.fwp-proximity-submit');

                // Toggle search form visibility
                $toggle.on('click', function(e) {
                    e.preventDefault();
                    $searchForm.slideToggle();
                });

                // Handle search submission
                $submitBtn.on('click', function() {
                    if (typeof FWP !== 'undefined') {
                        FWP.refresh();
                    }
                });

                // Disable auto-refresh for proximity facet
                $(document).on('facetwp-loaded', function() {
                    if (typeof FWP !== 'undefined' && typeof FWP.facets['proximity'] !== 'undefined') {
                        FWP.hooks.addFilter('facetwp/refresh/proximity', function() {
                            return false;
                        });
                    }
                });
            }

            // Initialize this instance
            initLocationSelector('<?php echo $instance_id; ?>');
        })(jQuery);
        </script>
        <?php
    }

    // Return the content
    return ob_get_clean();
}
Editor is loading...
Leave a Comment