PHP RSS Feed Aggregator changing to todays date everytime feed is aggregated


Joined
Mar 25, 2021
Messages
11
Reaction score
0
Hey, was wondering if someone might be able to rummage through the following and provide some insight as to why this custom RSS feed aggregation code is returning the current date and not the actual pub date? The key variable here is $cleanDate I believe. This is using some older RSS aggregation script which I will add at the bottom, it is still on Sourceforge but seems as though it has probably been abandoned. Everything seems to function normally with the exception that each time this thing reaggregates the feed it is converting the dates to todays date. Hopefully someone out there can shed light on what is going on here.

PHP:
// RSS Aggregation
function rssAggregation($feedSection) {
    $rss = new rss_php;
    $feedURL = array();
    $cleanFeed = array();
    $dups = array();
    $limit = array();

    if(get_field($feedSection, 'option')) {
        while(has_sub_field($feedSection, 'option')) {
            $feedURL[] = get_sub_field('url');
            $limit[] = get_sub_field('limit');
        }
    }

    for ($i = 0; $i < count($feedURL); $i++) {
        $rss->load($feedURL[$i]);
        $items = $rss->getItems();
        for ($j = 0; $j < $limit[$i]; $j++) {
            if (!in_array($items[$j]['title'], $dups)) {
                $dups[] = $items[$j]['title'];
                $cleanFeed[] = $items[$j];
            }
        }
    }

    return $cleanFeed;
}

// read feed from xml
function xmlFeed($section) {
    $rss = new rss_php;
    $feedURL = site_url().'/'.$section.'.xml';
    $rss->load($feedURL);
    return $rss->getItems();
}

// RSS News Items
function rssNewsItems() {
    $items = xmlFeed('MYFEED');
    $items = orderByDate($items);

    /// change this for max post display ///
    $maxPosts = 19;

    $counter = 1;
    $output = '<ul class="buzz-list">';
    if (count($items) >= 2) {
        for ($i = 0; $i < $maxPosts; $i++) {
            $cleanDate = convertPubDate($items[$i][pubDate], 'F dS, Y');
            if ($items[$i][description] != '') {
                $output .= '<li>';
                $output .= '<h2><a href="'.$items[$i][link].'" target="_blank">'.htmlspecialchars_decode($items[$i][title]).'</a></h2>';
                $output .= '<p>'.htmlspecialchars_decode(substr($items[$i][description], 0, 175)).' ... <em class="date">'.$cleanDate.'</em></p>';
                $output .= '</li>';

                if (($counter % 2) == 0) { $output .= '</ul><ul class="buzz-list">'; }
                $counter++;
            }
        }
    } else {
        $output = '<ul class="buzz-list"><li><p>Please check back soon...</p></li></ul>';
    }
    $output .= '</ul>';
    return $output;
}

// RSS Upcoming Items
function rssUpcomingItems() {
    $items = xmlFeed('events');
    $counter = 1;
    $output = '';
    $output = '<ul class="upcoming-list">';
    if (count($items) >= 2) {
        for ($i = 0; $i < count($items); $i++) {
            $cleanDate = convertPubDate($items[$i][pubDate], 'm/d');
            $temp  = explode(':', $items[$i][title]);
            $itemTitle = $temp[1];
            if ($itemTitle != '') {
                $output .= '<li>';
                $output .= '<header class="heading">';
                $output .= '<em class="date">'.$cleanDate.'</em>';
                $output .= '<h2><a href="'.$items[$i][link].'" target="_blank">'.$itemTitle.'</a></h2>';
                $output .= '</header>';
                $output .= convertPubDate($items[$i][pubDate], 'l, M. dS, g:iA');
                $output .= '</li>';

                if (($counter % 3) == 0) { $output .= '</ul><ul class="upcoming-list">'; }
                $counter++;
            }
        }
    } else {
        $output .= '<li><p>No events are currently scheduled. Please check back soon ...</p></li>';
    }
    $output .= '</ul>';
    return $output;
}


// RSS News Items with thumbnails
function rssNewsItemsThumbnails() {
    $items = xmlFeed('MYFEED');
    $items = orderByDate($items);

    /// change this for max post display ///
    $maxPosts = 12;

    $output = '';
    $output = '<ul id="spotlight">';

    if (count($items) >= 2) {
        for ($i = 0; $i < $maxPosts; $i++) {
            if ($items[$i][description] != '') {
                $cleanDate = convertPubDate($items[$i][pubDate], 'F dS, Y');
                $output .= '<li>';
                if ($items[$i][thumb] != '') {
                    $output .= '<img src="'.$items[$i][thumb].'" width="85px" height="85px" alt="'.$items[$i][title].'">';
                } else {
                    $output .= '<img src="https://domain.com/wp-content/themes/mytheme/assets/images/placeholder-news.png" width="85px" height="85px" alt="'.$items[$i][title].'">';
                }
                $output .= '<h3><a href="'.$items[$i][link].'" target="_blank">'.$items[$i][title].'</a></h3>';
                $output .= '<p>'.htmlspecialchars_decode($items[$i][description]).' <em class="date">'.$cleanDate.'</em></p>';
                $output .= '</li>';
            }
        }
    } else {
        $output .= '<li><p>Please check back soon ...</p></li>';
    }
    $output .= '</ul>';
    return $output;
}

// convert pub dates
function convertPubDate($date, $format) {
    $tempDate = new DateTime($date);
    return $tempDate->format($format);
}

// order by date
function orderByDate($items) {
    //$tempDate = strtotime($item);
    $referenceArray = array();
    for ($i = 0; $i < count($items); $i++) {
        $referenceArray[] = strtotime($items[$i][pubDate]);
    }
    array_multisort($referenceArray, SORT_DESC, $items);
    return $items;
}

function savePostUpdateXML($post_id) {
    wp_unschedule_event(time(), 'task_rss_update');
    wp_clear_scheduled_hook('task_rss_update');

    // cron job
    if (!wp_next_scheduled('task_rss_update')) {
        wp_schedule_event( time(), 'daily', 'task_rss_update' );
    }
    add_action('task_rss_update', 'updateRSS');


    if ( wp_is_post_revision( $post_id ) ){

          // unhook this function so it doesn't loop infinitely
          remove_action('save_post', 'savePostUpdateXML');
        // If this is a revision, get real post ID
        if ( $parent_id = wp_is_post_revision( $post_id ) ) { $post_id = $parent_id; }
        $my_post = array(
          'ID'           => $post_id
      );
          // update the post, which calls save_post again
          wp_update_post( $my_post );

          // re-hook this function
          add_action('save_post', 'savePostUpdateXML');
      }
}
add_action( 'save_post', 'savePostUpdateXML' );

/** reset cron job **/
/*
wp_unschedule_event(time(), 'task_rss_update');
wp_clear_scheduled_hook('task_rss_update');
*/
/** reset cron job **/

// cron job
if (!wp_next_scheduled('task_rss_update')) {
    wp_schedule_event( time(), 'daily', 'task_rss_update' );
}
add_action('task_rss_update', 'updateRSS');

function updateRSS() {
    // buzz buzz
    $items = rssAggregation('research-feed');
    $items = orderByDate($items);
    createXML($items, 'research');

    // upcoming
    $items = rssAggregation('events-feed');
    createXML($items, 'events');

}

function createXML($items, $fileName) {
    global $post;
    $feed = '';
    $feed = '<?xml version="1.0" encoding="UTF-8"?>';
    $feed .= '<rss version="2.0">';
    $feed .= '<channel>';
    $feed .= '<title>'.$fileName.'</title>';
    $feed .= '<description></description>';
    $feed .= '<link></link>';

    if ($fileName == 'research') {
        for($i = 0; $i < count($items); $i++) {
            if ($items[$i][description] != '') {
                $feed .= '<item>';
                $feed .= '<title><![CDATA['.$items[$i][title].']]></title>';
                $feed .= '<description><![CDATA['.$items[$i][description].']]></description>';
                $feed .= '<pubDate>'.convertPubDate($items[$i][pubDate], 'F dS, Y').'</pubDate>';
                $feed .= '<link><![CDATA['.$items[$i][link].']]></link>';
                if ($items[$i][thumb_url] != '') {
                    $feed .= '<thumb>'.$items[$i][thumb_url].'</thumb>';
                } else {
                    $feed .= '<thumb>https://domain.com/wp-content/themes/mytheme/assets/images/placeholder-news.png</thumb>';
                }
                $feed .= '</item>';
            }
        }

        $args = array('category_name' => 'uncategorized');
        query_posts($args);
        if ( have_posts() ) : while ( have_posts() ) : the_post();
            $feed .= '<item>';
            $feed .= '<title><![CDATA['.htmlspecialchars(get_the_title($post->ID), ENT_QUOTES, 'UTF-8').']]></title>';
            $feed .= '<description><![CDATA['.htmlspecialchars(get_field('content', $post->ID), ENT_QUOTES, 'UTF-8').']]></description>';
            $feed .= '<pubDate>'.convertPubDate(get_the_date(), 'F dS, Y').'</pubDate>';
            $feed .= '<link><![CDATA['.get_field('external-link', $post->ID).']]></link>';
            if (get_field('image', $post->ID) != '') {
                $feed .= '<thumb>'.get_field('image', $post->ID).'</thumb>';
            } else {
                $feed .= '<thumb>https://domain.com/wp-content/themes/mytheme/assets/images/placeholder-news.png</thumb>';
            }
            $feed .= '</item>';
        endwhile;
        endif;
    } else if ($fileName == 'events') {
        for($i = 0; $i < count($items); $i++) {
            if ($items[$i][description] != '') {
                $feed .= '<item>';
                $feed .= '<title><![CDATA['.$items[$i][title].']]></title>';
                $feed .= '<description><![CDATA['.$items[$i][description].']]></description>';
                $feed .= '<pubDate>'.convertPubDate($items[$i][pubDate], 'l, M. dS, g:iA').'</pubDate>';
                $feed .= '<pubDate1>'.convertPubDate($items[$i][pubDate], 'm/d').'</pubDate1>';
                $feed .= '<link><![CDATA['.$items[$i][link].']]></link>';
                $feed .= '</item>';
            }
        }
    }

    $feed .= '</channel> ';
    $feed .= '</rss>';

    $xmlFile = $fileName.'.xml';
    $fh = fopen($xmlFile, 'w') or die("can't open file");
    fwrite($fh, $feed);
    fclose($fh);
}

Code:
<?php
/*
    RSS_PHP - the PHP DOM based RSS Parser
    Author: <rssphp.net>
    Published: 200801 :: blacknet :: via rssphp.net
   
    RSS_PHP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY.

    Usage:
        See the documentation at https://rssphp.net/documentation
    Examples:
        Can be found online at https://rssphp.net/examples
*/

class rss_php {
   
    public $document;
    public $channel;
    public $items;

/****************************
    public load methods
***/
    # load RSS by URL
        public function load($url=false, $unblock=true) {
            if($url) {
                if($unblock) {
                    $this->loadParser(file_get_contents($url, false, $this->randomContext()));
                } else {
                    $this->loadParser(file_get_contents($url));
                }
            }
        }
    # load raw RSS data
        public function loadRSS($rawxml=false) {
            if($rawxml) {
                $this->loadParser($rawxml);
            }
        }
       
/****************************
    public load methods
        @param $includeAttributes BOOLEAN
        return array;
***/
    # return full rss array
        public function getRSS($includeAttributes=false) {
            if($includeAttributes) {
                return $this->document;
            }
            return $this->valueReturner();
        }
    # return channel data
        public function getChannel($includeAttributes=false) {
            if($includeAttributes) {
                return $this->channel;
            }
            return $this->valueReturner($this->channel);
        }
    # return rss items
        public function getItems($includeAttributes=false) {
            if($includeAttributes) {
                return $this->items;
            }
            return $this->valueReturner($this->items);
        }

/****************************
    internal methods
***/
    private function loadParser($rss=false) {
        if($rss) {
            $this->document = array();
            $this->channel = array();
            $this->items = array();
            $DOMDocument = new DOMDocument;
            $DOMDocument->strictErrorChecking = false;
            $DOMDocument->loadXML($rss);
            $this->document = $this->extractDOM($DOMDocument->childNodes);
        }
    }
   
    private function valueReturner($valueBlock=false) {
        if(!$valueBlock) {
            $valueBlock = $this->document;
        }
        foreach($valueBlock as $valueName => $values) {
                if(isset($values['value'])) {
                    $values = $values['value'];
                }
                if(is_array($values)) {
                    $valueBlock[$valueName] = $this->valueReturner($values);
                } else {
                    $valueBlock[$valueName] = $values;
                }
        }
        return $valueBlock;
    }
   
    private function extractDOM($nodeList,$parentNodeName=false) {
        $itemCounter = 0;
        foreach($nodeList as $values) {
            if(substr($values->nodeName,0,1) != '#') {
                if($values->nodeName == 'item') {
                    $nodeName = $values->nodeName.':'.$itemCounter;
                    $itemCounter++;
                } else {
                    $nodeName = $values->nodeName;
                }
                $tempNode[$nodeName] = array();               
                if($values->attributes) {
                    for($i=0;$values->attributes->item($i);$i++) {
                        $tempNode[$nodeName]['properties'][$values->attributes->item($i)->nodeName] = $values->attributes->item($i)->nodeValue;
                    }
                }
                if(!$values->firstChild) {
                    $tempNode[$nodeName]['value'] = $values->textContent;
                } else {
                    $tempNode[$nodeName]['value']  = $this->extractDOM($values->childNodes, $values->nodeName);
                }
                if(in_array($parentNodeName, array('channel','rdf:RDF'))) {
                    if($values->nodeName == 'item') {
                        $this->items[] = $tempNode[$nodeName]['value'];
                    } elseif(!in_array($values->nodeName, array('rss','channel'))) {
                        $this->channel[$values->nodeName] = $tempNode[$nodeName];
                    }
                }
            } elseif(substr($values->nodeName,1) == 'text') {
                $tempValue = trim(preg_replace('/\s\s+/',' ',str_replace("\n",' ', $values->textContent)));
                if($tempValue) {
                    $tempNode = $tempValue;
                }
            } elseif(substr($values->nodeName,1) == 'cdata-section'){
                $tempNode = $values->textContent;
            }
        }
        return $tempNode;
    }
   
    private function randomContext() {
        $headerstrings = array();
        $headerstrings['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.'.rand(0,2).'; en-US; rv:1.'.rand(2,9).'.'.rand(0,4).'.'.rand(1,9).') Gecko/2007'.rand(10,12).rand(10,30).' Firefox/2.0.'.rand(0,1).'.'.rand(1,9);
        $headerstrings['Accept-Charset'] = rand(0,1) ? 'en-gb,en;q=0.'.rand(3,8) : 'en-us,en;q=0.'.rand(3,8);
        $headerstrings['Accept-Language'] = 'en-us,en;q=0.'.rand(4,6);
        $setHeaders =     'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'."\r\n".
                        'Accept-Charset: '.$headerstrings['Accept-Charset']."\r\n".
                        'Accept-Language: '.$headerstrings['Accept-Language']."\r\n".
                        'User-Agent: '.$headerstrings['User-Agent']."\r\n";
        $contextOptions = array(
            'http'=>array(
                'method'=>"GET",
                'header'=>$setHeaders
            )
        );
        return stream_context_create($contextOptions);
    }
   
}

?>
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top