BMo-Design's Mootools Javascript ScrollGallery as a Plugin for the Wordpress NextGEN Gallery. Author: Benedikt Morschheuser Author URI: http://bmo-design.de/ Version: 2.0.2 Copyright 2011-2012 by Benedikt Morschheuser (http://bmo-design.de/kontakt/) 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 http://wordpress.org/about/gpl/ ################################################################# The current version used the scrollGallery2 1.10. with mobile extension ################################################################# */ // Restrictions if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) { die('You are not allowed to call this page directly.'); } //############################################################### define('SG2_SITEBASE_URL' , get_option('siteurl')); define('SG2_PLUGINNAME', trim(plugin_basename(dirname(__FILE__)))); define('SG2_URL', get_option('siteurl').'/wp-content/plugins/' . dirname(plugin_basename(__FILE__))); // get_bloginfo('wpurl') define('SG2_BASEPATH', WP_PLUGIN_DIR.'/'. dirname(plugin_basename(__FILE__))); //############################################################### require_once (SG2_BASEPATH . '/admin/admin.php'); class scrollGallery2 { private $sgversion = "2.0.2"; //Version private $sgJSArray = array(); public function scrollGallery2ReplaceShortcode($atts, $content = '') { //new Version, see http://codex.wordpress.org/Shortcode_API global $wpdb; //neue Version: $sgconfig = $this->get_SGConfiguration($atts); if( !is_numeric($sgconfig["galleryID"]) ) $sgconfig["galleryID"] = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $sgconfig["galleryID"]) ); if( !empty($sgconfig["galleryID"]) ) $out = $this->SGShow($sgconfig); else $out = '[Gallery not found]'; return $out.$content; } public function scrollGallery2ReplaceShortcode_thumbsSolo($atts, $content = '') { //new Version, see http://codex.wordpress.org/Shortcode_API global $wpdb; //neue Version: $sgconfig = $this->get_SGConfiguration($atts); if( !is_numeric($sgconfig["galleryID"]) ) $sgconfig["galleryID"] = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $sgconfig["galleryID"]) ); if( !empty($sgconfig["galleryID"]) ) $out = $this->SGShow($sgconfig, NULL, true, false); else $out = '[Gallery not found]'; return $out.$content; } public function scrollGallery2ReplaceShortcode_imagesSolo($atts, $content = '') { //new Version, see http://codex.wordpress.org/Shortcode_API global $wpdb; //neue Version: $sgconfig = $this->get_SGConfiguration($atts); if( !is_numeric($sgconfig["galleryID"]) ) $sgconfig["galleryID"] = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $sgconfig["galleryID"]) ); if( !empty($sgconfig["galleryID"]) ) $out = $this->SGShow($sgconfig, NULL, false, true); else $out = '[Gallery not found]'; return $out.$content; } public function get_SGConfiguration($data) { //build sgconfig from parameter and options $options = get_option("SG2_Options"); //extract sub arrays in $data clickable="clickable=true,next_button=true,prev_button=true,button_opacity=100" -> clickable=array('clickable'=>true,'next_button'=>true,'prev_button'=>true, 'button_opacity'=>100) foreach( $data as $key => $value ) { if(stripos($value,',')!==false){ $dataSubarrKomma = explode(',',str_replace(" ","", $value)); $dataSubarr = array(); foreach ($dataSubarrKomma as $k => $v){ $dataSubarrGleich = explode('=', $v); $dataSubarr[$dataSubarrGleich[0]]=$dataSubarrGleich[1]; } $data[$key]=$dataSubarr; } } // $sgconfig = array(); $sgconfig["galleryID"] = (int) ( (array_key_exists("id" , $data))? $data["id"] :0 ); $sgconfig["start"] = (int) ( (array_key_exists("start" , $data))? $data["start"] : $options["SG2_start"] ); $sgconfig["area"] = (int) ( (array_key_exists("area" , $data))? $data["area"] : $options["SG2_area"] ); $sgconfig["thumbarea"] = (string) ( (array_key_exists("thumbarea" , $data))? $data["thumbarea"] : $options["SG2_thumbarea"] ); $sgconfig["imagearea"] = (string) ( (array_key_exists("imagearea" , $data))? $data["imagearea"] : $options["SG2_imagearea"] ); $sgconfig["speed"] = (string) ( (array_key_exists("speed" , $data))? $data["speed"] : $options["SG2_speed"] ); $sgconfig["clickable"] = (array) ( (array_key_exists("clickable" , $data)&&is_array($data["clickable"]))?$data["clickable"] : $options["SG2_clickable"] ); $sgconfig["thumbArrows"] = (array) ( (array_key_exists("thumbarrows" , $data)&&is_array($data["thumbarrows"]))?$data["thumbarrows"] : $options["SG2_thumbArrows"] ); $sgconfig["autoScroll"] = (bool) ( (array_key_exists("autoscroll", $data))?($data["autoscroll"] == 'false'?false:true): $options["SG2_autoScroll"] ); $sgconfig["enableSwipeMode"] = (bool) ( (array_key_exists("enableswipemode", $data))?($data["enableswipemode"] == 'false'?false:true): $options["SG2_enableSwipeMode"] ); $sgconfig["followImages"] = (bool) ( (array_key_exists("followimages", $data))?($data["followimages"] == 'false'?false:true): $options["SG2_followImages"] ); $sgconfig["useCaptions"] = (bool) ( (array_key_exists("usecaptions", $data))?($data["usecaptions"] == 'false'?false:true): $options["SG2_useCaptions"] ); $sgconfig["useDesc"] = (bool) ( (array_key_exists("usedesc", $data))?($data["usedesc"] == 'false'?false:true): $options["SG2_useDesc"] ); $sgconfig["thumbsdown"] = (bool) ( (array_key_exists("thumbsdown", $data))?($data["thumbsdown"] == 'false'?false:true): $options["SG2_thumbsdown"] ); $sgconfig["diashow"] = (bool) ( (array_key_exists("diashow", $data))?($data["diashow"] == 'false'?false:true): $options["SG2_diashow"] ); $sgconfig["diashowDelay"] = (int) ( (array_key_exists("diashowdelay" , $data))? $data["diashowdelay"] : $options["SG2_diashowDelay"] ); $sgconfig["thumbOpacity"] = (int) ( (array_key_exists("thumbopacity" , $data))? $data["thumbopacity"] : $options["SG2_thumbOpacity"] ); $sgconfig["width"] = (int) ( (array_key_exists("width" , $data))? $data["width"] : $options["SG2_width"] ); $sgconfig["height"] = (int) ( (array_key_exists("height" , $data))? $data["height"] : $options["SG2_height"] ); //$sgconfig["design"] = (string) $options["SG2_design"] ;//page should use only one design //margins $dimensions = array("px","em","%","pt"); $margins = explode(" ", trim(str_replace($dimensions,"",$options["SG2_design_margin"])));//we only work with px $marginsT = explode(" ", trim(str_replace($dimensions,"",$options["SG2_design_thumbmargin"])));//we only work with px $sgconfig["margin_top"] =$margins[0]; $sgconfig["margin_right"] =$margins[1]; $sgconfig["margin_bottom"] =$margins[2]; $sgconfig["margin_left"] =$margins[3]; #todo SG2_design_margin wird bisher nicht genutzt bei design, geht alles über max-width % $sgconfig["margin_t_top"] =$marginsT[0]; $sgconfig["margin_t_right"] =$marginsT[1]; $sgconfig["margin_t_bottom"]=$marginsT[2]; $sgconfig["margin_t_left"] =$marginsT[3]; return $sgconfig; } public function SGShow($sgconfig, $pictures = NULL, $buildThumbs = true, $buildImages = true) { global $wpdb; extract($sgconfig); // Get the pictures if ($galleryID) { $ngg_options = get_option ('ngg_options'); //NextGenGallery Options $pictures = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.gid = '$galleryID' AND tt.exclude != 1 ORDER BY tt.$ngg_options[galSort] $ngg_options[galSortDir] "); $finalInformation = array(); foreach($pictures as $picture) { $getInformation = array(); $getInformation["title"] = stripslashes($picture->alttext); // $picture->alttext; $getInformation["desc"] = $picture->description; $getInformation["link"] = SG2_SITEBASE_URL . "/" . $picture->path ."/" . $picture->filename; $getInformation["img"] = SG2_SITEBASE_URL . "/" . $picture->path ."/" . $picture->filename; $getInformation["img_abs_path"] = ABSPATH . $picture->path ."/" . $picture->filename; $getInformation["thumb"] = SG2_SITEBASE_URL . "/" . $picture->path ."/thumbs/thumbs_" . $picture->filename; //metaData $serialized_data = unserialize($picture->meta_data); $getInformation["width"] = $serialized_data["width"]; $getInformation["height"] = $serialized_data["height"]; $getInformation["thumbWidth"] = $serialized_data["thumbnail"]["width"]; $getInformation["thumbHeight"] = $serialized_data["thumbnail"]["height"]; $finalInformation[] = $getInformation; } $pictures = $finalInformation; } else { $galleryID = rand();//falls pictures als parameter übergeben werden } if (empty($pictures)) return ""; //only if we have output enque the registered scripts wp_enqueue_script('mootools'); wp_enqueue_script('scrollGallery2'); wp_enqueue_script('powertools'); //JS $scriptout = 'var scrollGallery2Obj'.$galleryID.' = new scrollGallery2({scrollGalleryContainer:"scrollGallery2_'.$galleryID.'",'; if(is_numeric($start)) $scriptout .= 'start:'.$start.','; if($area) $scriptout .= 'area:'.$area.','; if(!$thumbarea||$thumbarea=="") $thumbarea="thumbarea"; $scriptout .= 'thumbarea:"'.$thumbarea.'_'.$galleryID.'",'; if(!$imagearea||$imagearea=="") $imagearea="imagearea"; $scriptout .= 'imagearea:"'.$imagearea.'_'.$galleryID.'",'; if($speed) $scriptout .= 'speed:'.$speed.','; if(!$clickable['clickable']) $scriptout .= 'clickable:false,'; if($autoScroll) $scriptout .= 'autoScroll:true,'; if($enableSwipeMode) $scriptout .= 'enableSwipeMode:true,'; if(!$followImages) $scriptout .= 'followImages:false,'; # if(...) $scriptout .= 'toElementClass:"img",';// img, standard ist li if($diashow&&$diashowDelay) $scriptout .= 'diashowDelay:'.$diashowDelay.','; if($thumbOpacity<100&&$thumbOpacity>0) $scriptout .= 'thumbOpacity:'.$thumbOpacity.','; $scriptout = substr($scriptout, 0, -1); // Remove last , $scriptout .= '}); '; $this->sgJSArray[$galleryID] = $scriptout; #save JS for later in foot //HTML $imageAreaWidth=0; $thumbAreaWidth=0; $thumbAreaHeight=0; foreach ($pictures as $picture){ if ($picture["img"]) { $imageAreaWidth+=($width+$margin_left+$margin_right); $thumbAreaWidth+=($picture["thumbWidth"]+$margin_t_left+$margin_t_right); if($thumbAreaHeight<($picture["thumbHeight"]+$margin_t_top+$margin_t_bottom)) $thumbAreaHeight=($picture["thumbHeight"]+$margin_t_top+$margin_t_bottom); } } $outT = ''; //Thumbs out if($buildThumbs==true){ $outT .= '
'; } $outI = ''; //Images out if($buildImages==true){ $outI .= '
'; } //Buttons $button_width="50%"; $outButtonsI=''; if(!($clickable['prev_button']&&$clickable['next_button']))$button_width="100%"; if($clickable['next_button']) $outButtonsI .= ''; if($clickable['prev_button']) $outButtonsI .= ''; $outButtonsT=''; if($thumbArrows['thumbArrows']&&!$autoScroll) $outButtonsT .= '
'; $out=''; //Build out with $thumbsdown==true or false? $out .= '
'; if(!$thumbsdown==true){ if($buildThumbs==true) $out .= '
'.$outT.$outButtonsT.'
'; if($buildImages==true) $out .= '
'.$outI.$outButtonsI.'
'; }else{ if($buildImages==true) $out .= '
'.$outI.$outButtonsI.'
'; if($buildThumbs==true) $out .= '
'.$outT.$outButtonsT.'
'; } $out .= '
'; return $out; } public function SG2Foot(){ if (sizeof($this->sgJSArray)> 0){ echo ''; } } public function SG2_get_picdescmeta($picdescmeta){ //BSP [scrollGallery imglink="http://www.bla.com" thumblink="http://www.blub.com" imglink_target="_blank" thumblink_target="_blank"] $metaArray = array("imglink_target"=>"'_self'","thumblink_target"=>"'_self'"); $splitContent = $this->SGFindStringBetween($picdescmeta, "[scrollGallery", "]"); (array_key_exists(0, $splitContent) ? $begin = $splitContent[0] :$begin=""); (array_key_exists(1, $splitContent) ? $middle = $splitContent[1] :$middle=""); (array_key_exists(2, $splitContent) ? $end = $splitContent[2] :$end=""); if ($begin == $picdescmeta) return NULL; //nichts gefunden $middleValues = substr($middle, 0, -1); // Remove last brackets $middleValues = explode("[scrollGallery", $middleValues); $middleValues = explode(" ",trim($middleValues[1])); foreach($middleValues as $value) { if(preg_match("/=/",$value)){ list($key, $value) = explode("=", $value, 2);//2 damit nur erstes = geteilt wird if (trim($key) != "") $metaArray[trim(strtolower($key))] = stripslashes(trim($value)); } } return $metaArray; } public function SGFindStringBetween($text, $begin, $end) { if ( ($posBegin = stripos($text, $begin )) === false) return Array($text, ""); if ( ($posEnd = stripos($text, $end, $posBegin)) === false) return Array($text, ""); $textBegin = (string) substr($text, 0, $posBegin); $textMiddle = (string) substr($text, $posBegin, $posEnd - $posBegin + strlen($end) ); $textEnd = (string) substr($text, $posEnd + strlen($end) , strlen($text)); return Array($textBegin, $textMiddle, $textEnd); } public function SG2RegisterPluginLinks($links, $file) { $plugin = plugin_basename(__FILE__); if ($file == $plugin) { return array_merge( $links, array( sprintf( '%s', SG2_PLUGINNAME."-admin", __('Settings') ), '' ) ); } return $links; } public function SG2_enqueueScripts(){//in footer wp_register_script( 'mootools', SG2_URL.'/scrollGallery/js/mootools-core-1.3.2-full-compat.js', false, '1.3.2',false); wp_register_script( 'scrollGallery2', SG2_URL.'/scrollGallery/js/scrollGallery2.js', array('mootools'), '1.12',false); wp_register_script( 'powertools', SG2_URL.'/scrollGallery/js/powertools-mobile-1.1.1.js', array('mootools'), '1.1.1',false); } public function SG2Head(){ $options = get_option("SG2_Options"); wp_register_style('scrollGallery2', SG2_URL.'/scrollGallery/css/scrollGallery.css',false,$this->sgversion,'screen'); wp_register_style('scrollGallery2Design', SG2_URL.'/scrollGallery/css/'.$options['SG2_design'],array('scrollGallery2'),$this->sgversion,'screen'); if (function_exists('wp_enqueue_style')) { wp_enqueue_style('scrollGallery2'); wp_enqueue_style('scrollGallery2Design'); } echo ''; } //install function activation() { if(get_option("SG_Options")&&!get_option("SG2_Options")){//upgrade support $options=get_option("SG_Options"); $newoptions = array(); foreach( $options as $key => $option ) { if(strpos($key,"SG_")!==false) $newoptions[str_replace('SG_', 'SG2_', $key)]=$option; else $newoptions[$key]=$option; } $newoptions['SG2_design']='scrollGallery_greyDesign.css';//overwrite $newoptions['SG2_design_margin']='0px 0px 0px 0px'; $newoptions['SG2_design_thumbmargin']='10px 10px 10px 10px'; add_option("SG2_Options",$newoptions); } //normal installation $options = array( 'SG2_design'=>'scrollGallery_greyDesign.css', 'SG2_design_margin'=>'0px 0px 0px 0px', 'SG2_design_thumbmargin'=>'10px 10px 10px 10px', 'SG2_start'=>'0', 'SG2_area'=>'200', 'SG2_thumbarea'=>'thumbarea', 'SG2_imagearea'=>'imagearea', 'SG2_speed'=>'0.1', 'SG2_clickable'=> array('clickable'=>true,'next_button'=>false,'prev_button'=>false, 'button_opacity'=>100), 'SG2_thumbArrows'=>array('thumbArrows'=>false,'button_opacity'=>100), 'SG2_autoScroll'=> true, 'SG2_enableSwipeMode'=> true, 'SG2_followImages'=>false, 'SG2_useCaptions'=> false, 'SG2_useDesc'=> false, 'SG2_thumbsdown'=> false, 'SG2_diashow'=> false, 'SG2_diashowDelay'=> '4', 'SG2_thumbOpacity'=>'100', 'SG2_width'=>'640', 'SG2_height'=>'480'); add_option("SG2_Options",$options);//It does nothing if the option already exists. } function deactivation() { wp_deregister_script(array('mootools')); wp_deregister_script(array('scrollGallery2')); wp_deregister_script(array('powertools')); } } $scrollGallery2 = new scrollGallery2(); $scrollGallery2admin = new scrollGallery2admin($scrollGallery2); if (isset($scrollGallery2)&&isset($scrollGallery2admin)) { // Plugin installieren bei aktivate register_activation_hook( __FILE__, array($scrollGallery2, 'activation')); register_deactivation_hook(__FILE__, array($scrollGallery2, 'deactivation')); // add_action('admin_menu' , array($scrollGallery2admin, 'admin_menu'));//add menu add_action('admin_init', array($scrollGallery2admin, 'admin_init'));//init settings for Admin Page add_filter('plugin_row_meta', array($scrollGallery2,'SG2RegisterPluginLinks'), 10, 2 ); //replace the shortcode, everywhere, new Version add_shortcode('scrollGallery', array($scrollGallery2, 'scrollGallery2ReplaceShortcode')); add_shortcode('ScrollGallery', array($scrollGallery2, 'scrollGallery2ReplaceShortcode')); // add_shortcode('sG_thumbsSolo', array($scrollGallery2, 'scrollGallery2ReplaceShortcode_thumbsSolo')); add_shortcode('sG_imagesSolo', array($scrollGallery2, 'scrollGallery2ReplaceShortcode_imagesSolo')); //add_filter('the_excerpt', array($scrollGallery2, 'scrollGallery2Replace'));//replace content in the_excerpt, Uncomment it on your own risk //add css add_action('wp_enqueue_scripts', array($scrollGallery2,'SG2_enqueueScripts')); add_action('wp_head' , array($scrollGallery2,'SG2Head'),1); add_action('wp_footer' , array($scrollGallery2,'SG2Foot'),100); } //Deinstall (outside Class) if ( function_exists('register_uninstall_hook') ) register_uninstall_hook(__FILE__, 'deinstallSG2'); function deinstallSG2() { delete_option("SG2_Options"); } ?>