1 |
efrain |
1 |
<?php
|
|
|
2 |
// This file is part of Moodle - http://moodle.org/
|
|
|
3 |
//
|
|
|
4 |
// Moodle is free software: you can redistribute it and/or modify
|
|
|
5 |
// it under the terms of the GNU General Public License as published by
|
|
|
6 |
// the Free Software Foundation, either version 3 of the License, or
|
|
|
7 |
// (at your option) any later version.
|
|
|
8 |
//
|
|
|
9 |
// Moodle is distributed in the hope that it will be useful,
|
|
|
10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
12 |
// GNU General Public License for more details.
|
|
|
13 |
//
|
|
|
14 |
// You should have received a copy of the GNU General Public License
|
|
|
15 |
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
16 |
|
|
|
17 |
namespace tiny_premium;
|
|
|
18 |
|
|
|
19 |
/**
|
|
|
20 |
* Tiny Premium manager.
|
|
|
21 |
*
|
|
|
22 |
* @package tiny_premium
|
|
|
23 |
* @copyright 2024 David Woloszyn <david.woloszyn@moodle.com>
|
|
|
24 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
25 |
*/
|
|
|
26 |
class manager {
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* Get all Tiny Premium plugins currently supported.
|
|
|
30 |
*
|
|
|
31 |
* The plugin identifiers are taken from Tiny Cloud (https://www.tiny.cloud/docs/tinymce/6/plugins/#premium-plugins).
|
|
|
32 |
*
|
|
|
33 |
* @return array The array of plugins.
|
|
|
34 |
*/
|
|
|
35 |
public static function get_plugins(): array {
|
|
|
36 |
return [
|
|
|
37 |
'advtable',
|
|
|
38 |
'typography',
|
|
|
39 |
'casechange',
|
|
|
40 |
'checklist',
|
|
|
41 |
'editimage',
|
|
|
42 |
'export',
|
|
|
43 |
'footnotes',
|
|
|
44 |
'formatpainter',
|
|
|
45 |
'linkchecker',
|
|
|
46 |
'pageembed',
|
|
|
47 |
'permanentpen',
|
|
|
48 |
'powerpaste',
|
|
|
49 |
'tinymcespellchecker',
|
|
|
50 |
'autocorrect',
|
|
|
51 |
'tableofcontents',
|
|
|
52 |
];
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* Get enabled Tiny Premium plugins.
|
|
|
57 |
*
|
|
|
58 |
* @return array The array of enabled plugins.
|
|
|
59 |
*/
|
|
|
60 |
public static function get_enabled_plugins(): array {
|
|
|
61 |
$plugins = self::get_plugins();
|
|
|
62 |
$enabledplugins = [];
|
|
|
63 |
foreach ($plugins as $plugin) {
|
|
|
64 |
if (self::is_plugin_enabled($plugin)) {
|
|
|
65 |
$enabledplugins[] = $plugin;
|
|
|
66 |
}
|
|
|
67 |
}
|
|
|
68 |
return $enabledplugins;
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
/**
|
|
|
72 |
* Check if a Tiny Premium plugin is enabled in config.
|
|
|
73 |
*
|
|
|
74 |
* @param string $plugin The plugin to check.
|
|
|
75 |
* @return bool Return true if enabled.
|
|
|
76 |
*/
|
|
|
77 |
public static function is_plugin_enabled(string $plugin): bool {
|
|
|
78 |
$config = get_config('tiny_premium_' . $plugin, 'enabled');
|
|
|
79 |
return ($config == 1);
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* Set a new value for a Tiny Premium plugin config.
|
|
|
84 |
*
|
|
|
85 |
* @param array $data The data to set.
|
|
|
86 |
* @param string $plugin The plugin to use.
|
|
|
87 |
*/
|
|
|
88 |
public static function set_plugin_config(array $data, string $plugin): void {
|
|
|
89 |
// Check this is a valid premium plugin.
|
|
|
90 |
if (!in_array($plugin, self::get_plugins())) {
|
|
|
91 |
return;
|
|
|
92 |
}
|
|
|
93 |
|
|
|
94 |
$plugin = 'tiny_premium_' . $plugin;
|
|
|
95 |
|
|
|
96 |
foreach ($data as $key => $newvalue) {
|
|
|
97 |
// Get the old value for the log.
|
|
|
98 |
$oldvalue = get_config($plugin, $key) ?? null;
|
|
|
99 |
add_to_config_log($key, $oldvalue, $newvalue, $plugin);
|
|
|
100 |
|
|
|
101 |
// If we are disabling the plugin, remove it, otherwise, set the new value.
|
|
|
102 |
if ($key === 'enabled' && $newvalue == 0) {
|
|
|
103 |
unset_config($key, $plugin);
|
|
|
104 |
} else {
|
|
|
105 |
set_config($key, $newvalue, $plugin);
|
|
|
106 |
}
|
|
|
107 |
}
|
|
|
108 |
}
|
|
|
109 |
}
|