seekingmichigan.org-Wordpress /plugins/nextgen-gallery/lib/shortcodes.php

Language PHP Lines 288
MD5 Hash b32af82ff275ba950cd501fad6a727e2
Repository https://github.com/archives-of-michigan/seekingmichigan.org-Wordpress.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
/**
 * @author Alex Rabe, Vincent Prat 
 * @copyright 2008
 * @since 1.0.0
 * @description Use WordPress Shortcode API for more features
 * @Docs http://codex.wordpress.org/Shortcode_API
 */

class NextGEN_shortcodes {
	
	// register the new shortcodes
	function NextGEN_shortcodes() {
	
		// convert the old shortcode
		add_filter('the_content', array(&$this, 'convert_shortcode'));
		
		// do_shortcode on the_excerpt could causes several unwanted output. Uncomment it on your own risk
		// add_filter('the_excerpt', array(&$this, 'convert_shortcode'));
		// add_filter('the_excerpt', 'do_shortcode', 11);
		
		add_shortcode( 'singlepic', array(&$this, 'show_singlepic' ) );
		add_shortcode( 'album', array(&$this, 'show_album' ) );
		add_shortcode( 'nggallery', array(&$this, 'show_gallery') );
		add_shortcode( 'imagebrowser', array(&$this, 'show_imagebrowser' ) );
		add_shortcode( 'slideshow', array(&$this, 'show_slideshow' ) );
		add_shortcode( 'nggtags', array(&$this, 'show_tags' ) );
		add_shortcode( 'thumb', array(&$this, 'show_thumbs' ));
	}

	 /**
	   * NextGEN_shortcodes::convert_shortcode()
	   * convert old shortcodes to the new WordPress core style
	   * [gallery=1]  ->> [nggallery id=1]
	   * 
	   * @param string $content Content to search for shortcodes
	   * @return string Content with new shortcodes.
	   */
	function convert_shortcode($content) {
		
		$ngg_options = nggGallery::get_option('ngg_options');
	
		if ( stristr( $content, '[singlepic' )) {
			$search = "@\[singlepic=(\d+)(|,\d+|,)(|,\d+|,)(|,watermark|,web20|,)(|,right|,center|,left|,)\]@i";
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					// remove the comma
					$match[2] = ltrim($match[2], ',');
					$match[3] = ltrim($match[3], ',');	
					$match[4] = ltrim($match[4], ',');	
					$match[5] = ltrim($match[5], ',');						
					$replace = "[singlepic id=\"{$match[1]}\" w=\"{$match[2]}\" h=\"{$match[3]}\" mode=\"{$match[4]}\" float=\"{$match[5]}\" ]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}

		if ( stristr( $content, '[album' )) {
			$search = "@(?:<p>)*\s*\[album\s*=\s*(\w+|^\+)(|,extend|,compact)\]\s*(?:</p>)*@i"; 
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					// remove the comma
					$match[2] = ltrim($match[2],',');
					$replace = "[album id=\"{$match[1]}\" template=\"{$match[2]}\"]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}		

		if ( stristr( $content, '[gallery' )) {
			$search = "@(?:<p>)*\s*\[gallery\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					$replace = "[nggallery id=\"{$match[1]}\"]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}	

		if ( stristr( $content, '[imagebrowser' )) {
			$search = "@(?:<p>)*\s*\[imagebrowser\s*=\s*(\w+|^\+)\]\s*(?:</p>)*@i";
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					$replace = "[imagebrowser id=\"{$match[1]}\"]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}

		if ( stristr( $content, '[slideshow' )) {
			$search = "@(?:<p>)*\s*\[slideshow\s*=\s*(\w+|^\+)(|,(\d+)|,)(|,(\d+))\]\s*(?:</p>)*@i";
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					// remove the comma
					$match[3] = ltrim($match[3],',');
					$match[5] = ltrim($match[5],',');	
					$replace = "[slideshow id=\"{$match[1]}\" w=\"{$match[3]}\" h=\"{$match[5]}\"]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}

		if ( stristr( $content, '[tags' )) {
			$search = "@(?:<p>)*\s*\[tags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i";
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					$replace = "[nggtags gallery=\"{$match[1]}\"]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}
		
		if ( stristr( $content, '[albumtags' )) {
			$search = "@(?:<p>)*\s*\[albumtags\s*=\s*(.*?)\s*\]\s*(?:</p>)*@i";
			if (preg_match_all($search, $content, $matches, PREG_SET_ORDER)) {

				foreach ($matches as $match) {
					$replace = "[nggtags album=\"{$match[1]}\"]";
					$content = str_replace ($match[0], $replace, $content);
				}
			}
		}

		// attach related images based on category or tags
		if ($ngg_options['activateTags']) 
			$content .= nggShowRelatedImages();
	
		return $content;
	}
	
	/**
	 * Function to show a single picture:
	 * 
	 *     [singlepic id="10" float="none|left|right" width="" height="" mode="none|watermark|web20" template="filename" /]
	 *
	 * where
	 *  - id is one picture id
	 *  - float is the CSS float property to apply to the thumbnail
	 *  - width is width of the single picture you want to show (original width if this parameter is missing)
	 *  - height is height of the single picture you want to show (original height if this parameter is missing)
	 *  - mode is one of none, watermark or web20 (transformation applied to the picture)
	 *  - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
	 * 
	 * If the tag contains some text, this will be inserted as an additional caption to the picture too. Example:
	 * 		[singlepic id="10"]This is an additional caption[/singlepic]
	 * This tag will show a picture with under it two HTML span elements containing respectively the alttext of the picture 
	 * and the additional caption specified in the tag. 
	 * 
	 * @param array $atts
	 * @param string $caption text
	 * @return the content
	 */
	function show_singlepic( $atts, $content = '' ) {
	
		extract(shortcode_atts(array(
			'id' 		=> 0,
			'w'		 	=> '',
			'h'		 	=> '',
			'mode'	 	=> '',
			'float'	 	=> '',
			'template' 	=> ''
		), $atts ));
	
		$out = nggSinglePicture($id, $w, $h, $mode, $float, $template, $content);
			
		return $out;
	}

	function show_album( $atts ) {
	
		extract(shortcode_atts(array(
			'id' 		=> 0,
			'template'	=> 'extend'	
		), $atts ));
		
		$out = nggShowAlbum($id, $template);
			
		return $out;
	}

	function show_gallery( $atts ) {
	
		extract(shortcode_atts(array(
			'id' 		=> 0,
			'template'	=> ''	
		), $atts ));
		
		$out = nggShowGallery( $id, $template);
			
		return $out;
	}

	function show_imagebrowser( $atts ) {
		
		global $wpdb;
	
		extract(shortcode_atts(array(
			'id' 		=> 0,
			'template'	=> ''	
		), $atts ));

		$out = nggShowImageBrowser($id, $template);
			
		return $out;
	}
	
	function show_slideshow( $atts ) {
		
		global $wpdb;
	
		extract(shortcode_atts(array(
			'id' 		=> 0,
			'w'		 	=> '',
			'h'		 	=> ''
		), $atts ));
		
		$galleryID = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE gid = '$id' ");
		if(!$galleryID) $galleryID = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE name = '$id' ");

		if( $galleryID )
			$out = nggShowSlideshow($galleryID, $w, $h);
		else 
			$out = __('[Gallery not found]','nggallery');
			
		return $out;
	}
	
	function show_tags( $atts ) {
	
		extract(shortcode_atts(array(
			'gallery' 		=> '',
			'album' 		=> ''
		), $atts ));
		
		if ( !empty($album) )
			$out = nggShowAlbumTags($album);
		else
			$out = nggShowGalleryTags($gallery);
		
		return $out;
	}

	/**
	 * Function to show a thumbnail or a set of thumbnails with shortcode of type:
	 * 
	 * [thumb id="1,2,4,5,..." template="filename" /]
	 * where 
	 * - id is one or more picture ids
	 * - template is a name for a gallery template, which is located in themefolder/nggallery or plugins/nextgen-gallery/view
	 * 
	 * @param array $atts
	 * @return the_content
	 */
	function show_thumbs( $atts ) {
	
		extract(shortcode_atts(array(
			'id' 		=> '',
			'template' 	=> ''
		), $atts));
		
		// make an array out of the ids
		$pids = explode( ',', $id );
		
		// Some error checks
		if ( count($pids) == 0 )
			return __('[Pictures not found]','nggallery');
		
		$picturelist = nggdb::find_images_in_list( $pids );
		
		// show gallery
		if ( is_array($picturelist) )
			$out = nggCreateGallery($picturelist, false, $template);
		
		return $out;
	}

}

// let's use it
$nggShortcodes = new NextGEN_Shortcodes;	

?>
Back to Top