Skip to content

WordPress Filters

Flowguard bietet Filter-Hooks, die Entwicklern ermöglichen, Daten und Verhalten vor der Verarbeitung zu modifizieren.

Übersicht

Filter sind WordPress-Hooks, mit denen Sie Daten modifizieren können, während sie durch Flowguard fließen. Im Gegensatz zu Actions (die Code ausführen), transformieren Filter Daten und geben modifizierte Werte zurück.

Verwenden Sie Filter, um:

  • Flow-Daten vor der Ausführung zu modifizieren
  • Standardeinstellungen anzupassen
  • Step-Konfigurationen zu transformieren
  • Benutzerdefinierte Validierung hinzuzufügen
  • Mit anderen Systemen zu integrieren

Verfügbare Filter

flowguard_flow_data

Modifiziert Flow-Daten, bevor sie zur Ausführung gesendet werden.

Hook-Name: flowguard_flow_data

Parameter:

ParameterTypBeschreibung
$flow_dataarrayVollständige Flow-Daten
$flow_idintDie Flow-Post-ID

Flow-Daten-Struktur:

php
$flow_data = [
    'id'       => 123,
    'title'    => 'Login-Test-Flow',
    'steps'    => [
        [
            'id'     => 'step_1',
            'type'   => 'navigate',
            'config' => [
                'url' => 'https://ihreseite.de/login'
            ]
        ],
        // ... weitere Steps
    ],
    'settings' => [
        'timeout'    => 30000,
        'retries'    => 0,
        'screenshot' => true,
    ],
    'siteUrl'  => 'https://ihreseite.de',
];

Rückgabewert: Modifiziertes $flow_data-Array

Beispielverwendung:

php
/**
 * Benutzerdefinierte Header zu allen Flows hinzufügen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $flow_data['headers'] = [
        'X-Flow-ID' => $flow_id,
        'X-Site-ID' => get_current_blog_id(),
    ];

    return $flow_data;
}, 10, 2);

Anwendungsfälle:

1. Authentifizierungs-Tokens hinzufügen

php
/**
 * Auth-Token zu Flows hinzufügen, die es benötigen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Nur für Flows mit "API" im Titel
    if (strpos($flow_data['title'], 'API') !== false) {
        $flow_data['authToken'] = get_option('my_api_token');
    }

    return $flow_data;
}, 10, 2);

2. Timeouts dynamisch modifizieren

php
/**
 * Timeout für langsame Seiten erhöhen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    foreach ($flow_data['steps'] as &$step) {
        if ($step['type'] === 'navigate') {
            // Timeout für Navigations-Steps verdoppeln
            if (!isset($step['timeout'])) {
                $step['timeout'] = $flow_data['settings']['timeout'] * 2;
            }
        }
    }

    return $flow_data;
}, 10, 2);

3. Benutzerdefinierte Step-Daten hinzufügen

php
/**
 * Metadaten zu Steps hinzufügen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    foreach ($flow_data['steps'] as &$step) {
        $step['metadata'] = [
            'flow_id'   => $flow_id,
            'timestamp' => current_time('c'),
            'user_id'   => get_current_user_id(),
        ];
    }

    return $flow_data;
}, 10, 2);

4. URLs für Staging ersetzen

php
/**
 * Produktions-URLs durch Staging-URLs ersetzen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $is_staging = defined('WP_ENVIRONMENT_TYPE') && WP_ENVIRONMENT_TYPE === 'staging';

    if ($is_staging) {
        foreach ($flow_data['steps'] as &$step) {
            if ($step['type'] === 'navigate' && isset($step['config']['url'])) {
                $step['config']['url'] = str_replace(
                    'https://produktion.de',
                    'https://staging.de',
                    $step['config']['url']
                );
            }
        }
    }

    return $flow_data;
}, 10, 2);

5. Performance-Monitoring hinzufügen

php
/**
 * Performance-Marker hinzufügen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $flow_data['performance'] = [
        'enabled'     => true,
        'track_steps' => true,
        'report_url'  => 'https://monitor.beispiel.de/api/report',
    ];

    return $flow_data;
}, 10, 2);

6. Testdaten einfügen

php
/**
 * Test-Benutzeranmeldedaten dynamisch hinzufügen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Fill-Steps für Benutzername/Passwort finden
    foreach ($flow_data['steps'] as &$step) {
        if ($step['type'] === 'fill') {
            // Platzhalter durch echte Test-Anmeldedaten ersetzen
            if (isset($step['config']['text'])) {
                $step['config']['text'] = str_replace(
                    '{{TEST_USER}}',
                    get_option('flowguard_test_username'),
                    $step['config']['text']
                );

                $step['config']['text'] = str_replace(
                    '{{TEST_PASS}}',
                    get_option('flowguard_test_password'),
                    $step['config']['text']
                );
            }
        }
    }

    return $flow_data;
}, 10, 2);

7. Bedingte Logik hinzufügen

php
/**
 * Bestimmte Steps basierend auf Bedingungen überspringen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Login-Steps überspringen, wenn bereits eingeloggt
    $is_logged_in = is_user_logged_in();

    if ($is_logged_in) {
        $flow_data['steps'] = array_filter($flow_data['steps'], function($step) {
            return !isset($step['tags']) || !in_array('login', $step['tags']);
        });

        // Array neu indizieren
        $flow_data['steps'] = array_values($flow_data['steps']);
    }

    return $flow_data;
}, 10, 2);

8. Benutzerdefinierte Validierung hinzufügen

php
/**
 * Flow-Daten vor der Ausführung validieren
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Sicherstellen, dass alle Navigate-Steps gültige URLs haben
    foreach ($flow_data['steps'] as $step) {
        if ($step['type'] === 'navigate') {
            $url = $step['config']['url'] ?? '';

            if (!filter_var($url, FILTER_VALIDATE_URL)) {
                // Fehler protokollieren
                error_log("Flowguard: Ungültige URL in Flow {$flow_id}: {$url}");

                // Korrigieren
                $step['config']['url'] = home_url();
            }
        }
    }

    return $flow_data;
}, 10, 2);

9. Umgebungsvariablen hinzufügen

php
/**
 * Umgebungsspezifische Konfiguration einfügen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $flow_data['environment'] = [
        'name'      => wp_get_environment_type(),
        'debug'     => WP_DEBUG,
        'multisite' => is_multisite(),
        'locale'    => get_locale(),
    ];

    return $flow_data;
}, 10, 2);

10. Einstellungen basierend auf Flow-Typ modifizieren

php
/**
 * Einstellungen für verschiedene Flow-Typen anpassen
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Kritische Flows bekommen mehr Wiederholungen
    if (strpos(strtolower($flow_data['title']), 'kritisch') !== false) {
        $flow_data['settings']['retries'] = 3;
        $flow_data['settings']['timeout'] = 60000;
    }

    // Schnelle Smoke-Tests laufen schnell
    if (strpos(strtolower($flow_data['title']), 'smoke') !== false) {
        $flow_data['settings']['timeout'] = 10000;
        $flow_data['settings']['screenshot'] = false;
    }

    return $flow_data;
}, 10, 2);

Filter-Priorität

Filter akzeptieren Prioritäts- und Argumentanzahl-Parameter:

php
add_filter('flowguard_flow_data', 'ihre_funktion', $prioritaet, $akzeptierte_args);

Standardpriorität: 10

Niedrigere Zahlen = Frühere Ausführung Höhere Zahlen = Spätere Ausführung

Beispiel mit Priorität:

php
// Zuerst ausführen (Priorität 5) - Standards setzen
add_filter('flowguard_flow_data', 'set_flow_defaults', 5, 2);

// Als zweites ausführen (Priorität 10) - URLs modifizieren
add_filter('flowguard_flow_data', 'replace_urls', 10, 2);

// Zuletzt ausführen (Priorität 20) - Finale Validierung
add_filter('flowguard_flow_data', 'validate_flow_data', 20, 2);

Filter entfernen

Um einen zuvor hinzugefügten Filter zu entfernen:

php
remove_filter('flowguard_flow_data', 'ihre_funktion', $prioritaet);

Beispiel:

php
// Filter hinzufügen
add_filter('flowguard_flow_data', 'modify_data', 10, 2);

// Filter später entfernen
remove_filter('flowguard_flow_data', 'modify_data', 10);

Best Practices

1. Immer Daten zurückgeben

Filter müssen die modifizierten Daten zurückgeben:

php
// Gut - Gibt modifizierte Daten zurück
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $flow_data['custom'] = 'value';
    return $flow_data;
}, 10, 2);

// Schlecht - Gibt nichts zurück
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $flow_data['custom'] = 'value';
    // Return-Statement fehlt!
}, 10, 2);

2. Datenstruktur beibehalten

Zerstören Sie nicht die erwartete Datenstruktur:

php
// Gut - Fügt neue Felder hinzu, behält Struktur
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    $flow_data['custom_field'] = 'value';
    return $flow_data;
}, 10, 2);

// Schlecht - Ersetzt gesamtes Array
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    return ['komplett' => 'anders'];
}, 10, 2);

3. Existenz vor Modifikation prüfen

Überprüfen Sie, ob Daten existieren, bevor Sie darauf zugreifen:

php
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Prüfen, ob Steps-Array existiert
    if (isset($flow_data['steps']) && is_array($flow_data['steps'])) {
        foreach ($flow_data['steps'] as &$step) {
            // Steps sicher modifizieren
        }
    }

    return $flow_data;
}, 10, 2);

4. Typprüfung verwenden

Stellen Sie sicher, dass Datentypen korrekt sind:

php
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Sicherstellen, dass Timeout ein Integer ist
    if (isset($flow_data['settings']['timeout'])) {
        $flow_data['settings']['timeout'] = absint($flow_data['settings']['timeout']);
    }

    return $flow_data;
}, 10, 2);

5. Fehler elegant behandeln

Lassen Sie Filter die Ausführung nicht unterbrechen:

php
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    try {
        // Ihre Modifikationen
        $flow_data = apply_custom_logic($flow_data);
    } catch (Exception $e) {
        error_log('Flowguard-Filter-Fehler: ' . $e->getMessage());
        // Originaldaten bei Fehler zurückgeben
    }

    return $flow_data;
}, 10, 2);

6. Filter dokumentieren

Kommentieren Sie komplexe Filter-Logik:

php
/**
 * Umgebungsspezifische URLs in Flow-Navigations-Steps einfügen
 *
 * Ersetzt Platzhalter-URLs wie {{BASE_URL}} durch die tatsächliche
 * WordPress-Site-URL basierend auf der aktuellen Umgebung.
 *
 * @param array $flow_data Vollständige Flow-Daten
 * @param int   $flow_id   Flow-Post-ID
 * @return array Modifizierte Flow-Daten
 */
add_filter('flowguard_flow_data', function($flow_data, $flow_id) {
    // Implementierung
    return $flow_data;
}, 10, 2);

7. Passende Priorität verwenden

Ordnen Sie Filter logisch basierend auf Abhängigkeiten:

php
// Priorität 5: Basiskonfiguration setzen
add_filter('flowguard_flow_data', 'set_defaults', 5, 2);

// Priorität 10: Modifikationen anwenden
add_filter('flowguard_flow_data', 'apply_customizations', 10, 2);

// Priorität 15: Finale Anpassungen
add_filter('flowguard_flow_data', 'finalize_data', 15, 2);

Vollständiges Beispiel: Dynamische Flow-Konfiguration

Hier ist ein vollständiges Beispiel, das dynamische Flow-Konfiguration implementiert:

php
<?php
/**
 * Flowguard Dynamische Konfiguration
 */

class Flowguard_Config {

    private $config;

    public function __construct() {
        // Konfiguration laden
        $this->config = $this->load_config();

        // Filter hinzufügen
        add_filter('flowguard_flow_data', [$this, 'modify_flow_data'], 10, 2);
    }

    /**
     * Konfiguration aus Optionen laden
     */
    private function load_config() {
        return [
            'environments' => [
                'production' => [
                    'base_url' => 'https://produktion.de',
                    'timeout'  => 30000,
                ],
                'staging' => [
                    'base_url' => 'https://staging.de',
                    'timeout'  => 60000,
                ],
            ],
            'test_users' => [
                'admin' => [
                    'username' => 'test_admin',
                    'password' => 'test_pass_123',
                ],
                'customer' => [
                    'username' => 'test_customer',
                    'password' => 'test_pass_456',
                ],
            ],
        ];
    }

    /**
     * Flow-Daten basierend auf Konfiguration modifizieren
     */
    public function modify_flow_data($flow_data, $flow_id) {
        // 1. Umgebungsplatzhalter ersetzen
        $flow_data = $this->replace_placeholders($flow_data);

        // 2. Test-Anmeldedaten einfügen
        $flow_data = $this->inject_credentials($flow_data);

        // 3. Timeouts anpassen
        $flow_data = $this->adjust_timeouts($flow_data);

        // 4. Tracking hinzufügen
        $flow_data = $this->add_tracking($flow_data, $flow_id);

        // 5. Validieren
        $flow_data = $this->validate($flow_data);

        return $flow_data;
    }

    /**
     * Umgebungsplatzhalter ersetzen
     */
    private function replace_placeholders($flow_data) {
        $environment = wp_get_environment_type();
        $base_url = $this->config['environments'][$environment]['base_url'] ?? home_url();

        foreach ($flow_data['steps'] as &$step) {
            if ($step['type'] === 'navigate' && isset($step['config']['url'])) {
                $step['config']['url'] = str_replace(
                    '{{BASE_URL}}',
                    $base_url,
                    $step['config']['url']
                );
            }
        }

        return $flow_data;
    }

    /**
     * Test-Benutzeranmeldedaten einfügen
     */
    private function inject_credentials($flow_data) {
        // Benutzertyp basierend auf Flow-Titel erkennen
        $user_type = 'customer'; // Standard

        if (stripos($flow_data['title'], 'admin') !== false) {
            $user_type = 'admin';
        }

        $credentials = $this->config['test_users'][$user_type] ?? null;

        if (!$credentials) {
            return $flow_data;
        }

        foreach ($flow_data['steps'] as &$step) {
            if ($step['type'] === 'fill' && isset($step['config']['text'])) {
                $text = $step['config']['text'];

                if (strpos($text, '{{USERNAME}}') !== false) {
                    $step['config']['text'] = $credentials['username'];
                }

                if (strpos($text, '{{PASSWORD}}') !== false) {
                    $step['config']['text'] = $credentials['password'];
                }
            }
        }

        return $flow_data;
    }

    /**
     * Timeouts basierend auf Umgebung anpassen
     */
    private function adjust_timeouts($flow_data) {
        $environment = wp_get_environment_type();
        $timeout = $this->config['environments'][$environment]['timeout'] ?? 30000;

        $flow_data['settings']['timeout'] = $timeout;

        return $flow_data;
    }

    /**
     * Tracking-Informationen hinzufügen
     */
    private function add_tracking($flow_data, $flow_id) {
        $flow_data['tracking'] = [
            'flow_id'     => $flow_id,
            'environment' => wp_get_environment_type(),
            'timestamp'   => current_time('c'),
            'user_id'     => get_current_user_id(),
            'site_id'     => get_current_blog_id(),
        ];

        return $flow_data;
    }

    /**
     * Flow-Daten validieren
     */
    private function validate($flow_data) {
        // Sicherstellen, dass alle URLs absolut sind
        foreach ($flow_data['steps'] as &$step) {
            if ($step['type'] === 'navigate') {
                $url = $step['config']['url'] ?? '';

                if ($url && !filter_var($url, FILTER_VALIDATE_URL)) {
                    error_log("Flowguard: Ungültige URL erkannt: {$url}");
                    $step['config']['url'] = home_url();
                }
            }
        }

        // Sicherstellen, dass Timeout vernünftig ist
        $timeout = $flow_data['settings']['timeout'] ?? 30000;

        if ($timeout < 1000 || $timeout > 300000) {
            error_log('Flowguard: Timeout außerhalb des Bereichs, verwende Standard');
            $flow_data['settings']['timeout'] = 30000;
        }

        return $flow_data;
    }
}

// Initialisieren
new Flowguard_Config();

Verwandte Dokumentation

Zukünftige Filter

Zusätzliche Filter für zukünftige Versionen geplant:

  • flowguard_step_config - Einzelne Step-Konfiguration modifizieren
  • flowguard_flow_results - Ergebnisse vor dem Speichern transformieren
  • flowguard_settings_defaults - Standardeinstellungen ändern
  • flowguard_validation_rules - Benutzerdefinierte Validierung hinzufügen
  • flowguard_timeout_value - Dynamische Timeout-Berechnung

Support

Bei Fragen zu Filtern:

  1. Lesen Sie die WordPress Plugin API Dokumentation
  2. Prüfen Sie den Flowguard-Quellcode
  3. Kontaktieren Sie den Support für Unterstützung