Last active
January 21, 2025 10:25
-
-
Save girafffee/2b4ea3d10f6ddcedaa991de5f06f28ad to your computer and use it in GitHub Desktop.
Pretty formating repeater values in Send Email action content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Works in JetFormBuilder >= 2.1.0 | |
*/ | |
add_filter( | |
'jet-form-builder/send-email/template-repeater', | |
/** | |
* Verbose repeater items array | |
* | |
* @param string $content | |
* @param array $items | |
* @param \Jet_Form_Builder\Classes\Macros_Parser $parser | |
* | |
* @return string | |
*/ | |
function ( string $content, array $items, $parser ) { | |
/** @var \Jet_Form_Builder\Actions\Types\Send_Email $action */ | |
$action = jet_fb_action_handler()->get_current_action(); | |
$is_html = 'text/html' === $action->get_content_type(); | |
$index = 0; | |
$separator = $is_html ? '<br>' : "\n\r"; | |
$tab = $is_html ? ' ' : "\t"; | |
$repeater_label = 'Repeater Heading'; | |
$repeater_name = function_exists( 'jet_fb_context' ) ? $parser->get_current_macro() : ''; | |
$rows = array(); | |
$if_array = function ( $value ) { | |
return is_array( $value ) ? implode( ', ', $value ) : $value; | |
}; | |
foreach ( $items as $item ) { | |
$item_data = array(); | |
foreach ( $item as $key => $value ) { | |
// for JetFormBuilder >= 3.1 | |
if ( function_exists( 'jet_fb_context' ) ) { | |
$label = jet_fb_context()->get_setting( 'label', array( $repeater_name, $index, $key ) ); | |
} else { | |
$label = jet_fb_request_handler()->get_attr( $key, 'label', $key ); | |
} | |
$item_data[] = sprintf( '%1$s: %2$s', $label, call_user_func( $if_array, $value ) ); | |
} | |
$row = 'Repeater Item ' . ++$index . $separator . $tab; | |
$row .= implode( $separator . $tab, $item_data ); | |
$rows[] .= $row; | |
} | |
return ( $separator . $repeater_label . $separator . implode( $separator, $rows ) ); | |
}, | |
10, | |
3 | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment