PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/shortcodes.php

https://bitbucket.org/benkeen/nextgen-gallery
PHP | 388 lines | 203 code | 74 blank | 111 comment | 23 complexity | af7cf42e62a0cd6ee04cd1fa8394112b MD5 | raw file
  1. <?php
  2. /**
  3. * @author Alex Rabe, Vincent Prat
  4. *
  5. * @since 1.0.0
  6. * @description Use WordPress Shortcode API for more features
  7. * @Docs http://codex.wordpress.org/Shortcode_API
  8. */
  9. class NextGEN_shortcodes {
  10. // register the new shortcodes
  11. function NextGEN_shortcodes() {
  12. //Long posts should require a higher limit, see http://core.trac.wordpress.org/ticket/8553
  13. @ini_set('pcre.backtrack_limit', 500000);
  14. // convert the old shortcode
  15. add_filter('the_content', array(&$this, 'convert_shortcode'));
  16. // do_shortcode on the_excerpt could causes several unwanted output. Uncomment it on your own risk
  17. // add_filter('the_excerpt', array(&$this, 'convert_shortcode'));
  18. // add_filter('the_excerpt', 'do_shortcode', 11);
  19. add_shortcode( 'singlepic', array(&$this, 'show_singlepic' ) );
  20. add_shortcode( 'album', array(&$this, 'show_album' ) );
  21. add_shortcode( 'nggallery', array(&$this, 'show_gallery') );
  22. add_shortcode( 'imagebrowser', array(&$this, 'show_imagebrowser' ) );
  23. add_shortcode( 'slideshow', array(&$this, 'show_slideshow' ) );
  24. add_shortcode( 'nggtags', array(&$this, 'show_tags' ) );
  25. add_shortcode( 'thumb', array(&$this, 'show_thumbs' ) );
  26. add_shortcode( 'random', array(&$this, 'show_random' ) );
  27. add_shortcode( 'recent', array(&$this, 'show_recent' ) );
  28. add_shortcode( 'tagcloud', array(&$this, 'show_tagcloud' ) );
  29. }
  30. /**
  31. * NextGEN_shortcodes::convert_shortcode()
  32. * convert old shortcodes to the new WordPress core style
  33. * [gallery=1] ->> [nggallery id=1]
  34. *
  35. * @param string $content Content to search for shortcodes
  36. * @return string Content with new shortcodes.
  37. */
  38. function convert_shortcode($content) {
  39. $ngg_options = nggGallery::get_option('ngg_options');
  40. if ( stristr( $content, '[singlepic' )) {
  41. $search = "@\[singlepic=(\d+)(|,\d+|,)(|,\d+|,)(|,watermark|,web20|,)(|,right|,center|,left|,)\]@i";
  42. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  43. foreach ($matches as $match) {
  44. // remove the comma
  45. $match[2] = ltrim($match[2], ',');
  46. $match[3] = ltrim($match[3], ',');
  47. $match[4] = ltrim($match[4], ',');
  48. $match[5] = ltrim($match[5], ',');
  49. $replace = "[singlepic id=\"{$match[1]}\" w=\"{$match[2]}\" h=\"{$match[3]}\" mode=\"{$match[4]}\" float=\"{$match[5]}\" ]";
  50. $content = str_replace ($match[0], $replace, $content);
  51. }
  52. }
  53. }
  54. if ( stristr( $content, '[album' )) {
  55. $search = "@(?:<p>)*\s*\[album\s*=\s*(\w+|^\+)(|,extend|,compact)\]\s*(?:</p>)*@i";
  56. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  57. foreach ($matches as $match) {
  58. // remove the comma
  59. $match[2] = ltrim($match[2],',');
  60. $replace = "[album id=\"{$match[1]}\" template=\"{$match[2]}\"]";
  61. $content = str_replace ($match[0], $replace, $content);
  62. }
  63. }
  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. foreach ($matches as $match) {
  69. $replace = "[nggallery id=\"{$match[1]}\"]";
  70. $content = str_replace ($match[0], $replace, $content);
  71. }
  72. }
  73. }
  74. if ( stristr( $content, '[imagebrowser' )) {
  75. $search = "@(?:<p>)*\s*\[imagebrowser\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
  76. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  77. foreach ($matches as $match) {
  78. $replace = "[imagebrowser id=\"{$match[1]}\"]";
  79. $content = str_replace ($match[0], $replace, $content);
  80. }
  81. }
  82. }
  83. if ( stristr( $content, '[slideshow' )) {
  84. $search = "@(?:<p>)*\s*\[slideshow\s*=\s*(\w+|^\+)(|,(\d+)|,)(|,(\d+))\]\s*(?:</p>)*@i";
  85. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  86. foreach ($matches as $match) {
  87. // remove the comma
  88. $match[3] = ltrim($match[3],',');
  89. $match[5] = ltrim($match[5],',');
  90. $replace = "[slideshow id=\"{$match[1]}\" w=\"{$match[3]}\" h=\"{$match[5]}\"]";
  91. $content = str_replace ($match[0], $replace, $content);
  92. }
  93. }
  94. }
  95. if ( stristr( $content, '[tags' )) {
  96. $search = "@(?:<p>)*\s*\[tags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i";
  97. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  98. foreach ($matches as $match) {
  99. $replace = "[nggtags gallery=\"{$match[1]}\"]";
  100. $content = str_replace ($match[0], $replace, $content);
  101. }
  102. }
  103. }
  104. if ( stristr( $content, '[albumtags' )) {
  105. $search = "@(?:<p>)*\s*\[albumtags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i";
  106. if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {
  107. foreach ($matches as $match) {
  108. $replace = "[nggtags album=\"{$match[1]}\"]";
  109. $content = str_replace ($match[0], $replace, $content);
  110. }
  111. }
  112. }
  113. // attach related images based on category or tags
  114. if ($ngg_options['activateTags'])
  115. $content .= nggShowRelatedImages();
  116. return $content;
  117. }
  118. /**
  119. * Function to show a single picture:
  120. *
  121. * [singlepic id="10" float="none|left|right" width="" height="" mode="none|watermark|web20" link="url" "template="filename" /]
  122. *
  123. * where
  124. * - id is one picture id
  125. * - float is the CSS float property to apply to the thumbnail
  126. * - width is width of the single picture you want to show (original width if this parameter is missing)
  127. * - height is height of the single picture you want to show (original height if this parameter is missing)
  128. * - mode is one of none, watermark or web20 (transformation applied to the picture)
  129. * - link is optional and could link to a other url instead the full image
  130. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  131. *
  132. * If the tag contains some text, this will be inserted as an additional caption to the picture too. Example:
  133. * [singlepic id="10"]This is an additional caption[/singlepic]
  134. * This tag will show a picture with under it two HTML span elements containing respectively the alttext of the picture
  135. * and the additional caption specified in the tag.
  136. *
  137. * @param array $atts
  138. * @param string $caption text
  139. * @return the content
  140. */
  141. function show_singlepic( $atts, $content = '' ) {
  142. extract(shortcode_atts(array(
  143. 'id' => 0,
  144. 'w' => '',
  145. 'h' => '',
  146. 'mode' => '',
  147. 'float' => '',
  148. 'link' => '',
  149. 'template' => ''
  150. ), $atts ));
  151. $out = nggSinglePicture($id, $w, $h, $mode, $float, $template, $content, $link);
  152. return $out;
  153. }
  154. /**
  155. * Function to show a collection of galleries:
  156. *
  157. * [album id="1,2,4,5,..." template="filename" gallery="filename" /]
  158. * where
  159. * - id of a album
  160. * - template is a name for a album template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  161. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  162. *
  163. * @param array $atts
  164. * @return the_content
  165. */
  166. function show_album( $atts ) {
  167. extract(shortcode_atts(array(
  168. 'id' => 0,
  169. 'template' => 'extend',
  170. 'gallery' => ''
  171. ), $atts ));
  172. $out = nggShowAlbum($id, $template, $gallery);
  173. return $out;
  174. }
  175. /**
  176. * Function to show a thumbnail or a set of thumbnails with shortcode of type:
  177. *
  178. * [gallery id="1,2,4,5,..." template="filename" images="number of images per page" /]
  179. * where
  180. * - id of a gallery
  181. * - images is the number of images per page (optional), 0 will show all images
  182. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  183. *
  184. * @param array $atts
  185. * @return the_content
  186. */
  187. function show_gallery( $atts ) {
  188. global $wpdb;
  189. extract(shortcode_atts(array(
  190. 'id' => 0,
  191. 'template' => '',
  192. 'images' => false
  193. ), $atts ));
  194. // backward compat for user which uses the name instead, still deprecated
  195. if( !is_numeric($id) )
  196. $id = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $id) );
  197. $out = nggShowGallery( $id, $template, $images );
  198. return $out;
  199. }
  200. function show_imagebrowser( $atts ) {
  201. global $wpdb;
  202. extract(shortcode_atts(array(
  203. 'id' => 0,
  204. 'template' => ''
  205. ), $atts ));
  206. $out = nggShowImageBrowser($id, $template);
  207. return $out;
  208. }
  209. function show_slideshow( $atts ) {
  210. global $wpdb;
  211. extract(shortcode_atts(array(
  212. 'id' => 0,
  213. 'w' => '',
  214. 'h' => ''
  215. ), $atts ));
  216. if( !is_numeric($id) )
  217. $id = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $id) );
  218. if( !empty( $id ) )
  219. $out = nggShowSlideshow($id, $w, $h);
  220. else
  221. $out = __('[Gallery not found]','nggallery');
  222. return $out;
  223. }
  224. function show_tags( $atts ) {
  225. extract(shortcode_atts(array(
  226. 'gallery' => '',
  227. 'album' => ''
  228. ), $atts ));
  229. if ( !empty($album) )
  230. $out = nggShowAlbumTags($album);
  231. else
  232. $out = nggShowGalleryTags($gallery);
  233. return $out;
  234. }
  235. /**
  236. * Function to show a thumbnail or a set of thumbnails with shortcode of type:
  237. *
  238. * [thumb id="1,2,4,5,..." template="filename" /]
  239. * where
  240. * - id is one or more picture ids
  241. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  242. *
  243. * @param array $atts
  244. * @return the_content
  245. */
  246. function show_thumbs( $atts ) {
  247. extract(shortcode_atts(array(
  248. 'id' => '',
  249. 'template' => ''
  250. ), $atts));
  251. // make an array out of the ids
  252. $pids = explode( ',', $id );
  253. // Some error checks
  254. if ( count($pids) == 0 )
  255. return __('[Pictures not found]','nggallery');
  256. $picturelist = nggdb::find_images_in_list( $pids );
  257. // show gallery
  258. if ( is_array($picturelist) )
  259. $out = nggCreateGallery($picturelist, false, $template);
  260. return $out;
  261. }
  262. /**
  263. * Function to show a gallery of random or the most recent images with shortcode of type:
  264. *
  265. * [random max="7" template="filename" id="2" /]
  266. * [recent max="7" template="filename" id="3" mode="date" /]
  267. * where
  268. * - max is the maximum number of random or recent images to show
  269. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  270. * - id is the gallery id, if the recent/random pictures shall be taken from a specific gallery only
  271. * - mode is either "id" (which takes the latest additions to the databse, default)
  272. * or "date" (which takes the latest pictures by EXIF date)
  273. * or "sort" (which takes the pictures by user sort order)
  274. *
  275. * @param array $atts
  276. * @return the_content
  277. */
  278. function show_random( $atts ) {
  279. extract(shortcode_atts(array(
  280. 'max' => '',
  281. 'template' => '',
  282. 'id' => 0
  283. ), $atts));
  284. $out = nggShowRandomRecent('random', $max, $template, $id);
  285. return $out;
  286. }
  287. function show_recent( $atts ) {
  288. extract(shortcode_atts(array(
  289. 'max' => '',
  290. 'template' => '',
  291. 'id' => 0,
  292. 'mode' => 'id'
  293. ), $atts));
  294. $out = nggShowRandomRecent($mode, $max, $template, $id);
  295. return $out;
  296. }
  297. /**
  298. * Shortcode for the Image tag cloud
  299. * Usage : [tagcloud template="filename" /]
  300. *
  301. * @param array $atts
  302. * @return the content
  303. */
  304. function show_tagcloud( $atts ) {
  305. extract(shortcode_atts(array(
  306. 'template' => ''
  307. ), $atts));
  308. $out = nggTagCloud( '', $template );
  309. return $out;
  310. }
  311. }
  312. // let's use it
  313. $nggShortcodes = new NextGEN_Shortcodes;
  314. ?>