roots / sage

WordPress starter theme with Laravel Blade components and templates, Tailwind CSS, and a modern development workflow
https://roots.io/sage/
MIT License
12.75k stars 3.06k forks source link

Bug: WPML and Sage 10 - Undefined index: __laravel_slots #3034

Closed brunodeangelis closed 2 years ago

brunodeangelis commented 2 years ago

Terms

Description

Activating WPML or OTGS Installer crashes the frontend of the site. The exception is: "Undefined index: __laravel_slots". You can see more in this screenshot:

screencapture-localhost-3000-2022-05-09-16_10_29

I have tried switching themes and it works fine. It's only when using Sage 10 that this happens. Hasn't happened with Sage 9. Also tried reinstalling composer packages, doesn't fix the issue.

I'm really not sure what can be causing this. Any insight is greatly appreciated!

Steps To Reproduce

  1. Create new theme using Sage 10, on a fresh WP project
  2. Install and activate WPML or also OTGS Installer

Expected Behavior

Site frontend loading properly and WPML functioning as expected.

Actual Behavior

Site frontend crashes when WPML is active.

Relevant Log Output

ErrorException  ViewException  ViewException
HTTP 500 Internal Server Error
Undefined index: __laravel_slots (View: /Users/bruno/Sites/project-climate/wp-content/themes/pc/resources/views/partials/content-page.blade.php) (View: /Users/bruno/Sites/project-climate/wp-content/themes/pc/resources/views/partials/content-page.blade.php)
Exceptions 3Stack Traces 3
Illuminate\View\
ViewException
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/DynamicComponent.php (line 76)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/PhpEngine.php -> handleViewException (line 60)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/CompilerEngine.php -> evaluatePath (line 61)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> get (line 139)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> getContents (line 122)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> renderContents (line 91)
View->render()
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/index.php (line 14)
  <body <?php body_class(); ?>>    <?php wp_body_open(); ?>    <?php do_action('get_header'); ?>    <div id="app">      <?php echo view(app('sage.view'), app('sage.data'))->render(); ?>    </div>    <?php do_action('get_footer'); ?>    <?php wp_footer(); ?>  </body>
include('/Users/bruno/Sites/project-climate/wp-content/themes/pc/index.php')
in /Users/bruno/Sites/project-climate/wp-includes/template-loader.php (line 106)
require_once('/Users/bruno/Sites/project-climate/wp-includes/template-loader.php')
in /Users/bruno/Sites/project-climate/wp-blog-header.php (line 19)
require('/Users/bruno/Sites/project-climate/wp-blog-header.php')
in /Users/bruno/Sites/project-climate/index.php (line 17)
Illuminate\View\
ViewException
Undefined index: __laravel_slots (View: /Users/bruno/Sites/project-climate/wp-content/themes/pc/resources/views/partials/content-page.blade.php)

in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/DynamicComponent.php (line 76)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/PhpEngine.php -> handleViewException (line 60)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/CompilerEngine.php -> evaluatePath (line 61)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> get (line 139)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> getContents (line 122)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> renderContents (line 91)
View->render()
in /Users/bruno/Sites/project-climate/wp-content/cache/acorn/framework/views/e33edb274216d65ba4b4d170a2808d5b65a868ce.php (line 3)
<?php $__env->startSection('content'); ?>  <?php while(have_posts()): ?> <?php (the_post()); ?>  <?php echo $__env->first(['partials.content-page', 'partials.content'], \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?>  <?php endwhile; ?><?php $__env->stopSection(); ?><?php echo $__env->make('layouts.app', \Illuminate\Support\Arr::except(get_defined_vars(), ['__data', '__path']))->render(); ?><?php /**PATH /Users/bruno/Sites/project-climate/wp-content/themes/pc/resources/views/page.blade.php ENDPATH**/ ?>
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/filesystem/Filesystem.php require (line 107)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/filesystem/Filesystem.php :: Illuminate\Filesystem\{closure} (line 108)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/PhpEngine.php -> getRequire (line 58)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/CompilerEngine.php -> evaluatePath (line 61)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> get (line 139)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> getContents (line 122)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> renderContents (line 91)
View->render()
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/index.php (line 14)
include('/Users/bruno/Sites/project-climate/wp-content/themes/pc/index.php')
in /Users/bruno/Sites/project-climate/wp-includes/template-loader.php (line 106)
require_once('/Users/bruno/Sites/project-climate/wp-includes/template-loader.php')
in /Users/bruno/Sites/project-climate/wp-blog-header.php (line 19)
require('/Users/bruno/Sites/project-climate/wp-blog-header.php')
in /Users/bruno/Sites/project-climate/index.php (line 17)

ErrorException
Undefined index: __laravel_slots

in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/DynamicComponent.php (line 76)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/roots/acorn/src/Roots/Acorn/Bootstrap/HandleExceptions.php -> handleError (line 54)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/DynamicComponent.php -> handleError (line 76)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Component.php -> Illuminate\View\{closure} (line 84)
in /Users/bruno/Sites/project-climate/wp-content/plugins/sitepress-multilingual-cms/vendor/wpml/collect/src/Illuminate/Support/helpers.php -> Illuminate\View\{closure} (line 26)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Concerns/ManagesComponents.php value (line 97)
Factory->renderComponent()
in /Users/bruno/Sites/project-climate/wp-content/cache/acorn/framework/views/f9a33895e5e0c39e5b5346aeb992b46503d1b30c.php (line 16)
<?php $component = $__env->getContainer()->make(Illuminate\View\DynamicComponent::class, ['component' => $component_name]); ?><?php $component->withName('dynamic-component'); ?><?php if ($component->shouldRender()): ?><?php $__env->startComponent($component->resolveView(), $component->data()); ?><?php $component->withAttributes([]); ?><?php echo $__env->renderComponent(); ?><?php endif; ?><?php if (isset($__componentOriginal3bf0a20793be3eca9a779778cf74145887b021b9)): ?><?php $component = $__componentOriginal3bf0a20793be3eca9a779778cf74145887b021b9; ?><?php unset($__componentOriginal3bf0a20793be3eca9a779778cf74145887b021b9); ?><?php endif; ?>
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/filesystem/Filesystem.php require (line 107)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/filesystem/Filesystem.php :: Illuminate\Filesystem\{closure} (line 108)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/PhpEngine.php -> getRequire (line 58)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/CompilerEngine.php -> evaluatePath (line 61)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> get (line 139)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> getContents (line 122)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> renderContents (line 91)
View->render()
in /Users/bruno/Sites/project-climate/wp-content/cache/acorn/framework/views/e33edb274216d65ba4b4d170a2808d5b65a868ce.php (line 3)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/filesystem/Filesystem.php require (line 107)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/filesystem/Filesystem.php :: Illuminate\Filesystem\{closure} (line 108)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/PhpEngine.php -> getRequire (line 58)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/Engines/CompilerEngine.php -> evaluatePath (line 61)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> get (line 139)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> getContents (line 122)
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/vendor/illuminate/view/View.php -> renderContents (line 91)
View->render()
in /Users/bruno/Sites/project-climate/wp-content/themes/pc/index.php (line 14)
include('/Users/bruno/Sites/project-climate/wp-content/themes/pc/index.php')
in /Users/bruno/Sites/project-climate/wp-includes/template-loader.php (line 106)
require_once('/Users/bruno/Sites/project-climate/wp-includes/template-loader.php')
in /Users/bruno/Sites/project-climate/wp-blog-header.php (line 19)
require('/Users/bruno/Sites/project-climate/wp-blog-header.php')
in /Users/bruno/Sites/project-climate/index.php (line 17)

Versions

Sage 10.1.4 - macOS Monterey 12.3.1 - PHP 7.4.21 - Apache 2.4.46

ckyoung commented 2 years ago

Hi, I've investigated this issue, and I found out that the error was due to WPML using the same helper function that Laravel/Acorn blade component used behind the scene, specifically the value() helper function. Since WPML first loaded before the theme, any helper function previously declared can't be redeclare, in this case redeclaring by theme functions.php.

WPML I think is using an older version of value() helper function here, that only have single parameter. As you can see the code below:

<?php

if (!function_exists('value')) {
    /**
     * Return the default value of the given value.
     *
     * @param mixed $value
     *
     * @return mixed
     */
    function value($value)
    {
        return $value instanceof Closure ? $value() : $value;
    }
}

And as I checked the Laravel ManageComponents trait that use to render a component, you can see that the value() helper function has a second parameter. Which you can also see on Laravel/Acorn value() helper function version, that indeed accepts multiple arguments.

<?php

public function renderComponent()
{
    $view = array_pop($this->componentStack);

    $this->currentComponentData = array_merge(
        $previousComponentData = $this->currentComponentData,
        $data = $this->componentData()
    );

    try {
        $view = value($view, $data);

        if ($view instanceof View) {
            return $view->with($data)->render();
        } elseif ($view instanceof Htmlable) {
            return $view->toHtml();
        } else {
            return $this->make($view, $data)->render();
        }
    } finally {
        $this->currentComponentData = $previousComponentData;
    }
}
if (! function_exists('value')) {
    /**
     * Return the default value of the given value.
     *
     * @param  mixed  $value
     * @return mixed
     */
    function value($value, ...$args)
    {
        return $value instanceof Closure ? $value(...$args) : $value;
    }
}

Right now, my solution for this is to create an MU-plugin and declare the value() function same with Laravel/Acorn version. This is because MU-plugins will load first before any normal plugins, in this case WPML.


<?php
/**
 * Plugin Name:  Fix WPML Helper Functions Conflict 
 * Version:      1.0.0
 * License:      MIT License
 */

if (!function_exists('value')) {
    /**
     * Return the default value of the given value.
     *
     * @param  mixed  $value
     * @return mixed
     */
    function value($value, ...$args)
    {
        return $value instanceof Closure ? $value(...$args) : $value;
    }
}
?>
brunodeangelis commented 2 years ago

@ckyoung my team has confirmed that this solution works. Thank you very much mate!