我正在尝试编写一个bash脚本来自动将活动站点同步到开发人员站点。我已经解决了一些基本问题,比如导出生产数据库和导入/更新开发站点中的URL,但我需要帮助更新一个将其数据保存为数组的选项。
具体来说,我想使用wp-cli
.
我可以使用wp cli查看选项:
$ wp option get woocommerce_stripe_settings
array (
\'enabled\' => \'yes\',
\'title\' => \'Credit Card (Stripe)\',
\'description\' => \'Pay with your credit card via Stripe.\',
\'testmode\' => \'no\',
\'test_secret_key\' => \'sk_test_xxxxxxxxxxxxxxxxxx\',
\'test_publishable_key\' => \'pk_test_xxxxxxxxxxxxxxxxxx\',
\'secret_key\' => \'sk_live_xxxxxxxxxxxxxxxxxx\',
\'publishable_key\' => \'pk_live_xxxxxxxxxxxxxxxxxx\',
\'capture\' => \'yes\',
\'stripe_checkout\' => \'no\',
\'allow_remember_me\' => \'yes\',
\'stripe_checkout_locale\' => \'en\',
\'stripe_bitcoin\' => \'no\',
\'stripe_checkout_image\' => \'\',
\'saved_cards\' => \'yes\',
\'logging\' => \'no\',
\'apple_pay_domain_set\' => \'yes\',
\'statement_descriptor\' => \'Statement From\',
\'request_payment_api\' => \'no\',
\'apple_pay\' => \'yes\',
\'apple_pay_button\' => \'black\',
\'apple_pay_button_lang\' => \'en\',
)
我尝试使用从直接查询DB得到的序列化值,更新
s:2:"no"
到
s:3:"yes"
但这似乎并没有正确地保存价值:
wp option update woocommerce_stripe_settings \'a:22:{s:7:"enabled";s:3:"yes";s:5:"title";s:20:"Credit Card (Stripe)";s:11:"description";s:37:"Pay with your credit card via Stripe.";s:8:"testmode";s:3:"yes";s:15:"test_secret_key";s:32:"sk_test_xxxxxxxxxxxxxxxxxxxxxxxx";s:20:"test_publishable_key";s:32:"pk_test_xxxxxxxxxxxxxxxxxxxxxxxx";s:10:"secret_key";s:32:"sk_live_xxxxxxxxxxxxxxxxxxxxxxxx";s:15:"publishable_key";s:32:"pk_live_xxxxxxxxxxxxxxxxxxxxxxxx";s:7:"capture";s:3:"yes";s:15:"stripe_checkout";s:2:"no";s:17:"allow_remember_me";s:3:"yes";s:22:"stripe_checkout_locale";s:2:"en";s:14:"stripe_bitcoin";s:2:"no";s:21:"stripe_checkout_image";s:0:"";s:11:"saved_cards";s:3:"yes";s:7:"logging";s:2:"no";s:20:"apple_pay_domain_set";s:3:"yes";s:20:"statement_descriptor";s:50:" ";s:19:"request_payment_api";s:2:"no";s:9:"apple_pay";s:3:"yes";s:16:"apple_pay_button";s:5:"black";s:21:"apple_pay_button_lang";s:2:"en";}\'
此外,我想更改的唯一信息是
\'testmode\' => \'no\'
到
\'testmode\' => \'yes\'
-- 因此,像对整个序列化数组进行硬编码这样的解决方案在我的情况下是行不通的,无论如何,我都觉得很脆弱。
有没有办法使用wpcli只更新数组的一个值?在我看来是这样的:
$ wp option update woocommerce_stripe_settings[\'testmode\'] yes
最合适的回答,由SO网友:passionsplay 整理而成
多亏了米洛在上面的评论中的提示,我看到了这个similar question.
Laurent提供了一个答案,它基本上是使用wp cli获取选项,将其传输到一个内联php程序,进行调整,然后将其传输回wp cli。我接受了这个想法,并通过创建与我的克隆脚本同级的文件对其进行了某种程度的概括:update-array-option.sh
.
#!/bin/bash
option_name=$1
option_key=$2
option_value=$3
wp option get ${option_name} --format=json | php -r "
\\$option = json_decode( fgets(STDIN) );
\\$option->${option_key} = \\"${option_value}\\";
print json_encode(\\$option);
" | wp option set ${option_name} --format=json
然后,用法变为:
./update-array-option.sh <option-name> <option-key> <value>
具体针对这个问题:
./update-array-option.sh woocommerce_stripe_settings testmode yes
显然,这是快速和肮脏的,不会处理所有的边缘情况。像这样混合bash/php,以及bash中所有与字符串相关的东西,看起来和感觉都很恶心,YMMV。