! empty( $q['id'] ) && is_scalar( $q['id'] ) ? sanitize_text_field( $q['id'] ) : 'q_' . wp_generate_password( 8, false ), 'type' => '' !== $type ? $type : 'multiple_choice', 'text' => sanitize_textarea_field( $q['text'] ), 'options' => array(), 'correct' => '', 'points' => isset( $q['points'] ) ? absint( $q['points'] ) : 1, 'video_url' => '', ); if ( 'multiple_choice' === $question['type'] && ! empty( $q['options'] ) ) { $question['options'] = array_map( 'sanitize_text_field', array_filter( (array) $q['options'], 'is_scalar' ) ); $question['correct'] = isset( $q['correct'] ) ? absint( $q['correct'] ) : 0; } elseif ( 'true_false' === $question['type'] ) { $question['options'] = array( __( 'True', 'accessnow-lms' ), __( 'False', 'accessnow-lms' ) ); $question['correct'] = isset( $q['correct'] ) ? absint( $q['correct'] ) : 0; } elseif ( 'fill_blank' === $question['type'] ) { $question['correct'] = isset( $q['correct'] ) && is_scalar( $q['correct'] ) ? sanitize_text_field( $q['correct'] ) : ''; } $question['video_url'] = ! empty( $q['video_url'] ) && is_string( $q['video_url'] ) ? esc_url_raw( $q['video_url'] ) : ''; $question['explanation'] = ! empty( $q['explanation'] ) && is_string( $q['explanation'] ) ? sanitize_textarea_field( $q['explanation'] ) : ''; if ( ! in_array( $question['type'], Quiz_Engine::BUILTIN_TYPES, true ) ) { // Its own fields (options, pairs, a model answer...), kept as plain text by default. foreach ( $q as $key => $value ) { $key = sanitize_key( $key ); if ( '' !== $key && ! array_key_exists( $key, $question ) ) { $question[ $key ] = map_deep( $value, 'sanitize_text_field' ); } } if ( isset( $q['options'] ) && is_array( $q['options'] ) ) { $question['options'] = map_deep( $q['options'], 'sanitize_text_field' ); } if ( isset( $q['correct'] ) ) { $question['correct'] = map_deep( $q['correct'], 'sanitize_text_field' ); } /** * A question of an added type, after the plugin's own sanitising: sanitise its own fields * more strictly here. Return null to drop the question. * * @since 1.1.0 * * @param array|null $question Sanitised question. * @param array $raw As submitted (or imported): untrusted. */ $question = apply_filters( 'accessnow_slms_sanitize_question', $question, $q ); if ( ! is_array( $question ) ) { continue; } } $sanitized[] = $question; } return $sanitized; } public static function get( $post_id, $key, $fallback = '' ) { $value = get_post_meta( $post_id, $key, true ); return '' !== $value ? $value : $fallback; } }