rockiger / reactpress

Plugin that lets you easily create, build and deploy React apps into your existing WordPress sites.
https://rockiger.com/en/reactpress/
GNU General Public License v2.0
51 stars 7 forks source link

[SOLUTION] Templates from repr_add_page_template() have malformed PATH in Windows #27

Closed sbettwieser closed 1 year ago

sbettwieser commented 1 year ago

SOLVES:

Warning: include(C:somepathpluginsreactpress/templates/react-page-template.php): failed to open stream: No such file or directory in C:\some\path\wp-includes\template-loader.php on line 106

Warning: include(): Failed opening ‘C:somepathpluginsreactpress/templates/react-page-template.php’ for inclusion (include_path=’someotherpath\PEAR’) in C:\some\path\wp-includes\template-loader.php on line 106

Q: Why does this happen? A: The Windows environment uses \ for directory separators. While normally this is represented internally in strings as \\, it is very likely that if these are present, at some point WordPress or ReactPress will print or otherwise resolve its value, causing it to act as an escape character in future strings.

What is the code?

Templates loaded in reactpress/admin/Admin.php from line 188:

public function repr_add_page_template($templates) {
    $templates[REPR_PLUGIN_PATH . 'templates/empty-react-page-template.php'] = __('ReactPress Canvas', 'text-domain');
    $templates[REPR_PLUGIN_PATH . 'templates/react-page-template.php'] = __('ReactPress Full Width', 'text-domain');

    return $templates;
}

REPR_PLUGIN_PATH defined in reactpress/reactpress.php from line 58:

define('REPR_PLUGIN_PATH', plugin_dir_path(__FILE__));

This definition resolves to "C:\\some\\path\\plugins\\reactpress/".

It should be noted that other defined PATHs and URLs in reactpress.php do make a distinction between Windows vs. standard separators, as seen on the line directly above the REPR_PLUGIN_PATH definition (shown below):

define('REPR_PLUGIN_URL', IS_WINDOWS ? str_replace('\\', '/', plugin_dir_path(__FILE__)) : plugin_dir_url(__FILE__));

RECOMMENDATION:

Rewrite the REPR_PLUGIN_PATH definition to:

define('REPR_PLUGIN_PATH', IS_WINDOWS ? str_replace('\\', '/', plugin_dir_path(__FILE__)) : plugin_dir_path(__FILE__));

I have tested this on my machine to verify that it works. This should not cause any issues with non-Windows environments and will improve compatibility for Windows users.

I sincerely hope you make this adjustment, as I have spent nearly 3 hours debugging this for what ended up being a simple fix that you, being intimately familiar with the code, may have been able to catch within minutes.

rockiger commented 1 year ago

Thanks awesome. I will update it this weekend.