Make WordPress Core


Ignore:
Timestamp:
02/07/2023 03:32:43 AM (3 years ago)
Author:
peterwilsoncc
Message:

Formatting: Guard wp_strip_all_tags() against fatal errors.

Check the input of wp_strip_all_tags() before passing it to strip_tags(). This protects against fatal errors introduced in PHP 8, retaining the E_USER_WARNING from PHP 7, and prevents a PHP 8.1 deprecation notice when passing null.

Props chocofc1, costdev, jrf, dd32, audrasjb, peterwilsoncc.
Fixes #56434.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r55209 r55245  
    53965396 */
    53975397function wp_strip_all_tags( $text, $remove_breaks = false ) {
     5398    if ( is_null( $text ) ) {
     5399        return '';
     5400    }
     5401
     5402    if ( ! is_scalar( $text ) ) {
     5403        /*
     5404         * To maintain consistency with pre-PHP 8 error levels,
     5405         * trigger_error() is used to trigger an E_USER_WARNING,
     5406         * rather than _doing_it_wrong(), which triggers an E_USER_NOTICE.
     5407         */
     5408        trigger_error(
     5409            sprintf(
     5410                /* translators: 1: The function name, 2: The argument number, 3: The argument name, 4: The expected type, 5: The provided type. */
     5411                __( 'Warning: %1$s expects parameter %2$s (%3$s) to be a %4$s, %5$s given.' ),
     5412                __FUNCTION__,
     5413                '#1',
     5414                '$text',
     5415                'string',
     5416                gettype( $text )
     5417            ),
     5418            E_USER_WARNING
     5419        );
     5420
     5421        return '';
     5422    }
     5423
    53985424    $text = preg_replace( '@<(script|style)[^>]*?>.*?</\\1>@si', '', $text );
    53995425    $text = strip_tags( $text );
Note: See TracChangeset for help on using the changeset viewer.