wordpress-fieldmanager icon indicating copy to clipboard operation
wordpress-fieldmanager copied to clipboard

TextArea and RichTextArea fields cause a warning in PHP 8.1

Open matthewmcvickar opened this issue 1 year ago • 1 comments

Description of the bug

Empty FieldManager_TextArea fields (e.g., ones on new post pages, or ones that have never been given a value) show a warning in PHP 8.1+.

This appears to be happening because esc_textarea() is run on the value when rendering the textarea, and the esc_textarea() function uses htmlspecialchars(), and htmlspecialchars() no longer accepts null without a warning as of PHP 8.1.

A workaround: if the FieldManager_TextArea is initiated with a default_value set to '' (empty string), this warning does not happen, presumably because then the htmlspecialchars() function runs on an empty string instead of null.

Steps To Reproduce

  1. Configure a field manager with a textarea field, like this example adapted from the FieldManager homepage:

    add_action( 'fm_post_post', function() {
      $fm = new Fieldmanager_Group( array(
        'name' => 'contact_information',
        'children' => array(
          'name' => new Fieldmanager_TextField( 'Name' ),
          'description' => new Fieldmanager_TextArea( 'Description' ),
        ),
      ) );
      $fm->add_meta_box( 'Contact Information', 'post' );
    } );
    
  2. Load the page in an environment running PHP 8.1.

  3. Notice the error in the PHP error log:

    PHP Deprecated:  htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated in /wp/wp-includes/formatting.php on line 4732
    

Additional Information

When I created this ticket, I thought this warning was only happening for textareas in repeatable groups, but it actually applies to all textareas.

matthewmcvickar avatar Jan 29 '24 21:01 matthewmcvickar

It appears this issue was fixed via https://github.com/alleyinteractive/wordpress-fieldmanager/pull/866 (see file diff), and so I'm going to close it.

matthewmcvickar avatar Oct 21 '24 19:10 matthewmcvickar