Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance and simplify usage of block_has_support() by supporting a string #4958

Closed

Conversation

kt-12
Copy link
Member

@kt-12 kt-12 commented Aug 3, 2023

Trac ticket: https://core.trac.wordpress.org/ticket/58532


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Thanks @kt-12 for the PR. Solid start. Left few suggestion.

One instance is missing here https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-supports/border.php#L25 that need to update as well. Thanks.

Comment on lines 1262 to 1264
} else {
$block_support = isset( $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] : $default_value;
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
} else {
$block_support = isset( $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] : $default_value;
}
} elseif ( isset( $block_type->supports[ $feature ] ) ) {
$block_support = $block_type->supports[ $feature ];
}

The default value already set at starting of the function so i will use elseif.

Copy link
Member

Choose a reason for hiding this comment

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

We should also add here another condition based on @swissspidy's feedback in https://core.trac.wordpress.org/ticket/58532#comment:4.

/**
* @ticket 58532
*/
public function test_block_has_support_string_true() {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of having two separate tests, I will use a single test with a data provider to test different scenarios. This approach allows me to efficiently test multiple cases using different data inputs within a single test method.


	/**
	 * Data provider for test_block_has_support_string
	 */
	public function data_block_has_support_string() {
		return array(
			array(
				array(),
				'color',
				false,
			),
			array(
				array(
					'supports' => array(
						'align'    => array( 'wide', 'full' ),
						'fontSize' => true,
						'color'    => array(
							'link'     => true,
							'gradient' => false,
						),
					),
				),
				'align',
				true,
			),
			array(
				array(
					'supports' => array(
						'align'    => array( 'wide', 'full' ),
						'fontSize' => true,
						'color'    => array(
							'link'     => true,
							'gradient' => false,
						),
					),
				),
				'fontSize',
				true,
			),
			array(
				array(
					'supports' => array(
						'align'    => array( 'wide', 'full' ),
						'fontSize' => false,
						'color'    => array(
							'link'     => true,
							'gradient' => false,
						),
					),
				),
				'fontSize',
				false,
			),
			array(
				array(
					'supports' => array(
						'align'    => array( 'wide', 'full' ),
						'fontSize' => true,
						'color'    => array(
							'link'     => true,
							'gradient' => false,
						),
					),
				),
				'anchor',
				false,
			),
		);
	}

	/**
	 * @ticket 58532
	 *
	 * @dataProvider data_block_has_support_string
	 *
	 * @param array  $block_data Block data.
	 * @param string $support    Support string to check.
	 * @param bool   $expected   Expected result.
	 */
	public function test_block_has_support_string( $block_data, $support, $expected ) {
		$this->registry->register( 'core/example', $block_data );
		$block_type  = $this->registry->get_registered( 'core/example' );
		$has_support = block_has_support( $block_type, $support );
		$this->assertEquals( $expected, $has_support );
	}

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @mukeshpanchal27. This make sense, I have updated the test cases.

Comment on lines 1260 to 1264
if ( is_array( $feature ) ) {
$block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
} else {
$block_support = isset( $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] : $default_value;
}
Copy link
Member

Choose a reason for hiding this comment

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

I think we should also check the length of the array, e.g. something like

if ( is_array( $feature ) && count( $feature ) === 1 ) {
	$feature = $feature[0];
}

If someone passes an array with just 1 element in it we can also short-circuit.

This way we don't necessarily have to change all existing instances in core, but can keep them as is.

And plugins and themes doing it wrong also benefit.

Copy link
Member

Choose a reason for hiding this comment

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

+1 to this change, that said I still think it's worth changing the instances in core since it's IMO just a bit simpler to use strings, more intuitive, and a tiny bit faster to check for.

Copy link
Member Author

Choose a reason for hiding this comment

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

I have included this check. Did a small rearrangement of condition to include this. w.r.t to core I have updated all the single element array to string.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thank you @kt-12, this looks great. Only two very minor docs suggestions other than the feedback that was already provided.

Comment on lines 1262 to 1264
} else {
$block_support = isset( $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] : $default_value;
}
Copy link
Member

Choose a reason for hiding this comment

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

We should also add here another condition based on @swissspidy's feedback in https://core.trac.wordpress.org/ticket/58532#comment:4.

src/wp-includes/blocks.php Outdated Show resolved Hide resolved
src/wp-includes/blocks.php Show resolved Hide resolved
Comment on lines 1260 to 1264
if ( is_array( $feature ) ) {
$block_support = _wp_array_get( $block_type->supports, $feature, $default_value );
} else {
$block_support = isset( $block_type->supports[ $feature ] ) ? $block_type->supports[ $feature ] : $default_value;
}
Copy link
Member

Choose a reason for hiding this comment

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

+1 to this change, that said I still think it's worth changing the instances in core since it's IMO just a bit simpler to use strings, more intuitive, and a tiny bit faster to check for.

Copy link
Member

@felixarntz felixarntz left a comment

Choose a reason for hiding this comment

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

Thanks @kt-12, LGTM!

Copy link
Member

@mukeshpanchal27 mukeshpanchal27 left a comment

Choose a reason for hiding this comment

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

Great work @kt-12,

One instance is missing here https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/block-supports/border.php#L25 that need to update as well. Thanks.

@felixarntz
Copy link
Member

@felixarntz felixarntz closed this Aug 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants