/** * Retrieve connections. * * @param string $p2p_type A valid connection type. * @param array $args Query args: * * - 'direction': Can be 'from', 'to' or 'any' * - 'from': Object id. The first end of the connection. (optional) * - 'to': Object id. The second end of the connection. (optional) * - 'fields': Which field of the connection to return. Can be: * 'all', 'object_id', 'p2p_from', 'p2p_to', 'p2p_id' or 'count' * * @return array */ function p2p_get_connections($p2p_type, $args = array()) { extract(wp_parse_args($args, array('direction' => 'from', 'from' => 'any', 'to' => 'any', 'fields' => 'all')), EXTR_SKIP); $r = array(); foreach (_p2p_expand_direction($direction) as $direction) { $args = array($from, $to); if ('to' == $direction) { $args = array_reverse($args); } if ('object_id' == $fields) { $field = 'to' == $direction ? 'p2p_from' : 'p2p_to'; } else { $field = $fields; } $r = array_merge($r, _p2p_get_connections($p2p_type, array('from' => $args[0], 'to' => $args[1], 'fields' => $field))); } if ('count' == $fields) { return array_sum($r); } return $r; }
/** * Retrieve connections. * * @param string $p2p_type A valid connection type. * @param array $args Query args: * * - 'direction': Can be 'from', 'to' or 'any' * - 'from': Object id. The first end of the connection. (optional) * - 'to': Object id. The second end of the connection. (optional) * - 'fields': Which field of the connection to return. Can be: * 'all', 'object_id', 'p2p_from', 'p2p_to', 'p2p_id' or 'count' * * @return array */ function p2p_get_connections($p2p_type, $args = array()) { $args = wp_parse_args($args, array('direction' => 'from', 'from' => 'any', 'to' => 'any', 'fields' => 'all')); $r = array(); foreach (_p2p_expand_direction($args['direction']) as $direction) { $dirs = array($args['from'], $args['to']); if ('to' == $direction) { $dirs = array_reverse($dirs); } if ('object_id' == $args['fields']) { $fields = 'to' == $direction ? 'p2p_from' : 'p2p_to'; } else { $fields = $args['fields']; } $r = array_merge($r, _p2p_get_connections($p2p_type, array('from' => $dirs[0], 'to' => $dirs[1], 'fields' => $fields))); } if ('count' == $args['fields']) { return array_sum($r); } return $r; }