WordPress 中文文档
Mrjc-feedwordpress-filters.php
From WordPress Chinese
The following code works with feedwordpress to syndicate only items that match certain criteria.
It shows how to use the outgoing hook, and to retag it using Simple Tagging Plugin.
<?php
/*
//http://projects.radgeek.com/2005/03/30/feedwordpress-09/
Plugin Name: Feed Filters
Description: Filters incoming posts
Author: Charles Johnson
Author: Martin Cleaver
Version: 2005.03.30
Author URI: http://www.radgeek.com/ http://projects.radgeek.com/feedwordpress/api/
Author URI: http://martin.cleaver.org/ http://www.blendedperspectives.com/
*/
/*
syndicated_item: a filter applied to each item coming off the Atom/RSS newsfeed. Filter functions receive as an argument an associative array representing the contents of the item (in the format returned by MagpieRSS). They should return either the item.with any changes that you would like reflected when it is converted into a post for WordPress.s database.or NULL. If NULL is returned, FeedWordPress will filter this item out and skip ahead to the next one
*/
add_filter('syndicated_item', 'mrjc_filter_post');
/*
feedwordpress_update: an action invoked just before FeedWordPress begins to check one or more newsfeeds for updates. Hooked in functions receive as an argument the URI of the blog or newsfeed that FeedWordPress is updating.
*/
add_filter('feedwordpress_update', 'mrjc_start_update');
/*update_syndicated_item: an action that is invoked whenever FeedWordPress has completed updating a previously existing post in the WordPress database. Receives the numerical ID of the updated post as an argument.
*/
add_filter('update_syndicated_item', 'mrjc_update_syndicated_item');
/*an action that is invoked whenever FeedWordPress has completed inserting a new post into the WordPress database. Receives the numerical ID of the new post as an argument.
*/
add_filter('post_syndicated_item', 'mrjc_post_syndicated_item');
function mrjc_start_update() {
print "Updating feed. Note that 1) Only items with wiki in the category are accepted. 2) currently the categories for input feeds are lost during import";
}
function mrjc_update_syndicated_item($item) {
print "Updating $item";
return $item;
}
function mrjc_filter_post($item) {
print "<h2>Filtering New Post</h2>";
print "<pre>";
print_r ($item);
print "";
$regex_match_blogname = '/.*this|that|another/i';
$regex_match_content = '/\sWORD/i'; // it's got to include word WORD e.g. a link to WORD is insufficient.
$regex_match_category = '/CAT/i';
$categories = extract_categories($item);
print "Added to $contentfield\n";
if ($item['content']['encoded']) {
$contentfield = "['content']['encoded']";
} else {
$contentfield = "['description']";
}
$description = get_nestedkey($item,$contentfield);
print "Categories: $categories\n";print "
"; print_r ($categories); print "";
print "Description($contentfield): ".substr($description, 0,200)."\n";
$store = false;
$blogid = $wpdb->blogid; $blogname = get_blogaddress_by_id($blogid); print $blogname;
if (preg_match($regex_match_blogname, $blogname)) {
$store = true;
print "Matched content on blogname
";
} else {
print "Didn't match on blogname ($blogname)
";
}
foreach($categories as $category) {
if (preg_match($regex_match_category, $category)) {
$store = true;
print "Matched Category $regex_match_category to $category
";
}
}
// print "";
// print "Post category: ".$item['category'];
// print "";
if (preg_match($regex_match_content, $description)) {
$store = true;
print "Matched Content $regex_match_content to $description";
}
$xcal = $item['xcal'];
if ($xcal) {
$add = "City: ".$xcal['x-calconnect-venue_adr_x-calconnect-city']."\n ";
$add .= "Country: ".$xcal['x-calconnect-venue_adr_x-calconnect-country'];
$item['description'] .= "\n".$add;
$event_categories = array('event',
$xcal['x-calconnect-venue_adr_x-calconnect-city'],
$xcal['x-calconnect-venue_adr_x-calconnect-country']);
$categories = array_merge($categories, $event_categories);
print "VENUE DETECTED:".$add;
}
print "Categories extracted:\n"; print_r($categories); $item['category'] = $categories; print "\n"; // fudge the categories into the end of the description
$serializedCategories = "CATSTART:".serialize($categories); $h = ""; print "Serialized as $serializedCategories and added hidden (as invisible, see source)\n";
set_nestedkey($item,
$contentfield,
get_nestedkey($item, $contentfield).$h);
print "Outputted item:\n";print "
"; print_r ($item); print "";
// might be good to also store in the event that there is a full txt match. // return NULL; // Temporary // return $item;
if ($store) {
return $item;
} else {
print "Skipped!";
return NULL;
}
}
function get_nestedkey($item, $key) {
$e_string = "return \$item{$key};";
$ans = eval($e_string);
return $ans;
}
function set_nestedkey(&$item, $key, $value) {
$e_string = "\$item{$key} = '".$value."';";
// print $e_string."\n";
$ans = eval($e_string);
return $ans;
}
/*
post_syndicated_item: an action that is invoked whenever FeedWordPress has completed inserting a new post into the WordPress database. Receives the numerical ID of the new post as an argument.
*/
function mrjc_post_syndicated_item($postid) {
print "Ok. We received $postid";
if ( class_exists('SimpleTagging') ) {
mrjc_tag_post_syndicated_item($postid);
}
}
function mrjc_tag_post_syndicated_item($postid) {
//might be problematic that we don't have the full $item - how do we traverse from the postid to the category, for e.g.
global $STagging, $wpdb;
$sTagObj = $STagging;
print "Tag post received $postid";
$stptable = $sTagObj->info['stptable']; //This value is supposed to be calculated from $this->option['tags_table'] so it can be overridden by the admin //It is set on http://trac.herewithme.fr/project/simpletagging/browser/trunk/simpletagging.php rev 143 line 307 // print "Tags table: $tagstable";
// $stptable = $wpdb->prefix . $tagstable;
$post = get_post($postid); // print_r($post); $categories = $post->post_category;
// print "Inbound cats: ".$categories;
print_r ($categories);
if ($categories) {
print "Not supported: real categories (need getting array of catname from category->category_name)";
$catnames = array('ERR_NOT_SUPPORTED');
// this would build this from $categories
} else {
$catnames = defudge_categories($post);
}
print "Pushing catnames into tags\n";
$count = 0;
foreach($catnames as $catname) {
print "Processing1 cat $catname\n";
$catname = str_replace(',', '.', $catname); // remove comma, for some reasons does Wordpress allow commas in categories
$catname = $sTagObj->tag_convertUserInput($catname); // Convert several other chars we don't allow
// print "Processing2 cat $catname\n";
$queryinsert = ;
if ($catname != ) {
$count++;
$addquote = ($count == 1) ? : ',';
$queryInsert .= "$addquote($postid,'$catname')";
}
// print "count=$count; $queryInsert\n";
}
if ($count > 0) {
$query = "INSERT IGNORE INTO $stptable (post_id, tag_name) VALUES {$queryInsert}";
// print $query;
$wpdb->query($query);
} else {
print "No tags added, count=$count\n";
}
}
// Note we ignore the multiple categories and we control the category name as per 540 comment /* http://trac.herewithme.fr/project/simpletagging/browser/trunk/simpletagging.admin.php rev 131 536 foreach($categories as $category) { 537 // Prepare values 538 $postid = $category->post_id; 539 $catname = str_replace(',', '.', $category->cat_name); // remove comma, for some reasons does Wordpress allow commas in categories 540 $catname = $this->sTagObj->tag_convertUserInput($catname); // Convert several other chars we don't allow 541 542 // Create query 543 if ( $postid != && $catname != ) { 544 $count++; 545 $addquote = ($count == 1) ? : ','; 546 $queryInsert .= "$addquote($postid,'$catname')"; 547 } 548 } 549 if ( $count > 0 ) { // Write values into STP table 550 $wpdb->query("INSERT IGNORE INTO {$this->sTagObj->info['stptable']} ( post_id, tag_name ) VALUES {$queryInsert}"); 551 } */
// from feedwordpress.php item_to_post --L2127
function extract_categories($item) {
$cat = $item['category'];
// $categories = array();
// This is basically a horrid hack that says that words start with a capital letter and that every word should be // a category /* Some versions of Magpie do not parse <category> tags correctly, it concatenates all the category strings into a single long category instead of making an array of categories." http://swik.net/MagpieRSS/Issues+with+MagpieRSS is the behaviour I see with wp-rss.php bundled with wordpress mu */
$categories = preg_split("/([A-Z][a-z]+)/", $cat, -1, PREG_SPLIT_DELIM_CAPTURE);
// This is for eekim
$subj = $item['dc']['subject'];
$subjects = explode('/', $subj);
print "Subjects (Not yet used)\n";
print_r ($subjects);
return array_unique(array_merge($categories,$subjects));
// The following would have been more sane
if ( isset($item['category#']) ) {
for ($i = 1; $i <= $item['category#']; $i++) {
$cat_idx = (($i > 1) ? "#{$i}" : "");
$cat = $item["category{$cat_idx}"];
if ( strpos($f['link/uri'], 'del.icio.us') !== false ) {
$categories = array_merge($post['named']['category'], explode(' ', $cat));
} else {
$categories = $cat;
}
}
} else {
print "No array\n";
}
return $categories;
}
function defudge_categories($post) {
$content = $post->post_content;
list($content, $cats) = preg_split('/CATSTART:/',$content);
// $cats = preg_replace('/\-\-\>
$cats = trim($cats);
// print "C2:'$cats'\n";
$categories = unserialize($cats);
if (!is_array($categories)) {
// something went wrong, initialize to empty array
print "Couldn't defudge cats '$cats'\n";
print "Content = ".$post->$content." ($content)\n";
$categories = array();
}
// print_r ($categories);
// print "Done\n";
return $categories;
}
?>
</pre>
