WordPress.org

Make WordPress Core

Opened 2 years ago

Last modified 4 weeks ago

#41498 new feature request

Why are global functions not wrapped in function_exists?

Reported by: doecode Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: I18N Keywords:
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Some time ago I've written a guide about integrating Laravel into WordPress, https://nehalist.io/integrating-laravel-into-wordpress/. Since Laravel now uses it's own __ function this integration has become impossible, because WordPress does not wrap global functions within function_exists.

Cannot redeclare __() (previously declared in /var/www/test.dev/public_html/wp-laravel-integration/app/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:821)

Attachments (1)

patch.diff (9.4 KB) - added by jonasemde 6 months ago.
Patch

Download all attachments as: .zip

Change History (11)

#1 @SergeyBiryukov
2 years ago

  • Description modified (diff)

Related: #17268

#2 @SergeyBiryukov
16 months ago

#43645 was marked as a duplicate.

#3 @swissspidy
6 months ago

#45973 was marked as a duplicate.

@jonasemde
6 months ago

Patch

#4 follow-up: @dd32
6 months ago

Is wrapping with function_exists() actually compatible? Wouldn't this cause any sites to loose translations if other-project didn't support the WordPress translations? Shouldn't Laravel wrap it in function_exists() as well? (and loose translation support if imported into WordPress) etc.

#5 @jonasemde
6 months ago

Shouldn't Laravel wrap it in function_exists() as well?

They do this already for all global helper functions included "()": https://github.com/laravel/framework/blob/5.7/src/Illuminate/Foundation/helpers.php

In cases, Laravel is loaded before WordPress inside a composer project an exception is thrown by WordPress.

A function_exists() wrap should solve this issue and not cause any issues on existing sites. Developers are more flexible with this approach.

The same is true for other integrations like:
https://modernmodules.com/plugins/magento-2-wordpress-integration-plugin/

To get this plugin to work you need to hack the /wp-includes/l10n.php file as well. This could be handled a lot cleaner if a function_exists() check is performed.

#6 in reply to: ↑ 4 @jonasemde
6 months ago

#7 follow-up: @swissspidy
5 months ago

Wouldn't this cause any sites to loose translations if other-project didn't support the WordPress translations?

@jonasemde Can you address this question as well?

#8 in reply to: ↑ 7 @jonasemde
4 months ago

This thread is about wrapping the __(...) function to be more flexible as a developer.

Out of the box, this will not cause any issues because if no __(...) function is declared earlier the WordPress default is loaded.

But development wise we gain some flexibility.

This allows initializing a __(...) adapter function before the WordPress PHP-files are loaded. In case of a Laravel/WordPress it could be something like that:

<?php
function __(...$args) {
    // Check if laravel translation exists:
    if(Lang::has(...$args)) {
        return app('translator')->getFromJson(...$args);
    }

    // WordPress as fallback
    return translate(...$args);
}

So this did not cause any sites to lose translations.

Replying to swissspidy:

Wouldn't this cause any sites to loose translations if other-project didn't support the WordPress translations?

@jonasemde Can you address this question as well?

#9 @ocean90
4 weeks ago

#47584 was marked as a duplicate.

#10 @fishpig
4 weeks ago

This is something I would recommend also. It provides flexibilty for developers to integrate WordPress into other applications and other applications into WordPress.

Any chance of this happening?

Note: See TracTickets for help on using tickets.