Filter uncanny-automator

uap_option_all_buddypress_groups

Filters the list of BuddyPress groups displayed in the UAP settings, allowing modification of the available groups.

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

Description

Fires when retrieving options for all BuddyPress groups. Developers can filter the $option array to modify the group selection settings, such as changing labels, input types, or required status. This hook is crucial for customizing how BuddyPress groups are presented as options within Uncanny Automator.


Usage

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

Parameters

$option (mixed)
This parameter is used to set the label for the select box where users can choose a BuddyPress group.

Return Value

The filtered value.


Examples

<?php

/**
 * Example of how to filter the available BuddyPress groups for the Uncanny Automator integration.
 * This function might be used to exclude certain groups from being available in an automation trigger or action.
 *
 * @param array $option The array containing options for the BuddyPress group selection field.
 * @return array The modified array of options.
 */
function my_automator_filter_buddypress_groups( $option ) {
	// Check if the option code is the one we're interested in (e.g., for a specific trigger or action).
	// Replace 'your_specific_option_code' with the actual option code if known,
	// otherwise, we'll filter all BuddyPress group options.
	if ( isset( $option['option_code'] ) && 'your_specific_option_code' === $option['option_code'] ) {
		// Example: Exclude groups with IDs 10 and 25 from the selection.
		$excluded_group_ids = array( 10, 25 );

		// Filter the 'options' array to remove the excluded groups.
		if ( ! empty( $option['options'] ) ) {
			$option['options'] = array_filter( $option['options'], function( $group ) use ( $excluded_group_ids ) {
				// Assuming each option in $option['options'] is an associative array
				// with a key like 'value' representing the group ID.
				if ( isset( $group['value'] ) && in_array( $group['value'], $excluded_group_ids, true ) ) {
					return false; // Exclude this group
				}
				return true; // Keep this group
			} );
		}
	} elseif ( ! isset( $option['option_code'] ) ) {
        // If no specific option_code is set, assume we want to filter all BuddyPress group options.
        // Example: Add a default "None Selected" option if the list is empty after filtering.
        if ( empty( $option['options'] ) ) {
            $option['options'][] = array(
                'value' => '',
                'label' => __( 'No BuddyPress groups found.', 'your-text-domain' ),
            );
        }
    }

	return $option;
}
add_filter( 'uap_option_all_buddypress_groups', 'my_automator_filter_buddypress_groups', 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/buddypress/helpers/buddypress-helpers.php:109

public function all_buddypress_groups( $label = null, $option_code = 'BPGROUPS', $args = array() ) {

		$args = wp_parse_args(
			$args,
			array(
				'uo_include_any' => false,
				'uo_any_label'   => esc_attr__( 'Any group', 'uncanny-automator' ),
				'status'         => array( 'public' ),
			)
		);

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

		global $wpdb;
		$qry     = "SHOW TABLES LIKE '{$wpdb->prefix}bp_groups';";
		$options = array();

		if ( Automator()->helpers->recipe->load_helpers ) {
			if ( $args['uo_include_any'] ) {
				$options[- 1] = $args['uo_any_label'];
			}

			if ( $wpdb->query( $qry ) ) { // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
				// Previous solution was not preparing correct query.
				$in_str_arr = array_fill( 0, count( $args['status'] ), '%s' );
				$in_str     = join( ',', $in_str_arr );

				$results = $wpdb->get_results(
					$wpdb->prepare(
						"SELECT * FROM {$wpdb->prefix}bp_groups WHERE status IN ($in_str)", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
						$args['status']
					)
				);

				if ( $results ) {
					foreach ( $results as $result ) {
						$options[ $result->id ] = $result->name;
					}
				}
			}
		}//end if

		$option = array(
			'option_code'              => $option_code,
			'label'                    => $label,
			'input_type'               => 'select',
			'required'                 => true,
			'options'                  => $options,
			'custom_value_description' => _x( 'Group ID', 'BuddyPress', 'uncanny-automator' ),
		);

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

Scroll to Top