PageRenderTime 52ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/blueprintmrk/bladencountyrecords
PHP | 375 lines | 202 code | 74 blank | 99 comment | 23 complexity | cce40dd511efdc527a8c9a1ca852e9de MD5 | raw file
  1. <?php
  2. /**
  3. * @author Alex Rabe, Vincent Prat
  4. * @copyright 2008 - 2009
  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. function show_album( $atts ) {
  155. extract(shortcode_atts(array(
  156. 'id' => 0,
  157. 'template' => 'extend'
  158. ), $atts ));
  159. $out = nggShowAlbum($id, $template);
  160. return $out;
  161. }
  162. /**
  163. * Function to show a thumbnail or a set of thumbnails with shortcode of type:
  164. *
  165. * [gallery id="1,2,4,5,..." template="filename" images="number of images per page" /]
  166. * where
  167. * - id of a gallery
  168. * - images is the number of images per page (optional), 0 will show all images
  169. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  170. *
  171. * @param array $atts
  172. * @return the_content
  173. */
  174. function show_gallery( $atts ) {
  175. global $wpdb;
  176. extract(shortcode_atts(array(
  177. 'id' => 0,
  178. 'template' => '',
  179. 'images' => false
  180. ), $atts ));
  181. // backward compat for user which uses the name instead, still deprecated
  182. if( !is_numeric($id) )
  183. $id = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $id) );
  184. $out = nggShowGallery( $id, $template, $images );
  185. return $out;
  186. }
  187. function show_imagebrowser( $atts ) {
  188. global $wpdb;
  189. extract(shortcode_atts(array(
  190. 'id' => 0,
  191. 'template' => ''
  192. ), $atts ));
  193. $out = nggShowImageBrowser($id, $template);
  194. return $out;
  195. }
  196. function show_slideshow( $atts ) {
  197. global $wpdb;
  198. extract(shortcode_atts(array(
  199. 'id' => 0,
  200. 'w' => '',
  201. 'h' => ''
  202. ), $atts ));
  203. if( !is_numeric($id) )
  204. $id = $wpdb->get_var( $wpdb->prepare ("SELECT gid FROM $wpdb->nggallery WHERE name = '%s' ", $id) );
  205. if( !empty( $id ) )
  206. $out = nggShowSlideshow($id, $w, $h);
  207. else
  208. $out = __('[Gallery not found]','nggallery');
  209. return $out;
  210. }
  211. function show_tags( $atts ) {
  212. extract(shortcode_atts(array(
  213. 'gallery' => '',
  214. 'album' => ''
  215. ), $atts ));
  216. if ( !empty($album) )
  217. $out = nggShowAlbumTags($album);
  218. else
  219. $out = nggShowGalleryTags($gallery);
  220. return $out;
  221. }
  222. /**
  223. * Function to show a thumbnail or a set of thumbnails with shortcode of type:
  224. *
  225. * [thumb id="1,2,4,5,..." template="filename" /]
  226. * where
  227. * - id is one or more picture ids
  228. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  229. *
  230. * @param array $atts
  231. * @return the_content
  232. */
  233. function show_thumbs( $atts ) {
  234. extract(shortcode_atts(array(
  235. 'id' => '',
  236. 'template' => ''
  237. ), $atts));
  238. // make an array out of the ids
  239. $pids = explode( ',', $id );
  240. // Some error checks
  241. if ( count($pids) == 0 )
  242. return __('[Pictures not found]','nggallery');
  243. $picturelist = nggdb::find_images_in_list( $pids );
  244. // show gallery
  245. if ( is_array($picturelist) )
  246. $out = nggCreateGallery($picturelist, false, $template);
  247. return $out;
  248. }
  249. /**
  250. * Function to show a gallery of random or the most recent images with shortcode of type:
  251. *
  252. * [random max="7" template="filename" id="2" /]
  253. * [recent max="7" template="filename" id="3" mode="date" /]
  254. * where
  255. * - max is the maximum number of random or recent images to show
  256. * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
  257. * - id is the gallery id, if the recent/random pictures shall be taken from a specific gallery only
  258. * - mode is either "id" (which takes the latest additions to the databse, default)
  259. * or "date" (which takes the latest pictures by EXIF date)
  260. * or "sort" (which takes the pictures by user sort order)
  261. *
  262. * @param array $atts
  263. * @return the_content
  264. */
  265. function show_random( $atts ) {
  266. extract(shortcode_atts(array(
  267. 'max' => '',
  268. 'template' => '',
  269. 'id' => 0
  270. ), $atts));
  271. $out = nggShowRandomRecent('random', $max, $template, $id);
  272. return $out;
  273. }
  274. function show_recent( $atts ) {
  275. extract(shortcode_atts(array(
  276. 'max' => '',
  277. 'template' => '',
  278. 'id' => 0,
  279. 'mode' => 'id'
  280. ), $atts));
  281. $out = nggShowRandomRecent($mode, $max, $template, $id);
  282. return $out;
  283. }
  284. /**
  285. * Shortcode for the Image tag cloud
  286. * Usage : [tagcloud template="filename" /]
  287. *
  288. * @param array $atts
  289. * @return the content
  290. */
  291. function show_tagcloud( $atts ) {
  292. extract(shortcode_atts(array(
  293. 'template' => ''
  294. ), $atts));
  295. $out = nggTagCloud( '', $template );
  296. return $out;
  297. }
  298. }
  299. // let's use it
  300. $nggShortcodes = new NextGEN_Shortcodes;
  301. ?>