diff --git a/admin/tool/usertours/classes/step.php b/admin/tool/usertours/classes/step.php index 3bfa5f2b16e..27c6517822e 100644 --- a/admin/tool/usertours/classes/step.php +++ b/admin/tool/usertours/classes/step.php @@ -813,21 +813,23 @@ class step { * @return string Processed tour content */ public static function get_step_image_from_input(string $content): string { - global $OUTPUT; - - if (preg_match('/(?<=@@PIXICON::).*?(?=@@)/', $content, $matches)) { - $bits = explode('::', $matches[0]); - $identifier = $bits[0]; - $component = $bits[1]; - if ($component == 'moodle') { - $component = 'core'; - } - $image = \html_writer::img($OUTPUT->image_url($identifier, $component)->out(false), - '', ['class' => 'img-fluid']); - $contenttoreplace = '@@PIXICON::' . $matches[0] . '@@'; - $content = str_replace($contenttoreplace, $image, $content); + if (strpos($content, '@@PIXICON') === false) { + return $content; } + $content = preg_replace_callback('%@@PIXICON::(?P([^::]*))::(?P([^@@]*))@@%', + function(array $matches) { + global $OUTPUT; + $component = $matches['component']; + if ($component == 'moodle') { + $component = 'core'; + } + return \html_writer::img($OUTPUT->image_url($matches['identifier'], $component)->out(false), '', + ['class' => 'img-fluid']); + }, + $content + ); + return $content; } } diff --git a/admin/tool/usertours/tests/step_test.php b/admin/tool/usertours/tests/step_test.php index d6c78d0dea5..6d4512828c1 100644 --- a/admin/tool/usertours/tests/step_test.php +++ b/admin/tool/usertours/tests/step_test.php @@ -828,6 +828,7 @@ class step_testcase extends advanced_testcase { * Ensure that the get_step_image_from_input function replace PIXICON placeholder with the correct images correctly. */ public function test_get_step_image_from_input() { + // Test step content with single image. $stepcontent = '@@PIXICON::tour/tour_mycourses::tool_usertours@@
Test'; $stepcontent = \tool_usertours\step::get_step_image_from_input($stepcontent); @@ -836,6 +837,16 @@ class step_testcase extends advanced_testcase { $this->assertStringEndsWith('Test', $stepcontent); $this->assertStringNotContainsString('PIXICON', $stepcontent); + // Test step content with multiple images. + $stepcontent = '@@PIXICON::tour/tour_mycourses::tool_usertours@@
Test
@@PIXICON::tour/tour_myhomepage::tool_usertours@@'; + $stepcontent = \tool_usertours\step::get_step_image_from_input($stepcontent); + // If the format is correct, PIXICON placeholder will be replaced with the img tag. + $this->assertStringStartsWith('assertEquals(2, substr_count($stepcontent, 'assertStringNotContainsString('PIXICON', $stepcontent); + + // Test step content with incorrect format. $stepcontent = '@@PIXICON::tour/tour_mycourses
Test'; $stepcontent = \tool_usertours\step::get_step_image_from_input($stepcontent);