Filter uncanny-automator

automator_licensing_timeout

Filters the licensing timeout duration for integrations. This filter fires when the licensing module checks for valid licenses.

add_filter( 'automator_licensing_timeout', $callback, 10, 1 );

Description

Filters the remote post request timeout for licensing API calls. Developers can adjust this value to control how long WordPress waits for a response from the licensing server, useful for troubleshooting slow connections or server issues. The default is 20 seconds.


Usage

add_filter( 'automator_licensing_timeout', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Extend the licensing API timeout for specific scenarios.
 *
 * This filter allows you to dynamically increase the timeout for the licensing API
 * calls made by the Automator plugin. This can be useful if you're experiencing
 * intermittent timeouts with your licensing server, perhaps due to network latency
 * or a particularly slow API response.
 *
 * @param int $timeout The current timeout value in seconds.
 * @return int The modified timeout value in seconds.
 */
add_filter( 'automator_licensing_timeout', function( $timeout ) {
	// Check if a specific condition is met to justify increasing the timeout.
	// For example, you might want to increase it during periods of high server load
	// or if you're integrating with a less performant external licensing service.
	// In this example, we're just doubling the timeout for demonstration purposes.
	$increased_timeout = $timeout * 2;

	// Ensure the timeout doesn't exceed a reasonable upper limit to prevent
	// excessively long waits for users.
	$max_timeout = 60; // Maximum allowed timeout of 60 seconds.
	$final_timeout = min( $increased_timeout, $max_timeout );

	return $final_timeout;
}, 10, 1 );

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/admin/class-admin-menu.php:1906

public static function licensing_call( $endpoint = 'check-license', $license_key = '', $item_id = AUTOMATOR_FREE_ITEM_ID, $store_url = AUTOMATOR_LICENSING_URL, $should_redirect = true ) {
		$valid_endpoints = array(
			'check-license',
			'activate-license',
			'deactivate-license',
			'get_version',
		);

		if ( ! in_array( $endpoint, $valid_endpoints, true ) ) {
			wp_die( 'Invalid endpoint selected.' );
		}

		if ( empty( $license_key ) ) {
			wp_die( 'License Key not provided.' );
		}

		$data = array(
			'license' => $license_key,
			'item_id' => $item_id,
			'url'     => home_url(),
		);

		// Convert array to JSON and then encode it with Base64
		$encoded_data = base64_encode( wp_json_encode( $data ) ); // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode

		$item_name    = AUTOMATOR_FREE_ITEM_NAME;
		$item_version = AUTOMATOR_PLUGIN_VERSION;
		if ( defined( 'AUTOMATOR_PRO_ITEM_ID' ) && (int) AUTOMATOR_PRO_ITEM_ID === (int) $item_id ) {
			$item_name    = AUTOMATOR_PRO_ITEM_NAME;
			$item_version = AUTOMATOR_PRO_PLUGIN_VERSION;
		}

		// Call the custom API.
		$url = $store_url . $endpoint . '?plugin=' . rawurlencode( $item_name ) . '&version=' . $item_version;

		$response = wp_remote_post(
			$url,
			array(
				'timeout'   => apply_filters( 'automator_licensing_timeout', 20 ),
				'body'      => '',
				'headers'   => array(
					'X-UO-Licensing'   => $encoded_data,
					'X-UO-Destination' => 'ap',
				),
				'sslverify' => true,
			)
		);

		if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {

			$error = wp_remote_retrieve_body( $response );

			if ( is_wp_error( $response ) ) {
				$error = $response->get_error_message();
			}

			$query_params = array(
				'sl_activation' => 'false',
				'error_message' => urlencode( $error ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.urlencode_urlencode
			);

			if ( $should_redirect ) {

				$redirect = add_query_arg( $query_params, self::get_license_page_url() );

				wp_safe_redirect( $redirect );

				exit();
			}

			return new WP_Error( 400, 'Invalid license', $query_params );

		}

		return json_decode( wp_remote_retrieve_body( $response ) );
	}

Scroll to Top