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 bulk edit #7515

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/fix-lesson-bulk-edit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fix lesson bulk edit.
70 changes: 37 additions & 33 deletions includes/class-sensei-lesson.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@
add_action( 'manage_lesson_posts_custom_column', array( $this, 'set_quick_edit_admin_defaults' ), 11, 2 );

// save bulk edit fields
add_action( 'wp_ajax_save_bulk_edit_book', array( $this, 'save_all_lessons_edit_fields' ) );
add_action( 'save_post', array( $this, 'save_all_lessons_edit_fields' ) );

add_action( 'admin_head', array( $this, 'add_custom_link_to_course' ) );

Expand Down Expand Up @@ -706,7 +706,6 @@
$this->save_quiz_settings( $post_id, $new_settings );

return $post_id;

}

/**
Expand Down Expand Up @@ -4323,38 +4322,47 @@
}

/**
* Respond to the ajax call from the bulk edit save function. This comes
* from the admin all lesson screen.
* Respond to the ajax call from the bulk edit save function.
* This comes from the admin all lesson screen.
*
* @since 1.8.0
*
* @internal
*/
function save_all_lessons_edit_fields() {

// verify all the data before attempting to save
if ( ! isset( $_POST['security'] ) || ! check_ajax_referer( 'bulk-edit-lessons', 'security' ) || empty( $_POST['post_ids'] ) || ! is_array( $_POST['post_ids'] ) ) {
die();
}

// get our variables
$new_course = isset( $_POST['sensei_edit_lesson_course'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_lesson_course'] ) ) : '';
$new_complexity = isset( $_POST['sensei_edit_complexity'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_complexity'] ) ) : '';
$new_pass_required = isset( $_POST['sensei_edit_pass_required'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_pass_required'] ) ) : '';
$new_pass_percentage = isset( $_POST['sensei_edit_pass_percentage'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_pass_percentage'] ) ) : '';
$new_enable_quiz_reset = isset( $_POST['sensei_edit_enable_quiz_reset'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_enable_quiz_reset'] ) ) : '';
$show_questions = isset( $_POST['sensei_edit_show_questions'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_show_questions'] ) ) : '';
$random_question_order = isset( $_POST['sensei_edit_random_question_order'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_random_question_order'] ) ) : '';
$quiz_grade_type = isset( $_POST['sensei_edit_quiz_grade_type'] ) ? sanitize_text_field( wp_unslash( $_POST['sensei_edit_quiz_grade_type'] ) ) : '';
// store the values for all selected posts.
foreach ( $_POST['post_ids'] as $lesson_id ) {

// do not save the items if the value is -1 as this
// means it was not changed
// update lesson course
if ( - 1 !== $new_course ) {
public function save_all_lessons_edit_fields() {

Check warning on line 4332 in includes/class-sensei-lesson.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-lesson.php#L4332

Added line #L4332 was not covered by tests
// Verify all the data before attempting to save.
if ( ! isset( $_REQUEST['_edit_lessons_nonce'] )
|| ! check_ajax_referer( 'bulk-edit-lessons', '_edit_lessons_nonce' )
|| empty( $_REQUEST['post'] )
|| ! is_array( $_REQUEST['post'] ) ) {
return;

Check warning on line 4338 in includes/class-sensei-lesson.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-lesson.php#L4334-L4338

Added lines #L4334 - L4338 were not covered by tests
}

// Get our variables.
$new_course = isset( $_REQUEST['lesson_course'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['lesson_course'] ) ) : '';
$new_complexity = isset( $_REQUEST['lesson_complexity'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['lesson_complexity'] ) ) : '';
$new_pass_required = isset( $_REQUEST['pass_required'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['pass_required'] ) ) : '';
$new_pass_percentage = isset( $_REQUEST['quiz_passmark'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['quiz_passmark'] ) ) : '';
$new_enable_quiz_reset = isset( $_REQUEST['enable_quiz_reset'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['enable_quiz_reset'] ) ) : '';
$show_questions = isset( $_REQUEST['show_questions'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['show_questions'] ) ) : '';
$random_question_order = isset( $_REQUEST['random_question_order'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['random_question_order'] ) ) : '';
$quiz_grade_type = isset( $_REQUEST['quiz_grade_type'] ) ? sanitize_text_field( (string) wp_unslash( $_REQUEST['quiz_grade_type'] ) ) : '';

Check warning on line 4349 in includes/class-sensei-lesson.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-lesson.php#L4342-L4349

Added lines #L4342 - L4349 were not covered by tests

// Store the values for all selected posts.
$lesson_ids = $_REQUEST['post'] ?? array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- Input is sanitized in the next lines.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably assigning to the empty array here is not necessary, because we already checked if it is an array on line 4337

$lesson_ids = array_map( 'wp_unslash', $lesson_ids );
$lesson_ids = array_map( 'sanitize_text_field', $lesson_ids );
$lesson_ids = array_map( 'intval', $lesson_ids );
foreach ( $lesson_ids as $lesson_id ) {

Check warning on line 4356 in includes/class-sensei-lesson.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-lesson.php#L4352-L4356

Added lines #L4352 - L4356 were not covered by tests
// Do not save the items if the value is -1 as this means it was not changed.

// Update lesson course.
if ( '-1' !== $new_course ) {

Check warning on line 4360 in includes/class-sensei-lesson.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-lesson.php#L4360

Added line #L4360 was not covered by tests
update_post_meta( $lesson_id, '_lesson_course', $new_course );
}
// update lesson complexity
if ( -1 !== $new_complexity ) {

// Update lesson complexity.
if ( '-1' !== $new_complexity ) {

Check warning on line 4365 in includes/class-sensei-lesson.php

View check run for this annotation

Codecov / codecov/patch

includes/class-sensei-lesson.php#L4365

Added line #L4365 was not covered by tests
update_post_meta( $lesson_id, '_lesson_complexity', $new_complexity );
}

Expand All @@ -4368,11 +4376,7 @@
);

$this->save_quiz_settings( $lesson_id, $new_settings );

}

die();

}

/**
Expand Down