diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index aa9015e17a..dad13c9023 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['position'] ? 1 : -1 ); } // Keep not sorted items in the end. @@ -804,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; }