Filter uncanny-automator

uap_option_list_mailpoet_forms

Filters the list of MailPoet forms available for integration, allowing for customization before display.

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

Description

Filters the list of available Mailpoet form fields for use in Uncanny Automator. Developers can use this hook to add custom Mailpoet form field options or modify existing ones, allowing for greater flexibility when creating automations triggered by or interacting with Mailpoet forms.


Usage

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

Parameters

$option (mixed)
This parameter contains the value of the selected Mailpoet form.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to filter the MailPoet forms list in Uncanny Automator.
 *
 * This example demonstrates how to add a custom option to the list of MailPoet forms
 * that Uncanny Automator can use in its triggers and actions.
 *
 * In this specific example, we'll add a placeholder option to represent a "Generic MailPoet Form"
 * which might be useful if you wanted to trigger an automation based on any MailPoet form submission,
 * rather than a specific one.
 *
 * @param array $option The original array of MailPoet form options.
 * @return array The modified array of MailPoet form options.
 */
function my_uncanny_automator_filter_mailpoet_forms( $option ) {

    // Check if the expected structure exists before adding our custom option.
    // The structure is typically like:
    // [
    //     'label' => 'MailPoet Forms',
    //     'options' => [
    //         'MAILPOET_FORM_ID_1' => 'Form Name 1',
    //         'MAILPOET_FORM_ID_2' => 'Form Name 2',
    //         // ... other form IDs and names
    //     ]
    // ]
    if ( isset( $option['label'] ) && $option['label'] === esc_attr_x( 'MailPoet Forms', 'Mailpoet', 'uncanny-automator' ) && isset( $option['options'] ) && is_array( $option['options'] ) ) {

        // Define a unique key for our custom option. It's good practice to prefix
        // custom options to avoid conflicts.
        $custom_option_code = 'MAILPOET_GENERIC_FORM';

        // Add our custom option to the beginning of the options array.
        // You could also use array_push to add it to the end, or other array
        // manipulation functions to insert it at a specific position.
        $option['options'] = array_merge(
            [ $custom_option_code => esc_attr_x( 'Any MailPoet Form', 'Mailpoet', 'uncanny-automator' ) ],
            $option['options']
        );
    }

    // Always return the modified (or original) option array.
    return $option;
}

// Add the filter hook.
// The second argument '1' indicates that our callback function accepts 1 parameter.
// The default priority is 10, so we'll use that here.
add_filter( 'uap_option_list_mailpoet_forms', 'my_uncanny_automator_filter_mailpoet_forms', 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/mailpoet/helpers/mailpoet-helpers.php:187

public function list_mailpoet_forms( $label = null, $option_code = 'MAILPOETFORMS', $args = array() ) {
		global $wpdb;


		if ( ! $label ) {
			$label = esc_attr_x( 'Form', 'Mailpoet', 'uncanny-automator' );
		}

		$token        = key_exists( 'token', $args ) ? $args['token'] : false;
		$is_ajax      = key_exists( 'is_ajax', $args ) ? $args['is_ajax'] : false;
		$target_field = key_exists( 'target_field', $args ) ? $args['target_field'] : '';
		$end_point    = key_exists( 'endpoint', $args ) ? $args['endpoint'] : '';
		$options      = array();

		if ( Automator()->helpers->recipe->load_helpers ) {
			$forms = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}mailpoet_forms WHERE `deleted_at` IS NULL AND `status` = %s ORDER BY `id` LIMIT %d", 'enabled', 9999 ) );
			if ( ! empty( $forms ) ) {
				foreach ( $forms as $form ) {
					$options[ $form->id ] = esc_html( $form->name );
				}
			}
		}
		$type = 'select';

		$option = array(
			'option_code'     => $option_code,
			'label'           => $label,
			'input_type'      => $type,
			'required'        => true,
			'supports_tokens' => $token,
			'is_ajax'         => $is_ajax,
			'fill_values_in'  => $target_field,
			'endpoint'        => $end_point,
			'options'         => $options,
			'relevant_tokens' => array(
				$option_code                => esc_attr_x( 'Form title', 'Mailpoet', 'uncanny-automator' ),
				$option_code . '_ID'        => esc_attr_x( 'Form ID', 'Mailpoet', 'uncanny-automator' ),
				$option_code . '_FIRSTNAME' => esc_attr_x( 'First name', 'Mailpoet', 'uncanny-automator' ),
				$option_code . '_LASTNAME'  => esc_attr_x( 'Last name', 'Mailpoet', 'uncanny-automator' ),
				$option_code . '_EMAIL'     => esc_attr_x( 'Email', 'Mailpoet', 'uncanny-automator' ),
			),
		);

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

Scroll to Top