wp-shortcake / shortcake

Shortcake makes using WordPress shortcodes a piece of cake.
GNU General Public License v2.0
664 stars 143 forks source link

Cannot use sequential integers as keys (values) in a select field #695

Open MattGeri opened 7 years ago

MattGeri commented 7 years ago

As of version 0.7.0, when you define a select form element, you cannot use integers as it's keys. If you do, it will use the name of the select option as the key instead of the integer value (key).

Here is an example of what I am talking about:

In the screenshot below, we're defining a select form element

screen shot 2017-02-01 at 16 35 35

This is how the select form element renders in the shortcake ui form

screen shot 2017-02-01 at 16 38 52

If you use a non-integer based key for the select form element, it works as expected and the key defined in the array shows up as the option value. Like this

screen shot 2017-02-01 at 16 40 43

One further thing to note, if you use integer values that are not in numerical order, it seems to work fine. Like this

screen shot 2017-02-01 at 16 42 14

Downgrading back to 0.6.0 fixed the issue for me. I tried to have a look at the code but ran out of time. If I get the chance, I'll have a deeper look and see if I can figure it out, but perhaps someone with more experience of the plugin architecture could figure it out quicker.

mattheu commented 7 years ago

Thanks for reporting this.

We made some changes to this code, the main reason being that it didn't maintain the order of the options.

The solution was to change the way this was defined to something like this:

'options' => array(
    array( 'value' => 'left-2', 'label' => esc_html__( 'Pull Left', 'shortcode-ui-example' ) ),
    array( 'value' => 'right-2', 'label' => esc_html__( 'Pull Right', 'shortcode-ui-example' ) ),
)

Short term fix would be to update your code to comply with this.

However, I did add some code to handle compatability, so the old behaviour should not have been broken. I'll take a look into this.

MattGeri commented 7 years ago

Thanks for the response!

I changed our code to the definition that you described above, and it works for the first example I listed above where the index is integers in numerical order.

However on the other elements (non-numerical), I implemented the same definition, and while the select boxes display correctly, the options never save (or they may save, but the selected value does not show when you reopen the shortcake editor).

Again, hoping to get a chance at looking in to this if I find some time this week. In the meantime, the hybrid solution of what you mentioned above, plus the old definition for non integers is working well with 0.7.0 :)

Thanks again for your help