diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index 65886fc3d6..61aebf1a70 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -773,37 +773,39 @@ private function validate_item_structure( array $raw_item ) { * @return array Sorted structure. */ public static function sort_structure( $structure, $order, $type ) { - usort( - $structure, - function( $a, $b ) use ( $order, $type ) { - // One of the types is not being sorted. - if ( $type !== $a['type'] || $type !== $b['type'] ) { - // If types are equal, keep in the current positions. - if ( $a['type'] === $b['type'] ) { - return 0; + if ( ! empty( $order ) + && [ 0 ] !== $order ) { + usort( + $structure, + function( $a, $b ) use ( $order, $type ) { + // One of the types is not being sorted. + if ( $type !== $a['type'] || $type !== $b['type'] ) { + // If types are equal, keep in the current positions. + if ( $a['type'] === $b['type'] ) { + return 0; + } + + // Always keep the modules before the lessons. + return 'module' === $a['type'] ? - 1 : 1; } - // Always keep the modules before the lessons. - return 'module' === $a['type'] ? - 1 : 1; - } + $a_position = array_search( $a['id'], $order, true ); + $b_position = array_search( $b['id'], $order, true ); - $a_position = array_search( $a['id'], $order, true ); - $b_position = array_search( $b['id'], $order, true ); + // If both weren't sorted, keep the current positions. + if ( false === $a_position && false === $b_position ) { + return 0; + } - // If both weren't sorted, keep the current positions. - if ( false === $a_position && false === $b_position ) { - return 0; - } + // Keep not sorted items in the end. + if ( false === $a_position ) { + return 1; + } - // Keep not sorted items in the end. - if ( false === $a_position ) { - return 1; + return false === $b_position || $a_position < $b_position ? -1 : 1; } - - return false === $b_position || $a_position < $b_position ? -1 : 1; - } - ); - + ); + } return $structure; } }