Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #332: adjacent items for unsorted connections #370

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions core/connection-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function get_adjacent( $from, $to, $which ) {
}

/**
* Get the previous, next and parent items, in an ordered connection type.
* Get the previous, next and parent items
*
* @param mixed The current item
*
Expand Down Expand Up @@ -356,9 +356,44 @@ public function get_adjacent_items( $item ) {
$parent = $connected_series[0];

$result['parent'] = $parent->get_object();
$result['previous'] = $this->get_previous( $item->ID, $parent->ID );
$result['next'] = $this->get_next( $item, $parent );

$directed = $this->set_direction( $direction );

$key = $directed->get_orderby_key();
if ( $key )
{
$result['previous'] = $this->get_previous( $item->ID, $parent->ID );
$result['next'] = $this->get_next( $item, $parent );
}
else // connection is unsorted
{
$relatives = $this->get_connected( $result['parent'],
array( 'p2p:per_page' => -1 ), 'abstract' )->items;

// sort it by post_date DESC

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that you changed the indentation from tabs to spaces. It makes it really hard to see the relevant changes.

$last2 = FALSE;
$last = FALSE;
$current = FALSE;
$i = 0;
$length = count ($relatives);
while ($i < $length AND $last != $item->ID)
{
if ($last) $last2 = $last;
$last = $current;
$current = $relatives[$i]->ID;
$i++;
}
if ($last == $item->ID)
{
if ($last2) $result['previous'] = get_post($last2);
$result['next'] = get_post($current);
}
if ($current == $item->ID)
{
$result['previous'] = get_post($last);
}
}
return $result;
}

Expand Down