From b8f9979bdc451cb7ab6b9079a7a8ac53d82a685d Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Wed, 3 Feb 2021 09:27:29 +0100 Subject: [PATCH 1/5] Pass original structure position to usort. --- includes/class-sensei-course-structure.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index aa9015e17a..8c02516257 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -774,6 +774,10 @@ private function validate_item_structure( array $raw_item ) { public static function sort_structure( $structure, $order, $type ) { if ( ! empty( $order ) && [ 0 ] !== $order ) { + // remember current position in structure + foreach ($structure as $key => $value) { + $structure[$key]['position'] = intval( $key ); + } usort( $structure, function( $a, $b ) use ( $order, $type ) { @@ -781,7 +785,7 @@ function( $a, $b ) use ( $order, $type ) { if ( $type !== $a['type'] || $type !== $b['type'] ) { // If types are equal, keep in the current positions. if ( $a['type'] === $b['type'] ) { - return 0; + return $a['position'] === $b['position'] ? 0 : ( $a['position'] > $b['position'] ? 1 : -1 ); } // Always keep the modules before the lessons. @@ -793,7 +797,7 @@ function( $a, $b ) use ( $order, $type ) { // If both weren't sorted, keep the current positions. if ( false === $a_position && false === $b_position ) { - return 0; + return $a['position'] === $b['position'] ? 0 : ( $a['position'] > $b['current-pos'] ? 1 : -1 ); } // Keep not sorted items in the end. From fce9df08a081c2931eafda846828abbb6ea29160 Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Wed, 3 Feb 2021 10:01:07 +0100 Subject: [PATCH 2/5] Fixed some line-errors (spaces). --- includes/class-sensei-course-structure.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index 8c02516257..4d2c96ce63 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -774,9 +774,9 @@ private function validate_item_structure( array $raw_item ) { public static function sort_structure( $structure, $order, $type ) { if ( ! empty( $order ) && [ 0 ] !== $order ) { - // remember current position in structure - foreach ($structure as $key => $value) { - $structure[$key]['position'] = intval( $key ); + // Remember current position in structure. + foreach ( $structure as $key => $value ) { + $structure[ $key ]['position'] = intval( $key ); } usort( $structure, From ce23de2cf94278319da1a52461a47a184ad63f7f Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Wed, 3 Feb 2021 10:12:53 +0100 Subject: [PATCH 3/5] Forget previous pos after sorting. --- includes/class-sensei-course-structure.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index 4d2c96ce63..7c6f453b97 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -808,6 +808,10 @@ function( $a, $b ) use ( $order, $type ) { return false === $b_position || $a_position < $b_position ? -1 : 1; } ); + // Forget previous positions in structure. + foreach ( $structure as $key => $value ) { + unset $structure[ $key ]['position']; + } } return $structure; } From dc4ae776e6f78149fae02cfcaf7b64deee12391f Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Wed, 3 Feb 2021 10:18:34 +0100 Subject: [PATCH 4/5] Fixed syntax error in the unset-call. --- includes/class-sensei-course-structure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index 7c6f453b97..c61c87571d 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -810,7 +810,7 @@ function( $a, $b ) use ( $order, $type ) { ); // Forget previous positions in structure. foreach ( $structure as $key => $value ) { - unset $structure[ $key ]['position']; + unset( $structure[ $key ]['position'] ); } } return $structure; From b585e9812716e884fc7dec914ee22199a80782aa Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Wed, 3 Feb 2021 10:45:43 +0100 Subject: [PATCH 5/5] Incorrect index found by phpunit test. --- includes/class-sensei-course-structure.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index c61c87571d..dad13c9023 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -797,7 +797,7 @@ function( $a, $b ) use ( $order, $type ) { // If both weren't sorted, keep the current positions. if ( false === $a_position && false === $b_position ) { - return $a['position'] === $b['position'] ? 0 : ( $a['position'] > $b['current-pos'] ? 1 : -1 ); + return $a['position'] === $b['position'] ? 0 : ( $a['position'] > $b['position'] ? 1 : -1 ); } // Keep not sorted items in the end.