Skip to content

Commit

Permalink
v1.0.2 (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymcp authored Apr 18, 2024
2 parents 99d4502 + 6109eea commit 724a087
Show file tree
Hide file tree
Showing 121 changed files with 87,203 additions and 67,851 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### v1.0.2
Fixes:
- Potential collision in language handling when languages share an ISO 639-1 code
- Translations packs not loading correctly in some circumstances
- Missing whitespace after formatted text in Collapsable block

### v1.0.1
Fixes:
- Prevent featured image output if post has a header block
Expand Down
12 changes: 10 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,17 @@
}
},
"scripts": {
"lint": "./vendor/bin/phpcs wp-content/themes/humanity-theme"
"lint": "./vendor/bin/phpcs wp-content/themes/humanity-theme",
"makePot": "wp i18n make-pot wp-content/themes/humanity-theme wp-content/themes/humanity-theme/languages/amnesty.pot --domain=amnesty --exclude=private",
"updatePoMo": [
"wp i18n update-po wp-content/themes/humanity-theme/languages/amnesty.pot",
"wp i18n make-mo wp-content/themes/humanity-theme/languages",
"wp i18n make-json wp-content/themes/humanity-theme/languages --no-purge"
]
},
"scripts-descriptions": {
"lint": "Runs PHP coding standard checks"
"lint": "Runs PHP coding standard checks",
"makePot": "Re-generates the POT language file",
"UpdatePoMo": "Updates PO, MO, and JSON translation files"
}
}
1 change: 1 addition & 0 deletions private/src/scripts/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ import '../styles/admin.scss';
import './admin/cmb-events';
import './admin/cmb-term-type';
import './admin/ordered-list-preview';
import './admin/options-general';
12 changes: 12 additions & 0 deletions private/src/scripts/admin/options-general.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const settingsGeneral = () => {
const wplang = document.getElementById('WPLANG')?.parentElement?.parentElement;
const language = document.getElementById('amnesty-language-name')?.parentElement?.parentElement;

if (!wplang || !language) {
return;
}

wplang.insertAdjacentElement('beforeBegin', language);
};

document.addEventListener('DOMContentLoaded', settingsGeneral);
1 change: 1 addition & 0 deletions wp-content/themes/humanity-theme/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
require_once realpath( __DIR__ . '/includes/admin/class-fonts.php' );
require_once realpath( __DIR__ . '/includes/admin/class-permalink-settings.php' );
require_once realpath( __DIR__ . '/includes/admin/user-options.php' );
require_once realpath( __DIR__ . '/includes/admin/settings-general.php' );
#endregion admin

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare( strict_types = 1 );

if ( ! function_exists( 'amnesty_register_general_settings' ) ) {
/**
* Register custom fields on general settings page
*
* @package Amnesty\Plugins\CMB2
*
* @return void
*/
function amnesty_register_general_settings() {
add_settings_field(
'amnesty_language_name',
/* translators: [admin] */
__( 'Language Display Name', 'amnesty' ),
'amnesty_render_setting_language_name',
'general'
);

register_setting( 'general', 'amnesty_language_name', 'sanitize_text_field' );
}
}

if ( ! function_exists( 'amnesty_render_setting_language_name' ) ) {
/**
* Render the language name settings field
*
* @package Amnesty\Plugins\CMB2
*
* @return void
*/
function amnesty_render_setting_language_name() {
$value = get_option( 'amnesty_language_name' );
printf(
'<input id="amnesty-language-name" class="regular-text" name="amnesty_language_name" type="text" value="%1$s">',
esc_attr( $value )
);

printf(
'<p class="description">%s</p>',
/* translators: [admin] */
esc_html__( 'Override the site language display name on the site frontend', 'amnesty' )
);
}
}

add_action( 'admin_init', 'amnesty_register_general_settings' );
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function render_collapsable_block( array $attributes, $content = '' ): string {
'anchor' => '',
'collapsed' => false,
'title' => '',
]
]
);

if ( ! $content ) {
Expand All @@ -33,8 +33,8 @@ function render_collapsable_block( array $attributes, $content = '' ): string {
$wrapper_attrs['class'] = 'is-collapsed';
}

spaceless();
ob_start();
require realpath( __DIR__ . '/views/block.php' );
return endspaceless( false );
return ob_get_clean();
}
}
52 changes: 51 additions & 1 deletion wp-content/themes/humanity-theme/includes/helpers/site.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ function get_site_language_code( int $blog_id = null ): string {
switch_to_blog( $blog_id );
}

$lang = preg_replace( '/^([a-z]{2})[-_][a-zA-Z]{2,}$/', '$1', get_option( 'WPLANG' ) ?: 'en_US' );
$lang = get_option( 'WPLANG' ) ?: 'en_US';

if ( true === apply_filters( 'amnesty_strip_locale_country_code', true ) ) {
$lang = preg_replace( '/^([a-z]{2,})[-_][a-zA-Z]{2,}$/', '$1', $lang );
}

if ( is_multisite() && $blog_id ) {
restore_current_blog();
Expand All @@ -59,6 +63,52 @@ function get_site_language_code( int $blog_id = null ): string {
}
}

if ( ! function_exists( 'get_site_language_name' ) ) {
/**
* Retrieve the current site's language name
*
* @package Amnesty
*
* @param int|null $blog_id the site to get the language name for
*
* @return string
*/
function get_site_language_name( int $blog_id = null ): string {
$override = get_blog_option( $blog_id, 'amnesty_language_name' );

if ( $override ) {
return $override;
}

$lang = get_blog_option( $blog_id, 'WPLANG' );
$lang = $lang ?: get_site_option( 'WPLANG' );
$lang = $lang ?: ( $GLOBALS['wp_local_package'] ?? false );
$lang = $lang ?: 'en_GB';

$lang = Locale::getDisplayName( $lang, $lang );

return strip_language_name_parentheticals( $lang );
}
}

if ( ! function_exists( 'strip_language_name_parentheticals' ) ) {
/**
* Strips locale names from language names
*
* @param string $language the language name
*
* @return string
*/
function strip_language_name_parentheticals( string $language ): string {
if ( false === apply_filters( 'amnesty_strip_locale_country', true ) ) {
return $language;
}

$stripped = preg_replace( '/\s*?([((]).*?([))])\s*?/ui', '', $language );
return $stripped ?: '';
}
}

if ( ! function_exists( 'do_404' ) ) {
/**
* Throw a 404
Expand Down
62 changes: 37 additions & 25 deletions wp-content/themes/humanity-theme/includes/mlp/language-selector.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,13 @@ function amnesty_get_object_translations( array $sites = [] ) {
}
}

$i18n_sites[ $language->isoCode() ] = (object) [
'lang' => preg_replace( '/\s*?([((]).*?([))])\s*?/ui', '', $language->name() ),
$lang = get_site_language_name( $translation->remoteSiteId() );
if ( ! $lang ) {
$lang = strip_language_name_parentheticals( $language->name() );
}

$i18n_sites[] = (object) [
'lang' => $lang,
'code' => $language->isoCode(),
'direction' => $language->isRtl() ? 'rtl' : 'ltr',
'name' => get_blog_option( $translation->remoteSiteId(), 'blogname' ),
Expand Down Expand Up @@ -91,16 +96,17 @@ function render_language_selector_dropdown() {
// fall back to home page if no translations
if ( ! count( $sites ) ) {
foreach ( amnesty_get_sites() as &$site ) {
$site->item_id = absint( get_blog_option( $site->site_id, 'page_on_front', '0' ) );
$site->blog_id = $site->site_id;
$site->type = 'post';
$sites[ $site->code ] = $site;
$site->item_id = absint( get_blog_option( $site->site_id, 'page_on_front', '0' ) );
$site->blog_id = $site->site_id;
$site->type = 'post';

$sites[] = $site;
}
}

$language = get_site_language_code();
$current = array_filter( $sites, fn ( string $lang ): bool => $language === $lang, ARRAY_FILTER_USE_KEY );
$others = array_filter( $sites, fn ( string $lang ): bool => $language !== $lang, ARRAY_FILTER_USE_KEY );
$others = array_filter( $sites, fn ( object $site ): bool => get_current_blog_id() !== $site->blog_id );
$current = array_filter( $sites, fn ( object $site ): bool => get_current_blog_id() === $site->blog_id );
$current = array_values( $current );

if ( ! count( $current ) ) {
return;
Expand All @@ -109,12 +115,13 @@ function render_language_selector_dropdown() {
$wrapper_open = sprintf( '<nav class="page-nav page-nav--left" aria-label="%s" aria-expanded="false"><ul class="site-selector">', /* translators: [front] */ esc_attr__( 'Available languages', 'amnesty' ) );
$wrapper_close = '</ul></nav><div class="site-separator"></div>';

$current_item_name = get_blog_option( $current[0]->blog_id, 'amnesty_language_name' ) ?: $current[0]->lang;
$current_item_open = sprintf(
'<li id="menu-item-%2$s-%3$s" class="menu-item menu-item-current menu-item-has-children menu-item-%2$s-%3$s" dir="%4$s"><span>%1$s</span>',
esc_html( ucwords( $current[ $language ]->lang, 'UTF-8' ) ),
esc_attr( $current[ $language ]->blog_id ),
esc_attr( $current[ $language ]->item_id ),
esc_attr( $current[ $language ]->direction ),
esc_html( ucwords( $current_item_name, 'UTF-8' ) ),
esc_attr( $current[0]->blog_id ),
esc_attr( $current[0]->item_id ),
esc_attr( $current[0]->direction ),
);

$current_item_close = '</li>';
Expand All @@ -123,7 +130,11 @@ function render_language_selector_dropdown() {
$other_items_close = '</ul>';

foreach ( amnesty_get_sites() as $site ) {
$other_items_inner .= render_language_selector_dropdown_item( $site, $others, $language );
if ( $site->current ) {
continue;
}

$other_items_inner .= render_language_selector_dropdown_item( $site, $others );
}

$html = implode(
Expand Down Expand Up @@ -152,35 +163,36 @@ function render_language_selector_dropdown() {
* @package Amnesty\Plugins\Multilingualpress
* @see render_language_selector_dropdown()
*
* @param object $site the site object
* @param array $sites the list of sites
* @param string $language the current language
* @param object $site the site object
* @param array $sites the list of sites
*
* @return string
*/
function render_language_selector_dropdown_item( object $site, array $sites, string $language ): string {
if ( $site->code === $language ) {
return '';
}
function render_language_selector_dropdown_item( object $site, array $sites ): string {
$found = array_filter( $sites, fn ( object $remote ): bool => $remote->blog_id === $site->site_id );
$found = array_values( $found );

// fallback to homepage if no translation
if ( ! isset( $sites[ $site->code ] ) ) {
if ( ! count( $found ) ) {
$name = get_blog_option( $site->site_id, 'ammesty_language_name' ) ?: $site->lang;

return sprintf(
'<li id="menu-item-%3$s-%4$s" class="menu-item menu-item-%3$s-%4$s" dir="%5$s"><a href="%2$s">%1$s</a></li>',
esc_html( $site->lang ),
esc_html( $name ),
esc_url( $site->url ),
esc_attr( $site->site_id ),
esc_attr( $site->post_id ),
esc_attr( $site->direction ),
);
}

$item = $sites[ $site->code ];
$item = $found[0];
$link = 'post' === $item->type ? get_blog_permalink( $item->blog_id, $item->item_id ) : get_blog_term_link( $item->blog_id, $item->item_id );
$name = get_blog_option( $item->blog_id, 'ammesty_language_name' ) ?: $item->lang;

return sprintf(
'<li id="menu-item-%3$s-%4$s" class="menu-item menu-item-%3$s-%4$s" dir="%5$s"><a href="%2$s">%1$s</a></li>',
esc_html( $item->lang ),
esc_html( $name ),
esc_url( $link ),
esc_attr( $item->blog_id ),
esc_attr( $item->item_id ),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function get_sites() {
switch_to_blog( $blog_id );

$sites[] = (object) [
'lang' => $this->get_lang( $blog_id ),
'lang' => get_site_language_name( $blog_id ),
'code' => get_site_language_code( $blog_id ),
'direction' => $this->get_direction( $blog_id ),
'name' => get_bloginfo( 'name' ),
Expand Down Expand Up @@ -116,23 +116,4 @@ protected function get_direction( $blog_id = 0 ) {
return $direction;
}

/**
* Retrieve language identifier for blog.
*
* @param int $blog_id the blog context
*
* @return string
*/
protected function get_lang( $blog_id = 0 ) {
$lang = $this->get_locale( $blog_id );
$lang = locale_get_display_name( $lang, $lang );

if ( false === apply_filters( 'amnesty_strip_locale_country', true ) ) {
return $lang;
}

// strip country identifier from language display name
return preg_replace( '/\s*?([((]).*?([))])\s*?/ui', '', $lang );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,44 @@ function amnesty_load_core_textdomain() {
}

add_action( 'after_setup_theme', 'amnesty_load_core_textdomain' );

if ( ! function_exists( 'amnesty_mofile_domain_prefix' ) ) {
/**
* Add textdomain prefix to mofile path when loading textdomains
*
* Fn {wp_set_script_translations} requires JSON files to be prefixed
* with the textdomain. Since the filename is not customisable when
* generating the JSON files using WP CLI, the POMO files need to
* include the textdomain as a prefix. Not doing this will cause
* the script translations to fail to load, when loading from the
* theme's languages directory.
* However, prefixing these files with the text domain then causes
* {load_theme_textdomain} to fail to find the MO files for loading
* via PHP.
*
* Since it's easier to filter PHP MO file loading than it is to
* rename the JSON files, this is the approach that we have gone for.
* It seems very silly that core expects the textdomain prefix in
* one place, whilst not expecting it in another place, but that
* is the trade-off when bundling one's own translation files in
* a theme/plugin, rather than in a core languages directory, but
* for plugins/themes that aren't released via wp.org, there's
* little alternative.
*
* @package Amnesty\ThemeSetup
*
* @param string $mofile the path to the mofile to load
* @param string $domain the textdomain being loaded
*
* @return string
*/
function amnesty_mofile_domain_prefix( string $mofile, string $domain ): string {
if ( 'amnesty' !== $domain ) {
return $mofile;
}

return trailingslashit( dirname( $mofile ) ) . 'amnesty-' . basename( $mofile );
}
}

add_filter( 'load_textdomain_mofile', 'amnesty_mofile_domain_prefix', 10, 2 );
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"translation-revision-date":"2024-04-17 11:39+0100","generator":"WP-CLI\/2.8.1","source":"assets\/scripts\/bundle.js","domain":"messages","locale_data":{"messages":{"":{"domain":"messages","lang":"ar","plural-forms":"nplurals=6; plural=(n==0 ? 0 : n==1 ? 1 : n==2 ? 2 : n%100>=3 && n%100<=10 ? 3 : n%100>=11 && n%100<=99 ? 4 : 5);"},"%d day":["\u202b%d \u064a\u0648\u0645"],"time format(prefix translated separately) i.e. \"x, H:i:s\"\u0004%1$s, %2$s:%3$s:%4$s":["%1$s, %2$s:%3$s:%4$s"],"time format, H:i:s\u0004%1$s:%2$s:%3$s":["%1$s:%2$s:%3$s"]}}}

Large diffs are not rendered by default.

Binary file not shown.
Loading

0 comments on commit 724a087

Please sign in to comment.