Filter
uncanny-automator-pro
automator_armember_pro_parse_common_trigger_code
Filters armember_pro trigger codes to modify common trigger code values before they are processed.
add_filter( 'automator_armember_pro_parse_common_trigger_code', $callback, 10, 1 );
Description
This filter hook allows developers to modify the parsing of common ARMember Pro trigger codes before they are processed. It's applied when ARMember Pro tokens are being parsed, enabling custom logic for handling specific trigger code values. Developers can use this to add or alter how certain ARMember Pro data, like plan expiry, is represented or validated within Uncanny Automator.
Usage
add_filter( 'automator_armember_pro_parse_common_trigger_code', 'your_function_name', 10, 1 );
Parameters
-
$pieces(mixed) - This parameter holds an array containing specific keys like `ARM_PLAN_EXPIRES` to identify the type of data being processed for an Armember Pro trigger.
Return Value
The filtered value.
Examples
/**
* Example function to filter the ARM_PLAN_EXPIRES token value.
*
* This function demonstrates how to modify the output of the ARM_PLAN_EXPIRES token
* when it's used within Uncanny Automator recipes for MemberPress.
* For instance, it could be used to format the expiration date differently
* or to add a custom message based on the expiration.
*
* @param array $default_tokens The default tokens being processed. Expected to contain 'ARM_PLAN_EXPIRES'.
* @param array $args An array containing contextual information for the token replacement.
* Contains 'pieces', 'recipe_id', 'trigger_data', and 'user_id'.
*
* @return array Modified tokens with potentially altered values or added keys.
*/
add_filter(
'automator_armember_pro_parse_common_trigger_code',
function ( $default_tokens, $args ) {
// Ensure we are dealing with the correct token and have the necessary arguments.
if ( in_array( 'ARM_PLAN_EXPIRES', $default_tokens, true ) && isset( $args['user_id'] ) ) {
$user_id = $args['user_id'];
$pieces = $args['pieces'];
// Example: Fetch the actual plan expiration date for the user.
// This is a hypothetical scenario, actual implementation would depend on
// how MemberPress stores and exposes this data to PHP.
// We'll assume a function like `get_user_memberpress_plan_expiration_date()` exists.
$plan_expiration_date = get_user_memberpress_plan_expiration_date( $user_id );
// If a plan expiration date is found, format it or add custom logic.
if ( $plan_expiration_date ) {
// Example: Format the date to be more human-readable.
$formatted_expiration = date_i18n( get_option( 'date_format' ), strtotime( $plan_expiration_date ) );
// Replace the default placeholder with our formatted date.
// We assume the $pieces array structure allows us to identify which token to replace.
// In this specific hook context, $default_tokens might be the array
// of tokens that Uncanny Automator needs to process for replacement.
// So, we might need to modify the actual token value that will be used later.
// For demonstration, let's assume we're modifying the value that
// will be used when the token is replaced in the recipe.
// A more realistic approach might involve modifying a specific entry in $pieces.
// Since the hook is named 'parse_common_trigger_code' and the parameter is $default_tokens,
// it's more likely that we are intended to manipulate the array of tokens themselves
// or their default values before they are further processed.
// Let's assume $default_tokens is an array where keys are token identifiers
// and values are their default output.
// If we want to replace the 'ARM_PLAN_EXPIRES' token's output:
if ( isset( $default_tokens['ARM_PLAN_EXPIRES'] ) ) {
$default_tokens['ARM_PLAN_EXPIRES'] = sprintf(
__( 'Plan expires on %s', 'your-text-domain' ),
$formatted_expiration
);
}
// Alternatively, if $default_tokens is just a list of tokens to be processed,
// and we need to return information to Uncanny Automator for replacement:
// This requires a deeper understanding of Uncanny Automator's internal token replacement logic.
// For this example, we'll stick to modifying the $default_tokens array directly if it's meant to hold values.
// If ARM_PLAN_EXPIRES is just an identifier in the $default_tokens array:
if ( is_array( $default_tokens ) && in_array( 'ARM_PLAN_EXPIRES', $default_tokens ) ) {
// This hook might be designed to *add* custom data related to tokens.
// Let's assume we need to add our formatted date under a custom key
// that Uncanny Automator will later use.
// The exact structure depends on the $pieces variable and how it's used.
// Let's re-evaluate the source context provided.
// `apply_filters('automator_armember_pro_parse_common_trigger_code', array('ARM_PLAN_EXPIRES'), ...)`
// This suggests the first parameter is an array of token *identifiers*.
// The function `parse_armember_tokens` then likely uses these identifiers to fetch data.
// Therefore, this filter is probably for *validating* or *modifying* the list of tokens to be processed,
// or perhaps adding custom data to be available during parsing.
// Let's assume the goal is to ensure ARM_PLAN_EXPIRES is processed,
// and we want to *provide* the actual data for it here.
// Uncanny Automator usually expects data in a structured format.
// If the intention is to *add* custom data to be used for replacement:
// We'll need to ensure the actual data is available.
// The `$pieces` array is crucial here. Based on the outer function
// `parse_armember_tokens($value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args)`
// `$pieces` likely contains information about the specific token placeholder.
// Let's assume `$pieces[1]` is the token identifier (e.g., 'ARM_PLAN_EXPIRES')
// and `$pieces[2]` is the index within a larger replacement array.
// The `$value` parameter in `parse_armember_tokens` is what gets returned if no modification occurs.
// This filter hook seems to be called *before* that final return.
// For this filter, let's assume we want to *modify* the data available for the 'ARM_PLAN_EXPIRES' token.
// If the hook intends to return a modified array of tokens or their data:
$modified_tokens = $default_tokens; // Start with the original tokens
if ( in_array( 'ARM_PLAN_EXPIRES', $modified_tokens ) ) {
// Fetch the actual expiration date
$expiration_timestamp = get_user_memberpress_plan_expiration_date_timestamp( $user_id ); // Hypothetical function returning timestamp
if ( $expiration_timestamp ) {
// Add the formatted date as extra data associated with the token
// Uncanny Automator might look for specific keys like '_data' or similar.
// This is speculative without exact Uncanny Automator internal structure.
// A common pattern is to pass an array for each token, containing its value.
// Let's assume the filter is meant to return an array of token => replacement_value.
// If $default_tokens is meant to be populated with replacement values:
$replacement_value = sprintf(
__( 'Your membership plan expires on %s.', 'your-text-domain' ),
date_i18n( get_option( 'date_format' ), $expiration_timestamp )
);
// We need to map this back to the correct token in the context of $pieces.
// Since $default_tokens is just `array('ARM_PLAN_EXPIRES')` in the `apply_filters` call,
// it seems this filter's purpose is to *extend* or *modify* what gets processed.
// Let's try to return an array where 'ARM_PLAN_EXPIRES' is mapped to its actual formatted value.
// This requires a good understanding of what the `apply_filters` consumer expects.
// Given the structure, it's more likely that this filter is to *prepare* the values.
// If the primary function of `parse_armember_tokens` returns a value,
// and this filter is called with `array('ARM_PLAN_EXPIRES')`,
// it suggests this filter might be responsible for generating the *value* for 'ARM_PLAN_EXPIRES'
// before the main function uses it.
// Let's try to return an array that the main function can consume.
// Assuming the main function will iterate through the returned array and replace tokens.
// We'll return an array where the key is the token identifier and the value is the generated replacement.
return array(
'ARM_PLAN_EXPIRES' => sprintf(
__( 'Plan expires %s', 'your-text-domain' ),
date_i18n( get_option( 'date_format' ), $expiration_timestamp )
),
);
}
}
}
}
}
// If no modifications are made, return the original array of tokens.
return $default_tokens;
},
10, // Priority
2 // Accepted args: $default_tokens, $args
);
/**
* Hypothetical function to get MemberPress plan expiration date timestamp.
* Replace with actual MemberPress API call if available.
*
* @param int $user_id The user ID.
* @return int|false The expiration timestamp or false if not found.
*/
function get_user_memberpress_plan_expiration_date_timestamp( $user_id ) {
// This is a placeholder. You would use MemberPress's actual API
// to get the expiration date for a given user.
// Example:
/*
if ( class_exists( 'MeprUser' ) ) {
$mepr_user = new MeprUser( $user_id );
if ( $mepr_user && $mepr_user->is_member() ) {
$expiration_date = $mepr_user->get_expiration_date();
if ( $expiration_date ) {
return $expiration_date->getTimestamp();
}
}
}
*/
// For demonstration, return a fixed date if user ID is 1.
if ( $user_id === 1 ) {
return strtotime( '+1 month' );
}
return false;
}
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
uncanny-automator-pro/src/integrations/armember/tokens/armember-pro-tokens.php:138
public function parse_armember_tokens( $value, $pieces, $recipe_id, $trigger_data, $user_id, $replace_args ) {
if ( ! is_array( $pieces ) || ! isset( $pieces[1] ) || ! isset( $pieces[2] ) ) {
return $value;
}
$trigger_meta_validations = apply_filters(
'automator_armember_pro_parse_common_trigger_code',
array( 'ARM_PLAN_EXPIRES' ),
array(
'pieces' => $pieces,
'recipe_id' => $recipe_id,
'trigger_data' => $trigger_data,
'user_id' => $user_id,
'replace_args' => $replace_args,
)
);
if ( ! array_intersect( $trigger_meta_validations, $pieces ) ) {
return $value;
}
$to_replace = $pieces[2];
$member_id = Automator()->db->token->get( 'save_user_id', $replace_args );
$plan_id = Automator()->db->token->get( 'save_plan_id', $replace_args );
switch ( $to_replace ) {
case 'ARM_PLAN_EXPIRY_DATE':
$get_plan_details = get_user_meta( $member_id, 'arm_user_plan_' . $plan_id, true );
$value = date( 'F j, Y', strtotime( $get_plan_details->arm_expire_plan ) );
break;
}
return $value;
}