'parent', 'id' => 'term_id'); //TODO: decouple this
function start_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "
";
}
function end_lvl(&$output, $depth, $args) {
$indent = str_repeat("\t", $depth);
$output .= "
";
}
function start_el(&$output, $category, $depth, $args) {
extract($args);
$class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
$output .= "" . '';
}
function end_el(&$output, $category, $depth, $args) {
$output .= "";
}
}
}
// Widget Initialization function
function WP_Widget_DACD_widget_init()
{
new WP_Widget_DACD_Widget();
}
if (!class_exists('WP_Widget_DACD_Widget') AND class_exists('WP_Widget')) {
class WP_Widget_DACD_Widget extends WP_Widget {
function WP_Widget_DACD_Widget() {
$widget_ops = array ('classname' => 'DACD_Widget', 'description' => __('The description for your Widget') );
//$widget_ops = array('description' => __('Ajax Category Dropdown', 'DACD_widget'));
$this->WP_Widget('DACD_widget', __('Dadc_widget'), $widget_ops);
//$control_ops = array ('width' => '300', 'height' => '400');
//$widget_ops = array ('classname' => 'DACD_Widget', 'description' => __('The description for your Widget') );
//$this->WP_Widget('DACD_Widget', __('Ajax Category Widget'), $widget_ops, $control_ops);
}
function widget($args, $instance) {
extract($args, EXTR_SKIP);
$myDACD = new dhat_ajax_cat_dropdown();
$myDACD->widget_dacd_Category_Dropdown($args, $instance);
/**
echo $before_widget;
$title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);
$entry_title = empty($instance['entry_title']) ? ' ' : apply_filters('widget_entry_title', $instance['entry_title']);
$comments_title = empty($instance['comments_title']) ? ' ' : apply_filters('widget_comments_title', $instance['comments_title']);
if ( !empty( $title ) ) { echo $before_title . $title . $after_title; };
echo '';
echo $after_widget;
**/
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['entry_title'] = strip_tags($new_instance['entry_title']);
$instance['comments_title'] = strip_tags($new_instance['comments_title']);
return $instance;
}
function form($instance) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'entry_title' => '', 'comments_title' => '' ) );
$title = strip_tags($instance['title']);
$entry_title = strip_tags($instance['entry_title']);
$comments_title = strip_tags($instance['comments_title']);
?>
__construct();
}
/**
* PHP 5 Constructor
*/
function __construct(){
add_action("admin_menu", array(&$this,"add_admin_pages"));
add_action("plugins_loaded",array(&$this,"register_widget_dacd_Category_Dropdown"));
//add_action("widgets_init", array(&$this,"dacd_widgets_init"));
//add_action("wp_head", array(&$this,"add_head_scripts"));
// add_action('dbx_post_advanced', array(&$this, "edit_form_advanced"));
//add_action('admin_head', array(&$this, 'add_admin_css'));
add_action('wp_print_scripts', array(&$this,"add_head_scripts"));
$this->adminOptions = $this->getAdminOptions();
$this->catLevels = $this->totalLevels();
}
/**
* Retrieves the options from the database.
* @return array
*/
function getAdminOptions() {
$adminOptions = array("postCategory" => "deep");
$savedOptions = get_option($this->adminOptionsName);
if (!empty($savedOptions)) {
foreach ($savedOptions as $key => $option) {
$adminOptions[$key] = $option;
}
}
update_option($this->adminOptionsName, $adminOptions);
return $adminOptions;
}
/**
* Checks if $parent is one $id's parent categories, checks back all levels.
*/
function isParentCategory($id, $parent) {
global $wpdb;
if ($id == $parent) {
return true;
}
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$id AND taxonomy='category'";
$cat = $wpdb->get_row($sql);
if ($cat->parent > 0) {
$sub = $cat->parent;
if ($sub == $parent) {
return true;
}
else {
while ($sub > 0){
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$sub and taxonomy='category'";
$sub = $wpdb->get_row($sql);
$sub = $sub->parent;
if ($sub == $parent) {
return true;
}
}
}
return false;
}
else {
if ($id == $parent) {
return true;
}
else {
return false;
}
}
}
/**
* Checks $id's has sub categories, checks back all levels.
*/
function hasChildCategories($id) {
global $wpdb;
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE parent=$id AND taxonomy='category'";
$cat = $wpdb->get_results($sql);
if ($cat) {
return true;
}
else {
return false;
}
}
/**
* Returns the depth adjusted parent(id) of category(id)
* NOT COMPLETE function abandoned....
*/
function categoryParentByDepth($id, $depth) {
global $wpdb;
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$id AND taxonomy='category'";
$cat = get_row($sql);
if ($cat->parent > 0) {
$sub = $cat->parent;
$sub_parent = 1; //starting value above 0
$i = 1;
$cat_depth = 1;
while ($sub_parent > 0){
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$sub and taxonomy='category'";
$sub_parent = $wpdb->get_row($sql);
$sub_parent = $sub_parent->parent;
//echo "Sub parent found: $sub_parent
";
if ($sub_parent > 0) {
$i++;
$sub = $sub_parent;
if ($i > $cat_depth) {
$cat_depth = $i;
}
}
}
return $cat_depth;
}
else { // category is the main parent
if ($depth == 0) {
return $id;
}
else {
return false; // this is the top category, so no depth other than 0 found.
}
}
}
/**
* Calculates the nested depth of a category by ID
*/
function categoryDepth($id) {
global $wpdb;
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$id AND taxonomy='category'";
$cat = $wpdb->get_row($sql);
if ($cat->parent > 0) {
$sub = $cat->parent;
$sub_parent = 1; //starting value above 0
$i = 1;
$cat_depth = 1;
while ($sub_parent > 0){
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$sub and taxonomy='category'";
$sub_parent = $wpdb->get_row($sql);
$sub_parent = $sub_parent->parent;
//echo "Sub parent found: $sub_parent
";
if ($sub_parent > 0) {
$i++;
$sub = $sub_parent;
if ($i > $cat_depth) {
$cat_depth = $i;
}
}
}
return $cat_depth;
}
else {
return 0;
}
}
/**
* Calculates if the current category has posts either within it's self, or it children
*/
function categoryHasPosts($id, $countwhat = 'subposts') {
global $wpdb;
if ($countwhat == 'subposts') {
$posts = new WP_Query("cat=$id&showposts=-1");
$count = $posts->post_count;
if ($count > 0) {
return $count;
}
else {
return false;
}
}
elseif ($countwhat == 'parentposts') {
$sql = "SELECT COUNT(*) FROM $wpdb->term_relationships LEFT JOIN
$wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
WHERE
$wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.term_id = $id
GROUP BY $wpdb->term_taxonomy.term_id";
echo $sql;
$count = $wpdb->get_var($sql);
//$count = $posts->post_count;
if ($count > 0) {
echo "FOUND $count";
return $count;
}
else {
echo "COULDNTFOUND $count";
$posts = new WP_Query("cat=$id&showposts=-1");
$count = $posts->post_count;
if ($count > 0) {
return $count;
}
else {
return false;
}
}
}
elseif ($countwhat == 'subcats') {
$posts = new WP_Query("cat=$id&showposts=-1");
$count = $posts->post_count;
if ($count > 0) {
$sql = "SELECT COUNT(*) FROM $wpdb->term_taxonomy
WHERE
$wpdb->term_taxonomy.taxonomy = 'category'
AND $wpdb->term_taxonomy.parent = $id
GROUP BY $wpdb->term_taxonomy.parent";
$count = $wpdb->get_var($sql);
if ($count == false) {
return '0';
}
return $count;
}
else {
return false;
}
}
else {
return false;
}
}
/**
* Returns the current category id;
*/
function currentCategory(){
global $wpdb;
global $post;
if (is_category()) {
return get_query_var('cat');
}
elseif (is_single()) {
global $post;
$dhat_ajax_topcat_query = "
SELECT $wpdb->term_taxonomy.term_taxonomy_id, $wpdb->term_taxonomy.term_id, $wpdb->term_taxonomy.parent FROM $wpdb->term_relationships
LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id )
WHERE $wpdb->term_relationships.object_id = '$post->ID'
AND $wpdb->term_taxonomy.taxonomy = 'category'
ORDER BY $wpdb->term_relationships.term_taxonomy_id DESC";
$dhat_post_categories = $wpdb->get_results($dhat_ajax_topcat_query);
if ($this->adminOptions['postCategory'] == 'deep') {
$deepest['id'] = 0;
$deepest['level'] = 0;
foreach ($dhat_post_categories as $cat) {
$cat_depth = $this->categoryDepth($cat->term_id);
if ($deepest['level'] < $cat_depth) {
$deepest['id'] = $cat->term_id;
$deepest['level'] = $cat_depth;
}
}
return $deepest['id'];
}
elseif ($this->adminOptions['postCategory'] == 'lowest') {
$lowest = 2000000;
foreach ($dhat_post_categories as $cat) {
if ($cat->term_taxonomy_id < $lowest) {
$lowest = $cat->term_taxonomy_id;
}
}
return $lowest;
}
else {
$highest = 0;
foreach ($dhat_post_categories as $cat) {
if ($cat->term_taxonomy_id > $highest) {
$highest = $cat->term_taxonomy_id;
}
}
return $highest;
}
}
}
/**
* Returns the total number of category levels;
*/
function totalLevels(){
global $wpdb;
$levels = 0;
$sql = "SELECT parent FROM $wpdb->term_taxonomy WHERE taxonomy='category' AND parent <> 0 GROUP BY parent";
$subs = $wpdb->get_results($sql);
//echo "Searched: $sql
";
foreach ($subs as $sub) { // cycle through
$sub = $sub->parent;
$sub_parent = 1; //initial value
//echo "Checking sub: $sub
";
$i = 1;
if ($i > $levels) {
$levels = $i;
}
while ($sub_parent > 0){
$sql = "SELECT * FROM $wpdb->term_taxonomy WHERE term_id=$sub and taxonomy='category'";
//echo "Sql: $sql
";
$sub_parent = $wpdb->get_row($sql);
$sub_parent = $sub_parent->parent;
//echo "Sub parent found: $sub_parent
";
if ($sub_parent > 0) {
$i++;
$sub = $sub_parent;
if ($i > $levels) {
$levels = $i;
}
}
}
}
//echo "Found $levels levels of categories";
return $levels;
}
/**
* Saves the admin options to the database.
*/
function saveAdminOptions(){
update_option($this->adminOptionsName, $this->adminOptions);
$this->adminOptions = $this->getAdminOptions();
}
function add_admin_pages(){
//add_submenu_page('options-general.php', "Ajax Category Dropdown", " Ajax Category Dropdown", 10, "Ajax Category Dropdown", array(&$this,"output_sub_admin_page_0"));
}
/**
* Outputs the HTML for the admin sub page.
*/
function output_sub_admin_page_0(){
?>
Admin Menu Placeholder for Category Dropdown a subpage of 'options-general.php'
You can modify the content that is output to this page by modifying the method output_sub_admin_page_0
Levels found:totalLevels(); ?>
'widget_dacd', 'description' => __('Ajax Category dropdown boxes widget.'));
$control_ops = array('width' => 460, 'height' => 350, 'id_base' => 'dacd');
$name = __('Ajax Category Dropdown');
$id = false;
foreach ( array_keys($options) as $o ) {
// Old widgets can have null values for some reason
if ( !isset($options[$o]['title']) || !isset($options[$o]['text']) )
continue;
$id = "dacd-$o"; // Never never never translate an id
wp_register_sidebar_widget($id, $name, array(&$this, 'widget_dacd'), $widget_ops, array( 'number' => $o ));
wp_register_widget_control($id, $name, array(&$this, 'widget_dacd_control'), $control_ops, array( 'number' => $o ));
}
// If there are none, we register the widget's existance with a generic template
if ( !$id ) {
wp_register_sidebar_widget( 'dacd-1', $name, array(&$this, 'widget_dacd'), $widget_ops, array( 'number' => -1 ) );
wp_register_widget_control( 'dacd-1', $name, array(&$this, 'widget_dacd_control'), $control_ops, array( 'number' => -1 ) );
}
//register_sidebar_widget("Ajax Category Dropdown",array(&$this,"widget_dacd_Category_Dropdown"));
}
/**
* Widget control function, for settings.
*/
function widget_dacd_control($widget_args) {
global $wp_registered_widgets;
static $updated = false;
if ( is_numeric($widget_args) )
$widget_args = array( 'number' => $widget_args );
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
extract( $widget_args, EXTR_SKIP );
$options = get_option(DACD_WIDGET_OPTION);
if ( !is_array($options) )
$options = array();
if ( !$updated && !empty($_POST['sidebar']) ) {
$sidebar = (string) $_POST['sidebar'];
$sidebars_widgets = wp_get_sidebars_widgets();
if ( isset($sidebars_widgets[$sidebar]) ) {
$this_sidebar =& $sidebars_widgets[$sidebar];
}
else {
$this_sidebar = array();
}
foreach ( $this_sidebar as $_widget_id ) {
if ( 'widget_dacd' == $wp_registered_widgets[$_widget_id]['callback'] && isset($wp_registered_widgets[$_widget_id]['params'][0]['number']) ) {
$widget_number = $wp_registered_widgets[$_widget_id]['params'][0]['number'];
unset($options[$widget_number]);
}
}
foreach ( (array) $_POST['widget-dacd'] as $widget_number => $widget_text ) {
$title = strip_tags(stripslashes($widget_text['title']));
if ( current_user_can('unfiltered_html') )
$text = stripslashes( $widget_text['text'] );
else
$text = stripslashes(wp_filter_post_kses( $widget_text['text'] ));
$countshow = $widget_text['countshow'];
$countwhat = $widget_text['countwhat'];
$direction = $widget_text['direction'];
$sortby = $widget_text['sortby'];
$emptyshow = $widget_text['emptyshow'];
$wrapformwidth = $widget_text['wrapformwidth'];
$categorywrapwidth= $widget_text['categorywrapwidth'];
$widget_vars = array('title', 'text');
$widget_vars[] = 'emptyshow';
$widget_vars[] = 'countshow';
$widget_vars[] = 'countwhat';
$widget_vars[] = 'direction';
$widget_vars[] = 'sortby';
$widget_vars[] = 'categorywrapwidth';
$widget_vars[] = 'wrapformwidth';
for($i=0;$i<=$this->totalLevels();$i++){
//set variable names eg: $level1
$a[$i] = "level".$i;
${$a[$i]} = strip_tags(stripslashes($widget_text["level$i"]));
$widget_vars[] = "level".$i;
}
$options[$widget_number] = compact( $widget_vars );
}
update_option(DACD_WIDGET_OPTION, $options);
$updated = true;
}
if ( -1 == $number ) {
$title = '';
$text = '';
$number = '%i%';
for($i=0;$i<=$this->totalLevels();$i++){
//set variable names eg: $level1
$a[$i] = "level".$i;
${$a[$i]} = "Level " . $i;
}
$countshow = 1;
$countwhat = "subposts";
$direction = "vertical";
$sortby = "titleasc";
$wrapformwidth = "100%";
$categorywrapwidth= "100%";
$emptyshow = 0;
} else {
$title = attribute_escape($options[$number]['title']);
$text = format_to_edit($options[$number]['text']);
$emptyshow = attribute_escape($options[$number]["emptyshow"]);
$countshow = attribute_escape($options[$number]["countshow"]);
$countwhat = attribute_escape($options[$number]["countwhat"]);
$direction = attribute_escape($options[$number]["direction"]);
$sortby = attribute_escape($options[$number]["sortby"]);
$wrapformwidth = attribute_escape($options[$number]["wrapformwidth"]);
$categorywrapwidth= attribute_escape($options[$number]["categorywrapwidth"]);
for($i=0;$i<=$this->totalLevels();$i++){
//set variable names eg: $level1
$level[$i] = attribute_escape($options[$number]["level$i"]);
}
}
?>
Text to display before the dropdown boxes eg, instructions etc.
totalLevels();$i++){
?>
" name="widget-dacd[][level]" type="text" value="" />
/>
/>
Choose what you want counted.
Choose the order you want the categories displayed in.
Display dropdowns as a vertical list (ie: one above the other) or as a horizontal list (ie: next to each other).
/>Vertical
/>Horizontal
';
}
/**
* Allows user to echo widget from theme
*/
function place_widget_dacd() {
}
/**
* Contains the widget logic
*/
function widget_dacd($args, $widget_args = 1) {
global $wpdb;
extract( $args, EXTR_SKIP );
if ( is_numeric($widget_args) ) {
$widget_args = array( 'number' => $widget_args );
}
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
extract( $widget_args, EXTR_SKIP );
$options = get_option(DACD_WIDGET_OPTION);
if ( !isset($options[$number]) )
return;
$title = $options[$number]['title'];
$text = $options[$number]['text'];
$emptyshow = $options[$number]['emptyshow'];
$countshow = $options[$number]['countshow'];
$countwhat = $options[$number]['countwhat'];
$sortby = $options[$number]['sortby'];
$direction = $options[$number]['direction'];
$wrapformwidth = $options[$number]['wrapformwidth'];
$categorywrapwidth = $options[$number]['categorywrapwidth'];
//create the styles
if ($direction == 'vertical') {
$wrapform = "width:$wrapformwidth;";
$categorywrap = "width:$categorywrapwidth;";
}
else {
$wrapform = "width:$wrapformwidth; float:left;";
$categorywrap = "width:$categorywrapwidth; float:left; clear:none;";
}
for($i=0;$i<=$this->totalLevels();$i++){
//set variable names eg: $level1
$leveltitle[$i] = attribute_escape($options[$number]["level$i"]);
}
//Saving as $widgetNumber to ensure clarity
$widgetNumber = $number;
//Creat MYSQL sort by
switch ($sortby) {
case "titleasc":
$sort = "$wpdb->terms.name ASC";
break;
case "titledesc":
$sort = "$wpdb->terms.name DESC";
break;
case "postcountasc":
$sort = "$wpdb->term_taxonomy.count ASC";
break;
case "postcountdesc":
$sort = "$wpdb->term_taxonomy.count DESC";
break;
}
//Show empty
if ($emptyshow == '1') {
$emptyshow = true;
}
else {
$emptyshow = false;
}
//show count
if ($countshow == '1') {
$countshow = true;
}
else {
$countshow = false;
}
?>
'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
if ( $descendants_and_self ) {
$categories = get_categories( "child_of=$descendants_and_self&hierarchical=0&hide_empty=0" );
$self = get_category( $descendants_and_self );
array_unshift( $categories, $self );
} else {
$categories = get_categories('get=all');
}
// Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
$checked_categories = array();
for ( $i = 0; isset($categories[$i]); $i++ ) {
if ( in_array($categories[$i]->term_id, $args['selected_cats']) ) {
$checked_categories[] = $categories[$i];
unset($categories[$i]);
}
}
// Put checked cats on top
$dhat_ajax_catchecklist = call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
// Then the rest of them
$dhat_ajax_catchecklist .= call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
return $dhat_ajax_catchecklist;
}
/**
* Parent function which calls all the JS and CSS scripts
*/
function add_head_scripts() {
if (!is_admin())
{
$this->add_scripts();
$this->add_css();
}
}
/**
* Tells WordPress to load the scripts
*/
function add_scripts(){
$requesturl = DACD_URL .'/includes/dhat-ajax-cat-dropdown-request.php';
wp_enqueue_script('sack');
wp_enqueue_script('dacd_script', DACD_URL .'/js/script.js', array('sack'));
wp_localize_script( 'dacd_script', 'DACDSettings', array(
'requesturl' => $requesturl,
'mode' => "auto"
));
}
/**
* Adds a link to the stylesheet to the header
*/
function add_css(){
echo '';
}
/**
* Adds a link to the admin stylesheet and Javascript to the header
*/
function add_admin_css(){
//wp_print_scripts( array( 'sack' ));
//echo "";
//";
}
function dropdown_only_form_advanced($post_id)
{
//global $post_ID;
global $wpdb;
$edit_html = '
';
return $edit_html;
}
function dropdown_only_form_filterbox()
{
//global $post_ID;
global $wpdb;
$edit_html = '
';
return $edit_html;
}
/* function edit_form_advanced()
{
global $post_ID;
global $wpdb;
$edit_html = '
';
echo $edit_html;
} */
}
}
//instantiate the class
if (class_exists('dhat_ajax_cat_dropdown')) {
$dhat_ajax_cat_dropdown = new dhat_ajax_cat_dropdown();
//register_widget('WP_Widget_DACD_Widget');
}
//add_action('widgets_init', 'WP_Widget_DACD_widget_init');
?>