• Resolved Inbound Horizons

    (@inboundhorizons)


    Over the last few weeks I have gotten a couple emails from my WordPress website about a fatal error caused by WooCommerce. I found the solution and wanted to share both the problem (so that it can be fixed in future versions) and the fix so anyone else who needs an immediate solution can resolve the issue on their own. My WooCommerce version is 8.1.1 but I think the error affects all versions starting from 8.0.0.

    An error of type E_PARSE was caused in line 521 of the file /content/wp-content/plugins/woocommerce/src/Internal/DataStores/Orders/CustomOrdersTableController.php. Error message: syntax error, unexpected ')'

    The code in question is lines 512-521 of woocommerce/src/Internal/DataStores/Orders/CustomOrdersTableController.php:

    $sync_message = sprintf(
    				// translators: %d: number of pending orders.
    				_n(
    					'Sync %d pending order. You can switch data storage for orders only when posts and orders table are in sync.',
    					'Sync %d pending orders. You can switch data storage for orders only when posts and orders table are in sync.',
    					$sync_status['current_pending_count'],
    					'woocommerce'
    				),
    				$sync_status['current_pending_count'],
    			);

    The issue is the very last comma at the end of “$sync_status['current_pending_count'],” on line 520. There are no arguments following it and thus it is unnecessary and breaks the sprintf() function which is expecting another argument. The solution is to simply remove the comma so that the final code like this.

    $sync_message = sprintf(
    				// translators: %d: number of pending orders.
    				_n(
    					'Sync %d pending order. You can switch data storage for orders only when posts and orders table are in sync.',
    					'Sync %d pending orders. You can switch data storage for orders only when posts and orders table are in sync.',
    					$sync_status['current_pending_count'],
    					'woocommerce'
    				),
    				$sync_status['current_pending_count']
    			);
Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Support Kader Ibrahim S. a11n

    (@kaderibrahim)

    Hello @inboundhorizons,

    Thank you for reaching out to WooCommerce support. I truly appreciate you reporting this issue as well as the solution.

    I checked the WooCommerce codebase and I can see that the issue is already addressed here.

    Once again my sincere thanks to you.

    Hi,
    you can upgrade you php version to >=7.4 to avoid this error.

    Thanks

    Thread Starter Inbound Horizons

    (@inboundhorizons)

    @kaderibrahim Thank you for your confirmation!

    Thread Starter Inbound Horizons

    (@inboundhorizons)

    @manuelmassella This particular website is running PHP v8.0. Sometimes changing to a more recent version of WordPress will work, but it also introduces its own challenges. When upgrading PHP you run the risk of breaking other plugins. And that’s assuming the website admin isn’t on shared hosting where he is unable to change the PHP version himself.

    Whenever possible it is best to make sure the code is as clean as possible. And the dev team is doing a great job of staying on top of that.

    Plugin Support Shameem (woo-hc)

    (@shameemreza)

    Hi @inboundhorizons

    It seems that this has been marked as resolved. Have you managed to get this sorted, or do you still need help resolving this?

    If the issue continues, we’re here and happy to help you out further.

    Looking forward to your response!

    Thread Starter Inbound Horizons

    (@inboundhorizons)

    Hi @shameemreza. Yes, this particular issue is resolved on my end.

    I have noticed that there seem to be a couple other places in the latest WooCommerce release where there are dangling commas like this one. I know this is a separate issue so I will merely comment on it. But as an example, in v8.1.1 in the file woocommerce\src\Internal\Admin\WcPayWelcomePage.php on lines 354-357 there is another dangling comma.

    $url = add_query_arg(
    $store_context,
    'https://public-api.wordpress.com/wpcom/v2/wcpay/incentives',
    );

    I was going to try a REGEX search through the codebase to identify any other places this issue occurred but unfortunately was not able to pull the information together in time. However, for anyone interested this REGEX ,\s*\) should be able to identify commas immediately preceding closing parentheses.

    Perhaps coupled with grep it would look like this: grep -rn ‘,\s*\)’ /woocommerce/directory/

    Plugin Support nicw.a11n

    (@nicw)

    Hi @inboundhorizons

    WooCommerce applies WordPress Coding Standards, and all our code is run through the WPCS code sniffer before release. WordPress coding standards specifically mentions the last comma in an array.

    You can find that recommendation here: https://developer.wordpress.org/coding-standards/wordpress-coding-standards/php/#indentation

    This is in keeping with modern PHP standards, first proposed in 2015 and implemented in 2017 in PHP 7.2

    https://wiki.php.net/rfc/list-syntax-trailing-commas

    Thread Starter Inbound Horizons

    (@inboundhorizons)

    @nicw,

    Thank you for that link to the guidelines. And yes, I do agree that arrays should use trailing commas on the last item to make them easier update and maintain. But the recent issues I have run into are with functions that have variable-length argument lists. https://www.php.net/manual/en/functions.arguments.php#functions.variable-arg-list. Both of the examples I cited in previous posts above are calls to variable-length functions with a dangling comma after the last argument.

    Is it possible that they are slipping through the WPCS code sniffer by using the same rules that check for a the last comma in an array?

    I realize now that my REGEX example from above would identify dangling commas in both arrays and functions even though it should specifically look for calls to functions only. I only meant for it to be used as an example. And unfortunately I don’t have spare time to delve into the WPCS project to help improve it.

    Thank you for your response!

    Plugin Support nicw.a11n

    (@nicw)

    Hi @inboundhorizons

    You’re quite right of course – I read the use-case completely incorrectly, and the dangling comma in the function call is an error!


    Currently there are no issues open on this, but I can confirm that our developers are aware of this as an issue, having had one reach out to me about this post.

    After some experimentation, it’s going to be difficult – as you suggest – to identify all occurrences. I’ll be consulting with the developers on this, on how best to raise this as an issue in the repository.

    Thank you for the feedback, and the time you’ve spent on this!

    Thread Starter Inbound Horizons

    (@inboundhorizons)

    @nicw, thank you again or your response and support! I hope that my previous comments may have been useful in some capacity.

    I appreciate the quick and thorough responses.

    Plugin Support nicw.a11n

    (@nicw)

    Hi @inboundhorizons

    I’ve been in touch with our engineers on this, and it appears that trailing commas are acceptable in function calls from PHP 7.3 upwards. Since WooCommerce requires PHP 7.4, therefore, the trailing commas might not be an issue after all.

    The RFC can be found here: https://wiki.php.net/rfc/trailing-comma-function-calls

    Researching this, I found these two links which you might find interesting

     https://github.com/squizlabs/PHP_CodeSniffer/issues/203

    https://github.com/WordPress/WordPress-Coding-Standards/issues/1363

    WordPress also works with PHP 5.6.20+ and MySQL 5.0+, but these versions have reached official End Of Life and as such may expose your site to security vulnerabilities.

    This is a fairly complex development topic. I’m going to leave it open for a bit to see if anyone is able to chime in to help you out.

    I can also recommend the WooCommerce Developer Resources Portal for resources on developing for WooCommerce.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Thread Starter Inbound Horizons

    (@inboundhorizons)

    @nicw Thank you for your reply. I apologize for the late follow-up.

    I am able to update all of my WooCommerce websites to at least PHP v7.3 so the original issue I reported no longer affects me.

    As you say, since WooCommerce has moved to require PHP 7.4 the trailing commas can technically be considered valid code and not an issue for the development team moving forward. You’ll just have to point that out to anyone else who also complains about this issue after me. 🙂 https://developer.woocommerce.com/2023/06/05/new-requirement-for-woocommerce-8-2-php-7-4/. As a side note if anyone was wondering, the current version of WordPress v6.4 still supports PHP v7.0. https://make.wordpress.org/core/handbook/references/php-compatibility-and-wordpress-versions/.

    It will be up to the WooCommerce development team to decide whether they want to continue keeping the trailing comma in functions with variable arguments as a coding standard or not. I personally do not use variable arguments often enough in my own code to feel comfortable leaving a dangling comma like I would with array elements. But that is a personal preference.

    And in this situation, I don’t think that it will be worth the time for a developer to track down all the locations where there is a dangling comma in order to maintain backwards compatibility with a PHP version no longer officially supported.

    Once again, thank you for your feedback and thorough responses!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Syntax Error – Location and Suggested Fix’ is closed to new replies.