Filter uncanny-automator

automator_review_get_credits_remaining

Filters the remaining credits for a user review, allowing modification before display.

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

Description

This filter hook fires when determining the remaining credits for a user. Developers can modify the default 250 credits or the calculated remaining credits. It's applied after initial checks for usage data, allowing custom credit logic or default values.


Usage

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

Parameters

$this (mixed)
This parameter contains information about the user's connection status and their paid usage count, which is used to determine the remaining credits.

Return Value

The filtered value.


Examples

/**
 * Example of using the 'automator_review_get_credits_remaining' filter.
 *
 * This example demonstrates how to modify the remaining credits calculation
 * for a user. It checks if the calculated credits are less than a certain threshold
 * and applies a bonus if they are, or applies a deduction if they are not.
 *
 * @param mixed $credits_remaining The current calculated remaining credits.
 * @param mixed $object            The instance of the Automator_Review class.
 *
 * @return int The modified remaining credits.
 */
function my_automator_adjust_credits_remaining( $credits_remaining, $object ) {
	// Define a threshold for applying a bonus or deduction.
	$bonus_threshold = 50;
	$bonus_amount    = 10;
	$deduction_amount = 5;

	// Ensure we are working with an integer value.
	$credits_remaining = absint( $credits_remaining );

	// Check if the user is an administrator or has a specific role.
	// This is a placeholder; in a real scenario, you'd have a proper user check.
	if ( current_user_can( 'manage_options' ) ) {
		// If credits are below the threshold, add a bonus.
		if ( $credits_remaining < $bonus_threshold ) {
			$credits_remaining += $bonus_amount;
			// Optionally log this adjustment.
			// error_log( 'Automator: Applied bonus credits. New remaining: ' . $credits_remaining );
		}
	} else {
		// For non-administrators, potentially deduct a small amount if credits are high.
		// This is just an illustrative example of applying a different logic.
		if ( $credits_remaining > $bonus_threshold * 2 ) {
			$credits_remaining -= $deduction_amount;
			// Ensure credits don't go below zero after deduction.
			$credits_remaining = max( 0, $credits_remaining );
			// Optionally log this adjustment.
			// error_log( 'Automator: Applied deduction. New remaining: ' . $credits_remaining );
		}
	}

	// Return the modified credits.
	return $credits_remaining;
}
add_filter( 'automator_review_get_credits_remaining', 'my_automator_adjust_credits_remaining', 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/core/admin/class-automator-review.php:1005
src/core/admin/class-automator-review.php:1010

public function get_credits_remaining( $user_connected ) {

		if ( false === $user_connected || empty( $user_connected['usage_limit'] ) || empty( $user_connected['paid_usage_count'] ) ) {
			// Assume unused if credits are empty.
			return apply_filters( 'automator_review_get_credits_remaining', 250, $this );
		}

		$credits_remaining = absint( intval( $user_connected['usage_limit'] ) - intval( $user_connected['paid_usage_count'] ) );

		return apply_filters( 'automator_review_get_credits_remaining', $credits_remaining, $this );
	}

Scroll to Top