function freshness($format = 'number')
{
global $wpdb;
if ($this->filtered()) {
// This should never happen.
FeedWordPress::critical_bug('SyndicatedPost', $this, __LINE__, __FILE__);
}
if (is_null($this->_freshness)) {
// Not yet checked and cached.
$guid = $this->post['guid'];
$eguid = esc_sql($this->post['guid']);
$q = new WP_Query(array('fields' => '_synfresh', 'ignore_sticky_posts' => true, 'guid' => $guid));
$old_post = NULL;
if ($q->have_posts()) {
while ($q->have_posts()) {
$q->the_post();
$old_post = $q->post;
}
}
if (is_null($old_post)) {
// No post with this guid
FeedWordPress::diagnostic('feed_items:freshness', 'Item [' . $guid . '] "' . $this->entry->get_title() . '" is a NEW POST.');
$this->_wp_id = NULL;
$this->_freshness = 2;
// New content
} else {
// Presume there is nothing new until we find
// something new.
$updated = false;
$live = false;
// Pull the list of existing revisions to get
// timestamps.
$revisions = wp_get_post_revisions($old_post->ID);
foreach ($revisions as $rev) {
$revisions_ts[] = mysql2date('G', $rev->post_modified_gmt);
}
$revisions_ts[] = mysql2date('G', $old_post->post_modified_gmt);
$last_rev_ts = end($revisions_ts);
$updated_ts = $this->updated(true, NULL);
// If we have an explicit updated timestamp,
// check that against existing stamps.
if (!is_null($updated_ts)) {
$updated = !in_array($updated_ts, $revisions_ts);
// If this a newer revision, make it go
// live. If an older one, just record
// the contents.
$live = ($updated and $updated_ts > $last_rev_ts);
}
// This is a revision we haven't seen before, judging by the date.
$updatedReason = NULL;
if ($updated) {
$updatedReason = preg_replace("/\\s+/", " ", 'has been marked with a new timestamp (' . date('Y-m-d H:i:s', $updated_ts) . " > " . date('Y-m-d H:i:s', $last_rev_ts) . ')');
// The date does not indicate a new revision, so
// let's check the hash.
} else {
// Or the hash...
$hash = $this->update_hash();
$seen = $this->stored_hashes($old_post->ID);
if (count($seen) > 0) {
$updated = !in_array($hash, $seen);
// Not seen yet?
} else {
$updated = true;
// Can't find syndication meta-data
}
if ($updated and FeedWordPress::diagnostic_on('feed_items:freshness:reasons')) {
// In the absence of definitive
// timestamp information, we
// just have to assume that a
// hash we haven't seen before
// is a newer version.
$live = true;
$updatedReason = ' has a not-yet-seen update hash: ' . MyPHP::val($hash) . ' not in {' . implode(", ", array_map(array('FeedWordPress', 'val'), $seen)) . '}. Basis: ' . MyPHP::val(array_keys($this->update_hash(false)));
}
}
$frozen = false;
if ($updated) {
// Ignore if the post is frozen
$frozen = 'yes' == $this->link->setting('freeze updates', 'freeze_updates', NULL);
if (!$frozen) {
$frozen_values = get_post_custom_values('_syndication_freeze_updates', $old_post->ID);
$frozen = (count($frozen_values) > 0 and 'yes' == $frozen_values[0]);
if ($frozen) {
$updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A UPDATE LOCK ON THIS POST, EVEN THOUGH IT ' . $updatedReason;
}
} else {
$updatedReason = ' IS BLOCKED FROM BEING UPDATED BY A FEEDWORDPRESS UPDATE LOCK, EVEN THOUGH IT ' . $updatedReason;
}
}
$live = ($live and !$frozen);
if ($updated) {
FeedWordPress::diagnostic('feed_items:freshness', 'Item [' . $guid . '] "' . $this->entry->get_title() . '" is an update of an existing post.');
if (!is_null($updatedReason)) {
$updatedReason = preg_replace('/\\s+/', ' ', $updatedReason);
FeedWordPress::diagnostic('feed_items:freshness:reasons', 'Item [' . $guid . '] "' . $this->entry->get_title() . '" ' . $updatedReason);
}
$this->_freshness = apply_filters('syndicated_item_freshness', $live ? 1 : -1, $updated, $frozen, $updated_ts, $last_rev_ts, $this);
$this->_wp_id = $old_post->ID;
$this->_wp_post = $old_post;
// We want this to keep a running list of all the
// processed update hashes.
$this->post['meta']['syndication_item_hash'] = array_merge($this->stored_hashes(), array($this->update_hash()));
} else {
FeedWordPress::diagnostic('feed_items:freshness', 'Item [' . $guid . '] "' . $this->entry->get_title() . '" is a duplicate of an existing post.');
$this->_freshness = 0;
// Same old, same old
$this->_wp_id = $old_post->ID;
}
}
}
switch ($format) {
case 'status':
switch ($this->_freshness) {
case -1:
$ret = 'stored';
break;
case 0:
$ret = NULL;
break;
case 1:
$ret = 'updated';
break;
case 2:
default:
$ret = 'new';
break;
}
break;
case 'number':
default:
$ret = $this->_freshness;
}
return $ret;
}