Make WordPress Core

Opened 21 months ago

Closed 21 months ago

Last modified 21 months ago

#62564 closed defect (bug) (worksforme)

Wrong warning about _load_textdomain_just_in_time used incorrectly if $theme->__toString()

Reported by: DaveFX Owned by:
Priority: normal Milestone:
Component: I18N Version: 6.7
Severity: normal Keywords:
Cc: Focuses:

Description

One of my installed plugins has these lines in its initialization function:

<?php
$current_theme = wp_get_theme();

if ('Flatsome' == $current_theme) {
  // Do something
} else {
  // Do a different thing
}

The issue here is that the implicit \_\_toString() call in the theme class ultimately calls

translate("My Child Theme Name","my-theme-name")

which generates a wrong warning about _load_textdomain_just_in_time used incorrectly, saying that the culprit is the theme code, when the backtraces shows this is not the case.

I honestly think this is an issue from WordPress Core, as this _ _toString() call to get the theme name shouldn't ever generate this warning.

Change History (4)

#1 @swissspidy
21 months ago

  • Component GeneralI18N
  • Milestone Awaiting Review
  • Resolutionworksforme
  • Status newclosed

One of my installed plugins has these lines in its initialization function:

How is this function defined and when is it called? Please share the full code.

This warning is only triggered if you trigger a translation call too early, for example if you are not waiting until init or so.

You should wait until after_setup_theme to call wp_get_theme(). If you do that, then you would not get such a warning.

I honestly think this is an issue from WordPress Core, as this _ _toString() call to get the theme name shouldn't ever generate this warning.

Why not? The WP_Theme::__toString() docblock explicitly says the string will be translated. And if you trigger a translation too early, then you should get a warning.
Changing the __toString() method would be a back compat break.

If you want the untranslated theme name, which for your code example would be desired anyway, use $current_theme->get( 'Name' ) or simply get_stylesheet()

#2 @rinkalpagdar
21 months ago

  • Component I18NUsers
  • Keywords needs-testing reporter-feedback added
  • Severity normalcritical
  • Summary Wrong warning about _load_textdomain_just_in_time used incorrectly if $theme->__toString() is called in plugin initializationInvalid username
  • Version 6.76.7.1
Last edited 21 months ago by rinkalpagdar (previous) (diff)

#3 @swissspidy
21 months ago

  • Component UsersI18N
  • Keywords needs-testing reporter-feedback removed
  • Severity criticalnormal
  • Summary Invalid usernameWrong warning about _load_textdomain_just_in_time used incorrectly if $theme->__toString()
  • Version 6.7.16.7

@rinkalpagdar maybe it was done by accident, please don't hijack a ticket like that :)

#4 @rinkalpagdar
21 months ago

Hello @swissspidy
There was some misunderstanding because I had created a ticket with the same number.
I have added a screenshot below.
https://go.screenpal.com/watch/cZXO6YnVJ6v

Note: See TracTickets for help on using tickets.