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

Navigation: Refactor the PHP render function to make it easier to make changes in the future #55605

Merged
merged 61 commits into from
Nov 9, 2023
Merged
Changes from 1 commit
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
65f5793
Navigation: Refactor the PHP render function to make it easier to mak…
scruffian Oct 25, 2023
5bcde61
move code to get_inner_blocks_html
scruffian Oct 25, 2023
dcd97b7
remove is-fallback class
scruffian Oct 25, 2023
e268651
allow multiple menus
scruffian Oct 25, 2023
8f54d44
add some comments
scruffian Oct 25, 2023
a9f0328
refactor directives to a different function
scruffian Oct 25, 2023
f087c4e
fix submenus
scruffian Oct 25, 2023
b40caa5
refactor responseive navigation function
scruffian Oct 25, 2023
a6ffa85
remove a passed param
scruffian Oct 25, 2023
2991d1e
create a function for get_styles
scruffian Oct 25, 2023
afe3474
dont pass the container_attributes as a param
scruffian Oct 25, 2023
58906ca
handle view script loading
scruffian Oct 25, 2023
67d5ec3
reorder code
scruffian Oct 25, 2023
40c4cce
simplify the return
scruffian Oct 25, 2023
0544d33
tidy up the wrapper attributes
scruffian Oct 25, 2023
21e973d
remove one dependence on has_submenu
scruffian Oct 25, 2023
c7e010a
remove the should load view script variable
scruffian Oct 25, 2023
001ed21
more simplification
scruffian Oct 25, 2023
e8b57df
Update packages/block-library/src/navigation/index.php
scruffian Oct 26, 2023
7826b78
rename class
scruffian Oct 26, 2023
dc5fb2c
remname is_responsive_navigation
scruffian Oct 26, 2023
0a7c0d1
rename does_navigation_have_submenus
scruffian Oct 26, 2023
362d7bd
rename get_inner_blocks_for_navigation
scruffian Oct 26, 2023
04f245e
rename get_layout_class_for_navigation
scruffian Oct 26, 2023
7123f27
Update packages/block-library/src/navigation/index.php
scruffian Oct 26, 2023
af5f8cd
Update packages/block-library/src/navigation/index.php
scruffian Oct 26, 2023
5157393
change name of get_nav_markup
scruffian Nov 2, 2023
cd2c7fd
split get_inner_blocks into smaller functions
scruffian Nov 2, 2023
5514082
add comment
scruffian Nov 2, 2023
34fde64
remove unnecessary spaces
scruffian Nov 2, 2023
8998c2b
update docs
scruffian Nov 2, 2023
62d192f
reverse parameter order
scruffian Nov 2, 2023
fdabfa4
reverse parameter order
scruffian Nov 2, 2023
1ae2f05
reverse parameter order
scruffian Nov 2, 2023
5ab9ae5
move the class to a new file
scruffian Nov 2, 2023
d51d7c3
add docs
scruffian Nov 2, 2023
9a7a313
rename to
scruffian Nov 2, 2023
70cf09b
move function that's only used by the plugin into that section of the…
scruffian Nov 2, 2023
643c967
fix linter
scruffian Nov 2, 2023
d3c876b
reference the class internally using static
scruffian Nov 3, 2023
736e27d
move variable to static variables
scruffian Nov 7, 2023
dcad41f
Update lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php
scruffian Nov 7, 2023
07f65eb
Update lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php
scruffian Nov 7, 2023
103c27a
move the logic for inner block rendering to a separate function
scruffian Nov 7, 2023
d98037e
test for navigation link markup
MaggieCabrera Nov 7, 2023
a19a890
test for site title markup
MaggieCabrera Nov 7, 2023
c5261b6
removed comments, changed assertSame to assertEquals
MaggieCabrera Nov 7, 2023
0e63a3a
format php
MaggieCabrera Nov 7, 2023
295bd03
hide the condition in a function
scruffian Nov 7, 2023
94dd338
hide the condition in a function
scruffian Nov 7, 2023
939c50f
return early from has_submenus
scruffian Nov 7, 2023
d21547c
comments for the tests
MaggieCabrera Nov 7, 2023
b16a3bd
create a function to get the name
scruffian Nov 7, 2023
aceed2e
rename variable to nav_blocks_wrapped_in_list_item
scruffian Nov 7, 2023
05ab18f
phpcs
scruffian Nov 7, 2023
5310ede
phpcs
scruffian Nov 7, 2023
163f08f
Update phpunit/class-wp-navigation-block-renderer-test.php
MaggieCabrera Nov 8, 2023
649f813
Update phpunit/class-wp-navigation-block-renderer-test.php
MaggieCabrera Nov 8, 2023
73764c9
Update phpunit/class-wp-navigation-block-renderer-test.php
MaggieCabrera Nov 8, 2023
9b669a1
Apply suggestions from code review
MaggieCabrera Nov 8, 2023
c56237d
dynamic url for links on test
MaggieCabrera Nov 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix linter
  • Loading branch information
scruffian committed Nov 2, 2023
commit 643c9677fa2c5e6002e2629dbbbd3bf134de3022
49 changes: 30 additions & 19 deletions lib/compat/wordpress-6.5/class-wp-navigation-block-renderer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
<?php
/**
* WP_Navigation_Block_Renderer class
*
* @package gutenberg
* @since 6.5.0
*/

if ( class_exists( 'WP_Navigation_Block_Renderer' ) ) {
return;
}

/**
* Helper functions used to render the navigation block.
Expand Down Expand Up @@ -50,7 +60,7 @@ private static function has_submenus( $inner_blocks ) {
* @return bool Returns whether or not to load the view script.
*/
private static function should_load_view_script( $attributes, $inner_blocks ) {
$has_submenus = WP_Navigation_Block_Renderer::has_submenus( $inner_blocks );
$has_submenus = WP_Navigation_Block_Renderer::has_submenus( $inner_blocks );
scruffian marked this conversation as resolved.
Show resolved Hide resolved
$is_responsive_menu = WP_Navigation_Block_Renderer::is_responsive( $attributes );
return ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;
}
Expand Down Expand Up @@ -79,8 +89,8 @@ private static function get_inner_blocks_html( $attributes, $inner_blocks ) {
'core/site-logo',
);

$style = WP_Navigation_Block_Renderer::get_styles( $attributes );
$class = WP_Navigation_Block_Renderer::get_classes( $attributes );
$style = WP_Navigation_Block_Renderer::get_styles( $attributes );
$class = WP_Navigation_Block_Renderer::get_classes( $attributes );
$container_attributes = get_block_wrapper_attributes(
array(
'class' => 'wp-block-navigation__container ' . $class,
Expand Down Expand Up @@ -269,6 +279,9 @@ private static function get_navigation_name( $attributes, &$seen_menu_names ) {

/**
* Returns the layout class for the navigation block.
*
* @param array $attributes The block attributes.
* @return string Returns the layout class for the navigation block.
*/
private static function get_layout_class( $attributes ) {
$layout_justification = array(
Expand Down Expand Up @@ -303,8 +316,7 @@ private static function get_layout_class( $attributes ) {
*/
private static function get_classes( $attributes ) {
// Restore legacy classnames for submenu positioning.
$layout_class = WP_Navigation_Block_Renderer::get_layout_class( $attributes );

$layout_class = WP_Navigation_Block_Renderer::get_layout_class( $attributes );
$colors = block_core_navigation_build_css_colors( $attributes );
$font_sizes = block_core_navigation_build_css_font_sizes( $attributes );
$is_responsive_menu = WP_Navigation_Block_Renderer::is_responsive( $attributes );
Expand All @@ -313,7 +325,7 @@ private static function get_classes( $attributes ) {
$text_decoration = $attributes['style']['typography']['textDecoration'] ?? null;
$text_decoration_class = sprintf( 'has-text-decoration-%s', $text_decoration );

$classes = array_merge(
$classes = array_merge(
$colors['css_classes'],
$font_sizes['css_classes'],
$is_responsive_menu ? array( 'is-responsive' ) : array(),
Expand All @@ -333,8 +345,7 @@ private static function get_styles( $attributes ) {
$colors = block_core_navigation_build_css_colors( $attributes );
$font_sizes = block_core_navigation_build_css_font_sizes( $attributes );
$block_styles = isset( $attributes['styles'] ) ? $attributes['styles'] : '';
$style = $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'];

return $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'];
}

/**
Expand All @@ -343,12 +354,12 @@ private static function get_styles( $attributes ) {
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @param string $inner_blocks_html The markup for the inner blocks.
* @return string Returns the container markup.
* @return string Returns the container markup.
*/
private static function get_responsive_container_markup( $attributes, $inner_blocks, $inner_blocks_html ) {
$should_load_view_script = WP_Navigation_Block_Renderer::should_load_view_script( $attributes, $inner_blocks );
$colors = block_core_navigation_build_css_colors( $attributes );
$modal_unique_id = wp_unique_id( 'modal-' );
$colors = block_core_navigation_build_css_colors( $attributes );
$modal_unique_id = wp_unique_id( 'modal-' );

$is_hidden_by_default = isset( $attributes['overlayMenu'] ) && 'always' === $attributes['overlayMenu'];

Expand Down Expand Up @@ -437,8 +448,8 @@ private static function get_responsive_container_markup( $attributes, $inner_blo
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks A list of inner blocks.
* @param string $nav_menu_name The name of the navigation menu.
* @return string Returns the navigation block markup.
* @param string $nav_menu_name The name of the navigation menu.
* @return string Returns the navigation block markup.
*/
private static function get_nav_wrapper_attributes( $attributes, $inner_blocks, $nav_menu_name ) {
$should_load_view_script = WP_Navigation_Block_Renderer::should_load_view_script( $attributes, $inner_blocks );
Expand All @@ -454,8 +465,8 @@ private static function get_nav_wrapper_attributes( $attributes, $inner_blocks,
);

if ( $is_responsive_menu ) {
$nav_element_directives = WP_Navigation_Block_Renderer::get_nav_element_directives( $should_load_view_script );
$wrapper_attributes .= ' ' . $nav_element_directives;
$nav_element_directives = WP_Navigation_Block_Renderer::get_nav_element_directives( $should_load_view_script );
$wrapper_attributes .= ' ' . $nav_element_directives;
}

return $wrapper_attributes;
Expand All @@ -465,7 +476,7 @@ private static function get_nav_wrapper_attributes( $attributes, $inner_blocks,
* Get the nav element directives
*
* @param bool $should_load_view_script Whether or not the view script should be loaded.
* @return string the directives for the navigation element.
* @return string the directives for the navigation element.
*/
private static function get_nav_element_directives( $should_load_view_script ) {
if ( ! $should_load_view_script ) {
Expand Down Expand Up @@ -523,7 +534,7 @@ private static function handle_view_script_loading( $attributes, $block, $inner_
*
* @param array $attributes The block attributes.
* @param WP_Block_List $inner_blocks The list of inner blocks.
* @return string Returns the navigation wrapper markup.
* @return string Returns the navigation wrapper markup.
*/
private static function get_wrapper_markup( $attributes, $inner_blocks ) {
$inner_blocks_html = WP_Navigation_Block_Renderer::get_inner_blocks_html( $attributes, $inner_blocks );
Expand All @@ -539,9 +550,9 @@ private static function get_wrapper_markup( $attributes, $inner_blocks ) {
* @param array $attributes The block attributes.
* @param string $content The saved content.
* @param WP_Block $block The parsed block.
* @return string Returns the navigation block markup.
* @return string Returns the navigation block markup.
*/
static function render( $attributes, $content, $block ) {
public static function render( $attributes, $content, $block ) {
static $seen_menu_names = array();

$nav_menu_name = WP_Navigation_Block_Renderer::get_navigation_name( $attributes, $seen_menu_names );
Expand Down
Loading