Make WordPress Core

Opened 10 years ago

Last modified 5 years ago

#31211 new enhancement

New function for link-category intersection

Reported by: krabat1's profile krabat1 Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: General Keywords:
Focuses: Cc:

Description

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;
	}
}

Change History (1)

#1 @afercia
9 years ago

  • Focuses accessibility removed

Removing the accessibility focus, should be used only for Web Content Accessibility related issues.

Note: See TracTickets for help on using tickets.