Filter tin-canny-learndash-reporting

uo_tincanny_by_request_url

Filters the URL used for a Tincanny request before it is sent.

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

Description

Filters the request URL before it's processed for Tincan statement storage. Developers can modify the URL to change how Tincan data is associated with specific requests or endpoints. The original URL and content are provided for context.


Usage

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

Parameters

$url (mixed)
The `$url` parameter is of mixed type and is intended to hold the request URL, though its specific usage within this filter is not fully illustrated by the provided code snippet.
$content (mixed)
This parameter contains the URL that triggered the request.

Return Value

The filtered value.


Examples

/**
 * Example callback function for the uo_tincanny_by_request_url filter.
 *
 * This function demonstrates how to modify the URL used for Tincan API requests
 * based on specific conditions. For instance, it might append a query parameter
 * to track analytics or redirect to a different URL for specific content types.
 *
 * @param string $url     The original URL generated by the plugin.
 * @param array  $content The content data associated with the request.
 *
 * @return string The modified or original URL.
 */
function my_tincanny_modify_request_url( $url, $content ) {

	// Check if the content contains a specific key that indicates a certain type of activity.
	if ( isset( $content['course_id'] ) && ! empty( $content['course_id'] ) ) {
		$course_id = absint( $content['course_id'] );

		// If it's a specific course, prepend a staging subdomain.
		if ( $course_id === 123 ) {
			$url = str_replace( 'api.example.com', 'staging.api.example.com', $url );
		}
	}

	// Append a UTM parameter if it's not already present.
	if ( strpos( $url, 'utm_source=' ) === false ) {
		$url = add_query_arg( 'utm_source', 'uncanny-tincan-plugin', $url );
	}

	// You could also add logic here to return a completely different URL
	// based on the $content, for example, if you wanted to proxy requests
	// through a different endpoint for security or load balancing.
	//
	// if ( isset( $content['user_id'] ) ) {
	//     $url = 'https://internal.api.example.com/track/' . esc_url_raw( $content['user_id'] );
	// }

	return $url;
}

// Add the filter to WordPress.
// The first parameter is the hook name.
// The second parameter is the callback function.
// The third parameter is the priority (default is 10).
// The fourth parameter is the number of arguments the callback function accepts.
add_filter( 'uo_tincanny_by_request_url', 'my_tincanny_modify_request_url', 10, 2 );

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:231

private function by_request( $content ) {

		$state_id = ultc_get_filter_var( 'stateId', '' );
		if ( isset( $content['stateId'] ) ) {
			$state_id = $content['stateId'];
		}

		Database::log( sprintf( 'by_request: state_id=%s, content=%s', $state_id, print_r( $content, true ) ), 'Server' );

		if ( $state_id ) {

			$method = ultc_filter_has_var( 'method' ) ? ultc_filter_input( 'method' ) : $_SERVER['REQUEST_METHOD'];
			$method = strtoupper( $method );
			Database::log( sprintf( 'by_request: method=%s, REQUEST_METHOD=%s', $method, $_SERVER['REQUEST_METHOD'] ), 'Server' );
			
			// Resolve best activity URL containing a valid module id
			$url = $this->resolve_module_activity_url( $content );
			Database::log( sprintf( 'by_request: resolved url=%s', $url ), 'Server' );

			$url = apply_filters( 'uo_tincanny_by_request_url', $url, $content );
			Database::log( sprintf( 'by_request: url after filter=%s', $url ), 'Server' );

			if ( empty( $url ) ) {
				Database::log( 'by_request FAILED: URL is empty, returning early', 'Server' );
				return;
			}

			if ( 'PUT' === $method ) {
				if ( isset( $content['content'] ) ) {
					$content = $content['content'];

				} elseif ( is_array( $content ) && count( $content ) === 1 && empty( array_values( $content )[0] ) ) {
					$content = key( $content );
					$content = str_replace( array( '_html', '_htm' ), array( '.html', '.htm' ), $content );

				} else {
					$content = wp_json_encode( $content );
				}
				Database::log( sprintf( 'by_request: PUT content length=%d', strlen( $content ) ), 'Server' );
			}

			if ( $method ) {
				Database::log( sprintf( 'by_request: Calling get_state with method=%s, url=%s, state_id=%s', $method, $url, $state_id ), 'Server' );
				$this->get_state( $method, $url, $state_id, $content );
			} else {
				Database::log( 'by_request FAILED: method is empty', 'Server' );
			}
		} else {
			Database::log( 'by_request: No state_id detected, skipping state handling', 'Server' );
		}
	}

Scroll to Top