/*
 Theme Name:   Bricks Child Theme
 Theme URI:    https://bricksbuilder.io/
 Description:  Use this child theme to extend Bricks.
 Author:       Bricks
 Author URI:   https://bricksbuilder.io/
 Template:     bricks
 Version:      1.1
 Text Domain:  bricks
*/

// Add new query type control called "Shipment Coverage Area"
add_filter( 'bricks/setup/control_options', function( $control_options ) {
	$control_options['queryTypes']['zone_0_ct_me_ma_nh_nj_ri_vt_b'] = esc_html__( 'Shipment Coverage Area' );

	return $control_options;
} );

// Avoid Undefined Function Error when Meta Box is not active.
if ( ! function_exists( 'rwmb_meta' ) ) {
	function rwmb_meta( $key, $args = '', $post_id = null ) {
		return false;
	}
}

// Return an array of "Shipment Coverage Area" Checkbox List field's values for the current post.
add_filter( 'bricks/query/run', function( $results, $query_obj ) {
	if ( $query_obj->object_type !== 'zone_0_ct_me_ma_nh_nj_ri_vt_b' ) {
		return $results;
	}

	return rwmb_meta( 'shipment_coverage_area' );
}, 10, 2 );

// Function to return the value of the "Shipment Coverage Area" Checkbox List field for the current loop iteration.
function bl_get_mb_checkbox_list_services_provided_service(): string {
	// check if there is any active query looping (nested queries) and if yes, store the query ID of the most deep query
	$looping_query_id = \Bricks\Query::is_any_looping();

	// get_query_object_type gets the current query object type (post, term, user or a custom one)
	// if the query ID of the most deep query exists and the current query object type is bl_mb_checkbox_list_services_provided..
	if ( $looping_query_id && \Bricks\Query::get_query_object_type( $looping_query_id ) === 'zone_0_ct_me_ma_nh_nj_ri_vt_b' ) {
		return Bricks\Query::get_loop_object( $looping_query_id );
	}

	return '';
}