apply_settings();
if ($this->cache) $this->cache = $this->check_cache();
if ($this->trigger('fullfeed')) return $this->outputfeed('fullfeed');
elseif ($this->trigger('posts')) return $this->outputfeed('posts');
elseif ($this->trigger('comments')) return $this->outputfeed('comments');
elseif ($this->trigger('pages')) return $this->outputfeed('pages');
add_action('publish_post', array(&$this, 'expire_post_feeds'));
add_action('delete_post', array(&$this, 'expire_post_feeds'));
add_action('private_to_published', array(&$this, 'expire_post_feeds'));
add_action('comment_post', array(&$this, 'expire_comments_feed'));
add_action('delete_comment', array(&$this, 'expire_comments_feed'));
add_action('trackback_post', array(&$this, 'expire_comments_feed'));
add_action('wp_set_comment_status', array(&$this, 'expire_comments_feed'));
add_action('wpmuadminedit', array(&$this, 'expire_feeds')); // in case the admin deletes a blog
if ( defined('MULTISITE') )
add_action('network_admin_menu', array(&$this, 'add_submenu'));
else
add_action('admin_menu', array(&$this, 'add_submenu'));
}
function trigger($type) {
global $wpdb; //global $blog_id doesn't work!
if ($wpdb->blogid != $this->triggerblog) return false;
if ($type == 'fullfeed') $url = $this->triggerurl;
elseif ($type == 'posts') $url = $this->triggerurl.$this->postsurl;
elseif ($type == 'comments') $url = $this->triggerurl.$this->commentsurl;
elseif ($type == 'pages') $url = $this->triggerurl.$this->pagesurl;
if( $this->is_subdomain_install() ) {
return (substr($_SERVER['REQUEST_URI'], strlen($url)*-1) == $url) ? true : false;
} else {
if ($type == 'fullfeed' && $_GET['wpmu-feed'] == 'full-feed') return true;
elseif ($type == 'posts' && $_GET['wpmu-feed'] == 'posts') return true;
elseif ($type == 'comments' && $_GET['wpmu-feed'] == 'comments') return true;
elseif ($type == 'pages' && $_GET['wpmu-feed'] == 'pages') return true;
return false;
}
}
function check_cache() {
global $wp_object_cache;
//return (is_object($wp_object_cache) && $wp_object_cache->cache_enabled == true) ? true : false;
return (is_object($wp_object_cache)) ? true : false;
}
function cache_expire_time() {
global $wp_object_cache;
if(property_exists('wp_object_cache', 'expiration_time'))
return ($wp_object_cache->expiration_time/60);
else
return (15);
}
function add_submenu() {
if (!is_site_admin()) return false;
if ( defined('MULTISITE') )
add_submenu_page('settings.php', 'Ada Sitewide Feed Configuration', 'Ada Sitewide Feed', 10, 'wpmu_sitewide_feed', array(&$this,'config_page'));
else
add_submenu_page('wpmu-admin.php', 'Ada Sitewide Feed Configuration', 'Ada Sitewide Feed', 10, 'wpmu_sitewide_feed', array(&$this,'config_page'));
}
function save_settings() {
global $wpdb, $wp_db_version, $updated, $configerror;
$changed = false;
check_admin_referer();
// validate all input!
if (preg_match('/^[0-9]+$/',$_POST['triggerblog']) && $_POST['triggerblog'] > 0) $triggerblog = intval($_POST['triggerblog']);
else $configerror[] = 'Trigger blog must be a numeric blog ID. Default: 1';
if (preg_match('/^\/[a-zA-Z0-9_\/\-]+\/$/',$_POST['triggerurl']) || ! $this->is_subdomain_install()) $triggerurl = $_POST['triggerurl'];
else $configerror[] = 'Invalid trigger URL. Must be a relative path beginning with and ending with a "/". Default: /full-feed/';
if (preg_match('/^[a-zA-Z0-9_\-]+\/$/',$_POST['postsurl']) || ! $this->is_subdomain_install()) $postsurl = $_POST['postsurl'];
else $configerror[] = 'Invalid posts URL. Must be a relative path ending with a "/". Default: posts/';
if (preg_match('/^[a-zA-Z0-9_\-]+\/$/',$_POST['commentsurl']) || ! $this->is_subdomain_install()) $commentsurl = $_POST['commentsurl'];
else $configerror[] = 'Invalid comments URL. Must be a relative path ending with a "/". Default: comments/';
if (preg_match('/^[a-zA-Z0-9_\-]+\/$/',$_POST['pagesurl']) || ! $this->is_subdomain_install()) $pagesurl = $_POST['pagesurl'];
else $configerror[] = 'Invalid pages URL. Must be a relative path ending with a "/". Default: pages/';
if (preg_match('/^[0-9]+$/',$_POST['feedcount']) && $_POST['feedcount'] > 0) $feedcount = intval($_POST['feedcount']);
else $configerror[] = 'Post count must be a number greater than zero. Default: 20';
$feedtitle = strip_tags($_POST['feedtitle']);
$feeddesc = strip_tags($_POST['feeddesc']);
$mincontentchars = intval($_POST['mincontentchars']);
if (preg_match('/^[a-zA-Z0-9_\-:%\/\.]*$/',$_POST['siteimageurl'])) $siteimageurl = trim($_POST['siteimageurl']);
else $configerror[] = 'Invalid image URL. Must be a full path. If you leave it blank, it will not be included.';
if (!empty($_POST['showavatar'])) $showavatar = 1;
else $showavatar = 0;
if (!empty($_POST['showstats'])) $showstats = 1;
else $showstats = 0;
if (!empty($_POST['etag'])) $etag = 1;
else $etag = 0;
if (!empty($_POST['cache'])) $cache = 1;
else $cache = 0;
if (isset($_POST['excerpt']) && ($_POST['excerpt'] == 0 || $_POST['excerpt'] == 1)) $excerpt = intval($_POST['excerpt']);
else $configerror[] = 'Use excerpts: Must be a one or zero. Default: 1';
if (preg_match('/^[0-9]+$/',$_POST['expiretime']) && $_POST['expiretime'] >= 0) $expiretime = intval($_POST['expiretime']);
elseif ($wp_db_version > 3513) $configerror[] = 'Expire time must be a number equal to or greater than zero. Default: 0 (expire only when needed)';
else $configerror[] = 'Expire time must be a number equal to or greater than zero. Default: '.$this->cache_expire_time().' (expire to account for future dated posts)';
if ($_POST['expiretime'] > $this->cache_expire_time())
$configerror[] = 'Expire Minutes: Cannot exceed WP Object Cache expiration time of '.$this->cache_expire_time().' minutes.';
if ($wpdb->blogid == $_POST['triggerblog'] && ($_POST['triggerurl'] == '/' || stristr($_POST['triggerurl'],'wp-admin')))
$configerror[] = 'Doh! That combination of blog id and trigger url may have locked you out of your site!';
if (is_array($configerror)) return $configerror;
$settings = compact('triggerblog'
, 'triggerurl'
, 'commentsurl'
, 'pagesurl'
, 'postsurl'
, 'feedtitle'
, 'feeddesc'
, 'feedcount'
, 'excerpt'
, 'mincontentchars'
, 'siteimageurl'
, 'showavatar'
, 'showstats'
, 'cache'
, 'etag'
, 'expiretime');
foreach($settings as $setting => $value) {
if ($this->$setting != $value) $changed = true;
}
if ($changed) {
update_site_option('wpmu_sitefeed_settings', $settings);
$this->expire_feeds();
$this->apply_settings($settings);
return $updated = true;
}
}
function set_defaults() {
global $wp_db_version;
//, $current_site;
// do not edit here - use the admin screen
$this->feedcount = 20;
$this->triggerblog = 1;
if( $this->is_subdomain_install() ) {
$this->triggerurl = '/full-feed/';
$this->commentsurl = 'comments/';
$this->pagesurl = 'pages/';
$this->postsurl = 'posts/';
} else {
$this->triggerurl = '?wpmu-feed=full-feed';
$this->postsurl = '?wpmu-feed=posts';
$this->commentsurl = '?wpmu-feed=comments';
$this->pagesurl = '?wpmu-feed=pages';
}
$this->mincontentchars = 25;
$this->domainpath = get_blog_option($this->triggerblog, 'siteurl');
$this->blogname = get_blog_option($this->triggerblog, 'blogname');
$this->siteimageurl = untrailingslashit($this->domainpath) . '/favicon.ico';
$this->showavatar = 1;
$this->showstats = 1;
$this->excerpt = 1;
$this->cache = 1;
$this->etag = 1;
$this->feedtitle = $this->blogname .' Master Site Feed';
$this->feeddesc = 'Shows all posts, comments, and pages from all blogs on this WordPress powered site';
($wp_db_version > 3513) ? $this->expiretime = 0 : ($this->cache_expire_time() > 15) ? $this->expiretime = 15 : $this->expiretime = $this->cache_expire_time();
//$this->expiretime = $this->cache_expire_time();
}
function apply_settings($settings = false) {
if (!$settings) $settings = get_site_option('wpmu_sitefeed_settings');
if (is_array($settings)) foreach($settings as $setting => $value) $this->$setting = $value;
if (!isset($this->mincontentchars)) { $this->delete_settings(); $this->set_defaults(); }
// else $this->set_defaults();
}
function delete_settings() {
global $wpdb, $updated;
$settings = get_site_option('wpmu_sitefeed_settings');
if ($settings) {
$wpdb->query("DELETE FROM $wpdb->sitemeta WHERE `meta_key` = 'wpmu_sitefeed_settings'");
//if ($this->check_cache()) wp_cache_delete('wpmu_sitefeed_settings','site-options');
if ($this->check_cache()) wp_cache_delete('wpmu_sitefeed_settings','site-options');
$this->set_defaults();
$this->expire_feeds();
return $updated = true;
}
}
function create_feedurl($type) {
//global $wpdb;
if ($type == 'fullfeed') $url = $this->triggerurl;
elseif ($type == 'posts') $url = $this->postsurl;
elseif ($type == 'comments') $url = $this->commentsurl;
elseif ($type == 'pages') $url = $this->pagesurl;
if(in_array($type, array('posts', 'comments', 'pages')) && $this->is_subdomain_install()) {
$url = $this->triggerurl . $url;
}
$this->domainpath = get_blog_option($this->triggerblog, 'siteurl');
$this->blogname = get_blog_option($this->triggerblog, 'blogname');
if($this->domainpath == '') return 'Trigger blog ID was not found!';
return untrailingslashit($this->domainpath).$url;
}
function create_testlink($type) {
return 'test link';
}
function create_map($type) {
global $wpdb;
$excl_blogs = "50";
$multiplier = 100; // new setting to dig deep for posts/comments until we workaround wpmu_update_blogs_date messing with timestamp
$blogs = $wpdb->get_col("SELECT A.`blog_id`
FROM ".$wpdb->base_prefix."blogs AS A
WHERE A.`public` = '1'
AND A.`archived` = '0'
AND A.`spam` = '0'
AND A.`blog_id` NOT IN (" . $excl_blogs . ")
AND A.`deleted` ='0'
AND A.`last_updated` != '0000-00-00 00:00:00'
AND A.site_id = (SELECT distinct site_id FROM ".$wpdb->base_prefix."blogs WHERE blog_id = " . $this->triggerblog . ")
ORDER BY A.`last_updated` DESC
LIMIT ".$this->feedcount*$multiplier);
if (!is_array($blogs)) return false; // New Site?
foreach($blogs as $blogid) {
if ($type == 'fullfeed') {
$results = $wpdb->get_results("SELECT `ID`,`post_date_gmt`
FROM `".$wpdb->base_prefix.$blogid."_posts`
WHERE `post_status` = 'publish'
AND `post_password` = ''
AND `post_date_gmt` < '".gmdate("Y-m-d H:i:s")."'
AND `ID` > 2
AND TRIM(`post_title`) != ''
AND LENGTH(TRIM(`post_content`)) > ".$this->mincontentchars."
ORDER BY `post_date_gmt` DESC
LIMIT ".$this->feedcount);
} elseif ($type == 'comments' ) {
$results = $wpdb->get_results("SELECT comment_ID, comment_date_gmt
FROM ".$wpdb->base_prefix.$blogid."_comments
LEFT JOIN ".$wpdb->base_prefix.$blogid."_posts
ON ".$wpdb->base_prefix.$blogid."_comments.comment_post_id = ".$wpdb->base_prefix.$blogid."_posts.id
WHERE ".$wpdb->base_prefix.$blogid."_posts.post_status = 'publish'
AND ".$wpdb->base_prefix.$blogid."_posts.`post_password` = ''
AND ".$wpdb->base_prefix.$blogid."_comments.comment_approved = '1'
AND ".$wpdb->base_prefix.$blogid."_comments.comment_date_gmt < '" . gmdate("Y-m-d H:i:s") . "'
AND ".$wpdb->base_prefix.$blogid."_comments.comment_ID > 1
AND ".$wpdb->base_prefix.$blogid."_comments.comment_type = ''
ORDER BY comment_date_gmt DESC LIMIT " . $this->feedcount);
} elseif ($type == 'posts') {
$results = $wpdb->get_results("SELECT `ID`,`post_date_gmt`
FROM `".$wpdb->base_prefix.$blogid."_posts`
WHERE `post_status` = 'publish'
AND (`post_type` = 'post' OR `post_type` = '')
AND `post_date_gmt` < '".gmdate("Y-m-d H:i:s")."'
AND `ID` > 1
AND TRIM(`post_title`) != ''
AND LENGTH(TRIM(`post_content`)) > ".$this->mincontentchars."
ORDER BY `post_date_gmt` DESC
LIMIT ".$this->feedcount);
} elseif ($type == 'pages') {
//now even Pages can be password protected
$results = $wpdb->get_results("SELECT `ID`,`post_date_gmt`
FROM `".$wpdb->base_prefix.$blogid."_posts`
WHERE ((`post_status` = 'publish' AND `post_type` = 'page') OR `post_status` = 'static' )
AND `post_password` = ''
AND `post_date_gmt` < '".gmdate("Y-m-d H:i:s")."'
AND `ID` > 2
AND TRIM(`post_title`) != ''
AND LENGTH(TRIM(`post_content`)) > ".$this->mincontentchars."
ORDER BY `post_date_gmt` DESC
LIMIT ".$this->feedcount);
}
if (is_array($results)) {
foreach($results as $result) {
if ($type == 'fullfeed' || $type == 'posts' || $type == 'pages') {
$map[] = array($blogid,$result->ID,$result->post_date_gmt);
$ID[] = $result->ID;
$date_gmt[] = $result->post_date_gmt;
} elseif ( $type == 'comments') {
$map[] = array($blogid,$result->comment_ID,$result->comment_date_gmt);
$ID[] = $result->comment_ID;
$date_gmt[] = $result->comment_date_gmt;
}
}
}
}
if (is_array($map)) {
array_multisort($date_gmt, SORT_DESC, $ID, SORT_DESC, $map);
return array_slice($map,0,$this->feedcount);
}
}
function get_data($type) {
global $wpdb;
$map = $this->create_map($type);
if (!is_array($map)) return false;
foreach($map as $item) {
if ($type == 'fullfeed' || $type == 'posts' || $type == 'pages') {
//has to have SELECT * so global foreach($posts as $post) works
$row = $wpdb->get_row("SELECT * FROM `".$wpdb->base_prefix.intval($item[0])."_posts` WHERE `ID` = '".intval($item[1])."'");
if ($row->ID)
{
$row->blogid = intval($item[0]);
$rows[] = $row;
}
} elseif ( $type == 'comments') {
//has to have SELECT * so global foreach($comments as $comment) works
$row = $wpdb->get_row("SELECT * FROM `".$wpdb->base_prefix.intval($item[0])."_comments` WHERE `comment_ID` = '".intval($item[1])."'");
if ($row->comment_ID)
{
$row->blogid = intval($item[0]);
$rows[] = $row;
}
}
}
if ($rows) return $rows;
}
function latest_time() {
global $posts, $comments;
return ($posts) ? $posts[0]->post_date_gmt : $comments[0]->comment_date_gmt;
}
function save_feed($name,$data) {
/* no need to save the expire time manually. Can set in expire time in wp_cache_set
if ($this->cache) update_site_option($name.'_ts',time());
return ($this->cache) ? wp_cache_set($name,$data,'site-options') : false;*/
return ($this->cache) ? wp_cache_set($name,$data,'site-options',$this->expiretime*60) : false;
}
function fetch_feed($name) {
/* no need to expire the feed manually. Expired cache will return false, when using wp_cache_get()
if ($this->cache) {
$expires = get_site_option($name.'_ts')+($this->expiretime*60);
if ($expires <= time()) $this->expire_feed($name);
}
*/
return ($this->cache) ? wp_cache_get($name,'site-options') : false;
}
function expire_feed($name = 'wpmu_sitefeed_cache') {
return ($this->check_cache()) ? wp_cache_delete($name,'site-options') : false;
}
function expire_comments_feed() {
$this->expire_feed('wpmu_sitecomments_cache');
}
function expire_pages_feed() {
return $this->expire_feed('wpmu_sitepages_cache');
}
function expire_post_feeds() {
$this->expire_feed('wpmu_siteposts_cache');
$this->expire_feed('wpmu_sitepages_cache');
}
function expire_feeds() {
$this->expire_feed(); //to expire the full-feed
$this->expire_comments_feed();
//$this->expire_pages_feed(); //will also expire posts
$this->expire_post_feeds();
}
function outputfeed($type) {
$cached = false;
if ($type == 'fullfeed') $name = 'wpmu_sitefeed_cache';
elseif ($type == 'posts') $name = 'wpmu_siteposts_cache';
elseif ($type == 'comments') $name = 'wpmu_sitecomments_cache';
elseif ($type == 'pages') $name = 'wpmu_sitepages_cache';
if ($this->cache) {
$feed = $this->fetch_feed($name);
if ($feed) {
$cached = true;
} else {
$feed = $this->generate_feed($type);
$saved = $this->save_feed($name,$feed);
}
} else {
$feed = $this->generate_feed($type);
}
if ($this->showstats) {
$feed .= "\r\n";
}
//preg_match('/ tag for the user's avatar
*/
function ada_get_avatar_url( $id_or_email, $size = '96', $default = '', $alt = false ) {
if ( ! get_option('show_avatars') )
return false;
if ( false === $alt)
$safe_alt = '';
else
$safe_alt = esc_attr( $alt );
if ( !is_numeric($size) )
$size = '96';
$email = '';
if ( is_numeric($id_or_email) ) {
$id = (int) $id_or_email;
$user = get_userdata($id);
if ( $user )
$email = $user->user_email;
} elseif ( is_object($id_or_email) ) {
if ( isset($id_or_email->comment_type) && '' != $id_or_email->comment_type && 'comment' != $id_or_email->comment_type )
return false; // No avatar for pingbacks or trackbacks
if ( !empty($id_or_email->user_id) ) {
$id = (int) $id_or_email->user_id;
$user = get_userdata($id);
if ( $user)
$email = $user->user_email;
} elseif ( !empty($id_or_email->comment_author_email) ) {
$email = $id_or_email->comment_author_email;
}
} else {
$email = $id_or_email;
}
if ( empty($default) ) {
$avatar_default = get_option('avatar_default');
if ( empty($avatar_default) )
$default = 'mystery';
else
$default = $avatar_default;
}
if ( is_ssl() )
$host = 'https://secure.gravatar.com';
else
$host = 'http://www.gravatar.com';
if ( 'mystery' == $default )
$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
elseif ( 'blank' == $default )
$default = includes_url('images/blank.gif');
elseif ( !empty($email) && 'gravatar_default' == $default )
$default = '';
elseif ( 'gravatar_default' == $default )
$default = "$host/avatar/s={$size}";
elseif ( empty($email) )
$default = "$host/avatar/?d=$default&s={$size}";
elseif ( strpos($default, 'http://') === 0 )
$default = add_query_arg( 's', $size, $default );
if ( !empty($email) ) {
$out = "$host/avatar/";
$out .= md5( strtolower( $email ) );
$out .= '?s='.$size;
$out .= '&d=' . urlencode( $default );
$rating = get_option('avatar_rating');
if ( !empty( $rating ) )
$out .= "&r={$rating}";
$avatar_url = $out;
} else {
$avatar_url = $default;
}
return $avatar_url;
}
function generate_feed($type) {
global $posts, $comments, $post, $comment;
//global $posts, $comments; //, $post, $comment; //doesn't work
if ($type == 'fullfeed') { $posts = $this->get_data($type); $feedtitlefull = $this->feedtitle . __(' Posts & Pages');}
elseif ($type == 'posts') { $posts = $this->get_data($type); $feedtitlefull = $this->feedtitle . __(' Posts');}
elseif ($type == 'comments') { $comments = $this->get_data($type); $feedtitlefull = $this->feedtitle . __(' Comments');}
elseif ($type == 'pages') { $posts = $this->get_data($type); $feedtitlefull = $this->feedtitle . __(' Pages');}
ob_start();
echo '';
if($type == 'fullfeed' || $type == 'posts' || $type == 'pages'){
?>
',$configerror); ?>