Last active
February 5, 2025 04:52
-
-
Save robertuniqid/a9495256768ea6eebf443a4fd86eaa9a to your computer and use it in GitHub Desktop.
ACF – Bypassing restrictions to allow HTML rendering in Gutenberg Blocks on the frontend.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Context : emergency project where there are hundreds of broken pages; not sure why, I came to fix it | |
// This is just one small part, yet the most hackish, I've had to do, because Gutenberg scripts the entire $attributes | |
// On the block before it reached the `acf_render_block_callback` | |
add_filter( 'block_parser_class', function() { | |
return 'X_WP_Block_Parser'; | |
}); | |
add_action( 'wp_head', function() { | |
if( is_admin() ) | |
return; | |
$all_registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered(); | |
foreach( $all_registered_blocks as $registered_block ) { | |
if(!str_starts_with($registered_block->name, 'acf/')) | |
continue; | |
$registered_block->render_callback = 'x_hijack_acf_render_block_callback'; | |
} | |
} ); | |
function x_hijack_acf_render_block_callback( $attributes, $content = '', $wp_block = null ) { | |
$current_position_from_parse = X_ACF_Raw_Data::read_current_position(); | |
if(empty($attributes['name']) && !empty($current_position_from_parse)) { | |
$attributes = $current_position_from_parse; | |
} | |
return acf_render_block_callback( $attributes, $content, $wp_block ); | |
} | |
class X_WP_Block_Parser extends WP_Block_Parser { | |
public function parse($document) { | |
if (!str_contains($document, 'wp:acf/')) { | |
return parent::parse($document); | |
} | |
$pattern = '/<!--\s*wp:acf\/[a-zA-Z0-9_-]+\s*({.*?})\s*\/-->/s'; | |
// Perform the regex match | |
preg_match_all($pattern, $document, $matches, PREG_SET_ORDER); | |
if (empty($matches)) { | |
return parent::parse($document); | |
} | |
foreach( $matches as $match ) { | |
$raw_json = $match[1]; | |
$data = json_decode( trim( $raw_json ), true ); | |
if (json_last_error() !== JSON_ERROR_NONE) { | |
X_ACF_Raw_Data::add( null ); | |
continue; | |
} | |
X_ACF_Raw_Data::add( $data ); | |
} | |
// Return the parsed document after replacements | |
return parent::parse($document); | |
} | |
} | |
class X_ACF_Raw_Data { | |
public static $by_position_data = []; | |
public static $_read_data_position = 0; | |
public static function add( $data ) { | |
self::$by_position_data[] = $data; | |
} | |
public static function read_current_position() { | |
if(isset(self::$by_position_data[self::$_read_data_position])) { | |
return self::$by_position_data[self::$_read_data_position++]; | |
} | |
self::$by_position_data[self::$_read_data_position] = 0; | |
return self::$by_position_data[self::$_read_data_position++]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment