Skip to content

Commit 8268182

Browse files
committed
Add kill-switch for built-in snippets
1 parent 8604122 commit 8268182

File tree

4 files changed

+80
-11
lines changed

4 files changed

+80
-11
lines changed

classes/snippets.php

+42-7
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ class snippets {
7070
'gif',
7171
];
7272

73+
/**
74+
* Constant for identifying builtin snippets.
75+
*
76+
* @var string
77+
*/
78+
const SOURCE_BUILTIN = 'theme_boost_union';
79+
7380
/**
7481
* Gets the snippet file based on the meta information.
7582
*
@@ -83,7 +90,7 @@ public static function get_snippet_file($path, $source): string|null {
8390

8491
// Get the snippet file based on the different sources.
8592
// Builtin CSS Snippets.
86-
if ($source === 'theme_boost_union') {
93+
if ($source === self::SOURCE_BUILTIN) {
8794
// Compose the file path.
8895
$file = $CFG->dirroot . self::BUILTIN_SNIPPETS_BASE_PATH . $path;
8996

@@ -151,7 +158,7 @@ public static function get_snippet_preview_url($path, $source): string|null {
151158

152159
// Get the snippet file based on the different sources.
153160
// Builtin CSS Snippets.
154-
if ($source === 'theme_boost_union') {
161+
if ($source === self::SOURCE_BUILTIN) {
155162
$url = self::get_builtin_snippet_preview_url($path, $source);
156163
// Other snippet sources (which are currently not supported).
157164
} else {
@@ -264,14 +271,24 @@ public static function compose_snippets_data($snippetrecordset): array {
264271
public static function get_enabled_snippet_scss(): string {
265272
global $DB;
266273

274+
// Prepare WHERE clause based on the enabled sources.
275+
$whereparts = self::get_enabled_sources();
276+
if (count($whereparts) == 0) {
277+
// If no sources are enabled, we do not want to use any snippets.
278+
// We do this by restricting the query to an impossible source.
279+
$whereparts[] = 'impossible';
280+
}
281+
list($insql, $inparams) = $DB->get_in_or_equal($whereparts, SQL_PARAMS_NAMED);
282+
267283
// Compose SQL base query.
268284
$sql = "SELECT *
269285
FROM {theme_boost_union_snippets} s
270-
WHERE enabled = '1'
271-
ORDER BY sortorder";
286+
WHERE enabled = '1' AND source ".$insql.
287+
" ORDER BY sortorder";
288+
$sqlparams = $inparams;
272289

273290
// Get records.
274-
$data = $DB->get_recordset_sql($sql);
291+
$data = $DB->get_recordset_sql($sql, $sqlparams);
275292

276293
// Initialize SCSS code.
277294
$scss = '';
@@ -368,6 +385,24 @@ private static function get_builtin_snippet_paths(): array {
368385
}
369386
}
370387

388+
/**
389+
* Get the enabled snippet sources.
390+
*
391+
* @return array
392+
*/
393+
public static function get_enabled_sources(): array {
394+
// Initialize sources array.
395+
$sources = [];
396+
397+
// If builtin snippets are enabled.
398+
if (get_config('theme_boost_union', 'enablebuiltinsnippets') == THEME_BOOST_UNION_SETTING_SELECT_YES) {
399+
$sources[] = self::SOURCE_BUILTIN;
400+
}
401+
402+
// Return.
403+
return $sources;
404+
}
405+
371406
/**
372407
* Make sure that the builtin snippets are in the database.
373408
*
@@ -382,7 +417,7 @@ public static function add_builtin_snippets(): void {
382417
// Get builtin snippets which are known in the database.
383418
$snippets = $DB->get_records(
384419
'theme_boost_union_snippets',
385-
['source' => 'theme_boost_union'],
420+
['source' => self::SOURCE_BUILTIN],
386421
'sortorder DESC',
387422
'id,path,sortorder'
388423
);
@@ -406,7 +441,7 @@ public static function add_builtin_snippets(): void {
406441
'theme_boost_union_snippets',
407442
[
408443
'path' => $path,
409-
'source' => 'theme_boost_union',
444+
'source' => self::SOURCE_BUILTIN,
410445
'sortorder' => $sortorder + 1,
411446
]
412447
);

classes/table/snippets_overview.php

+13-2
Original file line numberDiff line numberDiff line change
@@ -272,13 +272,24 @@ public function col_actions($data) {
272272
public function query_db($pagesize, $useinitialsbar = true) {
273273
global $DB;
274274

275+
// Prepare WHERE clause based on the enabled sources.
276+
$whereparts = snippets::get_enabled_sources();
277+
if (count($whereparts) == 0) {
278+
// If no sources are enabled, we do not want to show any snippets.
279+
// We do this by restricting the query to an impossible source.
280+
$whereparts[] = 'impossible';
281+
}
282+
list($insql, $inparams) = $DB->get_in_or_equal($whereparts, SQL_PARAMS_NAMED);
283+
275284
// Compose SQL base query.
276285
$sql = 'SELECT *
277286
FROM {theme_boost_union_snippets} s
278-
ORDER BY sortorder';
287+
WHERE source '.$insql.
288+
' ORDER BY sortorder';
289+
$sqlparams = $inparams;
279290

280291
// Get the raw records.
281-
$data = $DB->get_recordset_sql($sql);
292+
$data = $DB->get_recordset_sql($sql, $sqlparams);
282293

283294
// Compose the complete snippets data and add set it as raw table data.
284295
$this->rawdata = snippets::compose_snippets_data($data);

lang/en/theme_boost_union.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,8 @@
11061106

11071107
// Settings: CSS snippets page.
11081108
$string['configtitlesnippets'] = 'CSS snippets';
1109+
1110+
// Settings: Overview tab.
11091111
$string['snippetscreator'] = 'Creator';
11101112
$string['snippetsdescription'] = 'Description';
11111113
$string['snippetsdetails'] = 'Details';
@@ -1125,14 +1127,21 @@
11251127
$string['snippetsscopecourse'] = 'Course';
11261128
$string['snippetsscopedashboard'] = 'Dashboard';
11271129
$string['snippetsscopeglobal'] = 'Global';
1128-
$string['snippetssettings'] = 'Settings';
11291130
$string['snippetsshowdetails'] = 'Show details';
11301131
$string['snippetssnippets'] = 'CSS snippets';
11311132
$string['snippetssource'] = 'Source';
11321133
$string['snippetssourcetheme_boost_union'] = 'Boost Union built-in';
11331134
$string['snippetstitle'] = 'Title';
11341135
$string['snippetsusagenote'] = 'Usage note';
11351136

1137+
// Settings: Settings tab.
1138+
$string['snippetssettings'] = 'Settings';
1139+
// ... Section: Built-in snippets.
1140+
$string['snippetsbuiltinsnippetsheading'] = 'Built-in snippets';
1141+
// ... ... Setting: Enable built-in snippets.
1142+
$string['enablebuiltinsnippets'] = 'Enable built-in snippets';
1143+
$string['enablebuiltinsnippets_desc'] = 'With this setting, you can enable or disable the built-in CSS snippets which are shipped with Boost Union. If you disable this setting, all built-in CSS snippets are ignored and never added to the SCSS stack.';
1144+
11361145
// Settings: Smart menus page.
11371146
$string['smartmenus'] = 'Smart menus';
11381147
$string['error:smartmenusmenuitemnotfound'] = 'Smart menu item not found';

settings.php

+15-1
Original file line numberDiff line numberDiff line change
@@ -2840,10 +2840,24 @@
28402840
$page->add($tab);
28412841

28422842

2843-
// Create general settings tab.
2843+
// Create CSS snippets settings tab.
28442844
$tab = new admin_settingpage('theme_boost_union_snippets_settings',
28452845
get_string('snippetssettings', 'theme_boost_union', null, true));
28462846

2847+
// Create built-in snippets heading.
2848+
$name = 'theme_boost_union/builtinsnippetsheading';
2849+
$title = get_string('snippetsbuiltinsnippetsheading', 'theme_boost_union', null, true);
2850+
$setting = new admin_setting_heading($name, $title, null);
2851+
$tab->add($setting);
2852+
2853+
// Setting: Enable built-in snippets.
2854+
$name = 'theme_boost_union/enablebuiltinsnippets';
2855+
$title = get_string('enablebuiltinsnippets', 'theme_boost_union', null, true);
2856+
$description = get_string('enablebuiltinsnippets_desc', 'theme_boost_union', null, true);
2857+
$setting = new admin_setting_configselect($name, $title, $description, THEME_BOOST_UNION_SETTING_SELECT_NO, $yesnooption);
2858+
$setting->set_updatedcallback('theme_reset_all_caches');
2859+
$tab->add($setting);
2860+
28472861
// Add tab to settings page.
28482862
$page->add($tab);
28492863

0 commit comments

Comments
 (0)