Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_shortcode( 'gb_find_errors', function() {
- if ( ! current_user_can( 'manage_options' ) ) {
- return 'You do not have permission to run this scan.';
- }
- // Adjust 'numberposts' if you have a massive amount of content to avoid timeouts
- // or set to -1 to scan everything (may timeout on huge sites).
- $posts = get_posts( [
- 'post_type' => 'any',
- 'post_status' => 'publish',
- 'numberposts' => -1,
- 's' => 'generateblocks', // Only fetch posts containing GB to save resources
- ] );
- $output = '<h3>GenerateBlocks Scan Results</h3>';
- $found_issues = false;
- foreach ( $posts as $post ) {
- $blocks = parse_blocks( $post->post_content );
- if ( gb_has_broken_block( $blocks ) ) {
- $found_issues = true;
- $edit_link = get_edit_post_link( $post->ID );
- $output .= sprintf(
- '<p><strong>Issue found in:</strong> <a href="%s" target="_blank">%s</a> (ID: %d)</p>',
- $edit_link,
- $post->post_title,
- $post->ID
- );
- }
- }
- if ( ! $found_issues ) {
- $output .= '<p>No blocks with missing uniqueId found.</p>';
- }
- return $output;
- } );
- function gb_has_broken_block( $blocks ) {
- foreach ( $blocks as $block ) {
- // Check GB Container and Grid blocks
- if ( isset( $block['blockName'] ) && in_array( $block['blockName'], [ 'generateblocks/container', 'generateblocks/grid' ] ) ) {
- // Check if uniqueId attribute is missing
- if ( ! isset( $block['attrs']['uniqueId'] ) ) {
- return true;
- }
- }
- // Recursively check inner blocks
- if ( ! empty( $block['innerBlocks'] ) ) {
- if ( gb_has_broken_block( $block['innerBlocks'] ) ) {
- return true;
- }
- }
- }
- return false;
- }
Advertisement
Add Comment
Please, Sign In to add comment