@@ -53,6 +53,21 @@ class snippets {
53
53
*/
54
54
const BUILTIN_SNIPPETS_BASE_PATH = '/theme/boost_union/snippets/builtin/ ' ;
55
55
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
+
56
71
/**
57
72
* Gets the snippet file based on the meta information.
58
73
*
@@ -78,40 +93,67 @@ public static function get_snippet_file($path, $source): string|null {
78
93
return is_readable ($ file ) ? $ file : null ;
79
94
}
80
95
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
+
81
135
/**
82
136
* Gets the snippet's preview file URL.
83
137
*
84
138
* @param string $path The snippet's path.
85
139
* @param string $source The snippet's source.
86
140
*
87
- * @return string|null
141
+ * @return string|null The URL of the snippets preview image.
88
142
*/
89
143
public static function get_snippet_preview_url ($ path , $ source ): string |null {
90
144
global $ CFG ;
91
145
146
+ $ url = null ;
147
+
92
148
// Get the snippet file based on the different sources.
93
149
// Builtin CSS Snippets.
94
150
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 );
109
152
// Other snippet sources (which are currently not supported).
110
153
} else {
111
- return null ;
154
+ return $ url ;
112
155
}
113
-
114
- return is_readable ($ file ) ? $ url : null ;
156
+ return $ url ;
115
157
}
116
158
117
159
/**
0 commit comments