Make WordPress Core

Opened 7 months ago

Closed 7 months ago

Last modified 7 months ago

#62564 closed defect (bug) (worksforme)

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

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

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
7 months ago

  • Component changed from General to I18N
  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

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
7 months ago

  • Component changed from I18N to Users
  • Keywords needs-testing reporter-feedback added
  • Severity changed from normal to critical
  • Summary changed from Wrong warning about _load_textdomain_just_in_time used incorrectly if $theme->__toString() is called in plugin initialization to Invalid username
  • Version changed from 6.7 to 6.7.1
Last edited 7 months ago by rinkalpagdar (previous) (diff)

#3 @swissspidy
7 months ago

  • Component changed from Users to I18N
  • Keywords needs-testing reporter-feedback removed
  • Severity changed from critical to normal
  • Summary changed from Invalid username to Wrong warning about _load_textdomain_just_in_time used incorrectly if $theme->__toString()
  • Version changed from 6.7.1 to 6.7

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

#4 @rinkalpagdar
7 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.