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

Localise the footer links to point to the Localised WordPress site when it exists. #106

Merged
merged 3 commits into from
Jan 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 58 additions & 5 deletions mu-plugins/blocks/global-header-footer/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,11 +641,7 @@ function get_home_url() {

if ( is_rosetta_site() ) {
$root_site = $rosetta->get_root_site_id();
switch_to_blog( $root_site );

$url = home_url();

restore_current_blog();
$url = \get_home_url( $root_site, '/' );
}

if ( ! $url ) {
Expand Down Expand Up @@ -733,6 +729,7 @@ function render_global_footer() {

if ( is_rosetta_site() ) {
$locale_title = get_rosetta_name();
add_filter( 'render_block_data', __NAMESPACE__ . '\localize_nav_links' );
} else {
$locale_title = '';
}
Expand All @@ -753,9 +750,65 @@ function render_global_footer() {
$markup .= ob_get_clean();
}

remove_filter( 'render_block_data', __NAMESPACE__ . '\localize_nav_links' );

return $markup;
}

/**
* Localise a `core/navigation-link` block link to point to the Rosetta site resource.
*
* Unfortunately WordPress doesn't have a block-specific pre- filter, only a block-specific post-filter.
* That's why we specifically check for the blockName here.
*
* @param array $block The parsed block data.
*
* @return array
*/
function localize_nav_links( $block ) {
if (
! empty( $block['blockName'] ) &&
'core/navigation-link' === $block['blockName'] &&
! empty( $block['attrs']['url'] )
) {
$block['attrs']['url'] = get_localized_footer_link( $block['attrs']['url'] );
}

return $block;
}

/**
* Get a localized variant of a link included in the global footer.
*
* @param string $url The URL as it is in the menu.
*
* @return string|bool Replacement URL if one exists, false otherwise.
*/
function get_localized_footer_link( $url ) {
global $rosetta;
if ( empty( $rosetta->current_site_domain ) ) {
return $url;
}

switch ( $url ) {
case 'https://wordpress.org/showcase/':
case 'https://wordpress.org/hosting/':
return $url;

case 'https://wordpress.org/support/':
// Check if support forum exists.
if ( ! $rosetta->has_support_forum() ) {
return $url;
}
break;

case 'https://learn.wordpress.org/':
return add_query_arg( 'locale', get_locale(), $url );
}

return str_replace( 'https://wordpress.org/', 'https://' . $rosetta->current_site_domain . '/', $url );
}

/**
* Check if the current site is part of the w.org network.
*
Expand Down
2 changes: 1 addition & 1 deletion mu-plugins/blocks/global-header-footer/footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<div class="wp-block-group global-footer__navigation-container">
<!-- wp:navigation {"orientation":"vertical","className":"global-footer__navigation-important","overlayMenu":"never"} -->
<!-- wp:navigation-link {"label":"<?php echo esc_html_x( 'About', 'Menu item title', 'wporg' ); ?>","url":"https://wordpress.org/about/","kind":"custom","isTopLevelLink":true} /-->
<!-- wp:navigation-link {"label":"<?php echo esc_html_x( 'Blog', 'Menu item title', 'wporg' ); ?>","url":"https://wordpress.org/news","kind":"custom","isTopLevelLink":true} /-->
<!-- wp:navigation-link {"label":"<?php echo esc_html_x( 'Blog', 'Menu item title', 'wporg' ); ?>","url":"https://wordpress.org/news/","kind":"custom","isTopLevelLink":true} /-->
dd32 marked this conversation as resolved.
Show resolved Hide resolved
<!-- wp:navigation-link {"label":"<?php echo esc_html_x( 'Hosting', 'Menu item title', 'wporg' ); ?>","url":"https://wordpress.org/hosting/","kind":"custom","isTopLevelLink":true} /-->
<!-- wp:navigation-link {"label":"<?php echo esc_html_x( 'Donate', 'Menu item title', 'wporg' ); ?>","url":"https://wordpressfoundation.org/donate/","kind":"custom","isTopLevelLink":true} /-->
<!-- /wp:navigation -->
Expand Down