name, $category->term_id, 'category', $filter);
}
$cat_names = array_unique($cat_names);
foreach ( $cat_names as $cat_name ) {
$the_cats .= "\t\t\n";
}
return $the_cats;
}
/**
* Retrieve all of the post tags, formatted for use in feeds.
*
* @credit WordPress /includes/rss-feed.php
*
* Since 0.44.0
*/
function nrelate_get_the_tags_rss() {
$tags = get_the_tags();
$the_tags = '';
$cat_names = array();
$filter = 'rss';
if ( !empty($tags) ) foreach ( (array) $tags as $tag ) {
$cat_names[] = sanitize_term_field('name', $tag->name, $tag->term_id, 'post_tag', $filter);
}
$cat_names = array_unique($cat_names);
foreach ( $cat_names as $cat_name ) {
$the_tags .= "\t\t\n";
}
return $the_tags;
}
/**
* Display Categories and Tags in feed
*
* @cred WordPress /includes/rss-feed.php
*
* Since 0.44.0
*/
function nrelate_cats_tags_rss() {
echo nrelate_get_the_category_rss();
echo nrelate_get_the_tags_rss();
}
/**
* Get published post count
*
* Updated 0.45.0
*/
function nrelate_post_count() {
global $wp_query;
echo "\t" . $wp_query->found_posts . "\n";
}
/**
* Get Custom Images
*
* Attempts to retrieve any custom images
* set by user or themes/plugins
*
* If no custom image is found, grab the first image in the post
* @updated 0.47.8
*/
function nrelate_get_custom_images($content) {
global $post;
$thumb_found = false;
// Get custom field images if user set a custom field
$options = get_option('nrelate_admin_options');
$customfield = $options['admin_custom_field'];
if ($customfield != null) {
$customfieldvalue = get_post_meta($post->ID, $customfield, $single = true);
if ($customfieldvalue != null) {
$content = '

' . $content;
$thumb_found = true;
}
}
// Simple Post Thumbnails
// http://wordpress.org/extend/plugins/simple-post-thumbnails/
// Since 0.45.0
// Updated 0.47.8
if (!$thumb_found && function_exists('p75HasThumbnail')){
$p75image = p75GetOriginalImage($post->ID);
$p75default = get_option('p75_default_thumbnail');
$imageurl = get_bloginfo('wpurl') . $p75image;
if (empty($p75image)) $imageurl = $p75default;
if ($imageurl) {
$content = sprintf('
%s', $imageurl, $content);
$thumb_found = true;
}
}
// Thumbshots
// http://wordpress.org/extend/plugins/thumbshots/
// Since 0.49.3
if (!$thumb_found && class_exists('ThumbshotsPlugin')){
preg_match('#
]+src=[\"\']{1}(http:\/\/(www\.)?(open\.thumbshots.org/image\.aspx|robothumb\.com/src)[^\"\']*)[\"\']{1}[^>]+\/>#i', $content, $images);
@$imageurl = $images[1];
if ( $imageurl ) {
$content = sprintf('
%s', $imageurl, $content);
$thumb_found = true;
}
}
// WordPress Featured Image (Post Thumbnails)
if (!$thumb_found && function_exists('has_post_thumbnail') && has_post_thumbnail( $post->ID )) {
$default_attr = array( 'class' => "nrelate-image featured-image" );
$content = '' . get_the_post_thumbnail( $post->ID, 'large', $default_attr ) . '
' . $content;
$thumb_found = true;
}
// Last resort for custom images, search through all custom fields for image URL and grab the first
// url must start with http and file must be a gif, png or jpg.
// Since 0.45.0
if (!$thumb_found) {
foreach ( get_post_custom($post->ID) as $key => $values ) {
$meta = get_post_meta($post->ID, $key);
// Extract metadata
if (is_array($meta)) {
$meta = current($meta);
}
foreach( (array)$meta as $key => $imageurl ) {
if ( $imageurl = nrelate_get_img_url($imageurl) ) {
$content = sprintf('
%s', $imageurl, $content);
$thumb_found = true;
break;
}
}
if ($thumb_found) {
break;
}
}
}
// If no images are found yet, grab the first image in the post.
if (!$thumb_found) {
preg_match('#
]+src=[\"\']{1}(http:\/\/.*\.(gif|png|jpg|jpeg|tif|tiff|bmp){1})[\"\']{1}[^>]+\/>#i', $content, $images);
@$imageurl = $images[1];
if ( $imageurl = nrelate_get_img_url($imageurl) ) {
$content = sprintf('
%s', $imageurl, $content);
$thumb_found = true;
}
}
// Last resort. If no images in post, let's check post attachments.
if (!$thumb_found) {
$attachments = get_posts( array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order ID') );
foreach ($attachments as $attach) {
if($img = wp_get_attachment_image_src($attach->ID) ) {
$content = sprintf('
%s', $img[0], $content);
$thumb_found = true;
break;
}
}
}
return $content;
}
/**
* Helper to get the inner image URL from a string
* returns false if no img URL found
*/
function nrelate_get_img_url( $url ) {
if ( !$url = trim( (string) $url ) ) return false;
$parsed = parse_url( html_entity_decode($url) );
if( isset($parsed['query']) ) {
$params = explode( '&', $parsed['query']);
foreach ($params as $pair) {
if ( trim($pair) == '' ) continue;
list( $param, $value) = explode( '=', $pair );
if ( preg_match('#^http:\/\/(.*)\.(gif|png|jpg|jpeg|tif|tiff|bmp)$#i', $value) ) {
// found a valid image URL in a parameter of the original URL
return $value;
}
}
}
if ( preg_match('#^http:\/\/(.*)\.(gif|png|jpg|jpeg|tif|tiff|bmp)$#i', $url) ) {
return $url;
}
return false;
}
/**
* Remove Javascript from our feed
*
*/
function nrelate_remove_script($content) {
$content = preg_replace('#(\n?]*?>)|(\n?