Skip to content

FSE - infinity loop when search_template_hierarchy is empty #31652

@Chrico

Description

@Chrico

FSE causes following error:

Xdebug has detected a possible infinite loop, and aborted your script with a stack depth of '256' frames

with following Stack:

#85 /var/www/wp-content/plugins/gutenberg/lib/full-site-editing/template-loader.php(48): get_search_template()
#86 /var/www/wp-content/plugins/gutenberg/lib/full-site-editing/template-loader.php(152): get_template_hierarchy('search')
#87 /var/www/wp-content/plugins/gutenberg/lib/full-site-editing/template-loader.php(66): gutenberg_resolve_template('search', Array)
#88 /var/www/wp-includes/class-wp-hook.php(292): gutenberg_override_query_template('', 'search', Array)
#89 /var/www/wp-includes/plugin.php(212): WP_Hook->apply_filters('', Array)
#90 /var/www/wp-includes/template.php(63): apply_filters('search_template', '', 'search', Array)
#91 /var/www/wp-includes/template.php(471): get_query_template('search')

Description

Our Plugin hooks into the search and replaces the template. As "micro"-optimization we added following code:

add_filter('search_template_hierarchy', '__return_empty_array', PHP_INT_MAX);

This will clean up the template hierarchy and skips a few file_exists-call from WordPress core, because we just want 1 single search template which we know it exists.

Following problem occurs:

  1. Gutenberg hooks into search_template
  2. which tries to resolve in gutenberg_override_query_template() the $current_template.
  3. when the $template_hierachy is empty, Gutenberg tries to call get_template_hierarchy( $template_type )
  4. which calls afterwards get_search_template(),
  5. which calls internally get_query_template()
  6. which finally gets resetted again by our filter above to an empty array.
  7. And last but not least... in get_query_template() the search_template-filter (see 1.) is applied again.

An infinity loop is born. :)

WordPress information

  • WordPress version: 5.7.1
  • Gutenberg version: 10.5.4
  • Are all plugins except Gutenberg deactivated? yes (expect the Plugin with a single filter from above)
  • Are you using a default theme (e.g. Twenty Twenty-One)? yes

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions