fix: isolate CHRNX local integration state

This commit is contained in:
chrnx committed 2026-08-12 09:48:12 -05:00
1 parent 21da6da227
commit 6e93c0ad20
16 files changed
+536 -65

No files matched your search

+20 -9
View File
@@ -16,20 +16,31 @@ if [ "$chrnx_image_output" = "/" ] || [ "$chrnx_image_output" = "$chrnx_image_ro
echo "Refusing broad image output path: $chrnx_image_output" >&2
exit 2
fi
if [ -L "$chrnx_image_output" ]; then
echo "Refusing symbolic-link image output: $chrnx_image_output" >&2
exit 2
fi
if [ "$chrnx_image_output" != "$chrnx_image_default_output" ]; then
chrnx_image_safe_tmp=${TMPDIR:-/tmp}
chrnx_image_safe_tmp=${chrnx_image_safe_tmp%/}
case "$chrnx_image_output" in
"$chrnx_image_safe_tmp"/chrnx-*) ;;
*)
echo "Temporary image output must be an existing chrnx-* directory under $chrnx_image_safe_tmp." >&2
exit 2
;;
esac
if [ ! -d "$chrnx_image_output" ]; then
echo "Temporary image output does not exist: $chrnx_image_output" >&2
exit 2
fi
chrnx_image_safe_tmp=$(CDPATH= cd -P -- "${TMPDIR:-/tmp}" && pwd)
chrnx_image_output_parent=$(dirname -- "$chrnx_image_output")
chrnx_image_output_name=$(basename -- "$chrnx_image_output")
chrnx_image_output_parent=$(CDPATH= cd -P -- "$chrnx_image_output_parent" && pwd)
case "$chrnx_image_output_name" in
chrnx-*) ;;
*)
echo "Temporary image output basename must start with chrnx-." >&2
exit 2
;;
esac
if [ "$chrnx_image_output_parent" != "$chrnx_image_safe_tmp" ]; then
echo "Temporary image output must be a direct child of $chrnx_image_safe_tmp." >&2
exit 2
fi
chrnx_image_output=$(CDPATH= cd -P -- "$chrnx_image_output" && pwd)
fi
chrnx_image_tmp=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-images.XXXXXX")
trap 'rm -rf -- "$chrnx_image_tmp"' EXIT HUP INT TERM
+83 -2
View File
@@ -12,7 +12,7 @@ if ( ! in_array( $chrnx_seed_host, array( 'localhost', '127.0.0.1' ), true ) ) {
WP_CLI::error( 'Refusing to seed a non-local WordPress site.' );
}
$chrnx_seed_scenario = isset( $args[0] ) && is_string( $args[0] ) ? $args[0] : 'default';
$chrnx_seed_scenarios = array( 'default', 'featured-1', 'featured-2', 'featured-3', 'subscribe-unpublished', 'subscribe-unavailable' );
$chrnx_seed_scenarios = array( 'default', 'featured-1', 'featured-2', 'featured-3', 'subscribe-unpublished', 'subscribe-unavailable', 'restore' );
if ( ! in_array( $chrnx_seed_scenario, $chrnx_seed_scenarios, true ) ) {
WP_CLI::error( 'Unknown local fixture scenario.' );
}
@@ -62,6 +62,78 @@ function chrnx_seed_find_post( string $fixture_key, string $post_type ): int {
return empty( $ids ) ? 0 : absint( $ids[0] );
}
/** Return an option value together with whether the row exists. */
function chrnx_seed_get_option_state( string $name ): array {
$sentinel = new stdClass();
$value = get_option( $name, $sentinel );
return array(
'exists' => $sentinel !== $value,
'value' => $sentinel === $value ? null : $value,
);
}
/** Capture local configuration once before applying the visual fixture environment. */
function chrnx_seed_snapshot_environment(): void {
$snapshot_name = '_chrnx_fixture_environment_snapshot_v1';
if ( is_array( get_option( $snapshot_name, false ) ) ) {
return;
}
$options = array();
foreach ( array( 'sticky_posts', 'blogname', 'blogdescription', 'show_on_front', 'page_on_front', 'page_for_posts', 'permalink_structure', 'chrnx_subscribe_page_id' ) as $name ) {
$options[ $name ] = chrnx_seed_get_option_state( $name );
}
$subscribe_id = chrnx_seed_find_post( 'page-subscribe', 'page' );
$snapshot = array(
'version' => 1,
'options' => $options,
'subscribe' => array(
'exists' => $subscribe_id > 0,
'id' => $subscribe_id,
'status' => $subscribe_id > 0 ? get_post_status( $subscribe_id ) : null,
),
);
update_option( $snapshot_name, $snapshot, false );
}
/** Restore the exact local configuration captured before fixture ownership began. */
function chrnx_seed_restore_environment(): void {
$snapshot_name = '_chrnx_fixture_environment_snapshot_v1';
$snapshot = get_option( $snapshot_name, null );
if ( ! is_array( $snapshot ) || 1 !== ( $snapshot['version'] ?? null ) || ! is_array( $snapshot['options'] ?? null ) ) {
WP_CLI::log( 'CHRNX_FIXTURES_RESTORE snapshot=none' );
return;
}
foreach ( $snapshot['options'] as $name => $state ) {
if ( ! is_string( $name ) || ! is_array( $state ) || ! array_key_exists( 'exists', $state ) ) {
WP_CLI::error( 'Fixture environment snapshot is malformed.' );
}
if ( $state['exists'] ) {
update_option( $name, $state['value'] ?? null );
} else {
delete_option( $name );
}
}
$subscribe = $snapshot['subscribe'] ?? array();
if ( ! empty( $subscribe['exists'] ) ) {
$subscribe_id = absint( $subscribe['id'] ?? 0 );
if ( $subscribe_id > 0 && 'page-subscribe' === get_post_meta( $subscribe_id, '_chrnx_fixture_key', true ) ) {
wp_update_post(
array(
'ID' => $subscribe_id,
'post_status' => sanitize_key( $subscribe['status'] ?? 'publish' ),
)
);
}
}
delete_option( '_chrnx_fixture_featured_limit' );
delete_option( $snapshot_name );
flush_rewrite_rules( false );
WP_CLI::log( 'CHRNX_FIXTURES_RESTORE snapshot=restored' );
}
/**
* Insert or update one fixture-owned post without touching unrelated content.
*
@@ -97,6 +169,12 @@ function chrnx_seed_upsert_post( array $fixture, string $post_type, int $offset
return absint( $result );
}
if ( 'restore' === $chrnx_seed_scenario ) {
chrnx_seed_restore_environment();
return;
}
chrnx_seed_snapshot_environment();
chrnx_seed_editorial_terms();
$chrnx_seed_pages = chrnx_seed_read_fixture( 'pages.json' );
$chrnx_seed_posts = chrnx_seed_read_fixture( 'posts.json' );
@@ -172,7 +250,10 @@ if ( preg_match( '/^featured-([123])$/', $chrnx_seed_scenario, $chrnx_seed_match
} else {
delete_option( '_chrnx_fixture_featured_limit' );
}
update_option( 'sticky_posts', array_slice( $chrnx_seed_sticky_ids, 0, $chrnx_seed_featured_count ) );
$chrnx_seed_current_stickies = array_map( 'absint', (array) get_option( 'sticky_posts', array() ) );
$chrnx_seed_unrelated_stickies = array_values( array_diff( $chrnx_seed_current_stickies, array_values( $chrnx_seed_post_ids ) ) );
$chrnx_seed_selected_stickies = array_slice( $chrnx_seed_sticky_ids, 0, $chrnx_seed_featured_count );
update_option( 'sticky_posts', array_values( array_unique( array_merge( $chrnx_seed_unrelated_stickies, $chrnx_seed_selected_stickies ) ) ) );
update_option( 'blogname', 'CHRNX' );
update_option( 'blogdescription', 'Ideas made tangible.' );
update_option( 'show_on_front', 'page' );
+1 -1
View File
@@ -7,7 +7,7 @@ chrnx_docker_cli_dir=${CHRNX_DOCKER_CLI_DIR:-/opt/homebrew/opt/docker/bin}
chrnx_seed_scenario=${1:-default}
case "$chrnx_seed_scenario" in
default|featured-1|featured-2|featured-3|subscribe-unpublished|subscribe-unavailable) ;;
default|featured-1|featured-2|featured-3|subscribe-unpublished|subscribe-unavailable|restore) ;;
*)
echo "Unknown local fixture scenario: $chrnx_seed_scenario" >&2
exit 2