BUG: Saving a serialized PHP array in a textarea field as text returns error

Support General BUG: Saving a serialized PHP array in a textarea field as text returns error

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #50144
    Ahmed E.Ahmed E.
    Participant

    Hi there,

    I have a custom model with a textarea field named 'meta_value' that I want to accept any type of text, including custom code.

    
    [
                'name'              => 'Meta Value',
                'id'                => $prefix . 'meta_value',
                'type'              => 'textarea',
                'rows'               => 10,
                // 'required'          => true,
                'columns'           => 12,
                'sanitize_callback' => 'none',
             ],
    

    When the field has a serialized PHP Array that is saved as plain text, it returns an error on the editing page.
    Here's the complete error trace:

    
    Uncaught Error: htmlspecialchars(): Argument #1 ($string) must be of type string, array given
    in \wp-includes\formatting.php on line 4731
    
    Call stack:
    1. htmlspecialchars(array, 3, 'UTF-8')
    wp-includes/formatting.php:4731
    
    2. esc_textarea(array)
    wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box/inc/fields/textarea.php:21
    
    3. RWMB_Textarea_Field::html(array, array)
    wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box/inc/field.php:533
    
    4. RWMB_Field::call(array, 'html', array)
    wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box/inc/field.php:36
    
    5. RWMB_Field::show(array, true, '7')
    wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box/inc/field.php:533
    
    6. RWMB_Field::call('show', array, true, '7')
    wp-content/plugins/meta-box-aio/vendor/meta-box/meta-box/inc/meta-box.php:225
    
    7. RW_Meta_Box::show(NULL, array)
    wp-admin/includes/template.php:1458
    
    8. do_meta_boxes(WP_Screen, 'normal', NULL)
    wp-content/plugins/meta-box-aio/vendor/meta-box/mb-custom-table/views/edit.php:33
    
    9. include('C:\Users\PC\Local Si...table\views\edit.php')
    wp-content/plugins/meta-box-aio/vendor/meta-box/mb-custom-table/src/Model/Admin.php:279
    
    10. MetaBox\CustomTable\Model\Admin::render('')
    wp-includes/class-wp-hook.php:341
    
    11. WP_Hook::apply_filters('', array)
    wp-includes/class-wp-hook.php:365
    
    12. WP_Hook::do_action(array)
    wp-includes/plugin.php:522
    
    13. do_action('****')
    wp-admin/admin.php:264
    

    I think it is caused because the plugin aggressively unserializes the values (maybe_unserialize()).

    #50146
    PeterPeter
    Moderator

    Hello Ahmed,

    Thanks for reaching out and for your feedback.

    Yes, you are correct. The function maybe_unserialize in the storage of custom table interferes with the serialized array and causes the issue. I suggest you use the custom code below to fix the issue

    
    add_filter( 'rwmb_raw_meta', function ( $value, $field, $object_id ) {
          if ( ( $field['id'] ?? '' ) !== 'your_field_id' ) {
                  return $value;
          }
    
          $storage = $field['storage'] ?? null;
          if ( ! $object_id || ! $storage instanceof \MetaBox\CustomTable\Storage ) {
                  return $value;
          }
    
          $row = \MetaBox\CustomTable\Cache::get( $object_id, $storage->table );
    
          return $row[ $field['id'] ] ?? $value;
    }, 10, 3 );
    

    I'm going to escalate the issue to the development team to fix it as soon as possible.

    #50147
    Ahmed E.Ahmed E.
    Participant

    Hello Peter,
    Thanks for your reply.

    Ok, I will be waiting for a fix in the plugin soon.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.