diff --git a/addons/Core/Core.inc b/addons/Core/Core.inc index 9391012..4fa9153 100644 --- a/addons/Core/Core.inc +++ b/addons/Core/Core.inc @@ -205,6 +205,69 @@ class ComicPressAddonCore extends ComicPressAddon { return $nodes_with_statuses; } + function move_storyline_category_order($category, $direction) { + for ($i = 0, $il = count($this->comicpress->category_tree); $i < $il; ++$i) { + $node = $this->comicpress->category_tree[$i]; + $parts = explode("/", $node); + $parts_count = count($parts); + + if (end($parts) == $category) { + $current_node_index = $i; + $target_index = null; + do { + $current_node_index += $direction; + + if (isset($this->comicpress->category_tree[$current_node_index])) { + $current_parts_count = count(explode("/", $this->comicpress->category_tree[$current_node_index])); + if ($current_parts_count == $parts_count) { $target_index = $current_node_index; break; } + if ($current_parts_count < $parts_count) { break; } + } + } while (isset($this->comicpress->category_tree[$current_node_index])); + + if (!is_null($target_index)) { + $end_i_index = $i; + do { + $end_i_index++; + if (isset($this->comicpress->category_tree[$end_i_index])) { + if (strpos($this->comicpress->category_tree[$end_i_index], $node) !== 0) { + break; + } + } + } while (isset($this->comicpress->category_tree[$end_i_index])); + + $new_order = $this->comicpress->category_tree; + $target_index_node = $new_order[$target_index]; + + $move = array_splice($new_order, $i, $end_i_index - $i); + + $target = array_search($target_index_node, $new_order); + + $prefix = array_splice($new_order, 0, $target); + + if ($direction == -1) { + $new_order = array_merge($prefix, $move, $new_order); + } else { + $leftover = array(); + if (count($new_order) > 1) { + $start_parts_count = count(explode("/", $new_order[0])); + for ($j = 1, $jl = count($new_order); $j < $jl; ++$j) { + if (count(explode("/", $new_order[$i])) < $start_parts_count) { + $leftover = array_splice($new_order, $j); break; + } + } + } + + $new_order = array_merge($prefix, $new_order, $move, $leftover); + } + + $this->comicpress->comicpress_options['category_order'] = $new_order; + + break; + } + } + } + } + function handle_update() { if (isset($_POST['attachments'])) { //coming from media editor @@ -263,9 +326,16 @@ class ComicPressAddonCore extends ComicPressAddon { } } } + + if (isset($_GET['cp']['move_direction']) && isset($_GET['cp']['category'])) { + $this->move_storyline_category_order($_GET['cp']['category'], $_GET['cp']['move_direction']); + } + $this->comicpress->save(); $this->info("ComicPress configuration updated."); + + $this->comicpress->init(); } } } diff --git a/addons/Core/partials/options-admin.inc b/addons/Core/partials/options-admin.inc index 113d723..f25e8f9 100644 --- a/addons/Core/partials/options-admin.inc +++ b/addons/Core/partials/options-admin.inc @@ -34,8 +34,7 @@ comicpress->category_tree); $i < $il; ++$i) { - $node = $this->comicpress->category_tree[$i]; + foreach ($this->get_storyline_move_statuses() as $node => $statuses) { $parts = explode("/", $node); $category = get_category(end($parts)); if (!empty($category)) { @@ -47,16 +46,13 @@ "-1" => __("Up", 'comicpress'), "1" => __("Down", 'comicpress') ) as $direction => $label) { - $target = ($i - $direction); - if (isset($this->comicpress->category_tree[$target])) { - $target_parts = explode("/", $this->comicpress->category_tree[$target]); - if (count($target_parts) == count($parts)) { - $query = add_query_arg('cp[_nonce]', $nonce); - $query = add_query_arg('cp[category]', $category->term_id, $query); - $query = add_query_arg('cp[move_direction', $direction, $query); - ?> + $status = array_shift($statuses); + if ($status) { + $query = add_query_arg('cp[_nonce]', $nonce); + $query = add_query_arg('cp[category]', $category->term_id, $query); + $query = add_query_arg('cp[move_direction]', $direction, $query); ?> | - diff --git a/addons/Core/test/OptionsPageTest.php b/addons/Core/test/OptionsPageTest.php index ecc26ef..9adc20a 100644 --- a/addons/Core/test/OptionsPageTest.php +++ b/addons/Core/test/OptionsPageTest.php @@ -153,7 +153,7 @@ class CoreTest extends PHPUnit_Framework_TestCase { * @dataProvider providerTestHandleUpdate */ function testHandleUpdate($original, $change, $new) { - $this->core->comicpress = $this->getMock('ComicPress', array('save')); + $this->core->comicpress = $this->getMock('ComicPress', array('save', 'init')); $this->core->comicpress->comicpress_options = array( 'comic_category_id' => 1, 'comic_dimensions' => '760x', @@ -204,6 +204,97 @@ class CoreTest extends PHPUnit_Framework_TestCase { '0/1/11' => array(true, false) ), $this->core->get_storyline_move_statuses()); } + + function providerTestMoveStorylineCategoryOrder() { + return array( + array( + array('0/1', '0/1/2', '0/1/3'), + 3, -1, + array('0/1', '0/1/3', '0/1/2') + ), + array( + array('0/1', '0/1/2', '0/1/2/4', '0/1/3'), + 3, -1, + array('0/1', '0/1/3', '0/1/2', '0/1/2/4') + ), + array( + array('0/1', '0/1/2', '0/1/2/4', '0/1/3', '0/1/3/5'), + 3, -1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/2', '0/1/2/4') + ), + array( + array('0/1', '0/1/2', '0/1/3', '0/1/3/5'), + 3, -1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/2') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7'), + 7, -1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/7', '0/1/3/6') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7', '0/1/4'), + 7, -1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/7', '0/1/3/6', '0/1/4') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7', '0/1/4'), + 3, 1, + array('0/1', '0/1/4', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7') + ), + array( + array('0/1', '0/1/2', '0/1/2/4', '0/1/3'), + 2, 1, + array('0/1', '0/1/3', '0/1/2', '0/1/2/4') + ), + array( + array('0/1', '0/1/2', '0/1/2/4', '0/1/3', '0/1/3/5'), + 2, 1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/2', '0/1/2/4') + ), + array( + array('0/1', '0/1/2', '0/1/3', '0/1/3/5'), + 2, 1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/2') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7'), + 6, 1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/7', '0/1/3/6') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7', '0/1/4'), + 6, 1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/7', '0/1/3/6', '0/1/4') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7', '0/1/4'), + 5, 1, + array('0/1', '0/1/3', '0/1/3/6', '0/1/3/5', '0/1/3/7', '0/1/4') + ), + array( + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7', '0/1/4', '0/1/4/9'), + 9, 1, + array('0/1', '0/1/3', '0/1/3/5', '0/1/3/6', '0/1/3/7', '0/1/4', '0/1/4/9') + ), + ); + } + + /** + * @dataProvider providerTestMoveStorylineCategoryOrder + */ + function testMoveStorylineCategoryOrder($original_order, $category, $direction, $new_order) { + $this->core->comicpress = (object)array( + 'category_tree' => $original_order, + 'comicpress_options' => array( + 'category_order' => $original_order + ) + ); + + $this->core->move_storyline_category_order($category, $direction); + + $this->assertEquals($new_order, $this->core->comicpress->comicpress_options['category_order']); + } } ?> diff --git a/functions.php b/functions.php index 798d1b3..eb9e141 100644 --- a/functions.php +++ b/functions.php @@ -31,9 +31,9 @@ function __comicpress_init() { $addons[] = new $classname(); end($addons)->init(&$comicpress); if (is_admin()) { - if (is_array($_POST['cp'])) { - if (isset($_POST['cp']['_nonce'])) { - if (wp_verify_nonce($_POST['cp']['_nonce'], 'comicpress')) { + if (is_array($_REQUEST['cp'])) { + if (isset($_REQUEST['cp']['_nonce'])) { + if (wp_verify_nonce($_REQUEST['cp']['_nonce'], 'comicpress')) { if (method_exists(end($addons), 'handle_update')) { end($addons)->handle_update(); }