I have this tag cloud, which is currently set to show the most popular item first with the use of (line 205):
// arsort($array_end);
which reverses the array. If i leave this out it shows the list alphabetically.
I would like to have the results shown randomly. I thought of php-shuffle, but i don't know how to go about it. Appreciate if someone could help me out here, Cheers!
See php below:
//activate plugin WP function
//Checking search meter dependencies
global $wpdb, $table_prefix;
$sql = "SHOW TABLES LIKE '{$table_prefix}searchmeter_recent'";
$results = $wpdb->get_results($sql);
if (!$wpdb->num_rows )
{
die( 'This plugin will not work unless Search Meter plugin is installed and activated.
' );
}
register_activation_hook( __FILE__, 'initializeSearchTagCloud');
//activat plugin WP function
register_deactivation_hook( __FILE__, 'deactivateSearchTagCloud');
//set initial values when the plugin is activated
function initializeSearchTagCloud()
{
$search_tag_cloud=new searchTagCloud();
$search_tag_cloud->initializeSearchTagCloud();
}
//delete DB options when the plugin is activated
function deactivateSearchTagCloud() {
delete_option("searchTagCloudOption");
}
class searchTagCloud
{
public $widgetText;
public $numberSearches;
public $max_size;
public $min_size;
public $days_to_display;
var $error;
//constuctor function
function __construct()
{
$this->min_size=12;
$this->max_size=32;
$this->widgetText = 'What people is searching?';
$this->total_tags=10;
$this->show_author_credit=0;
$this->days_to_display=30;
//the size of the tag cloud is missed
}
//initialize options
//size of the smallest tag
//maximum size of the biggest tag
//Personalized text for the tag cloud
//how many links to display
function initializeSearchTagCloud()
{
global $wpdb, $table_prefix;
$wpdb->query("ALTER TABLE `{$table_prefix}searchmeter_recent` ADD COLUMN visible INT( 1 ) NOT NULL DEFAULT '1'");
$initializeOptions = array(
"min_size" => $this->min_size,
"max_size" => $this->max_size,
"total_tags" => $this->total_tags,
"widgetText" => $this->widgetText,
"days_to_display" => $this->days_to_display,
"show_author_credit" => $this->show_author_credit,
);
add_option("searchTagCloudOption", $initializeOptions, '', 'yes');
//select recent searched terms
}
//get DB options for the Search Tag Cloud
function getSearchTagCloudOptions()
{
$myOptions = get_option('searchTagCloudOption');
$this->min_size=$myOptions['min_size'];
$this->max_size=$myOptions['max_size'];
$this->widgetText=$myOptions['widgetText'];
$this->total_tags=$myOptions['total_tags'];
$this->days_to_display=$myOptions['days_to_display'];
$this->show_author_credit=$myOptions['show_author_credit'];
}
//set Search Tag Cloud class values
function setSearchTagCloudValues($min_size,$max_size,$total_tags,$widgetText,$show_author_credit,$days_to_display)
{
$this->min_size=$min_size;
$this->max_size=$max_size;
$this->widgetText=$widgetText;
$this->total_tags=$total_tags;
$this->show_author_credit=$show_author_credit;
$this->days_to_display=$days_to_display;
}
//update Search Tag Cloud class values and DB options
function updateSearchTagCloud($array_post)
{
global $wpdb, $table_prefix;
$this->setSearchTagCloudValues($array_post['min_size'],$array_post['max_size'],$array_post['total_tags'],$array_post['widgetText'],$array_post['show_author_credit'],$array_post['days_to_display']);
//set the new options in the database
update_option("searchTagCloudOption", $array_post, '', 'yes');
//I set all to visible
$wpdb->query("UPDATE `{$table_prefix}searchmeter_recent` SET visible=1");
if(is_array($array_post['checkbox_visible']))
{
foreach($array_post['checkbox_visible'] as $index=>$value)
$wpdb->query("UPDATE `{$table_prefix}searchmeter_recent` SET visible=0 WHERE terms = '{$value}'");
}
return __("Options Saved Correctly");
}
//Function to select from search meter tables in the database the most common searches.
function select_searches_for_tagcloud()
{
// List the most recent successful searches.
global $wpdb, $table_prefix;
$this->getSearchTagCloudOptions();
$count = intval($this->total_tags);
//first I need to know how many invisible tags we have
//select terms, COUNT( * ) AS total FROM `wp_searchmeter_recent` WHERE datetime>='2010-07-05' GROUP BY `terms` LIMIT 0,15
//$datebeginning = date()-dÌas
$datebeginning = date('Y-m-d', mktime(0, 0, 0, date("m"),date("d")-$this->days_to_display, date("Y")));
$tags = $wpdb->get_results(
//select recent searched terms
" SELECT terms, visible, COUNT( * ) AS total
FROM `{$table_prefix}searchmeter_recent`
WHERE datetime>='{$datebeginning}' AND
hits>0 AND
visible=1
GROUP BY `terms`
LIMIT {$count}");
return $tags;
}
function selectPopularSearchesforAdmin()
{
// List the most recent successful searches.
global $wpdb, $table_prefix;
$this->getSearchTagCloudOptions();
$count = intval($this->total_tags);
//first I need to know how many invisible tags we have
//select terms, COUNT( * ) AS total FROM `wp_searchmeter_recent` WHERE datetime>='2010-07-05' GROUP BY `terms` LIMIT 0,15
//$datebeginning = date()-dÌas
$datebeginning = date('Y-m-d', mktime(0, 0, 0, date("m"),date("d")-$this->days_to_display, date("Y")));
$invisible_tags = $wpdb->get_results(
" SELECT terms, COUNT( * ) AS total
FROM `{$table_prefix}searchmeter_recent`
WHERE visible=1 AND
`datetime`>='{$datebeginning}' AND
hits>0
GROUP BY `terms`
LIMIT {$count}");
// I have to show the tags plus the invisible ones
$count = $count + count($invisible_tags);
$tags = $wpdb->get_results(
//select recent searched terms
" SELECT terms, visible, COUNT( * ) AS total
FROM `{$table_prefix}searchmeter_recent`
WHERE datetime>='{$datebeginning}' AND
hits>0
GROUP BY `terms`
LIMIT {$count}");
return $tags;
}
//function that creates the tag cloud and prints it.
function popular_searches_tag_cloud($args)
{
$results=$this->select_searches_for_tagcloud();
if(count($results)>0)
{
foreach($results as $index)
{
$array_end[$index->terms]=$index->total;
}
// arsort($array_end);
// largest and smallest array values
$max_qty = max(array_values($array_end));
$min_qty = min(array_values($array_end));
// find the range of values
$spread = $max_qty - $min_qty;
if ($spread == 0) { // we don't want to divide by zero
$spread = 1;
}
// set the font-size increment
$step = ($this->max_size - $this->min_size) / ($spread);
//set the counter for the loop at 0
$counter=0;
// loop through the tag array
if(count($array_end)>0)
{
$html='';
$html.=''.$this->widgetText.'
';
foreach ($array_end as $key => $value)
{
if($counter<=$this->total_tags)
{
$counter++;
// calculate font-size
// find the $value in excess of $min_qty
// multiply by the font-size increment ($size)
// and add the $min_size set above
$size = round($this->min_size + (($value - $min_qty) * $step));
$html.= '' . $key . ' ';
}
else
break;
}
if($this->show_author_credit==1)
$html.='WP plugin by Marketing Online
';
$html.='';
echo $html;
}
}
}
}
/*end class--------------------------------*/
//setting the admin page
//create admin->settings page
//create Settings Section to configure plugin values
if (is_admin() ){ // admin actions
add_action('set_twitter_keyword_values','set_twitter_keyword');
add_action('admin_menu','admin_setSearchTagCloud');
add_action('admin_init','searchTagCloudSettings' );
} else {
// non-admin enqueues, actions, and filters
}
//adding the page in the admin section
function admin_setSearchTagCloud() {
add_options_page('Popular Searches Tag Cloud Options', 'Popular Searches Tag Cloud', 8,__FILE__, 'searchTagCloudOptions');
}
//register form fields
function searchTagCloudSettings() { // whitelist options
register_setting('search-tag-cloud-options', 'widgetText', 'wp_filter_nohtml_kses');
register_setting('search-tag-cloud-options', 'max_size', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'min_size', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'total_tags', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'checkbox_visible');
register_setting('search-tag-cloud-options', 'show_author_credit', 'checkValueisInt');
register_setting('search-tag-cloud-options', 'days_to_display', 'checkValueisInt');
}
function searchTagCloudOptions()
{
$html= '';
$html= '';
$html.= '';
echo $html;
}
//function to show common searches to edit in the admin page. Completes the admin form.
function getMostPopularSearchesAdmin(){
$searchcloud=new searchTagCloud();
$results=$searchcloud->selectPopularSearchesforAdmin();
if (count($results)) {
$html='';
$html.='';
$html.='Term Set not Visible ';
if ($do_include_successes) {
$html.='Results ';
}
$html.=' ';
$class= '';
$counter=0;
foreach ($results as $result) {
$html.='';
$html.=''.htmlspecialchars($result->terms).' ';
$html.='visible==0)
$html.='checked';
$html.='/>';
$html.=' ';
$html.=' ';
$class = ($class == '' ? 'alternate' : '');
$counter++;
}
$html.='';
$html.='
';
} else {
$html.='No searches recorded for this period.
';
}
return $html;
}
//This functions checks the data send by the form and calls the update the option.
function updateSearchTagCloudForm($array)
{
$message='';
$search_tag_cloud=new searchTagCloud();
//check values before inserting into DB
$message=checkNumbers($array['max_size']);
$message.=checkNumbers($array['min_size']);
$message.=checkNumbers($array['total_tags']);
$message.=checkSearchCloudWidgetText($array['widgetText']);
$message.=checkNumbers($array['days_to_display']);
if($array['show_author_credit'])
$message.=checkNumbers($array['show_author_credit']);
if($message!='')
return $message;
if($message=='')
{
$search_tag_cloud->updateSearchTagCloud($array);
}
}
//checking function for the form fields functions in the admin page
function checkNumbers($number)
{
if(!intval( $number ))
return __("The field maximum size and minimum size have to be numeric
");
elseif($number>100)
return __("Maximum size and minimum size have to be smaller than 100
");
else
return "";
}
//checking function for the form fields functions in the admin page
function checkSearchCloudWidgetText($widgetText)
{
if(strlen($widgetText)>150)
{
return __("You are not allowed to include more than 150 characters in the Widget Text
");
}
return "";
}
//Widgetizing the plugin functions
function setSearchTagCloudPlugin()
{
register_sidebar_widget(__('Popular Searches Tag Cloud'), 'callSearchTagCloud');
register_widget_control(__('Popular Searches Tag Cloud'), 'callSearchTagCloud', 200, 200 );
}
add_action("plugins_loaded", "setSearchTagCloudPlugin");
//function to initailize the class. Called from sidebar.php, it checks dependencies from the search meter plugin.
function callSearchTagCloud()
{
global $wpdb, $table_prefix;
$sql = "SHOW TABLES LIKE '{$table_prefix}searchmeter_recent'";
$results = $wpdb->get_results( $sql );
if ( ! $wpdb->num_rows )
{
die( 'This plugin will not work unless Search Meter plugin is installed and activated. -- widget
' );
}
else
{
$search_tag_cloud=new searchTagCloud();
$search_tag_cloud->initializeSearchTagCloud();
}
$searchcloud=new searchTagCloud();
$searchcloud->popular_searches_tag_cloud($tags,$args);
}
function setSearchTagCloudControl()
{
echo '';
}