<?php
// This is for Poedit.
function mummila_all_sites_latest_poo() {
	$mummila_plugin_name=__("all sites' latest", 'mummila');
	$mummila_plugin_description=__('Fetch latest posts from all blogs on a multisite installation.', 'mummila');
}

/*
*	Text Domain: mummila
*    	Plugin URI: http://mummila.net/nuudelisoppa/all-sites-latest-for-wordpress/
* 	Author: Jani Uusitalo
* 	Author URI: http://mummila.net/
*	Plugin Name: all sites' latest
*   	Description: Fetch latest posts from all blogs (10 altogether) on a multisite installation.
* 	Version: 9001
*
*
* 
*	To the extent possible under law, Jani Uusitalo  has waived all
*	copyright and related or neighboring rights to this work. This work is
*	published from Finland.
*	http://creativecommons.org/publicdomain/zero/1.0/
*/



// Post list page title, slug or ID.
$mum_latest_page = "Viimeisimmät päivitykset";

// ID's of blogs to exclude. Comma-separated if multiple.
// Surround each with quotes.
$mum_exclude = array("9");






if ( ! function_exists( 'mum_get_blogname' ) ) :
function mum_get_blogname() {
	global $post;
	return $post->pinged;
}
endif;

if ( ! function_exists( 'mum_blogname' ) ) :
function mum_blogname() {
	echo mum_get_blogname();
	return;
}
endif;

if ( ! function_exists( 'mum_datesort' ) ) :
function mum_datesort($a, $b) {
	$postdate_a = strtotime($a->post_date);
	$postdate_b = strtotime($b->post_date);
	if ($postdate_a > $postdate_b) {
		return -1;
	}
	else {
		return 1;
	}
}
endif;

if ( ! function_exists( 'mum_all_sites_latest' ) ) :
function mum_all_sites_latest() {

	global $wpdb;
	global $wp_query;
	global $posts;
	global $mum_exclude;
	global $mum_latest_page;

	if ( !is_page($mum_latest_page) ) {
		return;
	}

	// Ditch what's given, we're overriding completely.
	$posts = array();

	$posts_per_page = get_option('posts_per_page');

	$mum_excluded_ids = "";
	foreach ($mum_exclude as $mum_excluded) {
		$mum_excluded_ids .= " AND blog_id != ".$mum_excluded;
	}
	if ( !empty($mum_excluded_ids) ) {
		$mum_excluded_ids = "WHERE".substr($mum_excluded_ids, 4);
	}
	$blogs_query = "
		SELECT blog_id
		FROM $wpdb->blogs
		$mum_excluded_ids
	";
	$blogs = $wpdb->get_results($blogs_query, OBJECT);

	$posts_associative = array();
	foreach ($blogs as $blog) {
		$blog_prefix = $wpdb->base_prefix.$blog->blog_id;
		$blog_posts = $blog_prefix . '_posts';
		$posts_select = "
			SELECT *
			FROM $blog_posts
			WHERE post_status = 'publish'
			AND post_type = 'post'
			ORDER BY post_date DESC
			LIMIT 0, $posts_per_page
		";
	
		$posts_associative = $wpdb->get_results($posts_select, OBJECT);
		$blog_details = get_blog_details($blog->blog_id);
		foreach ($posts_associative as $post) {
			$post->post_author = $blog->blog_id;
			$post->pinged = $blog_details->blogname;
		}
		$posts = array_merge($posts, $posts_associative);
	}

	usort($posts, "mum_datesort");
	$posts = array_slice($posts, 0, $posts_per_page);

	$wp_query->posts = $posts;
	$wp_query->post_count = $posts_per_page;

	return;
}
endif;
add_action('wp_head', 'mum_all_sites_latest');

if ( ! function_exists( 'mum_fix_permalink' ) ) :
function mum_fix_permalink($permalink, $post) {
	
	global $mum_latest_page;

	if ( !is_page($mum_latest_page) ) {
		return $permalink;
	}

	$permalink_structure = get_option('permalink_structure');
	$blog_base = substr( $permalink_structure, 0, strpos($permalink_structure, "%") ); 
	return str_replace(home_url().$blog_base, get_blogaddress_by_id($post->post_author), $permalink);
}
endif;
add_filter('post_link', 'mum_fix_permalink', 10, 2);
?>
