Skip to content

Commit

Permalink
seek through the document as a string instead of parsing document blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
draganescu committed Jan 17, 2024
1 parent 6974af2 commit bf8dcdb
Showing 1 changed file with 61 additions and 39 deletions.
100 changes: 61 additions & 39 deletions lib/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,44 +58,69 @@ function gutenberg_menu() {
}
add_action( 'admin_menu', 'gutenberg_menu', 9 );

if ( ! function_exists( 'recursively_find_block_by_attribute' ) ) {
if ( ! function_exists( 'html_contains_block' ) ) {
/**
* Recursively find a block by attribute.
*
* @param array $blocks The blocks to search in.
* @param string $block_name The block type to search for.
* @param mixed $attribute_name The attribute name to search for.
* @param mixed $attribute_value The attribute value to search for.
* @param array $found_blocks The found blocks.
* @param string $html The html to search in.
* @param string $block_name The block type to search for.
* @param string $attribute_name The attribute name to search for.
* @param string $attribute_value The attribute value to search for.
*
* @return array Found blocks
* @return bool True if block is found, false otherwise
*/
function recursively_find_block_by_attribute( $blocks, $block_name, $attribute_name, $attribute_value, $found_blocks = array() ) {
foreach ( $blocks as $block ) {
if (
$block['blockName'] === $block_name &&
isset( $block['attrs'][ $attribute_name ] ) &&
$attribute_value === $block['attrs'][ $attribute_name ]
) {
$found_blocks[] = $block;
}
if ( $block['innerBlocks'] ) {
$found_blocks = array_merge(
$found_blocks,
recursively_find_block_by_attribute(
$block['innerBlocks'],
$block_name,
$attribute_name,
$attribute_value,
$found_blocks
)
);
}
function html_contains_block( $html, $block_name, $attribute_name = null, $attribute_value = null ) {
$at = 0;

Check failure on line 73 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 2 tabs, found 1

/**

Check failure on line 75 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 2 tabs, found 1
* This is the same regex as the one used in the block parser.
* It is better to use this solution to look for a block's existence
* in a document compared to having to parsing the blocks in the
* document, avoiding all the performance drawbacks of achieving
* a full representation of block content just to check if one block
* is there.
*/
$pattern = sprintf(

Check failure on line 83 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 2 tabs, found 1
'~<!--\s+?wp:%s\s+(?P<attrs>{(?:(?:[^}]+|}+(?=})|(?!}\s+/?-->).)*+)?}\s+)?/?-->~s',
// @TODO: we could ensure that there's a namespace on the block name.
preg_quote( $block_name, '~' )
);

Check failure on line 87 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 2 tabs, found 1

while ( false !== preg_match( $pattern, $html, $matches, PREG_OFFSET_CAPTURE, $at ) ) {

Check failure on line 89 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 2 tabs, found 1

// bail if no matches

Check failure on line 91 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected at least 3 tabs, found 2
if ( empty ($matches) ) {

Check failure on line 92 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Line indented incorrectly; expected 3 tabs, found 2

Check failure on line 92 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Space before opening parenthesis of function call prohibited

Check failure on line 92 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 92 in lib/init.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found
return false;
}

$at = $matches[0][1] + strlen( $matches[0][0] );

if ( ! isset( $attribute_name ) ) {
return true;
}

// Don't parse JSON if it's not possible for the attribute to exist.
if ( ! str_contains( $matches['attrs'][0], "\"{$attribute_name}\":" ) ) {
continue;
}

$attrs = json_decode( $matches['attrs'][0], /* as-associative */ true );
if ( ! isset( $attrs[ $attribute_name ] ) ) {
continue;
}

if ( ! isset( $attribute_value ) ) {
return true;
}

if ( $attribute_value === $attrs[ $attribute_name ] ) {
return true;
}
return $found_blocks;
}
}

return false;
}
}
if ( ! function_exists( 'get_template_parts_that_use_menu' ) ) {
/**
* Get all template parts that use a menu.
Expand All @@ -116,15 +141,12 @@ function get_template_parts_that_use_menu( $wp_navigation_id ) {
// with the ref attribute set to $wp_navigation_id.
$wp_template_part_posts_with_navigation = array();
foreach ( $wp_template_part_posts as $wp_template_part_post ) {
$wp_template_part_blocks = parse_blocks( $wp_template_part_post->post_content );
$found_avigation = count(
recursively_find_block_by_attribute(
$wp_template_part_blocks,
'core/navigation',
'ref',
$wp_navigation_id
)
) > 0;
$found_avigation = html_contains_block(
$wp_template_part_post->post_content,
'navigation',
'ref',
$wp_navigation_id
);
if ( $found_avigation ) {
$wp_template_part_posts_with_navigation[] = $wp_template_part_post->ID;
}
Expand Down

0 comments on commit bf8dcdb

Please sign in to comment.