| Total Complexity | 53 |
| Total Lines | 351 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 0 | Features | 0 |
Complex classes like TinyMCE often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use TinyMCE, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class TinyMCE |
||
| 28 | { |
||
| 29 | public $rootpath; |
||
| 30 | public $config = []; |
||
| 31 | public $setting = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * PHP 5 Constructor |
||
| 35 | * |
||
| 36 | * @param string $config The configuration |
||
| 37 | **/ |
||
| 38 | |||
| 39 | public function __construct($config) |
||
| 40 | { |
||
| 41 | $this->setConfig($config); |
||
| 42 | $this->rootpath = $this->config['rootpath'] . '/tinymce/js/tinymce'; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Creates one instance of the tinyMCE object |
||
| 47 | * |
||
| 48 | * @param array $config The configuration |
||
| 49 | * @return object $instance The instance of tinyMCE object |
||
| 50 | **/ |
||
| 51 | |||
| 52 | public function &instance($config) |
||
| 53 | { |
||
| 54 | static $instance; |
||
| 55 | if(!isset($instance)) { |
||
| 56 | $instance = new TinyMCE($config); |
||
| 57 | }else{ |
||
| 58 | $instance->setConfig($config); |
||
| 59 | } |
||
| 60 | |||
| 61 | return $instance; |
||
| 62 | } |
||
| 63 | |||
| 64 | public function setConfig($config) |
||
| 65 | { |
||
| 66 | foreach ($config as $key => $val) { |
||
| 67 | $this->config[$key] = $val; |
||
| 68 | } |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Initializes the tinyMCE |
||
| 73 | * @return true |
||
| 74 | **/ |
||
| 75 | |||
| 76 | public function init() |
||
| 77 | { |
||
| 78 | // list of configured options |
||
| 79 | $configured = []; |
||
| 80 | $this->setting['selector'] = 'textarea'; |
||
| 81 | $this->setting['theme'] = 'modern'; |
||
| 82 | |||
| 83 | // Load default settings |
||
| 84 | if (!($this->setting = @include($GLOBALS['xoops']->path('var/configs/tinymce.php')))) { |
||
| 85 | $this->setting = include __DIR__ . '/settings.php'; |
||
| 86 | } |
||
| 87 | |||
| 88 | // get editor language (from ...) |
||
| 89 | if (is_readable(XOOPS_ROOT_PATH . $this->rootpath . '/langs/' . $this->config['language'] . '.js')) { |
||
| 90 | $this->setting['language'] = $this->config['language']; |
||
| 91 | $configured[] = 'language'; |
||
| 92 | } |
||
| 93 | |||
| 94 | $this->setting['content_css'] = implode(',', $this->loadCss()); |
||
| 95 | $configured[] = 'content_css'; |
||
| 96 | |||
| 97 | if (!empty($this->config['theme']) && is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->config['theme'])) { |
||
| 98 | $this->setting['theme'] = $this->config['theme']; |
||
| 99 | $configured[] = 'theme'; |
||
| 100 | } |
||
| 101 | |||
| 102 | if (!empty($this->config['mode'])) { |
||
| 103 | $this->setting['mode'] = $this->config['mode']; |
||
| 104 | $configured[] = 'mode'; |
||
| 105 | } |
||
| 106 | |||
| 107 | // load all plugins except the plugins in setting["exclude_plugins"] |
||
| 108 | $this->setting['plugins'] = implode(',', $this->loadPlugins()); |
||
| 109 | $configured[] = 'plugins'; |
||
| 110 | |||
| 111 | if ('simple' !== $this->setting['theme']) { |
||
| 112 | if (empty($this->config['buttons'])) { |
||
| 113 | $this->config['buttons'][] = [ |
||
| 114 | 'before' => '', |
||
| 115 | 'add' => '', |
||
| 116 | ]; |
||
| 117 | $this->config['buttons'][] = [ |
||
| 118 | 'before' => '', |
||
| 119 | 'add' => '', |
||
| 120 | ]; |
||
| 121 | $this->config['buttons'][] = [ |
||
| 122 | 'before' => '', |
||
| 123 | 'add' => '', |
||
| 124 | ]; |
||
| 125 | } |
||
| 126 | $i = 0; |
||
| 127 | foreach ($this->config['buttons'] as $button) { |
||
| 128 | ++$i; |
||
| 129 | if (isset($button['before'])) { |
||
| 130 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add_before"] = $button['before']; |
||
| 131 | } |
||
| 132 | if (isset($button['add'])) { |
||
| 133 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}_add"] = $button['add']; |
||
| 134 | } |
||
| 135 | if (isset($button[''])) { |
||
| 136 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = $button['']; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | $configured[] = 'buttons'; |
||
| 140 | |||
| 141 | if (isset($this->config['toolbar_location'])) { |
||
| 142 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = $this->config['toolbar_location']; |
||
| 143 | $configured[] = 'toolbar_location'; |
||
| 144 | } else { |
||
| 145 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_location'] = 'top'; |
||
| 146 | } |
||
| 147 | |||
| 148 | if (isset($this->config['toolbar_align'])) { |
||
| 149 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = $this->config['toolbar_align']; |
||
| 150 | $configured[] = 'toolbar_align'; |
||
| 151 | } else { |
||
| 152 | $this->setting['theme_' . $this->setting['theme'] . '_toolbar_align'] = 'left'; |
||
| 153 | } |
||
| 154 | |||
| 155 | if (isset($this->config['statusbar_location'])) { |
||
| 156 | $this->setting['theme_' . $this->setting['theme'] . '_statusbar_location'] = $this->config['statusbar_location']; |
||
| 157 | $configured[] = 'statusbar_location'; |
||
| 158 | } |
||
| 159 | |||
| 160 | if (isset($this->config['path_location'])) { |
||
| 161 | $this->setting['theme_' . $this->setting['theme'] . '_path_location'] = $this->config['path_location']; |
||
| 162 | $configured[] = 'path_location'; |
||
| 163 | } |
||
| 164 | |||
| 165 | if (isset($this->config['resize_horizontal'])) { |
||
| 166 | $this->setting['theme_' . $this->setting['theme'] . '_resize_horizontal'] = $this->config['resize_horizontal']; |
||
| 167 | $configured[] = 'resize_horizontal'; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (isset($this->config['resizing'])) { |
||
| 171 | $this->setting['theme_' . $this->setting['theme'] . '_resizing'] = $this->config['resizing']; |
||
| 172 | $configured[] = 'resizing'; |
||
| 173 | } |
||
| 174 | |||
| 175 | if (!empty($this->config['fonts'])) { |
||
| 176 | $this->setting['theme_' . $this->setting['theme'] . '_fonts'] = $this->config['fonts']; |
||
| 177 | $configured[] = 'fonts'; |
||
| 178 | } |
||
| 179 | |||
| 180 | for ($i=1 ; $i <= 4 ; ++$i ) { |
||
| 181 | $buttons = []; |
||
| 182 | if (isset($this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"])) { |
||
| 183 | $checklist = explode(',', $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"]); |
||
| 184 | foreach ( $checklist as $plugin ) { |
||
| 185 | if (false != strpos(strtolower($plugin), 'xoops')) { |
||
|
|
|||
| 186 | if ( in_array( $plugin, $this->xoopsPlugins ) ) { |
||
| 187 | $buttons[] = $plugin; |
||
| 188 | } |
||
| 189 | } else { |
||
| 190 | $buttons[] = $plugin; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | $this->setting['theme_' . $this->setting['theme'] . "_buttons{$i}"] = implode(',', $buttons); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | $configured = array_unique($configured); |
||
| 199 | foreach ($this->config as $key => $val) { |
||
| 200 | if (isset($this->setting[$key]) || in_array($key, $configured)) { |
||
| 201 | continue; |
||
| 202 | } |
||
| 203 | $this->setting[$key] = $val; |
||
| 204 | } |
||
| 205 | |||
| 206 | if (!is_dir(XOOPS_ROOT_PATH . $this->rootpath . '/themes/' . $this->setting['theme'] . '/docs/' . $this->setting['language'] . '/')) { |
||
| 207 | $this->setting['docs_language'] = 'en'; |
||
| 208 | } |
||
| 209 | |||
| 210 | unset($this->config, $configured); |
||
| 211 | |||
| 212 | return true; |
||
| 213 | } |
||
| 214 | |||
| 215 | // load all plugins execpt the plugins in setting["exclude_plugins"] |
||
| 216 | public function loadPlugins() |
||
| 233 | } |
||
| 234 | |||
| 235 | // return all xoops plugins |
||
| 236 | public function get_xoopsPlugins() |
||
| 237 | { |
||
| 238 | $xoopsPlugins = []; |
||
| 239 | $allplugins = XoopsLists::getDirListAsArray(XOOPS_ROOT_PATH . $this->rootpath . '/plugins'); |
||
| 240 | foreach ( $allplugins as $plugin ) { |
||
| 241 | if (false != strpos(strtolower($plugin), 'xoops') && file_exists(XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php")) { |
||
| 242 | if ($right = @include XOOPS_ROOT_PATH . $this->config['rootpath'] . "/include/$plugin.php") { |
||
| 243 | $xoopsPlugins[$plugin] = $plugin; |
||
| 244 | } |
||
| 245 | } |
||
| 246 | } |
||
| 247 | |||
| 248 | return $xoopsPlugins; |
||
| 249 | } |
||
| 250 | |||
| 251 | public function loadCss($css_file = 'style.css') |
||
| 252 | { |
||
| 253 | static $css_url, $css_path; |
||
| 254 | |||
| 255 | if (!isset($css_url)) { |
||
| 256 | $css_url = dirname( xoops_getcss($GLOBALS['xoopsConfig']['theme_set']) ); |
||
| 257 | $css_path = str_replace(XOOPS_THEME_URL, XOOPS_THEME_PATH, $css_url); |
||
| 258 | } |
||
| 259 | |||
| 260 | $css = []; |
||
| 261 | $css[] = $css_url . '/' . $css_file; |
||
| 262 | $css_content = file_get_contents( $css_path . '/' . $css_file ); |
||
| 263 | |||
| 264 | // get all import css files |
||
| 265 | if ( preg_match_all("~\@import url\((.*\.css)\);~sUi", $css_content, $matches, PREG_PATTERN_ORDER) ) { |
||
| 266 | foreach( $matches[1] as $key => $css_import ) { |
||
| 267 | $css = array_merge( $css, $this->loadCss( $css_import) ); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | return $css; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Renders the tinyMCE |
||
| 276 | * @return string $ret The rendered HTML string |
||
| 277 | **/ |
||
| 278 | public function render() |
||
| 378 | } |
||
| 379 | } |
||
| 380 |