Skip to content

Instantly share code, notes, and snippets.

@josesayago
Created August 18, 2014 13:48
Show Gist options
  • Save josesayago/3df20da0c89925b377bc to your computer and use it in GitHub Desktop.
Save josesayago/3df20da0c89925b377bc to your computer and use it in GitHub Desktop.
Custom Network Latest Posts main function, modified to return an array.
function network_latest_posts( $parameters ) {
// Global variables
global $wpdb;
//global $nlp_time_frame;
// Default values
$defaults = array(
'title' => NULL, // Widget title
'number_posts' => 10, // Number of posts to be displayed
'time_frame' => 0, // Time frame to look for posts in days
'title_only' => TRUE, // Display the post title only
'display_type' => 'ulist', // Display content as a: olist (ordered), ulist (unordered), block
'blog_id' => NULL, // ID(s) of the blog(s) you want to display the latest posts
'ignore_blog' => NULL, // ID(s) of the blog(s) you want to ignore
'thumbnail' => FALSE, // Display the thumbnail
'thumbnail_wh' => '80x80', // Thumbnail Width & Height in pixels
'thumbnail_class' => NULL, // Thumbnail CSS class
'thumbnail_filler' => 'placeholder', // Replacement image for posts without thumbnail (placeholder, kittens, puppies)
'thumbnail_custom' => FALSE, // Pull thumbnails from custom fields
'thumbnail_field' => NULL, // Custom field containing image url
'thumbnail_url' => NULL, // Custom thumbnail URL
'custom_post_type' => 'post', // Type of posts to display
'category' => NULL, // Category(ies) to display
'tag' => NULL, // Tag(s) to display
'paginate' => FALSE, // Paginate results
'posts_per_page' => NULL, // Number of posts per page (paginate must be activated)
'display_content' => FALSE, // Display post content (when false, excerpts will be displayed)
'excerpt_length' => NULL, // Excerpt's length
'auto_excerpt' => FALSE, // Generate excerpt from content
'excerpt_trail' => 'text', // Excerpt's trailing element: text, image
'full_meta' => FALSE, // Display full metadata
'sort_by_date' => FALSE, // Display the latest posts first regardless of the blog they come from
'sort_by_blog' => FALSE, // Sort by Blog ID
'sorting_order' => NULL, // Sort posts from Newest to Oldest or vice versa (newer / older), asc / desc for blog ID
'sorting_limit' => NULL, // Limit the number of sorted posts to display
'post_status' => 'publish', // Post status (publish, new, pending, draft, auto-draft, future, private, inherit, trash)
'css_style' => NULL, // Custom CSS _filename_ (ex: custom_style)
'wrapper_list_css' => 'nav nav-tabs nav-stacked', // Custom CSS classes for the list wrapper
'wrapper_block_css'=> 'content', // Custom CSS classes for the block wrapper
'instance' => NULL, // Instance identifier, used to uniquely differenciate each shortcode or widget used
'random' => FALSE, // Pull random posts (true or false)
'post_ignore' => NULL, // Post ID(s) to ignore
'alert_msg' => __("Sorry, I couldn't find any recent posts matching your parameters.","trans-nlp"), // Alert Message
'use_pub_date' => FALSE, // AFW Display the most recently published posts first regardless of the blog they come from
'honor_sticky' => FALSE // AFW Sort sticky posts to the top of the list, ordered by requested sort order
);
// Parse & merge parameters with the defaults
$settings = wp_parse_args( $parameters, $defaults );
// Paranoid mode activated (yes I'm a security freak)
foreach($settings as $parameter => $value) {
// Strip everything
$settings[$parameter] = strip_tags($value);
}
// Extract each parameter as its own variable
extract( $settings, EXTR_SKIP );
// If no instance was set, make one
if( empty($instance) ) { $instance = 'default-'.rand(); }
// HTML Tags
$html_tags = nlp_display_type($display_type, $instance, $wrapper_list_css, $wrapper_block_css);
// If Custom CSS
if( !empty($css_style) ) {
// If RTL
if( is_rtl() ) {
// Tell WordPress this plugin is switching to RTL mode
/* Set the text direction to RTL
* This two variables will tell load-styles.php
* load the Dashboard in RTL instead of LTR mode
*/
global $wp_locale, $wp_styles;
$wp_locale->text_direction = 'rtl';
$wp_styles->text_direction = 'rtl';
}
// File path
$cssfile = get_stylesheet_directory_uri().'/'.$css_style.'.css';
// Load styles
nlp_load_styles($cssfile);
}
// Display blog or blogs
// if the user passes one value
if( !preg_match("/,/",$blog_id) ) {
// Always clean this stuff ;) (oh.. told you I'm a paranoid)
$blog_id = (int)htmlspecialchars($blog_id);
// Check if it's numeric
if( is_numeric($blog_id) ) {
// and put the sql
$display = " AND blog_id = $blog_id ";
}
// if the user passes more than one value separated by commas
} else {
// create an array
$display_arr = explode(",",$blog_id);
// and repeat the sql for each ID found
for( $counter=0; $counter < count($display_arr); $counter++){
// Add AND the first time
if( $counter == 0 ) {
$display .= " AND blog_id = ".(int)$display_arr[$counter];
// Add OR the rest of the time
} else {
$display .= " OR blog_id = ".(int)$display_arr[$counter];
}
}
}
// Ignore blog or blogs
// if the user passes one value
if( !preg_match("/,/",$ignore_blog) ) {
// Always clean this stuff ;)
$ignore_blog = (int)htmlspecialchars($ignore_blog);
// Check if it's numeric
if( is_numeric($ignore_blog) ) {
// and put the sql
$ignore = " AND blog_id != $ignore_blog ";
}
// if the user passes more than one value separated by commas
} else {
// create an array
$ignore_arr = explode(",",$ignore_blog);
// and repeat the sql for each ID found
for( $counter=0; $counter < count($ignore_arr); $counter++){
$ignore .= " AND blog_id != ".(int)$ignore_arr[$counter];
}
}
// If multiple tags found, set an array
if( preg_match("/,/",$tag) ) {
$tag = explode(",",$tag);
} else {
if( !empty($tag) ) {
$tag = str_split($tag,strlen($tag));
}
}
// If multiple categories found, set an array
if( preg_match("/,/",$category) ) {
$category = explode(",",$category);
} else {
if( !empty($category) ) {
$category = str_split($category,strlen($category));
}
}
// If multiple post type found, set an array
if( preg_match("/,/",$custom_post_type) ) {
$custom_post_type = explode(",",$custom_post_type);
} else {
if( !empty($category) ) {
$custom_post_type = str_split($custom_post_type,strlen($custom_post_type));
}
}
// Paranoid ;)
$time_frame = (int)$time_frame;
// Get the list of blogs in order of most recent update, get only public and nonarchived/spam/mature/deleted
if( $time_frame > 0 ) {
// By blog ID except those ignored
if( !empty($blog_id) && $blog_id != NULL ) {
$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' $display
$ignore AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $time_frame DAY)
ORDER BY last_updated DESC");
// Everything but ignored blogs
} else {
$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'
$ignore AND last_updated >= DATE_SUB(CURRENT_DATE(), INTERVAL $time_frame DAY)
ORDER BY last_updated DESC");
}
// Everything written so far
} else {
// By blog ID except those ignored
if( !empty($blog_id) && $blog_id != NULL ) {
$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0' $display
$ignore ORDER BY last_updated DESC");
// Everything but ignored blogs
} else {
$blogs = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs WHERE
public = '1' AND archived = '0' AND mature = '0' AND spam = '0' AND deleted = '0'
$ignore ORDER BY last_updated DESC");
}
}
// Ignore one or many posts
// if the user passes one value
if( !preg_match("/,/",$post_ignore) ) {
// Always clean this stuff ;) (oh.. told you I'm a paranoid)
$post_ignore = array( 0 => (int)htmlspecialchars($post_ignore) );
// if the user passes more than one value separated by commas
} else {
// create an array
$post_ignore = explode(",",$post_ignore);
}
// If it found something
if( $blogs ) {
// Count blogs found
$count_blogs = count($blogs);
// Dig into each blog
foreach( $blogs as $blog_key ) {
// Options: Site URL, Blog Name, Date Format
${'blog_url_'.$blog_key} = get_blog_option($blog_key,'siteurl');
${'blog_name_'.$blog_key} = get_blog_option($blog_key,'blogname');
${'date_format_'.$blog_key} = get_blog_option($blog_key,'date_format');
// Orderby
if( $random == 'true' ) { $orderby = 'rand'; } else { $orderby = 'post_date'; }
// Categories or Tags
if( !empty($category) && !empty($tag) ) {
$args = array(
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag
)
),
'numberposts' => $number_posts,
'post_status' => $post_status,
'post_type' => $custom_post_type,
'orderby' => $orderby
);
}
// Categories only
if( !empty($category) && empty($tag) ) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $category
)
),
'numberposts' => $number_posts,
'post_status' => $post_status,
'post_type' => $custom_post_type,
'orderby' => $orderby
);
}
// Tags only
if( !empty($tag) && empty($category) ) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => $tag
)
),
'numberposts' => $number_posts,
'post_status' => $post_status,
'post_type' => $custom_post_type,
'orderby' => $orderby
);
}
// Everything by Default
if( empty($category) && empty($tag) ) {
// By default
$args = array(
'numberposts' => $number_posts,
'post_status' => $post_status,
'post_type' => $custom_post_type,
'orderby' => $orderby
//'post__in' => get_option('sticky_posts')
);
}
// Switch to the blog
switch_to_blog($blog_key);
// Get posts
${'posts_'.$blog_key} = get_posts($args);
// Check if posts with the defined criteria were found
if( empty(${'posts_'.$blog_key}) ) {
/* If no posts matching the criteria were found then
* move to the next blog
*/
next($blogs);
}
$blog_sort_key = str_pad($blog_key, 6, '0', STR_PAD_LEFT);
if( $honor_sticky == 'true' ) {
switch ($sorting_order) {
case "newer":
$sticky = '1';
$unsticky = '0';
break;
case "desc":
$sticky = '1';
$unsticky = '0';
break;
default:
$sticky = '0';
$unsticky = '1';
break;
}
} else {
$sticky = '';
$unsticky = '';
}
// Put everything inside an array for sorting purposes
foreach( ${'posts_'.$blog_key} as $post ) {
// Access all post data
setup_postdata($post);
$sticky_key = ( is_sticky( $post->ID ) ) ? $sticky : $unsticky; //AFW
// AFW
if( $use_pub_date == 'true' ) {
$date_key = $post->post_date;
} else {
$date_key = $post->post_modified;
}
// Sort by blog ID
if( $sort_by_blog == 'true' ) {
// Ignore Posts
if( !in_array( $post->ID, $post_ignore ) ) {
// Put inside another array and use blog ID as keys
$all_posts[$sticky_key.$blog_sort_key.$date_key.$post->ID] = $post;
}
} else {
// Ignore Posts
if( !in_array( $post->ID, $post_ignore ) ) {
// Put everything inside another array using the modified date as
// the array keys
$all_posts[$sticky_key.$date_key.$blog_sort_key.$post->ID] = $post;
}
}
// The guid is the only value which can differenciate a post from
// others in the whole network
$all_permalinks[$post->guid] = get_blog_permalink($blog_key, $post->ID);
$all_blogkeys[$post->guid] = $blog_key;
}
// Back the current blog
restore_current_blog();
}
// If no content was found
if( empty($all_posts) ) {
// Nothing to do here, let people know and get out of here
echo "<div class='alert'><p>".$alert_msg."</p></div>";
return;
}
// Sort if Sticky
if( $honor_sticky == 'true' ) {
switch ( $sorting_order ) {
case "newer":
case "desc":
@krsort($all_posts);
break;
default:
@ksort($all_posts);
break;
}
}
// Sort by date (regardless blog IDs)
if( $sort_by_date == 'true' ) {
// Sorting order (newer / older)
if( !empty($sorting_order) ) {
switch( $sorting_order ) {
// From newest to oldest
case "newer":
// Sort the array
@krsort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
break;
// From oldest to newest
case "older":
// Sort the array
@ksort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
break;
// Newest to oldest by default
default:
// Sort the array
@krsort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
break;
}
} else {
// Sort the array
@krsort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
}
}
// Sort by blog ID
if( $sort_by_blog == 'true' ) {
// Sorting order (newer / older)
if( !empty($sorting_order) ) {
switch( $sorting_order ) {
// Ascendant
case "asc":
// Sort the array
@ksort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
break;
// Descendant
case "desc":
// Sort the array
@krsort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
break;
// Newest to oldest by default
default:
// Sort the array
@krsort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
break;
}
} else {
// Sort the array
@ksort($all_posts);
// Limit the number of posts
if( !empty($sorting_limit) ) {
$all_posts = @array_slice($all_posts,0,$sorting_limit,true);
}
}
}
}
// Return array of posts
return $all_posts;
// Reset post data
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment