Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The

WPMLSuperGlobalsServer::getServerName

function relies on the

$_SERVER

superglobal for HTTP host information, which is not available in the CLI environment. Consequently, the

WPML_URL_Converter_Domain_Strategy::convertRestUrlToCurrentDomain

function fails to retrieve the correct host name, rendering REST URLs generated in the CLI environment invalid.
Solution:
We tested the scenario using WPML version 4.5.15 with no other plugins active. We used the following code to simulate the environment and did not encounter the issue:

if (defined('WP_CLI') && WP_CLI) {<br />    class ExampleCommand extends \WP_CLI_Command {<br />        /**<br />         * @subcommand wpml-rest-api<br />         */<br />        public function wpmlRestApi() {<br />            echo get_rest_url(path: 'wp/v2') . "\n";<br />        }<br />    }<br /><br />    WP_CLI::add_command('example', ExampleCommand::class);<br />}

We recommend you provide more details or a screen recording of the issue to help us better understand and address the problem.

If this solution does not apply to your case, or if it seems outdated, please check the related known issues and confirm that you have installed the latest versions of themes and plugins. If the issue persists, we highly recommend opening a new support ticket at WPML support forum.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: 

This topic contains 5 replies, has 1 voice.

Last updated by romansS 1 week, 2 days ago.

Assisted by: Marcel.

Author Posts
November 26, 2024 at 5:07 pm #16446423

romansS

Background of the issue:

WPMLSuperGlobalsServer::getServerName

function unconditionally relies on the existence of information about HTTP host in the

$_SERVER

superglobal. But it is not true for CLI environment, so this function returns the empty value in this case.

WPML_URL_Converter_Domain_Strategy::convertRestUrlToCurrentDomain-

unconditionally relies on correct value being returned by

WPMLSuperGlobalsServer::getServerName

and due to the reason described above it wipes out host name of the URL. As a result REST URLs generated in CLI environment are not usable and not valid at all

Symptoms:
REST URLs generated in the CLI environment are not usable and not valid due to the absence of HTTP host information.

Questions:

November 26, 2024 at 5:11 pm #16446435

romansS

No idea what should be here...

November 28, 2024 at 3:28 pm #16456135

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+01:00)

Hi,

can you please post the WP-CLI command on how to reproduce the problem?

Thank You!

Best Regards,
Marcel

November 29, 2024 at 1:23 pm #16459711

romansS

Here is the example:

class ExampleCommand extends \WP_CLI_Command
{
    /**
     * @subcommand wpml-rest-api
     */
    public function wpmlRestApi(): void
    {
        echo get_rest_url(path: 'wp/v2') . "\n";
    }
}

WP_CLI::add_command('example', ExampleCommand::class);

And here is the use of it:

>wp example wpml-rest-api
<em><u>hidden link</u></em>

In case the hook that is added by the \WPML_URL_Converter_Domain_Strategy::add_hooks() function get removed - it starts to work properly:

>wp example wpml-rest-api
<em><u>hidden link</u></em>
December 2, 2024 at 1:29 pm #16466284

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+01:00)

Thank you for the information! We'll review this and get back to you with an update as soon as we have more details.

Best regards,
Marcel

January 14, 2025 at 2:39 pm #16593465

Marcel
Supporter

Languages: English (English ) German (Deutsch )

Timezone: Europe/Madrid (GMT+01:00)

Hi,

we’re unable to reproduce the issue. We tested this code with WPML 4.5.15, ensuring no other plugins were active during the test.

if (defined('WP_CLI') && WP_CLI) {
    class ExampleCommand extends \WP_CLI_Command {
        /**
         * @subcommand wpml-rest-api
         */
        public function wpmlRestApi() {
            echo get_rest_url(path: 'wp/v2') . "\n";
        }
    }

    WP_CLI::add_command('example', ExampleCommand::class);
}

I've attached a screenshot from the CLI for reference. Could you please provide more details or a screen recording of the issue? A video showing all the steps to reproduce the behavior would be very helpful, as we’ll use it to try and replicate the issue on our end.

Best Regards,
Marcel

Screenshot 2025-01-09 at 16.09.45.png
January 21, 2025 at 3:03 pm #16619524

romansS

I can confirm that issue is not directly in WPML code, probably it is a combination of different factors, but I was unable to reproduce it using clean install.