Skip to content

Commit

Permalink
MDL-76867 editor_tiny: Add plugin management page
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Mar 14, 2023
1 parent 51cfde0 commit e2de093
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 18 deletions.
24 changes: 11 additions & 13 deletions lib/editor/tiny/classes/manager.php
Expand Up @@ -41,17 +41,17 @@ public function get_plugin_configuration(
array $fpoptions = [],
?editor $editor = null
): array {
$disabledplugins = $this->get_disabled_plugins();

// Get the list of plugins.
// Note: Disabled plugins are already removed from this list.
$plugins = $this->get_shipped_plugins();

// Fetch configuration for Moodle plugins.
$moodleplugins = \core_component::get_plugin_list_with_class('tiny', 'plugininfo');
$enabledplugins = \editor_tiny\plugininfo\tiny::get_enabled_plugins();
foreach ($moodleplugins as $plugin => $classname) {
if (in_array($plugin, $disabledplugins) || in_array("{$plugin}/plugin", $disabledplugins)) {
// Skip getting data for disabled plugins.
[, $pluginname] = explode('_', $plugin, 2);
if (!in_array($pluginname, $enabledplugins)) {
// This plugin has been disabled.
continue;
}

Expand All @@ -75,10 +75,6 @@ public function get_plugin_configuration(
$editor
);

if (!empty($config)) {
$plugininfo['config'] = $config;
}

// We suffix the plugin name for Moodle plugins with /plugin to avoid conflicts with Tiny plugins.
$plugins["{$plugin}/plugin"] = $plugininfo;
}
Expand Down Expand Up @@ -190,7 +186,7 @@ protected function get_available_plugins(): array {
$plugins = $this->get_shipped_plugins();
$plugins += $this->get_moodle_plugins();

$disabledplugins = $this->get_disabled_plugins();
$disabledplugins = $this->get_disabled_tinymce_plugins();
$plugins = array_filter($plugins, function ($plugin) use ($disabledplugins) {
return !in_array($plugin, $disabledplugins);
}, ARRAY_FILTER_USE_KEY);
Expand All @@ -216,8 +212,8 @@ protected function get_shipped_plugins(): array {
$plugins += $this->get_premium_plugins();
}

$disabledplugins = $this->get_disabled_plugins();
return array_filter($plugins, function($plugin) use ($disabledplugins) {
$disabledplugins = $this->get_disabled_tinymce_plugins();
return array_filter($plugins, function ($plugin) use ($disabledplugins) {
return !in_array($plugin, $disabledplugins);
}, ARRAY_FILTER_USE_KEY);
}
Expand Down Expand Up @@ -480,11 +476,13 @@ protected function get_tinymce_plugins(): array {
}

/**
* Get a list of the disabled plugins.
* Get a list of the built-in TinyMCE plugins which we want to disable.
*
* These are usually disabled because we have replaced them, or they are not compatible with Moodle in some way.
*
* @return string[]
*/
protected function get_disabled_plugins(): array {
protected function get_disabled_tinymce_plugins(): array {
return [
// Disable the image and media plugins.
// These are not generally compatible with Moodle.
Expand Down
42 changes: 42 additions & 0 deletions lib/editor/tiny/classes/table/plugin_management_table.php
@@ -0,0 +1,42 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace editor_tiny\table;

use moodle_url;

/**
* Tiny admin settings.
*
* @package editor_tiny
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class plugin_management_table extends \core_admin\table\plugin_management_table {
protected function get_plugintype(): string {
return 'tiny';
}

public function guess_base_url(): void {
$this->define_baseurl(
new moodle_url('/admin/settings.php', ['section' => 'editorsettingstiny'])
);
}

protected function get_action_url(array $params = []): moodle_url {
return new moodle_url('/lib/editor/tiny/subplugins.php', $params);
}
}
2 changes: 2 additions & 0 deletions lib/editor/tiny/lang/en/editor_tiny.php
Expand Up @@ -28,6 +28,8 @@
$string['privacy:reason'] = 'The TinyMCE editor does not store any preferences or user data.';
$string['branding'] = 'TinyMCE branding';
$string['branding_desc'] = 'Support TinyMCE by displaying the logo in the bottom corner of the text editor. The logo links to the TinyMCE website.';
$string['plugin_enabled'] = 'The {$a} plugin has been enabled.';
$string['plugin_disabled'] = 'The {$a} plugin has been disabled.';
$string['tiny:hash'] = '#';
$string['tiny:accessibility'] = 'Accessibility';
$string['tiny:action'] = 'Action';
Expand Down
15 changes: 11 additions & 4 deletions lib/editor/tiny/settings.php
Expand Up @@ -27,6 +27,13 @@
$ADMIN->add('editorsettings', new admin_category('editortiny', $editor->displayname, $editor->is_enabled() === false));

$settings = new admin_settingpage('editorsettingstiny', new lang_string('settings', 'editor_tiny'));
$settings->add(new \core_admin\admin\admin_setting_plugin_manager(
'tiny',
\editor_tiny\table\plugin_management_table::class,
'editor_tiny_settings',
get_string('editorsettings', 'editor'),
));

if ($ADMIN->fulltree) {
$setting = new admin_setting_configcheckbox(
'editor_tiny/branding',
Expand All @@ -38,15 +45,15 @@
$settings->add($setting);
}

// Note: We add editortiny to the settings page here manually rather than deferring to the plugininfo class.
// This ensures that it shows in the category list too.
$ADMIN->add('editortiny', $settings);

foreach (core_plugin_manager::instance()->get_plugins_of_type('tiny') as $plugin) {
/** @var \editor_tiny\plugininfo\tiny $plugin */
$plugin->load_settings($ADMIN, 'editortiny', $hassiteconfig);
}

// Note: We add editortiny to the settings page here manually rather than deferring to the plugininfo class.
// This ensures that it shows in the category list too.
$ADMIN->add('editortiny', $settings);

// Required or the editor plugininfo will add this section twice.
unset($settings);
$settings = null;
63 changes: 63 additions & 0 deletions lib/editor/tiny/subplugins.php
@@ -0,0 +1,63 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tiny subplugin management.
*
* @package editor_tinymce
* @copyright 2023 Andrew Lyons <andrew@nicols.co.uk>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

require(__DIR__ . '/../../../config.php');
require_once("{$CFG->libdir}/adminlib.php");

$action = optional_param('action', '', PARAM_ALPHA);
$plugin = optional_param('plugin', '', PARAM_PLUGIN);

$PAGE->set_context(context_system::instance());
$PAGE->set_url('/lib/editor/tinymce/subplugins.php');

require_login();
require_capability('moodle/site:config', context_system::instance());
require_sesskey();

$tinymanager = \core_plugin_manager::resolve_plugininfo_class('tiny');
$pluginname = get_string('pluginname', "tiny_{$plugin}");

switch ($action) {
case 'disable':
if ($tinymanager::enable_plugin($plugin, 0)) {
\core\notification::add(
get_string('plugin_disabled', 'editor_tiny', $pluginname),
\core\notification::SUCCESS
);
}
break;
case 'enable':
if ($tinymanager::enable_plugin($plugin, 1)) {
\core\notification::add(
get_string('plugin_enabled', 'editor_tiny', $pluginname),
\core\notification::SUCCESS
);
}
break;
default:
}

redirect(new moodle_url('/admin/settings.php', [
'section' => 'editorsettingstiny',
]));
30 changes: 30 additions & 0 deletions lib/editor/tiny/tests/behat/manage_subplugins.feature
@@ -0,0 +1,30 @@
@core @core_admin
Feature: An administrator can manage TinyMCE subplugins
In order to alter the user experience
As an admin
I can manage TinyMCE subplugins

@javascript
Scenario: An administrator can control the enabled state of TinyMCE subplugins using JavaScript
Given I am logged in as "admin"
And I navigate to "Plugins > Text editors > TinyMCE > General settings" in site administration
When I click on "Disable the Tiny equation editor plugin" "link"
Then I should see "The Tiny equation editor plugin has been disabled"
And "Disable the Tiny equation editor plugin" "link" should not exist
But "Enable the Tiny equation editor plugin" "link" should exist
When I click on "Enable the Tiny equation editor plugin" "link"
Then I should see "The Tiny equation editor plugin has been enabled"
And "Enable the Tiny equation editor plugin" "link" should not exist
But "Disable the Tiny equation editor plugin" "link" should exist

Scenario: An administrator can control the enabled state of TinyMCE subplugins without JavaScript
Given I am logged in as "admin"
And I navigate to "Plugins > Text editors > TinyMCE > General settings" in site administration
When I click on "Disable the Tiny equation editor plugin" "link"
Then I should see "The Tiny equation editor plugin has been disabled"
And "Disable the Tiny equation editor plugin" "link" should not exist
But "Enable the Tiny equation editor plugin" "link" should exist
When I click on "Enable the Tiny equation editor plugin" "link"
Then I should see "The Tiny equation editor plugin has been enabled"
And "Enable the Tiny equation editor plugin" "link" should not exist
But "Disable the Tiny equation editor plugin" "link" should exist
2 changes: 1 addition & 1 deletion lib/editor/tiny/version.php
Expand Up @@ -24,6 +24,6 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2022112800; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023021301; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2022111800;
$plugin->component = 'editor_tiny'; // Full name of the plugin (used for diagnostics).

0 comments on commit e2de093

Please sign in to comment.