LINICKX LifeStream
\n";
echo "Posts with date before " . date('d - M - Y (H:i)',$expireDate) . " will be ignored
\n";
}
$lnx_feeddb_location = $lnx_lifestream_options['feeddb']; // Where should our DB live?
/*
load db into array
*/
if ($lnx_feeddb_location == "wp") { // DB Lives in WordPress
if ($lnx_lifestream_cronverbose) {
echo "The Feed DB lives in WordPress
\n";
}
if (get_option('lnx_lifestream_feeddb')) {
$savedItems = unserialize(get_option('lnx_lifestream_feeddb'));
if ($lnx_lifestream_cronverbose) {
echo "Found Old DB
\n";
}
} else {
$savedItems = array(); // A new DB is created if we don't find it.
if ($lnx_lifestream_cronverbose) {
echo "Creating new DB
\n";
}
}
} elseif ($lnx_feeddb_location == "file") { // DB Lives as a file
if ($lnx_lifestream_cronverbose) {
echo "The Feed DB is a File
\n";
}
$savedItemsFilename = WP_CONTENT_DIR . "/lnx_lifestream_feeddb.txt";
if(file_exists($savedItemsFilename)) {
if ($lnx_lifestream_cronverbose) {
echo "Found a file to read
\n";
}
$savedItems = unserialize(file_get_contents($savedItemsFilename));
if(!$savedItems) {
$savedItems = array();
if ($lnx_lifestream_cronverbose) {
echo "Creating new DB
\n";
}
}
}
} else {
$savedItems = array(); // failsafe, create array.
if ($lnx_lifestream_cronverbose) {
echo "Umm, I couldn't find a valid DB option!";
}
}
/*
Loop through items to find new ones and insert them into db and create post
*/
$counter = 0;
$lnx_lifestream_urls_meta = get_option('lnx_lifestream_urls_meta'); // Load up our Meta DB
foreach($lnx_lifestream_urls as $lnx_lifestream_url) {
set_time_limit(20); // Up the Execution Time
$lnx_lifestream_feed = fetch_feed($lnx_lifestream_url); // go WP-SimplePie, do your thing!
if ($lnx_lifestream_cronverbose) {
echo " \n Feed ID / Counter = " . $counter . "
\n";
echo 'Fetching Feed: ' . $lnx_lifestream_url .'
' . "\n";
}
if (isset($lnx_lifestream_feed->errors)) {
echo 'There is a error downloading the feed.
';
echo 'Printing debug:
';
print_r($lnx_lifestream_feed->errors);
echo '
';
} else {
foreach($lnx_lifestream_feed->get_items() as $item)
{
// if item is too old dont even look at it
if($item->get_date('U') < $expireDate) {
if ($lnx_lifestream_cronverbose) {
echo $item->get_title() . " Too Old, skipping...
\n";
}
continue;
}
// make id
$id = md5($item->get_id());
// if item is already in db, skip it
if(isset($savedItems[$id])) {
if ($lnx_lifestream_cronverbose) {
echo $item->get_title() . " Is Already in DB, Skipping...
\n";
}
continue;
}
// Post fail safe, if id exists in meta data, skip it...
$lnx_meta_querey_res = $wpdb->get_results("SELECT meta_key, meta_value FROM $wpdb->postmeta WHERE meta_key = \"lnx_lifestream_id\" AND meta_value = \"$id\"");
if ($lnx_meta_querey_res) {
// Ah-ha, we've found this already exists!
if ($lnx_lifestream_cronverbose) {
echo $item->get_title() . " Post Fail-Safe tripped, Skipping...
\n";
}
// Allow users to override our fail-safe thingy
if (isset($_GET['fsoverride'])) {
if (is_numeric($_GET['fsoverride'])) {
if ($_GET['fsoverride'] == "1") {
$lnx_lifestream_fso = true;
}
}
}
if (!$lnx_lifestream_fso) { // Override is off
continue;
} else {
if ($lnx_lifestream_cronverbose) {
echo "Fail-Safe-Overide enabled, posting... " . $item->get_title() . "
\n";
}
}
}
// found new item, add it to db
$i = array();
$i['title'] = $item->get_title();
$i['title'] = trim($i['title']);
$i['link'] = $item->get_link();
$i['link'] = trim($i['link']);
$i['author'] = '';
$author = $item->get_author();
if($author)
{
$i['author'] = $author->get_name();
$i['author'] = trim($i['author']);
}
$i['date'] = $item->get_date('U');
$i['date'] = trim($i['date']);
/*
// This line here is what caused the multi-post issue http://wordpress.org/support/topic/330243
// I wanted it so in the future I could post feed content, but since I don't need it now it's part of this comment :-)
$i['content'] = $item->get_content();
*/
$i_feed = $item->get_feed();
$i['feed_link'] = $i_feed->get_permalink();
$i['feed_link'] = trim($i['feed_link']);
$i['feed_title'] = $i_feed->get_title();
$i['feed_title'] = trim($i['feed_title']);
if ($lnx_lifestream_cronverbose) {
?>
Found a New Item
Item Title: " . $i['title'] . " \n";
echo "- Item Link: " . $i['link'] . "
\n";
echo "- Item Author: " . $i['author'] . "
\n";
echo "- Item Date: " . $i['date'] . "
\n";
echo "- Feed Link: " . $i['feed_link'] . "
\n";
echo "- Feed Title: " . $i['feed_title'] . "
\n";
?>
get_date('Y-m-d H:i:s');
if (isset($lnx_lifestream_urls_meta[$counter]['oembed'])) {
if ( $lnx_lifestream_urls_meta[$counter]['oembed'] == "1") {
$lnx_post['post_content'] = $i['link'];
} else {
$lnx_post['post_content'] = '' . $i['title'] . '';
}
} else {
$lnx_post['post_content'] = '' . $i['title'] . '';
}
// Insert the post into the database
$lnx_wp_post_ID = wp_insert_post( $lnx_post );
// Post Meta Data, it's like a TAG we were here
add_post_meta($lnx_wp_post_ID, 'lnx_lifestream_id', $id);
// Set the Post Format
if (isset($lnx_lifestream_urls_meta[$counter]['cat'])) {
set_post_format( $lnx_wp_post_ID , $lnx_lifestream_urls_meta[$counter]['format']);
}
if ($lnx_lifestream_cronverbose) {
?> Creating New Post -
Post Title: " . $lnx_post['post_title'] . " \n";
echo "- Post Status: " . $lnx_post['post_status'] . "
\n";
echo "- Post Author: " . $lnx_post['post_author'] . "
\n";
echo "- Post Category: " . $lnx_post['post_category'] . "
\n";
echo "- Post Tags: " . $lnx_post['tags_input'] . "
\n";
echo "- Post Date: " . $lnx_post['post_date'] . "
\n";
echo "- Post Content: " . $lnx_post['post_content'] . "
\n";
echo "- Post Meta ID: " . $id . "
\n";
echo "- Post Format: " . $lnx_lifestream_urls_meta[$counter]['format'] . "
\n";
?>
\n";
}
unset($savedItems[$key]);
}
}
/*
sort items in reverse chronological order
*/
function customSort($a,$b)
{
return $a['date'] <= $b['date'];
}
uasort($savedItems,'customSort');
if ($lnx_lifestream_cronverbose) {
echo "\n
\n \n"; // White space, to make output more readable.
}
/*
save db
*/
if ($lnx_feeddb_location == "wp") { // DB Lives in WordPress
if (get_option('lnx_lifestream_feeddb')) {
update_option('lnx_lifestream_feeddb',serialize($savedItems));
if ($lnx_lifestream_cronverbose) {
echo "Updating DB in WP
\n";
}
} else {
// Create New DB :-)
add_option('lnx_lifestream_feeddb',serialize($savedItems));
if ($lnx_lifestream_cronverbose) {
echo "Creating new DB in WP
\n";
}
}
} elseif ($lnx_feeddb_location == "file") { // DB Lives as a file
if (!$handle = fopen($savedItemsFilename, 'a')) {
echo "Cannot open file ($savedItemsFilename)";
}
if(!fwrite($handle,serialize($savedItems))) {
echo ("Error: Can't save items to file.
\n");
} else {
if ($lnx_lifestream_cronverbose) {
echo "Writing DB to File
\n";
}
}
} else {
echo ("Error: Can't save Feed Database.
\n");
}
/*
End Script
*/
$lnx_lifestream_options['isrunning'] = false;
update_option('lnx_lifestream_options', $lnx_lifestream_options);
if ($lnx_lifestream_cronverbose) {
echo "Done.";
}
} else {
if ($lnx_lifestream_cronverbose) {
echo "No URLS/FEEDS Found in Database. \n";
}
}
?>