So, I have this shortcode that produces a list of all published multisite and their pages. The network in question has about 50 sites in it. The problem is that loading the page I put the shortcode on takes an extra couple of seconds to load. So performance blows. Wondering what I can do to optimize this. Thoughts?
<?php
class Multisite_Shortcode{
/** Multisite parameters
* network_id' = $wpdb->siteid,
* site_order' = 'blog_id ', // blog_id, site_id, domain, path, registered, last_updated, public, archived, mature, spam, deleted, lang_id
* site_sort' = 'ASC', // ASC or DESC
* public' = null,
* archived' = null,
* mature' = null,
* spam' = null,
* deleted' = null,
* limit' = 100,
* offset' = 0,
** pages parameters
* depth' = 0,
* child_of' = 0,
* exclude' = '',
* sort_column' = 'menu_order, post_title',
* sort_order' = 'ASC', // ASC or DESC
* link_before' = '',
* link_after' = '',
*/
/**
* Shortcode initialization
* @return void
*/
function __construct() {
add_shortcode( 'blog_pages', array(&$this, 'shortcode_blog_pages') );
}
function shortcode_blog_pages( $atts ) {
global $wpdb;
$args = shortcode_atts( array(
// blog parameters
'network_id' => $wpdb->siteid,
'site_order' => 'blog_id ', // blog_id, site_id, domain, path, registered, last_updated, public, archived, mature, spam, deleted, lang_id
'site_sort' => 'ASC',
'public' => null,
'archived' => null,
'mature' => null,
'spam' => null,
'deleted' => null,
'limit' => 100,
'offset' => 0,
// pages parameters
'depth' => 0,
'child_of' => 0,
'exclude' => '',
'sort_column' => 'menu_order, post_title',
'sort_order' => 'ASC',
'link_before' => '',
'link_after' => '',
), $atts );
$html = '';
if ( wp_is_large_network() )
return $html;
$query = "SELECT * FROM $wpdb->blogs WHERE 1=1 ";
if ( isset( $args['network_id'] ) && ( is_array( $args['network_id'] ) || is_numeric( $args['network_id'] ) ) ) {
$network_ids = implode( ',', wp_parse_id_list( $args['network_id'] ) );
$query .= "AND site_id IN ($network_ids) ";
}
if ( isset( $args['public'] ) )
$query .= $wpdb->prepare( "AND public = %d ", $args['public'] );
if ( isset( $args['archived'] ) )
$query .= $wpdb->prepare( "AND archived = %d ", $args['archived'] );
if ( isset( $args['mature'] ) )
$query .= $wpdb->prepare( "AND mature = %d ", $args['mature'] );
if ( isset( $args['spam'] ) )
$query .= $wpdb->prepare( "AND spam = %d ", $args['spam'] );
if ( isset( $args['deleted'] ) )
$query .= $wpdb->prepare( "AND deleted = %d ", $args['deleted'] );
$query .= " ORDER BY {$args['site_order']} {$args['site_sort']} ";
if ( isset( $args['limit'] ) && $args['limit'] ) {
if ( isset( $args['offset'] ) && $args['offset'] )
$query .= $wpdb->prepare( "LIMIT %d , %d ", $args['offset'], $args['limit'] );
else
$query .= $wpdb->prepare( "LIMIT %d ", $args['limit'] );
}
$site_results = $wpdb->get_results( $query, ARRAY_A );
$html = '';
// Add other parameter to the list pages
$args['echo'] = false;
$args['title_li'] = '';
$args['walker'] = '';
foreach( $site_results as $blog ) {
switch_to_blog( $blog['blog_id'] );
$list_pages = @wp_list_pages( $args );
restore_current_blog();
$details = get_blog_details( array( 'blog_id' => $blog['blog_id'] ) );
$title = "<a href='{$details->siteurl}'>{$details->blogname}</a>";
$html .= "<div id='site-pages-{$details->blog_id}' class='site-pages'>
<h2>$title</h2>
<ul>$list_pages</ul>
</div>";
}
return $html;
}
}
new Multisite_Shortcode();
?>