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

Pass original structure position to usort. #3957

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions includes/class-sensei-course-structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,14 +774,18 @@ 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 ) {
// 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;
return $a['position'] === $b['position'] ? 0 : ( $a['position'] > $b['position'] ? 1 : -1 );
}

// Always keep the modules before the lessons.
Expand All @@ -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.
Expand All @@ -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;
}
Expand Down