[wp-trac] [WordPress Trac] #53625: The 'explode' function or something else does not work on widget block editor. (was: The 'explode' function does not work on widget block editor.)

WordPress Trac noreply at wordpress.org
Mon Jul 12 12:28:38 UTC 2021


#53625: The 'explode' function or something else does not work on widget block
editor.
-----------------------------------------------+---------------------------
 Reporter:  subrataemfluence                   |       Owner:  (none)
     Type:  defect (bug)                       |      Status:  reopened
 Priority:  normal                             |   Milestone:
Component:  Widgets                            |     Version:  trunk
 Severity:  major                              |  Resolution:
 Keywords:  has-screenshots reporter-feedback  |     Focuses:
                                               |  administration
-----------------------------------------------+---------------------------
Changes (by subrataemfluence):

 * status:  closed => reopened
 * resolution:  invalid =>


Comment:

 Hi @zieladam thanks for looking into the `array_pop()` thing. It was a
 mistake which now I have corrected and no more empty string.

 But I am almost certain that either the `explode()` functionality is not
 working or there is something else that is preventing it from working
 properly in widget block mode. Because everything works as expected when
 the block support is removed from the theme.

 So I am reopening the ticket with more test cases and their results.

 Here are my test cases:

 {{{#!php
 <?php
 function update( $old_instance, $new_instance ) {
    $instance = $old_instance;

    $instance['title']  = strip_tags($new_instance['title']);
    $instance['about']  = strip_tags($new_instance['about']);
    $instance['groups'] = $new_instance['groups'];

    return $instance;
 }
 }}}


 In the above code, I am simply assigning the coma-separated value
 (100,200,300...) to `$instance['groups']` and this **saves fine both with
 and without** the `widgets-block-editor` theme support. This also proves
 that the comma-separated values generated at the client-side are properly
 read.

 Then I incorporated the `explode()` function:

 {{{#!php
 <?php
 function update( $old_instance, $new_instance ) {
    $instance = $old_instance;

    $instance['title']  = strip_tags($new_instance['title']);
    $instance['about']  = strip_tags($new_instance['about']);

    $instance['groups_string'] = $new_instance['groups'];
    $instance['groups_object'] = explode( ',', $new_instance['groups'] );

    return $instance;
 }
 }}}

 The above snippet works perfectly fine when `widgets-block-editor` support
 is turned off. Here is the record that is saved in the database:

 {{{
 a: 2: {
    i: 2;a: 4: {
      s: 5: "title";s: 18: "New Platform Title";
      s: 5: "about";s: 0: "";
      s: 13: "groups_str";s: 20: "396060,237500,457844";
      s: 13: "groups";
             a: 3: {
               i: 0;s: 6: "396060";
               i: 1;s: 6: "237500";
               i: 2;s: 6: "457844";
             }
    }
    s: 12: "_multiwidget";i: 1;
 }
 }}}

 But, whenever I turn it on, either the explode function is not working or
 something else happening in the background prevents the groups from being
 saved. The same code above ends up with the following:


 {{{
 a: 2: {
    i: 2;a: 4: {
      s: 5: "title";s: 18: "New Platform Title";
      s: 5: "about";s: 0: "";
      s: 13: "groups_str";s: 20: "396060,237500,457844";
      s: 13: "groups";N;
    }
    s: 12: "_multiwidget";i: 1;
 }
 }}}


 There is another strange behavior that I have experienced with the
 following snippet:

 {{{#!php
 <?php
 function update( $old_instance, $new_instance ) {
    $instance = $old_instance;

    $instance['title']  = strip_tags($new_instance['title']);
    $instance['about']  = strip_tags($new_instance['about']);

    $instance['groups_str'] = $new_instance['groups'];
    $instance['groups'] = explode( ',', $new_instance['groups'] );

    return $instance;
 }
 }}}

 With the theme support enabled, the output becomes:


 {{{
 a: 2: {
    i: 2;a: 4: {
       s: 5: "title";
       s: 18: "New Platform Title";
       s: 5: "about";s: 0: "";
       s: 10: "groups_str";a: 2: {
          i: 0;s: 6: "396060";
          i: 1;s: 6: "237500";
       }
       s: 6: "groups";N;
    }
    s: 12: "_multiwidget";i: 1;
 }
 }}}


 The interesting thing in the above output is the value of groups_str. In
 the code, I have `$instance['groups_str'] = $new_instance['groups'];`
 meaning I am expecting the value to be saved as a string, i.e.
 `396060,237500`, but surprisingly it is saving as an array. And the next
 line of code, which is supposed to save the array is actually saving
 nothing `"groups";N;`

 And this also proves that the value generated by JavaScript is properly
 read even with the theme support enabled. Just to confirm, that the last
 snippet of code is working exactly the way I want when theme support is
 removed.

 The question is why the same code is not working in the same way! At least
 I cannot see any logical issue in `widget.php`.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/53625#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list