diff --git a/RELEASING.md b/RELEASING.md index 0aebebb..0452496 100644 --- a/RELEASING.md +++ b/RELEASING.md @@ -36,7 +36,7 @@ nothing to check. Start from `docker compose -f tests/compose.yml down -v` — a reused stack keeps the content of previous runs, which inflates the check and entry counts and makes a local number look unlike CI's. -0.8.3 (local, clean stack): 420 acceptance checks (same as 0.8.2) · 11 WPML checks · `npm run test:text` 74 checks +0.8.3 (local, clean stack): 427 acceptance checks (+7: empty title and empty required field) · 11 WPML checks · `npm run test:text` 74 checks (new: admin-only, minified and code-token exclusions, bulk decision) · `npm run test:coverage` passes · Plugin Check 0 errors / 0 warnings · `composer lint` 0 / 0 on 28 files · `contentrain validate` 14 models, 65 entries · canonical byte parity 55 files · handoff 13 · uninstall passes. diff --git a/includes/class-contentrain-bridge-jobs.php b/includes/class-contentrain-bridge-jobs.php index 0970c01..c6ace85 100644 --- a/includes/class-contentrain-bridge-jobs.php +++ b/includes/class-contentrain-bridge-jobs.php @@ -935,6 +935,9 @@ public static function review( $id, $decisions, $finish = false, $all = null ) { Models::file( $job, 'bridge/hardcoded-text.json', Policy::json( Text::document( $job['candidates'], $job['text']['errors'], $job['text']['counts'] ) ) ); } foreach ( $job['models'] as $model ) { + if ( ! empty( $job['derived'][ $model['id'] ] ) ) { + self::warning( $job, array( 'source' => 'model/' . $model['id'], 'reason' => 'empty-title-derived: ' . $job['derived'][ $model['id'] ] . ' record(s) had an empty title; a label was taken from the slug or id' ) ); + } Models::file( $job, '.contentrain/models/' . $model['id'] . '.json', Policy::json( $model, Policy::MODEL_ORDER ) ); if ( in_array( $model['kind'], array( 'collection', 'dictionary' ), true ) ) { foreach ( array_keys( $job['locales'] ) as $locale ) { diff --git a/includes/class-contentrain-bridge-models.php b/includes/class-contentrain-bridge-models.php index 64cca6c..961e5ba 100644 --- a/includes/class-contentrain-bridge-models.php +++ b/includes/class-contentrain-bridge-models.php @@ -87,6 +87,10 @@ public static function model( &$job, $id, $kind, $domain, $name, $fields, $title if ( 'dictionary' !== $kind && isset( $fields[ $title ] ) ) { $fields[ $title ]['required'] = true; } + // A field an earlier record found empty stays optional (see `filled()`). + foreach ( array_keys( $job['relaxed'][ $id ] ?? array() ) as $key ) { + unset( $fields[ $key ]['required'] ); + } $existing = $job['models'][ $id ] ?? null; if ( $existing ) { foreach ( $fields as $key => $definition ) { @@ -107,6 +111,32 @@ public static function model( &$job, $id, $kind, $domain, $name, $fields, $title } } + /** + * WordPress saves a record with no title, and ACF does not hold a draft, an + * import or an older row to a field made required later. An empty title gets + * a label from the record's slug or id, counted per model; any other + * required field that is empty is left out and stops being required, said + * once per field. Nothing is invented and no record is dropped: other + * records point at it. + */ + private static function filled( &$job, $model, $id, $data ) { + $m = $job['models'][ $model ]; + foreach ( (array) $m['fields'] as $name => $field ) { + if ( empty( $field['required'] ) || ( isset( $data[ $name ] ) && '' !== $data[ $name ] ) ) { + continue; + } + if ( $name === $m['title_field'] ) { + $data[ $name ] = (string) ( ( $data['source_slug'] ?? '' ) ?: ( isset( $data['wp_id'] ) ? '#' . $data['wp_id'] : '' ) ?: ( '' !== (string) $id ? '#' . $id : $m['name'] ) ); + $job['derived'][ $model ] = ( $job['derived'][ $model ] ?? 0 ) + 1; + continue; + } + unset( $data[ $name ], $job['models'][ $model ]['fields'][ $name ]['required'] ); + $job['relaxed'][ $model ][ $name ] = true; + Jobs::warning( $job, array( 'source' => 'model/' . $model . '.' . $name, 'reason' => 'required-value-empty: a record has no value for this required field, so the field is optional in this export' ) ); + } + return $data; + } + /** * A post's Contentrain status — wp-import's `mapStatus` rule for rule (`packages/wp-import/src/contentrain.ts`), * so Bridge's store and an imported one agree on what goes live (QA-23): @@ -144,6 +174,9 @@ public static function visibility( $raw ) { public static function entry( &$job, $model, $locale, $id, $data, $meta = null, $body = '' ) { $job['locales'][ $locale ] = true; + if ( is_array( $data ) && 'dictionary' !== $job['models'][ $model ]['kind'] ) { + $data = self::filled( $job, $model, $id, $data ); + } $m = $job['models'][ $model ]; // `url` fields are addresses, not content: the media record's own source // URL and a post's original permalink must survive relinking. Everything @@ -192,7 +225,7 @@ public static function post( &$job, $record ) { $kind = 'post' === $p['type'] ? 'document' : 'collection'; $domain = 'post' === $p['type'] ? 'blog' : 'site'; $fields = array( 'title' => array( 'type' => 'string', 'required' => true ), 'source_slug' => array( 'type' => 'string' ), 'source_url' => array( 'type' => 'url' ), 'excerpt' => array( 'type' => 'richtext' ), 'wp_id' => array( 'type' => 'integer' ) ); - $data = array( 'title' => wp_strip_all_tags( $p['title'] ) ?: '#' . $p['id'], 'source_slug' => $p['slug'], 'excerpt' => $p['excerpt'], 'wp_id' => $p['id'] ); + $data = array( 'title' => wp_strip_all_tags( $p['title'] ), 'source_slug' => $p['slug'], 'excerpt' => $p['excerpt'], 'wp_id' => $p['id'] ); if ( $p['link'] ) { $data['source_url'] = $p['link']; } diff --git a/includes/class-contentrain-bridge-validator.php b/includes/class-contentrain-bridge-validator.php index 57a5189..41622dd 100644 --- a/includes/class-contentrain-bridge-validator.php +++ b/includes/class-contentrain-bridge-validator.php @@ -65,7 +65,7 @@ private static function entry( $job, $model, $data, $locale ) { } $value = $data[ $name ]; if ( ! empty( $field['required'] ) && ( null === $value || '' === $value ) ) { - throw new \RuntimeException( 'Required content value is empty.' ); + throw new \RuntimeException( 'Required content value is empty: ' . $model['id'] . '.' . $name . ' (' . $locale . ')' ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped -- Diagnostic data is escaped at the admin output boundary or JSON encoded. } $valid = self::value( $job, $field, $value, $locale ); if ( ! $valid ) { diff --git a/readme.txt b/readme.txt index b919508..65ab2fc 100644 --- a/readme.txt +++ b/readme.txt @@ -95,6 +95,7 @@ The plugin stores export ownership and a source revision marker. It sends nothin * The content type list on the export screen shows each type's published count as a number ("Pages (7 published)", plus private ones when there are any). It used to join the per-status counts as text ("Pages (07010000000000)"). * Interface text only an administrator sees is no longer offered for review: an options panel (Redux, Kirki, a theme-options file), a bundled plugin installer (TGM), an `admin` directory, and minified scripts. JavaScript literals that are one lowercase word or a BEM class name ("fade", "collapsing", "pswp__img") are code. Each is still listed in bridge/hardcoded-text.json with its reason; the same words in a template a visitor sees stay a candidate. * "Include all undecided" and "Exclude all undecided" decide every text candidate still waiting, on every page, in one step. Menu labels keep their default. +* A record with an empty title, or an empty ACF field marked required, no longer stops the export with "Required content value is empty." WordPress saves both: a title-less post, attachment or menu link, and a private or draft post that ACF never held to its required field. An empty title gets a label from the record's slug or id, counted per model in bridge/warnings.json; an empty required field is left out, becomes optional in that model, and is named in the warnings. No record is dropped. When a required value is still empty, the message names the model, the field and the language. = 0.8.2 = diff --git a/tests/integration.php b/tests/integration.php index afa9bc5..b8bccc6 100644 --- a/tests/integration.php +++ b/tests/integration.php @@ -1074,6 +1074,20 @@ function rejects( $callback, $message ) { $broken = $job; $broken['tables'][ Models::content_path( $job, 'wp-authors', $address['locale'] ) ] = array(); rejects( static function () use ( $broken ) { Validator::run( $broken ); }, 'missing author relation fails validation' ); +// An empty required value still fails closed, and says where. +$empty_excerpt = $job; +$empty_message = ''; +foreach ( $empty_excerpt['models'] as $mid => $model ) { + if ( 'document' === $model['kind'] && isset( $model['fields']['excerpt'] ) ) { + $empty_excerpt['models'][ $mid ]['fields']['excerpt']['required'] = true; + } +} +try { + Validator::run( $empty_excerpt ); +} catch ( \RuntimeException $e ) { + $empty_message = $e->getMessage(); +} +check( (bool) preg_match( '/^Required content value is empty: [a-z0-9-]+\.excerpt \([a-z_A-Z-]+\)$/', $empty_message ), 'an empty required value names its model, field and locale' ); foreach ( $job['files'] as $path => $info ) { check( hash_file( 'sha256', Files::path( $dir, $path ) ) === $info['sha256'], 'file hash: ' . $path ); } @@ -1326,6 +1340,56 @@ function rejects( $callback, $message ) { update_user_meta( $admin->ID, 'contentrain_bridge_job_1', $id ); } +// WordPress saves a record with no title, and ACF does not hold a private or +// draft post to a required field (P1, 0.8.3): the export is ready, an empty +// title gets a label from the slug or id, and the empty field is optional. +register_post_type( 'bridge_note', array( 'public' => true, 'show_in_rest' => false, 'label' => 'Notes' ) ); +if ( $has_acf ) { + acf_add_local_field_group( array( + 'key' => 'group_bridge_note', + 'title' => 'Note fields', + 'location' => array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'bridge_note' ) ) ), + 'fields' => array( array( 'key' => 'field_bridge_note_lead', 'name' => 'lead', 'label' => 'Lead', 'type' => 'text', 'required' => 1 ) ), + ) ); +} +$note_titled = wp_insert_post( array( 'post_type' => 'bridge_note', 'post_title' => 'Bridge note', 'post_status' => 'publish', 'post_author' => $admin->ID ) ); +$note_empty = wp_insert_post( array( 'post_type' => 'bridge_note', 'post_title' => '', 'post_name' => '', 'post_status' => 'private', 'post_author' => $admin->ID ) ); +if ( $has_acf ) { + update_field( 'field_bridge_note_lead', 'A lead', $note_titled ); + update_field( 'field_bridge_note_lead', '', $note_empty ); +} +$untitled = wp_insert_attachment( array( 'post_mime_type' => 'image/png', 'post_title' => '', 'post_status' => 'inherit' ), $image_path ); +delete_user_meta( $admin->ID, 'contentrain_bridge_job_1' ); +$notes = Jobs::create( array( 'types' => array( 'bridge_note', 'attachment' ), 'private' => true, 'media_files' => false, 'scan_sources' => false ) ); +for ( $i = 0; $i < 300 && ! in_array( $notes['phase'], array( 'review', 'ready', 'failed' ), true ); ++$i ) { + $notes = Jobs::step( $notes['id'], $notes['step'] ); +} +if ( 'review' === $notes['phase'] ) { + $notes = Jobs::review( $notes['id'], array(), true ); + for ( $i = 0; $i < 300 && ! in_array( $notes['phase'], array( 'ready', 'failed' ), true ); ++$i ) { + $notes = Jobs::step( $notes['id'], $notes['step'] ); + } +} +check( 'ready' === $notes['phase'], 'an export with an untitled record and an empty required field is ready' ); +$notes_dir = Files::dir( $notes['id'] ) . '/output'; +$notes_warnings = array_column( json_decode( Files::read( $notes_dir, 'bridge/warnings.json' ), true ), 'reason', 'source' ); +$media_rows = json_decode( Files::read( $notes_dir, '.contentrain/content/assets/wp-media/data.json' ), true ); +$untitled_row = array_values( array_filter( $media_rows, static function ( $row ) use ( $untitled ) { return $untitled === ( $row['wp_id'] ?? null ); } ) ); +check( 1 === count( $untitled_row ) && '#' . $untitled === $untitled_row[0]['title'], 'an untitled attachment is kept, labelled by its id' ); +check( 0 === strpos( $notes_warnings['model/wp-media'] ?? '', 'empty-title-derived: ' ), 'and the export counts the derived labels' ); +$note_model = json_decode( Files::read( $notes_dir, '.contentrain/models/wp-bridge-note.json' ), true ); +check( ! empty( $note_model['fields']['title']['required'] ), 'a derived title stays required' ); +if ( $has_acf ) { + $lead = array_values( array_filter( array_keys( $note_model['fields'] ), static function ( $name ) { return false !== strpos( $name, 'lead' ); } ) ); + check( 1 === count( $lead ) && empty( $note_model['fields'][ $lead[0] ]['required'] ), 'a required ACF field one record left empty is optional in the model' ); + check( isset( $notes_warnings[ 'model/wp-bridge-note.' . $lead[0] ] ), 'and the export names that field' ); +} +Files::remove( Files::dir( $notes['id'] ) ); +wp_delete_post( $note_titled, true ); +wp_delete_post( $note_empty, true ); +wp_delete_post( $untitled, true ); +update_user_meta( $admin->ID, 'contentrain_bridge_job_1', $id ); + // Snapshot consistency check on a separate job, preserving the finished artifact for external validation. delete_user_meta( $admin->ID, 'contentrain_bridge_job_1' ); $other = Jobs::create( array( 'types' => array( 'page' ) ) );