Filter uncanny-automator

automator_mepr_validate_trigger_meta_pieces_save

Filters MemberPress trigger meta pieces before saving to allow modification of renewal subscription data.

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

Description

Fires when validating MemberPress trigger meta pieces before saving. Developers can use this filter to modify the allowed meta pieces for MemberPress triggers. It's crucial to ensure the meta pieces you return are valid MemberPress trigger codes to prevent unintended behavior or errors.


Usage

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

Parameters

$args (mixed)
This parameter is an array of trigger types that are being validated for saving meta pieces.

Return Value

The filtered value.


Examples

/**
 * Example function to filter the trigger meta validations.
 *
 * This function demonstrates how to add a custom trigger type to the
 * 'automator_mepr_validate_trigger_meta_pieces_save' filter.
 *
 * @param array $trigger_meta_validations The existing array of trigger meta validations.
 * @param array $args The arguments passed to the filter.
 *
 * @return array The modified array of trigger meta validations.
 */
function my_custom_mepr_trigger_validation( $trigger_meta_validations, $args ) {
    // Add a custom trigger type to the list of validations.
    // For example, let's assume we have a custom MemberPress trigger for 'MP_USER_LOGIN_COMPLETE'.
    $trigger_meta_validations[] = 'MP_USER_LOGIN_COMPLETE';

    // You could also perform more complex logic here based on $args.
    // For instance, if you wanted to conditionally validate based on a specific subscription ID:
    // if ( isset( $args['entry_args']['subscription_id'] ) && $args['entry_args']['subscription_id'] === 123 ) {
    //     $trigger_meta_validations[] = 'ANOTHER_CUSTOM_TRIGGER';
    // }

    return $trigger_meta_validations;
}

// Hook the custom function into the filter.
// The priority is 10, and it accepts 2 arguments.
add_filter( 'automator_mepr_validate_trigger_meta_pieces_save', 'my_custom_mepr_trigger_validation', 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/integrations/memberpress/tokens/mp-tokens.php:34

public function save_token_data( $args, $trigger ) {
		if ( ! isset( $args['trigger_args'] ) || ! isset( $args['entry_args']['code'] ) ) {
			return;
		}

		$trigger_meta_validations = apply_filters(
			'automator_mepr_validate_trigger_meta_pieces_save',
			array( 'MP_RENEW_SUBSCRIPTION' ),
			$args
		);

		if ( in_array( $args['entry_args']['code'], $trigger_meta_validations, true ) ) {

			$event = array_shift( $args['trigger_args'] );

			/** @var MeprTransaction $transaction */
			$mepr_transaction = $event->get_data();

			$trigger_log_entry = $args['trigger_entry'];
			if ( ! empty( $mepr_transaction ) ) {
				Automator()->db->token->save( 'mp_txn_id', $mepr_transaction->id, $trigger_log_entry );
				Automator()->db->token->save( 'mp_txn_amount', $mepr_transaction->amount, $trigger_log_entry );
			}
		}
	}

Scroll to Top