Skip to content

Fix early translation loading warning in Checkout controller#583

Open
proteusbr1 wants to merge 1 commit intopagarme:masterfrom
proteusbr1:fix/early-translation-loading-warning
Open

Fix early translation loading warning in Checkout controller#583
proteusbr1 wants to merge 1 commit intopagarme:masterfrom
proteusbr1:fix/early-translation-loading-warning

Conversation

@proteusbr1
Copy link

Qual o tipo de PR é esse? (marque todos os aplicáveis)

  • Refatoração
  • Adição de funcionalidade
  • Correção de bug
  • Otimização
  • Atualização de documentação

Descrição

Este PR resolve o aviso _load_textdomain_just_in_time que ocorria porque o construtor do Checkout controller estava chamando funções de tradução (__()) muito cedo (durante o hook plugins_loaded), antes que o sistema de tradução do WordPress estivesse totalmente pronto.

Alterações realizadas:

  • Implementação de Lazy Loading para a propriedade $payment_methods.
  • A inicialização da propriedade (e suas traduções) foi movida para o hook init (prioridade 20), garantindo que o textdomain já esteja carregado.
  • Adicionado o método get_payment_methods() para acesso seguro.
  • A propriedade foi mantida para garantir retrocompatibilidade, mesmo parecendo não ser utilizada no código atual (adicionado comentário deprecado).

Cenários testados

  • Análise Estática de Código: Verificado que a lógica de inicialização foi removida do construtor e corretamente alocada no método load_payment_methods_translations.
  • Verificação de Regressão: A estrutura da classe foi mantida (propriedades protegidas) para evitar quebras em classes que possam estender este controller.

This commit resolves the '_load_textdomain_just_in_time' warning caused by premature translation calls in the Checkout controller constructor.

Changes:
- Implemented lazy loading for the '' property.
- Translations are now loaded on the 'init' hook (priority 20) instead of 'plugins_loaded'.
- Added 'get_payment_methods()' method for safe access.
- Marked the property as deprecated/unused based on static analysis, but preserved it for backward compatibility.
Copilot AI review requested due to automatic review settings January 2, 2026 16:35
@backstage-catalog-validator
Copy link

⚠️ Este repositório ainda não está catalogado no Backstage. ⚠️

Por favor, catalogue-o seguindo as instruções nesta documentação. [Via VPN].

💁 Qualquer problema ou dúvida, estamos no Slack, basta abrir um ticket no canal #help-foundation-platform.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR addresses a _load_textdomain_just_in_time warning that occurred when translation functions were called too early in the Checkout controller's constructor, before WordPress's translation system was fully initialized.

Key Changes:

  • Deferred translation loading by moving payment methods initialization from the constructor to an init hook (priority 20)
  • Implemented lazy loading pattern with a new get_payment_methods() getter method
  • Added a $translations_loaded flag to prevent duplicate initialization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}

/**
* Loads payment methods translations after init
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

The method load_payment_methods_translations() is declared as public to be used as a WordPress action callback, but it's an internal initialization method. While this is a common pattern in WordPress, consider adding a documentation note explaining why it must be public (e.g., "Public visibility required for WordPress action hook callback").

Suggested change
* Loads payment methods translations after init
* Loads payment methods translations after init.
*
* Public visibility required for WordPress action hook callback.
*

Copilot uses AI. Check for mistakes.
Comment on lines +100 to +101
// If accessed before init hook fired but translations are needed
if (empty($this->payment_methods) && did_action('init')) {
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

The condition logic is flawed. If get_payment_methods() is called before the init hook fires, the method will return an empty array even though translations are needed. The condition did_action('init') returns 0 (falsy) before the hook fires, so the check empty($this->payment_methods) && did_action('init') will be false, and translations won't be loaded.

Consider inverting the logic to: if (!$this->translations_loaded && did_action('init')) to ensure translations are loaded whenever the init hook has fired, or return a meaningful result when called too early.

Suggested change
// If accessed before init hook fired but translations are needed
if (empty($this->payment_methods) && did_action('init')) {
// Load translations once init has fired and they haven't been loaded yet
if (!$this->translations_loaded && did_action('init')) {

Copilot uses AI. Check for mistakes.

/**
* @var array
* @deprecated This property appears to be unused in the codebase but is kept for backward compatibility.
Copy link

Copilot AI Jan 2, 2026

Choose a reason for hiding this comment

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

The deprecation notice format doesn't follow PHPDoc standards. Standard deprecation notices should use the @deprecated tag with a version number and optionally suggest an alternative. Consider formatting as: @deprecated since x.x.x Use get_payment_methods() instead. This property is kept for backward compatibility.

Suggested change
* @deprecated This property appears to be unused in the codebase but is kept for backward compatibility.
* @deprecated since 1.0.0 This property appears to be unused in the codebase and is kept for backward compatibility.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant