Advertisement
Guest User

Untitled

a guest
Jun 24th, 2014
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.70 KB | None | 0 0
  1. <?php
  2. /**
  3. Plugin Name: NextGen 2.0 Search engine
  4. Description: Add a search function for the images of NextGEN Gallery
  5. Author: Alex Rabe
  6. Version: 0.8.0
  7. Author URI: http://alexrabe.de/
  8. Plugin URI: http://alexrabe.de/
  9.  
  10. Copyright (C) 2010 - 2011 Alex Rabe
  11. @package NextGEN Gallery
  12. **/
  13.  
  14. /** Please refer to http://alexrabe.de/2010/01/13/search-for-images/ for code snippet for integration into search.php **/
  15.  
  16. class nggSearchEngine {
  17.  
  18. var $search_result = false;
  19. var $found_images = false;
  20. var $out = false;
  21.  
  22. function nggSearchEngine() {
  23. return $this->__construct();
  24. }
  25.  
  26. function __construct() {
  27.  
  28. // no NextGEN Gallery, no addon
  29. if (!class_exists('nggGallery') )
  30. return;
  31.  
  32. add_action('parse_request', array(&$this, 'search_query') );
  33. add_action('ngg_get_permalink', array(&$this, 'create_pagination') , 10, 2 );
  34. }
  35.  
  36. /**
  37. * Main search for images
  38. *
  39. * @return void
  40. **/
  41. function search_query( $wp ) {
  42. global $nggdb;
  43.  
  44. if ( array_key_exists('s', $wp->query_vars) && !empty($wp->query_vars['s']) ) {
  45.  
  46. $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' ));
  47.  
  48. if ( !empty($this->search_result) )
  49. $this->found_images = true;
  50.  
  51. }
  52.  
  53. }
  54.  
  55. /**
  56. * Return the output
  57. *
  58. * @return $output HTML content
  59. **/
  60. function return_result( $template = '') {
  61.  
  62. if ( $this->found_images )
  63. $this->out = nggCreateGallery($this->search_result, false, $template);
  64.  
  65. $this->out = apply_filters('ngg_show_search_result', $this->out);
  66.  
  67. return $this->out;
  68. }
  69.  
  70. /**
  71. * Modify the pagination for search results
  72. *
  73. * @param string $url
  74. * @param string $args
  75. * @return void
  76. */
  77. function create_pagination( $url, $args = '') {
  78. global $wp_rewrite, $wp_query;
  79.  
  80. $search = get_query_var('s');
  81.  
  82. // we just look for search results
  83. if ( !empty( $search ) ) {
  84.  
  85. $url = htmlspecialchars( add_query_arg( $args ) );
  86.  
  87. }
  88.  
  89. return $url;
  90.  
  91. }
  92. }
  93.  
  94. // Start this plugin once all other plugins are fully loaded
  95. add_action( 'plugins_loaded', create_function( '', 'global $nggSearch; $nggSearch = new nggSearchEngine();' ) );
  96.  
  97. /**
  98. * Template call for the frontend search, just use this code snippet in your theme template
  99. * <?php if ( function_exists( 'ngg_images_results' ) ) ngg_images_results(); ?>
  100. *
  101. * @param string $template if you would like to show the result with a differentlayout, just take a template
  102. * @return search output
  103. */
  104. function ngg_images_results( $template = '') {
  105. global $nggSearch;
  106.  
  107. echo $nggSearch->return_result( $template );
  108. }
  109.  
  110. function have_images() {
  111. global $nggSearch;
  112.  
  113. return $nggSearch->found_images;
  114. }
  115.  
  116. function nggCreateGallery($picturelist, $galleryID = false, $template = '', $images = false) {
  117. global $nggRewrite;
  118. if (class_exists("nggRewrite")) $nggRewrite=new nggRewrite();
  119.  
  120. $ngg_options = nggGallery::get_option('ngg_options');
  121.  
  122. //the shortcode parameter will override global settings, TODO: rewrite this to a class
  123. $ngg_options['galImages'] = ( $images === false ) ? $ngg_options['galImages'] : (int) $images;
  124.  
  125. $current_pid = false;
  126.  
  127. // $_GET from wp_query
  128. $nggpage = get_query_var('nggpage');
  129. $pageid = get_query_var('pageid');
  130. $pid = get_query_var('pid');
  131.  
  132. // in case of permalinks the pid is a slug, we need the id
  133. if( !is_numeric($pid) && !empty($pid) ) {
  134. $picture = nggdb::find_image($pid);
  135. $pid = $picture->pid;
  136. }
  137.  
  138. // we need to know the current page id
  139. $current_page = (get_the_ID() == false) ? 0 : get_the_ID();
  140.  
  141. if ( !is_array($picturelist) )
  142. $picturelist = array($picturelist);
  143.  
  144. // Populate galleries values from the first image
  145. $first_image = current($picturelist);
  146. $gallery = new stdclass;
  147. $gallery->ID = (int) $galleryID;
  148. $gallery->show_slideshow = false;
  149. $gallery->show_piclens = false;
  150. $gallery->name = stripslashes ( $first_image->name );
  151. $gallery->title = stripslashes( $first_image->title );
  152. $gallery->description = html_entity_decode(stripslashes( $first_image->galdesc));
  153. $gallery->pageid = $first_image->pageid;
  154. $gallery->anchor = 'ngg-gallery-' . $galleryID . '-' . $current_page;
  155. reset($picturelist);
  156.  
  157. $maxElement = $ngg_options['galImages'];
  158. $thumbwidth = $ngg_options['thumbwidth'];
  159. $thumbheight = $ngg_options['thumbheight'];
  160.  
  161. // fixed width if needed
  162. $gallery->columns = intval($ngg_options['galColumns']);
  163. $gallery->imagewidth = ($gallery->columns > 0) ? 'style="width:' . floor(100/$gallery->columns) . '%;"' : '';
  164.  
  165. // obsolete in V1.4.0, but kept for compat reason
  166. // pre set thumbnail size, from the option, later we look for meta data.
  167. $thumbsize = ($ngg_options['thumbfix']) ? $thumbsize = 'width="' . $thumbwidth . '" height="'.$thumbheight . '"' : '';
  168.  
  169. // show slideshow link
  170. if ($galleryID) {
  171. if ($ngg_options['galShowSlide']) {
  172. $gallery->show_slideshow = true;
  173. $gallery->slideshow_link = $nggRewrite->get_permalink(array ( 'show' => 'slide') );
  174. $gallery->slideshow_link_text = nggGallery::i18n($ngg_options['galTextSlide']);
  175. }
  176.  
  177. if ($ngg_options['usePicLens']) {
  178. $gallery->show_piclens = true;
  179. $gallery->piclens_link = "javascript:PicLensLite.start({feedUrl:'" . htmlspecialchars( nggMediaRss::get_gallery_mrss_url($gallery->ID) ) . "'});";
  180. }
  181. }
  182.  
  183. // check for page navigation
  184. if ($maxElement > 0) {
  185.  
  186. if ( !is_home() || $pageid == $current_page )
  187. $page = ( !empty( $nggpage ) ) ? (int) $nggpage : 1;
  188. else
  189. $page = 1;
  190.  
  191. $start = $offset = ( $page - 1 ) * $maxElement;
  192.  
  193. $total = count($picturelist);
  194.  
  195. //we can work with display:hidden for some javascript effects
  196. if (!$ngg_options['galHiddenImg']){
  197. // remove the element if we didn't start at the beginning
  198. if ($start > 0 )
  199. array_splice($picturelist, 0, $start);
  200.  
  201. // return the list of images we need
  202. array_splice($picturelist, $maxElement);
  203. }
  204.  
  205. $nggNav = new nggNavigation;
  206. $navigation = $nggNav->create_navigation($page, $total, $maxElement);
  207. } else {
  208. $navigation = '<div class="ngg-clear"></div>';
  209. }
  210.  
  211. //we cannot use the key as index, cause it's filled with the pid
  212. $index = 0;
  213. foreach ($picturelist as $key => $picture) {
  214.  
  215. //needed for hidden images (THX to Sweigold for the main idea at : http://wordpress.org/support/topic/228743/ )
  216. $picturelist[$key]->hidden = false;
  217. $picturelist[$key]->style = $gallery->imagewidth;
  218.  
  219. if ($maxElement > 0 && $ngg_options['galHiddenImg']) {
  220. if ( ($index < $start) || ($index > ($start + $maxElement -1)) ){
  221. $picturelist[$key]->hidden = true;
  222. $picturelist[$key]->style = ($gallery->columns > 0) ? 'style="width:' . floor(100/$gallery->columns) . '%;display: none;"' : 'style="display: none;"';
  223. }
  224. $index++;
  225. }
  226.  
  227. // get the effect code
  228. /*if ($galleryID)
  229. $thumbcode = ($ngg_options['galImgBrowser']) ? '' : $picture->get_thumbcode('set_' . $galleryID);
  230. else
  231. $thumbcode = ($ngg_options['galImgBrowser']) ? '' : $picture->get_thumbcode(get_the_title());
  232. */
  233. // create link for imagebrowser and other effects
  234. $args ['nggpage'] = empty($nggpage) || ($template != 'carousel') ? false : $nggpage; // only needed for carousel mode
  235. $args ['pid'] = ($ngg_options['usePermalinks']) ? $picture->image_slug : $picture->pid;
  236. $picturelist[$key]->pidlink = $nggRewrite->get_permalink( $args );
  237.  
  238. // generate the thumbnail size if the meta data available
  239. if ( isset($picturelist[$key]->meta_data['thumbnail']) && is_array ($size = $picturelist[$key]->meta_data['thumbnail']) )
  240. $thumbsize = 'width="' . $size['width'] . '" height="' . $size['height'] . '"';
  241.  
  242. // choose link between imagebrowser or effect
  243. $link = ($ngg_options['galImgBrowser']) ? $picturelist[$key]->pidlink : $picture->imageURL;
  244. // bad solution : for now we need the url always for the carousel, should be reworked in the future
  245. $picturelist[$key]->url = $picture->imageURL;
  246. // add a filter for the link
  247. $picturelist[$key]->imageURL = apply_filters('ngg_create_gallery_link', $link, $picture);
  248. $picturelist[$key]->thumbnailURL = $picture->thumbURL;
  249. $picturelist[$key]->size = $thumbsize;
  250. $picturelist[$key]->thumbcode = $thumbcode;
  251. $picturelist[$key]->caption = ( empty($picture->description) ) ? '&nbsp;' : html_entity_decode ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  252. $picturelist[$key]->description = ( empty($picture->description) ) ? ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->description, 'pic_' . $picture->pid . '_description')) );
  253. $picturelist[$key]->alttext = ( empty($picture->alttext) ) ? ' ' : htmlspecialchars ( stripslashes(nggGallery::i18n($picture->alttext, 'pic_' . $picture->pid . '_alttext')) );
  254.  
  255. // filter to add custom content for the output
  256. $picturelist[$key] = apply_filters('ngg_image_object', $picturelist[$key], $picture->pid);
  257.  
  258. //check if $pid is in the array
  259. if ($picture->pid == $pid)
  260. $current_pid = $picturelist[$key];
  261. }
  262. reset($picturelist);
  263.  
  264. //for paged galleries, take the first image in the array if it's not in the list
  265. $current_pid = ( empty($current_pid) ) ? current( $picturelist ) : $current_pid;
  266.  
  267. // look for gallery-$template.php or pure gallery.php
  268. $filename = ( empty($template) ) ? 'gallery' : 'gallery-' . $template;
  269.  
  270. //filter functions for custom addons
  271. $gallery = apply_filters( 'ngg_gallery_object', $gallery, $galleryID );
  272. $picturelist = apply_filters( 'ngg_picturelist_object', $picturelist, $galleryID );
  273.  
  274. //additional navigation links
  275. $next = ( empty($nggNav->next) ) ? false : $nggNav->next;
  276. $prev = ( empty($nggNav->prev) ) ? false : $nggNav->prev;
  277.  
  278. // create the output
  279. $out = nggGallery::capture ( $filename, array ('gallery' => $gallery, 'images' => $picturelist, 'pagination' => $navigation, 'current' => $current_pid, 'next' => $next, 'prev' => $prev) );
  280.  
  281. // apply a filter after the output
  282. $out = apply_filters('ngg_gallery_output', $out, $picturelist);
  283.  
  284. return $out;
  285. }
  286.  
  287. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement