AlkantarClanX12

Your IP : 216.73.217.24


Current Path : /www/capitalgmcbuickregina_830/public/wp-content/themes/lbx-egypt/app/
Upload File :
Current File : /www/capitalgmcbuickregina_830/public/wp-content/themes/lbx-egypt/app/admin.php

<?php

/**
 * Theme admin.
 */

namespace App;

use WP_Customize_Manager;

use function Roots\asset;

/**
 * Register the `.brand` selector to the blogname.
 *
 * @param  \WP_Customize_Manager $wp_customize
 * @return void
 */
add_action('customize_register', function (WP_Customize_Manager $wp_customize) {
    $wp_customize->get_setting('blogname')->transport = 'postMessage';
    $wp_customize->selective_refresh->add_partial('blogname', [
        'selector' => '.brand',
        'render_callback' => function () {
            bloginfo('name');
        }
    ]);
});

/**
 * Register the customizer assets.
 *
 * @return void
 */
add_action('customize_preview_init', function () {
    wp_enqueue_script('sage/customizer.js', asset('scripts/customizer.js')->uri(), ['customize-preview'], null, true);
});

/**
 * Inyecta los campos de geolocalización (Latitude / Longitude) en el panel
 * "Dealer Settings → Contact", sección Address, que provee el plugin
 * leadbox-plugin. No se modifica el plugin: este registra sus settings en
 * `admin_init` y su callback `sanitize()` devuelve el input completo, así que
 * basta con añadir dos campos más (con prioridad posterior) sobre la misma
 * option `dealer-settings-contact-options` para que se guarden junto al resto
 * de los datos de contacto. El theme los lee luego con get_dealer_contact()
 * para el schema AutoDealer del <head>.
 *
 * @return void
 */
add_action('admin_init', function () {
    // Misma guarda que usa el plugin: registra los campos tanto al mostrar el
    // formulario de Contact como al guardarlo (options.php).
    if (function_exists('leadbox_is_settings_page') && !leadbox_is_settings_page()) {
        return;
    }

    $options = get_option('dealer-settings-contact-options');
    $options = is_array($options) ? $options : [];

    $render_field = function (string $key, string $placeholder) use ($options) {
        printf(
            '<input type="text" id="%1$s" name="dealer-settings-contact-options[%1$s]" value="%2$s" placeholder="%3$s" class="regular-text" inputmode="decimal" />',
            esc_attr($key),
            esc_attr($options[$key] ?? ''),
            esc_attr($placeholder)
        );
    };

    add_settings_field(
        'latitude',
        'Latitude',
        function () use ($render_field) {
            $render_field('latitude', 'e.g. 49.883304');
        },
        'dealer-settings-address',
        'address'
    );

    add_settings_field(
        'longitude',
        'Longitude',
        function () use ($render_field) {
            $render_field('longitude', 'e.g. -97.195405');
        },
        'dealer-settings-address',
        'address'
    );
}, 11);

/**
 * Normaliza Latitude / Longitude a 6 decimales (~11 cm) al guardar el panel de
 * Contact, para no almacenar precisión innecesaria. Corre después del sanitize
 * del plugin (que es passthrough), sin necesidad de modificarlo.
 *
 * @param  mixed $value Valor entrante de la option.
 * @return mixed
 */
add_filter('pre_update_option_dealer-settings-contact-options', function ($value) {
    if (!is_array($value)) {
        return $value;
    }

    foreach (['latitude', 'longitude'] as $key) {
        if (isset($value[$key]) && is_numeric(trim((string) $value[$key]))) {
            $value[$key] = \lbx_normalize_coordinate($value[$key]);
        }
    }

    return $value;
});

/**
 * Sección "Schema" en "Dealer Settings → Contact" (LBT-1510): controla dónde
 * se emite el JSON-LD AutoDealer + LocalBusiness del <head>. Igual que los
 * campos de geolocalización, los valores se guardan en la option
 * `dealer-settings-contact-options` y el theme los lee con
 * get_dealer_contact() en lbx_autodealer_schema_in_scope().
 *
 * @return void
 */
add_action('admin_init', function () {
    if (function_exists('leadbox_is_settings_page') && !leadbox_is_settings_page()) {
        return;
    }

    $options = get_option('dealer-settings-contact-options');
    $options = is_array($options) ? $options : [];

    add_settings_section(
        'lbx-schema',
        'Schema (AutoDealer JSON-LD)',
        function () {
            echo '<p>Structured data emitted in the site <code>&lt;head&gt;</code> for search engines and AI platforms (Shift Digital AI-Readiness).</p>';
        },
        'dealer-settings-address'
    );

    add_settings_field(
        'schema_scope',
        'Schema Scope',
        function () use ($options) {
            $scope = trim((string) ($options['schema_scope'] ?? ''));
            $scope = $scope === '' ? 'all' : $scope;
            $choices = [
                'all'      => 'All pages (SD "Suggested") — default',
                'required' => 'Homepage, Service & About Us only (SD "Required" minimum)',
            ];

            echo '<select id="schema_scope" name="dealer-settings-contact-options[schema_scope]">';
            foreach ($choices as $value => $label) {
                printf(
                    '<option value="%s"%s>%s</option>',
                    esc_attr($value),
                    selected($scope, $value, false),
                    esc_html($label)
                );
            }
            echo '</select>';
        },
        'dealer-settings-address',
        'lbx-schema'
    );

    add_settings_field(
        'schema_required_pages',
        'Required Page Slugs',
        function () use ($options) {
            printf(
                '<input type="text" id="schema_required_pages" name="dealer-settings-contact-options[schema_required_pages]" value="%s" placeholder="%s" class="regular-text" />',
                esc_attr($options['schema_required_pages'] ?? ''),
                esc_attr('about-us, service')
            );
            echo '<p class="description">Comma-separated page slugs that emit the schema besides the homepage. Only used with the "Homepage, Service &amp; About Us only" scope. Empty = <code>about-us, service</code>.</p>';
        },
        'dealer-settings-address',
        'lbx-schema'
    );
}, 12);

/**
 * Sanitiza los campos de la sección Schema al guardar el panel de Contact:
 * el scope se restringe a los valores conocidos y los slugs se normalizan
 * con sanitize_title (acepta que el admin pegue títulos, p. ej. "About Us").
 *
 * @param  mixed $value Valor entrante de la option.
 * @return mixed
 */
add_filter('pre_update_option_dealer-settings-contact-options', function ($value) {
    if (!is_array($value)) {
        return $value;
    }

    if (isset($value['schema_scope'])) {
        $value['schema_scope'] = in_array($value['schema_scope'], ['all', 'required'], true)
            ? $value['schema_scope']
            : 'all';
    }

    if (isset($value['schema_required_pages'])) {
        $value['schema_required_pages'] = implode(', ', array_filter(array_map(
            'sanitize_title',
            explode(',', (string) $value['schema_required_pages'])
        )));
    }

    return $value;
});