Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update class-notification.php - add products tag #245

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions includes/class-notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,18 +521,20 @@ public static function replace_field_tags( $text, $entry_id = null ) {
// Users looking for {field:something} or {value:something}, determine which one.
$is_field = preg_match_all( '/{field:(\w*)}/', $text, $matches_field );
$is_value = preg_match_all( '/{value:(\w*)}/', $text, $matches_value );
$is_product = preg_match_all( '/{product:(\w*)}/', $text, $matches_product );

if ( $is_field ) {
$meta_keys = $matches_field[1];
// Create an array of meta values to replace.
$meta_values = array();
foreach ( $meta_keys as $meta_key ) {
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
// Add values to the array.
array_push( $meta_values, $meta_value );
// implode $modified_value before adding it to the $modified_values array
if ( is_array( $meta_value ) ) {
$meta_value = implode( WeForms::$field_separator, $meta_value );
}
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
// Add values to the array.
array_push( $meta_values, $meta_value );
}
// $text may include HTML tags, only replace tag that was matched. Replace all matches.
$text = str_replace( $matches_field[0], $meta_values, $text );
Expand All @@ -545,15 +547,40 @@ public static function replace_field_tags( $text, $entry_id = null ) {
$form_field_values = WeForms_Form_Entry::get_form( $entry_id )->get_field_values()[ $meta_key ]['options'];
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
$modified_value = array_search( $meta_value, $form_field_values );
// implode $modified_value before adding it to the $modified_values array
if ( is_array( $modified_value ) ) {
$modified_value = implode( WeForms::$field_separator, $modified_value );
}
// Add values to the array.
array_push( $modified_values, $modified_value );
}
// $text may include HTML tags, only replace tag that was matched.
$text = str_replace( $matches_value[0], $modified_values, $text );
}
if( $is_product ) {
$meta_keys = $matches_product[1];
// Create an array of modified values to replace.
$modified_values = array();
foreach ( $meta_keys as $meta_key ) {
$form_field_values = WeForms_Form_Entry::get_form( $entry_id )->get_field_values()[ $meta_key ]['options'];
$meta_value = weforms_get_entry_meta( $entry_id, $meta_key, true );
// muli-products are an array of arrays so parse each value
$modified_value = array();
foreach($meta_value as $product){
array_push( $modified_value, array_keys($form_field_values)[array_search( $product['product'], array_keys($form_field_values) )] );
}
// implode $modified_value before adding it to the $modified_values array
if ( is_array( $modified_value ) ) {
$modified_value = implode( WeForms::$field_separator, $modified_value );
}
// Add values to the array.
array_push( $modified_values, $modified_value );
}
// $text may include HTML tags, only replace tag that was matched.
$text = str_replace( $matches_value[0], $modified_values, $text );
$text = str_replace( $matches_product[0], $modified_values, $text );
}
// Add filter for custom templating snippets and field logic
$text = apply_filters('weforms_notification_replace_field_tags', $text, $entry_id);
return $text;
}

Expand Down