<?php
/*
Plugin Name: Photospace
Plugin URI:
Description: A image gallery plugin for WordPress
Author: Dean Oakley
Author URI:
Version: 9999.9.9
*/
/* Copyright 2010 Dean Oakley (email : contact@deanoakley.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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
*/
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
die('Illegal Entry');
}
//============================== Photospace options ========================//
class photospace_plugin_options {
function PS_getOptions() {
$options = get_option('ps_options');
if (!is_array($options)) {
$options['use_paging'] = false;
$options['enable_history'] = false;
$options['num_thumb'] = '9';
$options['show_captions'] = false;
$options['show_download'] = false;
$options['show_controls'] = false;
$options['show_bg'] = false;
$options['auto_play'] = false;
$options['delay'] = 3500;
$options['button_size'] = 50;
$options['hide_thumbs'] = false;
$options['reset_css'] = false;
$options['thumbnail_margin'] = 10;
$options['thumbnail_width'] = 50;
$options['thumbnail_height'] = 50;
$options['thumbnail_crop'] = true;
$options['thumb_col_width'] = '181';
$options['main_col_width'] = '400';
$options['main_col_height'] = '500';
$options['gallery_width'] = '600';
$options['play_text'] = 'Play Slideshow';
$options['pause_text'] = 'Pause Slideshow';
$options['previous_text'] = '‹ Previous Photo';
$options['next_text'] = 'Next Photo ›';
$options['download_text'] = 'Download Original';
update_option('ps_options', $options);
}
return $options;
}
function update() {
if(isset($_POST['ps_save'])) {
$options = photospace_plugin_options::PS_getOptions();
$options['num_thumb'] = stripslashes($_POST['num_thumb']);
$options['thumbnail_margin'] = stripslashes($_POST['thumbnail_margin']);
$options['thumbnail_width'] = stripslashes($_POST['thumbnail_width']);
$options['thumbnail_height'] = stripslashes($_POST['thumbnail_height']);
$options['thumb_col_width'] = stripslashes($_POST['thumb_col_width']);
$options['main_col_width'] = stripslashes($_POST['main_col_width']);
$options['main_col_height'] = stripslashes($_POST['main_col_height']);
$options['gallery_width'] = stripslashes($_POST['gallery_width']);
$options['delay'] = stripslashes($_POST['delay']);
$options['button_size'] = stripslashes($_POST['button_size']);
if (isset($_POST['enable_history'])) {
$options['enable_history'] = (bool)true;
} else {
$options['enable_history'] = (bool)false;
}
if (isset($_POST['use_paging'])) {
$options['use_paging'] = (bool)true;
} else {
$options['use_paging'] = (bool)false;
}
if (isset($_POST['thumbnail_crop'])) {
$options['thumbnail_crop'] = (bool)true;
} else {
$options['thumbnail_crop'] = (bool)false;
}
if (isset($_POST['show_controls'])) {
$options['show_controls'] = (bool)true;
} else {
$options['show_controls'] = (bool)false;
}
if (isset($_POST['show_download'])) {
$options['show_download'] = (bool)true;
} else {
$options['show_download'] = (bool)false;
}
if (isset($_POST['show_captions'])) {
$options['show_captions'] = (bool)true;
} else {
$options['show_captions'] = (bool)false;
}
if (isset($_POST['show_bg'])) {
$options['show_bg'] = (bool)true;
} else {
$options['show_bg'] = (bool)false;
}
if (isset($_POST['auto_play'])) {
$options['auto_play'] = (bool)true;
} else {
$options['auto_play'] = (bool)false;
}
if (isset($_POST['hide_thumbs'])) {
$options['hide_thumbs'] = (bool)true;
} else {
$options['hide_thumbs'] = (bool)true;
}
if (isset($_POST['reset_css'])) {
$options['reset_css'] = (bool)true;
} else {
$options['reset_css'] = (bool)false;
}
$options['play_text'] = stripslashes($_POST['play_text']);
$options['pause_text'] = stripslashes($_POST['pause_text']);
$options['previous_text'] = stripslashes($_POST['previous_text']);
$options['next_text'] = stripslashes($_POST['next_text']);
$options['download_text'] = stripslashes($_POST['download_text']);
update_option('ps_options', $options);
} else {
photospace_plugin_options::PS_getOptions();
}
add_menu_page('Photospace options', 'Photospace Gallery Options', 'edit_themes', basename(__FILE__), array('photospace_plugin_options', 'display'));
}
function display() {
$options = photospace_plugin_options::PS_getOptions();
?>
<div class="wrap">
<h2>Photospace Options</h2>
<form method="post" action="#" enctype="multipart/form-data">
<div class="wp-menu-separator" style="clear:both; padding-bottom:15px; border-bottom:solid 1px #e6e6e6" ></div>
<h3><label><input name="show_download" type="checkbox" value="checkbox" <?php if($options['show_download']) echo "checked='checked'"; ?> /> Show download link</label></h3>
<h3><label><input name="show_controls" type="checkbox" value="checkbox" <?php if($options['show_controls']) echo "checked='checked'"; ?> /> Show controls (play slide show / Next Prev image links)</label></h3>
<h3><label><input name="use_paging" type="checkbox" value="checkbox" <?php if($options['use_paging']) echo "checked='checked'"; ?> /> Use paging </label></h3>
<h3><label><input name="enable_history" type="checkbox" value="checkbox" <?php if($options['enable_history']) echo "checked='checked'"; ?> /> Enable history </label></h3>
<h3><label><input name="show_captions" type="checkbox" value="checkbox" <?php if($options['show_captions']) echo "checked='checked'"; ?> /> Show Title / Caption / Desc under image</label></h3>
<h3><label><input name="reset_css" type="checkbox" value="checkbox" <?php if($options['reset_css']) echo "checked='checked'"; ?> /> Try to clear current theme image css / formatting</label></h3>
<h3><label><input name="show_bg" type="checkbox" value="checkbox" <?php if($options['show_bg']) echo "checked='checked'"; ?> /> Show background colours for layout testing</label></h3>
<div style="clear:both; padding-bottom:15px; border-bottom:solid 1px #e6e6e6" ></div>
<p><input class="button-primary" type="submit" name="ps_save" value="Save Changes" /></p>
</form>
</div>
<?php
}
}
function PS_getOption($option) {
global $mytheme;
return $mytheme->option[$option];
}
// register functions
add_action('admin_menu', array('photospace_plugin_options', 'update'));
$options = get_option('ps_options');
add_theme_support( 'post-thumbnails' );
add_image_size('photospace_thumbnails', $options['thumbnail_width'], $options['thumbnail_height'], $options['thumbnail_crop']);
add_image_size('photospace_full', $options['main_col_width'], $options['main_col_height']);
//============================== insert HTML header tag ========================//
function photospace_scripts_method() {
wp_enqueue_script('jquery');
$photospace_wp_plugin_path = site_url()."/wp-content/plugins/photospace";
//wp_enqueue_style( 'photospace-styles', $photospace_wp_plugin_path . '/gallery.css');
wp_enqueue_script( 'galleriffic', $photospace_wp_plugin_path . '/jquery.galleriffic.js');
}
add_action('wp_enqueue_scripts', 'photospace_scripts_method');
function photospace_scripts_method_history() {
$photospace_wp_plugin_path = site_url()."/wp-content/plugins/photospace";
wp_enqueue_script( 'history', $photospace_wp_plugin_path . '/jquery.history.js');
}
if ($options['enable_history']) {
add_action('wp_enqueue_scripts', 'photospace_scripts_method_history');
}
function photospace_wp_headers() {
}
//add_action( 'wp_head', 'photospace_wp_headers', 10 );
add_shortcode( 'photospace', 'photospace_shortcode' );
function photospace_shortcode( $atts ) {
global $post;
$options = get_option('ps_options');
extract(shortcode_atts(array(
'id' => intval($post->ID),
'num_thumb' => 9,
'num_preload' => 9,
'show_captions' => $options['show_captions'],
'show_download' => $options['show_download'],
'show_controls' => $options['show_controls'],
'auto_play' => 'true',
'delay' => $options['delay'],
'hide_thumbs' => 'false',
'use_paging' => 'true',
'horizontal_thumb' => 0,
'include' => '',
'exclude' => '',
'sync_transitions' => 1
), $atts));
$post_id = intval($post->ID);
$hide_thumb_style = '';
if($hide_thumbs){
$hide_thumb_style = 'hide_me';
}
$thumb_style_init = 'display:none';
$thumb_style_on = "'display', 'block'";
$thumb_style_off = "'display', 'none'";
$photospace_wp_plugin_path = site_url()."/wp-content/plugins/photospace";
$output_buffer ='<div class="clear"></div>
<div id="gallery_'.$post_id.'" class="photospace">
<div class="thumbs_wrap2">
<div class="thumbs_wrap">
<div id="thumbs_'.$post_id.'">';
if($horizontal_thumb){
$output_buffer .='<a class="pageLink prev" style="'. $thumb_style_init . '" href="#" title="Previous Page"></a>';
}
$output_buffer .='<ul class="thumbs noscript">';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order') );
}
if ( !empty($attachments) ) {
foreach ( $attachments as $aid => $attachment ) {
$img = wp_get_attachment_image_src( $aid , 'gal');
$thumb = wp_get_attachment_image_src( $aid , 'gal-thumb');
$full = wp_get_attachment_image_src( $aid , 'gal');
$_post = & get_post($aid);
$image_title = esc_attr($_post->post_title);
$image_alttext = get_post_meta($aid, '_wp_attachment_image_alt', true);
$image_caption = $_post->post_excerpt;
$image_description = $_post->post_content;
$output_buffer .='<li><a class="thumb" href="' . $img[0] . '" title="' . $image_title . '" >
<img src="' . $thumb[0] . '" alt="' . $image_alttext . '" title="' . $image_title . '" width="60" height="60" />
</a>';
$output_buffer .='<div class="caption">';
if($show_captions){
if($image_caption != ''){
$output_buffer .='<div class="image-caption">' . $image_caption . '</div>';
}
}
$output_buffer .='
</div>';
$output_buffer .='
</li>';
}
}
$output_buffer .='</ul>';
if(!$horizontal_thumb){
$output_buffer .='
<a class="pageLink prev" style="'.$thumb_style_init.'" href="#" title="Previous Page"></a>';
}
$output_buffer .='<a class="pageLink next" style="'.$thumb_style_init.'" href="#" title="Next Page"></a>
</div></div></div><div class="gal_content">';
if($show_controls){
$output_buffer .='<div id="controls_'.$post_id.'" class="controls"></div>';
}
$output_buffer .='<div class="slideshow-container">
<div id="loading_'.$post_id.'" class="loader"></div>
<div id="slideshow_'.$post_id.'" class="slideshow"></div>
<div id="caption_'.$post_id.'" class="caption-container"></div>
</div>
</div><div class="clear"></div></div>';
$output_buffer .= "<script type='text/javascript'>
jQuery(document).ready(function($) {
// We only want these styles applied when javascript is enabled
";
if(!$horizontal_thumb){
$output_buffer .= "$('.thumnail_col').css('width', '". $options['thumb_col_width'] . "px');";
}
$output_buffer .= "
// Initialize Advanced Galleriffic Gallery
var gallery = $('#thumbs_".$post_id."').galleriffic({
delay: 3000,
numThumbs: 9,
preloadAhead: 9,
enableTopPager: false,
enableBottomPager: true,
imageContainerSel: '#slideshow_".$post_id."',
controlsContainerSel: '#controls_".$post_id."',
captionContainerSel: '#caption_".$post_id."',
loadingContainerSel: '#loading_".$post_id."',
renderSSControls: true,
renderNavControls: true,
playLinkText: '". $options['play_text'] ."',
pauseLinkText: '". $options['pause_text'] ."',
prevLinkText: 'Prev',
nextLinkText: 'Next',
nextPageLinkText: 'Next',
prevPageLinkText: 'Prev',
enableHistory: " . intval($options['enable_history']) . ",
autoStart: true,
enableKeyboardNavigation: true,
syncTransitions: " . intval($sync_transitions) . ",
defaultTransitionDuration: 300,
onTransitionOut: function(slide, caption, isSync, callback) {
slide.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0, callback);
caption.fadeTo(this.getDefaultTransitionDuration(isSync), 0.0);
},
onTransitionIn: function(slide, caption, isSync) {
var duration = this.getDefaultTransitionDuration(isSync);
slide.fadeTo(duration, 1.0);
// Position the caption at the bottom of the image and set its opacity
var slideImage = slide.find('img');
caption.width(slideImage.width())
.css({
//'bottom' : Math.floor((slide.height() - slideImage.outerHeight()) / 2 - 40),
'top' : slideImage.outerHeight(),
'left' : Math.floor((slide.width() - slideImage.width()) / 2) + slideImage.outerWidth() - slideImage.width()
})
.fadeTo(duration, 1.0);
},
onPageTransitionOut: function(callback) {
this.hide();
setTimeout(callback, 100); // wait a bit
},
onPageTransitionIn: function() {
var prevPageLink = this.find('a.prev').css(".$thumb_style_off.");
var nextPageLink = this.find('a.next').css(".$thumb_style_off.");
// Show appropriate next / prev page links
if (this.displayedPage > 0)
prevPageLink.css(".$thumb_style_on.");
var lastPage = this.getNumPages() - 1;
if (this.displayedPage < lastPage)
nextPageLink.css(".$thumb_style_on.");
this.fadeTo('fast', 1.0);
}
});
";
if ($options['enable_history']) {
$output_buffer .= "
/**** Functions to support integration of galleriffic with the jquery.history plugin ****/
// PageLoad function
// This function is called when:
// 1. after calling $.historyInit();
// 2. after calling $.historyLoad();
// 3. after pushing Go Back button of a browser
function pageload(hash) {
// alert('pageload: ' + hash);
// hash doesn't contain the first # character.
if(hash) {
$.galleriffic.gotoImage(hash);
} else {
gallery.gotoIndex(0);
}
}
// Initialize history plugin.
// The callback is called at once by present location.hash.
$.historyInit(pageload, 'advanced.html');
// set onlick event for buttons using the jQuery 1.3 live method
$('a[rel=history]').live('click', function(e) {
if (e.button != 0) return true;
var hash = this.href;
hash = hash.replace(/^.*#/, '');
// moves to a new page.
// pageload is called at once.
$.historyLoad(hash);
return false;
});
/****************************************************************************************/
";
}
$output_buffer .= "
/**************** Event handlers for custom next / prev page links **********************/
gallery.find('a.prev').click(function(e) {
gallery.previousPage();
e.preventDefault();
});
gallery.find('a.next').click(function(e) {
gallery.nextPage();
e.preventDefault();
});
});
</script>";
return $output_buffer;
}