Filter
uncanny-automator
uap_option_get_all_mailpoet_lists
Filters the retrieved Mailpoet lists before they are returned for use.
add_filter( 'uap_option_get_all_mailpoet_lists', $callback, 10, 1 );
Description
Filters the array of Mailpoet lists retrieved by Uncanny Automator, allowing developers to modify the available lists for triggers and actions. This hook fires when Automator is fetching Mailpoet lists. Developers can add, remove, or rename lists before they are presented to the user.
Usage
add_filter( 'uap_option_get_all_mailpoet_lists', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter is a mixed type variable that holds the value of the option being filtered, allowing for modification or inspection.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to hook into the 'uap_option_get_all_mailpoet_lists' filter.
* This example demonstrates how to add a custom check or modification
* to the MailPoet lists retrieved by Uncanny Automator.
*
* In this scenario, we'll assume we want to exclude any MailPoet lists
* that have "Internal" in their name.
*
* @param array $option An array containing MailPoet lists and their associated data.
* Expected structure:
* [
* 'options' => [
* 'list_id_1' => 'List Name 1',
* 'list_id_2' => 'List Name 2',
* // ... more lists
* ],
* 'relevant_tokens' => [
* 'list_id_1' => 'List Name 1',
* 'list_id_1_ID' => 'List ID 1',
* // ... more tokens
* ],
* ]
* @return array The modified $option array, potentially with some lists filtered out.
*/
add_filter( 'uap_option_get_all_mailpoet_lists', function ( $option ) {
// Check if the 'options' key exists and is an array.
if ( isset( $option['options'] ) && is_array( $option['options'] ) ) {
// Iterate over the MailPoet lists.
foreach ( $option['options'] as $list_id => $list_name ) {
// If the list name contains "Internal" (case-insensitive),
// remove it from the options and its corresponding tokens.
if ( stripos( $list_name, 'internal' ) !== false ) {
unset( $option['options'][ $list_id ] );
// Also remove the corresponding tokens.
if ( isset( $option['relevant_tokens'][ $list_id ] ) ) {
unset( $option['relevant_tokens'][ $list_id ] );
}
if ( isset( $option['relevant_tokens'][ $list_id . '_ID' ] ) ) {
unset( $option['relevant_tokens'][ $list_id . '_ID' ] );
}
}
}
}
// Always return the (potentially modified) option array.
return $option;
}, 10, 1 ); // 10 is the default priority, 1 is the number of accepted arguments.
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:96
public function get_all_mailpoet_lists( $label = null, $option_code = 'MAILPOETLISTS', $args = array() ) {
$any_option = key_exists( 'any_option', $args ) ? $args['any_option'] : false;
$all_include = key_exists( 'all_include', $args ) ? $args['all_include'] : false;
if ( ! $label ) {
$label = esc_attr_x( 'List', 'Mailpoet', 'uncanny-automator' );
}
$options = array();
if ( true === $any_option ) {
$options['-1'] = esc_attr_x( 'Any list', 'Mailpoet', 'uncanny-automator' );
}
if ( true === $all_include ) {
$options['all'] = esc_attr_x( 'All lists', 'Mailpoet', 'uncanny-automator' );
}
$mailpoet = MailPoetAPIAPI::MP( 'v1' );
$all_lists = $mailpoet->getLists();
foreach ( $all_lists as $list ) {
$options[ $list['id'] ] = $list['name'];
}
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'options' => $options,
'relevant_tokens' => array(
$option_code => esc_attr_x( 'List', 'Mailpoet', 'uncanny-automator' ),
$option_code . '_ID' => esc_attr_x( 'List ID', 'Mailpoet', 'uncanny-automator' ),
),
);
return apply_filters( 'uap_option_get_all_mailpoet_lists', $option );
}