Filter uncanny-learndash-groups

ulgm_rest_api_get_group_courses

Filters the data returned by the REST API for group courses.

add_filter( 'ulgm_rest_api_get_group_courses', $callback, 10, 2 );

Description

Filters the data returned by the `ulgm_rest_api_get_group_courses` REST API endpoint. Developers can modify the course data, reload status, or function variables before the JSON response is sent. This hook fires after courses for a specific group are retrieved and prepared for API output.


Usage

add_filter( 'ulgm_rest_api_get_group_courses', 'your_function_name', 10, 2 );

Parameters

$data (mixed)
This parameter contains an array of data that will be returned as a JSON response, including information about courses related to a specific group.
$_POST (mixed)
This parameter is a mixed type array containing the data that will be sent back as a JSON response, including information about courses associated with a group.

Return Value

The filtered value.


Examples

add_filter(
	'ulgm_rest_api_get_group_courses',
	function( $data, $posted_data ) {
		// Example: If a specific course ID is present in the POST data,
		// filter the courses to only include that one.
		if ( isset( $posted_data['specific_course_id'] ) && ! empty( $posted_data['specific_course_id'] ) ) {
			$specific_course_id = absint( $posted_data['specific_course_id'] );

			// Ensure 'group_courses' exists and is an array
			if ( isset( $data['function_vars']['group_courses'] ) && is_array( $data['function_vars']['group_courses'] ) ) {
				$filtered_courses = array_filter(
					$data['function_vars']['group_courses'],
					function( $course ) use ( $specific_course_id ) {
						// Assuming each course object has an 'ID' property.
						// Adjust 'ID' if your course objects have a different property name for the ID.
						return isset( $course->ID ) && $course->ID === $specific_course_id;
					}
				);

				// Update the group_courses in the data to only contain the filtered course.
				$data['function_vars']['group_courses'] = $filtered_courses;

				// Optionally, you might want to signal that the data has been modified.
				$data['filtered_by_specific_course'] = true;
			}
		}

		// Always return the $data to ensure the filter works correctly.
		return $data;
	},
	10, // Priority: 10 is the default, so it's usually fine.
	2   // Accepted args: The function accepts $data and $_POST.
);

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-reports-interface.php:523

$data['reload']        = false;
			$data['call_function'] = 'populateCoursesDropDown';
			$data['function_vars'] = array(
				'group_courses' => $courses->posts,
				'group_id'      => $group_id,
			);

			$data = apply_filters( 'ulgm_rest_api_get_group_courses', $data, $_POST );

			wp_send_json_success( $data );

		}
	}

	/**

Scroll to Top