%2$s

', get_block_wrapper_attributes(), esc_html__( 'Advanced Custom Fields plugin is required.', 'agilepress-content-block-for-acf' ) ); } /** * Process ACF conditional blocks with {{#if}}...{{/if}} syntax * Supports: * - {{#if field}} - checks if field exists and is truthy * - {{#if field = value}} - checks if field equals value * - {{#if field != value}} - checks if field does not equal value * - {{#elseif field}} - alternative condition * - {{#else}} - fallback */ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable, not a global $agilepress_acf_block_processed = preg_replace_callback( '/\{\{#if ([^}]+)\}\}(.*?)(?:\{\{#elseif ([^}]+)\}\}(.*?))*(?:\{\{#else\}\}(.*?))?\{\{\/if\}\}/s', function( $matches ) use ( $agilepress_acf_block_post_id ) { $condition = trim( $matches[1] ); $if_content = $matches[2]; $else_content = isset( $matches[5] ) ? $matches[5] : ''; // Parse condition: field, field = value, or field != value $field_name = ''; $operator = 'exists'; $compare_value = ''; if ( strpos( $condition, '!=' ) !== false ) { $parts = array_map( 'trim', explode( '!=', $condition, 2 ) ); $field_name = $parts[0]; $operator = '!='; $compare_value = isset( $parts[1] ) ? $parts[1] : ''; } elseif ( strpos( $condition, '=' ) !== false ) { $parts = array_map( 'trim', explode( '=', $condition, 2 ) ); $field_name = $parts[0]; $operator = '='; $compare_value = isset( $parts[1] ) ? $parts[1] : ''; } else { $field_name = $condition; } // Get field value $field_value = get_field( $field_name, $agilepress_acf_block_post_id ); // Evaluate condition $condition_met = false; if ( 'exists' === $operator ) { $condition_met = ! empty( $field_value ); } elseif ( '=' === $operator ) { $condition_met = ( (string) $field_value === (string) $compare_value ); } elseif ( '!=' === $operator ) { $condition_met = ( (string) $field_value !== (string) $compare_value ); } return $condition_met ? $if_content : $else_content; }, $agilepress_acf_block_content ); /** * Process ACF repeater/gallery loops with {{field_name}}...{{/field_name}} syntax */ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable, not a global $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_processed ); /** * Process ACF group fields with {group_name:sub_field_name} syntax */ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable, not a global $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 */ // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Template variable, not a global $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 ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound $agilepress_acf_block_wrapper_attributes = get_block_wrapper_attributes(); ?>
>