Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- Plugin Name: NextGen 2.0 Search engine
- Description: Add a search function for the images of NextGEN Gallery
- Author: Alex Rabe
- Version: 0.8.0
- Author URI: http://alexrabe.de/
- Plugin URI: http://alexrabe.de/
- Copyright (C) 2010 - 2011 Alex Rabe
- @package NextGEN Gallery
- **/
- /** Please refer to http://alexrabe.de/2010/01/13/search-for-images/ for code snippet for integration into search.php **/
- class nggSearchEngine {
- var $search_result = false;
- var $found_images = false;
- var $out = false;
- function nggSearchEngine() {
- return $this->__construct();
- }
- function __construct() {
- // no NextGEN Gallery, no addon
- if (!class_exists('nggGallery') )
- return;
- add_action('parse_request', array(&$this, 'search_query') );
- add_action('ngg_get_permalink', array(&$this, 'create_pagination') , 10, 2 );
- }
- /**
- * Main search for images
- *
- * @return void
- **/
- function search_query( $wp ) {
- global $nggdb;
- if ( array_key_exists('s', $wp->query_vars) && !empty($wp->query_vars['s']) ) {
- $this->search_result = array_merge( (array) $nggdb->search_for_images( $wp->query_vars['s'] ), (array) nggTags::find_images_for_tags( $wp->query_vars['s'] , 'ASC' ));
- if ( !empty($this->search_result) )
- $this->found_images = true;
- }
- }
- /**
- * Return the output
- *
- * @return $output HTML content
- **/
- function return_result( $template = '') {
- if ( $this->found_images )
- $this->out = nggCreateGallery($this->search_result, false, $template);
- $this->out = apply_filters('ngg_show_search_result', $this->out);
- return $this->out;
- }
- /**
- * Modify the pagination for search results
- *
- * @param string $url
- * @param string $args
- * @return void
- */
- function create_pagination( $url, $args = '') {
- global $wp_rewrite, $wp_query;
- $search = get_query_var('s');
- // we just look for search results
- if ( !empty( $search ) ) {
- $url = htmlspecialchars( add_query_arg( $args ) );
- }
- return $url;
- }
- }
- // Start this plugin once all other plugins are fully loaded
- add_action( 'plugins_loaded', create_function( '', 'global $nggSearch; $nggSearch = new nggSearchEngine();' ) );
- /**
- * Template call for the frontend search, just use this code snippet in your theme template
- * <?php if ( function_exists( 'ngg_images_results' ) ) ngg_images_results(); ?>
- *
- * @param string $template if you would like to show the result with a differentlayout, just take a template
- * @return search output
- */
- function ngg_images_results( $template = '') {
- global $nggSearch;
- echo $nggSearch->return_result( $template );
- }
- function have_images() {
- global $nggSearch;
- return $nggSearch->found_images;
- }
- function nggCreateGallery($picturelist, $galleryID = false, $template = '', $images = false) {
- global $nggRewrite;
- if (class_exists("nggRewrite")) $nggRewrite=new nggRewrite();
- $ngg_options = nggGallery::get_option('ngg_options');
- //the shortcode parameter will override global settings, TODO: rewrite this to a class
- $ngg_options['galImages'] = ( $images === false ) ? $ngg_options['galImages'] : (int) $images;
- $current_pid = false;
- // $_GET from wp_query
- $nggpage = get_query_var('nggpage');
- $pageid = get_query_var('pageid');
- $pid = get_query_var('pid');
- // in case of permalinks the pid is a slug, we need the id
- if( !is_numeric($pid) && !empty($pid) ) {
- $picture = nggdb::find_image($pid);
- $pid = $picture->pid;
- }
- // we need to know the current page id
- $current_page = (get_the_ID() == false) ? 0 : get_the_ID();
- if ( !is_array($picturelist) )
- $picturelist = array($picturelist);
- // Populate galleries values from the first image
- $first_image = current($picturelist);
- $gallery = new stdclass;
- $gallery->ID = (int) $galleryID;
- $gallery->show_slideshow = false;
- $gallery->show_piclens = false;
- $gallery->name = stripslashes ( $first_image->name );
- $gallery->title = stripslashes( $first_image->title );
- $gallery->description = html_entity_decode(stripslashes( $first_image->galdesc));
- $gallery->pageid = $first_image->pageid;
- $gallery->anchor = 'ngg-gallery-' . $galleryID . '-' . $current_page;
- reset($picturelist);
- $maxElement = $ngg_options['galImages'];
- $thumbwidth = $ngg_options['thumbwidth'];
- $thumbheight = $ngg_options['thumbheight'];
- // fixed width if needed
- $gallery->columns = intval($ngg_options['galColumns']);
- $gallery->imagewidth = ($gallery->columns > 0) ? 'style="width:' . floor(100/$gallery->columns) . '%;"' : '';
- // obsolete in V1.4.0, but kept for compat reason
- // pre set thumbnail size, from the option, later we look for meta data.
- $thumbsize = ($ngg_options['thumbfix']) ? $thumbsize = 'width="' . $thumbwidth . '" height="'.$thumbheight . '"' : '';
- // show slideshow link
- if ($galleryID) {
- if ($ngg_options['galShowSlide']) {
- $gallery->show_slideshow = true;
- $gallery->slideshow_link = $nggRewrite->get_permalink(array ( 'show' => 'slide') );
- $gallery->slideshow_link_text = nggGallery::i18n($ngg_options['galTextSlide']);
- }
- if ($ngg_options['usePicLens']) {
- $gallery->show_piclens = true;
- $gallery->piclens_link = "javascript:PicLensLite.start({feedUrl:'" . htmlspecialchars( nggMediaRss::get_gallery_mrss_url($gallery->ID) ) . "'});";
- }
- }
- // check for page navigation
- if ($maxElement > 0) {
- if ( !is_home() || $pageid == $current_page )
- $page = ( !empty( $nggpage ) ) ? (int) $nggpage : 1;
- else
- $page = 1;
- $start = $offset = ( $page - 1 ) * $maxElement;
- $total = count($picturelist);
- //we can work with display:hidden for some javascript effects
- if (!$ngg_options['galHiddenImg']){
- // remove the element if we didn't start at the beginning
- if ($start > 0 )
- array_splice($picturelist, 0, $start);
- // return the list of images we need
- array_splice($picturelist, $maxElement);
- }
- $nggNav = new nggNavigation;
- $navigation = $nggNav->create_navigation($page, $total, $maxElement);
- } else {
- $navigation = '<div class="ngg-clear"></div>';
- }
- //we cannot use the key as index, cause it's filled with the pid
- $index = 0;
- foreach ($picturelist as $key => $picture) {
- //needed for hidden images (THX to Sweigold for the main idea at : http://wordpress.org/support/topic/228743/ )
- $picturelist[$key]->hidden = false;
- $picturelist[$key]->style = $gallery->imagewidth;
- if ($maxElement > 0 && $ngg_options['galHiddenImg']) {
- if ( ($index < $start) || ($index > ($start + $maxElement -1)) ){
- $picturelist[$key]->hidden = true;
- $picturelist[$key]->style = ($gallery->columns > 0) ? 'style="width:' . floor(100/$gallery->columns) . '%;display: none;"' : 'style="display: none;"';
- }
- $index++;
- }
- // get the effect code
- /*if ($galleryID)
- $thumbcode = ($ngg_options['galImgBrowser']) ? '' : $picture->get_thumbcode('set_' . $galleryID);
- else
- $thumbcode = ($ngg_options['galImgBrowser']) ? '' : $picture->get_thumbcode(get_the_title());
- */
- // create link for imagebrowser and other effects
- $args ['nggpage'] = empty($nggpage) || ($template != 'carousel') ? false : $nggpage; // only needed for carousel mode
- $args ['pid'] = ($ngg_options['usePermalinks']) ? $picture->image_slug : $picture->pid;
- $picturelist[$key]->pidlink = $nggRewrite->get_permalink( $args );
- // generate the thumbnail size if the meta data available
- if ( isset($picturelist[$key]->meta_data['thumbnail']) && is_array ($size = $picturelist[$key]->meta_data['thumbnail']) )
- $thumbsize = 'width="' . $size['width'] . '" height="' . $size['height'] . '"';
- // choose link between imagebrowser or effect
- $link = ($ngg_options['galImgBrowser']) ? $picturelist[$key]->pidlink : $picture->imageURL;
- // bad solution : for now we need the url always for the carousel, should be reworked in the future
- $picturelist[$key]->url = $picture->imageURL;
- // add a filter for the link
- $picturelist[$key]->imageURL = apply_filters('ngg_create_gallery_link', $link, $picture);
- $picturelist[$key]->thumbnailURL = $picture->thumbURL;
- $picturelist[$key]->size = $thumbsize;
- $picturelist[$key]->thumbcode = $thumbcode;
- $picturelist[$key]->caption = ( empty($picture->description) ) ? ' ' : html_entity_decode ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
- $picturelist[$key]->description = ( empty($picture->description) ) ? ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
- $picturelist[$key]->alttext = ( empty($picture->alttext) ) ? ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) );
- // filter to add custom content for the output
- $picturelist[$key] = apply_filters('ngg_image_object', $picturelist[$key], $picture->pid);
- //check if $pid is in the array
- if ($picture->pid == $pid)
- $current_pid = $picturelist[$key];
- }
- reset($picturelist);
- //for paged galleries, take the first image in the array if it's not in the list
- $current_pid = ( empty($current_pid) ) ? current( $picturelist ) : $current_pid;
- // look for gallery-$template.php or pure gallery.php
- $filename = ( empty($template) ) ? 'gallery' : 'gallery-' . $template;
- //filter functions for custom addons
- $gallery = apply_filters( 'ngg_gallery_object', $gallery, $galleryID );
- $picturelist = apply_filters( 'ngg_picturelist_object', $picturelist, $galleryID );
- //additional navigation links
- $next = ( empty($nggNav->next) ) ? false : $nggNav->next;
- $prev = ( empty($nggNav->prev) ) ? false : $nggNav->prev;
- // create the output
- $out = nggGallery::capture ( $filename, array ('gallery' => $gallery, 'images' => $picturelist, 'pagination' => $navigation, 'current' => $current_pid, 'next' => $next, 'prev' => $prev) );
- // apply a filter after the output
- $out = apply_filters('ngg_gallery_output', $out, $picturelist);
- return $out;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement