Hi Tabish,
If this works out well Ill mod all the queries in a similar fashion. Heres what I have come up with. I had to modify the query since we need to get the category and its sub-categories by traversing through.
function wp_dlm_parse_downloads_cats($data) {
if (substr_count($data,"[download_cat#")) {
global $wpdb,$wp_dlm_root,$allowed_extentions,$max_upload_size,$wp_dlm_db,$wp_dlm_db_cats;
$patts = array();
$subs = array();
$url = get_option('wp_dlm_url');
$downloadurl = get_bloginfo('wpurl').'/'.$url;
if (empty($url)) $downloadurl = $wp_dlm_root.'download.php?id=';
$downloadtype = get_option('wp_dlm_type');
// Get cats and dig for sub cats
function get_sub_cats($the_cats) {
global $wpdb, $wp_dlm_db_cats;
$a=sizeof($the_cats);
$query = sprintf("SELECT id from %s WHERE parent IN (%s);",
$wpdb->escape( $wp_dlm_db_cats ),
$wpdb->escape( implode(",",$the_cats) ));
$res = $wpdb->get_results($query);
if ($res) {
foreach($res as $r) {
if (!in_array($r->id, $the_cats)) $the_cats[]=$r->id;
}
}
$b=sizeof($the_cats);
if ($a!=$b) {
$sub_cats = get_sub_cats($the_cats);
$the_cats = $the_cats + $sub_cats;
}
return $the_cats;
}
preg_match_all("|\[download_cat#(.*)\]|U",$data,$result,PREG_SET_ORDER);
foreach ($result as $val) {
// Traverse through categories to get sub-cats
$the_cats = array();
$the_cats[] = $val[1];
$the_cats = get_sub_cats($the_cats);
// Get downloads for category and sub-categories
$query ="SELECT * FROM $wp_dlm_db WHERE category_id IN (".implode(',',$the_cats).") ORDER BY 'title'";
$downloads = $wpdb->get_results($query);
// GENERATE LIST
$links = '<ul>';
if (!empty($downloads)) {
foreach($downloads as $d) {
switch ($downloadtype) {
case ("Title") :
$downloadlink = urlencode($d->title);
break;
case ("Filename") :
$downloadlink = $d->filename;
$link = explode("/",$downloadlink);
$downloadlink = end($link);
break;
default :
$downloadlink = $d->id;
break;
}
if (!empty($d->dlversion))
$links.= '<li><a href="'.$downloadurl.$downloadlink.'" title="'.__("Version","wp-download_monitor").' '.$d->dlversion.' '.__("downloaded","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.$d->hits.')</a></li>';
else $links.= '<li><a href="'.$downloadurl.$downloadlink.'" title="'.__("Downloaded","wp-download_monitor").' '.$d->hits.' '.__("times","wp-download_monitor").'" >'.$d->title.' ('.$d->hits.')</a></li>';
}
} else {
$links .= '<li>No Downloads Found</li>';
}
$links .= '</ul>';
$patts[] = "[download_cat#" . $val[1] . "]";
$subs[] = $links;
}
$data = str_replace($patts, $subs, $data);
}
return $data;
}