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

issue-1208: Improve i18n support for WordPress 6.7 #1209

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
37 changes: 10 additions & 27 deletions apple-news.php
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
<?php
/**
* Entry point for the plugin.
*
* This file is read by WordPress to generate the plugin information in the
* admin panel.
*
* @link http://github.com/alleyinteractive/apple-news
* @since 0.2.0
* @package WP_Plugin
*/

/*
* Plugin Name: Publish to Apple News
* Plugin Name: Publish To Apple News
* Plugin URI: http://github.com/alleyinteractive/apple-news
* Description: Export and sync posts to Apple format.
* Version: 2.6.1
* Author: Alley
* Author URI: https://alley.com
* Text Domain: apple-news
* Domain Path: lang/
* License: GPLv3 or later
* License URI: https://www.gnu.org/licenses/gpl.html
*
* @package Apple_News
*/

/**
Expand Down Expand Up @@ -79,29 +71,20 @@ function apple_news_uninstall_wp_plugin() {
require __DIR__ . '/includes/class-apple-news.php';
require __DIR__ . '/admin/class-admin-apple-news.php';

/**
* Load plugin textdomain.
*
* @since 0.9.0
*/
function apple_news_load_textdomain() {
load_plugin_textdomain( 'apple-news', false, __DIR__ . '/lang' );
}
add_action( 'plugins_loaded', 'apple_news_load_textdomain' );

Comment on lines -82 to -91
Copy link
Author

@renatonascalves renatonascalves Dec 7, 2024

Choose a reason for hiding this comment

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

Since WordPress 4.7, this is no longer required for plugins hosted in WordPress.org.

The translation comes automatically from here: https://translate.wordpress.org/projects/wp-plugins/publish-to-apple-news/

/**
* Gets plugin data.
* Used to provide generator info in the metadata class.
*
* @return array
*
* @since 1.0.4
*
* @param bool $translate Whether to translate the plugin data.
* @return array
*/
function apple_news_get_plugin_data() {
function apple_news_get_plugin_data( $translate = true ) {
Copy link
Author

Choose a reason for hiding this comment

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

Allow this function to be used before init, where translation is not supported or necessary. See the related changed unit test as part of this pr.

if ( ! function_exists( 'get_plugin_data' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
return get_plugin_data( __DIR__ . '/apple-news.php' );
return get_plugin_data( __DIR__ . '/apple-news.php', true, $translate );
}

new Admin_Apple_News();
Expand Down
34 changes: 20 additions & 14 deletions includes/class-apple-news.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,34 +365,37 @@ public static function has_uninitialized_error(): WP_Error|null {

/**
* Constructor. Registers action hooks.
*
* @access public
*/
public function __construct() {
add_action(
'admin_enqueue_scripts',
[ $this, 'action_admin_enqueue_scripts' ]
);

add_action(
'enqueue_block_editor_assets',
[ $this, 'action_enqueue_block_editor_assets' ]
);

add_action(
'plugins_loaded',
[ $this, 'action_plugins_loaded' ]
'init',
Copy link
Author

@renatonascalves renatonascalves Dec 7, 2024

Choose a reason for hiding this comment

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

I learned as part of this fix that loading __( translations before init has always been incorrect from plugin developers. The difference now is that WordPress is making that obvious to developers like me as part of WordPress 6.7 and encouraging us to hook translation code in the proper hook.

[ $this, 'action_init' ]
);

add_filter(
'update_post_metadata',
[ $this, 'filter_update_post_metadata' ],
10,
5
);

add_filter(
'author_link',
[ $this, 'filter_author_link' ],
10,
3
);

add_filter(
'the_author',
[ $this, 'filter_the_author' ],
Expand Down Expand Up @@ -471,21 +474,21 @@ public function action_enqueue_block_editor_assets(): void {
}

/**
* Action hook callback for plugins_loaded.
* Action hook callback for init.
*
* @since 1.3.0
*/
public function action_plugins_loaded(): void {
public function action_init(): void {

// Determine if the database version and code version are the same.
$current_version = get_option( 'apple_news_version' );
if ( version_compare( $current_version, self::$version, '>=' ) ) {
$current_version = get_option( 'apple_news_version', '' );
if ( is_string( $current_version ) && version_compare( $current_version, self::$version, '>=' ) ) {
return;
}

// Determine if this is a clean install (no settings set yet).
$settings = get_option( self::$option_name );
if ( ! empty( $settings ) ) {
if ( ! empty( $settings ) && is_string( $current_version ) ) {

// Handle upgrade to version 1.3.0.
if ( version_compare( $current_version, '1.3.0', '<' ) ) {
Expand Down Expand Up @@ -517,8 +520,6 @@ public function action_plugins_loaded(): void {

/**
* Create the default themes, if they do not exist.
*
* @access public
*/
public function create_default_theme(): void {

Expand All @@ -529,9 +530,14 @@ public function create_default_theme(): void {
}

// Build the theme formatting settings from the base settings array.
$theme = new Theme();
$options = Theme::get_options();
$wp_settings = get_option( self::$option_name, [] );
$theme = new Theme();
$options = Theme::get_options();
$wp_settings = get_option( self::$option_name, [] );

if ( ! is_array( $wp_settings ) ) {
$wp_settings = [];
}

$theme_settings = [];
foreach ( array_keys( $options ) as $option_key ) {
if ( isset( $wp_settings[ $option_key ] ) ) {
Expand Down
1 change: 0 additions & 1 deletion lang/apple-export.pot

This file was deleted.

10 changes: 9 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ Please visit our [wiki](https://github.com/alleyinteractive/apple-news/wiki) for

== Changelog ==

= 2.6.2 =

* Enhancement: i18n - Remove `load_plugin_textdomain`. Since WordPress 4.6, plugins no longer need to load their own textdomain.
* Enhancement: i18n - Moved some plugin initialization code to the `init` action hook.
* Bugfix: i18n - Fixed the `_load_textdomain_just_in_time` error with WordPress 6.7.
* Bugfix: Update plugin name in plugin header.
* Enhancement: Add license to plugin header.

= 2.6.1 =

* Enhancement: Ensured support for WordPress 6.7.
Expand Down Expand Up @@ -90,4 +98,4 @@ Information on previous releases can be found on the plugin's [GitHub Releases p

== Developers ==

Please visit us on [github](https://github.com/alleyinteractive/apple-news) to [submit issues](https://github.com/alleyinteractive/apple-news/issues), [pull requests](https://github.com/alleyinteractive/apple-news/pulls) or [read our wiki page about contributing](https://github.com/alleyinteractive/apple-news/wiki/contributing).
Please visit us on [GitHub](https://github.com/alleyinteractive/apple-news) to [submit issues](https://github.com/alleyinteractive/apple-news/issues), [pull requests](https://github.com/alleyinteractive/apple-news/pulls) or [read our wiki page about contributing](https://github.com/alleyinteractive/apple-news/wiki/contributing).
2 changes: 1 addition & 1 deletion tests/test-class-apple-news.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public function test_upgrade_to_2_5_0(): void {
* @see Apple_News::$version
*/
public function test_version() {
$plugin_data = apple_news_get_plugin_data();
$plugin_data = apple_news_get_plugin_data( translate: false );
$this->assertEquals( Apple_News::$version, $plugin_data['Version'] );
}
}
Loading