From 1353caec41c5836076128664108f7949fb34e777 Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Mon, 1 Feb 2021 20:20:42 +0100 Subject: [PATCH 1/2] Skip sort_structure method for single lesson save. --- includes/class-sensei-course-structure.php | 52 +++++++++++----------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index 1c8b5e2fee..71ac3fb370 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -772,37 +772,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; } } From 919be5530d2fe218eeed3bfb5229afc13bb6d3dc Mon Sep 17 00:00:00 2001 From: Hans Schuijff Date: Mon, 1 Feb 2021 20:45:13 +0100 Subject: [PATCH 2/2] Fixed lint-errors (just some spaces). --- includes/class-sensei-course-structure.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-sensei-course-structure.php b/includes/class-sensei-course-structure.php index 71ac3fb370..aa9015e17a 100644 --- a/includes/class-sensei-course-structure.php +++ b/includes/class-sensei-course-structure.php @@ -772,8 +772,8 @@ private function validate_item_structure( array $raw_item ) { * @return array Sorted structure. */ public static function sort_structure( $structure, $order, $type ) { - if ( ! empty( $order ) - && [0] !== $order ) { + if ( ! empty( $order ) + && [ 0 ] !== $order ) { usort( $structure, function( $a, $b ) use ( $order, $type ) {