PHP Classes

File: public/partials/site-switcher.php

Recommend this page to a friend!
  Packages of Adrian M   QuickWP   public/partials/site-switcher.php   Download  
File: public/partials/site-switcher.php
Role: Example script
Content type: text/plain
Description: Example script
Class: QuickWP
Manage WordPress sites using the REST API
Author: By
Last change: feat(i18n): add zero-dependency UI localization (EN/RO) and language switcher
v2.2: refactor services, add retry/pagination, shared UI layout, PHP 8.3+, PHPStan L8, and CI
Date: 12 hours ago
Size: 1,737 bytes
 

 

Contents

Class file image Download
<?php
/**
 * QuickWP v2 - Site Switcher Partial
 * 
 * Include this in controllers to add a site switcher dropdown.
 * 
 * Required variables before including:
 *   $loader   - ConfigLoader instance
 *   $siteKey  - Current site key
 * 
 * Usage:
 *   include __DIR__ . '/partials/site-switcher.php';
 */

if (!isset($loader) || !isset($siteKey)) {
    return;
}

$switcherSites = $loader->getSites();

if (empty($switcherSites)) {
    return;
}

// Determine current page and existing query params
$currentPage = basename($_SERVER['SCRIPT_NAME']);
$existingParams = $_GET;
unset($existingParams['site']); // Remove site from params, will be re-added by form
$existingQuery = !empty($existingParams) ? '&' . http_build_query($existingParams) : '';
?>
<div class="site-switcher">
    <form method="get" action="<?= htmlspecialchars($currentPage) ?>">
        <?php foreach ($existingParams as $key => $value): ?>
            <input type="hidden" name="<?= htmlspecialchars($key) ?>" value="<?= htmlspecialchars($value) ?>">
        <?php endforeach; ?>
        
        <label for="site-select"><?= isset($t) ? $t->e('common.site') : 'Site:' ?></label>
        <select id="site-select" name="site" onchange="this.form.submit()">
            <?php foreach ($switcherSites as $key => $site): ?>
                <option value="<?= htmlspecialchars($key, ENT_QUOTES, 'UTF-8') ?>" <?= ($key === $siteKey) ? 'selected' : '' ?>>
                    <?= htmlspecialchars($site['label'] ?? $key, ENT_QUOTES, 'UTF-8') ?>
                </option>
            <?php endforeach; ?>
        </select>
        <noscript><button type="submit" class="btn-small"><?= isset($t) ? $t->e('common.switch') : 'Switch' ?></button></noscript>
    </form>
</div>