Action tin-canny-learndash-reporting

tincanny_before_process_request

Fires before Tincanny processes an incoming request, allowing for early modification or interruption.

add_action( 'tincanny_before_process_request', $callback, 10, 1 );

Description

Fires before Uncanny Tin Can processes an incoming LRS request. Developers can use this hook to modify request data, perform custom validation, or execute actions before the request is handled by the core Uncanny Tin Can LRS. It's important to note that this hook fires before the LRS data is decoded.


Usage

add_action( 'tincanny_before_process_request', 'your_function_name', 10, 1 );

Examples

/**
 * Example function to hook into tincanny_before_process_request.
 *
 * This function demonstrates how to use the tincanny_before_process_request hook
 * to potentially modify the request content or prevent processing entirely
 * based on certain conditions.
 *
 * @param Server $server The instance of the Server class that is processing the request.
 */
function my_custom_tincanny_request_handler( $server ) {
	// Example: Log the incoming request details for debugging.
	// This is just for demonstration and would likely be removed in production.
	error_log( 'Tincanny: Before processing request. User agent: ' . $_SERVER['HTTP_USER_AGENT'] );

	// Example: Access and potentially modify data from the $server object.
	// For instance, let's say we want to check for a specific user role
	// and only allow processing if the user is an administrator.
	if ( class_exists( 'WP_User' ) ) {
		$current_user = wp_get_current_user();
		if ( ! in_array( 'administrator', $current_user->roles, true ) ) {
			// If the user is not an administrator, we might want to prevent
			// further processing of this request.
			error_log( 'Tincanny: Non-administrator user detected. Aborting request processing.' );

			// To completely stop the request processing, we can exit.
			// This is a drastic measure and should be used with caution.
			// Alternatively, you could modify data within the $server object
			// that the subsequent logic will interpret as an invalid request.
			header( 'Status: 403 Forbidden' );
			header( 'HTTP/1.1 403 Forbidden' );
			exit();
		}
	}

	// Example: Accessing decoded content after it has been retrieved but before processing.
	// The 'content' property might not be directly available here as it's fetched later in process_request.
	// However, if the hook were placed differently, you could potentially access $server->decoded_content.
	// For this specific hook placement, we can only access properties of the $server object itself.

	// Example: If this were a filter hook, you would need to return a value.
	// For an action hook, no return value is expected.
}

// Add the action hook.
// The 'tincanny_before_process_request' hook doesn't pass any arguments
// directly to the hooked function in the provided source. So, we'll accept
// just the $server object which is the context of the call.
add_action( 'tincanny_before_process_request', 'my_custom_tincanny_request_handler', 10 );

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/uncanny-tincan/classes/Server.php:56

private function process_request() {
		//error_reporting( 0 );

		do_action( 'tincanny_before_process_request' );

		$this->modify_header();
		$content = $this->get_decoded();

		// Get Resume
		$this->by_request( $content );

		// Get Client and Contents
		$client = $this->get_client();

		$is_app = strstr( $_SERVER['HTTP_USER_AGENT'], 'AdobeAIR/' );

		// Check Nonce
		if ( 'H5P' !== $client && ! $this->get_nonce() && ! $is_app ) {
			header( 'Status: 403 Forbidden' );
			header( 'HTTP/1.1 403 Forbidden' );
			exit();
		}

		if ( isset( $content['content'] ) && ! is_array( $content['content'] ) ) {
			$content = json_decode( $content['content'], true );
		}

		// Should we process this request?
		$tincanny_process_content = apply_filters( 'tincanny_process_content', true, [
			'content' => $content,
			'client'  => $client
		] );

		if ( true !== $tincanny_process_content ) {
			$this->print_guid( false );
			return;
		}

		// Get the First Key
		$keys      = array_keys( $content );
		$first_key = array_shift( $keys );

		// Multiple Request
		if ( is_numeric( $first_key ) || 0 === $first_key ) {
			for ( $i = ( count( $content ) - 1 ); $i >= 0; $i -- ) {
				$completion = $this->create_tincan_record( $client, $content[ $i ] );
			}

			// Single Request
		} else {
			$completion = $this->create_tincan_record( $client, $content );
		}

		$this->print_guid( $completion );
	}


Scroll to Top