public
Description: WordPress 2.5 plugin for automagical Facebox goodness
Homepage: http://evocateur.org/projects/wp-facebox/
Clone URL: git://github.com/evocateur/wp-facebox.git
wp-facebox / wp-facebox.php
100644 109 lines (96 sloc) 3.439 kb
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
<?php
/*
Plugin Name: WP Facebox
Plugin URI: http://evocateur.org/projects/wp-facebox/
Description: Automagical Facebox for WordPress
Version: 1.2.2
Author: Daniel Stockman
Author URI: http://evocateur.org/
*/
/*  Copyright 2008 Daniel Stockman <daniel.stockman@gmail.com>
 
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
 
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  GNU General Public License for more details.
*/
/**
* WP Facebox
*   julienne fries!
*/
class WP_Facebox {
  var $opts;
  var $site;
  var $home;
  var $root; // plugin root dir
 
  /*
    Utilities
  */
  function header() {
    echo <<<HTML
<link rel="stylesheet" type="text/css" href="{$this->root}/facebox.css" />
<script type="text/javascript">/* wp-facebox */
  WPFB = { root: "{$this->root}", home: "{$this->home}", site: "{$this->site}" };
  WPFB.options = { loadingImage: WPFB.root + '/images/loading.gif', closeImage: WPFB.root + '/images/closelabel.gif', opacity: 0.5 };
</script>\n
HTML;
  }
 
  function invoke_header() {
    $selectors = array();
    if ( $this->opts['do_default'] ) $selectors[] = "a[rel*='facebox']";
    if ( $this->opts['do_gallery'] ) $selectors[] = ".gallery-item a";
    $selectors = implode(', ', $selectors);
    if ( !empty($selectors) )
      echo "<script type=\"text/javascript\">if (jQuery && jQuery.facebox) jQuery(function($) { $(\"$selectors\").facebox(WPFB.options); });</script>\n";
  }
 
  function rel_replace( $content ) {
    $pattern = "/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>(.*?)<\/a>/i";
    $replacement = '<a$1href=$2$3.$4$5 rel="facebox"$6>$7</a>';
    $content = preg_replace($pattern, $replacement, $content);
    return $content;
  }
 
  function filter_gallery_link( $link, $id ) {
    // By default, the gallery shortcode creates permalinks to the attachment
    // Facebox, however, expects a direct link to the resource
    // wp_get_attachment_url does this for us
    // I <3 filters
    return wp_get_attachment_url( $id );
  }
 
  /*
    Init / Constructor
  */
  function init() {
    $this->home = get_option('home');
    $this->site = get_option('siteurl');
    $this->root = $this->site . '/wp-content/plugins/wp-facebox';
 
    wp_register_script( 'facebox', "{$this->root}/facebox.js", array('jquery'), '1.2' );
 
    if ( $this->opts['loadscript'] ) {
      wp_enqueue_script( 'facebox' );
      add_action( 'wp_print_scripts', array(&$this, 'header') );
      add_action( 'wp_head', array(&$this, 'invoke_header') );
      // turn gallery permalinks into direct links
      add_filter( 'attachment_link', array(&$this, 'filter_gallery_link'), 11, 2 );
    }
 
    if ( $this->opts['autofilter'] ) {
      add_filter( 'the_content', array(&$this, 'rel_replace') );
    }
  }
 
  function WP_Facebox() {  // constructor
    // TODO: implement admin options interface for these values
    // For the time being, turn off options by replacing 1 with 0
    $this->opts = array(
      'autofilter' => 1,
      'do_default' => 1,
      'do_gallery' => 1,
      'loadscript' => 1
    );
    // don't disable 'loadscript', unless you're only after the header output
    $this->init();
  }
}
 
// make those julienne fries, baby
$wp_facebox = new WP_Facebox();
 
?>