NextGEN_shortcodes

The NextCellent Gallery NextGEN shortcodes class.

Defined (1)

The class is defined in the following location(s).

/lib/shortcodes.php  
  1. class NextGEN_shortcodes { 
  2.  
  3. function __construct() 
  4. //Long posts should require a higher limit, see http://core.trac.wordpress.org/ticket/8553 
  5. @ini_set('pcre.backtrack_limit', 500000); 
  6.  
  7. // convert the old shortcode 
  8. add_filter('the_content', array(&$this, 'convert_shortcode')); 
  9. add_filter('loop_start', array(&$this, 'reset_globals')); 
  10.  
  11. // do_shortcode on the_excerpt could causes several unwanted output. Uncomment it on your own risk 
  12. // add_filter('the_excerpt', array(&$this, 'convert_shortcode')); 
  13. // add_filter('the_excerpt', 'do_shortcode', 11); 
  14.  
  15. add_shortcode( 'singlepic', array(&$this, 'show_singlepic' ) ); 
  16. add_shortcode( 'album', array(&$this, 'show_album' ) ); 
  17. add_shortcode( 'nggalbum', array(&$this, 'show_album' ) ); 
  18. add_shortcode( 'nggallery', array(&$this, 'show_gallery') ); 
  19. add_shortcode( 'imagebrowser', array(&$this, 'show_imagebrowser' ) ); 
  20. add_shortcode( 'slideshow', array(&$this, 'show_slideshow' ) ); 
  21. add_shortcode( 'nggtags', array(&$this, 'show_tags' ) ); 
  22. add_shortcode( 'thumb', array(&$this, 'show_thumbs' ) ); 
  23. add_shortcode( 'random', array(&$this, 'show_random' ) ); 
  24. add_shortcode( 'recent', array(&$this, 'show_recent' ) ); 
  25. add_shortcode( 'tagcloud', array(&$this, 'show_tagcloud' ) ); 
  26.  
  27. function reset_globals() 
  28. unset($GLOBALS['subalbum']); 
  29. unset($GLOBALS['nggShowGallery']); 
  30.  
  31. /** 
  32. * NextGEN_shortcodes::convert_shortcode() 
  33. * convert old shortcodes to the new WordPress core style 
  34. * [gallery=1] ->> [nggallery id=1] 
  35. * @param string $content Content to search for shortcodes 
  36. * @return string Content with new shortcodes. 
  37. */ 
  38. function convert_shortcode($content) { 
  39.  
  40. $ngg_options = nggGallery::get_option('ngg_options'); 
  41.  
  42. if ( stristr( $content, '[singlepic' )) { 
  43. $search = "@\[singlepic=(\d+)(|, \d+|, )(|, \d+|, )(|, watermark|, web20|, )(|, right|, center|, left|, )\]@i"; 
  44. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  45.  
  46. foreach ($matches as $match) { 
  47. // remove the comma 
  48. $match[2] = ltrim($match[2], ', '); 
  49. $match[3] = ltrim($match[3], ', '); 
  50. $match[4] = ltrim($match[4], ', '); 
  51. $match[5] = ltrim($match[5], ', '); 
  52. $replace = "[singlepic id=\"{$match[1]}\" w=\"{$match[2]}\" h=\"{$match[3]}\" mode=\"{$match[4]}\" float=\"{$match[5]}\" ]"; 
  53. $content = str_replace ($match[0], $replace, $content); 
  54.  
  55. if ( stristr( $content, '[album' )) { 
  56. $search = "@(?:<p>)*\s*\[album\s*=\s*(\w+|^\+)(|, extend|, compact)\]\s*(?:</p>)*@i"; 
  57. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  58.  
  59. foreach ($matches as $match) { 
  60. // remove the comma 
  61. $match[2] = ltrim($match[2], ', '); 
  62. $replace = "[album id=\"{$match[1]}\" template=\"{$match[2]}\"]"; 
  63. $content = str_replace ($match[0], $replace, $content); 
  64.  
  65. if ( stristr( $content, '[gallery' )) { 
  66. $search = "@(?:<p>)*\s*\[gallery\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i"; 
  67. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  68.  
  69. foreach ($matches as $match) { 
  70. $replace = "[nggallery id=\"{$match[1]}\"]"; 
  71. $content = str_replace ($match[0], $replace, $content); 
  72.  
  73. if ( stristr( $content, '[imagebrowser' )) { 
  74. $search = "@(?:<p>)*\s*\[imagebrowser\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i"; 
  75. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  76.  
  77. foreach ($matches as $match) { 
  78. $replace = "[imagebrowser id=\"{$match[1]}\"]"; 
  79. $content = str_replace ($match[0], $replace, $content); 
  80.  
  81. if ( stristr( $content, '[slideshow' )) { 
  82. $search = "@(?:<p>)*\s*\[slideshow\s*=\s*(\w+|^\+)(|, (\d+)|, )(|, (\d+))\]\s*(?:</p>)*@i"; 
  83. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  84.  
  85. foreach ($matches as $match) { 
  86. // remove the comma 
  87. $match[3] = ltrim($match[3], ', '); 
  88. $match[5] = ltrim($match[5], ', '); 
  89. $replace = "[slideshow id=\"{$match[1]}\" w=\"{$match[3]}\" h=\"{$match[5]}\"]"; 
  90. $content = str_replace ($match[0], $replace, $content); 
  91.  
  92. if ( stristr( $content, '[tags' )) { 
  93. $search = "@(?:<p>)*\s*\[tags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i"; 
  94. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  95.  
  96. foreach ($matches as $match) { 
  97. //$replace = "[nggtags gallery=\"{$match[1]}\"]"; 
  98. $replace = "[nggtags gallery=\"{$match[1]}\" template=\"{$match[2]}\"]"; 
  99. $content = str_replace ($match[0], $replace, $content); 
  100.  
  101. if ( stristr( $content, '[albumtags' )) { 
  102. $search = "@(?:<p>)*\s*\[albumtags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i"; 
  103. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) { 
  104.  
  105. foreach ($matches as $match) { 
  106. $replace = "[nggtags album=\"{$match[1]}\"]"; 
  107. $content = str_replace ($match[0], $replace, $content); 
  108.  
  109. // attach related images based on category or tags 
  110. if ($ngg_options['activateTags']) 
  111. $content .= nggShowRelatedImages(); 
  112.  
  113. return $content; 
  114.  
  115. /** 
  116. * Function to show a single picture: 
  117. * [singlepic id="10" float="none|left|right" width="" height="" mode="none|watermark|web20" link="url" "template="filename" /] 
  118. * where 
  119. * - id is one picture id 
  120. * - float is the CSS float property to apply to the thumbnail 
  121. * - width is width of the single picture you want to show (original width if this parameter is missing) 
  122. * - height is height of the single picture you want to show (original height if this parameter is missing) 
  123. * - mode is one of none, watermark or web20 (transformation applied to the picture) 
  124. * - link is optional and could link to a other url instead the full image 
  125. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view 
  126. * If the tag contains some text, this will be inserted as an additional caption to the picture too. Example: 
  127. * [singlepic id="10"]This is an additional caption[/singlepic] 
  128. * This tag will show a picture with under it two HTML span elements containing respectively the alttext of the picture 
  129. * and the additional caption specified in the tag. 
  130. * @param array $atts 
  131. * @param string $caption text 
  132. * @return the content 
  133. */ 
  134. function show_singlepic( $atts, $content = '' ) { 
  135.  
  136. extract(shortcode_atts(array( 
  137. 'id' => 0,  
  138. 'w' => '',  
  139. 'h' => '',  
  140. 'mode' => '',  
  141. 'float' => '',  
  142. 'link' => '',  
  143. 'template' => '' 
  144. ), $atts )); 
  145.  
  146. $out = nggSinglePicture($id, $w, $h, $mode, $float, $template, $content, $link); 
  147.  
  148. return $out; 
  149.  
  150. /** 
  151. * Function to show a collection of galleries: 
  152. * [album id="1, 2, 4, 5, ..." template="filename" gallery="filename" /] 
  153. * where 
  154. * - id of a album 
  155. * - template is a name for a album template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view 
  156. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view 
  157. * @param array $atts 
  158. * @return the_content 
  159. */ 
  160. function show_album( $atts ) { 
  161.  
  162. extract(shortcode_atts(array( 
  163. 'id' => 0,  
  164. 'template' => 'extend',  
  165. 'gallery' => '' 
  166. ), $atts )); 
  167.  
  168. $out = nggShowAlbum($id, $template, $gallery); 
  169.  
  170. return $out; 
  171. /** 
  172. * Function to show a thumbnail or a set of thumbnails with shortcode of type: 
  173. * [gallery id="1, 2, 4, 5, ..." template="filename" images="number of images per page" /] 
  174. * where 
  175. * - id of a gallery 
  176. * - images is the number of images per page (optional), 0 will show all images 
  177. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view 
  178. * @param array $atts 
  179. * @return the_content 
  180. */ 
  181. function show_gallery( $atts ) { 
  182.  
  183. global $wpdb; 
  184.  
  185. extract(shortcode_atts(array( 
  186. 'id' => 0,  
  187. 'template' => '',  
  188. 'images' => false 
  189. ), $atts )); 
  190.  
  191. // backward compat for user which uses the name instead, still deprecated 
  192. if( !is_numeric($id) ) 
  193. $id = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $id) ); 
  194.  
  195. $out = nggShowGallery( $id, $template, $images ); 
  196.  
  197. return $out; 
  198.  
  199. function show_imagebrowser( $atts ) { 
  200.  
  201. global $wpdb; 
  202.  
  203. extract(shortcode_atts(array( 
  204. 'id' => 0,  
  205. 'template' => '' 
  206. ), $atts )); 
  207.  
  208. $out = nggShowImageBrowser($id, $template); 
  209.  
  210. return $out; 
  211.  
  212. /** 
  213. * Render a slideshow. 
  214. * @since 1.9.25 Don't use extract anymore. @see https://core.trac.wordpress.org/ticket/22400 
  215. * @param $atts array The shortcode attributes. 
  216. * @return string The output that will be displayed on the page. 
  217. */ 
  218. function show_slideshow( $atts ) { 
  219.  
  220. $data = shortcode_atts( array( 
  221. 'id' => 'random',  
  222. 'w' => null,  
  223. 'h' => null,  
  224. 'dots' => null 
  225. ), $atts ); 
  226.  
  227. array_map( 'esc_attr', $data ); 
  228.  
  229. if( isset($data['w']) || isset($data['h'])) { 
  230. $data['autodim'] = false; 
  231. $data['width'] = $data['w']; 
  232. $data['height']= $data['h']; 
  233. } else { 
  234. unset($data['w'], $data['h']); 
  235.  
  236. if($data['dots'] == null) { 
  237. unset($data['dots']); 
  238. } else { 
  239. $data['nav_dots'] = $data['dots']; 
  240. try { 
  241. return nggShowSlideshow( $data['id'], $data ); 
  242. } catch (NGG_Not_Found $e) { 
  243. return $e->getMessage(); 
  244.  
  245. /** 
  246. * nggtags shortcode implementation 
  247. * 20140120: Improved: template option. 
  248. * Reference: based on improvement of Tony Howden's code 
  249. * http://howden.net.au/thowden/2012/12/nextgen-gallery-wordpress-nggtags-template-caption-option/ 
  250. * Included template to galleries and albums 
  251. * Included sorting mode: ASC/DESC/RAND 
  252. * @param $atts 
  253. * @return $out 
  254. */ 
  255. function show_tags( $atts ) { 
  256.  
  257. extract(shortcode_atts(array( 
  258. 'gallery' => '',  
  259. 'album' => '',  
  260. 'template' => '',  
  261. 'sort' => '' 
  262. ), $atts )); 
  263.  
  264. //gallery/album contains tag list comma separated of terms to filtering out. 
  265. //Counterintuitive: I'd like something like tags='red, green' and then to specify album/gallery instead. 
  266. $modes = array ('ASC', 'DESC', 'RAND'); 
  267.  
  268. $sorting = strtoupper($sort); 
  269.  
  270. if (!in_array(strtoupper($sorting), $modes)) { 
  271. $sorting = 'NOTSET'; 
  272.  
  273. if ( !empty($album) ) 
  274. $out = nggShowAlbumTags ($album, $template, $sorting); 
  275. else 
  276. $out = nggShowGalleryTags($gallery, $template, $sorting); 
  277. return $out; 
  278.  
  279. /** 
  280. * Function to show a thumbnail or a set of thumbnails with shortcode of type: 
  281. * [thumb id="1, 2, 4, 5, ..." template="filename" /] 
  282. * where 
  283. * - id is one or more picture ids 
  284. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view 
  285. * @param array $atts 
  286. * @return the_content 
  287. */ 
  288. function show_thumbs( $atts ) { 
  289.  
  290. extract(shortcode_atts(array( 
  291. 'id' => '',  
  292. 'template' => '' 
  293. ), $atts)); 
  294.  
  295. // make an array out of the ids 
  296. $pids = explode( ', ', $id ); 
  297.  
  298. // Some error checks 
  299. if ( count($pids) == 0 ) 
  300. return __('[Pictures not found]', 'nggallery'); 
  301.  
  302. $picturelist = nggdb::find_images_in_list( $pids ); 
  303.  
  304. // show gallery 
  305. if ( is_array($picturelist) ) 
  306. $out = nggCreateGallery($picturelist, false, $template); 
  307. return $out; 
  308.  
  309. /** 
  310. * Function to show a gallery of random or the most recent images with shortcode of type: 
  311. * [random max="7" template="filename" id="2" /] 
  312. * [recent max="7" template="filename" id="3" mode="date" /] 
  313. * where 
  314. * - max is the maximum number of random or recent images to show 
  315. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view 
  316. * - id is the gallery id, if the recent/random pictures shall be taken from a specific gallery only 
  317. * - mode is either "id" (which takes the latest additions to the databse, default) 
  318. * or "date" (which takes the latest pictures by EXIF date) 
  319. * or "sort" (which takes the pictures by user sort order) 
  320. * @param array $atts 
  321. * @return the_content 
  322. */ 
  323. function show_random( $atts ) { 
  324.  
  325. extract(shortcode_atts(array( 
  326. 'max' => '',  
  327. 'template' => '',  
  328. 'id' => 0 
  329. ), $atts)); 
  330.  
  331. $out = nggShowRandomRecent('random', $max, $template, $id); 
  332.  
  333. return $out; 
  334.  
  335. function show_recent( $atts ) { 
  336.  
  337. extract(shortcode_atts(array( 
  338. 'max' => '',  
  339. 'template' => '',  
  340. 'id' => 0,  
  341. 'mode' => 'id' 
  342. ), $atts)); 
  343.  
  344. $out = nggShowRandomRecent($mode, $max, $template, $id); 
  345.  
  346. return $out; 
  347.  
  348. /** 
  349. * Shortcode for the Image tag cloud 
  350. * Usage : [tagcloud template="filename" /] 
  351. * @param array $atts 
  352. * @return the content 
  353. */ 
  354. function show_tagcloud( $atts ) { 
  355.  
  356. extract(shortcode_atts(array( 
  357. 'template' => '' 
  358. ), $atts)); 
  359.  
  360. $out = nggTagCloud( '', $template ); 
  361.  
  362. return $out; 
  363.