Skip to content

Commit

Permalink
Merge pull request #3951 from hansschuijff/fix/#3921-keep-lesson-orde…
Browse files Browse the repository at this point in the history
…r-on-single-lesson-save

Skip sort_structure method for single lesson save.
  • Loading branch information
yscik committed Feb 2, 2021
2 parents 42758db + 919be55 commit 6d0fc9e
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions includes/class-sensei-course-structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 6d0fc9e

Please sign in to comment.