| | 1 | <?php |
| | 2 | |
| | 3 | /* |
| | 4 | * Archives shortcode |
| | 5 | * @author bubel & mtdewvirus |
| | 6 | */ |
| | 7 | |
| | 8 | function archives_shortcode($attr) { |
| | 9 | global $allowedposttags; |
| | 10 | extract(shortcode_atts(array( |
| | 11 | 'type' => 'postbypost', |
| | 12 | 'limit' => '', |
| | 13 | 'format' => 'html', |
| | 14 | 'show_post_count' => false, |
| | 15 | 'before' => '', |
| | 16 | 'after' => '', |
| | 17 | ), $attr)); |
| | 18 | |
| | 19 | if ( !in_array($type, array('yearly', 'monthly', 'daily', 'weekly', 'postbypost')) ) |
| | 20 | $type = 'postbypost'; |
| | 21 | |
| | 22 | if ( !in_array($format, array('html', 'option', 'custom')) ) |
| | 23 | $format = 'html'; |
| | 24 | |
| | 25 | if ( '' != $limit ) |
| | 26 | $limit = (int)$limit; |
| | 27 | |
| | 28 | $show_post_count = (bool)$show_post_count; |
| | 29 | $before = wp_kses($before, $allowedposttags); |
| | 30 | $after = wp_kses($after, $allowedposttags); |
| | 31 | |
| | 32 | // Get the archives |
| | 33 | $archives = wp_get_archives('type=' . $type . '&limit=' . $limit . '&format=' . $format . '&echo=0&show_post_count=' . $show_post_count . ''); |
| | 34 | |
| | 35 | // Check to see if there are any archives |
| | 36 | if ( empty($archives) ) |
| | 37 | $archives = '<p>' . __('Your blog does not currently have any published posts.') . '</p>'; |
| | 38 | elseif ( 'option' == $format ) |
| | 39 | $archives = "<select name='archive-dropdown' onchange='document.location.href=this.options[this.selectedIndex].value;'><option value='" . get_permalink() . "'>--</option>" . $archives . "</select>"; |
| | 40 | |
| | 41 | return $archives; |
| | 42 | } |
| | 43 | |
| | 44 | /* Flickr Short Code |
| | 45 | Author: kellan |
| | 46 | License: BSD/GPL/public domain (take your pick) |
| | 47 | |
| | 48 | [flickr video=http://flickr.com/photos/revdancatt/2345938910/] |
| | 49 | [flickr video=2345938910] |
| | 50 | [flickr video=2345938910 show_info=true w=400 h=300] |
| | 51 | [flickr video=2345938910 show_info=true w=400 h=300 secret=846d9c1be9] |
| | 52 | |
| | 53 | and eventually |
| | 54 | |
| | 55 | [flickr photo=107274692] |
| | 56 | [flickr photo=107274692 size=m] |
| | 57 | */ |
| | 58 | |
| | 59 | function flickr_shortcode_handler($atts) { |
| | 60 | $atts = shortcode_atts(array( |
| | 61 | 'video' => 0, |
| | 62 | 'photo' => 0, |
| | 63 | 'show_info' => 0, |
| | 64 | 'w' => 400, |
| | 65 | 'h' => 300, |
| | 66 | 'secret' => 0, |
| | 67 | 'size' => 0 |
| | 68 | ), $atts); |
| | 69 | |
| | 70 | if ($atts['video']) { |
| | 71 | $showing = 'video'; |
| | 72 | $src = $atts['video']; |
| | 73 | } |
| | 74 | elseif ($atts['photo']) { |
| | 75 | $showing = 'photo'; |
| | 76 | $src = $atts['photo']; |
| | 77 | } |
| | 78 | else { |
| | 79 | return ''; |
| | 80 | } |
| | 81 | |
| | 82 | if (preg_match("!photos/(([0-9a-zA-Z-_]+)|([0-9]+@N[0-9]+))/([0-9]+)/?$!", $src, $m)) { |
| | 83 | $atts['photo_id'] = $m[4]; |
| | 84 | } |
| | 85 | else { |
| | 86 | $atts['photo_id'] = $atts['video']; |
| | 87 | } |
| | 88 | |
| | 89 | if ($showing == 'video') { |
| | 90 | if (!$atts['show_info'] or in_array($atts['show_info'], array('yes', 'true'))) { |
| | 91 | $atts['show_info'] = 'true'; |
| | 92 | } |
| | 93 | elseif (in_array($atts['show_info'], array('false', 'no'))) { |
| | 94 | $atts['show_info'] = 'false'; |
| | 95 | } |
| | 96 | |
| | 97 | return flickr_shortcode_video_markup($atts); |
| | 98 | } |
| | 99 | else { |
| | 100 | return ''; |
| | 101 | } |
| | 102 | } |
| | 103 | |
| | 104 | function flickr_shortcode_video_markup($atts) { |
| | 105 | $photo_vars = "photo_id=$atts[photo_id]"; |
| | 106 | if ($atts['secret']) { |
| | 107 | $photo_vars .= "&photo_secret=$atts[secret]"; |
| | 108 | } |
| | 109 | |
| | 110 | $markup = <<<EOD |
| | 111 | <object type="application/x-shockwave-flash" width="$atts[w]" height="$atts[h]" data="http://www.flickr.com/apps/video/stewart.swf?v=1.161" |
| | 112 | classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"> <param name="flashvars" value="$photo_vars&flickr_show_info_box=$atts[show_info]"></param> |
| | 113 | <param name="movie" value="http://www.flickr.com/apps/video/stewart.swf?v=1.161"></param><param name="bgcolor" value="#000000"></param><param name="allowFullScreen" value="true"></param><embed type="application/x-shockwave-flash" src="http://www.flickr.com/apps/video/stewart.swf?v=1.161" bgcolor="#000000" allowfullscreen="true" |
| | 114 | flashvars="$photo_vars&flickr_show_info_box=$atts[show_info]" height="$atts[h]" width="$atts[w]"></embed></object> |
| | 115 | EOD; |
| | 116 | return $markup; |
| | 117 | } |
| | 118 | |
| | 119 | /* |
| | 120 | polldaddy.com |
| | 121 | [polldaddy poll="139742"] |
| | 122 | */ |
| | 123 | |
| | 124 | function polldaddy_shortcode_handler_set_data() { |
| | 125 | $resource = wp_remote_get( 'http://polldaddy.com/xml/keywords.xml' ); |
| | 126 | $body = wp_remote_retrieve_body( $resource ); |
| | 127 | $keywords_xml = simplexml_load_string ( $body ); |
| | 128 | $keywords = array(); |
| | 129 | $keywords['generated'] = time(); |
| | 130 | |
| | 131 | foreach ( $keywords_xml->keyword as $keyword_xml ){ |
| | 132 | $keywords[] = array( 'keyword' => (string) $keyword_xml, 'url' => (string) $keyword_xml['url'] ); |
| | 133 | } |
| | 134 | wp_cache_set( 'pd-keywords', $keywords, 'site-options', 864000 ); |
| | 135 | |
| | 136 | return $keywords; |
| | 137 | } |
| | 138 | |
| | 139 | function polldaddy_add_rating_js() { |
| | 140 | wp_print_scripts( 'polldaddy-rating-js' ); |
| | 141 | } |
| | 142 | |
| | 143 | function polldaddy_shortcode_handler( $atts, $content = null ) { |
| | 144 | global $post; |
| | 145 | |
| | 146 | extract( shortcode_atts( array( 'poll' => 'empty', 'rating' => 'empty', 'unique_id' => null, 'title' => null, 'permalink' => null, ), $atts ) ); |
| | 147 | $poll = (int) $poll; |
| | 148 | $rating = (int) $rating; |
| | 149 | |
| | 150 | if ( $rating > 0 ) { |
| | 151 | if ( $unique_id != null ) { |
| | 152 | $unique_id = wp_specialchars( $unique_id ); |
| | 153 | } else { |
| | 154 | if ( is_page() ) { |
| | 155 | $unique_id = 'wp-page-' . $post->ID; |
| | 156 | } |
| | 157 | else { |
| | 158 | $unique_id = 'wp-post-' . $post->ID; |
| | 159 | } |
| | 160 | } |
| | 161 | |
| | 162 | if ( $title != null ) { |
| | 163 | $title = wp_specialchars( $title ); |
| | 164 | } else { |
| | 165 | $title = urlencode( $post->post_title ); |
| | 166 | } |
| | 167 | |
| | 168 | if ( $permalink != null ) { |
| | 169 | $permalink = clean_url( $permalink ); |
| | 170 | } else { |
| | 171 | $permalink = urlencode( get_permalink( $post->ID ) ); |
| | 172 | } |
| | 173 | |
| | 174 | wp_register_script( 'polldaddy-rating-js', 'http://i.polldaddy.com/ratings/rating.js' ); |
| | 175 | add_filter( 'wp_footer', 'polldaddy_add_rating_js' ); |
| | 176 | |
| | 177 | return '<div id="pd_rating_holder_' . $rating . '"></div> |
| | 178 | <script language="javascript"> |
| | 179 | PDRTJS_settings_' . $rating . ' = { |
| | 180 | "id" : "' . $rating . '", |
| | 181 | "unique_id" : "' . $unique_id . '", |
| | 182 | "title" : "' . $title . '", |
| | 183 | "permalink" : "' . $permalink . '" |
| | 184 | }; |
| | 185 | </script>'; |
| | 186 | } else if ( $poll > 0 ) { |
| | 187 | $keywords = wp_cache_get( 'pd-keywords', 'site-options' ); |
| | 188 | if ( !$keywords || $keywords['generated'] <= ( time() - 300 ) ) { |
| | 189 | |
| | 190 | if ( !wp_cache_get( 'pd-keywords-fetching', 'site-options' ) ) { |
| | 191 | wp_cache_set( 'pd-keywords-fetching', 1, 'site-options', 30 ); |
| | 192 | $keywords = polldaddy_shortcode_handler_set_data(); |
| | 193 | } |
| | 194 | } |
| | 195 | |
| | 196 | if ( !$keywords ) { |
| | 197 | $keywords = array(); |
| | 198 | } |
| | 199 | |
| | 200 | $mod = ( $poll % ( count( $keywords ) - 1 ) ); |
| | 201 | |
| | 202 | return '<script type="text/javascript" language="javascript" charset="utf-8" src="http://static.polldaddy.com/p/' . $poll . '.js"></script> |
| | 203 | <noscript> |
| | 204 | <a href="http://answers.polldaddy.com/poll/' . $poll . '/">View This Poll</a><br/><span style="font-size:10px;"><a href="' . $keywords[ $mod ][ 'url' ] . '">' . $keywords[ $mod ][ 'keyword' ] . '</a></span> |
| | 205 | </noscript>'; |
| | 206 | } |
| | 207 | } |
| | 208 | |
| | 209 | /* |
| | 210 | * url can be: |
| | 211 | * http://www.youtube.com/watch?v=H2Ncxw1xfck |
| | 212 | * or http://www.youtube.com/watch?v=H2Ncxw1xfck&w=320&h=240&fmt=1&rel=0&showsearch=1&hd=0 |
| | 213 | */ |
| | 214 | |
| | 215 | function youtube_shortcode_handler( $atts, $content = null ) { |
| | 216 | if ( !empty($atts) && !isset($atts['url']) && isset($atts[0]) ) |
| | 217 | $atts['url'] = $atts[0]; |
| | 218 | |
| | 219 | $atts = shortcode_atts( array('url' => ''), $atts ); |
| | 220 | |
| | 221 | if ( ! $atts['url'] ) |
| | 222 | return ''; |
| | 223 | |
| | 224 | return youtube_id($atts['url']); |
| | 225 | } |
| | 226 | |
| | 227 | function youtube_id( $url ) { |
| | 228 | $url = trim( $url, ' "' ); |
| | 229 | $url = trim( $url ); |
| | 230 | $url = str_replace( '/v/', '/?v=', $url ); // new format - http://www.youtube.com/v/jF-kELmmvgA |
| | 231 | $url = str_replace( '&', '&', $url ); |
| | 232 | $url = parse_url($url); |
| | 233 | |
| | 234 | if ( !isset($url['query']) ) |
| | 235 | return '<!--YouTube Error: bad URL entered-->'; |
| | 236 | |
| | 237 | parse_str($url['query'], $qargs); |
| | 238 | |
| | 239 | if ( !isset($qargs['v']) ) |
| | 240 | return '<!--YouTube Error: bad URL entered-->'; |
| | 241 | |
| | 242 | $id = preg_replace('|[^_a-z0-9-]|i', '', $qargs['v']); |
| | 243 | |
| | 244 | if ( is_feed() ) |
| | 245 | return '<span style="text-align:center; display: block;"><a href="' . get_permalink() . '"><img src="http://img.youtube.com/vi/' . $id . '/2.jpg" alt="" /></a></span>'; |
| | 246 | |
| | 247 | // calculate the width and height, taken content_width into consideration |
| | 248 | global $content_width; |
| | 249 | |
| | 250 | $input_w = (isset($qargs['w']) && intval($qargs['w'])) ? intval($qargs['w']) : 0; |
| | 251 | $input_h = (isset($qargs['h']) && intval($qargs['h'])) ? intval($qargs['h']) : 0; |
| | 252 | |
| | 253 | if ( $input_w > 0 && $input_h > 0 ){ |
| | 254 | $w = $input_w; |
| | 255 | $h = $input_h; |
| | 256 | } else if ( $input_w == 0 && $input_h == 0 ){ |
| | 257 | |
| | 258 | if ( isset($qargs['fmt']) && intval($qargs['fmt']) ) |
| | 259 | $w = ( !empty($content_width) ? min( $content_width, 480) : 480 ); |
| | 260 | else |
| | 261 | $w = ( !empty($content_width) ? min( $content_width, 425) : 425 ); |
| | 262 | |
| | 263 | $h = ceil($w * 14 / 17); |
| | 264 | |
| | 265 | } else if ( $input_w > 0 ) { |
| | 266 | |
| | 267 | $w = $input_w; |
| | 268 | $h = ceil($w * 14 / 17); |
| | 269 | |
| | 270 | } else { //input_h > 0 |
| | 271 | |
| | 272 | if ( isset($qargs['fmt']) && intval($qargs['fmt']) ) |
| | 273 | $w = ( !empty($content_width) ? min( $content_width, 480) : 480 ); |
| | 274 | else |
| | 275 | $w = ( !empty($content_width) ? min( $content_width, 425) : 425 ); |
| | 276 | |
| | 277 | $h = $input_h; |
| | 278 | } |
| | 279 | |
| | 280 | $fmt = ''; |
| | 281 | if ( isset($qargs['fmt']) && intval($qargs['fmt']) ) |
| | 282 | $fmt = "&fmt={$qargs['fmt']}"; |
| | 283 | |
| | 284 | if ( isset($qargs['rel']) && 0 == $qargs['rel']) { |
| | 285 | $rel = 0; |
| | 286 | } else { |
| | 287 | $rel = 1; |
| | 288 | } |
| | 289 | |
| | 290 | if ( isset($qargs['showsearch']) && 1 == $qargs['showsearch']) { |
| | 291 | $search = 1; |
| | 292 | } else { |
| | 293 | $search = 0; |
| | 294 | } |
| | 295 | |
| | 296 | if ( isset($qargs['hd']) && 1 == $qargs['hd']) { |
| | 297 | $hd = 1; |
| | 298 | } else { |
| | 299 | $hd = 0; |
| | 300 | } |
| | 301 | |
| | 302 | $video = "<span style='text-align:center; display: block;'><object width='$w' height='$h'><param name='movie' value='http://www.youtube.com/v/$id&rel=$rel&fs=1$fmt&showsearch=$search&hd=$hd' /> <param name='allowfullscreen' value='true' /> <param name='wmode' value='transparent' /> <embed src='http://www.youtube.com/v/$id&rel=$rel&fs=1$fmt&showsearch=$search&hd=$hd' type='application/x-shockwave-flash' allowfullscreen='true' width='$w' height='$h' wmode='transparent'></embed> </object></span>"; |
| | 303 | |
| | 304 | return $video; |
| | 305 | } |
| | 306 | |
| | 307 | function default_shortcode_init() { |
| | 308 | add_shortcode('archives', 'archives_shortcode'); |
| | 309 | add_shortcode('flickr', 'flickr_shortcode_handler'); |
| | 310 | add_shortcode('polldaddy', 'polldaddy_shortcode_handler'); |
| | 311 | add_shortcode('youtube', 'youtube_shortcode_handler'); |
| | 312 | } |
| | 313 | No newline at end of file |