Custom query filters on container/div/block

im trying to add custom filter options like the ones seen in BricksUltimate so i can make custom queries outside of the wp posts objects… i.e. amelia events and appointments.

Custom Amelia query loop is working well but i cannot seem to get the custom filters working…any ideas?

<?php 

// Custom Query Loop for Amelia Events
add_filter( 'bricks/setup/control_options', function( $control_options ) {
    $control_options['queryTypes']['bl_amelia_events'] = esc_html__( 'Amelia Events', 'my-plugin' );


    return $control_options;
});

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
    if ( $query_obj->object_type !== 'bl_amelia_events' ) {
        return $results;
    }

    global $wpdb;
    $prefix = $wpdb->prefix;

    // Retrieve user-specified query parameters
    $limit = $query_obj->settings['ameliaLimit'] ?? 10;
    $offset = $query_obj->settings['ameliaOffset'] ?? 0;
    $orderBy = $query_obj->settings['ameliaOrderBy'] ?? 'name';
    $order = $query_obj->settings['ameliaOrder'] ?? 'ASC';

    // Build the query with user-specified parameters
    $events = $wpdb->get_results("SELECT * FROM {$prefix}amelia_events ORDER BY $orderBy $order LIMIT $limit OFFSET $offset", ARRAY_A);
    $event_data = [];

    foreach ( $events as $event ) {
        $event_data[] = array_map('esc_html', $event); // Escaping data for security
    }

    return $event_data;
}, 10, 2 );

function add_query_controls( $controls ) {
    // Check if the current query type is 'bl_amelia_events'
    if ( isset( $controls['query']['objectType'] ) && $controls['query']['objectType']['default'] === 'bl_amelia_events' ) {
        // Merge your custom controls into the existing controls array
        $controls = array_merge( $controls, [
        'ameliaLimit' => [
            'type'    => 'number',
            'label'   => esc_html__( 'Limit', 'my-plugin' ),
            'min'     => 1,
            'default' => 10,
        ],
        'ameliaOffset' => [
            'type'    => 'number',
            'label'   => esc_html__( 'Offset', 'my-plugin' ),
            'min'     => 0,
            'default' => 0,
        ],
        'ameliaOrderBy' => [
            'type'    => 'select',
            'label'   => esc_html__( 'Order By', 'my-plugin' ),
            'options' => [
                'name'          => esc_html__('Name', 'my-plugin'),
                'bookingOpens'  => esc_html__('Booking Opens', 'my-plugin'),
                'bookingCloses' => esc_html__('Booking Closes', 'my-plugin'),
                // Add other fields as needed
            ],
            'default' => 'name',
        ],
        'ameliaOrder' => [
            'type'    => 'select',
            'label'   => esc_html__( 'Order', 'my-plugin' ),
            'options' => [
                'ASC'  => esc_html__('Ascending', 'my-plugin'),
                'DESC' => esc_html__('Descending', 'my-plugin'),
            ],
            'default' => 'ASC',
        ],
            // Add other controls (ameliaOffset, ameliaOrderBy, ameliaOrder) in a similar fashion
        ]);
    }

    return $controls;
}

// Hook the function into Bricks elements
add_filter( 'bricks/elements/container/controls', 'add_query_controls' );
add_filter( 'bricks/elements/div/controls', 'add_query_controls' );
add_filter( 'bricks/elements/block/controls', 'add_query_controls' );
// Function to get property from the custom loop object
function bl_get_custom_loop_object_property( $name ) {
    $loop_object = \Bricks\Query::get_loop_object();
    if ( ! $loop_object || ! is_array( $loop_object ) || ! array_key_exists( $name, $loop_object ) ) {
        return false;
    }
    return $loop_object[$name];
}

// Function to get keys for dynamic tag generation
function get_event_data_keys() {
    global $wpdb;
    $prefix = $wpdb->prefix;
    $keys = $wpdb->get_col("DESCRIBE {$prefix}amelia_events");
    return $keys ?: [];
}

// Register dynamic tags based on Amelia Event properties
add_filter( 'bricks/dynamic_tags_list', 'add_my_tags_to_builder' );
function add_my_tags_to_builder( $tags ) {
    $keys = get_event_data_keys();
    foreach ($keys as $key) {
        $tags[] = [
            'name'  => "{amelia_event_{$key}}",
            'label' => "Amelia Event " . ucfirst($key),
            'group' => 'Amelia Event Data',
        ];
    }
    return $tags;
}

// Render tag value based on Amelia Event properties
add_filter( 'bricks/dynamic_data/render_tag', 'get_my_tag_value', 10, 3 );
function get_my_tag_value( $tag, $post, $context = 'text' ) {
    $keys = get_event_data_keys();
    foreach ($keys as $key) {
        if ( $tag === "amelia_event_{$key}" ) {
            return bl_get_custom_loop_object_property( $key );
        }
    }
    return $tag;
}

// Optional: Render content filter, if needed
add_filter( 'bricks/dynamic_data/render_content', 'render_my_tag', 10, 3 );
add_filter( 'bricks/frontend/render_data', 'render_my_tag', 10, 2 );
function render_my_tag( $content, $post, $context = 'text' ) {
    if ( strpos( $content, '{amelia_event_' ) === false ) {
        return $content;
    }

    $keys = get_event_data_keys();
    foreach ($keys as $key) {
        if ( strpos( $content, "{amelia_event_{$key}}" ) !== false ) {
            $value = bl_get_custom_loop_object_property( $key );
            $content = str_replace( "{amelia_event_{$key}}", $value, $content );
        }
    }
    return $content;
}

1 Like