wysiwyg.module 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. <?php
  2. /**
  3. * @file
  4. * Integrates client-side editors with Drupal.
  5. */
  6. require_once 'includes/styling.inc';
  7. /**
  8. * Implements hook_entity_info().
  9. */
  10. function wysiwyg_entity_info() {
  11. $types['wysiwyg_profile'] = array(
  12. 'label' => t('Wysiwyg profile'),
  13. 'base table' => 'wysiwyg',
  14. 'controller class' => 'WysiwygProfileController',
  15. 'fieldable' => FALSE,
  16. // When loading all entities, DrupalDefaultEntityController::load() ignores
  17. // its static cache. Therefore, wysiwyg_profile_load_all() implements a
  18. // custom static cache.
  19. 'static cache' => FALSE,
  20. 'entity keys' => array(
  21. 'id' => 'format',
  22. ),
  23. );
  24. return $types;
  25. }
  26. /**
  27. * Controller class for Wysiwyg profiles.
  28. */
  29. class WysiwygProfileController extends DrupalDefaultEntityController {
  30. /**
  31. * Overrides DrupalDefaultEntityController::attachLoad().
  32. */
  33. function attachLoad(&$queried_entities, $revision_id = FALSE) {
  34. // Unserialize the profile settings.
  35. foreach ($queried_entities as $key => $record) {
  36. $settings = unserialize($record->settings);
  37. // Profile preferences are stored with the editor settings to avoid adding
  38. // an extra table column.
  39. if (isset($settings['_profile_preferences'])) {
  40. $preferences = $settings['_profile_preferences'];
  41. unset($settings['_profile_preferences']);
  42. }
  43. else {
  44. $preferences = array();
  45. }
  46. $queried_entities[$key]->settings = $settings;
  47. $queried_entities[$key]->preferences = $preferences;
  48. // @todo Store the name in the profile when allowing more than one per
  49. // format.
  50. $queried_entities[$key]->name = 'format' . $record->format;
  51. }
  52. // Call the default attachLoad() method.
  53. parent::attachLoad($queried_entities, $revision_id);
  54. }
  55. }
  56. /**
  57. * Implementation of hook_menu().
  58. */
  59. function wysiwyg_menu() {
  60. $items['admin/config/content/wysiwyg'] = array(
  61. 'title' => 'Wysiwyg profiles',
  62. 'page callback' => 'drupal_get_form',
  63. 'page arguments' => array('wysiwyg_profile_overview'),
  64. 'description' => 'Configure client-side editors.',
  65. 'access arguments' => array('administer filters'),
  66. 'file' => 'wysiwyg.admin.inc',
  67. );
  68. $items['admin/config/content/wysiwyg/list'] = array(
  69. 'title' => 'List',
  70. 'type' => MENU_DEFAULT_LOCAL_TASK,
  71. 'weight' => -10,
  72. );
  73. $items['admin/config/content/wysiwyg/profile/%wysiwyg_ui_profile_cache'] = array(
  74. 'title callback' => 'wysiwyg_admin_profile_title',
  75. 'title arguments' => array(5),
  76. 'page callback' => 'drupal_get_form',
  77. 'page arguments' => array('wysiwyg_profile_form', 5),
  78. 'access arguments' => array('administer filters'),
  79. 'file' => 'wysiwyg.admin.inc',
  80. );
  81. $items['admin/config/content/wysiwyg/profile/%wysiwyg_ui_profile_cache/edit'] = array(
  82. 'title' => 'Edit',
  83. 'type' => MENU_DEFAULT_LOCAL_TASK,
  84. );
  85. $items['admin/config/content/wysiwyg/profile/%wysiwyg_ui_profile_cache/delete'] = array(
  86. 'title' => 'Remove',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('wysiwyg_profile_delete_confirm', 5),
  89. 'access arguments' => array('administer filters'),
  90. 'file' => 'wysiwyg.admin.inc',
  91. 'type' => MENU_LOCAL_TASK,
  92. 'weight' => 10,
  93. );
  94. $items['admin/config/content/wysiwyg/profile/%wysiwyg_ui_profile_cache/break-lock'] = array(
  95. 'title' => 'Break lock',
  96. 'page callback' => 'drupal_get_form',
  97. 'page arguments' => array('wysiwyg_profile_break_lock_confirm', 5),
  98. 'access arguments' => array('administer filters'),
  99. 'file' => 'wysiwyg.admin.inc',
  100. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  101. );
  102. // @see wysiwyg_dialog()
  103. $items['wysiwyg/%'] = array(
  104. 'page callback' => 'wysiwyg_dialog',
  105. 'page arguments' => array(1),
  106. 'delivery callback' => 'wysiwyg_deliver_dialog_page',
  107. 'access arguments' => array('access content'),
  108. 'type' => MENU_CALLBACK,
  109. 'file' => 'wysiwyg.dialog.inc',
  110. );
  111. $items['wysiwyg_theme/%'] = array(
  112. 'theme callback' => '_wysiwyg_theme_callback',
  113. 'theme arguments' => array(1),
  114. 'page callback' => '_wysiwyg_theme_check_active',
  115. 'page arguments' => array(1),
  116. 'delivery callback' => '_wysiwyg_delivery_dummy',
  117. 'access arguments' => array('access content'),
  118. 'type' => MENU_CALLBACK,
  119. );
  120. return $items;
  121. }
  122. /**
  123. * Display an editor profile title.
  124. *
  125. * @param $profile
  126. * An editor profile object.
  127. *
  128. * @return
  129. * The unfiltered name of an editor profile.
  130. * Currently the same as the text format name.
  131. */
  132. function wysiwyg_admin_profile_title($profile) {
  133. $format = filter_format_load($profile->format);
  134. return $format->name;
  135. }
  136. /**
  137. * Implements hook_admin_menu_map().
  138. */
  139. function wysiwyg_admin_menu_map() {
  140. if (!user_access('administer filters')) {
  141. return;
  142. }
  143. $profiles = wysiwyg_profile_load_all();
  144. $map['admin/config/content/wysiwyg/profile/%wysiwyg_profile'] = array(
  145. 'parent' => 'admin/config/content/wysiwyg',
  146. 'hide' => 'admin/config/content/wysiwyg/list',
  147. 'arguments' => array(
  148. array('%wysiwyg_profile' => array_keys($profiles)),
  149. ),
  150. );
  151. return $map;
  152. }
  153. /**
  154. * Implements hook_element_info().
  155. */
  156. function wysiwyg_element_info() {
  157. // @see wysiwyg_dialog()
  158. $types['wysiwyg_dialog_page'] = array(
  159. '#theme' => 'wysiwyg_dialog_page',
  160. '#theme_wrappers' => array('html'),
  161. '#show_messages' => TRUE,
  162. );
  163. return $types;
  164. }
  165. /**
  166. * Implementation of hook_theme().
  167. *
  168. * @see drupal_common_theme(), common.inc
  169. * @see template_preprocess_page(), theme.inc
  170. */
  171. function wysiwyg_theme() {
  172. return array(
  173. 'wysiwyg_profile_overview' => array(
  174. 'render element' => 'form',
  175. ),
  176. 'wysiwyg_admin_button_table' => array(
  177. 'render element' => 'form',
  178. ),
  179. // @see wysiwyg_dialog()
  180. 'wysiwyg_dialog_page' => array(
  181. 'render element' => 'page',
  182. 'file' => 'wysiwyg.dialog.inc',
  183. 'template' => 'wysiwyg-dialog-page',
  184. ),
  185. );
  186. }
  187. /**
  188. * Implementation of hook_help().
  189. */
  190. function wysiwyg_help($path, $arg) {
  191. switch ($path) {
  192. case 'admin/config/content/wysiwyg':
  193. $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>';
  194. return $output;
  195. }
  196. }
  197. /**
  198. * Implements hook_element_info_alter().
  199. */
  200. function wysiwyg_element_info_alter(&$types) {
  201. $types['text_format']['#pre_render'][] = 'wysiwyg_pre_render_text_format';
  202. // For filtering stylesheets before Core aggregates them.
  203. array_unshift($types['styles']['#pre_render'], '_wysiwyg_filter_editor_styles');
  204. // For recording and caching the added stylesheets.
  205. $types['styles']['#pre_render'][] = '_wysiwyg_pre_render_styles';
  206. }
  207. /**
  208. * Process a text format widget to load and attach editors.
  209. *
  210. * The element's #id is used as reference to attach client-side editors.
  211. */
  212. function wysiwyg_pre_render_text_format($element) {
  213. // filter_process_format() copies properties to the expanded 'value' child
  214. // element. Skip this text format widget, if it contains no 'format' or when
  215. // the current user does not have access to edit the value.
  216. // Simplify module creates an extra incomplete 'format' on the base field.
  217. if (!isset($element['format']['format']) || !empty($element['value']['#disabled'])) {
  218. return $element;
  219. }
  220. // Allow modules to programmatically enforce no client-side editor by setting
  221. // the #wysiwyg property to FALSE.
  222. if (isset($element['#wysiwyg']) && !$element['#wysiwyg']) {
  223. return $element;
  224. }
  225. $format_field = &$element['format'];
  226. $field = &$element['value'];
  227. $settings = array(
  228. 'field' => $field['#id'],
  229. );
  230. // If this textarea is #resizable the 'none' editor will attach/detach it to
  231. // avoid hi-jacking the UI.
  232. if (!empty($field['#resizable'])) {
  233. $settings['resizable'] = 1;
  234. }
  235. if (isset($element['summary']) && $element['summary']['#type'] == 'textarea') {
  236. $settings['summary'] = $element['summary']['#id'];
  237. }
  238. if (!$format_field['format']['#access'] || (isset($format_field['#access']) && !$format_field['#access'])) {
  239. // Directly specify which the single available format is.
  240. $available_formats = array($format_field['format']['#value'] => $format_field['format']['#options'][$format_field['format']['#value']]);
  241. $settings['activeFormat'] = $format_field['format']['#value'];
  242. }
  243. else {
  244. // Let the client check the selectbox for the active format.
  245. $available_formats = $format_field['format']['#options'];
  246. $settings['select'] = $format_field['format']['#id'];
  247. }
  248. // Determine the available text formats.
  249. foreach ($available_formats as $format_id => $format_name) {
  250. $format = 'format' . $format_id;
  251. // Fetch the profile associated to this text format.
  252. $profile = wysiwyg_get_profile($format_id);
  253. if ($profile) {
  254. // Initialize default settings, defaulting to 'none' editor.
  255. $settings[$format] = array(
  256. 'editor' => 'none',
  257. 'status' => 1,
  258. 'toggle' => 1,
  259. );
  260. $loaded = TRUE;
  261. if (isset($profile->settings['add_to_summaries']) && !$profile->settings['add_to_summaries']) {
  262. $settings[$format]['skip_summary'] = 1;
  263. }
  264. $settings[$format]['editor'] = $profile->editor;
  265. $settings[$format]['status'] = (int) wysiwyg_user_get_status($profile);
  266. if (isset($profile->preferences['show_toggle'])) {
  267. $settings[$format]['toggle'] = (int) $profile->preferences['show_toggle'];
  268. }
  269. // Check editor theme (and reset it if not/no longer available).
  270. $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : ''));
  271. // Add plugin settings (first) for this text format.
  272. wysiwyg_add_plugin_settings($profile);
  273. // Add profile settings for this text format.
  274. wysiwyg_add_editor_settings($profile, $theme);
  275. }
  276. }
  277. $element['value']['#attributes']['class'][] = 'wysiwyg';
  278. $element['#attached']['js'][] = array(
  279. 'data' => array(
  280. 'wysiwyg' => array(
  281. 'triggers' => array(
  282. $element['value']['#id'] => $settings,
  283. ),
  284. ),
  285. ),
  286. 'type' => 'setting',
  287. );
  288. return $element;
  289. }
  290. /**
  291. * Determine the profile to use for a given input format id.
  292. *
  293. * This function also performs sanity checks for the configured editor in a
  294. * profile to ensure that we do not load a malformed editor.
  295. *
  296. * @param $format
  297. * The internal id of an input format.
  298. *
  299. * @return
  300. * A wysiwyg profile.
  301. *
  302. * @see wysiwyg_load_editor(), wysiwyg_get_editor()
  303. */
  304. function wysiwyg_get_profile($format) {
  305. if ($profile = wysiwyg_profile_load($format)) {
  306. if (wysiwyg_load_editor($profile)) {
  307. return $profile;
  308. }
  309. }
  310. return FALSE;
  311. }
  312. /**
  313. * Load an editor library and initialize basic Wysiwyg settings.
  314. *
  315. * @param $profile
  316. * A wysiwyg editor profile.
  317. *
  318. * @return
  319. * TRUE if the editor has been loaded, FALSE if not.
  320. *
  321. * @see wysiwyg_get_profile()
  322. */
  323. function wysiwyg_load_editor($profile) {
  324. static $settings_added;
  325. static $loaded = array();
  326. $path = drupal_get_path('module', 'wysiwyg');
  327. $name = $profile->editor;
  328. // Library files must be loaded only once.
  329. if (!isset($loaded[$name])) {
  330. // Load editor.
  331. $editor = wysiwyg_get_editor($name);
  332. if ($editor) {
  333. $default_library_options = array(
  334. 'type' => 'file',
  335. 'scope' => 'header',
  336. 'defer' => FALSE,
  337. 'cache' => TRUE,
  338. 'preprocess' => TRUE,
  339. );
  340. // Determine library files to load.
  341. // @todo Allow to configure the library/execMode to use.
  342. if (isset($profile->preferences['library']) && isset($editor['libraries'][$profile->preferences['library']])) {
  343. $library = $profile->preferences['library'];
  344. $files = $editor['libraries'][$library]['files'];
  345. }
  346. else {
  347. // Fallback to the first defined library by default (external libraries can change).
  348. $library = key($editor['libraries']);
  349. $files = array_shift($editor['libraries']);
  350. $files = $files['files'];
  351. }
  352. // Check whether the editor requires an initialization script.
  353. if (!empty($editor['init callback'])) {
  354. $init = $editor['init callback']($editor, $library, $profile);
  355. if (!empty($init)) {
  356. // Build a file for each of the editors to hold the init scripts.
  357. // @todo Aggregate all initialization scripts into one file.
  358. $uri = 'public://js/wysiwyg/wysiwyg_' . $name . '_' . drupal_hash_base64($init) . '.js';
  359. $init_exists = file_exists($uri);
  360. if (!$init_exists) {
  361. $js_path = dirname($uri);
  362. file_prepare_directory($js_path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  363. }
  364. // Attempt to create the file, or fall back to an inline script (which
  365. // will not work in Ajax calls).
  366. if (!$init_exists && !file_unmanaged_save_data($init, $uri, FILE_EXISTS_REPLACE)) {
  367. drupal_add_js($init, array('type' => 'inline') + $default_library_options);
  368. }
  369. else {
  370. drupal_add_js($uri, $default_library_options);
  371. }
  372. }
  373. }
  374. foreach ($files as $file => $options) {
  375. if (is_array($options)) {
  376. $options += $default_library_options;
  377. drupal_add_js($editor['library path'] . '/' . $file, $options);
  378. }
  379. else {
  380. drupal_add_js($editor['library path'] . '/' . $options);
  381. }
  382. }
  383. // If editor defines an additional load callback, invoke it.
  384. // @todo Isn't the settings callback sufficient?
  385. if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
  386. $editor['load callback']($editor, $library);
  387. }
  388. // Load JavaScript integration files for this editor.
  389. $files = array();
  390. if (isset($editor['js files'])) {
  391. $files = $editor['js files'];
  392. }
  393. foreach ($files as $file) {
  394. drupal_add_js($editor['js path'] . '/' . $file);
  395. }
  396. // Load CSS stylesheets for this editor.
  397. $files = array();
  398. if (isset($editor['css files'])) {
  399. $files = $editor['css files'];
  400. }
  401. foreach ($files as $file) {
  402. drupal_add_css($editor['css path'] . '/' . $file);
  403. }
  404. $loaded[$name] = TRUE;
  405. }
  406. else {
  407. $loaded[$name] = FALSE;
  408. }
  409. }
  410. // Check if settings were already added on the page that makes an AJAX call.
  411. if (isset($_POST['ajax_page_state']) && !empty($_POST['ajax_page_state']['js'][$path . '/wysiwyg.js'])) {
  412. $settings_added = TRUE;
  413. }
  414. // Add basic Wysiwyg settings if any editor has been added.
  415. if (!isset($settings_added) && $loaded[$name]) {
  416. drupal_add_js(array('wysiwyg' => array(
  417. 'configs' => array(),
  418. 'plugins' => array(),
  419. 'disable' => t('Disable rich-text'),
  420. 'enable' => t('Enable rich-text'),
  421. )), 'setting');
  422. // Initialize our namespaces in the *header* to do not force editor
  423. // integration scripts to check and define Drupal.wysiwyg on its own.
  424. drupal_add_js($path . '/wysiwyg.init.js', array('group' => JS_LIBRARY));
  425. // The 'none' editor is a special editor implementation, allowing us to
  426. // attach and detach regular Drupal behaviors just like any other editor.
  427. drupal_add_js($path . '/editors/js/none.js');
  428. // Add wysiwyg.js to the footer to ensure it's executed after the
  429. // Drupal.settings array has been rendered and populated. Also, since editor
  430. // library initialization functions must be loaded first by the browser,
  431. // and Drupal.wysiwygInit() must be executed AFTER editors registered
  432. // their callbacks and BEFORE Drupal.behaviors are applied, this must come
  433. // last.
  434. drupal_add_js($path . '/wysiwyg.js', array('scope' => 'footer'));
  435. $settings_added = TRUE;
  436. }
  437. return $loaded[$name];
  438. }
  439. /**
  440. * Add editor settings for a given input format.
  441. */
  442. function wysiwyg_add_editor_settings($profile, $theme) {
  443. static $formats = array();
  444. if (!isset($formats[$profile->format])) {
  445. $config = wysiwyg_get_editor_config($profile, $theme);
  446. // drupal_to_js() does not properly convert numeric array keys, so we need
  447. // to use a string instead of the format id.
  448. if ($config) {
  449. drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format' . $profile->format => $config)))), 'setting');
  450. }
  451. $formats[$profile->format] = TRUE;
  452. }
  453. }
  454. /**
  455. * Add settings for external plugins.
  456. *
  457. * Plugins can be used in multiple profiles, but not necessarily in all. Because
  458. * of that, we need to process plugins for each profile, even if most of their
  459. * settings are not stored per profile.
  460. *
  461. * Implementations of hook_wysiwyg_plugin() may execute different code for each
  462. * editor. Therefore, we have to invoke those implementations for each editor,
  463. * but process the resulting plugins separately for each profile.
  464. *
  465. * Drupal plugins differ to native plugins in that they have plugin-specific
  466. * definitions and settings, which need to be processed only once. But they are
  467. * also passed to the editor to prepare settings specific to the editor.
  468. * Therefore, we load and process the Drupal plugins only once, and hand off the
  469. * effective definitions for each profile to the editor.
  470. *
  471. * @param $profile
  472. * A wysiwyg editor profile.
  473. *
  474. * @todo Rewrite wysiwyg_process_form() to build a registry of effective
  475. * profiles in use, so we can process plugins in multiple profiles in one shot
  476. * and simplify this entire function.
  477. */
  478. function wysiwyg_add_plugin_settings($profile) {
  479. static $plugins = array();
  480. static $processed_plugins = array();
  481. static $processed_formats = array();
  482. // Each input format must only processed once.
  483. // @todo ...as long as we do not have multiple profiles per format.
  484. if (isset($processed_formats[$profile->format])) {
  485. return;
  486. }
  487. $processed_formats[$profile->format] = TRUE;
  488. $editor = wysiwyg_get_editor($profile->editor);
  489. // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
  490. // and Drupal plugins provided via hook_wysiwyg_include_directory().
  491. if (!array_key_exists($editor['name'], $plugins)) {
  492. $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
  493. }
  494. // Nothing to do, if there are no plugins.
  495. if (empty($plugins[$editor['name']])) {
  496. return;
  497. }
  498. // Determine name of proxy plugin for Drupal plugins.
  499. $proxy = (isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '');
  500. // Process native editor plugins.
  501. $profile_plugins_native = array();
  502. foreach ($plugins[$editor['name']] as $plugin => $meta) {
  503. // Skip Drupal plugins (handled below) and 'core' functionality.
  504. if ($plugin === $proxy || $plugin === 'default') {
  505. continue;
  506. }
  507. // Only keep native plugins that are enabled in this profile.
  508. if (isset($profile->settings['buttons'][$plugin])) {
  509. $profile_plugins_native[$plugin] = $meta;
  510. if (!isset($processed_plugins[$editor['name']][$plugin])) {
  511. if (isset($editor['plugin meta callback'])) {
  512. // Invoke the editor's plugin meta callback, so it can populate the
  513. // global metadata for native plugins with required values.
  514. $meta['name'] = $plugin;
  515. if (($native_meta = call_user_func($editor['plugin meta callback'], $editor, $meta))) {
  516. drupal_add_js(array('wysiwyg' => array('plugins' => array('native' => array($editor['name'] => array($plugin => $native_meta))))), 'setting');
  517. }
  518. }
  519. $processed_plugins[$editor['name']][$plugin] = $meta;
  520. }
  521. }
  522. }
  523. if (!empty($profile_plugins_native) && isset($editor['plugin settings callback'])) {
  524. // Invoke the editor's plugin settings callback, so it can populate the
  525. // format specific settings for native plugins with required values.
  526. if (($settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native))) {
  527. drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('native' => $settings_native)))), 'setting');
  528. }
  529. }
  530. // Process Drupal plugins.
  531. if ($proxy && isset($editor['proxy plugin settings callback'])) {
  532. $profile_plugins_drupal = array();
  533. foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
  534. if (isset($profile->settings['buttons'][$proxy][$plugin])) {
  535. // JavaScript and plugin-specific settings for Drupal plugins must be
  536. // loaded and processed only once. Plugin information is cached
  537. // statically to pass it to the editor's proxy plugin settings callback.
  538. if (!isset($processed_plugins[$proxy][$plugin])) {
  539. $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;
  540. // Load the Drupal plugin's JavaScript.
  541. drupal_add_js($meta['js path'] . '/' . $meta['js file']);
  542. // Add plugin-specific settings.
  543. $settings = (isset($meta['settings']) ? $meta['settings'] : array());
  544. $settings['title'] = $meta['title'];
  545. $settings['icon'] = base_path() . $meta['icon path'] . '/' . $meta['icon file'];
  546. if (!empty($meta['css path']) && !empty($meta['css file'])) {
  547. $settings['css'] = base_path() . $meta['css path'] . '/' . $meta['css file'];
  548. }
  549. drupal_add_js(array('wysiwyg' => array('plugins' => array('drupal' => array($plugin => $settings)))), 'setting');
  550. }
  551. else {
  552. $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
  553. }
  554. }
  555. }
  556. // Invoke the editor's proxy plugin settings callback, so it can populate
  557. // the settings for Drupal plugins with custom, required values.
  558. $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);
  559. if ($settings_drupal) {
  560. drupal_add_js(array('wysiwyg' => array('plugins' => array('format' . $profile->format => array('drupal' => $settings_drupal)))), 'setting');
  561. }
  562. }
  563. }
  564. /**
  565. * Retrieve available themes for an editor.
  566. *
  567. * Editor themes control the visual presentation of an editor.
  568. *
  569. * @param $profile
  570. * A wysiwyg editor profile; passed/altered by reference.
  571. * @param $selected_theme
  572. * An optional theme name that ought to be used.
  573. *
  574. * @return
  575. * An array of theme names, or a single, checked theme name if $selected_theme
  576. * was given.
  577. */
  578. function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
  579. static $themes = array();
  580. if (!isset($themes[$profile->editor])) {
  581. $editor = wysiwyg_get_editor($profile->editor);
  582. if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
  583. $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
  584. }
  585. // Fallback to 'default' otherwise.
  586. else {
  587. $themes[$editor['name']] = array('default');
  588. }
  589. }
  590. // Check optional $selected_theme argument, if given.
  591. if (isset($selected_theme)) {
  592. // If the passed theme name does not exist, use the first available.
  593. if (!in_array($selected_theme, $themes[$profile->editor])) {
  594. $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
  595. }
  596. }
  597. return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
  598. }
  599. /**
  600. * Return plugin metadata from the plugin registry.
  601. *
  602. * @param $editor_name
  603. * The internal name of an editor to return plugins for.
  604. *
  605. * @return
  606. * An array for each plugin.
  607. */
  608. function wysiwyg_get_plugins($editor_name) {
  609. $plugins = array();
  610. if (!empty($editor_name)) {
  611. $editor = wysiwyg_get_editor($editor_name);
  612. // Add internal editor plugins.
  613. if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
  614. $plugins = $editor['plugin callback']($editor);
  615. }
  616. // Add editor plugins provided via hook_wysiwyg_plugin().
  617. $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
  618. // Add API plugins provided by Drupal modules.
  619. // @todo We need to pass the filepath to the plugin icon for Drupal plugins.
  620. if (isset($editor['proxy plugin'])) {
  621. $plugins += $editor['proxy plugin'];
  622. $proxy = key($editor['proxy plugin']);
  623. foreach (wysiwyg_get_all_plugins() as $plugin_name => $info) {
  624. $plugins[$proxy]['buttons'][$plugin_name] = $info['title'];
  625. }
  626. }
  627. }
  628. return $plugins;
  629. }
  630. /**
  631. * Return an array of initial editor settings for a Wysiwyg profile.
  632. */
  633. function wysiwyg_get_editor_config($profile, $theme) {
  634. $editor = wysiwyg_get_editor($profile->editor);
  635. $settings = array();
  636. $installed_version = $editor['installed version'];
  637. $settings = $profile->settings;
  638. if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
  639. if (!empty($profile->preferences['version']) && !empty($installed_version)) {
  640. $profile_version = $profile->preferences['version'];
  641. $version_status = version_compare($profile_version, $installed_version);
  642. if ($version_status !== 0) {
  643. // Installed version is different from profile version. Silently migrate
  644. // the stored editor settings to the installed version if possible.
  645. $migrated = FALSE;
  646. if (!empty($editor['migrate settings callback']) && function_exists($editor['migrate settings callback'])) {
  647. $migrated = $editor['migrate settings callback']($settings, $editor, $profile_version, $installed_version);
  648. }
  649. }
  650. }
  651. $settings = $editor['settings callback']($editor, $settings, $theme);
  652. // Allow other modules to alter the editor settings for this format.
  653. $context = array('editor' => $editor, 'profile' => $profile, 'theme' => $theme);
  654. drupal_alter('wysiwyg_editor_settings', $settings, $context);
  655. }
  656. return $settings;
  657. }
  658. /**
  659. * Retrieve stylesheets for HTML/IFRAME-based editors.
  660. *
  661. * This assumes that the content editing area only needs stylesheets defined
  662. * for the scope 'theme'.
  663. *
  664. * Performs a background request of a dummy page to cache as many of a theme's
  665. * stylesheets as possible before returning the cached list.
  666. *
  667. * Note: if set to explicitly use the current admin theme by name, no access
  668. * check on the 'view the administration theme' permission is performed.
  669. *
  670. * @param $theme
  671. * The id of a theme to get stylesheets for. Defaults to the current theme.
  672. *
  673. * @return
  674. * An array containing CSS files, including proper base path.
  675. */
  676. function wysiwyg_get_css($theme = NULL) {
  677. // Default to the node edit theme, if the user has access.
  678. if (empty($theme)) {
  679. $theme = variable_get('node_admin_theme') && user_access('view the administration theme') ? variable_get('admin_theme') : variable_get('theme_default', 'bartik');
  680. }
  681. // If set to use the admin theme, ensure the user has access.
  682. elseif ($theme == 'wysiwyg_theme_admin' && user_access('view the administration theme') && $admin_theme = variable_get('admin_theme')) {
  683. $theme = $admin_theme;
  684. }
  685. // Make sure the theme system is initialized.
  686. $themes = list_themes();
  687. if (!isset($themes[$theme])) {
  688. drupal_theme_initialize();
  689. }
  690. // Ensure the selected theme is enabled (or is the admin theme).
  691. if (!drupal_theme_access($theme)) {
  692. $theme = variable_get('theme_default', 'bartik');
  693. }
  694. $cached = cache_get('wysiwyg_css');
  695. $css = array();
  696. // Trigger a cache update if:
  697. // this is NOT the wysiwyg_theme page (avoid loop),
  698. // the cache is empty or does not have the current theme,
  699. // the CSS/JS cache-busting query string has changed,
  700. // or the theme's aggregation state has changed.
  701. $update_cache = strpos(current_path(), 'wysiwyg_theme/') === FALSE && (
  702. !$cached || (
  703. empty($cached->data[$theme])
  704. || $cached->data[$theme]['aggregated'] != variable_get('preprocess_css', FALSE))
  705. || $cached->data['_css_js_query_string'] != variable_get('css_js_query_string'));
  706. if ($update_cache) {
  707. $url = url('wysiwyg_theme/' . $theme, array('absolute' => TRUE, 'max_redirects' => 0));
  708. $response = drupal_http_request($url);;
  709. $cached = cache_get('wysiwyg_css');
  710. if ($cached && !empty($cached->data[$theme])) {
  711. $css = $cached->data[$theme]['files'];
  712. }
  713. }
  714. elseif (!empty($cached->data[$theme])) {
  715. $css = $cached->data[$theme]['files'];
  716. }
  717. return $css;
  718. }
  719. /**
  720. * Implements hook_themes_enabled().
  721. */
  722. function wysiwyg_themes_enabled($theme_list) {
  723. $cached = cache_get('wysiwyg_css');
  724. foreach ($theme_list as $theme) {
  725. if ($cached && !empty($cached->data)) {
  726. $css = $cached->data;
  727. unset($css[$theme]);
  728. }
  729. }
  730. cache_set('wysiwyg_css', $css);
  731. }
  732. /**
  733. * Implements hook_form_FORM_ID_alter().
  734. *
  735. * Alters the system's theme settings form to react when themes change.
  736. */
  737. function wysiwyg_form_system_theme_settings_alter(&$form, &$form_state, $form_id) {
  738. $form['#submit'][] = '_wysiwyg_system_theme_settings_submit';
  739. }
  740. /**
  741. * Submit callback for the theme settings form.
  742. *
  743. * Removes the edited theme from the cache.
  744. */
  745. function _wysiwyg_system_theme_settings_submit($form, &$form_state) {
  746. $theme = NULL;
  747. if ($form_state['build_info']['form_id'] == 'system_theme_settings' && !empty($form_state['build_info']['args'])) {
  748. $theme = $form_state['build_info']['args'][0];
  749. }
  750. if ($theme !== NULL) {
  751. $cached = cache_get('wysiwyg_css');
  752. if ($cached && !empty($cached->data)) {
  753. $css = $cached->data;
  754. unset($css[$theme]);
  755. cache_set('wysiwyg_css', $css);
  756. }
  757. }
  758. wysiwyg_get_css($theme);
  759. }
  760. /**
  761. * Loads a profile for a given text format.
  762. *
  763. * Since there are commonly not many text formats, and each text format-enabled
  764. * form element will possibly have to load every single profile, all existing
  765. * profiles are loaded and cached once to reduce the amount of database queries.
  766. *
  767. * @param $format
  768. * The machine-name of a text format.
  769. *
  770. * @return A profile if found, else FALSE.
  771. */
  772. function wysiwyg_profile_load($format) {
  773. $profiles = wysiwyg_profile_load_all();
  774. return (isset($profiles[$format]) ? $profiles[$format] : FALSE);
  775. }
  776. /**
  777. * Loads all profiles.
  778. *
  779. * @return An array of profiles keyed by format name.
  780. */
  781. function wysiwyg_profile_load_all() {
  782. // entity_load(..., FALSE) does not re-use its own static cache upon
  783. // repetitive calls, so a custom static cache is required.
  784. // @see wysiwyg_entity_info()
  785. $profiles = &drupal_static(__FUNCTION__);
  786. if (!isset($profiles)) {
  787. // Additional database cache to support alternative caches like memcache.
  788. if ($cached = cache_get('wysiwyg_profiles')) {
  789. $profiles = $cached->data;
  790. }
  791. else {
  792. $profiles = entity_load('wysiwyg_profile', FALSE);
  793. $formats = filter_formats();
  794. foreach ($profiles as $key => $profile) {
  795. if (empty($profile->editor) || !isset($formats[$profile->format])) {
  796. unset($profiles[$key]);
  797. }
  798. }
  799. cache_set('wysiwyg_profiles', $profiles);
  800. }
  801. }
  802. return $profiles;
  803. }
  804. /**
  805. * Deletes a profile from the database.
  806. */
  807. function wysiwyg_profile_delete($profile) {
  808. db_delete('wysiwyg')
  809. ->condition('format', $profile->format)
  810. ->execute();
  811. // Clear the editing caches.
  812. if (module_exists('ctools')) {
  813. ctools_include('object-cache');
  814. ctools_object_cache_clear_all('wysiwyg_profile', $profile->name);
  815. }
  816. else {
  817. cache_clear_all('wysiwyg_profile:' . $profile->name, 'cache');
  818. }
  819. wysiwyg_profile_cache_clear();
  820. }
  821. /**
  822. * Specialized menu callback to load a profile and check its locked status.
  823. *
  824. * @param $name
  825. * The machine name of the profile.
  826. *
  827. * @return
  828. * The profile object, with a "locked" property indicating whether or not
  829. * someone else is already editing the profile.
  830. */
  831. function wysiwyg_ui_profile_cache_load($format) {
  832. $original_profile = wysiwyg_profile_load($format);
  833. $profile = FALSE;
  834. $name = ($original_profile ? $original_profile->name : 'format' . $format);
  835. $profile = wysiwyg_ui_profile_cache_get($name);
  836. if (empty($profile)) {
  837. $profile = $original_profile;
  838. }
  839. if (!empty($profile)) {
  840. $profile->editing = TRUE;
  841. return $profile;
  842. }
  843. return FALSE;
  844. }
  845. /**
  846. * Specialized cache function to load a profile from the editing cache.
  847. *
  848. * @param $name
  849. * The name of a profile to load. Currently the format name prefixed by
  850. * 'format'.
  851. * @return
  852. * The profile object, with a "locked" property indicating whether or not
  853. * someone else is already editing the profile, or FALSE if not cached.
  854. */
  855. function wysiwyg_ui_profile_cache_get($name) {
  856. $profile = FALSE;
  857. if (module_exists('ctools')) {
  858. ctools_include('object-cache');
  859. $profile = ctools_object_cache_get('wysiwyg_profile', $name);
  860. if ($profile) {
  861. $profile->locked = ctools_object_cache_test('wysiwyg_profile', $name);
  862. }
  863. }
  864. else {
  865. // Fall back on simple caching in its own bin without locking.
  866. $cached = cache_get('wysiwyg_profile:' . $name);
  867. if ($cached) {
  868. $profile = $cached->data;
  869. $profile->locked = FALSE;
  870. }
  871. }
  872. return $profile;
  873. }
  874. /**
  875. * Specialized cache function to add a profile to the editing cache.
  876. */
  877. function wysiwyg_ui_profile_cache_set(&$profile) {
  878. if (!empty($profile->locked)) {
  879. drupal_set_message(t('Changes can not be made to a locked profile.'), 'error');
  880. return;
  881. }
  882. $profile->changed = TRUE;
  883. if (module_exists('ctools')) {
  884. ctools_include('object-cache');
  885. ctools_object_cache_set('wysiwyg_profile', $profile->name, $profile);
  886. }
  887. else {
  888. cache_set('wysiwyg_profile:' . $profile->name, $profile);
  889. }
  890. }
  891. /**
  892. * Clear all Wysiwyg profile caches.
  893. */
  894. function wysiwyg_profile_cache_clear() {
  895. entity_get_controller('wysiwyg_profile')->resetCache();
  896. drupal_static_reset('wysiwyg_profile_load_all');
  897. cache_clear_all('wysiwyg_profiles', 'cache');
  898. }
  899. /**
  900. * Implements hook_form_FORM_ID_alter().
  901. */
  902. function wysiwyg_form_user_profile_form_alter(&$form, &$form_state, $form_id) {
  903. if ($form['#user_category'] != 'account') {
  904. return;
  905. }
  906. $account = $form['#user'];
  907. $user_formats = filter_formats($account);
  908. $options = array();
  909. $options_default = array();
  910. foreach (wysiwyg_profile_load_all() as $format => $profile) {
  911. // Only show profiles that have user_choose enabled.
  912. if (!empty($profile->preferences['user_choose']) && isset($user_formats[$format])) {
  913. $options[$format] = check_plain($user_formats[$format]->name);
  914. if (wysiwyg_user_get_status($profile, $account)) {
  915. $options_default[] = $format;
  916. }
  917. }
  918. }
  919. if (!empty($options)) {
  920. $form['wysiwyg']['wysiwyg_status'] = array(
  921. '#type' => 'checkboxes',
  922. '#title' => t('Text formats enabled for rich-text editing'),
  923. '#options' => $options,
  924. '#default_value' => $options_default,
  925. );
  926. }
  927. }
  928. /**
  929. * Implements hook_user_insert().
  930. *
  931. * Wysiwyg's user preferences are normally not exposed on the user registration
  932. * form, but in case they are manually altered in, we invoke
  933. * wysiwyg_user_update() accordingly.
  934. */
  935. function wysiwyg_user_insert(&$edit, $account, $category) {
  936. wysiwyg_user_update($edit, $account, $category);
  937. }
  938. /**
  939. * Implements hook_user_update().
  940. */
  941. function wysiwyg_user_update(&$edit, $account, $category) {
  942. if (isset($edit['wysiwyg_status'])) {
  943. db_delete('wysiwyg_user')
  944. ->condition('uid', $account->uid)
  945. ->execute();
  946. $query = db_insert('wysiwyg_user')
  947. ->fields(array('uid', 'format', 'status'));
  948. foreach ($edit['wysiwyg_status'] as $format => $status) {
  949. $query->values(array(
  950. 'uid' => $account->uid,
  951. 'format' => $format,
  952. 'status' => (int) (bool) $status,
  953. ));
  954. }
  955. $query->execute();
  956. }
  957. }
  958. function wysiwyg_user_get_status($profile, $account = NULL) {
  959. global $user;
  960. if (!isset($account)) {
  961. $account = $user;
  962. }
  963. // Default wysiwyg editor status information is only required on forms, so we
  964. // do not pre-emptively load and attach this information on every user_load().
  965. if (!isset($account->wysiwyg_status)) {
  966. $account->wysiwyg_status = db_query("SELECT format, status FROM {wysiwyg_user} WHERE uid = :uid", array(
  967. ':uid' => $account->uid,
  968. ))->fetchAllKeyed();
  969. }
  970. if (!empty($profile->preferences['user_choose']) && isset($account->wysiwyg_status[$profile->format])) {
  971. $status = $account->wysiwyg_status[$profile->format];
  972. }
  973. else {
  974. $status = isset($profile->preferences['default']) ? $profile->preferences['default'] : TRUE;
  975. }
  976. return (bool) $status;
  977. }
  978. /**
  979. * @defgroup wysiwyg_api Wysiwyg API
  980. * @{
  981. *
  982. * @todo Forked from Panels; abstract into a separate API module that allows
  983. * contrib modules to define supported include/plugin types.
  984. */
  985. /**
  986. * Return library information for a given editor.
  987. *
  988. * @param $name
  989. * The internal name of an editor.
  990. *
  991. * @return
  992. * The library information for the editor, or FALSE if $name is unknown or not
  993. * installed properly.
  994. */
  995. function wysiwyg_get_editor($name) {
  996. $editors = wysiwyg_get_all_editors();
  997. return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
  998. }
  999. /**
  1000. * Compile a list holding all supported editors including installed editor version information.
  1001. */
  1002. function wysiwyg_get_all_editors() {
  1003. static $editors;
  1004. if (isset($editors)) {
  1005. return $editors;
  1006. }
  1007. $editors = wysiwyg_load_includes('editors', 'editor');
  1008. foreach ($editors as $editor => $properties) {
  1009. // Fill in required properties.
  1010. $editors[$editor] += array(
  1011. 'title' => '',
  1012. 'vendor url' => '',
  1013. 'download url' => '',
  1014. 'editor path' => wysiwyg_get_path($editors[$editor]['name']),
  1015. 'library path' => wysiwyg_get_path($editors[$editor]['name']),
  1016. 'libraries' => array(),
  1017. 'version callback' => NULL,
  1018. 'themes callback' => NULL,
  1019. 'settings form callback' => NULL,
  1020. 'settings callback' => NULL,
  1021. 'plugin callback' => NULL,
  1022. 'plugin settings callback' => NULL,
  1023. 'versions' => array(),
  1024. 'js path' => $editors[$editor]['path'] . '/js',
  1025. 'css path' => $editors[$editor]['path'] . '/css',
  1026. );
  1027. // Check whether library is present.
  1028. if (!($editors[$editor]['installed'] = file_exists($editors[$editor]['library path']))) {
  1029. continue;
  1030. }
  1031. $installed_version = NULL;
  1032. // Detect library version.
  1033. if (function_exists($editors[$editor]['version callback'])) {
  1034. $installed_version = $editors[$editor]['installed version'] = $editors[$editor]['version callback']($editors[$editor]);
  1035. }
  1036. if (empty($installed_version)) {
  1037. $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
  1038. $editors[$editor]['installed'] = FALSE;
  1039. continue;
  1040. }
  1041. $editors[$editor]['installed version verified'] = TRUE;
  1042. if (!empty($editors[$editor]['verified version range'])) {
  1043. $version_range = $editors[$editor]['verified version range'];
  1044. if (version_compare($installed_version, $version_range[0], '<') || version_compare($installed_version, $version_range[1], '>')) {
  1045. $editors[$editor]['installed version verified'] = FALSE;
  1046. }
  1047. }
  1048. // Determine to which supported version the installed version maps.
  1049. ksort($editors[$editor]['versions']);
  1050. $version = 0;
  1051. foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
  1052. if (version_compare($installed_version, $supported_version, '>=')) {
  1053. $version = $supported_version;
  1054. }
  1055. }
  1056. if (!$version) {
  1057. $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $installed_version, '%editor' => $editors[$editor]['title']));
  1058. $editors[$editor]['installed'] = FALSE;
  1059. continue;
  1060. }
  1061. // Apply library version specific definitions and overrides.
  1062. $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
  1063. unset($editors[$editor]['versions']);
  1064. }
  1065. drupal_alter('wysiwyg_editor', $editors);
  1066. return $editors;
  1067. }
  1068. /**
  1069. * Invoke hook_wysiwyg_plugin() in all modules.
  1070. */
  1071. function wysiwyg_get_all_plugins() {
  1072. static $plugins;
  1073. if (isset($plugins)) {
  1074. return $plugins;
  1075. }
  1076. $plugins = wysiwyg_load_includes('plugins', 'plugin');
  1077. foreach ($plugins as $name => $properties) {
  1078. $plugin = &$plugins[$name];
  1079. // Fill in required/default properties.
  1080. $plugin += array(
  1081. 'title' => $plugin['name'],
  1082. 'vendor url' => '',
  1083. 'js path' => $plugin['path'] . '/' . $plugin['name'],
  1084. 'js file' => $plugin['name'] . '.js',
  1085. 'css path' => $plugin['path'] . '/' . $plugin['name'],
  1086. 'css file' => $plugin['name'] . '.css',
  1087. 'icon path' => $plugin['path'] . '/' . $plugin['name'] . '/images',
  1088. 'icon file' => $plugin['name'] . '.png',
  1089. 'dialog path' => $plugin['name'],
  1090. 'dialog settings' => array(),
  1091. 'settings callback' => NULL,
  1092. 'settings form callback' => NULL,
  1093. );
  1094. // Fill in default settings.
  1095. $plugin['settings'] += array(
  1096. 'path' => base_path() . $plugin['path'] . '/' . $plugin['name'],
  1097. );
  1098. // Check whether library is present.
  1099. if (!($plugin['installed'] = file_exists($plugin['js path'] . '/' . $plugin['js file']))) {
  1100. continue;
  1101. }
  1102. }
  1103. return $plugins;
  1104. }
  1105. /**
  1106. * Load include files for wysiwyg implemented by all modules.
  1107. *
  1108. * @param $type
  1109. * The type of includes to search for, can be 'editors'.
  1110. * @param $hook
  1111. * The hook name to invoke.
  1112. * @param $file
  1113. * An optional include file name without .inc extension to limit the search to.
  1114. *
  1115. * @see wysiwyg_get_directories(), _wysiwyg_process_include()
  1116. */
  1117. function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
  1118. // Determine implementations.
  1119. $directories = wysiwyg_get_directories($type);
  1120. $directories['wysiwyg'] = drupal_get_path('module', 'wysiwyg') . '/' . $type;
  1121. $file_list = array();
  1122. foreach ($directories as $module => $path) {
  1123. $file_list[$module] = drupal_system_listing("/{$file}.inc\$/", $path, 'name', 0);
  1124. }
  1125. // Load implementations.
  1126. $info = array();
  1127. foreach (array_filter($file_list) as $module => $files) {
  1128. foreach ($files as $file) {
  1129. include_once './' . $file->uri;
  1130. $result = _wysiwyg_process_include($module, $module . '_' . $file->name, dirname($file->uri), $hook);
  1131. if (is_array($result)) {
  1132. $info = array_merge($info, $result);
  1133. }
  1134. }
  1135. }
  1136. return $info;
  1137. }
  1138. /**
  1139. * Helper function to build paths to libraries.
  1140. *
  1141. * @param $library
  1142. * The external library name to return the path for.
  1143. * @param $base_path
  1144. * Whether to prefix the resulting path with base_path().
  1145. *
  1146. * @return
  1147. * The path to the specified library.
  1148. *
  1149. * @ingroup libraries
  1150. */
  1151. function wysiwyg_get_path($library, $base_path = FALSE) {
  1152. static $libraries;
  1153. if (!isset($libraries)) {
  1154. $libraries = wysiwyg_get_libraries();
  1155. }
  1156. if (!isset($libraries[$library])) {
  1157. // Most often, external libraries can be shared across multiple sites.
  1158. return 'sites/all/libraries/' . $library;
  1159. }
  1160. $path = ($base_path ? base_path() : '');
  1161. $path .= $libraries[$library];
  1162. return $path;
  1163. }
  1164. /**
  1165. * Return an array of library directories.
  1166. *
  1167. * Returns an array of library directories from the all-sites directory
  1168. * (i.e. sites/all/libraries/), the profiles directory, and site-specific
  1169. * directory (i.e. sites/somesite/libraries/). The returned array will be keyed
  1170. * by the library name. Site-specific libraries are prioritized over libraries
  1171. * in the default directories. That is, if a library with the same name appears
  1172. * in both the site-wide directory and site-specific directory, only the
  1173. * site-specific version will be listed.
  1174. *
  1175. * @return
  1176. * A list of library directories.
  1177. *
  1178. * @ingroup libraries
  1179. */
  1180. function wysiwyg_get_libraries() {
  1181. if (function_exists('libraries_get_libraries')) {
  1182. $directories = libraries_get_libraries();
  1183. }
  1184. else {
  1185. global $profile;
  1186. // When this function is called during Drupal's initial installation process,
  1187. // the name of the profile that is about to be installed is stored in the
  1188. // global $profile variable. At all other times, the regular system variable
  1189. // contains the name of the current profile, and we can call variable_get()
  1190. // to determine the profile.
  1191. if (!isset($profile)) {
  1192. $profile = variable_get('install_profile', 'default');
  1193. }
  1194. $directory = 'libraries';
  1195. $searchdir = array();
  1196. $config = conf_path();
  1197. // The 'profiles' directory contains pristine collections of modules and
  1198. // themes as organized by a distribution. It is pristine in the same way
  1199. // that /modules is pristine for core; users should avoid changing anything
  1200. // there in favor of sites/all or sites/<domain> directories.
  1201. if (file_exists("profiles/$profile/$directory")) {
  1202. $searchdir[] = "profiles/$profile/$directory";
  1203. }
  1204. // Always search sites/all/*.
  1205. $searchdir[] = 'sites/all/' . $directory;
  1206. // Also search sites/<domain>/*.
  1207. if (file_exists("$config/$directory")) {
  1208. $searchdir[] = "$config/$directory";
  1209. }
  1210. // Retrieve list of directories.
  1211. // @todo Core: Allow to scan for directories.
  1212. $directories = array();
  1213. $nomask = array('CVS');
  1214. foreach ($searchdir as $dir) {
  1215. if (is_dir($dir) && $handle = opendir($dir)) {
  1216. while (FALSE !== ($file = readdir($handle))) {
  1217. if (!in_array($file, $nomask) && $file[0] != '.') {
  1218. if (is_dir("$dir/$file")) {
  1219. $directories[$file] = "$dir/$file";
  1220. }
  1221. }
  1222. }
  1223. closedir($handle);
  1224. }
  1225. }
  1226. }
  1227. return $directories;
  1228. }
  1229. /**
  1230. * Return a list of directories by modules implementing wysiwyg_include_directory().
  1231. *
  1232. * @param $plugintype
  1233. * The type of a plugin; can be 'editors'.
  1234. *
  1235. * @return
  1236. * An array containing module names suffixed with '_' and their defined
  1237. * directory.
  1238. *
  1239. * @see wysiwyg_load_includes(), _wysiwyg_process_include()
  1240. */
  1241. function wysiwyg_get_directories($plugintype) {
  1242. $directories = array();
  1243. foreach (module_implements('wysiwyg_include_directory') as $module) {
  1244. $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
  1245. if (isset($result) && is_string($result)) {
  1246. $directories[$module] = drupal_get_path('module', $module) . '/' . $result;
  1247. }
  1248. }
  1249. return $directories;
  1250. }
  1251. /**
  1252. * Create a placeholder structure for JavaScript callbacks.
  1253. *
  1254. * @param $name
  1255. * A string with the name of the callback, use 'object.subobject.method'
  1256. * syntax for methods in nested objects.
  1257. * @param $context
  1258. * An optional string with the name of an object for overriding 'this' inside
  1259. * the function. Use 'object.subobject' syntax for nested objects. Defaults to
  1260. * the window object.
  1261. *
  1262. * @return
  1263. * An array with placeholder information for creating a JavaScript function
  1264. * reference on the client.
  1265. */
  1266. function wysiwyg_wrap_js_callback($name, $context = NULL) {
  1267. $obj = array(
  1268. 'drupalWysiwygType' => 'callback',
  1269. 'name' => $name,
  1270. );
  1271. if ($context) {
  1272. $obj['context'] = $context;
  1273. }
  1274. return $obj;
  1275. }
  1276. /**
  1277. * Create a placeholder structure for JavaScript RegExp objects.
  1278. *
  1279. * @param $regexp
  1280. * A JavaScript Regular Expression as a string, without / wrappers.
  1281. * @param $modifiers
  1282. * An optional string with modifiers for the RegExp object.
  1283. *
  1284. * @return
  1285. * An array with placeholder information for creating a JavaScript RegExp
  1286. * object on the client.
  1287. */
  1288. function wysiwyg_wrap_js_regexp($regexp, $modifiers = NULL) {
  1289. $obj = array(
  1290. 'drupalWysiwygType' => 'regexp',
  1291. 'regexp' => $regexp,
  1292. );
  1293. if ($modifiers) {
  1294. $obj['modifiers'] = $modifiers;
  1295. }
  1296. return $obj;
  1297. }
  1298. /**
  1299. * Process a single hook implementation of a wysiwyg editor.
  1300. *
  1301. * @param $module
  1302. * The module that owns the hook.
  1303. * @param $identifier
  1304. * Either the module or 'wysiwyg_' . $file->name
  1305. * @param $hook
  1306. * The name of the hook being invoked.
  1307. */
  1308. function _wysiwyg_process_include($module, $identifier, $path, $hook) {
  1309. $function = $identifier . '_' . $hook;
  1310. if (!function_exists($function)) {
  1311. return NULL;
  1312. }
  1313. $result = $function();
  1314. if (!isset($result) || !is_array($result)) {
  1315. return NULL;
  1316. }
  1317. // Fill in defaults.
  1318. foreach ($result as $editor => $properties) {
  1319. $result[$editor]['module'] = $module;
  1320. $result[$editor]['name'] = $editor;
  1321. $result[$editor]['path'] = $path;
  1322. }
  1323. return $result;
  1324. }
  1325. /**
  1326. * @} End of "defgroup wysiwyg_api".
  1327. */
  1328. /**
  1329. * Implements hook_features_api().
  1330. */
  1331. function wysiwyg_features_api() {
  1332. return array(
  1333. 'wysiwyg' => array(
  1334. 'name' => t('Wysiwyg profiles'),
  1335. 'default_hook' => 'wysiwyg_default_profiles',
  1336. 'default_file' => FEATURES_DEFAULTS_INCLUDED,
  1337. 'feature_source' => TRUE,
  1338. 'file' => drupal_get_path('module', 'wysiwyg') . '/wysiwyg.features.inc',
  1339. ),
  1340. );
  1341. }