Filter uncanny-automator

uncanny_automator_log_get_download_url

Filters the download URL for logs, allowing customization before the file is accessed.

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

Description

Filters the download URL for log files. Developers can modify the URL before it's returned, allowing for custom download handling or parameters. This filter fires just before the log download URL is generated.


Usage

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

Parameters

$download_url (mixed)
This parameter contains the download URL for the log file.
$this (mixed)
This parameter contains the URL for downloading the log file.

Return Value

The filtered value.


Examples

add_filter( 'uncanny_automator_log_get_download_url', 'my_custom_log_download_url', 10, 2 );

/**
 * Modify the download URL for logs.
 *
 * This function appends an extra parameter to the log download URL,
 * potentially for custom handling or filtering on the client-side or server-side.
 *
 * @param string $download_url The original download URL.
 * @param array  $params       The parameters associated with the log endpoint.
 * @return string The modified download URL.
 */
function my_custom_log_download_url( $download_url, $params ) {
	// Let's say we want to add a custom identifier to the download URL
	// if the log entry is related to a specific recipe or user.
	// This is just an example and would require checking the $params array
	// for relevant keys.
	if ( isset( $params['recipe_id'] ) && ! empty( $params['recipe_id'] ) ) {
		$download_url = add_query_arg( 'recipe_identifier', absint( $params['recipe_id'] ), $download_url );
	} elseif ( isset( $params['user_id'] ) && ! empty( $params['user_id'] ) ) {
		$download_url = add_query_arg( 'user_log_ref', sanitize_key( $params['user_id'] ), $download_url );
	}

	// You could also potentially modify the 'as_attachment' value here if needed,
	// though it's less common for a filter that's already adding it.
	// For instance, to disable attachment download under certain conditions:
	// if ( ! current_user_can( 'manage_options' ) ) {
	//     $download_url = remove_query_arg( 'as_attachment', $download_url );
	// }

	return $download_url;
}

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/core/services/rest/endpoint/log-endpoint.php:392

public function get_download_url() {

		if ( ! current_user_can( automator_get_capability() ) ) {
			return '';
		}

		$curr_rest_url = strtr(
			get_rest_url( null, 'automator/v1/log/recipe_id/<recipe_id>/run_number/<run_number>/recipe_log_id/<recipe_log_id>' ),
			array(
				'<recipe_id>'     => $this->params['recipe_id'],
				'<run_number>'    => $this->params['run_number'],
				'<recipe_log_id>' => $this->params['recipe_log_id'],
			)
		);

		$download_url = add_query_arg(
			array(
				'as_attachment' => 'yes',
			),
			$curr_rest_url
		);

		return apply_filters( 'uncanny_automator_log_get_download_url', $download_url, $this->params );
	}


Scroll to Top