• Hi,

    I’m actually a premium user, but your customer support page is currently 404 ?? (it happens to all of us!) so I’m leaving this here instead, mostly for the sake of those getting the same errors and needing a simple fix.

    The mentioned error will flood the PHP/webserver logs, essentially complaining that the method doing_rest_api() has tried to access $current_url['path'], but when path is actually not a key on this array, under recent versions of PHP (8.2 for instance), PHP stops execution with an error.

    On the free version, this method is under the class WAPT_Plugin, defined on the includes/class-plugin.php file, at the very end (line 544 or so).

    On the premium version, this method is placed in the class WAPT_Premium, defined in includes/class.waptp.php, and the error is given at line 122 (at the end of the declaration). It’s essentially the same thing.

    From what I understand, this method attempts to figure out if the current path passed to WP includes a REST request (usually given by having wp-json/ in the path), since, at the moment this plugin was written, WordPress didn’t seem to have such a function available for plugin developers.

    Former PHP versions were (probably) more lenient and less strict on the error-checking, thus this access (probably) just returned a NULL, which would eventually be caught by the strpos() at the end, and harmlessly reply with the equivalent of ‘no REST path found here’.

    These days, however, accessing an associative array with a key that doesn’t exist is an error (think a bit — that’s actually quite sound reasoning from the PHP core developers!). So, an additional check should have been made before using the value of $current_url['path'], to make sure it’s a valid key.

    A quick & dirty method is just to replace the last line of the method, which currently has

    return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;

    and change it simply to

    return strpos( $current_url['path'] ?? '/', $rest_url['path'], 0 ) === 0;

    Don’t you love the ternary operator ?? I’m a huge fan of it! What the above code essentially does is the equivalent of (the more readable PHP5-style code):

    return strpos( empty( $current_url['path'] ) ? '/' : $current_url['path'], $rest_url['path'], 0 ) === 0;

    where empty( $var ) is conveniently the double-checking of is_null( $var ) || $var == ''.

    Anyway, this is a simple fix that will get rid of the error, but it might require a bit more of code polishing. In particular, the AFI/APT developer who wrote this method generously credited it to @matzeeable, who posted that code as one of the answers on https://wordpress.stackexchange.com/questions/221202/does-something-like-is-rest-exist — a very useful resource, having been updated over the years with several techniques to detect if the current request came from the REST subsystem.

    The problem is that @matzeeable’s solution, which was originally copied and used on the AFI/APT plugin, dates back to 2018 — before PHP 8.X and its much stricter requirements were upon us. @matzeeable has since then updated his answer on the WordPress StackExchange, and if you visit it today, you’ll see that there was, indeed, an update back in 2022, to better comply with the stricter demands of PHP 8.X.

    It seems that the AFI/APT developers just need to take a look at the ‘new’ code and adapt it to their own needs — although, to be honest, my very simple fix should be sufficient in most cases.

    Since WP 5.X, the WordPress core developers included a function which might avoid the need of maintaining one’s own code and make the plugin future-proof: wp_is_json_request(). Now, I’m not claiming that this function will be the best solution for the AFI/APT plugin. It is also mentioned on the WordPress StackExchange thread. It might be worth taking a look at it, so that things continue to work under WP 17.1.3 and PHP 10.9 ??

    I hope this is useful to others. Remember, you have different places to make changes, depending if you’re using the free or the premium version of AFI/APT.

  • The topic ‘[HAS QUICK FIX] Error: Undefined array key “path” (in method doing_rest_api())’ is closed to new replies.