Skip to content

fix: repair legacy embed className via WP-CLI - #149

Merged
kraftbj merged 2 commits into
WordPress:trunkfrom
faisalahammad:fix/131-repair-legacy-embed-classname
Oct 20, 2026
Merged

kraftbj merged 2 commits into
WordPress:trunkfrom
faisalahammad:fix/131-repair-legacy-embed-classname

Conversation

@faisalahammad

Copy link
Copy Markdown
Contributor

Summary

Posts saved by Press This before PR #129 have core/embed blocks whose <figure> className is missing wp-block-embed-{provider} (e.g. wp-block-embed-youtube). Gutenberg's save() output for that block always includes this class when type/providerNameSlug attrs are set, so the mismatch fails block validation and shows "Block contains unexpected or invalid content" in the standard editor.

Adds a WP-CLI command that finds and repairs this specific mismatch in stored post content.

Fixes #131

Changes

includes/class-press-this-embed-repair.php (new)

Before:

// no repair mechanism existed

After:

class Press_This_Embed_Repair {
	public static function repair_content( $content ) {
		// parse_blocks(), find core/embed blocks with type + providerNameSlug
		// but missing wp-block-embed-{provider} in the figure class, append it,
		// serialize_blocks() back. Returns false if nothing needed fixing.
	}
}

Why: Isolated, pure content-rewrite logic that's easy to unit test without a live WP-CLI environment. Validates providerNameSlug against sanitize_html_class() before it touches HTML, since that value comes from stored post content, not a trusted source.

includes/class-press-this-cli-commands.php (new)

Before:

// no CLI commands existed in the plugin

After:

class Press_This_CLI_Commands {
	public function repair_embeds( $args, $assoc_args ) {
		// wp press-this repair-embeds [--dry-run] [--post-type=<type>]
		// batches posts via $wpdb LIKE query on post_content, pre-filters
		// with has_block(), repairs, wp_update_post()s only real changes
	}
}

Why: Explicit, auditable repair path with dry-run support, rather than silently rewriting content on every page load.

press-this-plugin.php

Before:

add_action( 'init', 'press_this_load_textdomain' );

After:

add_action( 'init', 'press_this_load_textdomain' );

if ( defined( 'WP_CLI' ) && WP_CLI ) {
	require_once __DIR__ . '/includes/class-press-this-cli-commands.php';
	WP_CLI::add_command( 'press-this', 'Press_This_CLI_Commands' );
}

Why: Registers the command only in a WP-CLI context, no overhead on regular requests.

Testing

Test 1: Legacy embed fails validation before repair

  1. Create a post with <figure class="wp-block-embed is-type-video is-provider-youtube"> (missing the 4th class)
  2. Open it in the standard editor
    Result: "Block contains unexpected or invalid content" shown, as reported in the issue

Test 2: Repair fixes it

  1. Run wp press-this repair-embeds --dry-run, confirm it lists the post
  2. Run wp press-this repair-embeds
  3. Reload the post in the standard editor
    Result: embed renders normally, no validation warning. Re-running the command reports 0 posts repaired (idempotent).

Test 3: Unaffected content is left alone

  1. Run the command against a post created after PR Fix YouTube and Vimeo embed previews not showing in editor #129, or one using the generic embed shape with no type/providerNameSlug attrs (the PHP get_suggested_content() path)
    Result: not touched, since neither is broken

Also ran full PHP unit suite (283 tests, all passing) and composer php:lint / composer php:compatibility clean.

faisalahammad and others added 2 commits July 12, 2026 00:09
Posts saved by Press This before PR WordPress#129 have core/embed blocks
missing the wp-block-embed-{provider} class in their figure element.
This fails Gutenberg block validation in the standard editor, showing
"Block contains unexpected or invalid content" instead of the embed.

Adds a WP-CLI command, wp press-this repair-embeds, that scans posts
for this exact mismatch and rewrites the content to match what
core/embed's save() output expects. Supports --dry-run and
--post-type.

Fixes WordPress#131
@kraftbj
kraftbj added this pull request to the merge queue Oct 20, 2026
Merged via the queue into WordPress:trunk with commit c11d17b Oct 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Existing posts with buggy embed className stay invalid in the standard editor

2 participants