Filter uncanny-automator

uap_option_all_memberpress_products_recurring

Filters the option for all MemberPress recurring products, allowing modification before it's used.

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

Description

Filters the options for selecting recurring MemberPress products within Uncanny Automator. Developers can modify the available product options or add custom ones to tailor automation triggers and actions based on recurring MemberPress subscriptions. This hook fires when retrieving product options for use in Automator's recipe builder.


Usage

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

Parameters

$option (mixed)
This parameter contains the current value or default value of the option being filtered, which in this context is related to recurring MemberPress products.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of filtering the 'uap_option_all_memberpress_products_recurring' hook.
 * This function adds a custom option to the list of recurring MemberPress products,
 * for example, to exclude a specific product from being considered recurring in Uncanny Automator.
 *
 * @param array $option The original array of options for the select field.
 * @return array The modified array of options.
 */
function my_automator_filter_memberpress_recurring_products( $option ) {
    // Ensure we are working with the expected structure.
    if ( ! isset( $option['options'] ) || ! is_array( $option['options'] ) ) {
        return $option;
    }

    // Let's assume there's a MemberPress product with ID 123 that we want to explicitly mark as NOT recurring for Uncanny Automator.
    // In a real scenario, you might fetch this ID dynamically or have it configured elsewhere.
    $product_id_to_exclude = 123;

    // Iterate through the existing options and modify the one for our specific product ID.
    foreach ( $option['options'] as $product_id => &$product_data ) {
        if ( $product_id == $product_id_to_exclude ) {
            // If the product is found, let's add a custom label or modify its properties
            // to indicate it's not to be treated as recurring by Uncanny Automator.
            // For example, we could append '(Not Recurring)' to its label in the select list.
            // The exact property to modify would depend on how Uncanny Automator processes these options.
            // Assuming it might check for a specific key or a string pattern in the label.

            // A more robust approach might involve adding a custom flag if the hook structure supported it.
            // For this example, we'll modify the label to signify non-recurring.
            if ( isset( $product_data['label'] ) ) {
                $product_data['label'] .= ' (Not Recurring)';
            }
            // Alternatively, if Uncanny Automator checks a specific 'recurring' flag:
            // $product_data['recurring'] = false; // This assumes the structure allows such a key.
        }
    }
    unset($product_data); // Unset the reference to avoid unexpected behavior.

    // You could also add a new, entirely custom option if needed.
    // $option['options']['custom_recurring_option'] = array(
    //     'label' => 'My Custom Recurring Product',
    //     'recurring' => true, // Assuming this is how it's determined
    // );

    return $option;
}
add_filter( 'uap_option_all_memberpress_products_recurring', 'my_automator_filter_memberpress_recurring_products', 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/integrations/memberpress/helpers/memberpress-helpers.php:250
uncanny-automator-pro/src/integrations/memberpress/helpers/memberpress-pro-helpers.php:157

public function all_memberpress_products_recurring( $label = null, $option_code = 'MPPRODUCT', $args = array() ) {

		if ( ! $label ) {
			$label = esc_attr__( 'Product', 'uncanny-automator' );
		}

		$args = wp_parse_args(
			$args,
			array(
				'uo_include_any' => false,
				'uo_any_label'   => esc_attr__( 'Any recurring subscription product', 'uncanny-automator' ),
			)
		);

		$options = array();

		if ( $args['uo_include_any'] ) {
			$options['-1'] = $args['uo_any_label'];
		}

		$query_args = array(
			'post_type'      => 'memberpressproduct',
			'posts_per_page' => 999,
			'post_status'    => 'publish',
			'meta_query'     => array(
				array(
					'key'     => '_mepr_product_period_type',
					'value'   => 'lifetime',
					'compare' => '!=',
				),
			),
		);

		$results = Automator()->helpers->recipe->wp_query( $query_args );
		if ( ! empty( $results ) ) {
			foreach ( $results as $k => $v ) {
				$options[ $k ] = $v;
			}
		}

		$relevant_tokens = array(
			$option_code                => esc_attr__( 'Product title', 'uncanny-automator' ),
			$option_code . '_ID'        => esc_attr__( 'Product ID', 'uncanny-automator' ),
			$option_code . '_URL'       => esc_attr__( 'Product URL', 'uncanny-automator' ),
			$option_code . '_THUMB_ID'  => esc_attr__( 'Product featured image ID', 'uncanny-automator' ),
			$option_code . '_THUMB_URL' => esc_attr__( 'Product featured image URL', 'uncanny-automator' ),
		);

		if ( isset( $args['relevant_tokens'] ) && ! empty( $args['relevant_tokens'] ) && is_array( $args['relevant_tokens'] ) ) {
			$relevant_tokens = array_merge( $relevant_tokens, $args['relevant_tokens'] );
		}

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => 'select',
			'required'        => true,
			'options'         => $options,
			'relevant_tokens' => $relevant_tokens,
		);

		return apply_filters( 'uap_option_all_memberpress_products_recurring', $option );
	}

Scroll to Top