ulgm_rest_api_get_essays_data
Filters essays data from the REST API before it's sent to the client.
add_filter( 'ulgm_rest_api_get_essays_data', $callback, 10, 2 );
Description
Filters the data retrieved for group essays in the REST API. Allows developers to modify the `$essays_table` content or manipulate the `$_POST` data before it's processed, enabling custom data filtering or augmentation.
Usage
add_filter( 'ulgm_rest_api_get_essays_data', 'your_function_name', 10, 2 );
Parameters
-
$essays_table(mixed) - This parameter holds the data intended to be displayed in the essays table, potentially modified by the filter.
-
$_POST(mixed) - This parameter represents the table or data structure that will hold the essay information being retrieved.
Return Value
The filtered value.
Examples
add_filter( 'ulgm_rest_api_get_essays_data', 'my_custom_essays_data_filter', 10, 2 );
/**
* Example of filtering the essays data for the ULGM REST API.
* This function demonstrates how to modify or augment the essays data
* before it's returned to the front-end. In this specific example,
* we'll add a custom field to each essay entry if a specific $_POST value is present.
*
* @param array $essays_table The original array of essays data.
* @param array $_POST The $_POST data received by the request.
* @return array The potentially modified essays data.
*/
function my_custom_essays_data_filter( $essays_table, $_POST ) {
// Check if a specific flag is set in the POST data to enable our custom modification.
if ( isset( $_POST['include_extra_essay_info'] ) && 'yes' === $_POST['include_extra_essay_info'] ) {
// Iterate through each essay entry in the table.
foreach ( $essays_table as &$essay ) {
// Add a custom field to each essay.
// In a real-world scenario, this data might come from another meta field
// or be calculated based on the existing essay data and $_POST parameters.
if ( isset( $essay['id'] ) ) {
$essay['custom_notes'] = sprintf( 'Processed for group %s on %s',
esc_html( $_POST['groupId'] ?? 'N/A' ),
current_time( 'mysql' )
);
}
}
}
// It's crucial to return the modified (or original if no changes were made) essays_table.
return $essays_table;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/reports/group-essays.php:1035
public function get_essays_data() {
$data = $_POST;
// validate inputs
$lesson_ID = absint( $data['lessonId'] );
$course_ID = absint( $data['courseId'] );
$group_ID = absint( $data['groupId'] );
$quiz_ID = absint( $data['quizId'] );
$status = $data['status'];
// Maybe prevent data load.
$get_data = apply_filters( 'ulgm_rest_api_should_get_essays_data', true, $data );
if ( ! $get_data ) {
return array();
}
$essays_table = $this->essays_table( $lesson_ID, $course_ID, $group_ID, $quiz_ID, $status );
$essays_table = apply_filters( 'ulgm_rest_api_get_essays_data', $essays_table, $_POST );
return $essays_table;
}