[wp-trac] [WordPress Trac] #31211: New function for link-category intersection

WordPress Trac noreply at wordpress.org
Mon Feb 2 14:29:57 UTC 2015


#31211: New function for link-category intersection
---------------------------+-----------------------------
 Reporter:  krabat1        |      Owner:
     Type:  enhancement    |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  General        |    Version:  4.1
 Severity:  normal         |   Keywords:
  Focuses:  accessibility  |
---------------------------+-----------------------------
 Maybe passed to wp-includes/bookmark.php...

 {{{
 function link_cat_intersect($input, $args=array() ){
         /*
                 $input (string) - Comma separated list of link-category
 ID-s to find common links.
                 $args (array) - $args for get_bookmarks() function
 http://codex.wordpress.org/Function_Reference/get_bookmarks
                 Defaults:
                 $args = array(
                         'orderby'        => 'name',
                         'order'          => 'ASC',
                         'limit'          => -1,
                         'category'       => '',
                         'category_name'  => '',
                         'hide_invisible' => 1,
                         'show_updated'   => 0,
                         'include'        => '',
                         'exclude'        => '',
                         'search'         => '' );

                 Example:
                 $links = link_cat_intersect('65,75',array('include' => '8'
 ));
                 Get the common links of link categories 65 and 75, then
 add the link with id 8 to result.

                 Return Values
 http://codex.wordpress.org/Function_Reference/get_bookmarks
                 (array)
                 List of bookmark row objects. Each bookmark object may
 contain the following: 'link_id', 'link_url', 'link_name', 'link_image',
 'link_target', 'link_category', 'link_description', 'link_visible',
 'link_owner', 'link_rating', 'link_updated', 'link_rel', 'link_notes',
 'link_rss'
         */
         $input = explode(',',$input);
         $input = array_unique($input); // remove duplicates
         if( count($input)==1 ){
                 return new WP_Error( 'broke', __( "One id is not
 enough...", "my_textdomain" ) );
         }
         $keys=$input;
         $input = array_combine($keys, array_values($input));
         // get the link ids
         foreach($input as $cat_id => $posts){
                 $objects =
 get_objects_in_term($input[$cat_id],'link_category');
                 if( is_wp_error( $objects ) ) {
                         echo $objects->get_error_message().'
 !link_category_id: '.$cat_id;
                         return false;
                 }else{
                         $input[$cat_id]= $objects;
                 }
         }

         // thx to Shackrock and outis http://stackoverflow.com/a/8198111
         function recursive($inarray,$result){
                 foreach ($inarray as $inkey => $inval) {
                         if ( is_array($inval) ) {
                                 $result = recursive($inval, $result);
                         } else {
                                 $result[] = $inval;
                         }
                 }
                 return $result;
         };

         $intersect = function($array){
                 foreach(array_count_values(recursive( $array, array() ))
 as $key => $value ){
                         if( $value == count($array) ){ // intersect
                                 $result[] = $key;
                         }
                 }
                 return $result;
         };

         $result = $intersect($input);


         if( !isset($args['include']) || $args['include']=='' ){
                 $args['include'] = implode(',',$result);
         }else if( isset($args['include']) && $args['include'] != '' ){
                 $args['include'] = explode(',',$args['include']);
                 $args['include'] = array_merge($args['include'],$result);
                 $args['include'] = implode(',',$args['include']);
         }

         $result = get_bookmarks($args);
         if( count($result) == 0 ){
                 return new WP_Error( 'broke', __( "No posts in the
 intersection...", "my_textdomain" ) );
         }else{
                 return $result;
         }
 }

 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31211>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list