Ticket #1787: heir_cats.diff

File heir_cats.diff, 3.0 KB (added by ringmaster, 7 years ago)

Patches functions.php and classes.php to find subcategories based on their full path.

  • wp-includes/classes.php

     
    420420 
    421421                // Category stuff for nice URIs 
    422422 
     423                global $cache_categories; 
    423424                if ('' != $q['category_name']) { 
    424                         if (stristr($q['category_name'],'/')) { 
    425                                 $q['category_name'] = explode('/',$q['category_name']); 
    426                                 if ($q['category_name'][count($q['category_name'])-1]) { 
    427                                         $q['category_name'] = $q['category_name'][count($q['category_name'])-1]; // no trailing slash 
    428                                 } else { 
    429                                         $q['category_name'] = $q['category_name'][count($q['category_name'])-2]; // there was a trailling slash 
    430                                 } 
     425                        $cat_paths = '/' . trim(urldecode($q['category_name']), '/'); 
     426                        $q['category_name'] = sanitize_title(basename($cat_paths)); 
     427                        $cat_paths = explode('/', $cat_paths); 
     428                        foreach($cat_paths as $pathdir) $cat_path .= ($pathdir!=''?'/':'') . sanitize_title($pathdir); 
     429                        $q['cat'] = array_reduce( 
     430                                $cache_categories,  
     431                                create_function('$a, $b', 'return ($b->fullpath == "'.$cat_path.'") ? $b->cat_ID : $a;'), 
     432                                0 
     433                        ); 
     434                        // If full path not found, look for last dir as category ignoring parent 
     435                        if($q['cat'] == 0) { 
     436                                $q['cat'] = array_reduce( 
     437                                        $cache_categories,  
     438                                        create_function('$a, $b', 'return ($b->category_nicename == "'.$q['category_name'].'") ? $b->cat_ID : $a;'), 
     439                                        0 
     440                                ); 
    431441                        } 
    432                         $q['category_name'] = sanitize_title($q['category_name']); 
     442                         
    433443                        $tables = ", $wpdb->post2cat, $wpdb->categories"; 
    434444                        $join = " LEFT JOIN $wpdb->post2cat ON ($wpdb->posts.ID = $wpdb->post2cat.post_id) LEFT JOIN $wpdb->categories ON ($wpdb->post2cat.category_id = $wpdb->categories.cat_ID) "; 
    435445                        $whichcat = " AND (category_nicename = '" . $q['category_name'] . "'"; 
    436                         $q['cat'] = $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '" . $q['category_name'] . "'"); 
    437446                        $whichcat .= get_category_children($q['cat'], " OR category_id = "); 
    438447                        $whichcat .= ")"; 
    439448                } 
  • wp-includes/functions.php

     
    13041304function update_category_cache() { 
    13051305        global $cache_categories, $wpdb; 
    13061306        if($dogs = $wpdb->get_results("SELECT * FROM $wpdb->categories")): 
    1307         foreach ($dogs as $catt) 
    1308                 $cache_categories[$catt->cat_ID] = $catt; 
     1307                foreach ($dogs as $catt) $cache_categories[$catt->cat_ID] = $catt; 
     1308                foreach ($cache_categories as $catt) { 
     1309                        $curcat = $catt->cat_ID; 
     1310                        $cache_categories[$catt->cat_ID]->fullpath = '/' . $cache_categories[$catt->cat_ID]->category_nicename; 
     1311                        while ($cache_categories[$curcat]->category_parent != 0) { 
     1312                                $curcat = $cache_categories[$curcat]->category_parent; 
     1313                                $cache_categories[$catt->cat_ID]->fullpath = '/' . $cache_categories[$curcat]->category_nicename . $cache_categories[$catt->cat_ID]->fullpath; 
     1314                        }  
     1315                } 
    13091316                return true; 
    13101317        else :  
    13111318                return false;