-
Notifications
You must be signed in to change notification settings - Fork 197
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
Handle empty module on the course structure saving #7649
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Significance: patch | ||
Type: fixed | ||
|
||
Set default names for modules without titles |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -230,7 +230,8 @@ private function get_modules(): array { | |
* @return bool|WP_Error | ||
*/ | ||
public function save( array $raw_structure ) { | ||
$structure = $this->sanitize_structure( $raw_structure ); | ||
$raw_structure = $this->ensure_modules_have_names( $raw_structure ); | ||
$structure = $this->sanitize_structure( $raw_structure ); | ||
if ( is_wp_error( $structure ) ) { | ||
return $structure; | ||
} | ||
|
@@ -713,6 +714,54 @@ private function flatten_structure( array $structure ): array { | |
]; | ||
} | ||
|
||
/** | ||
* Check if modules have titles and set the default ones otherwise. | ||
* | ||
* @param array $raw_structure Structure array as returned by get(). | ||
* | ||
* @return array Structure array with module titles set. | ||
*/ | ||
private function ensure_modules_have_names( array $raw_structure ) { | ||
$existing_module_names = array_map( | ||
function ( $item ) { | ||
if ( ! isset( $item['type'], $item['title'] ) ) { | ||
return null; | ||
} | ||
return 'module' === $item['type'] ? $item['title'] : null; | ||
}, | ||
$raw_structure | ||
); | ||
$existing_module_names = array_filter( $existing_module_names ); | ||
|
||
$i = 0; | ||
$raw_structure = array_map( | ||
function ( $item ) use ( &$existing_module_names, &$i ) { | ||
// Ignore items with improper structure. | ||
if ( ! isset( $item['type'], $item['title'] ) ) { | ||
return $item; | ||
} | ||
|
||
// Ignore items that are not modules or have a title already. | ||
if ( 'module' !== $item['type'] || ! empty( $item['title'] ) ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nitpick: Just an edge case as we are trying to work against user-made scenarios - can we trim the title and then check if it's empty? Because otherwise, it doesn't assign a name to the module if the title is just one or more spaces. Does it make sense? |
||
return $item; | ||
} | ||
|
||
do { | ||
// translators: Placeholder value is an ordinal number. | ||
$module_title = sprintf( __( 'Module %d', 'sensei-lms' ), ++$i ); | ||
} while ( in_array( $module_title, $existing_module_names, true ) ); | ||
|
||
$item['title'] = $module_title; | ||
$existing_module_names[] = $module_title; | ||
|
||
return $item; | ||
}, | ||
$raw_structure | ||
); | ||
|
||
return $raw_structure; | ||
} | ||
|
||
/** | ||
* Parse, validate, and sanitize the structure input. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Has this check become redundant now?
sensei/includes/class-sensei-course-structure.php
Lines 897 to 902 in 53dbffa