<?php
/** Shared safety, cache and state helpers for Beaver Builder templates/settings. */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! defined( 'WPAGENT_BEAVER_TEMPLATE_MAX' ) ) {
	define( 'WPAGENT_BEAVER_TEMPLATE_MAX', 200 );
}
if ( ! defined( 'WPAGENT_BEAVER_TEMPLATE_MAX_NAME_BYTES' ) ) {
	define( 'WPAGENT_BEAVER_TEMPLATE_MAX_NAME_BYTES', 200 );
}
if ( ! defined( 'WPAGENT_BEAVER_TEMPLATE_LOCK_OPTION' ) ) {
	define( 'WPAGENT_BEAVER_TEMPLATE_LOCK_OPTION', 'wpagent_beaver_templates_lock' );
}
if ( ! defined( 'WPAGENT_BEAVER_GLOBAL_SETTINGS_LOCK_OPTION' ) ) {
	define( 'WPAGENT_BEAVER_GLOBAL_SETTINGS_LOCK_OPTION', 'wpagent_beaver_global_settings_lock' );
}
if ( ! defined( 'WPAGENT_BEAVER_GLOBAL_SETTINGS_REVISION_PREFIX' ) ) {
	define( 'WPAGENT_BEAVER_GLOBAL_SETTINGS_REVISION_PREFIX', 'wpagent_beaver_global_settings_revision_' );
}

/** @return string[] */
function wpagent_beaver_user_template_types(): array {
	return array( 'layout', 'row', 'column', 'module' );
}

function wpagent_beaver_user_templates_available(): bool {
	return wpagent_beaver_available()
		&& method_exists( 'FLBuilderModel', 'get_user_templates' )
		&& method_exists( 'FLBuilderModel', 'save_user_template' )
		&& method_exists( 'FLBuilderModel', 'save_node_template' )
		&& method_exists( 'FLBuilderModel', 'delete_user_template' )
		&& post_type_exists( 'fl-builder-template' )
		&& taxonomy_exists( 'fl-builder-template-type' )
		&& taxonomy_exists( 'fl-builder-template-category' );
}

function wpagent_beaver_user_templates_unavailable_error(): WP_Error {
	return new WP_Error( 'wpagent_beaver_templates_unavailable', __( 'Die aktive Beaver-Builder-Version stellt keine registrierten nativen User-Template-Posttypen und Taxonomien bereit.', 'wp-agent-companion' ), array( 'status' => 409 ) );
}

/** @return string|WP_Error */
function wpagent_beaver_templates_expected_hash( $value ) {
	$hash = is_string( $value ) ? strtolower( trim( $value ) ) : '';
	return preg_match( '/^[a-f0-9]{64}$/', $hash )
		? $hash
		: new WP_Error( 'wpagent_beaver_bad_hash', __( 'Eine gültige erwartete SHA-256-Prüfsumme ist erforderlich.', 'wp-agent-companion' ), array( 'status' => 400 ) );
}

/** @return string|WP_Error */
function wpagent_beaver_templates_acquire_lock() {
	$token = wpagent_companion_acquire_option_lock( WPAGENT_BEAVER_TEMPLATE_LOCK_OPTION, WPAGENT_BEAVER_LOCK_TTL );
	if ( is_string( $token ) ) {
		return $token;
	}
	return new WP_Error( 'wpagent_beaver_templates_locked', __( 'Die Beaver-Builder-Templates werden gerade geändert. Bitte erneut versuchen.', 'wp-agent-companion' ), array( 'status' => 409 ) );
}

function wpagent_beaver_templates_release_lock( string $token ): void {
	wpagent_companion_release_option_lock( WPAGENT_BEAVER_TEMPLATE_LOCK_OPTION, $token );
}

/** @return string|WP_Error */
function wpagent_beaver_global_settings_acquire_lock() {
	$token = wpagent_companion_acquire_option_lock( WPAGENT_BEAVER_GLOBAL_SETTINGS_LOCK_OPTION, WPAGENT_BEAVER_LOCK_TTL );
	if ( is_string( $token ) ) {
		return $token;
	}
	return new WP_Error( 'wpagent_beaver_global_settings_locked', __( 'Die globalen Beaver-Builder-Einstellungen werden gerade geändert. Bitte erneut versuchen.', 'wp-agent-companion' ), array( 'status' => 409 ) );
}

function wpagent_beaver_global_settings_release_lock( string $token ): void {
	wpagent_companion_release_option_lock( WPAGENT_BEAVER_GLOBAL_SETTINGS_LOCK_OPTION, $token );
}

/**
 * Beaver caches get_user_templates() in a private static property. There is no
 * public invalidation API in Lite, so clear only that provider cache through a
 * narrowly scoped reflection call. A failed invalidation is a recovery state.
 */
function wpagent_beaver_refresh_template_cache(): bool {
	if ( ! class_exists( 'FLBuilderModel' ) ) {
		return false;
	}
	try {
		$property = new ReflectionProperty( 'FLBuilderModel', 'get_user_templates_cache' );
		$property->setAccessible( true );
		$property->setValue( null, array() );
		return true;
	} catch ( Throwable $error ) {
		return false;
	}
}

/** @return WP_Post|WP_Error */
function wpagent_beaver_user_template_post( int $post_id ) {
	if ( ! wpagent_beaver_user_templates_available() ) {
		return wpagent_beaver_user_templates_unavailable_error();
	}
	// The template's type, status and ID are target state. Resolve the concrete
	// edit meta-cap first so missing and inaccessible templates are indistinct.
	if ( ! current_user_can( 'edit_post', $post_id ) ) {
		return new WP_Error( 'wpagent_beaver_forbidden', __( 'Keine Berechtigung zum Bearbeiten dieses Beaver-Builder-Templates.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	$post = get_post( $post_id );
	if ( ! $post || 'fl-builder-template' !== $post->post_type ) {
		return new WP_Error( 'wpagent_beaver_forbidden', __( 'Keine Berechtigung zum Bearbeiten dieses Beaver-Builder-Templates.', 'wp-agent-companion' ), array( 'status' => 403 ) );
	}
	if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) {
		return new WP_Error( 'wpagent_beaver_template_revision', __( 'Revisionen und Autosaves sind keine Beaver-Builder-Templates.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	return $post;
}

/** @return true|WP_Error */
function wpagent_beaver_template_static_guard( int $post_id ) {
	$type = FLBuilderModel::get_user_template_type( $post_id );
	if ( ! in_array( $type, wpagent_beaver_user_template_types(), true ) ) {
		return new WP_Error( 'wpagent_beaver_template_type', __( 'Der Beaver-Builder-Template-Typ ist nicht sicher unterstützt.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	if ( ( method_exists( 'FLBuilderModel', 'is_post_global_node_template' ) && FLBuilderModel::is_post_global_node_template( $post_id ) )
		|| ( method_exists( 'FLBuilderModel', 'is_post_dynamic_editing_node_template' ) && FLBuilderModel::is_post_dynamic_editing_node_template( $post_id ) ) ) {
		return new WP_Error( 'wpagent_beaver_template_linked', __( 'Globale, dynamische oder verknüpfte Beaver-Builder-Templates sind im sicheren Pfad gesperrt.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	return true;
}

/** @param mixed $value */
function wpagent_beaver_template_value_is_empty( $value ): bool {
	return null === $value || '' === $value || array() === $value || ( is_object( $value ) && array() === get_object_vars( $value ) );
}

/** @return true|WP_Error */
function wpagent_beaver_template_validate_value( $value, int $depth = 0 ) {
	if ( $depth > WPAGENT_BEAVER_MAX_VALUE_DEPTH ) {
		return new WP_Error( 'wpagent_beaver_template_value_depth', __( 'Ein Template-Wert ist zu tief verschachtelt.', 'wp-agent-companion' ), array( 'status' => 413 ) );
	}
	if ( is_object( $value ) ) {
		$value = get_object_vars( $value );
	}
	if ( is_string( $value ) ) {
		if ( strlen( $value ) > WPAGENT_BEAVER_MAX_STRING_BYTES ) {
			return new WP_Error( 'wpagent_beaver_template_value_size', __( 'Ein Template-Wert ist zu groß.', 'wp-agent-companion' ), array( 'status' => 413 ) );
		}
		// The native save APIs copy the complete provider state. No markup,
		// shortcode, CSS or JavaScript may cross this boundary.
		if ( preg_match( '/[<>]/', $value ) || preg_match( '/\[[A-Za-z][A-Za-z0-9_-]*(?:\s|\]|\/)/', $value ) || preg_match( '/(?:javascript\s*:|data\s*:\s*text\/html|on[a-z]+\s*=|<style|<script|url\s*\()/i', $value ) ) {
			return new WP_Error( 'wpagent_beaver_template_unsafe_value', __( 'HTML, Shortcodes, CSS und JavaScript sind in statischen Beaver-Builder-Templates nicht erlaubt.', 'wp-agent-companion' ), array( 'status' => 400 ) );
		}
		return true;
	}
	if ( is_array( $value ) ) {
		if ( count( $value ) > WPAGENT_BEAVER_MAX_FIELDS ) {
			return new WP_Error( 'wpagent_beaver_template_value_size', __( 'Ein Template-Wert enthält zu viele Einträge.', 'wp-agent-companion' ), array( 'status' => 413 ) );
		}
		foreach ( $value as $item ) {
			$valid = wpagent_beaver_template_validate_value( $item, $depth + 1 );
			if ( is_wp_error( $valid ) ) {
				return $valid;
			}
		}
		return true;
	}
	if ( is_null( $value ) || is_bool( $value ) || is_int( $value ) || is_float( $value ) ) {
		return true;
	}
	return new WP_Error( 'wpagent_beaver_template_value_type', __( 'Ein Template-Wert besitzt einen nicht unterstützten Typ.', 'wp-agent-companion' ), array( 'status' => 400 ) );
}

/** @param object $node @return true|WP_Error */
function wpagent_beaver_template_validate_node_settings( object $node ) {
	$type     = sanitize_key( (string) ( $node->type ?? '' ) );
	$settings = is_object( $node->settings ?? null ) ? get_object_vars( $node->settings ) : array();
	if ( 'column-group' === $type ) {
		return wpagent_beaver_template_validate_value( $settings );
	}
	$fields = wpagent_beaver_fields_for_node( $node );
	if ( is_wp_error( $fields ) ) {
		return $fields;
	}
	foreach ( $settings as $name => $value ) {
		if ( 'module' === $type && 'type' === $name ) {
			$valid = wpagent_beaver_template_validate_value( $value );
		} elseif ( ! is_string( $name ) || ! isset( $fields[ $name ] ) ) {
			// Beaver materializes responsive/unit helper keys that are not part
			// of the base form field map. Permit those only as ordinary values,
			// while still applying the shared executable-name deny-list.
			if ( ! is_string( $name ) || ! wpagent_beaver_field_is_writable( $name, array( 'type' => 'text' ) ) ) {
				return new WP_Error( 'wpagent_beaver_template_blocked_setting', __( 'Ein unbekanntes Beaver-Builder-Feld ist im statischen Template gesperrt.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => is_scalar( $name ) ? (string) $name : '' ) );
			}
			$valid = wpagent_beaver_template_validate_value( $value );
		} elseif ( ! wpagent_beaver_field_is_writable( $name, $fields[ $name ] ) ) {
			$valid = wpagent_beaver_template_value_is_empty( $value )
				? true
				: new WP_Error( 'wpagent_beaver_template_blocked_setting', __( 'CSS, JavaScript, HTML und andere nicht-primitive Beaver-Builder-Felder sind gesperrt.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => $name ) );
		} else {
			$valid = wpagent_beaver_template_validate_value( $value );
		}
		if ( is_wp_error( $valid ) ) {
			return $valid;
		}
	}
	return true;
}

/** @param array<string,object> $data @param mixed $settings @return true|WP_Error */
function wpagent_beaver_template_validate_state( array $data, $settings ) {
	$safe = wpagent_beaver_validate_safe_subtrees( $data, array_keys( $data ), 'wpagent_beaver_template_unsafe_subtree' );
	if ( is_wp_error( $safe ) ) {
		return $safe;
	}
	foreach ( $data as $node ) {
		if ( ! is_object( $node ) || ! empty( $node->dynamic ) ) {
			return new WP_Error( 'wpagent_beaver_template_unsafe_subtree', __( 'Globale, dynamische oder verknüpfte Beaver-Builder-Knoten sind gesperrt.', 'wp-agent-companion' ), array( 'status' => 400 ) );
		}
		$valid = wpagent_beaver_template_validate_node_settings( $node );
		if ( is_wp_error( $valid ) ) {
			return $valid;
		}
	}
	$settings = is_object( $settings ) ? get_object_vars( $settings ) : ( is_array( $settings ) ? $settings : array() );
	foreach ( $settings as $name => $value ) {
		if ( in_array( $name, array( 'css', 'js', 'shape_form' ), true ) && ! wpagent_beaver_template_value_is_empty( $value ) ) {
			return new WP_Error( 'wpagent_beaver_template_blocked_setting', __( 'CSS, JavaScript und komplexe Shape-Einstellungen sind in statischen Beaver-Builder-Templates gesperrt.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => $name ) );
		}
		$valid = wpagent_beaver_template_validate_value( $value );
		if ( is_wp_error( $valid ) ) {
			return $valid;
		}
	}
	return true;
}

/** @param array<string,object> $data @return array<string,object>|WP_Error */
function wpagent_beaver_template_subtree_data( array $data, string $root_id ) {
	$root_id = sanitize_key( $root_id );
	if ( '' === $root_id || ! isset( $data[ $root_id ] ) || ! is_object( $data[ $root_id ] ) ) {
		return new WP_Error( 'wpagent_beaver_node_not_found', __( 'Beaver-Builder-Knoten nicht gefunden.', 'wp-agent-companion' ), array( 'status' => 404 ) );
	}
	$children = array();
	foreach ( $data as $node_id => $node ) {
		if ( is_object( $node ) ) {
			$children[ sanitize_key( (string) ( $node->parent ?? '' ) ) ][] = sanitize_key( (string) $node_id );
		}
	}
	$result = array();
	$queue  = array( $root_id );
	while ( array() !== $queue ) {
		$node_id = array_pop( $queue );
		if ( isset( $result[ $node_id ] ) ) {
			continue;
		}
		$result[ $node_id ] = $data[ $node_id ];
		foreach ( $children[ $node_id ] ?? array() as $child_id ) {
			$queue[] = $child_id;
		}
		if ( count( $result ) > WPAGENT_BEAVER_MAX_NODES ) {
			return new WP_Error( 'wpagent_beaver_node_limit', __( 'Das Beaver-Builder-Template überschreitet die sichere Knotengrenze.', 'wp-agent-companion' ), array( 'status' => 413 ) );
		}
	}
	return $result;
}

/** @param array<string,object> $data @return string|WP_Error */
function wpagent_beaver_template_data_signature( array $data ) {
	$roots = array();
	foreach ( $data as $node_id => $node ) {
		if ( is_object( $node ) && '' === sanitize_key( (string) ( $node->parent ?? '' ) ) ) {
			$roots[] = sanitize_key( (string) $node_id );
		}
	}
	if ( array() === $data ) {
		return hash( 'sha256', 'empty-layout' );
	}
	$signatures = array();
	foreach ( $roots as $root_id ) {
		$signature = wpagent_beaver_subtree_signature( $data, $root_id );
		if ( is_wp_error( $signature ) ) {
			return $signature;
		}
		$signatures[] = $signature;
	}
	sort( $signatures, SORT_STRING );
	return hash( 'sha256', implode( "\0", $signatures ) );
}

/** @return string|WP_Error */
function wpagent_beaver_template_plain_name( $value, string $field = 'name', bool $required = true ) {
	$name = is_string( $value ) ? trim( wp_unslash( $value ) ) : '';
	if ( ! $required && '' === $name ) {
		return '';
	}
	if ( ( $required && '' === $name ) || strlen( $name ) > WPAGENT_BEAVER_TEMPLATE_MAX_NAME_BYTES || preg_match( '/[<>\[\]]/', $name ) || wp_strip_all_tags( $name ) !== $name ) {
		/* translators: %s: Beaver Builder field name. */
		return new WP_Error( 'wpagent_beaver_template_bad_name', sprintf( __( 'Das Beaver-Builder-Feld „%s“ muss ein kurzer Text ohne HTML oder Shortcodes sein.', 'wp-agent-companion' ), $field ), array( 'status' => 400, 'field' => $field ) );
	}
	return sanitize_text_field( $name );
}

/** @return array<string,mixed> */
function wpagent_beaver_template_public_layout_settings( $settings ): array {
	$settings = is_object( $settings ) ? get_object_vars( $settings ) : ( is_array( $settings ) ? $settings : array() );
	$result   = array();
	foreach ( $settings as $name => $value ) {
		if ( in_array( (string) $name, array( 'css', 'js', 'shape_form' ), true ) || is_array( $value ) || is_object( $value ) ) {
			continue;
		}
		if ( is_scalar( $value ) || null === $value ) {
			$valid = wpagent_beaver_template_validate_value( $value );
			if ( ! is_wp_error( $valid ) ) {
				$result[ (string) $name ] = $value;
			}
		}
	}
	ksort( $result, SORT_STRING );
	return $result;
}

/** @return array<string,array<string,mixed>> */
function wpagent_beaver_global_settings_field_catalog(): array {
	return array(
		'show_default_heading'    => array( 'type' => 'select', 'values' => array( '0', '1' ) ),
		'top_level_box_spacing'   => array( 'type' => 'select', 'values' => array( '0', '1' ) ),
		'row_width'               => array( 'type' => 'integer', 'maximum' => 9999 ),
		'row_width_default'       => array( 'type' => 'select', 'values' => array( 'fixed', 'full' ) ),
		'row_content_width_default' => array( 'type' => 'select', 'values' => array( 'fixed', 'full' ) ),
		'responsive_enabled'      => array( 'type' => 'select', 'values' => array( '0', '1' ) ),
		'auto_spacing'            => array( 'type' => 'select', 'values' => array( '0', '1' ) ),
		'large_breakpoint'        => array( 'type' => 'integer', 'maximum' => 9999 ),
		'medium_breakpoint'       => array( 'type' => 'integer', 'maximum' => 9999 ),
		'responsive_breakpoint'   => array( 'type' => 'integer', 'maximum' => 9999 ),
		'responsive_preview'      => array( 'type' => 'select', 'values' => array( '0', '1' ) ),
		'responsive_col_max_width' => array( 'type' => 'select', 'values' => array( '0', '1' ) ),
		'responsive_base_fontsize' => array( 'type' => 'integer', 'maximum' => 9999 ),
	);
}

function wpagent_perm_beaver_global_settings(): bool {
	return current_user_can( 'delete_others_posts' );
}

/** @return array<string,string>|WP_Error */
function wpagent_beaver_global_settings_safe_values( $settings ) {
	$settings = is_object( $settings ) ? get_object_vars( $settings ) : ( is_array( $settings ) ? $settings : array() );
	$result   = array();
	foreach ( wpagent_beaver_global_settings_field_catalog() as $name => $field ) {
		if ( ! array_key_exists( $name, $settings ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Beaver Builder lieferte ein unvollständiges globales Einstellungsset.', 'wp-agent-companion' ), array( 'status' => 502, 'setting' => $name ) );
		}
		$value = $settings[ $name ];
		if ( is_bool( $value ) ) {
			$value = $value ? '1' : '0';
		} elseif ( is_int( $value ) || is_float( $value ) ) {
			$value = (string) $value;
		}
		if ( ! is_string( $value ) || strlen( $value ) > 16 || preg_match( '/[<>\[\]]/', $value ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Beaver Builder lieferte einen nicht-primitiven oder unsicheren globalen Einstellungswert.', 'wp-agent-companion' ), array( 'status' => 502, 'setting' => $name ) );
		}
		if ( 'select' === $field['type'] && ! in_array( $value, $field['values'], true ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Beaver Builder lieferte einen unbekannten globalen Auswahlwert.', 'wp-agent-companion' ), array( 'status' => 502, 'setting' => $name ) );
		}
		if ( 'integer' === $field['type'] && ( ! preg_match( '/^(?:0|[1-9][0-9]{0,3})$/', $value ) || (int) $value > (int) $field['maximum'] ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Beaver Builder lieferte einen ungültigen globalen Zahlenwert.', 'wp-agent-companion' ), array( 'status' => 502, 'setting' => $name ) );
		}
		$result[ $name ] = $value;
	}
	return $result;
}

/** @return array<string,string>|WP_Error */
function wpagent_beaver_validate_global_settings_patch( $patch ) {
	if ( ! is_array( $patch ) || array() === $patch || count( $patch ) > count( wpagent_beaver_global_settings_field_catalog() ) ) {
		return new WP_Error( 'wpagent_beaver_global_settings_patch', __( 'Mindestens eine primitive Beaver-Builder-Einstellung ist erforderlich.', 'wp-agent-companion' ), array( 'status' => 400 ) );
	}
	$result = array();
	$catalog = wpagent_beaver_global_settings_field_catalog();
	foreach ( $patch as $name => $value ) {
		if ( ! is_string( $name ) || ! array_key_exists( $name, $catalog ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_unknown', __( 'Diese globale Beaver-Builder-Einstellung ist nicht im sicheren Allowlist-Ausschnitt enthalten.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => is_scalar( $name ) ? (string) $name : '' ) );
		}
		$field = $catalog[ $name ];
		if ( is_bool( $value ) ) {
			$value = $value ? '1' : '0';
		} elseif ( is_int( $value ) || is_float( $value ) ) {
			$value = (string) $value;
		}
		if ( ! is_string( $value ) || strlen( $value ) > 16 || preg_match( '/[<>\[\]]/', $value ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Der globale Beaver-Builder-Einstellungswert ist nicht primitiv oder nicht zulässig.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => $name ) );
		}
		if ( 'select' === $field['type'] && ! in_array( $value, $field['values'], true ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Der globale Beaver-Builder-Auswahlwert ist nicht zulässig.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => $name ) );
		}
		if ( 'integer' === $field['type'] && ( ! preg_match( '/^(?:0|[1-9][0-9]{0,3})$/', $value ) || (int) $value > (int) $field['maximum'] ) ) {
			return new WP_Error( 'wpagent_beaver_global_settings_invalid', __( 'Der globale Beaver-Builder-Zahlenwert ist nicht zulässig.', 'wp-agent-companion' ), array( 'status' => 400, 'setting' => $name ) );
		}
		$result[ $name ] = $value;
	}
	return $result;
}

/** @return WP_Error */
function wpagent_beaver_templates_recovery_required( string $kind, int $post_id = 0, string $revision_id = '', array $extra = array() ) {
	$data = array_merge( array( 'status' => 500, 'rollback_verified' => false, 'recovery_required' => true, 'kind' => $kind ), $extra );
	if ( $post_id > 0 ) {
		$data['post_id'] = $post_id;
	}
	if ( '' !== $revision_id ) {
		$data['revision_id'] = $revision_id;
	}
	return new WP_Error( 'wpagent_beaver_templates_recovery_required', __( 'Der Beaver-Builder-Schreibvorgang wurde nicht vollständig verifiziert. Es wurde kein automatisches Rollback ausgeführt. Lies den aktuellen Zustand manuell neu.', 'wp-agent-companion' ), $data );
}

/** @return array{revision_id:string,option:string}|WP_Error */
function wpagent_beaver_global_settings_revision( array $settings, string $hash ) {
	$revision_id = wp_generate_uuid4();
	$option      = WPAGENT_BEAVER_GLOBAL_SETTINGS_REVISION_PREFIX . 'latest';
	$record      = array( 'revision_id' => $revision_id, 'hash' => $hash, 'settings' => $settings, 'created_at' => time() );
	$stored = false === get_option( $option, false )
		? wpagent_companion_add_option_create_only( $option, $record, '', false )
		: update_option( $option, $record, false );
	if ( ! $stored ) {
		return new WP_Error( 'wpagent_beaver_global_settings_revision', __( 'Die sichere globale Beaver-Builder-Revision konnte nicht angelegt werden.', 'wp-agent-companion' ), array( 'status' => 500 ) );
	}
	return array( 'revision_id' => $revision_id, 'option' => $option );
}
