WordPress 中文文档

Wp-cron-feedwordpress.php

From WordPress Chinese

Jump to: navigation, search

The following code saved as wp-cron-feedwordpress.php initiates a feedwordpress once an hour.

Everytime the scheduled job runs it calls http://hostname/wp-content/update-feeds.php

It stores the output of the feedwordpress program in a system-reports blog, which on my site is blog 22 user 34. This way that blog can be checked for the output of the program running.

<?php
/*
Plugin Name: WP-Cron FeedOnFeeds
Plugin URI: http://www.skippy.net/blog/2005/05/26/plugin-wp-cron/
Description: regularly pings your feedwordpress update-quiet.php; requires WP-Cron.
Version: 1.1
Author: Scott Merrill and Martin Cleaver
Author URI: http://www.skippy.net/
Author URI: http://martin.cleaver.org/
Copyright (c) 2005 Scott Merrill (skippy@skippy.net)
Copyright (c) 2006 Martin Cleaver
Released under the terms of the GNU GPL
*/
wp_schedule_event(0, 'hourly', 'wp_cron_feedonfeeds' );
function wp_cron_feedonfeeds() {
 //  wp_mail(get_site_option( "admin_email" ), "wp_cron_feedonfeeds started");
       require_once(ABSPATH . 'wp-includes/class-snoopy.php');
       $snoopy = new Snoopy();
       $snoopy->agent = 'WP-Cron (Snoopy)';
       $snoopy->host = $_SERVER[ 'HTTP_HOST' ];
       $snoopy->read_timeout = "180";
       $snoopy->use_gzip = MAGPIE_USE_GZIP;
       $snoopy->user = 'admin';
       $snoopy->pass = 'ADMIN_PASS_HERE';
       $url = 'http://'.$snoopy->host.'/wp-content/update-feeds.php';
       $submit_vars["action"] = "";
       $submit_vars["method"] = "post";
       $submit_vars["update"] = "verbose";
       if(@$snoopy->submit($url,$submit_vars))
       {
//              echo "response code: ".$snoopy->response_code."
\n"; // $arr=$snoopy->headers; // while(list($key,$var)=each($arr)){echo "$key , $var
";} $results = $snoopy->results; } else { $results = "error fetching document: ".$snoopy->error."\n"; }
       wp_cron_notify($results);
}
function wp_cron_notify($message = 'No message') {
       global $wpdb;
       $system_reports_blog = 22;
       $blogid = $wpdb->blogid;
       if ($blogid == $system_reports_blog) {
         // no point reporting about ourselves!
         return;
       }
       if (preg_match("/ERROR: I don.t syndicate/",$message)==1) {
         return;
       }
       if (preg_match("/0 new posts were syndicated and 0 existing posts were updated/") == 1)
{
         return;
       }
       $blog = get_blogaddress_by_id($blogid);
       $super_admin_email = get_site_option( "admin_email" );
#       print $super_admin_email;
       if( $super_admin_email ==  )
               $admin_email = 'support@' . $_SERVER[ 'SERVER_NAME' ];
       $message_headers = "MIME-Version: 1.0\n" . "From: " . get_site_option("site_name" ) ." <{$admin_email}>\n" . "Content-Type: text/plain; charset=\"" . get_settings('blog_charset') . "\"\n";
       $subject = "Feedsync:  $blog";
#        wp_mail($super_admin_email, $subject, $message, $message_headers);
       // 34 = sruser
       $post = array('post_author'=>34, 'post_title'=>$subject, 'comment_status' => 'open', 'p

ost_content' => $message, 'post_status' => 'publish');


       switch_to_blog($system_reports_blog);
       $postid = wp_insert_post($post);
       if ($postid == 0) {
         wp_mail($super_admin_email, "wp_insert_post failed postid=$postid", $subject, $message, $message_headers);
       }
       restore_current_blog();


}
?>

用户