Filter
uncanny-automator-pro
uap_option_list_buddypress_forums
Filters BuddyPress forum options for integration with the plugin, allowing customization before they are displayed.
add_filter( 'uap_option_list_buddypress_forums', $callback, 10, 1 );
Description
Filters the list of available options for BuddyPress forums in Uncanny Automator. Developers can use this hook to add or modify the tokens available for representing BuddyPress forum data within Automator recipes, such as forum titles, IDs, or URLs.
Usage
add_filter( 'uap_option_list_buddypress_forums', 'your_function_name', 10, 1 );
Parameters
-
$option(mixed) - This parameter contains the current value of the option being filtered.
Return Value
The filtered value.
Examples
<?php
/**
* Example of filtering the 'uap_option_list_buddypress_forums' hook.
* This example adds an extra token for 'Forum Slug' if the forum exists and has a slug.
*
* @param array $option The original option array.
* @return array The modified option array.
*/
function my_custom_automator_bp_forum_tokens( $option ) {
// Check if the 'relevant_tokens' key exists and if it's an array
if ( isset( $option['relevant_tokens'] ) && is_array( $option['relevant_tokens'] ) ) {
// Assuming $option_code is available in the context where the filter is applied.
// In a real scenario, you might need to find a way to access or pass this.
// For demonstration purposes, let's assume we can get it or it's passed implicitly.
// If $option_code isn't directly accessible, you might need to modify the source
// to pass it as an argument to the filter.
// For this example, we'll simulate it based on a hypothetical scenario.
// Let's assume $option_code is derived from the label or another part of $option.
// A realistic approach would be to have a way to determine the $option_code.
// For this example, let's assume the $option_code is available through a separate mechanism
// or if the $option itself contains information about the $option_code.
// Since the source context shows `$option_code` being used, we'll try to simulate its presence.
// In a real plugin, the `$option_code` is usually determined before the `apply_filters`.
// Let's simulate obtaining the option_code. This is a crucial assumption.
// If the original function provided $option_code as a parameter to the filter,
// the closure would have access to it. Since it's not, we'll make a hypothetical assumption.
$hypothetical_option_code = 'my_bp_forum_token'; // Replace with actual logic if available
// Let's try to extract the actual forum ID from the $option, if possible.
// This depends on how $option is structured. Looking at the source,
// $option seems to be an array that includes 'relevant_tokens'.
// The key to the existing tokens often includes the option_code.
// For example, if $option['relevant_tokens'] contains 'forum_title',
// then $option_code would be 'forum'.
$existing_keys = array_keys( $option['relevant_tokens'] );
$found_option_code = false;
foreach ( $existing_keys as $key ) {
if ( str_ends_with( $key, '_title' ) ) {
$potential_code = substr( $key, 0, -strlen( '_title' ) );
// Further validation could be done here if needed
$hypothetical_option_code = $potential_code;
$found_option_code = true;
break;
}
}
if ( ! $found_option_code ) {
// If we can't deduce the option code, we might have to skip adding new tokens or
// use a default that might not be dynamic.
// For this example, we'll assume we *can* deduce it or it's available.
// If not, this part of the logic would be skipped gracefully.
}
// If we have a deduced option code and it's not empty
if ( ! empty( $hypothetical_option_code ) ) {
// Let's assume we can retrieve the forum object to get its slug.
// This requires knowing how the $option array is populated and what data it contains.
// The original function is likely iterating through forums.
// Let's assume the option array passed to the filter might contain
// the forum object or its ID, which can be used to get the slug.
// A realistic scenario: the $option array might already contain the forum object.
// Or, the filter might be applied within a loop where the forum object is accessible.
// For demonstration, let's assume we can get a forum object via its ID.
// In the context of Uncanny Automator, the $option is likely generated for a specific automation trigger/action.
// If the trigger/action is "BuddyPress Forum Created", then the forum object would be available.
// If it's for selecting a forum, the option might not have the forum object directly.
// For this example, let's make a realistic assumption:
// The original function iterating through forums passes the forum ID or object.
// If the 'relevant_tokens' are generated from a specific forum, that forum's data should be accessible.
// Let's hypothesize that the $option array *itself* might contain a 'forum_object' or 'forum_id'.
// This is a common pattern in WordPress development where context is passed.
$forum_object = null;
if ( isset( $option['forum_object'] ) && is_object( $option['forum_object'] ) ) {
$forum_object = $option['forum_object'];
} elseif ( isset( $option['forum_id'] ) && (int) $option['forum_id'] > 0 ) {
// If only ID is available, try to get the object.
// This requires BuddyPress to be active and the forum to exist.
if ( function_exists( 'bp_forums_get_forum' ) ) {
$forum_object = bp_forums_get_forum( array( 'forum_id' => (int) $option['forum_id'] ) );
}
}
// If we successfully obtained a forum object
if ( $forum_object && isset( $forum_object->slug ) && ! empty( $forum_object->slug ) ) {
$option['relevant_tokens'][ $hypothetical_option_code . '_SLUG' ] = esc_attr_x( 'Forum Slug', 'Buddypress', 'uncanny-automator' );
}
}
}
return $option;
}
add_filter( 'uap_option_list_buddypress_forums', 'my_custom_automator_bp_forum_tokens', 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
uncanny-automator-pro/src/integrations/buddypress/helpers/buddypress-pro-helpers.php:280
public function list_buddypress_forums( $label = null, $option_code = 'BPFORUMS', $args = array() ) {
if ( ! function_exists( 'bbp_get_forum_post_type' ) ) {
return Automator()->helpers->recipe->build_default_options_array( $label, $option_code );
}
$args = wp_parse_args(
$args,
array(
'uo_include_any' => false,
'uo_any_label' => esc_attr_x( 'Any forum', 'Buddypress', 'uncanny-automator' ),
)
);
if ( ! $label ) {
$label = esc_attr_x( 'Forum', 'Buddypress', 'uncanny-automator' );
}
$options = array();
$forum_args = array(
'post_type' => bbp_get_forum_post_type(),
'posts_per_page' => 999, // phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
'orderby' => 'title',
'order' => 'ASC',
'post_status' => array( 'publish', 'private' ),
);
if ( $args['uo_include_any'] ) {
$options[- 1] = $args['uo_any_label'];
}
$forums = Automator()->helpers->recipe->options->wp_query( $forum_args );
if ( ! empty( $forums ) ) {
foreach ( $forums as $key => $forum ) {
$options[ $key ] = $forum;
}
}
$option = array(
'option_code' => $option_code,
'label' => $label,
'input_type' => 'select',
'required' => true,
'options' => $options,
'relevant_tokens' => array(
$option_code => esc_attr_x( 'Forum title', 'Buddypress', 'uncanny-automator' ),
$option_code . '_ID' => esc_attr_x( 'Forum ID', 'Buddypress', 'uncanny-automator' ),
$option_code . '_URL' => esc_attr_x( 'Forum URL', 'Buddypress', 'uncanny-automator' ),
),
);
return apply_filters( 'uap_option_list_buddypress_forums', $option );
}