Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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  /**
  18   * Subplugin info class.
  19   *
  20   * @package   editor_tinymce
  21   * @copyright 2012 Petr Skoda {@link http://skodak.org}
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  namespace editor_tinymce\plugininfo;
  25  
  26  use core\plugininfo\base, moodle_url, part_of_admin_tree, admin_settingpage, core_component;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  
  31  class tinymce extends base {
  32      /**
  33       * Finds all enabled plugins, the result may include missing plugins.
  34       * @return array|null of enabled plugins $pluginname=>$pluginname, null means unknown
  35       */
  36      public static function get_enabled_plugins() {
  37          $disabledsubplugins = array();
  38          $config = get_config('editor_tinymce', 'disabledsubplugins');
  39          if ($config) {
  40              $config = explode(',', $config);
  41              foreach ($config as $sp) {
  42                  $sp = trim($sp);
  43                  if ($sp !== '') {
  44                      $disabledsubplugins[$sp] = $sp;
  45                  }
  46              }
  47          }
  48  
  49          $enabled = array();
  50          $installed = core_component::get_plugin_list('tinymce');
  51          foreach ($installed as $plugin => $fulldir) {
  52              if (isset($disabledsubplugins[$plugin])) {
  53                  continue;
  54              }
  55              $enabled[$plugin] = $plugin;
  56          }
  57  
  58          return $enabled;
  59      }
  60  
  61      public static function enable_plugin(string $pluginname, int $enabled): bool {
  62          $haschanged = false;
  63          $plugins = [];
  64          $oldvalue = get_config('editor_tinymce', 'disabledsubplugins');
  65          if (!empty($oldvalue)) {
  66              $plugins = array_flip(explode(',', $oldvalue));
  67          }
  68          // Only set visibility if it's different from the current value.
  69          if ($enabled && array_key_exists($pluginname, $plugins)) {
  70              unset($plugins[$pluginname]);
  71              $haschanged = true;
  72          } else if (!$enabled && !array_key_exists($pluginname, $plugins)) {
  73              $plugins[$pluginname] = $pluginname;
  74              $haschanged = true;
  75          }
  76  
  77          if ($haschanged) {
  78              $new = implode(',', array_flip($plugins));
  79              add_to_config_log('disabledsubplugins', $oldvalue, $new, 'editor_tinymce');
  80              set_config('disabledsubplugins', $new, 'editor_tinymce');
  81              // Reset caches.
  82              \core_plugin_manager::reset_caches();
  83          }
  84  
  85          return $haschanged;
  86      }
  87  
  88      public function is_uninstall_allowed() {
  89          return true;
  90      }
  91  
  92      /**
  93       * Return URL used for management of plugins of this type.
  94       * @return moodle_url
  95       */
  96      public static function get_manage_url() {
  97          return new moodle_url('/admin/settings.php', array('section'=>'editorsettingstinymce'));
  98      }
  99  
 100      public function get_settings_section_name() {
 101          return 'tinymce'.$this->name.'settings';
 102      }
 103  
 104      public function load_settings(part_of_admin_tree $adminroot, $parentnodename, $hassiteconfig) {
 105          global $CFG, $USER, $DB, $OUTPUT, $PAGE; // In case settings.php wants to refer to them.
 106          $ADMIN = $adminroot; // May be used in settings.php.
 107          $plugininfo = $this; // Also can be used inside settings.php.
 108  
 109          if (!$this->is_installed_and_upgraded()) {
 110              return;
 111          }
 112  
 113          if (!$hassiteconfig or !file_exists($this->full_path('settings.php'))) {
 114              return;
 115          }
 116  
 117          $section = $this->get_settings_section_name();
 118          $settings = new admin_settingpage($section, $this->displayname, 'moodle/site:config', $this->is_enabled() === false);
 119          include($this->full_path('settings.php')); // This may also set $settings to null.
 120  
 121          if ($settings) {
 122              $ADMIN->add($parentnodename, $settings);
 123          }
 124      }
 125  }