Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lesson order being changed when saving lesson #3951

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 27 additions & 25 deletions includes/class-sensei-course-structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}