Programmatically removing an ACF block – issue with creating Gutenberg wp-image blocks

Programmatically removing an ACF block – issue with creating Gutenberg wp-image blocks


I inherited a site where the previous programmer harbored a serious hatred for Gutenberg and did his utmost to cripple it. I’m trying to clean up the mess left behind now that I’ve stabilized the size and have it under e2e testing.

One of its ACF blocks has a WYSIWYG text field. That’s it. So I need to extract the content from this block and transform it into Gutenberg blocks.

    public function remove_text_container_block( ) {

    // First, get the posts. We'll grab blocks of 100. 
    // The bash script will use the output to determine
    // if it needs to recall the command. That's outside
    // The scope of this post.
    $query = new \WP_Query([
      'post_type'      => 'any',
      'posts_per_page' => 100,
      's'              => 'acf/text-container',
    ]);

    $total = 0;

    while ( $query->have_posts() ) {
      $query->the_post();
      $post_id = get_the_ID();
      $content = get_post_field( 'post_content', $post_id );

      $textContainers = [];

      // This will fetch our target blocks.
      preg_match_all('//', $content, $textContainers);

      if ( empty( $textContainers[0] ) ) {
        continue;
      } else {
        $changed = 0;
        foreach ( $textContainers[0] as $textContainer ) {
          $changed++;

          // We need to parse the block which, among other things,
          // 

tags where the user's input is just a line return. $innerContent = apply_filters('the_content', $textContainer); // Wrap common HTML elements with corresponding Gutenberg block comments $innerContent = preg_replace('/

(.*)<\/p>/U', "

$1

", $innerContent); $innerContent = preg_replace('/
    (.*)<\/ul>/U', "

    $1

    ", $innerContent); $innerContent = preg_replace('/
  • (.*)<\/li>/U', "

    $1

    ", $innerContent); $innerContent = preg_replace('/(.*)<\/h(\d)>/U', "$2", $innerContent); // All of the above is working beautifully. // The trouble is images... $innerContent = preg_replace_callback('/

    ()<\/p>/U', function($matches) { $parser = new HTML5(); // Using Masterminds/HTML5 library from Composer. $doc = $parser->loadHTML($matches[1]); $img = $doc->getElementsByTagName('img')[0]; $class = $img->getAttribute('class'); $id = preg_match_one('/wp-image-(\d+)/', $class); $size = preg_match_one('/size-([a-z]+)/', $class); $src = $img->getAttribute('src'); $alt = $img->getAttribute('alt'); $figureClass = str_replace('wp-image-' . $id, '', $class); return "\n

    \n"; }, $innerContent); // Having done the above we replace the ACF block with the // Gutenberg blocks $content = str_replace( $textContainer, $innerContent, $content ); } } wp_update_post([ 'ID' => $post_id, 'post_content' => $content, ]); $total += $changed; \WP_CLI::log( "Updated post ID: $post_id, changed $changed, total $total" ); } \WP_CLI::success( "Removed text-container blocks from $total posts." ); wp_reset_postdata(); }

This works correctly for the frontend. The admin dashboard thinks the image block is malformed and returns an error. I’ve been going over this all morning and I’m at my wits end. When I use the Gutenberg editor to insert a new image, I get something like this:

""

I managed to get my conversion to fully match this, but I still get an error in the admin dashboard. Do I need to update something in post_meta or something like that?

#Programmatically #removing #ACF #block #issue #creating #Gutenberg #wpimage #blocks

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *