post_status ) { return $block_content; } // Get the condition data. $display_conditions = get_post_meta( $condition_id, '_gb_conditions', true ); if ( empty( $display_conditions ) ) { return $block_content; } // Pass the current post context to the conditions system. // This ensures conditions are evaluated for the correct post in loops. $context = array( 'post_id' => get_the_ID() ); // Use the existing conditions system to evaluate. $show = GenerateBlocks_Pro_Conditions::show( $display_conditions, $context ); // If invert is enabled, flip the result. if ( ! empty( $attributes['gbBlockConditionInvert'] ) ) { $show = ! $show; } // Return empty string to prevent block from rendering if condition is not met. if ( ! $show ) { return ''; } return $block_content; } /** * Add block conditions handler to usage search. * * @param array $handlers Existing handlers. * @param int $condition_id The condition ID. * @return array Modified handlers. */ public function add_block_usage_handler( $handlers, $condition_id ) { $handlers['block_conditions'] = [ 'method' => 'search_block_conditions_usage', 'label' => __( 'Block Conditions', 'generateblocks-pro' ), ]; return $handlers; } /** * Add condition attributes to all blocks during server-side registration. * This ensures ServerSideRender calls don't fail validation. * * @param array $args The block registration arguments. * @param string $block_type The block type name. * @return array Modified arguments. */ public function add_condition_attributes( $args, $block_type ) { // Ensure attributes array exists. if ( ! isset( $args['attributes'] ) ) { $args['attributes'] = []; } // Add gbBlockCondition attribute if not already defined. if ( ! isset( $args['attributes']['gbBlockCondition'] ) ) { $args['attributes']['gbBlockCondition'] = [ 'type' => 'string', 'default' => '', ]; } // Add gbBlockConditionInvert attribute if not already defined. if ( ! isset( $args['attributes']['gbBlockConditionInvert'] ) ) { $args['attributes']['gbBlockConditionInvert'] = [ 'type' => 'boolean', 'default' => false, ]; } return $args; } } // Initialize the class. GenerateBlocks_Pro_Block_Conditions::get_instance()->init();