Freemius / wordpress-sdk

https://freemius.com
GNU General Public License v3.0
258 stars 76 forks source link

E_WARNING: Undefined array key 1 #689

Open jhard opened 4 months ago

jhard commented 4 months ago

E_WARNING in /var/www/public/wp-content/plugins/tablepress/libraries/freemius/includes/class-freemius.php:14598 Undefined array key 1

If an ajax request is being made and the endpoint URL is not inside wp-admin, freemius triggers a warning because it's trying to access a non-existant array key.

The bug is in the static function get_current_page():

if ( is_network_admin() ) {
        preg_match( '#/wp-admin/network/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
} else if ( is_user_admin() ) {
        preg_match( '#/wp-admin/user/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
} else {
        preg_match( '#/wp-admin/?(.*?)$#i', $_SERVER['PHP_SELF'], $self_matches );
}
$pagenow = $self_matches[1];

preg_match will only fill self_matches if it actually matches something.

The solution is to check return values or using something like the null-coalescing operator, e.g. by turning the offending line into $pagenow = $self_matches[1] ?? "";.