%2$s

', get_block_wrapper_attributes(), esc_html__( 'Advanced Custom Fields plugin is required.', 'agilepress-content-block-for-acf' ) ); } /** * Process ACF repeater/gallery loops with {{field_name}}...{{/field_name}} syntax */ $agilepress_acf_block_processed = preg_replace_callback( '/\{\{([^}]+)\}\}(.*?)\{\{\/\1\}\}/s', function( $matches ) use ( $agilepress_acf_block_post_id ) { $field_name = trim( $matches[1] ); $template = $matches[2]; $field_value = get_field( $field_name, $agilepress_acf_block_post_id ); // Handle repeater fields. if ( is_array( $field_value ) && ! empty( $field_value ) ) { $output = ''; foreach ( $field_value as $row ) { $row_output = $template; // Determine if it's a gallery (image array) or a normal repeater. $is_gallery = ( isset( $row['url'] ) && isset( $row['alt'] ) ); $row_output = preg_replace_callback( '/\{([^}:]+)\}/', function( $sub_matches ) use ( $row, $is_gallery ) { $sub_field = trim( $sub_matches[1] ); $value = ''; if ( $is_gallery ) { // Gallery logic: field names are keys of the image array. if ( isset( $row[ $sub_field ] ) ) { $value = $row[ $sub_field ]; } } else { // Repeater logic. if ( isset( $row[ $sub_field ] ) ) { $value = $row[ $sub_field ]; } } // Handle URL explicitly for security inside tags. if ( 'url' === $sub_field || 'link' === $sub_field || ( is_array( $value ) && isset( $value['url'] ) ) ) { $url_val = is_array( $value ) ? $value['url'] : $value; return esc_url( $url_val ); } // Return raw value (can be HTML). It will be sanitized by wp_kses_post at the end. // If value is array/object and not handled above, return empty to avoid errors. if ( is_array( $value ) || is_object( $value ) ) { return ''; } return $value; }, $row_output ); $output .= $row_output; } return $output; } return ''; }, $agilepress_acf_block_content ); /** * Process ACF group fields with {group_name:sub_field_name} syntax */ $agilepress_acf_block_processed = preg_replace_callback( '/\{([^}:]+):([^}]+)\}/', function( $matches ) use ( $agilepress_acf_block_post_id ) { $group_name = trim( $matches[1] ); $sub_field_name = trim( $matches[2] ); $group_value = get_field( $group_name, $agilepress_acf_block_post_id ); if ( is_array( $group_value ) && isset( $group_value[ $sub_field_name ] ) ) { $field_value = $group_value[ $sub_field_name ]; if ( is_array( $field_value ) && isset( $field_value['url'] ) ) { return esc_url( $field_value['url'] ); } if ( is_string( $field_value ) || is_numeric( $field_value ) ) { return $field_value; // Raw return, sanitized later. } } // If field not found, return original string to avoid breaking CSS/JS that uses braces. return $matches[0]; }, $agilepress_acf_block_processed ); /** * Replace simple ACF field tags with actual field values */ $agilepress_acf_block_processed = preg_replace_callback( '/\{([^}:]+)\}/', function( $matches ) use ( $agilepress_acf_block_post_id ) { $field_name = trim( $matches[1] ); // Get the field value. $field_value = get_field( $field_name, $agilepress_acf_block_post_id ); // If get_field returns null, the field does not exist in ACF. // We return the original match to avoid breaking CSS/JS {curly braces}. if ( null === $field_value ) { return $matches[0]; } // Handle array returns (like Image or Link objects). if ( is_array( $field_value ) ) { if ( isset( $field_value['url'] ) ) { return esc_url( $field_value['url'] ); } elseif ( isset( $field_value['value'] ) ) { return $field_value['value']; } return ''; } if ( false !== $field_value ) { return $field_value; // Raw return, sanitized later. } return ''; }, $agilepress_acf_block_processed ); $agilepress_acf_block_wrapper = get_block_wrapper_attributes(); ?>
>