Last active
August 29, 2015 14:17
-
-
Save eriktorsner/caece89bf8b401062f9d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: More WpCli commands | |
* Plugin URI: http://notaurl.com | |
* Description: Some useful commands for wp-cli | |
* Author: Erik Torsner | |
* Author URI: http://erik.torgesta.com | |
* Version: 1.0. | |
*/ | |
/** | |
* README: | |
* Drop this file into your wp-config/plugins folder. Then activate | |
* the plugin from your WP admin pages or via the command line: | |
* wp plugin activate morewpclicommands. | |
* | |
* Then you can update settings like this: | |
* | |
* Original setting for foobar: | |
* Array( | |
* 'stringValue' => 'value', | |
* 'untouchedValue' => 'value2', | |
* 'arrVal' => Array( | |
* 'Key1' => 'value'. | |
* 'Key2' => 'OtherValue' | |
* ) | |
* ) | |
* | |
* Run this command | |
* wp more option update foobar '{"stringValue":"newValue", "arrVal":{"_":"a", "newKey":"newArrValue"}}' | |
* | |
* New setting for foobar: | |
* Array( | |
* 'stringValue' => 'newValue', | |
* 'untouchedValue' => 'value2', | |
* 'arrVal' => Array( | |
* 'newKey' => 'newArrValue' | |
* ) | |
* ) | |
*/ | |
// Exit if accessed directly | |
if (! defined('ABSPATH')) { | |
exit; | |
} | |
if (defined('WP_CLI')) { | |
WP_CLI::add_command('more', 'morewpclicommands'); | |
} | |
class morewpclicommands | |
{ | |
public function __construct() | |
{ | |
} | |
public function option($args, $assoc_args) | |
{ | |
$command = $args[0]; | |
$optionName = $args[1]; | |
$newValue = json_decode($args[2]); | |
switch ($command) { | |
case 'update': | |
$oldValue = get_option($optionName); | |
if (is_array($oldValue)) { | |
foreach ($newValue as $key => $value) { | |
$this->assignArray($oldValue, $key, $value); | |
} | |
} elseif (is_object($oldValue)) { | |
foreach ($newValue as $key => $value) { | |
$this->assignObject($oldValue, $key, $value); | |
} | |
} else { | |
$oldValue = $newValue; | |
} | |
update_option($optionName, $oldValue); | |
WP_CLI::success(sprintf('Updated %s', $optionName)); | |
break; | |
} | |
} | |
private function assignArray(&$oldValue, $key, $value) | |
{ | |
if (is_object($value)) { | |
if (isset($value->_)) { | |
switch ($value->_) { | |
case 'a': | |
unset($value->_); | |
$oldValue[$key] = get_object_vars($value); | |
return; | |
break; | |
} | |
} | |
} | |
$oldValue[$key] = $value; | |
} | |
private function assignObject(&$oldValue, $key, $value) | |
{ | |
if (is_object($value)) { | |
if (isset($value->_)) { | |
switch ($value->_) { | |
case 'a': | |
unset($value->_); | |
$oldValue[$key] = get_object_vars($value); | |
return; | |
break; | |
} | |
} | |
} | |
$oldValue->$key = $value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment