You are here

iframe_filter.module in Iframe Filter 7

Creates a filter which will convert content into an iFrame and link to it. Useful for speeding up ad provider load times.

File

iframe_filter.module
View source
<?php

/**
 * @file
 * Creates a filter which will convert content into an iFrame and link to it.
 * Useful for speeding up ad provider load times.
 */

/**
 * Implements hook_filter_info().
 */
function iframe_filter_filter_info() {
  $filters['iframe'] = array(
    'title' => t('iFrame'),
    'description' => t('Save text content to a file and link to it from an iFrame, reducing load time for the current page. This MUST be the last filter run on an input filter.'),
    'process callback' => 'iframe_filter_iframe_process',
    'tips callback' => 'iframe_filter_iframe_tips',
    'cache' => FALSE,
  );
  return $filters;
}

/**
 * Implements hook_filter_tips().
 */
function iframe_filter_iframe_tips($filter, $format, $long) {
  return t('The content of this field will be saved to a file and included on the original page in an iFrame.');
}

/**
* Implements hook_filter_FILTER_process()
*/
function iframe_filter_iframe_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  return iframe_filter_parse($text);
}

/**
 * Implements hook_theme().
 */
function iframe_filter_theme() {
  return array(
    'iframe_filter_page' => array(
      'variables' => array(
        'content' => NULL,
      ),
      'template' => 'iframe-filter-page',
    ),
    'iframe_filter_iframe' => array(
      'variables' => array(
        'src' => NULL,
        'width' => NULL,
        'height' => NULL,
      ),
      'template' => 'iframe-filter-iframe',
    ),
  );
}
function template_preprocess_iframe_filter_page(&$vars) {
  $vars['site_title'] = variable_get('site_name', '');
}

/**
 * Finds the height and width of the iframe and wrap into it.
 */
function iframe_filter_parse($text) {

  // Sanity check
  $files_dir = variable_get('file_public_path', conf_path() . '/files') . '/iframe_filter';
  if (!file_prepare_directory($files_dir, FILE_CREATE_DIRECTORY)) {
    return $text;
  }
  $filename = md5($text) . '.html';
  $destination_file = $files_dir . '/' . $filename;

  // Look for a width= and height= attributes in this text (most obvious).
  if (preg_match('/width[ ]*=[ ]*"(\\d+)"/', $text, $matches)) {
    $width = $matches[1];
  }
  if (preg_match('/height[ ]*=[ ]*"(\\d+)"/', $text, $matches)) {
    $height = $matches[1];
  }

  // Look for a [width]x[height] type attributes in this text.
  if ((!isset($width) || !isset($height)) && preg_match('/=(\\d+)x(\\d+)/', $text, $matches)) {
    $width = $matches[1];
    $height = $matches[2];
  }

  // Only attempt to create an iFrame if a width and height are found.
  if (isset($width) && isset($height)) {
    $html_page = theme('iframe_filter_page', array(
      'content' => $text,
    ));
    file_put_contents($destination_file, $html_page);
  }
  if (file_exists($destination_file)) {
    $iframe = theme('iframe_filter_iframe', array(
      'src' => base_path() . $destination_file,
      'width' => $width,
      'height' => $height,
    ));
    return $iframe;
  }
  else {
    return $text;
  }
}

Functions

Namesort descending Description
iframe_filter_filter_info Implements hook_filter_info().
iframe_filter_iframe_process Implements hook_filter_FILTER_process()
iframe_filter_iframe_tips Implements hook_filter_tips().
iframe_filter_parse Finds the height and width of the iframe and wrap into it.
iframe_filter_theme Implements hook_theme().
template_preprocess_iframe_filter_page