Files
chrnx-dev-wp/scripts/seed-the-no-names-local.php
chrnx 76917decd0 feat(editorial): widen sections and add local track pages
Unify the editorial canvas across section pages, keep release tracks out of Journal, and give each song a listening surface with audio, metadata, platform links, sharing, and optional featured art. Document and seed the local The No Names fixture without changing production data.
2026-08-27 20:24:39 -05:00

275 lines
9.2 KiB
PHP

<?php
/** Local-only The No Names importer. Never run against a public host. */
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals, WordPress.WP.GlobalVariablesOverride.Prohibited, WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents, WordPress.WP.AlternativeFunctions.file_system_operations_file_put_contents
$host = wp_parse_url( home_url( '/' ), PHP_URL_HOST );
if ( ! in_array( $host, array( 'localhost', '127.0.0.1' ), true ) ) {
WP_CLI::error( 'Refusing to seed a non-local WordPress site.' );
}
$source_root = '/var/www/html/wp-content/suno-source/discs';
$releases = array(
'el-hermoso-caos' => array(
'name' => 'El Hermoso Caos que habita en mi cabeza',
'source' => 'el-hermoso-caos-que-habita-en-mi-cerebro',
'cover' => 'cover-3000.jpg',
'track_count' => 13,
),
'ando-bien-arriba' => array(
'name' => 'Ando Bien Arriba',
'source' => 'ando-bien-arriba',
'cover' => 'cover-art-v1-1200.jpg',
'track_count' => 6,
),
'aqui-se-rompio-una-jerga' => array(
'name' => 'Aquí se Rompió una Jerga',
'source' => 'aqui-se-rompio-una-jerga',
'cover' => 'cover-art-v1-1200.jpg',
'track_count' => 6,
),
'side-b' => array(
'name' => 'Side B',
'source' => 'el-hermoso-caos-que-habita-en-mi-cerebro/side-b',
'cover' => 'cover-art-v1-1200.jpg',
'track_count' => 19,
),
);
function chrnx_local_frontmatter( string $path ): array {
$text = (string) file_get_contents( $path );
if ( ! preg_match( '/\A---\s*\R(.*?)\R---/s', $text, $match ) ) {
return array();
}
$data = array();
foreach ( preg_split( '/\R/', $match[1] ) as $line ) {
if ( preg_match( '/^([a-z_]+):\s*(.*)$/', $line, $field ) ) {
$data[ $field[1] ] = trim( trim( $field[2] ), "\"'" );
}
}
return $data;
}
function chrnx_local_concept( string $path ): string {
$text = (string) file_get_contents( $path );
if ( preg_match( '/^## Concept\s*\R(.*?)(?=^## |\z)/ms', $text, $match ) ) {
$paragraphs = preg_split( '/\R\s*\R/', trim( $match[1] ) );
return trim( preg_replace( '/[`*_]/', '', (string) reset( $paragraphs ) ) );
}
return '';
}
function chrnx_local_lyrics( string $path ): string {
$text = (string) file_get_contents( $path );
if ( preg_match( '/^## Lyrics\s*\R(.*?)(?=^## |\z)/ms', $text, $match ) ) {
return trim( $match[1] );
}
return '';
}
function chrnx_local_cover( string $path, string $key ): int {
$existing = get_posts(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => 1,
'fields' => 'ids',
'meta_key' => '_chrnx_fixture_key',
'meta_value' => $key,
)
);
if ( ! empty( $existing ) ) {
return absint( $existing[0] );
}
$uploads = wp_upload_dir();
$name = sanitize_file_name( basename( $path ) );
$target = trailingslashit( $uploads['path'] ) . $name;
wp_mkdir_p( dirname( $target ) );
file_put_contents( $target, file_get_contents( $path ) );
$type = wp_check_filetype( $name );
$id = wp_insert_attachment(
array(
'post_mime_type' => $type['type'] ?? 'image/jpeg',
'post_title' => sanitize_text_field( pathinfo( $name, PATHINFO_FILENAME ) ),
'post_status' => 'inherit',
),
$target
);
update_post_meta( $id, '_chrnx_fixture_key', $key );
return absint( $id );
}
function chrnx_local_attachment( string $path, string $key ): int {
$existing = get_posts(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'posts_per_page' => 1,
'fields' => 'ids',
'meta_key' => '_chrnx_fixture_key',
'meta_value' => $key,
)
);
if ( ! empty( $existing ) ) {
return absint( $existing[0] );
}
$uploads = wp_upload_dir();
$name = sanitize_file_name( basename( $path ) );
$target = trailingslashit( $uploads['path'] ) . $key . '-' . $name;
wp_mkdir_p( dirname( $target ) );
file_put_contents( $target, file_get_contents( $path ) );
$type = wp_check_filetype( $name );
$id = wp_insert_attachment(
array(
'post_mime_type' => $type['type'] ?? 'audio/mp4',
'post_title' => sanitize_text_field( pathinfo( $name, PATHINFO_FILENAME ) ),
'post_status' => 'inherit',
),
$target
);
update_post_meta( $id, '_chrnx_fixture_key', $key );
return absint( $id );
}
foreach ( get_posts(
array(
'post_type' => 'post',
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
'meta_key' => '_chrnx_fixture_key',
'meta_value' => 'the-no-names-',
'meta_compare' => 'LIKE',
)
) as $old_id ) {
wp_delete_post( $old_id, true );
}
$old_release_posts = get_posts(
array(
'post_type' => 'post',
'post_status' => 'any',
'posts_per_page' => -1,
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'chrnx_release',
'field' => 'slug',
'terms' => array_keys( $releases ),
),
),
)
);
foreach ( $old_release_posts as $old_id ) {
wp_delete_post( $old_id, true );
}
foreach ( $releases as $slug => $release ) {
$source_dir = trailingslashit( $source_root ) . $release['source'];
$disc_path = trailingslashit( $source_dir ) . 'disc.md';
$term = get_term_by( 'slug', $slug, 'chrnx_release' );
if ( ! $term instanceof WP_Term ) {
$insert = wp_insert_term( $release['name'], 'chrnx_release', array( 'slug' => $slug ) );
$term = get_term( $insert['term_id'] );
}
$description = is_readable( $disc_path ) ? chrnx_local_concept( $disc_path ) : 'Material experimental y alterno de The No Names: pruebas, desvíos y canciones que todavía están buscando dónde caer.';
$cover_id = 0;
wp_update_term(
$term->term_id,
'chrnx_release',
array(
'name' => $release['name'],
'description' => $description,
)
);
update_term_meta( $term->term_id, 'chrnx_release_status', 'in production' );
foreach ( array(
'suno' => 'https://suno.com/playlist/local-the-no-names',
'spotify' => 'https://open.spotify.com/album/local-the-no-names',
'apple' => 'https://music.apple.com/us/album/local-the-no-names',
) as $platform => $url ) {
update_term_meta( $term->term_id, 'chrnx_release_url_' . $platform, $url );
}
$cover_path = trailingslashit( $source_dir ) . $release['cover'];
if ( is_readable( $cover_path ) ) {
$cover_id = chrnx_local_cover( $cover_path, 'the-no-names-cover-' . $slug );
update_term_meta( $term->term_id, 'chrnx_release_cover', $cover_id );
}
$track_dirs = glob( trailingslashit( $source_dir ) . '[0-9][0-9]-*', GLOB_ONLYDIR );
if ( 'side-b' === $slug ) {
$track_dirs = glob( trailingslashit( $source_dir ) . '[0-9][0-9]-*', GLOB_ONLYDIR );
}
foreach ( $track_dirs as $track_dir ) {
$song_path = trailingslashit( $track_dir ) . 'song.md';
if ( ! is_readable( $song_path ) ) {
$alternate_song = trailingslashit( $track_dir ) . 'song-hibrido.md';
if ( is_readable( $alternate_song ) ) {
$song_path = $alternate_song;
}
}
if ( ! is_readable( $song_path ) ) {
continue;
}
$meta = chrnx_local_frontmatter( $song_path );
$num = 0;
if ( preg_match( '/\A(\d+)-/', basename( $track_dir ), $number_match ) ) {
$num = absint( $number_match[1] );
}
if ( 0 === $num ) {
continue;
}
$key = 'the-no-names-' . $slug . '-' . str_pad( (string) $num, 2, '0', STR_PAD_LEFT );
$posts = get_posts(
array(
'post_type' => 'post',
'post_status' => 'any',
'posts_per_page' => 1,
'fields' => 'ids',
'meta_key' => '_chrnx_fixture_key',
'meta_value' => $key,
)
);
$post = array(
'post_title' => sanitize_text_field( $meta['name'] ?? basename( $track_dir ) ),
'post_name' => sanitize_title( $slug . '-' . $num . '-' . ( $meta['name'] ?? basename( $track_dir ) ) ),
'post_status' => 'publish',
'post_type' => 'post',
'post_content' => wp_kses_post( chrnx_local_lyrics( $song_path ) ),
);
if ( ! empty( $posts ) ) {
$post['ID'] = absint( $posts[0] );
}
$post_id = wp_insert_post( $post, true );
update_post_meta( $post_id, '_chrnx_fixture_key', $key );
update_post_meta( $post_id, 'chrnx_track_number', $num );
wp_set_object_terms( $post_id, $slug, 'chrnx_release', false );
wp_set_object_terms( $post_id, 'music', 'chrnx_topic', false );
wp_set_object_terms( $post_id, 'es', 'chrnx_language', false );
$description = chrnx_local_concept( $song_path );
if ( '' !== $description ) {
update_post_meta( $post_id, 'chrnx_track_description', $description );
}
foreach ( array(
'suno' => 'https://suno.com/song/local-' . $key,
'spotify' => 'https://open.spotify.com/track/local-' . $key,
'apple' => 'https://music.apple.com/us/song/local-' . $key,
) as $platform => $url ) {
update_post_meta( $post_id, 'chrnx_track_url_' . $platform, $url );
}
$audio_path = trailingslashit( $track_dir ) . 'song.mp3';
if ( ! is_readable( $audio_path ) ) {
$audio_path = trailingslashit( $track_dir ) . 'song.m4a';
}
if ( is_readable( $audio_path ) ) {
$audio_id = chrnx_local_attachment( $audio_path, $key . '-audio' );
update_post_meta( $post_id, 'chrnx_track_audio', $audio_id );
}
if ( ! empty( $cover_id ) ) {
set_post_thumbnail( $post_id, $cover_id );
}
}
}
flush_rewrite_rules( false );
WP_CLI::success( 'The No Names local fixture loaded: 4 releases with local test platform URLs.' );
// phpcs:enable