diff --git a/src/CustomForm.php b/src/CustomForm.php index 50c5eb7..5ef1113 100644 --- a/src/CustomForm.php +++ b/src/CustomForm.php @@ -124,20 +124,7 @@ function CustomForm() } // Add this fields value to the list of variables for the output post. - $vars[] = '/\{' . $field['title'] . '\}/'; - $replace[] = str_replace('$', '\$', $value); - - // {{ }} Syntax: Setup REGEX for removing entire {{ }} string or just stripping the outermost { }, depending upon the replacement value being blank or not - if ($value == '') - { - $vars_blank[] = '/\{[^\{\}]*\{' . $field['title'] . '\}[^\{\}]*\}/'; - $vars_non_blank[] = '//'; - } - else - { - $vars_blank[] = '//'; - $vars_non_blank[] = '/\{[^\{\}]*\{' . $field['title'] . '\}[^\{\}]*\}/'; - } + $vars[$field['title']] = $value; } // Check whether the visual verification code was entered correctly. @@ -163,39 +150,11 @@ function CustomForm() if (!$fail_submit) { require_once($sourcedir . '/Subs-Post.php'); - - // {{ }} Syntax: Strip out everything in {{ }} if value is blank - $output = preg_replace($vars_blank, '', $output); - $subject = preg_replace($vars_blank, '', $subject); - - // {{ }} Syntax: Remove outside brackets if value is not blank - $output = preg_replace_callback( - $vars_non_blank, - function ($matches) - { - return substr($matches[0],1,-1); - }, - $output - ); - $subject = preg_replace_callback( - $vars_non_blank, - function ($matches) - { - return substr($matches[0],1,-1); - }, - $subject - ); - - // Replace all vars with their correct value, for both the message and the subject. - $output = preg_replace($vars, $replace, $output); - $subject = preg_replace($vars, $replace, $subject); - - // Collect all necessary parameters for the creation of the post. $msgOptions = array( 'id' => 0, - 'subject' => $subject, + 'subject' => customform_replace_vars($subject, $vars), 'icon' => $icon, - 'body' => $output, + 'body' => customform_replace_vars($output, $vars), 'smileys_enabled' => true, ); diff --git a/src/Subs-CustomForm.php b/src/Subs-CustomForm.php index 6b4a6e5..ed18097 100644 --- a/src/Subs-CustomForm.php +++ b/src/Subs-CustomForm.php @@ -33,4 +33,14 @@ function customform_list_classes() foreach (get_declared_classes() as $class) if (is_subclass_of($class, 'CustomFormBase')) yield trim(strpbrk($class, '_'), '_'); +} + +function customform_replace_vars(string $text, array $array): string +{ + return preg_replace_callback('~{{1,2}\s*?([a-zA-Z0-9\-_\.]+)\s*?}{1,2}~', + function($matches) use ($array) + { + return $array[$matches[1]] ?? $matches[1]; + }, + $text); } \ No newline at end of file diff --git a/tests/Test.php b/tests/Test.php index a634db4..4718112 100644 --- a/tests/Test.php +++ b/tests/Test.php @@ -6,6 +6,49 @@ final class Test extends TestCase { + /** + * @dataProvider replaceProvider + */ + public function testReplace(string $test, string $expected): void + { + $result = customform_replace_vars($test, []); + $this->assertSame($expected, $result); + } + + /** + * @return string[][] + */ + public function replaceProvider(): array + { + return [ + ['abc{{def}}ghi', 'abcdefghi'], + ['abc{{def }}ghi', 'abcdefghi'], + ['abc{{def }}ghi', 'abcdefghi'], + ['abc{{ def}}ghi', 'abcdefghi'], + ['abc{{ def}}ghi', 'abcdefghi'], + ['abc{{ def }}ghi', 'abcdefghi'], + ['abc{{ def }}ghi', 'abcdefghi'], + ['abc{def}ghi', 'abcdefghi'], + ['abc{def }ghi', 'abcdefghi'], + ['abc{def }ghi', 'abcdefghi'], + ['abc{ def}ghi', 'abcdefghi'], + ['abc{ def}ghi', 'abcdefghi'], + ['abc{ def }ghi', 'abcdefghi'], + ['abc{ def }ghi', 'abcdefghi'] + ]; + } + + public function testReplaceVar(): void + { + $this->assertSame( + 'Hello world', + customform_replace_vars( + 'Hello {{ term }}', + ['term' => 'world'] + ) + ); + } + public function testFindClasses(): void { $classes = iterator_to_array(customform_list_classes());