wysiwyg.module 37 KB

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