Last active
December 9, 2024 22:06
-
-
Save afragen/acf808925400dc8a0544fd9611ff2189 to your computer and use it in GitHub Desktop.
Simple plugin to fix some PHP8 errors in GlotPress and more
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* GlotPress Modifications. | |
* | |
* Plugin Name: GlotPress Modifications | |
* Plugin URI: https://gist.github.com/afragen/acf808925400dc8a0544fd9611ff2189 | |
* Description: Modifications to GlotPress to fix PHP 8 errors, use extended locale and more. | |
* Version: 0.9.0 | |
* Author: Andy Fragen | |
* License: MIT | |
* Requires at least: 6.0 | |
* Requires PHP: 8.0 | |
* Gist Plugin URI: https://gist.github.com/afragen/acf808925400dc8a0544fd9611ff2189 | |
* Requires Plugins: glotpress | |
*/ | |
// Exit if accessed directly. | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
// Add Dashboard link in header nav. | |
add_filter( | |
'gp_nav_menu_items', | |
function ( $items, $location ) { | |
$arr = array( '/wp-admin/' => 'Dashboard' ); | |
if ( 'side' === $location ) { | |
$items = array_merge( $arr, $items ); | |
} | |
return $items; | |
}, | |
10, | |
2 | |
); | |
// Fixes https://github.com/GlotPress/GlotPress/issues/1858 | |
add_action( | |
'plugins_loaded', | |
function () { | |
class GP_Format_PO_Mods extends GP_Format_PO { | |
public function print_exported_file( $project, $locale, $translation_set, $entries ) { | |
$po = new $this->class(); | |
// See https://www.gnu.org/software/gettext/manual/html_node/Header-Entry.html for header details. | |
// TODO: add more meta data in the project: language team, report URL. | |
$this->set_header( $po, 'PO-Revision-Date', GP::$translation->last_modified( $translation_set ) . '+0000' ); | |
$this->set_header( $po, 'MIME-Version', '1.0' ); | |
$this->set_header( $po, 'Content-Type', 'text/plain; charset=UTF-8' ); | |
$this->set_header( $po, 'Content-Transfer-Encoding', '8bit' ); | |
$this->set_header( $po, 'Plural-Forms', "nplurals=$locale->nplurals; plural=$locale->plural_expression;" ); | |
$this->set_header( $po, 'X-Generator', 'GlotPress/' . GP_VERSION ); | |
$language_code = $this->get_language_code( $locale ); | |
if ( false !== $language_code ) { | |
$this->set_header( $po, 'Language', $language_code ); | |
} | |
// Force export only current translations. | |
$filters = array(); | |
$filters['status'] = 'current'; | |
foreach ( $entries as $entry ) { | |
// Convert NULL to empty string to prevent PHP 8 passing NULL errors. | |
$entry->translations = array_map( | |
function ( $translation ) { | |
return $translation ?: ''; | |
}, | |
$entry->translations | |
); | |
$po->add_entry( $entry ); | |
} | |
$current = $project; | |
$project_tree = array(); | |
$project_tree[] = $current->name; | |
while ( $current->parent_project_id > 0 ) { | |
$current = GP::$project->get( $current->parent_project_id ); | |
$project_tree[] = $current->name; | |
} | |
$project_tree = array_reverse( $project_tree ); | |
$project_id_version = implode( ' - ', $project_tree ); | |
/** | |
* Filter the project name and version header before export. | |
* | |
* @since 2.1.0 | |
* | |
* @param string $project_id_version The default project name/version to use in the header and | |
* comments ( "Parent - Child - GrandChild - etc." by default). | |
* @param array $project_tree An array of the parent/child project tree, ordered from Parent | |
* to child to grandchild to etc... | |
*/ | |
$project_id_version = apply_filters( 'gp_pomo_export_project_id_version', $project_id_version, $project_tree ); | |
$this->set_header( $po, 'Project-Id-Version', $project_id_version ); | |
$this->add_comments_before_headers( $po, "Translation of {$project_id_version} in {$locale->english_name}\n" ); | |
$this->add_comments_before_headers( $po, "This file is distributed under the same license as the {$project_id_version} package.\n" ); | |
// error_log( 'GP_Format_PO_Mods::print_exported_file end' ); | |
return $po->export(); | |
} | |
/** | |
* Modify slug or locale to return language code as extended slug (xx_XX). | |
* | |
* @param string|\GP_Locale $locale Language slug or locale. | |
* | |
* @return string | |
*/ | |
public function get_language_code( $locale ) { | |
$locales = new GP_Locales(); | |
if ( is_string( $locale ) ) { | |
$locale = $locales->locales[ $locale ]; | |
} | |
$slug = $locale->slug; | |
if ( ! str_contains( $slug, '_' ) ) { | |
if ( null !== $locale->wp_locale && str_contains( $locale->wp_locale, '_' ) ) { | |
$slug = $locale->wp_locale; | |
} else { | |
foreach ( $locale as $locale_slug ) { | |
if ( null !== $locale_slug && str_contains( $locale_slug, '_' ) ) { | |
$slug = $locale_slug; | |
break; | |
} | |
} | |
} | |
} | |
return $slug; | |
} | |
} | |
GP::$formats['po'] = new GP_Format_PO_Mods(); | |
// Exports files with extended slug if not present. | |
add_filter( 'gp_export_locale', array( GP::$formats['po'], 'get_language_code' ), 15, 2 ); | |
} | |
); | |
// Fixes https://github.com/GlotPress/GlotPress/issues/1860 | |
add_action( | |
'plugins_loaded', | |
function () { | |
class GP_Validator_Permission_Mods extends GP_Validator_Permission { | |
public $user; | |
public $project; | |
} | |
GP::$validator_permission = new GP_Validator_Permission_Mods(); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment