You are here

wysiwyg.module in Wysiwyg 5.2

Same filename and directory in other branches
  1. 5 wysiwyg.module
  2. 6.2 wysiwyg.module
  3. 6 wysiwyg.module
  4. 7.2 wysiwyg.module

Integrate client-side editors with Drupal.

File

wysiwyg.module
View source
<?php

/**
 * @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',
      'title' => t('Wysiwyg'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'wysiwyg_profile_overview',
      ),
      'description' => t('Configure client-side editors.'),
      'access' => user_access('administer filters'),
    );
    $items[] = array(
      'path' => 'wysiwyg',
      'callback' => 'wysiwyg_dialog',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK,
    );
    return $items;
  }
  if (strpos($_GET['q'], 'admin/settings/wysiwyg') !== FALSE) {
    require_once drupal_get_path('module', 'wysiwyg') . '/wysiwyg.admin.inc';
    if ($profile = wysiwyg_profile_load(arg(4))) {
      $items[] = array(
        'path' => "admin/settings/wysiwyg/profile/{$profile->format}/edit",
        'title' => t('Edit'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array(
          'wysiwyg_profile_form',
          $profile,
        ),
        'access' => user_access('administer filters'),
        'type' => MENU_LOCAL_TASK,
      );
      $items[] = array(
        'path' => "admin/settings/wysiwyg/profile/{$profile->format}/delete",
        'title' => t('Remove'),
        'callback' => 'drupal_get_form',
        'callback arguments' => array(
          'wysiwyg_profile_delete_confirm',
          $profile,
        ),
        'access' => user_access('administer filters'),
        'type' => MENU_LOCAL_TASK,
        'weight' => 10,
      );
    }
  }
  elseif (arg(0) == 'wysiwyg') {
    require_once drupal_get_path('module', 'wysiwyg') . '/wysiwyg.dialog.inc';
  }
  return $items;
}

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

/**
 * 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' && module_exists('blog')) {
    $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]];

          // If this textarea is #resizable and we will load at least one
          // editor, then only load the behavior and let the 'none' editor
          // attach/detach it to avoid hi-jacking the UI. Due to our CSS class
          // parsing, we can add arbitrary parameters for each input format.
          // The #resizable property will be removed below, if at least one
          // profile has been loaded.
          $extra_class = '';
          if (!isset($field['#resizable']) || !empty($field['#resizable'])) {
            $extra_class = ' wysiwyg-resizable-1';
            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;
            $toggle = 1;

            // Fetch the profile associated to this input format.
            $profile = wysiwyg_get_profile($format);
            if ($profile) {
              $loaded = TRUE;
              $editor = $profile->editor;
              $status = (int) wysiwyg_user_get_status($profile);
              if (isset($profile->settings['show_toggle'])) {
                $toggle = (int) $profile->settings['show_toggle'];
              }

              // 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 . ' wysiwyg-toggle-' . $toggle . $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 {
              $element[$format]['#attributes']['class'] = isset($element[$format]['#attributes']['class']) ? $element[$format]['#attributes']['class'] . ' ' : '';
              $element[$format]['#attributes']['class'] .= 'wysiwyg wysiwyg-format-' . $format . ' wysiwyg-editor-' . $editor . ' wysiwyg-field-' . $field['#id'] . ' wysiwyg-status-' . $status . ' wysiwyg-toggle-' . $toggle . $extra_class;
            }
          }

          // If we loaded at least one editor, then the 'none' editor will
          // handle resizable textareas instead of core.
          if (isset($loaded) && (!isset($field['#resizable']) || !empty($field['#resizable']))) {
            $field['#resizable'] = FALSE;
          }
        }

        // 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_profile_load($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(
              'global' => array(
                // @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(),
        'plugins' => array(),
        'disable' => t('Disable rich-text'),
        'enable' => t('Enable rich-text'),
      ),
    ), 'setting');
    $path = drupal_get_path('module', 'wysiwyg');

    // 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($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($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($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.
    if ($config) {
      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 = array();
  static $processed_plugins = array();
  static $processed_formats = array();

  // Each input format must only processed once.
  // @todo ...as long as we do not have multiple profiles per format.
  if (isset($processed_formats[$profile->format])) {
    return;
  }
  $processed_formats[$profile->format] = TRUE;
  $editor = wysiwyg_get_editor($profile->editor);

  // Collect native plugins for this editor provided via hook_wysiwyg_plugin()
  // and Drupal plugins provided via hook_wysiwyg_include_directory().
  if (!array_key_exists($editor['name'], $plugins)) {
    $plugins[$editor['name']] = wysiwyg_get_plugins($editor['name']);
  }

  // Nothing to do, if there are no plugins.
  if (empty($plugins[$editor['name']])) {
    return;
  }

  // Determine name of proxy plugin for Drupal plugins.
  $proxy = isset($editor['proxy plugin']) ? key($editor['proxy plugin']) : '';

  // Process native editor plugins.
  if (isset($editor['plugin settings callback'])) {

    // @todo Require PHP 5.1 in 3.x and use array_intersect_key().
    $profile_plugins_native = array();
    foreach ($plugins[$editor['name']] as $plugin => $meta) {

      // Skip Drupal plugins (handled below).
      if ($plugin === $proxy) {
        continue;
      }

      // Only keep native plugins that are enabled in this profile.
      if (isset($profile->settings['buttons'][$plugin])) {
        $profile_plugins_native[$plugin] = $meta;
      }
    }

    // Invoke the editor's plugin settings callback, so it can populate the
    // settings for native external plugins with required values.
    $settings_native = call_user_func($editor['plugin settings callback'], $editor, $profile, $profile_plugins_native);
    if ($settings_native) {
      drupal_add_js(array(
        'wysiwyg' => array(
          'plugins' => array(
            'format' . $profile->format => array(
              'native' => $settings_native,
            ),
          ),
        ),
      ), 'setting');
    }
  }

  // Process Drupal plugins.
  if ($proxy && isset($editor['proxy plugin settings callback'])) {
    $profile_plugins_drupal = array();
    foreach (wysiwyg_get_all_plugins() as $plugin => $meta) {
      if (isset($profile->settings['buttons'][$proxy][$plugin])) {

        // JavaScript and plugin-specific settings for Drupal plugins must be
        // loaded and processed only once. Plugin information is cached
        // statically to pass it to the editor's proxy plugin settings callback.
        if (!isset($processed_plugins[$proxy][$plugin])) {
          $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin] = $meta;

          // Load the Drupal plugin's JavaScript.
          drupal_add_js($meta['js path'] . '/' . $meta['js file']);

          // Add plugin-specific settings.
          if (isset($meta['settings'])) {
            drupal_add_js(array(
              'wysiwyg' => array(
                'plugins' => array(
                  'drupal' => array(
                    $plugin => $meta['settings'],
                  ),
                ),
              ),
            ), 'setting');
          }
        }
        else {
          $profile_plugins_drupal[$plugin] = $processed_plugins[$proxy][$plugin];
        }
      }
    }

    // Invoke the editor's proxy plugin settings callback, so it can populate
    // the settings for Drupal plugins with custom, required values.
    $settings_drupal = call_user_func($editor['proxy plugin settings callback'], $editor, $profile, $profile_plugins_drupal);
    if ($settings_drupal) {
      drupal_add_js(array(
        'wysiwyg' => array(
          'plugins' => array(
            'format' . $profile->format => array(
              'drupal' => $settings_drupal,
            ),
          ),
        ),
      ), '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);
    }
    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 (!in_array($selected_theme, $themes[$profile->editor])) {
      $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);
    }

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

    // Add API plugins provided by Drupal modules.
    // @todo We need to pass the filepath to the plugin icon for Drupal plugins.
    if (isset($editor['proxy plugin'])) {
      $plugins += $editor['proxy plugin'];
      $proxy = key($editor['proxy plugin']);
      foreach (wysiwyg_get_all_plugins() as $plugin_name => $info) {
        $plugins[$proxy]['buttons'][$plugin_name] = $info['title'];
      }
    }
  }
  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;
  }

  // In node form previews, the theme has not been initialized yet.
  if (!empty($_POST)) {
    init_theme();
  }
  $files = array();
  foreach (drupal_add_css() as $media => $css) {
    if ($media != 'print') {
      foreach ($css['theme'] as $filepath => $preprocess) {
        if (file_exists($filepath)) {
          $files[] = base_path() . $filepath;
        }
      }
    }
  }
  return $files;
}

/**
 * Load profile for a given input format.
 */
function wysiwyg_profile_load($format) {
  static $profiles;
  if (!isset($profiles) || !array_key_exists($format, $profiles)) {
    $result = db_query('SELECT format, editor, settings FROM {wysiwyg} WHERE format = %d', $format);
    while ($profile = db_fetch_object($result)) {
      $profile->settings = unserialize($profile->settings);
      $profiles[$profile->format] = $profile;
    }
  }
  return isset($profiles[$format]) ? $profiles[$format] : FALSE;
}

/**
 * Load all profiles.
 */
function wysiwyg_profile_load_all() {
  static $profiles;
  if (!isset($profiles)) {
    $profiles = array();
    $result = db_query('SELECT format, editor, settings FROM {wysiwyg}');
    while ($profile = db_fetch_object($result)) {
      $profile->settings = unserialize($profile->settings);
      $profiles[$profile->format] = $profile;
    }
  }
  return $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($editors[$editor]['name']),
      'library path' => wysiwyg_get_path($editors[$editor]['name']),
      'libraries' => array(),
      'version callback' => NULL,
      'themes callback' => NULL,
      'settings callback' => NULL,
      'plugin callback' => NULL,
      'plugin settings callback' => NULL,
      'versions' => array(),
      'js path' => $editors[$editor]['path'] . '/js',
      'css path' => $editors[$editor]['path'] . '/css',
    );

    // Check whether library is present.
    if (!($editors[$editor]['installed'] = file_exists($editors[$editor]['library path']))) {
      continue;
    }

    // Detect library version.
    if (function_exists($editors[$editor]['version callback'])) {
      $editors[$editor]['installed version'] = $editors[$editor]['version callback']($editors[$editor]);
    }
    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' => $editors[$editor]['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;
}

/**
 * Invoke hook_wysiwyg_plugin() in all modules.
 */
function wysiwyg_get_all_plugins() {
  static $plugins;
  if (isset($plugins)) {
    return $plugins;
  }
  $plugins = wysiwyg_load_includes('plugins', 'plugin');
  foreach ($plugins as $name => $properties) {
    $plugin =& $plugins[$name];

    // Fill in required/default properties.
    $plugin += array(
      'title' => $plugin['name'],
      'vendor url' => '',
      'js path' => $plugin['path'] . '/' . $plugin['name'],
      'js file' => $plugin['name'] . '.js',
      'css path' => $plugin['path'] . '/' . $plugin['name'],
      'css file' => $plugin['name'] . '.css',
      'icon path' => $plugin['path'] . '/' . $plugin['name'] . '/images',
      'icon file' => $plugin['name'] . '.png',
      'dialog path' => $plugin['name'],
      'dialog settings' => array(),
      'settings callback' => NULL,
      'settings form callback' => NULL,
    );

    // Fill in default settings.
    $plugin['settings'] += array(
      'path' => base_path() . $plugin['path'] . '/' . $plugin['name'],
    );

    // Check whether library is present.
    if (!($plugin['installed'] = file_exists($plugin['js path'] . '/' . $plugin['js file']))) {
      continue;
    }
  }
  return $plugins;
}

/**
 * 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'] = drupal_get_path('module', 'wysiwyg') . '/' . $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($module, $module . '_' . $file->name, dirname($file->filename), $hook);
      if (is_array($result)) {
        $info = array_merge($info, $result);
      }
    }
  }
  return $info;
}

/**
 * Helper function to build paths to libraries.
 *
 * @param $library
 *   The external library name to return the path for.
 * @param $base_path
 *   Whether to prefix the resulting path with base_path().
 *
 * @return
 *   The path to the specified library.
 *
 * @ingroup libraries
 */
function wysiwyg_get_path($library, $base_path = FALSE) {
  static $libraries;
  if (!isset($libraries)) {
    $libraries = wysiwyg_get_libraries();
  }
  if (!isset($libraries[$library])) {

    // Most often, external libraries can be shared across multiple sites.
    return 'sites/all/libraries/' . $library;
  }
  $path = $base_path ? base_path() : '';
  $path .= $libraries[$library];
  return $path;
}

/**
 * Return an array of library directories.
 *
 * Returns an array of library directories from the all-sites directory
 * (i.e. sites/all/libraries/), the profiles directory, and site-specific
 * directory (i.e. sites/somesite/libraries/). The returned array will be keyed
 * by the library name. Site-specific libraries are prioritized over libraries
 * in the default directories. That is, if a library with the same name appears
 * in both the site-wide directory and site-specific directory, only the
 * site-specific version will be listed.
 *
 * @return
 *   A list of library directories.
 *
 * @ingroup libraries
 */
function wysiwyg_get_libraries() {
  global $profile;

  // When this function is called during Drupal's initial installation process,
  // the name of the profile that is about to be installed is stored in the
  // global $profile variable. At all other times, the regular system variable
  // contains the name of the current profile, and we can call variable_get()
  // to determine the profile.
  if (!isset($profile)) {
    $profile = variable_get('install_profile', 'default');
  }
  $directory = 'libraries';
  $searchdir = array();
  $config = conf_path();

  // The 'profiles' directory contains pristine collections of modules and
  // themes as organized by a distribution.  It is pristine in the same way
  // that /modules is pristine for core; users should avoid changing anything
  // there in favor of sites/all or sites/<domain> directories.
  if (file_exists("profiles/{$profile}/{$directory}")) {
    $searchdir[] = "profiles/{$profile}/{$directory}";
  }

  // Always search sites/all/*.
  $searchdir[] = 'sites/all/' . $directory;

  // Also search sites/<domain>/*.
  if (file_exists("{$config}/{$directory}")) {
    $searchdir[] = "{$config}/{$directory}";
  }

  // Retrieve list of directories.
  // @todo Core: Allow to scan for directories.
  $directories = array();
  $nomask = array(
    'CVS',
  );
  foreach ($searchdir as $dir) {
    if (is_dir($dir) && ($handle = opendir($dir))) {
      while (FALSE !== ($file = readdir($handle))) {
        if (!in_array($file, $nomask) && $file[0] != '.') {
          if (is_dir("{$dir}/{$file}")) {
            $directories[$file] = "{$dir}/{$file}";
          }
        }
      }
      closedir($handle);
    }
  }
  return $directories;
}

/**
 * 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;
}

/**
 * Create a placeholder structure for JavaScript callbacks.
 *
 * @param $name
 *  A string with the name of the callback, use 'object.subobject.method'
 *  syntax for methods in nested objects.
 * @param $context
 *  An optional string with the name of an object for overriding 'this' inside
 *  the function. Use 'object.subobject' syntax for nested objects. Defaults to
 *  the window object.
 *
 * @return
 *  An array with placeholder information for creating a JavaScript function
 *  reference on the client.
 */
function wysiwyg_wrap_js_callback($name, $context = null) {
  $obj = array(
    'drupalWysiwygType' => 'callback',
    'name' => $name,
  );
  if ($context) {
    $obj['context'] = $context;
  }
  return $obj;
}

/**
 * Create a placeholder structure for JavaScript RegExp objects.
 *
 * @param $regexp
 *  A JavaScript Regular Expression as a string, without / wrappers.
 * @param $modifiers
 *  An optional string with modifiers for the RegExp object.
 *
 * @return
 *  An array with placeholder information for creating a JavaScript RegExp
 *  object on the client.
 */
function wysiwyg_wrap_js_regexp($regexp, $modifiers = null) {
  $obj = array(
    'drupalWysiwygType' => 'regexp',
    'regexp' => $regexp,
  );
  if ($modifiers) {
    $obj['modifiers'] = $modifiers;
  }
  return $obj;
}

/**
 * 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".
 */

Functions

Namesort descending Description
wysiwyg_add_editor_settings Add editor settings for a given input format.
wysiwyg_add_plugin_settings Add settings for external plugins.
wysiwyg_form_alter Implementation of hook_form_alter().
wysiwyg_get_all_editors Compile a list holding all supported editors including installed editor version information.
wysiwyg_get_all_plugins Invoke hook_wysiwyg_plugin() in all modules.
wysiwyg_get_css Retrieve stylesheets for HTML/IFRAME-based editors.
wysiwyg_get_directories Return a list of directories by modules implementing wysiwyg_include_directory().
wysiwyg_get_editor Return library information for a given editor.
wysiwyg_get_editor_config Return an array of initial editor settings for a Wysiwyg profile.
wysiwyg_get_editor_themes Retrieve available themes for an editor.
wysiwyg_get_libraries Return an array of library directories.
wysiwyg_get_path Helper function to build paths to libraries.
wysiwyg_get_plugins Return plugin metadata from the plugin registry.
wysiwyg_get_profile Determine the profile to use for a given input format id.
wysiwyg_help Implementation of hook_help().
wysiwyg_load_editor Load an editor library and initialize basic Wysiwyg settings.
wysiwyg_load_includes Load include files for wysiwyg implemented by all modules.
wysiwyg_menu Implementation of hook_menu().
wysiwyg_process_form Process a textarea for Wysiwyg Editor.
wysiwyg_profile_load Load profile for a given input format.
wysiwyg_profile_load_all Load all profiles.
wysiwyg_user Implementation of hook_user().
wysiwyg_user_get_status
wysiwyg_wrap_js_callback Create a placeholder structure for JavaScript callbacks.
wysiwyg_wrap_js_regexp Create a placeholder structure for JavaScript RegExp objects.
_wysiwyg_process_include Process a single hook implementation of a wysiwyg editor.