pronamic / wp-pay-core

Core components for the WordPress payment processing library. This library is used in the WordPress plugin Pronamic Pay: https://www.pronamicpay.com/, but also allows other plugin developers to set up a payment plugin.
https://www.wp-pay.org/
GNU General Public License v3.0
27 stars 3 forks source link

On Gateway configuration page - Issue if key in the option array is of integer type #164

Closed knit-pay closed 9 months ago

knit-pay commented 9 months ago

The code is working fine if we pass the 'key' as a string in the options array. But if the 'key' is of integer type or some other data type, the "selected" attribute is not getting updated. This happens because the value is saved in the database as a "string" even for the 'int' type key in options. Do you suggest using string keys instead of int keys?

example: Working fine

$fields[] = [
            'section'  => 'general',
            'meta_key' => '_pronamic_gateway_ccavenue_country',
            'title'    => 'Country'
            'type'     => 'select',
            'options'  => [
                'in' => 'India',
                'ae' => 'UAE',
            ],
            'default'  => 'in',
        ];

Not Working fine

$fields[] = [
            'section'  => 'general',
            'meta_key' => '_pronamic_gateway_orderbox_config_id',
            'title'    => 'Configuration',
            'type'     => 'select',
            'options'  => Plugin::get_config_select_options(),
            'default'  => get_option( 'pronamic_pay_config_id' ),
        ];

https://github.com/pronamic/wp-pay-core/blob/a34a2f0b7178649e901aaa64fd28f59d65463289/views/meta-box-gateway-settings.php#L361

I would suggest to use == instead of === operator. Or we can also convert the $key to a string before comparing it with the $value. What do you suggest?

rvdsteege commented 9 months ago

Thanks Gautam, I can confirm the issue and resolved it in https://github.com/pronamic/wp-pay-core/commit/8d349ce1e61e7960aa6d384248b7a165bdb41f11 with casting to string type in the comparison.