<?php
/*
Plugin Name: Galleria WP
Plugin URI: http://y2itec.com/samples/wordpress/galleria-wp/
Description: A simple image gallery for WordPress2.5 using galleria 1.0 image gallery.
Galleria <a href="http://devkick.com/lab/galleria/> DEVKICK </a>
jQuery <a href="http://jquery.com/> jQuery </a>
Author: Y2 (Yokoyama Yasuaki)
Author URI: http://y2itec.com/blog/
Version: 1.0.3
*/
/* Copyright 2008 Y2 (Yokoyama Yasuaki)
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.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function galleria_wp_headers() {
$galleria_plugin_path =
get_option('siteurl')."/wp-content/plugins/galleria-wp";
$galleria_style_path =
"<link rel=\"stylesheet\" type=\"text/css\" " .
"href=\"$galleria_plugin_path/css/galleria.css\" media=\"screen\" />\n";
$galleria_script_path =
"<script type='text/javascript' ".
"src='$galleria_plugin_path/js/jquery.min.js'></script>\n";
$galleria_script_path .=
"<script type='text/javascript' ".
"src='$galleria_plugin_path/js/jquery.galleria.js'></script>\n";
echo "<!-- Galleria WP [ BEGIN ] --> \n";
echo $galleria_style_path;
echo $galleria_script_path;
echo "<!-- Galleria WP [ END ] --> \n";
}
add_action( 'wp_head', 'galleria_wp_headers');
//==============================================================================//
// function galleria_shortcode(): WP short code application for //
// Galleria image gallery //
// //
// Usage: [galleria <options>] //
// options: exif="1" show EXIF meta data (default: "0") //
// gid="<string>" use for grouping //
// cssclass="<string>" CSS class selector identifier //
// bgs="<styles>" overrides default background styles //
// thumbs="<styles>" overrides default thumbnail styles //
// caps1="<styles>" overrides default caption #1 styles //
// caps2="<styles>" overrides default caption #2 styles //
// playbutton="1" add automatic slide playback button //
// interval="<sec>" display intervals ( second ) //
// orderby, id, size => same as the original [gallery] //
// //
// Galleria is licensed under the GPL licenses. //
// [ http://devkick.com/lab/galleria/ ] //
// //
// jQuery is licensed under both MIT and GPL licenses. //
// [ http://jquery.com/ ] //
// //
// V1.0 : This is a demo sample. This is not fully tested. //
// //
// V1.0.3 Added image shrink mode Last Updated: May 09 2008 by Y2 //
//==============================================================================//
function galleria_shortcode( $attr ) {
global $post;
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract( shortcode_atts( array(
'orderby' => 'menu_order ASC, ID ASC',
'id' => $post->ID,
'size' => 'thumbnail',
'shrink' => 0,
'gid' => '',
'cssclass' => 'galleria_std',
'bgs' => '',
'thumbs' => '',
'caps1' => '',
'caps2' => '',
'exif' => 0,
'playbutton' => 0,
'interval' => 5.0,
), $attr ) );
$id = intval($id);
$columns = intval($columns);
$classString = trim($cssclass);
$attachments =
get_children("post_parent=$id&post_type=attachment&post_mime_type=image&orderby={$orderby}");
if ( empty($attachments) )
return '';
if ( !is_page() && !is_single() ) { // added by Y2 May 08 2008
return '';
}
// if ( is_feed() ) {
// $output = "\n";
// foreach ( $attachments as $id => $attachment )
// $output .= wp_get_attachment_link( $id, $size, true ) . "\n";
// return $output;
// }
$output = "";
if( !$classString ) {
$classString = 'galleria_std';
}
if ( $bgs || $thumbs || $caps1 || $caps2 ) {
$output .= overrideGalleriaStyles( $classString, $bgs, $thumbs, $caps1, $caps2 );
}
$output .= apply_filters( 'gallery_style',
" <div class='$classString'>
<ul class='$classString'>
");
$counter = 0;
foreach ( $attachments as $id => $attachment ) {
$link = galleria_wp_get_attachment_link( $id, $size, $gid, $exif );
if ( ! $link ) continue;
if ( $counter == 0 ) {
// activate the first image
$output .= apply_filters( 'gallery_style',
" <li class='active'>
$link
</li>
");
} else {
$output .= apply_filters( 'gallery_style',
" <li>
$link
</li>
");
}
$counter++;
}
$output .= apply_filters( 'gallery_style',
" </ul>
<div id='main_stage'>
<!-- The target image and caption will be inseted here. -->
</div>
<div class='galleria_nav'>
<p class='galleria_nav'>
<a href='#' onclick='jQuery.galleria.prev(); return false;'>
« Previous</a> |
<a href='#' onclick='jQuery.galleria.next(); return false;'>
Next »</a>
</p>
<button class='play'>Play</button>
</div>
</div> <!-- end of [ div class='$classString' ] -->
<p style='clear: both; display: block; height: 0.5em;'> </p>
" );
$output .= prepareGalleriaScript( $classString,
$shrink,
$playbutton,
$interval );
return $output;
}
add_shortcode( 'galleria', 'galleria_shortcode' );
//==============================================================================//
// function galleria_wp_get_attachment_link(): //
//==============================================================================//
function galleria_wp_get_attachment_link( $id = 0,
$size = 'thumbnail',
$gid = '',
$exif = 0 ) {
global $post;
$exif_text ='';
$_post = & get_post( intval($id) );
if ( ('attachment' != $_post->post_type) ||
!$url = wp_get_attachment_url($_post->ID) ) {
return __('Missing Attachment');
}
$image_title = attribute_escape($_post->post_title);
$image_caption = attribute_escape($_post->post_excerpt);
$image_description = attribute_escape($_post->post_content);
$groups_text = strstr( $image_title, "[" );
if ( $groups_text ) {
$groups_text = str_replace( "[", "", $groups_text );
$groups_text = str_replace( "]", "", $groups_text );
$groups_text = trim( $groups_text );
}
if ( $gid ) {
$regexp = trim( $gid );
if ( $groups_text ) {
if ( ! ereg( $regexp, $groups_text ) ) {
return '';
}
} else {
return '';
} // end of [ if ( $groups_text ) ]
} // end of [ if ( $gid ) ]
// invoke bug fixed version of "wp_get_attachment_image()"
$link_text = _wp_get_attachment_image( $id, $size );
$alt_text = 'alt="' . $image_caption . '"';
$thumb_link = str_replace( 'alt=""', $alt_text, $link_text );
if ( $exif == 1 ) {
if ( $url ) {
$exifdata = @ exif_read_data( $url, "EXIF" );
if ( $exifdata ) {
$datetime = $exifdata["DateTimeOriginal"];
$model = $exifdata["Model"];
$aperture = $exifdata["COMPUTED"]["ApertureFNumber"];
$exposure = $exifdata["ExposureTime"];
$exif_text .= " Date: $datetime ";
$exif_text .= " Aperture: $aperture ";
$exif_text .= " Exposure: $exposure";
$exif_text .= " Model: $model";
}
}
}
if ( $exif_text ) {
$image_description .= ' :: [ '.$exif_text.' ]';
}
return "<a href='$url' title='$image_description'>$thumb_link</a>";
}
//==============================================================================//
// function overrideGalleriaStyles(): overrides default CSS styles //
//==============================================================================//
function overrideGalleriaStyles ( $classString,
$bgs = "",
$thumbs = "",
$caps1 = "",
$caps2 = "" ) {
$output = "";
if ( $bgs || $thumbs || $caps1 || $caps2 ) {
$output .= apply_filters( 'gallery_style',"<style type='text/css'>\n" );
if ( $bgs ) {
$output .= apply_filters( 'gallery_style',
"div.$classString { $bgs; }\n");
}
if ( $thumbs ) {
$output .= apply_filters( 'gallery_style',
"div.$classString ul li { $thumbs; }\n");
}
if ( $caps1 ) {
$output .= apply_filters( 'gallery_style',
"div.$classString span.caption1 { $caps1; }\n");
}
if ( $caps2 ) {
$output .= apply_filters( 'gallery_style',
"div.$classString span.caption2 { $caps2; }\n");
}
$output .= apply_filters( 'gallery_style',"</style>\n" );
}
return $output;
}
//==============================================================================//
// function prepareGalleriaScript(): prepares custom galleria handler //
// //
// original source code is "demo_01.htm" ( galleria-1.0b demo sample ) //
//==============================================================================//
function prepareGalleriaScript( $classString,
$shrink = 0,
$playbutton = 0,
$interval = 5.0 ) {
$intervalMilliSec = (int) ($interval * 1000);
$output = "";
$output .= apply_filters( 'gallery_style',
"<script type='text/javascript'>
var timerID;
jQuery('ul.$classString').galleria( {
shrink : $shrink,
history : false,
clickNext : true,
insert : '#main_stage',
onImage : function( image, caption1, caption2, thumb ) {
image.css('display','none').fadeIn(1000);
caption1.css('display','none').fadeIn(100);
caption2.css('display','none').fadeIn(100);
var _li = thumb.parents('li');
_li.siblings().children('img.selected').fadeTo(500,0.5);
thumb.fadeTo('fast',1).addClass('selected');
image.attr('title','Click to Show Next image >>');
},
onThumb : function( thumb ) {
var _li = thumb.parents('li');
var _fadeTo = _li.is('.active') ? '1' : '0.5';
thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
thumb.hover(
function() { thumb.fadeTo('fast',1); },
function() { _li.not('.active').children('img').fadeTo('fast',0.5); }
)
}
});
// show navigation items
jQuery('div.$classString p.galleria_nav').css('display', 'block' );
//
if ( $playbutton == 1 ) {
jQuery('button.play').css('display', 'block' );
// automatic navigation
jQuery('button.play').click( function() {
if ( jQuery(this).text() == 'Play' ) {
jQuery(this).text('Stop');
timerID =setInterval( 'jQuery.galleria.next();', $intervalMilliSec );
} else {
clearInterval( timerID );
jQuery(this).text('Play');
}
} );
} else {
// hide the play button
jQuery('button.play').css('display', 'none' );
}
// add keybord navigation [ May 04th 2008 by Y2 ]
jQuery(document).keydown( function (e) {
// if ( e.which == 39 ) {
// // press [ Right Arrow ] key
// jQuery.galleria.next();
// } else if ( e.which == 37 ) {
// // press
[ Left Arrow ] key
// jQuery.galleria.prev();
// }
if ( e.which == 27 && ($playbutton == 1) ) { // Escape Key
if ( jQuery('button.play').text() == 'Stop' ) {
clearInterval( timerID );
jQuery('button.play').text('Play');
}
}
});
</script>\n");
return $output;
}
//==========================================================================//
// following three functions are minor modification version of //
// the "/wp-include/media.php" //
//==========================================================================//
function _wp_get_attachment_image( $attachment_id,
$size='thumbnail',
$icon = false ) {
$html = '';
$image = _wp_get_attachment_image_src($attachment_id, $size, $icon);
if ( $image ) {
list($src, $width, $height) = $image;
$hwstring = image_hwstring($width, $height);
if ( is_array($size) )
$size = join('x', $size);
$html = '<img src="'.attribute_escape($src).'"
'.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />';
}
return $html;
}
function _wp_get_attachment_image_src( $attachment_id,
$size='thumbnail',
$icon = false ) {
// get a thumbnail or intermediate image if there is one
if ( $image = _image_downsize($attachment_id, $size) )
return $image;
if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {
$icon_dir =
apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );
$src_file = $icon_dir . '/' . basename($src);
@list($width, $height) = getimagesize($src_file);
}
if ( $src && $width && $height )
return array( $src, $width, $height );
return false;
}
function _image_downsize($id, $size = 'medium') {
if ( !wp_attachment_is_image($id) )
return false;
$img_url = wp_get_attachment_url($id);
$meta = wp_get_attachment_metadata($id);
$width = $height = 0;
// plugins can use this to provide resize services
if ( $out = apply_filters('_image_downsize', false, $id, $size) )
return $out;
// try for a new style intermediate size
if ( $intermediate = image_get_intermediate_size($id, $size) ) {
$img_url = str_replace(basename($img_url), $intermediate['file'], $img_url);
$width = $intermediate['width'];
$height = $intermediate['height'];
}
elseif ( $size == 'thumbnail' ) {
// fall back to the old thumbnail
$thumb_file = wp_get_attachment_thumb_file( $id ); // modified by Y2
if ( $info = getimagesize($thumb_file) ) { //
$img_url = str_replace(basename($img_url), basename($thumb_file), $img_url);
$width = $info[0];
$height = $info[1];
}
}
if ( !$width && !$height && isset($meta['width'], $meta['height']) ) {
// any other type: use the real image and constrain it
list( $width, $height ) =
image_constrain_size_for_editor( $meta['width'],
$meta['height'], $size );
}
if ( $img_url)
return array( $img_url, $width, $height );
return false;
}
?>