> Fixture records. */ function chrnx_seed_read_fixture( string $filename ): array { $path = get_theme_file_path( 'tests/fixtures/' . $filename ); if ( ! is_readable( $path ) ) { WP_CLI::error( 'Fixture is not readable: ' . $filename ); } // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- This is a local mapped theme fixture, never a URL. $data = json_decode( (string) file_get_contents( $path ), true ); if ( ! is_array( $data ) || ! array_is_list( $data ) ) { WP_CLI::error( 'Fixture JSON is invalid: ' . $filename ); } return $data; } /** * Find the post managed by one stable fixture key. * * @param string $fixture_key Fixture key. * @param string $post_type Post type. * @return int Post ID or zero. */ /** * Insert or update one fixture-owned post without touching unrelated content. * * @param array $fixture Fixture record. * @param string $post_type Post type. * @param int $offset Stable publication-date offset. * @return int Post ID. */ function chrnx_seed_upsert_post( array $fixture, string $post_type, int $offset ): int { $key = isset( $fixture['fixtureKey'] ) ? sanitize_key( $fixture['fixtureKey'] ) : ''; if ( '' === $key ) { WP_CLI::error( 'Every fixture requires a fixtureKey.' ); } $post_id = chrnx_fixture_environment_find_post( $key, $post_type ); $payload = array( 'ID' => $post_id, 'post_type' => $post_type, 'post_status' => sanitize_key( $fixture['status'] ?? 'publish' ), 'post_title' => sanitize_text_field( $fixture['title'] ?? '' ), 'post_name' => sanitize_title( $fixture['slug'] ?? $fixture['title'] ?? $key ), 'post_excerpt' => wp_kses_post( $fixture['excerpt'] ?? '' ), 'post_content' => wp_kses_post( $fixture['content'] ?? '' ), 'post_date' => gmdate( 'Y-m-d H:i:s', strtotime( '2026-08-01 18:00:00 UTC' ) + ( $offset * HOUR_IN_SECONDS ) ), ); $result = $post_id ? wp_update_post( $payload, true ) : wp_insert_post( $payload, true ); if ( is_wp_error( $result ) ) { WP_CLI::error( 'Could not upsert fixture ' . $key . '.' ); } update_post_meta( $result, '_chrnx_fixture_key', $key ); return absint( $result ); } /** Return the option that stores temporary unrelated-post statuses. */ function chrnx_seed_related_status_snapshot_name(): string { return '_chrnx_fixture_related_unowned_status_snapshot_v1'; } /** * Capture every eligible unrelated post before a Related Stories scenario. * * @return array Exact records from the first scenario. */ function chrnx_seed_snapshot_related_unowned_statuses(): array { $snapshot_name = chrnx_seed_related_status_snapshot_name(); $snapshot = get_option( $snapshot_name, false ); if ( is_array( $snapshot ) && 1 === ( $snapshot['version'] ?? null ) && is_array( $snapshot['posts'] ?? null ) ) { return $snapshot['posts']; } $candidate_ids = get_posts( array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'meta_query' => array( array( 'key' => '_chrnx_fixture_key', 'compare' => 'NOT EXISTS', ), ), ) ); $records = array(); foreach ( $candidate_ids as $candidate_id ) { $candidate_id = absint( $candidate_id ); if ( chrnx_is_eligible_editorial_post( $candidate_id ) ) { $records[] = array( 'id' => $candidate_id, 'status' => (string) get_post_status( $candidate_id ), ); } } update_option( $snapshot_name, array( 'version' => 1, 'posts' => $records, ), false ); return $records; } /** Temporarily draft the exact unrelated posts captured for a Related Stories scenario. */ function chrnx_seed_draft_related_unowned_posts(): void { foreach ( chrnx_seed_snapshot_related_unowned_statuses() as $record ) { $post_id = absint( $record['id'] ?? 0 ); if ( $post_id <= 0 || ! get_post( $post_id ) ) { continue; } $result = wp_update_post( array( 'ID' => $post_id, 'post_status' => 'draft', ), true ); if ( is_wp_error( $result ) ) { WP_CLI::error( 'Could not isolate an unrelated Related Stories fixture candidate.' ); } } } /** Restore exact unrelated-post statuses after a Related Stories scenario. */ function chrnx_seed_restore_related_unowned_statuses(): bool { $snapshot_name = chrnx_seed_related_status_snapshot_name(); $snapshot = get_option( $snapshot_name, false ); if ( ! is_array( $snapshot ) ) { return false; } if ( 1 !== ( $snapshot['version'] ?? null ) || ! is_array( $snapshot['posts'] ?? null ) ) { WP_CLI::error( 'Related Stories fixture snapshot is invalid.' ); } foreach ( $snapshot['posts'] as $record ) { $post_id = absint( $record['id'] ?? 0 ); $status = sanitize_key( $record['status'] ?? '' ); if ( $post_id <= 0 || ! get_post( $post_id ) ) { continue; } if ( ! array_key_exists( $status, get_post_stati() ) ) { WP_CLI::error( 'Related Stories fixture snapshot contains an invalid post status.' ); } $result = wp_update_post( array( 'ID' => $post_id, 'post_status' => $status, ), true ); if ( is_wp_error( $result ) ) { WP_CLI::error( 'Could not restore an unrelated Related Stories fixture candidate.' ); } } delete_option( $snapshot_name ); return true; } if ( 'restore' === $chrnx_seed_scenario ) { chrnx_seed_restore_related_unowned_statuses(); WP_CLI::log( chrnx_fixture_environment_restore() ? 'CHRNX_FIXTURES_RESTORE snapshot=restored' : 'CHRNX_FIXTURES_RESTORE snapshot=none' ); return; } if ( 'default' === $chrnx_seed_scenario ) { chrnx_seed_restore_related_unowned_statuses(); } chrnx_fixture_environment_snapshot(); chrnx_seed_editorial_terms(); $chrnx_seed_pages = chrnx_seed_read_fixture( 'pages.json' ); $chrnx_seed_posts = chrnx_seed_read_fixture( 'posts.json' ); $chrnx_seed_page_ids = array(); $chrnx_seed_post_ids = array(); $chrnx_seed_keys = array_map( 'sanitize_key', array_merge( array_column( $chrnx_seed_pages, 'fixtureKey' ), array_column( $chrnx_seed_posts, 'fixtureKey' ) ) ); $chrnx_seed_removed = 0; $chrnx_seed_owned_ids = get_posts( array( 'post_type' => array( 'page', 'post' ), 'post_status' => 'any', 'posts_per_page' => -1, 'fields' => 'ids', 'no_found_rows' => true, 'meta_key' => '_chrnx_fixture_key', ) ); foreach ( $chrnx_seed_owned_ids as $chrnx_seed_owned_id ) { $chrnx_seed_owned_key = sanitize_key( get_post_meta( $chrnx_seed_owned_id, '_chrnx_fixture_key', true ) ); if ( preg_match( '/^(?:page|post)-[a-z0-9-]+$/', $chrnx_seed_owned_key ) && ! in_array( $chrnx_seed_owned_key, $chrnx_seed_keys, true ) ) { wp_delete_post( $chrnx_seed_owned_id, true ); ++$chrnx_seed_removed; } } foreach ( $chrnx_seed_pages as $chrnx_seed_index => $chrnx_seed_page ) { $chrnx_seed_page_id = chrnx_seed_upsert_post( $chrnx_seed_page, 'page', $chrnx_seed_index ); $chrnx_seed_page_ids[ $chrnx_seed_page['slug'] ] = $chrnx_seed_page_id; } foreach ( $chrnx_seed_posts as $chrnx_seed_index => $chrnx_seed_post ) { $chrnx_seed_post_id = chrnx_seed_upsert_post( $chrnx_seed_post, 'post', 24 + $chrnx_seed_index ); wp_set_object_terms( $chrnx_seed_post_id, sanitize_key( $chrnx_seed_post['topic'] ?? '' ), 'chrnx_topic', false ); wp_set_object_terms( $chrnx_seed_post_id, sanitize_key( $chrnx_seed_post['language'] ?? '' ), 'chrnx_language', false ); if ( isset( $chrnx_seed_post['show'] ) ) { wp_set_object_terms( $chrnx_seed_post_id, sanitize_key( $chrnx_seed_post['show'] ), 'chrnx_show', false ); } else { wp_set_object_terms( $chrnx_seed_post_id, array(), 'chrnx_show', false ); } foreach ( array( 'chrnx_episode_number', 'chrnx_episode_duration', 'canonical_episode_url' ) as $chrnx_seed_meta_key ) { delete_post_meta( $chrnx_seed_post_id, $chrnx_seed_meta_key ); } if ( isset( $chrnx_seed_post['episodeNumber'] ) ) { update_post_meta( $chrnx_seed_post_id, 'chrnx_episode_number', absint( $chrnx_seed_post['episodeNumber'] ) ); } if ( isset( $chrnx_seed_post['episodeDuration'] ) ) { update_post_meta( $chrnx_seed_post_id, 'chrnx_episode_duration', chrnx_sanitize_episode_duration( $chrnx_seed_post['episodeDuration'] ) ); } if ( isset( $chrnx_seed_post['canonicalEpisodeUrl'] ) ) { update_post_meta( $chrnx_seed_post_id, 'canonical_episode_url', chrnx_sanitize_canonical_episode_url( $chrnx_seed_post['canonicalEpisodeUrl'] ) ); } $chrnx_seed_post_ids[ $chrnx_seed_post['fixtureKey'] ] = $chrnx_seed_post_id; } if ( preg_match( '/^related-([124])$/', $chrnx_seed_scenario, $chrnx_seed_related_match ) ) { chrnx_seed_draft_related_unowned_posts(); $chrnx_seed_related_count = absint( $chrnx_seed_related_match[1] ); $chrnx_seed_current_key = 'post-literature-long'; $chrnx_seed_related_candidates = array_values( array_diff( array_keys( $chrnx_seed_post_ids ), array( $chrnx_seed_current_key ) ) ); $chrnx_seed_published_keys = array_merge( array( $chrnx_seed_current_key ), array_slice( $chrnx_seed_related_candidates, 0, $chrnx_seed_related_count ) ); foreach ( $chrnx_seed_post_ids as $chrnx_seed_post_key => $chrnx_seed_owned_post_id ) { $chrnx_seed_status_result = wp_update_post( array( 'ID' => $chrnx_seed_owned_post_id, 'post_status' => in_array( $chrnx_seed_post_key, $chrnx_seed_published_keys, true ) ? 'publish' : 'draft', ), true ); if ( is_wp_error( $chrnx_seed_status_result ) ) { WP_CLI::error( 'Could not apply Related Stories fixture status.' ); } } } $chrnx_seed_sticky_ids = array(); foreach ( $chrnx_seed_posts as $chrnx_seed_post ) { if ( ! empty( $chrnx_seed_post['featured'] ) && isset( $chrnx_seed_post_ids[ $chrnx_seed_post['fixtureKey'] ] ) ) { $chrnx_seed_sticky_ids[] = $chrnx_seed_post_ids[ $chrnx_seed_post['fixtureKey'] ]; } } $chrnx_seed_featured_count = 3; if ( preg_match( '/^featured-([123])$/', $chrnx_seed_scenario, $chrnx_seed_match ) ) { $chrnx_seed_featured_count = absint( $chrnx_seed_match[1] ); update_option( '_chrnx_fixture_featured_limit', $chrnx_seed_featured_count ); } else { delete_option( '_chrnx_fixture_featured_limit' ); } chrnx_fixture_environment_apply( array( 'fixture_ids' => array_values( $chrnx_seed_post_ids ), 'featured_ids' => $chrnx_seed_sticky_ids, 'featured_count' => $chrnx_seed_featured_count, 'blogname' => 'CHRNX', 'blogdescription' => 'Ideas made tangible.', 'show_on_front' => 'page', 'page_on_front' => $chrnx_seed_page_ids['home'], 'page_for_posts' => 0, 'permalink_structure' => '/%postname%/', 'subscribe_id' => $chrnx_seed_page_ids['subscribe'], 'subscribe_status' => 'subscribe-unpublished' === $chrnx_seed_scenario ? 'draft' : 'publish', 'subscribe_available' => 'subscribe-unavailable' !== $chrnx_seed_scenario, ) ); WP_CLI::log( sprintf( 'CHRNX_FIXTURES pages=%d posts=%d removed=%d front=%d scenario=%s', count( $chrnx_seed_page_ids ), count( $chrnx_seed_post_ids ), $chrnx_seed_removed, $chrnx_seed_page_ids['home'], $chrnx_seed_scenario ) );