';
$line_start = '';
$line_end = "
\n";
$list_end = "
\n";
break;
case 'p':
$list_start = '';
$line_start = '';
$line_end = "
\n";
$list_end = '';
break;
case 'ul':
default:
$list_start = "\n";
$line_start = "- ";
$line_end = "
\n";
$list_end = "
\n";
}
$finalString .= $list_start;
for ( $x=0; $x'.$rand_articles[$x]['title'].'' . $line_end;
}
$finalString .= $list_end;
return $finalString;
}
function ara_get_random_posts( $numPosts = '5', $category = '' ) {
global $wpdb, $wp_db_version;
if( '' == $category ):
$sql = "SELECT $wpdb->posts.ID FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post'";
else:
if($wp_db_version >= 6124): // Database structure has changed since WP 2.3
$sql = "SELECT $wpdb->posts.ID ";
$sql.= "FROM $wpdb->posts, $wpdb->term_relationships, $wpdb->term_taxonomy ";
$sql.= "WHERE $wpdb->posts.post_status = 'publish' ";
$sql.= "AND $wpdb->posts.post_type = 'post' ";
$sql.= 'AND ';
$sql.= '( ';
$sql.= "$wpdb->posts.ID = $wpdb->term_relationships.object_id ";
$sql.= "AND $wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id ";
$sql.= "AND $wpdb->term_taxonomy.term_id = $category ";
$sql.= ')';
else:
$sql = "SELECT $wpdb->posts.ID ";
$sql.= "FROM $wpdb->posts, $wpdb->post2cat ";
$sql.= "WHERE $wpdb->posts.post_status = 'publish' ";
$sql.= "AND $wpdb->posts.post_type = 'post'";
$sql.= "AND $wpdb->post2cat.post_id = $wpdb->posts.ID ";
$sql.= "AND $wpdb->post2cat.category_id = $category";
endif;
endif;
$the_ids = $wpdb->get_results( $sql );
$luckyPosts = (array) array_rand( $the_ids, ( $numPosts > count( $the_ids ) ? count( $the_ids ) : $numPosts ) );
$sql = "SELECT $wpdb->posts.post_title, $wpdb->posts.ID";
$sql .= " FROM $wpdb->posts";
$sql .= ' WHERE';
# Here we minimize number of query to the database by using ORs - just one query needed
foreach ( $luckyPosts as $id )
{
if( true == $notfirst ) $sql .= ' OR';
else $sql .= ' (';
$sql .= " $wpdb->posts.ID = ".$the_ids[$id]->ID;
$notfirst = true;
}
$sql .= ')';
$rand_articles = $wpdb->get_results( $sql );
# Give it a shuffle just to spice it up
shuffle( $rand_articles );
if ( $rand_articles ) {
foreach ( $rand_articles as $item ) {
$posts_results[] = array( 'title' => str_replace( '"', '', stripslashes ( $item->post_title ) ), 'permalink' => post_permalink( $item->ID ) );
}
return $posts_results;
}
else {
return false;
}
}
if( $wp_db_version < 11548 ) # WordPress older than 2.8 doesn't have WP_Widget
{
function widget_ara_randomposts_control() {
$options = $newoptions = get_option( 'widget_ara_randomposts' );
if ( $_POST['randomposts-submit'] ) {
$newoptions['title'] = strip_tags( stripslashes( $_POST['randomposts-title'] ) );
$newoptions['type'] = $_POST['randomposts-type'];
$newoptions['count'] = (int) $_POST['randomposts-count'];
$newoptions['cat'] = $_POST['randomposts-category'];
}
if ( $options != $newoptions ) {
$options = $newoptions;
update_option( 'widget_ara_randomposts', $options );
}
# Get categories from the database
$all_categories = get_categories();
?>
'wp_ara_widget_random_posts', 'description' => 'Display Random Posts Widget.' );
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'wp_ara_widget_random_posts' );
$this->WP_Widget( 'wp_ara_widget_random_posts', 'Random Posts Widget', $widget_ops, $control_ops );
}
public function widget( $args, $instance ) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo ara_random_posts( $instance );
echo $after_widget;
}
public function form( $instance ) {
$defaults = array( 'title' => 'Random Posts', 'type' => 'ul' );
$instance = wp_parse_args( (array) $instance, $defaults );
?>