static function ( $attributes ) use ( $type ) { return Embed::render( $type, $attributes, true ); }, ) ); } } /** * Sanitize snapshot URLs on save, including nested blocks and REST/classic saves. * Leave content byte-identical when no snapshot needs changing. * * @param string $content Slashed post content from content_save_pre. */ public static function sanitize_content( string $content ): string { if ( false === strpos( $content, 'wp:showfm/' ) ) { return $content; } $blocks = parse_blocks( wp_unslash( $content ) ); $changed = false; $blocks = self::sanitize_snapshots( $blocks, $changed ); return $changed ? wp_slash( serialize_blocks( $blocks ) ) : $content; } /** * Apply the same URL policy to every authored snapshot, including inner blocks. * * @param array> $blocks Parsed blocks. * @param bool $changed Whether any snapshot was changed. * @return array> */ private static function sanitize_snapshots( array $blocks, bool &$changed ): array { foreach ( $blocks as &$block ) { if ( in_array( $block['blockName'], array( 'showfm/player', 'showfm/episodes', 'showfm/play', 'showfm/transcript' ), true ) && is_array( $block['attrs']['snapshot'] ?? null ) ) { $snapshot = Attributes::snapshot( $block['attrs']['snapshot'] ); if ( $snapshot !== $block['attrs']['snapshot'] ) { $block['attrs']['snapshot'] = $snapshot; $changed = true; } } $block['innerBlocks'] = self::sanitize_snapshots( $block['innerBlocks'], $changed ); } return $blocks; } }