summaryrefslogtreecommitdiffstats
path: root/wysiwyg.module
blob: b63eb76382279de5f2b116594107d27d59f033bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
<?php
// $Id$

/**
 * @file
 * Integrate client-side editors with Drupal.
 */

/**
 * Implementation of hook_menu().
 */
function wysiwyg_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/wysiwyg/profile',
      'title' => t('Wysiwyg'),
      'callback' => 'wysiwyg_admin',
      'description' => t('Configure client-side editor profiles.'),
      'access' => user_access('administer filters'),
    );
  }
  return $items;
}

/**
 * Implementation of hook_help().
 */
function wysiwyg_help($section) {
  switch ($section) {
    case 'admin/settings/wysiwyg/profile':
      if (!arg(4)) {
        $output = '<p>'. t('A Wysiwyg profile can be associated to an input format. A Wysiwyg profile defines which client-side editor is loaded, what buttons or themes are enabled for the editor, how the editor is displayed, and a few other editor-specific functions.') .'</p>';
        return $output;
      }
      break;
  }
}

/**
 * Callback handler for admin pages; menu callback.
 */
function wysiwyg_admin($arg = '', $name = '') {
  require_once drupal_get_path('module', 'wysiwyg') .'/wysiwyg.admin.inc';
  return _wysiwyg_admin($arg, $name);
}

/**
 * Implementation of hook_form_alter().
 *
 * Before Drupal 7, there is no way to easily identify form fields that are
 * input format enabled. As a workaround, we assign a form #after_build
 * processing callback that is executed on all forms after they have been
 * completely built, so form elements are in their effective order
 * and position already.
 *
 * @see wysiwyg_process_form()
 */
function wysiwyg_form_alter($form_id, &$form) {
  $form['#after_build'][] = 'wysiwyg_process_form';
  // Blog module in Drupal 5 uses a wrong/unusual form element key for the input
  // format selector.
  if ($form_id == 'blog_node_form') {
    $form['body_filter']['format'] = $form['body_filter']['filter'];
    unset($form['body_filter']['filter']);
  }
}

/**
 * Process a textarea for Wysiwyg Editor.
 *
 * This way, we can recurse into the form and search for certain, hard-coded
 * elements that have been added by filter_form(). If an input format selector
 * or input format guidelines element is found, we assume that the preceding
 * element is the corresponding textarea and use it's #id for attaching
 * client-side editors.
 *
 * @see wysiwyg_elements(), filter_form()
 */
function wysiwyg_process_form(&$form) {
  // Iterate over element children; resetting array keys to access last index.
  if ($children = array_values(element_children($form))) {
    foreach ($children as $index => $item) {
      $element = &$form[$item];

      // filter_form() always uses the key 'format'. We need a type-agnostic
      // match to prevent false positives. Also, there must have been at least
      // one element on this level.
      if ($item === 'format' && $index > 0) {
        // Make sure we either match a input format selector or input format
        // guidelines (displayed if user has access to one input format only).
        if ((isset($element['#type']) && $element['#type'] == 'fieldset') || isset($element['format']['guidelines'])) {
          // The element before this element is the target form field.
          $field = &$form[$children[$index - 1]];

          // Disable #resizable to avoid resizable behavior to hi-jack the UI,
          // but load the behavior, so the 'none' editor can attach/detach it.
          $extra_class = '';
          if (!isset($field['#resizable']) || !empty($field['#resizable'])) {
            // Due to our CSS class parsing, we can add arbitrary parameters
            // for each input format.
            $extra_class = ' wysiwyg-resizable-1';
            $field['#resizable'] = FALSE;
            drupal_add_js('misc/textarea.js');
          }

          // Determine the available input formats. The last child element is a
          // link to "More information about formatting options". When only one
          // input format is displayed, we also have to remove formatting
          // guidelines, stored in the child 'format'.
          $formats = element_children($element);
          array_pop($formats);
          if (($key = array_search('format', $formats)) !== FALSE) {
            unset($formats[$key]);
          }
          foreach ($formats as $format) {
            // Default to 'none' editor (Drupal's default behaviors).
            $editor = 'none';
            $status = 1;
            // Fetch the profile associated to this input format.
            $profile = wysiwyg_get_profile($format);
            if ($profile) {
              $editor = $profile->editor;
              $status = (int)wysiwyg_user_get_status($profile);
              // Check editor theme (and reset it if not/no longer available).
              $theme = wysiwyg_get_editor_themes($profile, (isset($profile->settings['theme']) ? $profile->settings['theme'] : ''));

              // Add plugin settings (first) for this input format.
              wysiwyg_add_plugin_settings($profile);
              // Add profile settings for this input format.
              wysiwyg_add_editor_settings($profile, $theme);
            }

            // Use a prefix/suffix for a single input format, or attach to input
            // format selector radio buttons.
            if (isset($element['format']['guidelines'])) {
              $element['format']['guidelines']['#prefix'] = '<div class="wysiwyg wysiwyg-format-'. $format .' wysiwyg-editor-'. $editor .' wysiwyg-field-'. $field['#id'] .' wysiwyg-status-'. $status . $extra_class .'">';
              $element['format']['guidelines']['#suffix'] = '</div>';
              // Edge-case: Default format contains no input filters.
              if (empty($element['format']['guidelines']['#value'])) {
                $element['format']['guidelines']['#value'] = ' ';
              }
            }
            else {
              if (isset($element[$format]['#attributes']['class'])) {
                $element[$format]['#attributes']['class'] .= ' ';
              }
              else {
                $element[$format]['#attributes']['class'] = '';
              }
              $element[$format]['#attributes']['class'] .= 'wysiwyg wysiwyg-format-'. $format .' wysiwyg-editor-'. $editor .' wysiwyg-field-'. $field['#id'] .' wysiwyg-status-'. $status . $extra_class;
            }
          }
        }
        // If this element is 'format', do not recurse further.
        continue;
      }
      // Recurse into children.
      wysiwyg_process_form($element);
    }
  }
  return $form;
}

/**
 * Determine the profile to use for a given input format id.
 *
 * This function also performs sanity checks for the configured editor in a
 * profile to ensure that we do not load a malformed editor.
 *
 * @param $format
 *   The internal id of an input format.
 *
 * @return
 *   A wysiwyg profile.
 *
 * @see wysiwyg_load_editor(), wysiwyg_get_editor()
 */
function wysiwyg_get_profile($format) {
  if ($profile = wysiwyg_load_profile($format)) {
    if (wysiwyg_load_editor($profile)) {
      return $profile;
    }
  }
  return FALSE;
}

/**
 * Load an editor library and initialize basic Wysiwyg settings.
 *
 * @param $profile
 *   A wysiwyg editor profile.
 *
 * @return
 *   TRUE if the editor has been loaded, FALSE if not.
 *
 * @see wysiwyg_get_profile()
 */
function wysiwyg_load_editor($profile) {
  static $settings_added;
  static $loaded = array();

  $name = $profile->editor;
  // Library files must be loaded only once.
  if (!isset($loaded[$name])) {
    // Load editor.
    $editor = wysiwyg_get_editor($name);
    if ($editor) {
      // Determine library files to load.
      // @todo Allow to configure the library/execMode to use.
      if (isset($profile->settings['library']) && isset($editor['libraries'][$profile->settings['library']])) {
        $library = $profile->settings['library'];
        $files = $editor['libraries'][$library]['files'];
      }
      else {
        // Fallback to the first defined library by default (external libraries can change).
        $library = key($editor['libraries']);
        $files = array_shift($editor['libraries']);
        $files = $files['files'];
      }
      foreach ($files as $file => $options) {
        if (is_array($options)) {
          $options += array('type' => 'module', 'scope' => 'header', 'defer' => FALSE, 'cache' => TRUE, 'preprocess' => TRUE);
          drupal_add_js($editor['library path'] . '/' . $file, $options['type'], $options['scope'], $options['defer'], $options['cache'], $options['preprocess']);
        }
        else {
          drupal_add_js($editor['library path'] . '/' . $options);
        }
      }
      // If editor defines an additional load callback, invoke it.
      // @todo Isn't the settings callback sufficient?
      if (isset($editor['load callback']) && function_exists($editor['load callback'])) {
        $editor['load callback']($editor, $library);
      }
      // Load JavaScript integration files for this editor.
      $files = array();
      if (isset($editor['js files'])) {
        $files = $editor['js files'];
      }
      foreach ($files as $file) {
        drupal_add_js($editor['js path'] . '/' . $file);
      }
      // Load CSS stylesheets for this editor.
      $files = array();
      if (isset($editor['css files'])) {
        $files = $editor['css files'];
      }
      foreach ($files as $file) {
        drupal_add_css($editor['css path'] . '/' . $file);
      }

      drupal_add_js(array('wysiwyg' => array(
        'configs' => array($editor['name'] => array()),
        // @todo Move into profile settings.
        'showToggle' => isset($profile->settings['show_toggle']) ? $profile->settings['show_toggle'] : TRUE,
        // @todo Move into (global) editor settings.
        // If JS compression is enabled, at least TinyMCE is unable to determine
        // its own base path and exec mode since it can't find the script name.
        'editorBasePath' => base_path() . $editor['library path'],
        'execMode' => $library,
      )), 'setting');

      $loaded[$name] = TRUE;
    }
    else {
      $loaded[$name] = FALSE;
    }
  }

  // Add basic Wysiwyg settings if any editor has been added.
  if (!isset($settings_added) && $loaded[$name]) {
    drupal_add_js(array('wysiwyg' => array(
      'configs' => array(),
      'disable' => t('Disable rich-text'),
      'enable' => t('Enable rich-text'),
    )), 'setting');

    // Initialize our namespaces in the *header* to do not force editor
    // integration scripts to check and define Drupal.wysiwyg on its own.
    drupal_add_js(wysiwyg_get_path('wysiwyg.init.js'), 'core');

    // The 'none' editor is a special editor implementation, allowing us to
    // attach and detach regular Drupal behaviors just like any other editor.
    drupal_add_js(wysiwyg_get_path('editors/js/none.js'));

    // Add wysiwyg.js to the footer to ensure it's executed after the
    // Drupal.settings array has been rendered and populated. Also, since editor
    // library initialization functions must be loaded first by the browser,
    // and Drupal.wysiwygInit() must be executed AFTER editors registered
    // their callbacks and BEFORE Drupal.behaviors are applied, this must come
    // last.
    drupal_add_js(wysiwyg_get_path('wysiwyg.js'), 'module', 'footer');

    $settings_added = TRUE;
  }

  return $loaded[$name];
}

/**
 * Add editor settings for a given input format.
 */
function wysiwyg_add_editor_settings($profile, $theme) {
  static $formats = array();

  if (!isset($formats[$profile->format])) {
    $config = wysiwyg_get_editor_config($profile, $theme);
    // drupal_to_js() does not properly convert numeric array keys, so we need
    // to use a string instead of the format id.
    drupal_add_js(array('wysiwyg' => array('configs' => array($profile->editor => array('format'. $profile->format => $config)))), 'setting');
    $formats[$profile->format] = TRUE;
  }
}

/**
 * Add settings for external plugins.
 *
 * Plugins can be used in multiple profiles, but not necessarily in all. Because
 * of that, we need to process plugins for each profile, even if most of their
 * settings are not stored per profile.
 *
 * Implementations of hook_wysiwyg_plugin() may execute different code for each
 * editor. Therefore, we have to invoke those implementations for each editor,
 * but process the resulting plugins separately for each profile.
 *
 * Drupal plugins differ to native plugins in that they have plugin-specific
 * definitions and settings, which need to be processed only once. But they are
 * also passed to the editor to prepare settings specific to the editor.
 * Therefore, we load and process the Drupal plugins only once, and hand off the
 * effective definitions for each profile to the editor.
 *
 * @param $profile
 *   A wysiwyg editor profile.
 *
 * @todo Rewrite wysiwyg_process_form() to build a registry of effective
 *   profiles in use, so we can process plugins in multiple profiles in one shot
 *   and simplify this entire function.
 */
function wysiwyg_add_plugin_settings($profile) {
  static $plugins_native = array();

  $editor = wysiwyg_get_editor($profile->editor);
  // Assume that this editor does not support neither native external plugins
  // nor Drupal plugins if it does not provide a callback.
  if (!(isset($editor['plugin settings callback']) && function_exists($editor['plugin settings callback']))) {
    return;
  }

  // Load our own plugins.
  include_once drupal_get_path('module', 'wysiwyg') .'/wysiwyg.plugins.inc';

  // Collect native external plugins for this editor provided via
  // hook_wysiwyg_plugin().
  if (!array_key_exists($editor['name'], $plugins_native)) {
    $plugins_native[$editor['name']] = module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']);
  }
  // Only keep native external plugins that are enabled in this profile.
  foreach ($plugins_native[$editor['name']] as $plugin => $meta) {
    if (!isset($profile->settings['buttons'][$plugin])) {
      unset($plugins_native[$editor['name']][$plugin]);
    }
  }
  // Invoke the editor's plugin settings callback, so it can populate the
  // settings for native external plugins with custom, required values.
  $settings_native = $editor['plugin settings callback']($editor, $profile, $plugins_native[$editor['name']]);

  drupal_add_js(array('wysiwyg' => array('plugins' => array($profile->editor => $settings_native))), 'setting');
}

/**
 * Retrieve available themes for an editor.
 *
 * Editor themes control the visual presentation of an editor.
 *
 * @param $profile
 *   A wysiwyg editor profile; passed/altered by reference.
 * @param $selected_theme
 *   An optional theme name that ought to be used.
 *
 * @return
 *   An array of theme names, or a single, checked theme name if $selected_theme
 *   was given.
 */
function wysiwyg_get_editor_themes(&$profile, $selected_theme = NULL) {
  static $themes = array();

  if (!isset($themes[$profile->editor])) {
    $editor = wysiwyg_get_editor($profile->editor);
    if (isset($editor['themes callback']) && function_exists($editor['themes callback'])) {
      $themes[$editor['name']] = $editor['themes callback']($editor, $profile);
    }
    // Fallback to 'default' otherwise.
    else {
      $themes[$editor['name']] = array('default');
    }
  }

  // Check optional $selected_theme argument, if given.
  if (isset($selected_theme)) {
    // If the passed theme name does not exist, use the first available.
    if (!isset($themes[$profile->editor][$selected_theme])) {
      $selected_theme = $profile->settings['theme'] = $themes[$profile->editor][0];
    }
  }

  return isset($selected_theme) ? $selected_theme : $themes[$profile->editor];
}

/**
 * Return plugin metadata from the plugin registry.
 *
 * @param $editor_name
 *   The internal name of an editor to return plugins for.
 *
 * @return
 *   An array for each plugin.
 */
function wysiwyg_get_plugins($editor_name) {
  $plugins = array();
  if (!empty($editor_name)) {
    $editor = wysiwyg_get_editor($editor_name);
    // Add internal editor plugins.
    if (isset($editor['plugin callback']) && function_exists($editor['plugin callback'])) {
      $plugins = $editor['plugin callback']($editor);
    }
    // Load our own plugins.
    include_once drupal_get_path('module', 'wysiwyg') .'/wysiwyg.plugins.inc';

    // Add editor plugins provided via hook_wysiwyg_plugin().
    $plugins = array_merge($plugins, module_invoke_all('wysiwyg_plugin', $editor['name'], $editor['installed version']));
  }
  return $plugins;
}

/**
 * Return an array of initial editor settings for a Wysiwyg profile.
 */
function wysiwyg_get_editor_config($profile, $theme) {
  $editor = wysiwyg_get_editor($profile->editor);
  $settings = array();
  if (!empty($editor['settings callback']) && function_exists($editor['settings callback'])) {
    $settings = $editor['settings callback']($editor, $profile->settings, $theme);
  }
  return $settings;
}

/**
 * Retrieve stylesheets for HTML/IFRAME-based editors.
 *
 * This assumes that the content editing area only needs stylesheets defined
 * for the scope 'theme'.
 *
 * @return
 *   An array containing CSS files, including proper base path.
 */
function wysiwyg_get_css() {
  static $files;

  if (isset($files)) {
    return $files;
  }
  $files = array();
  foreach (drupal_add_css() as $media => $css) {
    if ($media != 'print') {
      foreach ($css['theme'] as $filepath => $preprocess) {
        $files[] = base_path() . $filepath;
      }
    }
  }
  return $files;
}

/**
 * Load all profiles. Just load one profile if $name is passed in.
 */
function wysiwyg_load_profile($format = '') {
  static $profiles;

  if (!isset($profiles)) {
    $profiles = array();
    $result = db_query('SELECT * FROM {wysiwyg}');
    while ($profile = db_fetch_object($result)) {
      $profile->settings = unserialize($profile->settings);
      $profiles[$profile->format] = $profile;
    }
  }

  return ($format && isset($profiles[$format]) ? $profiles[$format] : ($format ? FALSE : $profiles));
}

/**
 * Implementation of hook_user().
 */
function wysiwyg_user($type, &$edit, &$user, $category = NULL) {
  if ($type == 'form' && $category == 'account') {
    // @todo http://drupal.org/node/322433
    $profile = new stdClass;
    if (isset($profile->settings['user_choose']) && $profile->settings['user_choose']) {
      $form['wysiwyg'] = array(
        '#type' => 'fieldset',
        '#title' => t('Wysiwyg Editor settings'),
        '#weight' => 10,
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['wysiwyg']['wysiwyg_status'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable editor by default'),
        '#default_value' => isset($user->wysiwyg_status) ? $user->wysiwyg_status : (isset($profile->settings['default']) ? $profile->settings['default'] : FALSE),
        '#return_value' => 1,
        '#description' => t('If enabled, rich-text editing is enabled by default in textarea fields.'),
      );
      return array('wysiwyg' => $form);
    }
  }
  elseif ($type == 'validate' && isset($edit['wysiwyg_status'])) {
    return array('wysiwyg_status' => $edit['wysiwyg_status']);
  }
}

function wysiwyg_user_get_status($profile) {
  global $user;

  if (!empty($profile->settings['user_choose']) && isset($user->wysiwyg_status)) {
    $status = $user->wysiwyg_status;
  }
  else {
    $status = isset($profile->settings['default']) ? $profile->settings['default'] : TRUE;
  }

  return $status;
}

/**
 * @defgroup wysiwyg_api Wysiwyg API
 * @{
 *
 * @todo Forked from Panels; abstract into a separate API module that allows
 *   contrib modules to define supported include/plugin types.
 */

/**
 * Return library information for a given editor.
 *
 * @param $name
 *   The internal name of an editor.
 *
 * @return
 *   The library information for the editor, or FALSE if $name is unknown or not
 *   installed properly.
 */
function wysiwyg_get_editor($name) {
  $editors = wysiwyg_get_all_editors();
  return isset($editors[$name]) && $editors[$name]['installed'] ? $editors[$name] : FALSE;
}

/**
 * Compile a list holding all supported editors including installed editor version information.
 */
function wysiwyg_get_all_editors() {
  static $editors;

  if (isset($editors)) {
    return $editors;
  }

  $editors = wysiwyg_load_includes('editors', 'editor');
  foreach ($editors as $editor => $properties) {
    // Fill in required properties.
    $editors[$editor] += array(
      'title' => '',
      'vendor url' => '',
      'download url' => '',
      'editor path' => wysiwyg_get_path($properties['name']),
      'library path' => wysiwyg_get_path($properties['name']),
      'libraries' => array(),
      'version callback' => NULL,
      'themes callback' => NULL,
      'settings callback' => NULL,
      'plugin callback' => NULL,
      'plugin settings callback' => NULL,
      'versions' => array(),
      'js path' => $properties['path'] .'/js',
      'css path' => $properties['path'] .'/css',
    );
    // Check whether library is present.
    if (!($editors[$editor]['installed'] = file_exists($properties['library path']))) {
      continue;
    }
    // Detect library version.
    if (function_exists($editors[$editor]['version callback'])) {
      $editors[$editor]['installed version'] = $editors[$editor]['version callback']($properties);
    }
    if (empty($editors[$editor]['installed version'])) {
      $editors[$editor]['error'] = t('The version of %editor could not be detected.', array('%editor' => $properties['title']));
      $editors[$editor]['installed'] = FALSE;
      continue;
    }
    // Determine to which supported version the installed version maps.
    ksort($editors[$editor]['versions']);
    $version = 0;
    foreach ($editors[$editor]['versions'] as $supported_version => $version_properties) {
      if (version_compare($editors[$editor]['installed version'], $supported_version, '>=')) {
        $version = $supported_version;
      }
    }
    if (!$version) {
      $editors[$editor]['error'] = t('The installed version %version of %editor is not supported.', array('%version' => $editors[$editor]['installed version'], '%editor' => $properties['title']));
      $editors[$editor]['installed'] = FALSE;
      continue;
    }
    // Apply library version specific definitions and overrides.
    $editors[$editor] = array_merge($editors[$editor], $editors[$editor]['versions'][$version]);
    unset($editors[$editor]['versions']);
  }
  return $editors;
}

/**
 * Load include files for wysiwyg implemented by all modules.
 *
 * @param $type
 *   The type of includes to search for, can be 'editors'.
 * @param $hook
 *   The hook name to invoke.
 * @param $file
 *   An optional include file name without .inc extension to limit the search to.
 *
 * @see wysiwyg_get_directories(), _wysiwyg_process_include()
 */
function wysiwyg_load_includes($type = 'editors', $hook = 'editor', $file = NULL) {
  // Determine implementations.
  $directories = wysiwyg_get_directories($type);
  $directories['wysiwyg_'] = wysiwyg_get_path($type);
  $file_list = array();
  foreach ($directories as $module => $path) {
    $file_list[$module] = drupal_system_listing("$file" . '.inc$', $path, 'name', 0);
  }

  // Load implementations.
  $info = array();
  foreach (array_filter($file_list) as $module => $files) {
    foreach ($files as $file) {
      include_once './' . $file->filename;
      $result = _wysiwyg_process_include('wysiwyg', $module . $file->name, dirname($file->filename), $hook);
      if (is_array($result)) {
        $info = array_merge($info, $result);
      }
    }
  }
  return $info;
}

/**
 * Helper function to build module/file paths.
 *
 * @param $file
 *   A file or directory in a module to return.
 * @param $base_path
 *   Whether to prefix the resulting path with base_path().
 * @param $module
 *   The module name to use as prefix.
 *
 * @return
 *   The path to the specified file in a module.
 */
function wysiwyg_get_path($file = '', $base_path = FALSE, $module = 'wysiwyg') {
  $base_path = ($base_path ? base_path() : '');
  return $base_path . drupal_get_path('module', $module) . '/' . $file;
}

/**
 * Return a list of directories by modules implementing wysiwyg_include_directory().
 *
 * @param $plugintype
 *   The type of a plugin; can be 'editors'.
 *
 * @return
 *   An array containing module names suffixed with '_' and their defined
 *   directory.
 *
 * @see wysiwyg_load_includes(), _wysiwyg_process_include()
 */
function wysiwyg_get_directories($plugintype) {
  $directories = array();
  foreach (module_implements('wysiwyg_include_directory') as $module) {
    $result = module_invoke($module, 'wysiwyg_include_directory', $plugintype);
    if (isset($result) && is_string($result)) {
      $directories[$module .'_'] = drupal_get_path('module', $module) .'/'. $result;
    }
  }
  return $directories;
}

/**
 * Process a single hook implementation of a wysiwyg editor.
 *
 * @param $module
 *   The module that owns the hook.
 * @param $identifier
 *   Either the module or 'wysiwyg_' . $file->name
 * @param $hook
 *   The name of the hook being invoked.
 */
function _wysiwyg_process_include($module, $identifier, $path, $hook) {
  $function = $identifier . '_' . $hook;
  if (!function_exists($function)) {
    return NULL;
  }
  $result = $function();
  if (!isset($result) || !is_array($result)) {
    return NULL;
  }

  // Fill in defaults.
  foreach ($result as $editor => $properties) {
    $result[$editor]['module'] = $module;
    $result[$editor]['name'] = $editor;
    $result[$editor]['path'] = $path;
  }
  return $result;
}

/**
 * @} End of "defgroup wysiwyg_api".
 */