Skip to content

Sensei_Course_Completed_Actions_Block: Run filter only on core/button blocks#7860

Merged
m1r0 merged 3 commits intotrunkfrom
reish20251103/reduce-scope-completed-actions-filter
Nov 4, 2025
Merged

Sensei_Course_Completed_Actions_Block: Run filter only on core/button blocks#7860
m1r0 merged 3 commits intotrunkfrom
reish20251103/reduce-scope-completed-actions-filter

Conversation

@mreishus
Copy link
Contributor

@mreishus mreishus commented Nov 3, 2025

Proposed Changes

  • The Sensei_Course_Completed_Actions_Block runs a filter on all block renders, but it calls into update_button_block_url which is only concerned with one block type, enforced by a guard clause at the top:
	public static function update_button_block_url( $block_content, $block, $class_name, $url ): string {
		if (
			! isset( $block['blockName'] )
			|| 'core/button' !== $block['blockName']
			|| ! isset( $block['attrs']['className'] )
			|| false === strpos( $block['attrs']['className'], $class_name )
		) {
			return $block_content;
		}

Because it's only concerned with core/button, and because WordPress gives us a more specific filter: (https://github.com/WordPress/wordpress-develop/blob/26230c33261cf559907a90547b4827ea66ff9f30/src/wp-includes/class-wp-block.php#L666)

		$block_content = apply_filters( 'render_block', $block_content, $this->parsed_block, $this );
		$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this );

Let's reduce the scope of this filter so it only runs on the button blocks we are concerned with.

Note that this has the side effect of reducing 100+ duplicate SQL queries on (internal url). This seems to be a separate problem, but reducing of the scope of this filter is a pretty big mitigation, since it stops a query from running on every block render.

Testing Instructions

New/Updated Hooks

Deprecated Code

Pre-Merge Checklist

  • PR title and description contain sufficient detail and accurately describe the changes
  • Adheres to coding standards (PHP, JavaScript, CSS, HTML)
  • All strings are translatable (without concatenation, handles plurals)
  • Follows our naming conventions (P6rkRX-4oA-p2)
  • Hooks (p6rkRX-1uS-p2) and functions are documented
  • New UIs are responsive and use a mobile-first approach
  • Code is tested on the minimum supported PHP and WordPress versions

Copilot AI review requested due to automatic review settings November 3, 2025 20:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR optimizes the rendering filter for the Course Completed Actions Block by changing from the generic render_block filter to the block-specific render_block_core/button filter. This improves performance by only executing the callback for button blocks instead of all block types.

  • Changed filter hook from render_block to render_block_core/button for better performance
  • Maintains the same functionality while reducing unnecessary function calls

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

*/
public function __construct() {
add_filter( 'render_block', [ $this, 'update_more_courses_button_url' ], 10, 2 );
add_filter( 'render_block_core/button', [ $this, 'update_more_courses_button_url' ], 10, 2 );
Copy link

Copilot AI Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The render_block_core/button filter passes 3 parameters (block content, block array, and WP_Block instance), but only 2 are specified here. While the current implementation only needs 2 parameters, this is inconsistent with the pattern used elsewhere in the codebase (see class-sensei-page-blocks.php line 28 which uses 3 parameters). Consider updating to accept 3 parameters for consistency and future extensibility: add_filter( 'render_block_core/button', [ $this, 'update_more_courses_button_url' ], 10, 3 );

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's fine, Copilot. Don't worry about it.

@mreishus mreishus requested a review from m1r0 November 3, 2025 20:25
@mreishus
Copy link
Contributor Author

mreishus commented Nov 3, 2025

@m1r0 - can you help with this? This is my first time working on Sensei, so I'm not sure of the process. Just wanted to help out because I saw 100+ duplicate queries on ( internal url ).

@m1r0 m1r0 added this to the 4.25.2 milestone Nov 4, 2025
@m1r0 m1r0 added the Hooks This change adds or modifies one or more hooks. label Nov 4, 2025
@m1r0 m1r0 merged commit d4828e3 into trunk Nov 4, 2025
21 checks passed
@m1r0 m1r0 deleted the reish20251103/reduce-scope-completed-actions-filter branch November 4, 2025 14:58
@m1r0
Copy link
Member

m1r0 commented Nov 4, 2025

All makes sense, I just updated the changelog to make it more user-facing! Thanks for the help, @mreishus! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Hooks This change adds or modifies one or more hooks.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants