Advertisement
maorb

is_child

Feb 20th, 2012
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.87 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: is_child
  4. Plugin URI: http://erik.range-it.de/wordpress/plugins/is_child/
  5. Description: Checks if current page, posting or category is in direct or recursive relation with a specific parent category or parent page.
  6. Version: 1.1.5
  7. Author: Erik Range
  8. Author URI: http://erik.range-it.de/
  9.  
  10.  
  11. ---------------------------------------------------------------------
  12. Usage:
  13. ---------------------------------------------------------------------
  14.     bool is_child( mixed parent [, bool recursive] )
  15.  
  16.  
  17. ---------------------------------------------------------------------
  18. Examples:
  19. ---------------------------------------------------------------------
  20. --> is_child( 10 )
  21.     Checks if the current page, posting or category is somehow
  22.     related to category- (or page-)ID 10.
  23.    
  24. --> is_child( "Example Category", false )
  25.     Checks just if the current element is a direct child of "Example
  26.     Category".
  27.    
  28. --> is_child( "some-page", 1 )
  29.     Checks if the current element (page) is somehow related to a page
  30.     with the slug "some-page".
  31.  
  32.  
  33. ---------------------------------------------------------------------
  34. History:
  35. ---------------------------------------------------------------------
  36. 1.1.5 - Maor Barazany's fix - on posts it caused a lot of errors, see line #156.
  37. 1.1.2 - Fixed a bug which ended up in seldomly sending an invalid
  38.         SQL-query when browsing archives.
  39. 1.1.1 - Fixed a bug which resulted in getting wrong page IDs from
  40.         SQL-queries.
  41. 1.1.0 - Added checking for parental pages.
  42. 1.0.0 - Final Release.
  43. 0.5.0 - No more catlooping. All categories in queue are now handled
  44.         with one single query to boost up performance.
  45.       - Catching IDs if zero, as they're always true.
  46. 0.4.0 - Now being able to look for category name or slug instead of the
  47.         plain ID.
  48. 0.3.0 - Automated detection of category, page or single posting.
  49. 0.2.0 - Added recursive search.
  50. 0.1.0 - Initial Test Release.
  51.    
  52. */
  53.  
  54. if( !function_exists( "is_child" ) ) {
  55.    
  56.     function is_child( $ofParent, $doRecursive = true ) {
  57.  
  58.         global $wpdb;
  59.         $allCats = array();
  60.        
  61.         // Turn title or slug into ID if needed.
  62.         if( !is_int( $ofParent ) ) {
  63.             if( is_page() )
  64.                 # Different handling for Pages
  65.                 $getID = $wpdb->get_results("
  66.                     SELECT  ID as cat_ID
  67.                     FROM    {$wpdb->posts}
  68.                     WHERE   post_title = '{$ofParent}'
  69.                     OR      post_name = '{$ofParent}'
  70.                     LIMIT   0,1
  71.                 ");
  72.             /*else
  73.                 # Get catID
  74.                 $getID = $wpdb->get_results("
  75.                     SELECT  cat_ID
  76.                     FROM    {$wpdb->categories}
  77.                     WHERE   cat_name = '{$ofParent}'
  78.                     OR      category_nicename = '{$ofParent}'
  79.                     LIMIT   0,1
  80.                 ");
  81. */
  82.             if( !$getID )
  83.                 # Not found.
  84.                 return false;
  85.             else {
  86.                 # Found.
  87.                 $ofParent = $getID[0]->cat_ID;
  88.                 unset( $getID );
  89.             }
  90.         }
  91.        
  92.         // Everyone's a sub zero.
  93.         if( $ofParent == 0 && $doRecursive )
  94.             return true;
  95.        
  96.         // Now let's break it down to categories (or pages).
  97.         if( is_page() ) {
  98.             global $post;
  99.             $allCats[] = $post->ID;
  100.         } elseif( is_single() ) {
  101.             $getCats = get_the_category();
  102.             foreach( $getCats as $getCat )
  103.                 $allCats[] = $getCat->cat_ID;
  104.             unset( $getCats );
  105.         } elseif( is_category ) {
  106.             global $cat;
  107.             $allCats[] = $cat;
  108.         }
  109.  
  110.         // Already a match? Would save processing time.
  111.         if( in_array( $ofParent, $allCats ) )
  112.             return true;
  113.            
  114.         // Post without recursive search ends here.
  115.         if( ( is_single() ) && !$doRecursive )
  116.             return false;
  117.  
  118.         // Otherwise, let's do some genealogy.
  119.         while( count( $allCats ) != 0 ) {
  120.             if( in_array( $ofParent, $allCats ) )
  121.                 return true;
  122.             else
  123.                 $allCats =
  124.                     is_child_getParents( $allCats );
  125.         }
  126.        
  127.         // Still here? Then nothing has been found.
  128.         return false;
  129.  
  130.     }
  131. }
  132.  
  133.  
  134. if( !function_exists( "is_child_getParents" ) ) {
  135.  
  136.     function is_child_getParents( $fromChilds ) {
  137.        
  138.         // As there's only get_category_parents which isn't useful
  139.         // for fetching parental data, we'll have to query this
  140.         // directly to the DB.
  141.         global $wpdb;
  142.        
  143.         $fromChilds = implode( ", ", $fromChilds );
  144.         if( !$fromChilds ) return array();
  145.        
  146.         $getParents =
  147.             ( is_page() )
  148.             ?   # Pages
  149.                 $wpdb->get_results("
  150.                     SELECT  post_parent AS category_parent
  151.                     FROM    {$wpdb->posts}
  152.                     WHERE   ID IN ({$fromChilds})
  153.                 ")
  154.             :   # Posts / Categories
  155.             /*
  156.              * Maor change
  157.              * Remove the else pharse
  158.              * This old code caused tons of errors errors, since $wpdb->categories is not a WP table.
  159.              * more than that - it is not needed, since you should use this for pages only, for posts you should use wp defauly is_category() function           *
  160.              * */
  161.                 /*$wpdb->get_results("
  162.                     SELECT  category_parent
  163.                     FROM    {$wpdb->categories}
  164.                     WHERE   cat_ID IN ({$fromChilds})
  165.                 ")*/
  166.             '';
  167.         if ($getParents != '')  {
  168.             foreach( $getParents as $getParent )
  169.                 if( $getParent->category_parent != 0 )
  170.                     $allParents[] = $getParent->category_parent;
  171.         }
  172.            
  173.         return $allParents;
  174.  
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement