[wp-trac] [WordPress Trac] #56010: apache_mod_loaded returns fatal TypeError in some environments with PHP 8

WordPress Trac noreply at wordpress.org
Mon Jun 20 06:56:31 UTC 2022


#56010: apache_mod_loaded returns fatal TypeError in some environments with PHP 8
--------------------------+-----------------------------
 Reporter:  engahmeds3ed  |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  critical      |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 When we call WP Core function (apache_mod_loaded) in environment like WP
 Engine which has the PHP function (apache_get_modules) already disabled
 also with php 8 or above will lead to the following fatal error:


 {{{
 PHP Fatal error: Uncaught TypeError: in_array(): Argument #2 ($haystack)
 must be of type array, null given in /<redacted>/wp-
 includes/functions.php:5834\nStack trace:
 #0 /<redacted>/wp-includes/functions.php(5834): in_array('mod_pagespeed',
 NULL, true)
 #1 /<redacted>/wp-content/plugins/wp-
 rocket/inc/ThirdParty/Plugins/ModPagespeed.php(52):
 apache_mod_loaded('mod_pagespeed', false)
 }}}

 and this is happening because of the following code:

 https://github.com/WordPress/WordPress/blob/master/wp-
 includes/functions.php#L5890

 Here we get the return value from (apache_get_modules) which is this cases
 returns null because it's already disabled by the hosting company then
 pass this null as a second argument to the in_array function which expects
 an array.

 Before PHP 8, we may see only notice in logs but after PHP 8 we get fatal
 error which blocks the site completely.

 Possible solutions:-
 1. Cast the value passed to (in_array) to be array (but this will return
 not valid values in such mentioned environments)
 2. Add the following condition along with the check if the function exists
 or not like this
 {{{#!php
 <?php
 if ( function_exists( 'apache_get_modules' ) && false === strpos( ini_get(
 'disable_functions' ), 'apache_get_modules' ) ) {
 }}}
 to make sure that this function isn't in the disabled functions.

 Whatever solution we will take, I can work on it.
 Thanks

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56010>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list