171 lines
7.0 KiB
PHP
171 lines
7.0 KiB
PHP
<?php
|
|
/**
|
|
* Reversible ownership helpers for the local fixture environment.
|
|
*
|
|
* @package CHRNX
|
|
*/
|
|
|
|
/** Return every WordPress option owned temporarily by the visual fixture environment. */
|
|
function chrnx_fixture_environment_option_names(): array {
|
|
return array( 'sticky_posts', 'blogname', 'blogdescription', 'show_on_front', 'page_on_front', 'page_for_posts', 'permalink_structure', 'chrnx_subscribe_page_id' );
|
|
}
|
|
|
|
/** Return exact persisted bytes and existence for one option. */
|
|
function chrnx_fixture_environment_raw_option( string $name ): array {
|
|
global $wpdb;
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Exact persisted bytes are required for reversible local fixtures.
|
|
$raw = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) );
|
|
return array(
|
|
'exists' => null !== $raw,
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- Binary-safe storage of exact local option bytes, not executable code.
|
|
'raw_base64' => null === $raw ? null : base64_encode( $raw ),
|
|
);
|
|
}
|
|
|
|
/** Find one post owned by a stable fixture key. */
|
|
function chrnx_fixture_environment_find_post( string $fixture_key, string $post_type ): int {
|
|
$ids = get_posts(
|
|
array(
|
|
'post_type' => $post_type,
|
|
'post_status' => 'any',
|
|
'posts_per_page' => 1,
|
|
'fields' => 'ids',
|
|
'no_found_rows' => true,
|
|
'meta_key' => '_chrnx_fixture_key',
|
|
'meta_value' => $fixture_key,
|
|
)
|
|
);
|
|
return empty( $ids ) ? 0 : absint( $ids[0] );
|
|
}
|
|
|
|
/** Capture exact outer state once. */
|
|
function chrnx_fixture_environment_snapshot(): bool {
|
|
$snapshot_name = '_chrnx_fixture_environment_snapshot_v1';
|
|
if ( is_array( get_option( $snapshot_name, false ) ) ) {
|
|
return false;
|
|
}
|
|
$options = array();
|
|
foreach ( chrnx_fixture_environment_option_names() as $name ) {
|
|
$options[ $name ] = chrnx_fixture_environment_raw_option( $name );
|
|
}
|
|
$subscribe_id = chrnx_fixture_environment_find_post( 'page-subscribe', 'page' );
|
|
update_option(
|
|
$snapshot_name,
|
|
array(
|
|
'version' => 2,
|
|
'options' => $options,
|
|
'subscribe' => array(
|
|
'exists' => $subscribe_id > 0,
|
|
'id' => $subscribe_id,
|
|
'status' => $subscribe_id > 0 ? get_post_status( $subscribe_id ) : null,
|
|
),
|
|
),
|
|
false
|
|
);
|
|
return true;
|
|
}
|
|
|
|
/** Restore one raw option row exactly. */
|
|
function chrnx_fixture_environment_restore_raw_option( string $name, array $state ): void {
|
|
global $wpdb;
|
|
if ( ! empty( $state['exists'] ) ) {
|
|
// phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode -- Binary-safe restoration of exact local option bytes, not executable code.
|
|
$raw = base64_decode( (string) ( $state['raw_base64'] ?? '' ), true );
|
|
if ( ! is_string( $raw ) ) {
|
|
throw new InvalidArgumentException( 'Fixture option snapshot contains invalid bytes.' );
|
|
}
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Exact raw restoration is required for reversible local fixtures.
|
|
$wpdb->query( $wpdb->prepare( "INSERT INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no') ON DUPLICATE KEY UPDATE option_value = VALUES(option_value)", $name, $raw ) );
|
|
} else {
|
|
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- Exact option nonexistence is required for reversible local fixtures.
|
|
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s", $name ) );
|
|
}
|
|
}
|
|
|
|
/** Synchronize the live permalink structure, optionally preserving an exact raw option row. */
|
|
function chrnx_fixture_environment_sync_permalink_structure( string $permalink_structure, ?array $raw_state = null ): void {
|
|
global $wp_rewrite;
|
|
$wp_rewrite->set_permalink_structure( $permalink_structure );
|
|
if ( is_array( $raw_state ) ) {
|
|
chrnx_fixture_environment_restore_raw_option( 'permalink_structure', $raw_state );
|
|
wp_cache_delete( 'permalink_structure', 'options' );
|
|
}
|
|
flush_rewrite_rules( false );
|
|
}
|
|
|
|
/** Restore the captured environment and remove its snapshot. */
|
|
function chrnx_fixture_environment_restore(): bool {
|
|
$snapshot_name = '_chrnx_fixture_environment_snapshot_v1';
|
|
$snapshot = get_option( $snapshot_name, null );
|
|
if ( ! is_array( $snapshot ) || ! is_array( $snapshot['options'] ?? null ) ) {
|
|
return false;
|
|
}
|
|
if ( 2 === ( $snapshot['version'] ?? null ) ) {
|
|
foreach ( $snapshot['options'] as $name => $state ) {
|
|
if ( is_string( $name ) && is_array( $state ) ) {
|
|
chrnx_fixture_environment_restore_raw_option( $name, $state );
|
|
}
|
|
}
|
|
} elseif ( 1 === ( $snapshot['version'] ?? null ) ) {
|
|
foreach ( $snapshot['options'] as $name => $state ) {
|
|
if ( ! is_string( $name ) || ! is_array( $state ) ) {
|
|
continue;
|
|
}
|
|
if ( ! empty( $state['exists'] ) ) {
|
|
update_option( $name, $state['value'] ?? null );
|
|
} else {
|
|
delete_option( $name );
|
|
}
|
|
}
|
|
} else {
|
|
return false;
|
|
}
|
|
$subscribe = $snapshot['subscribe'] ?? array();
|
|
if ( ! empty( $subscribe['exists'] ) && get_post( absint( $subscribe['id'] ?? 0 ) ) ) {
|
|
wp_update_post(
|
|
array(
|
|
'ID' => absint( $subscribe['id'] ),
|
|
'post_status' => sanitize_key( $subscribe['status'] ?? 'publish' ),
|
|
)
|
|
);
|
|
}
|
|
delete_option( '_chrnx_fixture_featured_limit' );
|
|
delete_option( $snapshot_name );
|
|
wp_cache_flush();
|
|
$permalink_raw_state = 2 === ( $snapshot['version'] ?? null ) && is_array( $snapshot['options']['permalink_structure'] ?? null )
|
|
? $snapshot['options']['permalink_structure']
|
|
: null;
|
|
chrnx_fixture_environment_sync_permalink_structure( (string) get_option( 'permalink_structure', '' ), $permalink_raw_state );
|
|
return true;
|
|
}
|
|
|
|
/** Apply one fixture-owned presentation without replacing unrelated sticky posts. */
|
|
function chrnx_fixture_environment_apply( array $config ): void {
|
|
$fixture_ids = array_map( 'absint', $config['fixture_ids'] ?? $config['featured_ids'] ?? array() );
|
|
$featured_ids = array_map( 'absint', $config['featured_ids'] ?? array() );
|
|
$featured_count = max( 0, absint( $config['featured_count'] ?? count( $featured_ids ) ) );
|
|
$current = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );
|
|
$unrelated = array_values( array_diff( $current, $fixture_ids ) );
|
|
update_option( 'sticky_posts', array_values( array_unique( array_merge( $unrelated, array_slice( $featured_ids, 0, $featured_count ) ) ) ) );
|
|
foreach ( array( 'blogname', 'blogdescription', 'show_on_front', 'page_on_front', 'page_for_posts' ) as $name ) {
|
|
if ( array_key_exists( $name, $config ) ) {
|
|
update_option( $name, $config[ $name ] );
|
|
}
|
|
}
|
|
if ( array_key_exists( 'permalink_structure', $config ) ) {
|
|
chrnx_fixture_environment_sync_permalink_structure( (string) $config['permalink_structure'] );
|
|
}
|
|
$subscribe_id = absint( $config['subscribe_id'] ?? 0 );
|
|
if ( $subscribe_id > 0 && isset( $config['subscribe_status'] ) ) {
|
|
wp_update_post(
|
|
array(
|
|
'ID' => $subscribe_id,
|
|
'post_status' => sanitize_key( $config['subscribe_status'] ),
|
|
)
|
|
);
|
|
}
|
|
if ( array_key_exists( 'subscribe_available', $config ) ) {
|
|
update_option( 'chrnx_subscribe_page_id', $config['subscribe_available'] ? $subscribe_id : 0 );
|
|
}
|
|
}
|