Skip to content

Commit 4761b0f

Browse files
committed
snippet preview: allow for multiple (modern) image formats
1 parent 1ea947c commit 4761b0f

4 files changed

+76
-18
lines changed

classes/snippets.php

+60-18
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ class snippets {
5353
*/
5454
const BUILTIN_SNIPPETS_BASE_PATH = '/theme/boost_union/snippets/builtin/';
5555

56+
/**
57+
* Allowed file extensions for the visual preview of the SCSS snippet in the more detail modal.
58+
*
59+
* The order in this array also reflects their priority if multiple matches should exist.
60+
*
61+
* @var array
62+
*/
63+
const ALLOWED_PREVIEW_FILE_EXTENSIONS = [
64+
'webp',
65+
'png',
66+
'jpg',
67+
'jpeg',
68+
'gif',
69+
];
70+
5671
/**
5772
* Gets the snippet file based on the meta information.
5873
*
@@ -78,40 +93,67 @@ public static function get_snippet_file($path, $source): string|null {
7893
return is_readable($file) ? $file : null;
7994
}
8095

96+
/**
97+
* Get the preview images URL for a builtin snippet.
98+
*
99+
* The preview file has currently the same path but a different file extension.
100+
*
101+
* @param string $path The snippet's path.
102+
* @param string $source The snippet's source.
103+
* @return string|null The URL of the preview image.
104+
*/
105+
public static function get_builtin_snippet_preview_url($path, $source) {
106+
global $CFG;
107+
108+
// Replace the .scss suffix with a .png suffix in the path.
109+
$search = '.scss';
110+
$pos = strrpos($path, $search);
111+
if ($pos !== false) {
112+
// Compose the file pattern that searched for files with the same basename and the supported extensions.
113+
$pattern = $CFG->dirroot .
114+
self::BUILTIN_SNIPPETS_BASE_PATH .
115+
substr_replace(
116+
$path,
117+
'.{' . implode(',', self::ALLOWED_PREVIEW_FILE_EXTENSIONS) . '}',
118+
$pos,
119+
strlen($search)
120+
);
121+
// Search for the preview file.
122+
$files = glob($pattern, GLOB_BRACE);
123+
// Select the first match of the found preview file(s).
124+
if (!empty($files)) {
125+
$file = $files[0];
126+
// Compose the files URL.
127+
$url = new \moodle_url(substr($file, strlen($CFG->dirroot)));
128+
return is_readable($file) ? $url : null;
129+
}
130+
}
131+
// If anything wen't wrong return null, just as if no snippet preview is present.
132+
return null;
133+
}
134+
81135
/**
82136
* Gets the snippet's preview file URL.
83137
*
84138
* @param string $path The snippet's path.
85139
* @param string $source The snippet's source.
86140
*
87-
* @return string|null
141+
* @return string|null The URL of the snippets preview image.
88142
*/
89143
public static function get_snippet_preview_url($path, $source): string|null {
90144
global $CFG;
91145

146+
$url = null;
147+
92148
// Get the snippet file based on the different sources.
93149
// Builtin CSS Snippets.
94150
if ($source === 'theme_boost_union') {
95-
// Replace the .scss suffix with a .png suffix in the path.
96-
$search = '.scss';
97-
$pos = strrpos($path, $search);
98-
if ($pos !== false) {
99-
$path = substr_replace($path, '.png', $pos, strlen($search));
100-
} else {
101-
// In this case the .scss suffix was not found, so anything is broken.
102-
return null;
103-
}
104-
105-
// Compose the file path and URL.
106-
$file = $CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . $path;
107-
$url = new \moodle_url(self::BUILTIN_SNIPPETS_BASE_PATH . $path);
108-
151+
$url = self::get_builtin_snippet_preview_url($path, $source);
109152
// Other snippet sources (which are currently not supported).
110153
} else {
111-
return null;
154+
return $url;
112155
}
113-
114-
return is_readable($file) ? $url : null;
156+
return $url;
115157
}
116158

117159
/**
Binary file not shown.
Binary file not shown.

tests/snippets_test.php

+16
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,20 @@ public function test_fetch_enabled_snippets_scss(): void {
146146
$this->assertNotEquals('', $scss);
147147
$this->assertStringContainsString('Snippet Title: Visual depth', $scss);
148148
}
149+
150+
/**
151+
* Test looking for visual preview image file of snippet.
152+
153+
* @covers ::get_snippet_preview_url
154+
*
155+
* @return void
156+
*/
157+
public function test_lookup_visual_preview_file(): void {
158+
global $CFG;
159+
160+
$file = snippets::get_snippet_preview_url('international_day_against_homophobia.scss', 'theme_boost_union');
161+
162+
// Check that indeed the present webp preview for this snippet is returned.
163+
$this->assertEquals($CFG->wwwroot . '/theme/boost_union/snippets/builtin/international_day_against_homophobia.webp', $file);
164+
}
149165
}

0 commit comments

Comments
 (0)