Advertisement
simonwheatley

WP RSS Images with filters

May 2nd, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.95 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: WP RSS Images
  4. Plugin URI: http://web-argument.com/wp-rss-images-wordpress-plugin/
  5. Description: Add feature images to your blog rss.
  6. Version: 1.1-Filters
  7. Author: Alain Gonzalez
  8. Author URI: http://web-argument.com/
  9. */
  10.  
  11. function wp_rss_img_get_options($default = false){
  12.     $rss_img_default_op = array(
  13.                                         "size"=>"thumbnail",
  14.                                         "rss"=>0,
  15.                                         "rss2"=>1,
  16.                                         "media"=>0,
  17.                                         "enclosure"=>1,
  18.                                         "version"=>"1.1"
  19.     );
  20.    
  21.     if ($default) {
  22.     update_option('wp_rss_img_op', $rss_img_default_op);
  23.     return $rss_img_default_op;
  24.     }
  25.    
  26.     $options = get_option('wp_rss_img_op');
  27.     if (isset($options)){
  28.         if (isset($options['version'])) {  
  29.             $chk_version = version_compare("1.1",$options['version']);
  30.             if ($chk_version == 0)  return $options;
  31.             else if ($chk_version > 0) $options = $rss_img_default_op;
  32.         } else {
  33.         $options = $rss_img_default_op;
  34.         }
  35.     }  
  36.     update_option('wp_rss_img_op', $options);
  37.     return $options;
  38. }
  39.  
  40. add_action('admin_menu', 'wp_rss_img_menu');
  41. add_action("do_feed_rss","wp_rss_img_do_feed",5,1);
  42. add_action("do_feed_rss2","wp_rss_img_do_feed",5,1);
  43.  
  44. function wp_rss_img_do_feed($for_comments){
  45.     if(!$for_comments) {
  46.         $options = wp_rss_img_get_options();
  47.         if($options['media']) add_action('rss2_ns', 'wp_rss_img_adding_yahoo_media_tag');
  48.         if ($options['rss'])  add_action('rss_item', 'wp_rss_img_include');
  49.         if ($options['rss2'])  add_action('rss2_item', 'wp_rss_img_include');
  50.     }
  51. }
  52.  
  53. function wp_rss_img_include (){
  54.     $options = wp_rss_img_get_options();
  55.     $media = $options['media'];
  56.     $enclosure = $options['enclosure'];
  57.     $image_size = $options['size'];
  58.        
  59.     $image_url = wp_rss_img_url($image_size);
  60.    
  61.     if (!empty($image_url) && ($enclosure || $media)) {
  62.        
  63.         $uploads = wp_upload_dir();
  64.         $url = parse_url($image_url);
  65.         $path = $uploads['basedir'] . preg_replace( '/.*uploads(.*)/', '${1}', $url['path'] );
  66.        
  67.         if ( file_exists( $path ) )
  68.         {
  69.           $filesize = filesize( $path );
  70.           $url = $path;
  71.          
  72.         } else {       
  73.             $ary_header = get_headers($image_url, 1);                      
  74.             $filesize = $ary_header['Content-Length']; 
  75.             $url = $image_url;             
  76.         }
  77.        
  78.         if($enclosure) echo '<enclosure url="' . $image_url . '" length="' . $filesize . '" type="image/jpg" />';              
  79.         if($media){
  80.                 list($width, $height, $type, $attr) = getimagesize($url);
  81.                 echo '<media:content url="'.$image_url.'" width="'.$width.'" height="'.$height.'" medium="image" type="'.image_type_to_mime_type($type).'" />';
  82.         }
  83.    
  84.     }
  85.    
  86. }
  87.  
  88. function wp_rss_img_adding_yahoo_media_tag(){
  89.  
  90.     echo 'xmlns:media="http://search.yahoo.com/mrss/"';
  91.  
  92. }
  93.  
  94. /**
  95.  * Returns a URL to an image in the user uploads directory on this server.
  96.  *
  97.  * Developers can hook the wp_rss_image_alternative filter to provide their
  98.  * own image URL on this server, or hook the wp_rss_image_size filter to
  99.  * provide a specific size for the featured_image.
  100.  *
  101.  * @param string $size Defaults to medium, a WordPress image size added with add_image_size
  102.  * @return string The URL to an image on this server
  103.  */
  104. function wp_rss_img_url($size = 'medium') {
  105.     global $post;
  106.     if ( $img_url = apply_filters( 'wp_rss_image_alternative', false, $post->ID ) ) {
  107.         return $img_url;
  108.     } else if( function_exists ('has_post_thumbnail') && has_post_thumbnail($post->ID)) {
  109.         $thumbnail_id = get_post_thumbnail_id( $post->ID );
  110.         $size = apply_filters( 'wp_rss_image_size', 'medium', $post->ID );
  111.         if(!empty($thumbnail_id))
  112.             $img = wp_get_attachment_image_src( $thumbnail_id, $size );
  113.         error_log( "SW: IMG: " . print_r( $img, true ) );
  114.     } else {
  115.         $attachments = get_children( array(
  116.                                             'post_parent' => $post->ID,
  117.                                             'post_type' => 'attachment',
  118.                                             'post_mime_type' => 'image',
  119.                                             'orderby' => 'menu_order',
  120.                                             'order' => 'ASC',
  121.                                             'numberposts' => 1) );
  122.         if($attachments == true) {
  123.             foreach($attachments as $id => $attachment) :
  124.                 $img = wp_get_attachment_image_src($id, $size);        
  125.             endforeach;                
  126.         }
  127.    
  128.     }
  129.     if (isset($img)) return $img[0];
  130. }
  131.  
  132.  
  133.  
  134. function wp_rss_img_menu() {
  135.     add_options_page('WP RSS Images', 'WP RSS Images', 'administrator', 'wp-rss-image', 'wp_rss_image_setting');     
  136. }
  137.  
  138.  
  139.  
  140. function wp_rss_image_setting() {
  141.    
  142.     $options = wp_rss_img_get_options();
  143.          
  144.     if(isset($_POST['Submit'])){
  145.         $newoptions = array();         
  146.         $newoptions['media'] = isset ($_POST['media'])? $_POST['media']: 0;
  147.         $newoptions['enclosure'] = isset ($_POST['enclosure'])? $_POST['enclosure']: 0;
  148.         $newoptions['rss'] = isset ($_POST['rss'])? $_POST['rss']: 0;
  149.         $newoptions['rss2'] = isset ($_POST['rss2'])? $_POST['rss2']: 0;
  150.         $newoptions['size'] = isset ($_POST['size'])? $_POST['size']: $options['size'];
  151.         $newoptions['version'] = $options['version'];
  152.    
  153.         if ( $options != $newoptions ) {
  154.             $options = $newoptions;
  155.             update_option('wp_rss_img_op', $options);      
  156. ?>
  157.          <div class="updated"><p><strong><?php _e('Options saved.', 'mt_trans_domain' ); ?></strong></p></div>        
  158. <?php  }  ?>
  159. <?php  }  
  160.  
  161.     $media = $options['media'];
  162.     $enclosure = $options['enclosure'];
  163.     $rss = $options['rss'];
  164.     $rss2 = $options['rss2'];  
  165.     $image_size = $options['size'];
  166.     $version = $options['version'];
  167.    
  168. ?>
  169.  
  170. <div class="wrap">  
  171.  
  172. <form method="post" name="options" target="_self">
  173.  
  174. <h2><?php _e("WP RSS Images Setting") ?></h2>
  175. <h3><?php _e("Select the size of the images") ?></h3>
  176. <p><?php _e("The plugin uses the Posts feature images or the first image added to the post gallery. You can change the dimension of this sizes under Settings/ Media.") ?></p>
  177. <table width="100%" cellpadding="10" class="form-table">
  178.  
  179.   <tr valign="top">
  180.     <td width="200" align="right">
  181.       <input type="radio" name="size" id="radio" value="thumbnail" <?php if ($image_size == 'thumbnail') echo "checked=\"checked\"";?>/>
  182.     </td>
  183.     <td align="left" scope="row"><?php _e("Thumbnail") ?></td>
  184.   </tr>
  185.   <tr valign="top">
  186.     <td width="200" align="right">
  187.      <input name="size" type="radio" id="radio" value="medium" <?php if ($image_size == 'medium') echo "checked=\"checked\"";?> />
  188.      </td>
  189.     <td align="left" scope="row"><?php _e("Medium Size") ?></td>
  190.   </tr>
  191.   <tr valign="top">
  192.     <td width="200" align="right">
  193.      <input type="radio" name="size" id="radio" value="full" <?php if ($image_size == 'full') echo "checked=\"checked\"";?>/>
  194.      </td>
  195.     <td align="left" scope="row"><?php _e("Full Size") ?></td>
  196.   </tr>
  197. </table>
  198.  
  199. <h3> <?php _e("Apply to: ") ?></h3>
  200. <table width="100%" cellpadding="10" class="form-table">  
  201.   <tr valign="top">
  202.     <td width="200" align="right"><input name="rss" type="checkbox" id="rss" value="1"
  203.     <?php if ($rss) echo "checked" ?> /></td>
  204.     <td align="left" scope="row"><?php _e("RSS") ?>   <a href="<?php echo get_bloginfo('rss_url'); ?> " title="<?php bloginfo('name'); ?> - rss" target="_blank"><?php echo get_bloginfo('rss_url'); ?> </a> </td>
  205.   </tr>
  206.   <tr valign="top">
  207.     <td width="200" align="right"><input name="rss2" type="checkbox" id="rss2" value="1"
  208.     <?php if ($rss2)  echo "checked" ?> /></td>
  209.     <td align="left" scope="row"><?php _e("RSS 2") ?>    <a href="<?php echo get_bloginfo('rss2_url'); ?> " title="<?php bloginfo('name'); ?> - rss2" target="_blank"><?php echo get_bloginfo('rss2_url'); ?> </a> </td>
  210.   </tr>    
  211. </table>
  212.  
  213. <h3> <?php _e("Include: ") ?></h3>
  214. <table width="100%" cellpadding="10" class="form-table">  
  215.   <tr valign="top">
  216.     <td width="200" align="right"><input name="enclosure" type="checkbox" value="1"
  217.     <?php if ($enclosure) echo "checked" ?> /></td>
  218.     <td align="left" scope="row"><?php _e("Enclosure Tag") ?></td>
  219.   </tr>
  220.   <tr valign="top">
  221.     <td width="200" align="right"><input name="media" type="checkbox" value="1"
  222.     <?php if ($media)  echo "checked" ?> /></td>
  223.     <td align="left" scope="row"><?php _e("Media Tag") ?></td>
  224.   </tr>    
  225. </table>
  226.  
  227. <p class="submit">
  228. <input type="submit" name="Submit" value="<?php _e("Update") ?>" />
  229. </p>
  230.  
  231. </form>
  232. </div>
  233.  
  234. <?php }  ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement