wysiwyg.module 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  1. <?php
  2. /**
  3. * @file
  4. * Integrates client-side editors with Drupal.
  5. */
  6. /**
  7. * Implements hook_entity_info().
  8. */
  9. function wysiwyg_entity_info() {
  10. $types['wysiwyg_profile'] = array(
  11. 'label' => t('Wysiwyg profile'),
  12. 'base table' => 'wysiwyg',
  13. 'controller class' => 'WysiwygProfileController',
  14. 'fieldable' => FALSE,
  15. // When loading all entities, DrupalDefaultEntityController::load() ignores
  16. // its static cache. Therefore, wysiwyg_profile_load_all() implements a
  17. // custom static cache.
  18. 'static cache' => FALSE,
  19. 'entity keys' => array(
  20. 'id' => 'format',
  21. ),
  22. );
  23. return $types;
  24. }
  25. /**
  26. * Controller class for Wysiwyg profiles.
  27. */
  28. class WysiwygProfileController extends DrupalDefaultEntityController {
  29. /**
  30. * Overrides DrupalDefaultEntityController::attachLoad().
  31. */
  32. function attachLoad(&$queried_entities, $revision_id = FALSE) {
  33. // Unserialize the profile settings.
  34. foreach ($queried_entities as $key => $record) {
  35. $queried_entities[$key]->settings = unserialize($record->settings);
  36. }
  37. // Call the default attachLoad() method.
  38. parent::attachLoad($queried_entities, $revision_id);
  39. }
  40. }
  41. /**
  42. * Implementation of hook_menu().
  43. */
  44. function wysiwyg_menu() {
  45. $items['admin/config/content/wysiwyg'] = array(
  46. 'title' => 'Wysiwyg profiles',
  47. 'page callback' => 'drupal_get_form',
  48. 'page arguments' => array('wysiwyg_profile_overview'),
  49. 'description' => 'Configure client-side editors.',
  50. 'access arguments' => array('administer filters'),
  51. 'file' => 'wysiwyg.admin.inc',
  52. );
  53. $items['admin/config/content/wysiwyg/profile'] = array(
  54. 'title' => 'List',
  55. 'type' => MENU_DEFAULT_LOCAL_TASK,
  56. );
  57. $items['admin/config/content/wysiwyg/profile/%wysiwyg_profile/edit'] = array(
  58. 'title' => 'Edit',
  59. 'page callback' => 'drupal_get_form',
  60. 'page arguments' => array('wysiwyg_profile_form', 5),
  61. 'access arguments' => array('administer filters'),
  62. 'file' => 'wysiwyg.admin.inc',
  63. 'tab_root' => 'admin/config/content/wysiwyg/profile',
  64. 'tab_parent' => 'admin/config/content/wysiwyg/profile/%wysiwyg_profile',
  65. 'type' => MENU_LOCAL_TASK,
  66. );
  67. $items['admin/config/content/wysiwyg/profile/%wysiwyg_profile/delete'] = array(
  68. 'title' => 'Remove',
  69. 'page callback' => 'drupal_get_form',
  70. 'page arguments' => array('wysiwyg_profile_delete_confirm', 5),
  71. 'access arguments' => array('administer filters'),
  72. 'file' => 'wysiwyg.admin.inc',
  73. 'tab_root' => 'admin/config/content/wysiwyg/profile',
  74. 'tab_parent' => 'admin/config/content/wysiwyg/profile/%wysiwyg_profile',
  75. 'type' => MENU_LOCAL_TASK,
  76. 'weight' => 10,
  77. );
  78. $items['wysiwyg/%'] = array(
  79. 'page callback' => 'wysiwyg_dialog',
  80. 'page arguments' => array(1),
  81. 'access arguments' => array('access content'),
  82. 'type' => MENU_CALLBACK,
  83. 'file' => 'wysiwyg.dialog.inc',
  84. );
  85. return $items;
  86. }
  87. /**
  88. * Implementation of hook_theme().
  89. *
  90. * @see drupal_common_theme(), common.inc
  91. * @see template_preprocess_page(), theme.inc
  92. */
  93. function wysiwyg_theme() {
  94. return array(
  95. 'wysiwyg_profile_overview' => array(
  96. 'render element' => 'form',
  97. ),
  98. 'wysiwyg_admin_button_table' => array(
  99. 'render element' => 'form',
  100. ),
  101. 'wysiwyg_dialog_page' => array(
  102. 'variables' => array('content' => NULL, 'show_messages' => TRUE),
  103. 'file' => 'wysiwyg.dialog.inc',
  104. 'template' => 'wysiwyg-dialog-page',
  105. ),
  106. );
  107. }
  108. /**
  109. * Implementation of hook_help().
  110. */
  111. function wysiwyg_help($path, $arg) {
  112. switch ($path) {
  113. case 'admin/config/content/wysiwyg':
  114. $output = '<p>' . t('A Wysiwyg profile is associated with an input format. A Wysiwyg profile defines which client-side editor is loaded with a particular input format, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') . '</p>';
  115. return $output;
  116. }
  117. }
  118. /**
  119. * Implementation of hook_form_alter().
  120. */
  121. function wysiwyg_form_alter(&$form, &$form_state) {
  122. // Teaser splitter is unconditionally removed and NOT supported.
  123. if (isset($form['body_field'])) {
  124. unset($form['body_field']['teaser_js']);
  125. }
  126. }
  127. /**
  128. * Implements hook_element_info_alter().
  129. */
  130. function wysiwyg_element_info_alter(&$types) {
  131. $types['text_format']['#pre_render'][] = 'wysiwyg_pre_render_text_format';
  132. }
  133. /**
  134. * Process a text format widget to load and attach editors.
  135. *
  136. * The element's #id is used as reference to attach client-side editors.
  137. */
  138. function wysiwyg_pre_render_text_format($element) {
  139. // filter_process_format() copies properties to the expanded 'value' child
  140. // element. Skip this text format widget, if it contains no 'format' or when
  141. // the current user does not have access to edit the value.
  142. if (!isset($element['format']) || !empty($element['value']['#disabled'])) {
  143. return $element;
  144. }
  145. // Allow modules to programmatically enforce no client-side editor by setting
  146. // the #wysiwyg property to FALSE.
  147. if (isset($element['#wysiwyg']) && !$element['#wysiwyg']) {
  148. return $element;
  149. }
  150. $format_field = &$element['format'];
  151. $field = &$element['value'];
  152. $settings = array(
  153. 'field' => $field['#id'],
  154. );
  155. // If this textarea is #resizable and we will load at least one
  156. // editor, then only load the behavior and let the 'none' editor
  157. // attach/detach it to avoid hi-jacking the UI. Due to our CSS class
  158. // parsing, we can add arbitrary parameters for each input format.
  159. // The #resizable property will be removed below, if at least one
  160. // profile has been loaded.
  161. $resizable = 0;
  162. if (!empty($field['#resizable'])) {
  163. $resizable = 1;
  164. drupal_add_js('misc/textarea.js');
  165. }
  166. // Determine the available text formats.
  167. foreach ($format_field['format']['#options'] as $format_id => $format_name) {
  168. $format = 'format' . $format_id;
  169. // Initialize default settings, defaulting to 'none' editor.
  170. $settings[$format] = array(
  171. 'editor' => 'none',
  172. 'status' => 1,
  173. 'toggle' => 1,
  174. 'resizable' => $resizable,
  175. );
  176. // Fetch the profile associated to this text format.
  177. $profile = wysiwyg_get_profile($format_id);
  178. if ($profile) {
  179. $loaded = TRUE;
  180. $settings[$format]['editor'] = $profile->editor;
  181. $settings[$format]['status'] = (int) wysiwyg_user_get_status($profile);
  182. if (isset($profile->settings['show_toggle'])) {
  183. $settings[$format]['toggle'] = (int) $profile->settings['show_toggle'];
  184. }
  185. // Check editor theme (and reset it if not/no longer available).
  186. $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : ''));
  187. // Add plugin settings (first) for this text format.
  188. wysiwyg_add_plugin_settings($profile);
  189. // Add profile settings for this text format.
  190. wysiwyg_add_editor_settings($profile, $theme);
  191. }
  192. }
  193. // Use a hidden element for a single text format.
  194. if (!$format_field['format']['#access']) {
  195. $format_field['wysiwyg'] = array(
  196. '#type' => 'hidden',
  197. '#name' => $format_field['format']['#name'],
  198. '#value' => $format_id,
  199. '#attributes' => array(
  200. 'id' => $format_field['format']['#id'],
  201. 'class' => array('wysiwyg'),
  202. ),
  203. );
  204. $format_field['wysiwyg']['#attached']['js'][] = array(
  205. 'data' => array(
  206. 'wysiwyg' => array(
  207. 'triggers' => array(
  208. $format_field['format']['#id'] => $settings,
  209. ),
  210. ),
  211. ),
  212. 'type' => 'setting',
  213. );
  214. }
  215. // Otherwise, attach to text format selector.
  216. else {
  217. $format_field['format']['#attributes']['class'][] = 'wysiwyg';
  218. $format_field['format']['#attached']['js'][] = array(
  219. 'data' => array(
  220. 'wysiwyg' => array(
  221. 'triggers' => array(
  222. $format_field['format']['#id'] => $settings,
  223. ),
  224. ),
  225. ),
  226. 'type' => 'setting',
  227. );
  228. }
  229. // If we loaded at least one editor, then the 'none' editor will
  230. // handle resizable textareas instead of core.
  231. if (isset($loaded) && $resizable) {
  232. $field['#resizable'] = FALSE;
  233. }
  234. return $element;
  235. }
  236. /**
  237. * Determine the profile to use for a given input format id.
  238. *
  239. * This function also performs sanity checks for the configured editor in a
  240. * profile to ensure that we do not load a malformed editor.
  241. *
  242. * @param $format
  243. * The internal id of an input format.
  244. *
  245. * @return
  246. * A wysiwyg profile.
  247. *
  248. * @see wysiwyg_load_editor(), wysiwyg_get_editor()
  249. */
  250. function wysiwyg_get_profile($format) {
  251. if ($profile = wysiwyg_profile_load($format)) {
  252. if (wysiwyg_load_editor($profile)) {
  253. return $profile;
  254. }
  255. }
  256. return FALSE;
  257. }
  258. /**
  259. * Load an editor library and initialize basic Wysiwyg settings.
  260. *
  261. * @param $profile
  262. * A wysiwyg editor profile.
  263. *
  264. * @return
  265. * TRUE if the editor has been loaded, FALSE if not.
  266. *
  267. * @see wysiwyg_get_profile()
  268. */
  269. function wysiwyg_load_editor($profile) {
  270. static $settings_added;
  271. static $loaded = array();
  272. $name = $profile->editor;
  273. // Library files must be loaded only once.
  274. if (!isset($loaded[$name])) {
  275. // Load editor.
  276. $editor = wysiwyg_get_editor($name);
  277. if ($editor) {
  278. // Determine library files to load.
  279. // @todo Allow to configure the library/execMode to use.
  280. if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
  281. $library = $profile->settings['library'];
  282. $files = $editor['libraries'][$library]['files'];
  283. }
  284. else {
  285. // Fallback to the first defined library by default (external libraries can change).
  286. $library = key($editor['libraries']);
  287. $files = array_shift($editor['libraries']);
  288. $files = $files['files'];
  289. }
  290. foreach ($files as $file => $options) {
  291. if (is_array($options)) {
  292. $options += array('type' => 'file', 'scope' => 'header', 'defer' => FALSE, 'cache' => TRUE, 'preprocess' => TRUE);
  293. drupal_add_js($editor['library path'] . '/' . $file, $options);
  294. }
  295. else {
  296. drupal_add_js($editor['library path'] . '/' . $options);
  297. }
  298. }
  299. // If editor defines an additional load callback, invoke it.
  300. // @todo Isn't the settings callback sufficient?
  301. if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
  302. $editor['load callback']($editor, $library);
  303. }
  304. // Load JavaScript integration files for this editor.
  305. $files = array();
  306. if (isset($editor['js files'])) {
  307. $files = $editor['js files'];
  308. }
  309. foreach ($files as $file) {
  310. drupal_add_js($editor['js path'] . '/' . $file);
  311. }
  312. // Load CSS stylesheets for this editor.
  313. $files = array();
  314. if (isset($editor['css files'])) {
  315. $files = $editor['css files'];
  316. }
  317. foreach ($files as $file) {
  318. drupal_add_css($editor['css path'] . '/' . $file);
  319. }
  320. drupal_add_js(array('wysiwyg' => array(
  321. 'configs' => array($editor['name'] => array('global' => array(
  322. // @todo Move into (global) editor settings.
  323. // If JS compression is enabled, at least TinyMCE is unable to determine
  324. // its own base path and exec mode since it can't find the script name.
  325. 'editorBasePath' => base_path() . $editor['library path'],
  326. 'execMode' => $library,
  327. ))),
  328. )), 'setting');
  329. $loaded[$name] = TRUE;
  330. }
  331. else {
  332. $loaded[$name] = FALSE;
  333. }
  334. }
  335. // Add basic Wysiwyg settings if any editor has been added.
  336. if (!isset($settings_added) && $loaded[$name]) {
  337. drupal_add_js(array('wysiwyg' => array(
  338. 'configs' => array(),
  339. 'plugins' => array(),
  340. 'disable' => t('Disable rich-text'),
  341. 'enable' => t('Enable rich-text'),
  342. )), 'setting');
  343. $path = drupal_get_path('module', 'wysiwyg');
  344. // Initialize our namespaces in the *header* to do not force editor
  345. // integration scripts to check and define Drupal.wysiwyg on its own.
  346. drupal_add_js($path . '/wysiwyg.init.js', array('group' => JS_LIBRARY));
  347. // The 'none' editor is a special editor implementation, allowing us to
  348. // attach and detach regular Drupal behaviors just like any other editor.
  349. drupal_add_js($path . '/editors/js/none.js');
  350. // Add wysiwyg.js to the footer to ensure it's executed after the
  351. // Drupal.settings array has been rendered and populated. Also, since editor
  352. // library initialization functions must be loaded first by the browser,
  353. // and Drupal.wysiwygInit() must be executed AFTER editors registered
  354. // their callbacks and BEFORE Drupal.behaviors are applied, this must come
  355. // last.
  356. drupal_add_js($path . '/wysiwyg.js', array('scope' => 'footer'));
  357. $settings_added = TRUE;
  358. }
  359. return $loaded[$name];
  360. }
  361. /**
  362. * Add editor settings for a given input format.
  363. */
  364. function wysiwyg_add_editor_settings($profile, $theme) {
  365. static $formats = array();
  366. if (!isset($formats[$profile->format])) {
  367. $config = wysiwyg_get_editor_config($profile, $theme);
  368. // drupal_to_js() does not properly convert numeric array keys, so we need
  369. // to use a string instead of the format id.
  370. if ($config) {
  371. drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format' . $profile->format => $config)))), 'setting');
  372. }
  373. $formats[$profile->format] = TRUE;
  374. }
  375. }
  376. /**
  377. * Add settings for external plugins.
  378. *
  379. * Plugins can be used in multiple profiles, but not necessarily in all. Because
  380. * of that, we need to process plugins for each profile, even if most of their
  381. * settings are not stored per profile.
  382. *
  383. * Implementations of hook_wysiwyg_plugin() may execute different code for each
  384. * editor. Therefore, we have to invoke those implementations for each editor,
  385. * but process the resulting plugins separately for each profile.
  386. *
  387. * Drupal plugins differ to native plugins in that they have plugin-specific
  388. * definitions and settings, which need to be processed only once. But they are
  389. * also passed to the editor to prepare settings specific to the editor.
  390. * Therefore, we load and process the Drupal plugins only once, and hand off the
  391. * effective definitions for each profile to the editor.
  392. *
  393. * @param $profile
  394. * A wysiwyg editor profile.
  395. *
  396. * @todo Rewrite wysiwyg_process_form() to build a registry of effective
  397. * profiles in use, so we can process plugins in multiple profiles in one shot
  398. * and simplify this entire function.
  399. */
  400. function wysiwyg_add_plugin_settings($profile) {
  401. static $plugins = array();
  402. static $processed_plugins = array();
  403. static $processed_formats = array();
  404. // Each input format must only processed once.
  405. // @todo ...as long as we do not have multiple profiles per format.
  406. if (isset($processed_formats[$profile->format])) {
  407. return;
  408. }
  409. $processed_formats[$profile->format] = TRUE;
  410. $editor = wysiwyg_get_editor($profile->editor);
  411. // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
  412. // and Drupal plugins provided via hook_wysiwyg_include_directory().
  413. if (!array_key_exists($editor['name'], $plugins)) {
  414. $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
  415. }
  416. // Nothing to do, if there are no plugins.
  417. if (empty($plugins[$editor['name']])) {
  418. return;
  419. }
  420. // Determine name of proxy plugin for Drupal plugins.
  421. $proxy = (isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '');
  422. // Process native editor plugins.
  423. if (isset($editor['plugin settings callback'])) {
  424. // @todo Require PHP 5.1 in 3.x and use array_intersect_key().
  425. $profile_plugins_native = array();
  426. foreach ($plugins[$editor['name']] as $plugin => $meta) {
  427. // Skip Drupal plugins (handled below).
  428. if ($plugin === $proxy) {
  429. continue;
  430. }
  431. // Only keep native plugins that are enabled in this profile.
  432. if (isset($profile->settings['buttons'][$plugin])) {
  433. $profile_plugins_native[$plugin] = $meta;
  434. }
  435. }
  436. // Invoke the editor's plugin settings callback, so it can populate the
  437. // settings for native external plugins with required values.
  438. $settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native);
  439. if ($settings_native) {
  440. drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('native' => $settings_native)))), 'setting');
  441. }
  442. }
  443. // Process Drupal plugins.
  444. if ($proxy && isset($editor['proxy plugin settings callback'])) {
  445. $profile_plugins_drupal = array();
  446. foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
  447. if (isset($profile->settings['buttons'][$proxy][$plugin])) {
  448. // JavaScript and plugin-specific settings for Drupal plugins must be
  449. // loaded and processed only once. Plugin information is cached
  450. // statically to pass it to the editor's proxy plugin settings callback.
  451. if (!isset($processed_plugins[$proxy][$plugin])) {
  452. $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;
  453. // Load the Drupal plugin's JavaScript.
  454. drupal_add_js($meta['js path'] . '/' . $meta['js file']);
  455. // Add plugin-specific settings.
  456. if (isset($meta['settings'])) {
  457. drupal_add_js(array('wysiwyg' => array('plugins' => array('drupal' => array($plugin => $meta['settings'])))), 'setting');
  458. }
  459. }
  460. else {
  461. $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
  462. }
  463. }
  464. }
  465. // Invoke the editor's proxy plugin settings callback, so it can populate
  466. // the settings for Drupal plugins with custom, required values.
  467. $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);
  468. if ($settings_drupal) {
  469. drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('drupal' => $settings_drupal)))), 'setting');
  470. }
  471. }
  472. }
  473. /**
  474. * Retrieve available themes for an editor.
  475. *
  476. * Editor themes control the visual presentation of an editor.
  477. *
  478. * @param $profile
  479. * A wysiwyg editor profile; passed/altered by reference.
  480. * @param $selected_theme
  481. * An optional theme name that ought to be used.
  482. *
  483. * @return
  484. * An array of theme names, or a single, checked theme name if $selected_theme
  485. * was given.
  486. */
  487. function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
  488. static $themes = array();
  489. if (!isset($themes[$profile->editor])) {
  490. $editor = wysiwyg_get_editor($profile->editor);
  491. if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
  492. $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
  493. }
  494. // Fallback to 'default' otherwise.
  495. else {
  496. $themes[$editor['name']] = array('default');
  497. }
  498. }
  499. // Check optional $selected_theme argument, if given.
  500. if (isset($selected_theme)) {
  501. // If the passed theme name does not exist, use the first available.
  502. if (!in_array($selected_theme, $themes[$profile->editor])) {
  503. $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
  504. }
  505. }
  506. return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
  507. }
  508. /**
  509. * Return plugin metadata from the plugin registry.
  510. *
  511. * @param $editor_name
  512. * The internal name of an editor to return plugins for.
  513. *
  514. * @return
  515. * An array for each plugin.
  516. */
  517. function wysiwyg_get_plugins($editor_name) {
  518. $plugins = array();
  519. if (!empty($editor_name)) {
  520. $editor = wysiwyg_get_editor($editor_name);
  521. // Add internal editor plugins.
  522. if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
  523. $plugins = $editor['plugin callback']($editor);
  524. }
  525. // Add editor plugins provided via hook_wysiwyg_plugin().
  526. $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
  527. // Add API plugins provided by Drupal modules.
  528. // @todo We need to pass the filepath to the plugin icon for Drupal plugins.
  529. if (isset($editor['proxy plugin'])) {
  530. $plugins += $editor['proxy plugin'];
  531. $proxy = key($editor['proxy plugin']);
  532. foreach (wysiwyg_get_all_plugins() as $plugin_name => $info) {
  533. $plugins[$proxy]['buttons'][$plugin_name] = $info['title'];
  534. }
  535. }
  536. }
  537. return $plugins;
  538. }
  539. /**
  540. * Return an array of initial editor settings for a Wysiwyg profile.
  541. */
  542. function wysiwyg_get_editor_config($profile, $theme) {
  543. $editor = wysiwyg_get_editor($profile->editor);
  544. $settings = array();
  545. if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
  546. $settings = $editor['settings callback']($editor, $profile->settings, $theme);
  547. // Allow other modules to alter the editor settings for this format.
  548. $context = array('editor' => $editor, 'profile' => $profile, 'theme' => $theme);
  549. drupal_alter('wysiwyg_editor_settings', $settings, $context);
  550. }
  551. return $settings;
  552. }
  553. /**
  554. * Retrieve stylesheets for HTML/IFRAME-based editors.
  555. *
  556. * This assumes that the content editing area only needs stylesheets defined
  557. * for the scope 'theme'.
  558. *
  559. * @return
  560. * An array containing CSS files, including proper base path.
  561. */
  562. function wysiwyg_get_css() {
  563. static $files;
  564. if (isset($files)) {
  565. return $files;
  566. }
  567. // In node form previews, the theme has not been initialized yet.
  568. if (!empty($_POST)) {
  569. drupal_theme_initialize();
  570. }
  571. $files = array();
  572. foreach (drupal_add_css() as $filepath => $info) {
  573. if ($info['group'] >= CSS_THEME && $info['media'] != 'print') {
  574. if (file_exists($filepath)) {
  575. $files[] = base_path() . $filepath;
  576. }
  577. }
  578. }
  579. return $files;
  580. }
  581. /**
  582. * Loads a profile for a given text format.
  583. *
  584. * Since there are commonly not many text formats, and each text format-enabled
  585. * form element will possibly have to load every single profile, all existing
  586. * profiles are loaded and cached once to reduce the amount of database queries.
  587. */
  588. function wysiwyg_profile_load($format) {
  589. $profiles = wysiwyg_profile_load_all();
  590. return (isset($profiles[$format]) ? $profiles[$format] : FALSE);
  591. }
  592. /**
  593. * Loads all profiles.
  594. */
  595. function wysiwyg_profile_load_all() {
  596. // entity_load(..., FALSE) does not re-use its own static cache upon
  597. // repetitive calls, so a custom static cache is required.
  598. // @see wysiwyg_entity_info()
  599. $profiles = &drupal_static(__FUNCTION__);
  600. if (!isset($profiles)) {
  601. // Additional database cache to support alternative caches like memcache.
  602. if ($cached = cache_get('wysiwyg_profiles')) {
  603. $profiles = $cached->data;
  604. }
  605. else {
  606. $profiles = entity_load('wysiwyg_profile', FALSE);
  607. cache_set('wysiwyg_profiles', $profiles);
  608. }
  609. }
  610. return $profiles;
  611. }
  612. /**
  613. * Deletes a profile from the database.
  614. */
  615. function wysiwyg_profile_delete($format) {
  616. db_delete('wysiwyg')
  617. ->condition('format', $format)
  618. ->execute();
  619. }
  620. /**
  621. * Clear all Wysiwyg profile caches.
  622. */
  623. function wysiwyg_profile_cache_clear() {
  624. entity_get_controller('wysiwyg_profile')->resetCache();
  625. drupal_static_reset('wysiwyg_profile_load_all');
  626. cache_clear_all('wysiwyg_profiles', 'cache');
  627. }
  628. /**
  629. * Implements hook_form_FORM_ID_alter().
  630. */
  631. function wysiwyg_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  632. $account = $form['#user'];
  633. $user_formats = filter_formats($account);
  634. $options = array();
  635. $options_default = array();
  636. foreach (wysiwyg_profile_load_all() as $format => $profile) {
  637. // Only show profiles that have user_choose enabled.
  638. if (!empty($profile->settings['user_choose']) && isset($user_formats[$format])) {
  639. $options[$format] = check_plain($user_formats[$format]->name);
  640. if (wysiwyg_user_get_status($profile, $account)) {
  641. $options_default[] = $format;
  642. }
  643. }
  644. }
  645. if (!empty($options)) {
  646. $form['wysiwyg']['wysiwyg_status'] = array(
  647. '#type' => 'checkboxes',
  648. '#title' => t('Text formats enabled for rich-text editing'),
  649. '#options' => $options,
  650. '#default_value' => $options_default,
  651. );
  652. }
  653. }
  654. /**
  655. * Implements hook_user_insert().
  656. *
  657. * Wysiwyg's user preferences are normally not exposed on the user registration
  658. * form, but in case they are manually altered in, we invoke
  659. * wysiwyg_user_update() accordingly.
  660. */
  661. function wysiwyg_user_insert(&$edit, $account, $category) {
  662. wysiwyg_user_update($edit, $account, $category);
  663. }
  664. /**
  665. * Implements hook_user_update().
  666. */
  667. function wysiwyg_user_update(&$edit, $account, $category) {
  668. if (isset($edit['wysiwyg_status'])) {
  669. db_delete('wysiwyg_user')
  670. ->condition('uid', $account->uid)
  671. ->execute();
  672. $query = db_insert('wysiwyg_user')
  673. ->fields(array('uid', 'format', 'status'));
  674. foreach ($edit['wysiwyg_status'] as $format => $status) {
  675. $query->values(array(
  676. 'uid' => $account->uid,
  677. 'format' => $format,
  678. 'status' => (int) (bool) $status,
  679. ));
  680. }
  681. $query->execute();
  682. }
  683. }
  684. function wysiwyg_user_get_status($profile, $account = NULL) {
  685. global $user;
  686. if (!isset($account)) {
  687. $account = $user;
  688. }
  689. // Default wysiwyg editor status information is only required on forms, so we
  690. // do not pre-emptively load and attach this information on every user_load().
  691. if (!isset($account->wysiwyg_status)) {
  692. $account->wysiwyg_status = db_query("SELECT format, status FROM {wysiwyg_user} WHERE uid = :uid", array(
  693. ':uid' => $account->uid,
  694. ))->fetchAllKeyed();
  695. }
  696. if (!empty($profile->settings['user_choose']) && isset($account->wysiwyg_status[$profile->format])) {
  697. $status = $account->wysiwyg_status[$profile->format];
  698. }
  699. else {
  700. $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE;
  701. }
  702. return (bool) $status;
  703. }
  704. /**
  705. * @defgroup wysiwyg_api Wysiwyg API
  706. * @{
  707. *
  708. * @todo Forked from Panels; abstract into a separate API module that allows
  709. * contrib modules to define supported include/plugin types.
  710. */
  711. /**
  712. * Return library information for a given editor.
  713. *
  714. * @param $name
  715. * The internal name of an editor.
  716. *
  717. * @return
  718. * The library information for the editor, or FALSE if $name is unknown or not
  719. * installed properly.
  720. */
  721. function wysiwyg_get_editor($name) {
  722. $editors = wysiwyg_get_all_editors();
  723. return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
  724. }
  725. /**
  726. * Compile a list holding all supported editors including installed editor version information.
  727. */
  728. function wysiwyg_get_all_editors() {
  729. static $editors;
  730. if (isset($editors)) {
  731. return $editors;
  732. }
  733. $editors = wysiwyg_load_includes('editors', 'editor');
  734. foreach ($editors as $editor => $properties) {
  735. // Fill in required properties.
  736. $editors[$editor] += array(
  737. 'title' => '',
  738. 'vendor url' => '',
  739. 'download url' => '',
  740. 'editor path' => wysiwyg_get_path($editors[$editor]['name']),
  741. 'library path' => wysiwyg_get_path($editors[$editor]['name']),
  742. 'libraries' => array(),
  743. 'version callback' => NULL,
  744. 'themes callback' => NULL,
  745. 'settings callback' => NULL,
  746. 'plugin callback' => NULL,
  747. 'plugin settings callback' => NULL,
  748. 'versions' => array(),
  749. 'js path' => $editors[$editor]['path'] . '/js',
  750. 'css path' => $editors[$editor]['path'] . '/css',
  751. );
  752. // Check whether library is present.
  753. if (!($editors[$editor]['installed'] = file_exists($editors[$editor]['library path']))) {
  754. continue;
  755. }
  756. // Detect library version.
  757. if (function_exists($editors[$editor]['version callback'])) {
  758. $editors[$editor]['installed version'] = $editors[$editor]['version callback']($editors[$editor]);
  759. }
  760. if (empty($editors[$editor]['installed version'])) {
  761. $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
  762. $editors[$editor]['installed'] = FALSE;
  763. continue;
  764. }
  765. // Determine to which supported version the installed version maps.
  766. ksort($editors[$editor]['versions']);
  767. $version = 0;
  768. foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
  769. if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
  770. $version = $supported_version;
  771. }
  772. }
  773. if (!$version) {
  774. $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $editors[$editor]['title']));
  775. $editors[$editor]['installed'] = FALSE;
  776. continue;
  777. }
  778. // Apply library version specific definitions and overrides.
  779. $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
  780. unset($editors[$editor]['versions']);
  781. }
  782. return $editors;
  783. }
  784. /**
  785. * Invoke hook_wysiwyg_plugin() in all modules.
  786. */
  787. function wysiwyg_get_all_plugins() {
  788. static $plugins;
  789. if (isset($plugins)) {
  790. return $plugins;
  791. }
  792. $plugins = wysiwyg_load_includes('plugins', 'plugin');
  793. foreach ($plugins as $name => $properties) {
  794. $plugin = &$plugins[$name];
  795. // Fill in required/default properties.
  796. $plugin += array(
  797. 'title' => $plugin['name'],
  798. 'vendor url' => '',
  799. 'js path' => $plugin['path'] . '/' . $plugin['name'],
  800. 'js file' => $plugin['name'] . '.js',
  801. 'css path' => $plugin['path'] . '/' . $plugin['name'],
  802. 'css file' => $plugin['name'] . '.css',
  803. 'icon path' => $plugin['path'] . '/' . $plugin['name'] . '/images',
  804. 'icon file' => $plugin['name'] . '.png',
  805. 'dialog path' => $plugin['name'],
  806. 'dialog settings' => array(),
  807. 'settings callback' => NULL,
  808. 'settings form callback' => NULL,
  809. );
  810. // Fill in default settings.
  811. $plugin['settings'] += array(
  812. 'path' => base_path() . $plugin['path'] . '/' . $plugin['name'],
  813. );
  814. // Check whether library is present.
  815. if (!($plugin['installed'] = file_exists($plugin['js path'] . '/' . $plugin['js file']))) {
  816. continue;
  817. }
  818. }
  819. return $plugins;
  820. }
  821. /**
  822. * Load include files for wysiwyg implemented by all modules.
  823. *
  824. * @param $type
  825. * The type of includes to search for, can be 'editors'.
  826. * @param $hook
  827. * The hook name to invoke.
  828. * @param $file
  829. * An optional include file name without .inc extension to limit the search to.
  830. *
  831. * @see wysiwyg_get_directories(), _wysiwyg_process_include()
  832. */
  833. function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
  834. // Determine implementations.
  835. $directories = wysiwyg_get_directories($type);
  836. $directories['wysiwyg'] = drupal_get_path('module', 'wysiwyg') . '/' . $type;
  837. $file_list = array();
  838. foreach ($directories as $module => $path) {
  839. $file_list[$module] = drupal_system_listing("/{$file}.inc\$/", $path, 'name', 0);
  840. }
  841. // Load implementations.
  842. $info = array();
  843. foreach (array_filter($file_list) as $module => $files) {
  844. foreach ($files as $file) {
  845. include_once './' . $file->uri;
  846. $result = _wysiwyg_process_include($module, $module . '_' . $file->name, dirname($file->uri), $hook);
  847. if (is_array($result)) {
  848. $info = array_merge($info, $result);
  849. }
  850. }
  851. }
  852. return $info;
  853. }
  854. /**
  855. * Helper function to build paths to libraries.
  856. *
  857. * @param $library
  858. * The external library name to return the path for.
  859. * @param $base_path
  860. * Whether to prefix the resulting path with base_path().
  861. *
  862. * @return
  863. * The path to the specified library.
  864. *
  865. * @ingroup libraries
  866. */
  867. function wysiwyg_get_path($library, $base_path = FALSE) {
  868. static $libraries;
  869. if (!isset($libraries)) {
  870. $libraries = wysiwyg_get_libraries();
  871. }
  872. if (!isset($libraries[$library])) {
  873. // Most often, external libraries can be shared across multiple sites.
  874. return 'sites/all/libraries/' . $library;
  875. }
  876. $path = ($base_path ? base_path() : '');
  877. $path .= $libraries[$library];
  878. return $path;
  879. }
  880. /**
  881. * Return an array of library directories.
  882. *
  883. * Returns an array of library directories from the all-sites directory
  884. * (i.e. sites/all/libraries/), the profiles directory, and site-specific
  885. * directory (i.e. sites/somesite/libraries/). The returned array will be keyed
  886. * by the library name. Site-specific libraries are prioritized over libraries
  887. * in the default directories. That is, if a library with the same name appears
  888. * in both the site-wide directory and site-specific directory, only the
  889. * site-specific version will be listed.
  890. *
  891. * @return
  892. * A list of library directories.
  893. *
  894. * @ingroup libraries
  895. */
  896. function wysiwyg_get_libraries() {
  897. global $profile;
  898. // When this function is called during Drupal's initial installation process,
  899. // the name of the profile that is about to be installed is stored in the
  900. // global $profile variable. At all other times, the regular system variable
  901. // contains the name of the current profile, and we can call variable_get()
  902. // to determine the profile.
  903. if (!isset($profile)) {
  904. $profile = variable_get('install_profile', 'default');
  905. }
  906. $directory = 'libraries';
  907. $searchdir = array();
  908. $config = conf_path();
  909. // The 'profiles' directory contains pristine collections of modules and
  910. // themes as organized by a distribution. It is pristine in the same way
  911. // that /modules is pristine for core; users should avoid changing anything
  912. // there in favor of sites/all or sites/<domain> directories.
  913. if (file_exists("profiles/$profile/$directory")) {
  914. $searchdir[] = "profiles/$profile/$directory";
  915. }
  916. // Always search sites/all/*.
  917. $searchdir[] = 'sites/all/' . $directory;
  918. // Also search sites/<domain>/*.
  919. if (file_exists("$config/$directory")) {
  920. $searchdir[] = "$config/$directory";
  921. }
  922. // Retrieve list of directories.
  923. // @todo Core: Allow to scan for directories.
  924. $directories = array();
  925. $nomask = array('CVS');
  926. foreach ($searchdir as $dir) {
  927. if (is_dir($dir) && $handle = opendir($dir)) {
  928. while (FALSE !== ($file = readdir($handle))) {
  929. if (!in_array($file, $nomask) && $file[0] != '.') {
  930. if (is_dir("$dir/$file")) {
  931. $directories[$file] = "$dir/$file";
  932. }
  933. }
  934. }
  935. closedir($handle);
  936. }
  937. }
  938. return $directories;
  939. }
  940. /**
  941. * Return a list of directories by modules implementing wysiwyg_include_directory().
  942. *
  943. * @param $plugintype
  944. * The type of a plugin; can be 'editors'.
  945. *
  946. * @return
  947. * An array containing module names suffixed with '_' and their defined
  948. * directory.
  949. *
  950. * @see wysiwyg_load_includes(), _wysiwyg_process_include()
  951. */
  952. function wysiwyg_get_directories($plugintype) {
  953. $directories = array();
  954. foreach (module_implements('wysiwyg_include_directory') as $module) {
  955. $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
  956. if (isset($result) && is_string($result)) {
  957. $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
  958. }
  959. }
  960. return $directories;
  961. }
  962. /**
  963. * Process a single hook implementation of a wysiwyg editor.
  964. *
  965. * @param $module
  966. * The module that owns the hook.
  967. * @param $identifier
  968. * Either the module or 'wysiwyg_' . $file->name
  969. * @param $hook
  970. * The name of the hook being invoked.
  971. */
  972. function _wysiwyg_process_include($module, $identifier, $path, $hook) {
  973. $function = $identifier . '_' . $hook;
  974. if (!function_exists($function)) {
  975. return NULL;
  976. }
  977. $result = $function();
  978. if (!isset($result) || !is_array($result)) {
  979. return NULL;
  980. }
  981. // Fill in defaults.
  982. foreach ($result as $editor => $properties) {
  983. $result[$editor]['module'] = $module;
  984. $result[$editor]['name'] = $editor;
  985. $result[$editor]['path'] = $path;
  986. }
  987. return $result;
  988. }
  989. /**
  990. * @} End of "defgroup wysiwyg_api".
  991. */