PHP include() seems to be altering javascript syntax and breaking the code!

TL;DR:

In some unaltered, off-the-shelf plug-in code, the PHP include() statement seems to be altering a javascript file being sent to the client browser, causing a javascript syntax error. The include() statement works fine with the same javascript file on a simple test page. What could be the cause?

Details

On our WordPress Events Manager search page, there is a strange javascript syntax error that causes the Google Maps autocomplete location search to break.

Using the browser Dev Tools, I see that it is because of a javascript syntax error:

autocompleteService.getPlacePredictions( {‘input’: input.val(), ‘offset’: input.val().length }, function listentoresult(list, status) {
if(list != null && list.length != 0) {
placesService = new google.maps.places.PlacesService(document.getElementById(’em-search-geo-attr’));

On the 2nd line, the if(list != null && list.length != 0) { should be if(list != null && list.length != 0) {. The first ampersand has been encoded as &. The error is reported in the browser console as Uncaught SyntaxError: Invalid or unexpected token.

I did a lot of digging and found that this javascript code comes from wp-content/plugins/events-manager/templates/templates/search/geo.js. The file has not been modified in any way. There is no “override” javascript file anywhere else. In addition, I found that the file is included by the code em_locate_template(‘templates/search/geo.js’,true); in the wp-content/plugins/events-manager/templates/templates/search/geo.php file, and that the em_locate_template() function is in wp-content/plugins/events-manager/events-manager.php, and finally writes geo.js to the output using the PHP include() statement (include($located);). The $located variable contains the full path to the geo.js.

In geo.js, the javascript code is correct: if(list != null && list.length != 0) {. I don’t understand why it is being altered when it is sent to the browser, causing the interactive location search to fail.

To see if this was a systemic problem with the Linux server, I created a PHP test page (shown below), and when the geo.js is included in this simple test page, the javascript is not altered and no syntax errors occur. So it doesn’t appear to be anything related to the PHP or server configuration.

I’m running on WordPress v.4.9.9 with the current version of Events Manager (v.5.9.5).

It seems to boil down to the PHP include() statement behaving differently on the two pages. Why else would && be converted to && in one case but not the other?

PHP include() Test Page

The page is named “__tryme.php” and is located in the “home” WordPress folder. When I view the page source or inspect using the browser Dev Tools, there are no syntax errors. The && has not been altered.

<html>
<head>
<title>PHP include() test</title>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js”></script></head>
<body>

<h1>include() javascript test</h1>
<p>Let’s see what happens…</p>

<script type=”text/javascript”>
console.log(“I’m starting a javascript block and ‘include()’ing a *.js file…”);
<?php
include(‘./wp-content/plugins/events-manager/templates/templates/search/geo.js’);
?>
console.log(“The script file has been included.”);
</script>
</body>
</html>

The Dev Tools output from the page is below… no syntax errors:

__tryme.php:11 I’m starting a javascript block and ‘include()’ing a *.js file…
__tryme.php:111 The script file has been included.

Read more here:: PHP include() seems to be altering javascript syntax and breaking the code!

Leave a Reply

Your email address will not be published. Required fields are marked *