PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/nextgen-gallery/lib/shortcodes.php

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