Created
April 16, 2025 08:58
-
-
Save stoffl6781/05ca6c2cf84ad9d195964c08d7a8046b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// === MainWP Bricks Dynamic Tags === | |
// Dynamic Tags für verschiedene MainWP API Endpunkte | |
define('MAINWP_API_URL', 'https://YOUR_MAIN_WP_URL_HOST_HERE/wp-json/mainwp/v2/'); | |
define('MAINWP_API_TOKEN', 'Bearer YOUR_TOKEN_HERE'); // <-- Token hier eintragen | |
// Tag-Konfiguration (Felder, Gruppen, Endpunkte) | |
function mainwp_get_tag_sources() { | |
return [ | |
'sites' => [ | |
'label' => 'MainWP Site', | |
'endpoint' => 'sites/{site_id}', | |
'fields' => [ | |
'id' => 'Site ID', | |
'name' => 'Site Name', | |
'url' => 'Website URL', | |
'tags' => 'Tags', | |
'note' => 'Notiz', | |
'ip' => 'IP-Adresse', | |
'client_id' => 'Client ID', | |
'is_staging' => 'Staging Site', | |
'wp_version' => 'WordPress Version', | |
'php_version' => 'PHP Version', | |
'child_version' => 'MainWP Child Version', | |
'mysql_version' => 'MySQL Version', | |
'memory_limit' => 'Memory Limit', | |
'database_size' => 'Datenbankgröße (Byte)', | |
'active_theme' => 'Aktives Theme', | |
'status' => 'Verbindungsstatus', | |
'sync_errors' => 'Sync Fehler', | |
'http_status' => 'HTTP Status', | |
'health_status' => 'Health Status (Raw)', | |
'health_score' => 'Health Score', | |
'icon' => 'Seiten-Icon', | |
'last_sync' => 'Letzter Sync (Datum/Zeit)', | |
'plugins' => 'Plugins (JSON)', | |
'themes' => 'Themes (JSON)', | |
'last_post_time' => 'Letzter Post Timestamp', | |
'recent_posts' => 'Letzte Beiträge (JSON)', | |
'recent_pages' => 'Letzte Seiten (JSON)', | |
'recent_comments' => 'Letzte Kommentare (JSON)', | |
'categories' => 'Kategorien (JSON)', | |
'wp_upgrades' => 'Core Updates (JSON)', | |
'plugin_upgrades' => 'Plugin Updates (JSON)', | |
'premium_upgrades' => 'Premium Plugin Updates', | |
'theme_upgrades' => 'Theme Updates (JSON)', | |
'translation_upgrades' => 'Übersetzungs-Updates (JSON)', | |
'ignored_plugins' => 'Ignorierte Plugins', | |
'ignored_themes' => 'Ignorierte Themes', | |
'http_user' => 'HTTP Auth Benutzer', | |
'http_pass' => 'HTTP Auth Passwort', | |
'uniqueId' => 'Eindeutige ID', | |
'is_ignoreCoreUpdates' => 'Ignoriere Core Updates?', | |
'is_ignorePluginUpdates' => 'Ignoriere Plugin Updates?', | |
'is_ignoreThemeUpdates' => 'Ignoriere Theme Updates?', | |
'verify_certificate' => 'SSL-Zertifikat prüfen?', | |
'force_use_ipv4' => 'IPv4 erzwingen?', | |
'ssl_version' => 'SSL Version', | |
'suspended' => 'Site pausiert?', | |
], | |
], | |
'security' => [ | |
'label' => 'MainWP Security', | |
'endpoint' => 'sites/{site_id}/security', | |
'fields' => [ | |
'listing' => 'Directory Listing aktiviert', | |
'wp_version' => 'WordPress Versions-Info sichtbar', | |
'rsd' => 'RSD-Link vorhanden (Really Simple Discovery)', | |
'wlw' => 'WLW-Link vorhanden (Windows Live Writer)', | |
'db_reporting' => 'Datenbank-Fehlermeldungen aktiviert', | |
'php_reporting' => 'PHP-Fehlermeldungen aktiviert', | |
'versions' => 'Versionssichtbarkeit (Generator-Tag)', | |
'registered_versions' => 'Registrierte WP-Versionen sichtbar', | |
'admin' => 'Admin Username vorhanden', | |
'readme' => 'readme.html vorhanden', | |
'wp_uptodate' => 'WordPress aktuell', | |
'phpversion_matched' => 'PHP-Version entspricht empfohlenem Wert', | |
'sslprotocol' => 'SSL-Protokoll aktiv', | |
'debug_disabled' => 'WP_DEBUG deaktiviert', | |
], | |
], | |
]; | |
} | |
function mainwp_translate_flag($value) { | |
return match ($value) { | |
'Y' => 'Ja', | |
'N' => 'Nein', | |
'Y_UNABLE' => 'Ja, aber nicht überprüfbar', | |
'N_UNABLE' => 'Nein, aber nicht überprüfbar', | |
'connected' => 'verbunden', | |
default => $value, | |
}; | |
} | |
// 1. Tags registrieren | |
add_filter('bricks/dynamic_tags_list', function ($tags) { | |
foreach (mainwp_get_tag_sources() as $group_key => $group) { | |
foreach ($group['fields'] as $field_key => $label) { | |
$tags[] = [ | |
'name' => '{mainwp:' . $group_key . ':' . $field_key . ':<site_id>}', | |
'label' => 'MainWP: ' . $label, | |
'group' => $group['label'], | |
]; | |
} | |
} | |
return $tags; | |
}); | |
// 2. Einzelnen Tag rendern | |
add_filter('bricks/dynamic_data/render_tag', 'mainwp_render_tag', 20, 3); | |
function mainwp_render_tag($tag, $post, $context = 'text') { | |
if (!preg_match('/^{mainwp:([a-z_]+):([a-z_]+):([^}]+)}$/', $tag, $m)) { | |
return $tag; | |
} | |
[$full, $group, $field, $site_id] = $m; | |
return mainwp_fetch_value($group, $field, $site_id); | |
} | |
// 3. Content-Parser für alle Inhalte | |
add_filter('bricks/dynamic_data/render_content', 'mainwp_parse_content', 20, 3); | |
add_filter('bricks/frontend/render_data', 'mainwp_parse_content', 20, 2); | |
function mainwp_parse_content($content, $post = null, $context = 'text') { | |
if (strpos($content, '{mainwp:') === false) return $content; | |
preg_match_all('/{mainwp:([a-z_]+):([a-z_]+):([^}]+)}/', $content, $matches); | |
foreach ($matches[0] as $i => $full_tag) { | |
$group = $matches[1][$i]; | |
$field = $matches[2][$i]; | |
$site_id = $matches[3][$i]; | |
$value = mainwp_fetch_value($group, $field, $site_id); | |
$content = str_replace($full_tag, $value, $content); | |
} | |
return $content; | |
} | |
// 4. API Call Handler | |
function mainwp_fetch_value($group_key, $field_key, $site_id) { | |
$sources = mainwp_get_tag_sources(); | |
if (!isset($sources[$group_key])) return '[ungültige Gruppe]'; | |
if (!isset($sources[$group_key]['fields'][$field_key])) return '[ungültiges Feld]'; | |
$endpoint = $sources[$group_key]['endpoint']; | |
// Platzhalter ersetzen, falls vorhanden | |
if (strpos($endpoint, '{site_id}') !== false) { | |
$endpoint = str_replace('{site_id}', urlencode($site_id), $endpoint); | |
} | |
$cache_key = 'mainwp_' . md5($group_key . '_' . $site_id); | |
$data = get_transient($cache_key); | |
if (!$data) { | |
$url = MAINWP_API_URL . $endpoint; | |
$response = wp_remote_get($url, [ | |
'headers' => [ | |
'Authorization' => MAINWP_API_TOKEN, | |
'Accept' => 'application/json', | |
'User-Agent' => 'MainWP-Client', | |
], | |
'timeout' => 15, | |
'sslverify' => false, | |
]); | |
if (is_wp_error($response)) return '[API Fehler]'; | |
$json = json_decode(wp_remote_retrieve_body($response), true); | |
$data = $json['data'] ?? $json; | |
if (!is_array($data)) return '[keine Daten]'; | |
set_transient($cache_key, $data, 60); // Cache 1 Minute | |
} | |
$value = $data[$field_key] ?? null; | |
// Universelle Flag-Übersetzung nur für bestimmte Werte | |
if (in_array($value, ['Y', 'N', 'Y_UNABLE', 'N_UNABLE', 'connected'], true)) { | |
return mainwp_translate_flag($value); | |
} | |
return $value ?? '[Wert fehlt]'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment