[wp-trac] [WordPress Trac] #59969: Conditional loading `build_template_part_block_variations` for performance improvement

WordPress Trac noreply at wordpress.org
Wed Nov 29 18:29:32 UTC 2023


#59969: Conditional loading `build_template_part_block_variations` for performance
improvement
--------------------------+------------------------------
 Reporter:  thekt12       |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  General       |     Version:
 Severity:  normal        |  Resolution:
 Keywords:  has-patch     |     Focuses:  performance
--------------------------+------------------------------

Comment (by thekt12):

 == register_block_core_template_part
 `register_block_core_template_part` registers template parts from
 `block.json` and the custom template created from the editor side.
 You can find the list of templates on the local instance via the following
 link ->
 [http://localhost:8889/wp-admin/site-
 editor.php?path=%2Fwp_template_part%2Fall] (change the base domain asper
 your env)

 This function is mostly needed to register header and footer templates for
 the page and is in use by both frontend and backend.

 The function makes a call to `register_block_type_from_metadata` which
 registers the settings( to brief it is a function that combines data
 obtained from JSON files with the settings provided during its call and
 registers it as a `WP_Block_Type` which is used at many places).

 Specifically, In `register_block_core_template_part` we are passing on the
 instance and area variation(detailed in the next section) obtained from
 `build_template_part_block_variations` (which is an expensive function)

 == build_template_part_block_variations()
 build_template_part_block_variations is a combination of two functions
 summarized below:
 **$instance_variations =
 build_template_part_block_instance_variations();**
 - It gets all the wp_template_parts (check the link above for template
 listing local)

 **$area_variations     = build_template_part_block_area_variations(
 $instance_variations );**
 - It gets the area which has some variation
 - Some improvements and bugs were found in this function ( noted towards
 last )

 == Where the registered block type variation is used (Improvement)?
 Searching for places where variations in block type were used it was found
 only inside ->
 [https://github.com/WordPress/wordpress-
 develop/blob/fdec26724691d64c58e4cfe17557ec5ef00bc0bd/src/wp-
 admin/includes/post.php#L2284 get_block_editor_server_block_settings()]
 on [https://github.com/WordPress/wordpress-
 develop/blob/fdec26724691d64c58e4cfe17557ec5ef00bc0bd/src/wp-
 admin/includes/post.php#L2318 line 2318] which is an editor-centric
 function and is not needed in the frontend.

 This aligns with the fact that it will have no effect on the frontend if
 removed -
 [https://github.com/WordPress/gutenberg/issues/45601#issuecomment-1516214955
 build block variation only for editor]


 Opened a PR for the same - [https://github.com/WordPress/wordpress-
 develop/pull/5718 PR#5718]
 Other improvements found during research
 - We must remove variables each time we hit one in the loop -
 [https://github.com/WordPress/wordpress-
 develop/blob/fdec26724691d64c58e4cfe17557ec5ef00bc0bd/src/wp-
 includes/blocks/template-part.php#L183 line 183]
 - Also, the whole looping is to get the area which has some variation. I
 feel there should be a better way than looping.

 == Possible chance of bug
 ^ (I am not 100% sure here, just speaking as per what the code says) ^
 The scope attribute in the final outcome is highly dependent on the first
 variation that we encounter - [https://github.com/WordPress/wordpress-
 develop/blob/fdec26724691d64c58e4cfe17557ec5ef00bc0bd/src/wp-
 includes/blocks/template-part.php#L189 line 189]. However, the variations
 can be added from the editor and there maybe two variations in an area
 with different scope and this can lead to ambiguity as it takes the scope
 of first variation it encounters.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/59969#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list