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

Replace each_connected() with something less confusing #348

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
31 changes: 28 additions & 3 deletions core/connection-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,36 @@ public function get_adjacent_items( $item ) {
*/
public function each_connected( $items, $extra_qv = array(), $prop_name = 'connected' ) {
if ( is_a( $items, 'WP_Query' ) )
$items =& $items->posts;
$items = $items->posts;

if ( empty( $items ) || !is_object( $items[0] ) )
return;

try {
list( $raw_connected ) = $this->_connected_many( $items, $extra_qv );

p2p_distribute_connected( $items, $raw_connected, $prop_name );
} catch ( P2P_Exception $e ) {
trigger_error( $e->getMessage(), E_USER_WARNING );
}
}

public function connected_many( $items, $extra_qv = array() ) {
if ( is_a( $items, 'WP_Query' ) )
$items = $items->posts;

Xdebug_break();

try {
list( $raw_connected, $directed ) = $this->_connected_many( $items, $extra_qv );

return new P2P_Connections_Map( $raw_connected, $directed );
} catch ( P2P_Exception $e ) {
return new WP_Error( $e->getMessage() );
}
}

private function _connected_many( $items, $extra_qv = array() ) {
$post_types = array_unique( wp_list_pluck( $items, 'post_type' ) );

if ( count( $post_types ) > 1 ) {
Expand All @@ -401,7 +426,7 @@ public function each_connected( $items, $extra_qv = array(), $prop_name = 'conne
$direction = _p2p_compress_direction( $possible_directions );

if ( !$direction )
return false;
throw new P2P_Exception( "Can't determine direction." );

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

Expand All @@ -419,7 +444,7 @@ public function each_connected( $items, $extra_qv = array(), $prop_name = 'conne
foreach ( $q->items as $item )
$raw_connected[] = $item->get_object();

p2p_distribute_connected( $items, $raw_connected, $prop_name );
return array( $raw_connected, $directed );
}

public function get_desc() {
Expand Down
17 changes: 17 additions & 0 deletions core/connections-map.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

class P2P_Connections_Map {

private $directed;
private $buckets;

function __construct( $items, $directed ) {
$this->buckets = scb_list_group_by( $items, '_p2p_get_other_id' );
$this->directed = $directed;
}

function _for( $item ) {
return $this->buckets[ $item->ID ];
}
}

19 changes: 14 additions & 5 deletions tests/test-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -364,14 +364,23 @@ function test_each_connected() {
'order' => 'ASC'
) );

$ctype->each_connected( $query );
$connected = $ctype->connected_many( $query );
if ( is_wp_error( $connected ) )
throw new Exception('WTF');

$this->assertEquals( $query->posts[0]->connected[0]->ID, $movie->ID );
$this->assertEquals( $query->posts[1]->connected[0]->p2p_id, $p2p_id_1 );
$this->assertEmpty( $query->posts[2]->connected );
var_dump( wp_list_pluck( $query->posts, 'ID' ) );
var_dump( $connected );

$connected_for_0 = $connected->_for( $query->posts[0] );
$connected_for_1 = $connected->_for( $query->posts[1] );
$connected_for_2 = $connected->_for( $query->posts[2] );

$this->assertEquals( $connected_for_0[0]->ID, $movie->ID );
$this->assertEquals( $connected_for_0[1]->p2p_id, $p2p_id_1 );
$this->assertEmpty( $connected_for_2 );

// Test that connected posts have real properties
$properties = get_object_vars( $query->posts[0]->connected[0] );
$properties = get_object_vars( $connected_for_0[0] );
$this->assertTrue( isset( $properties['post_type'] ) );
}

Expand Down