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

Add support for reciprocal relationships #33

Merged
merged 14 commits into from
Jan 5, 2020
91 changes: 21 additions & 70 deletions inc/api.php
Original file line number Diff line number Diff line change
@@ -1,75 +1,41 @@
<?php
/**
* Public API helper functions.
*
* @package Meta Box
* @subpackage MB Relationships
*/

/**
* The API class.
*/
class MB_Relationships_API {
/**
* Reference to relationship factory.
*
* @var MBR_Relationship_Factory
*/
protected static $factory;
private static $factory;

/**
* Reference to post query object.
*
* @var MBR_Query_Post
*/
protected static $post_query;
private static $post_query;

/**
* Reference to term query object.
*
* @var MBR_Query_Term
*/
protected static $term_query;
private static $term_query;

/**
* Reference to user query object.
*
* @var MBR_Query_User
*/
protected static $user_query;
private static $user_query;

/**
* Set relationship factory.
*
* @param MBR_Relationship_Factory $factory The object factory.
*/
public static function set_relationship_factory( MBR_Relationship_Factory $factory ) {
self::$factory = $factory;
}

/**
* Set post query.
*
* @param MBR_Query_Post $post_query The post query object.
*/
public static function set_post_query( MBR_Query_Post $post_query ) {
self::$post_query = $post_query;
}

/**
* Set term query.
*
* @param MBR_Query_Term $term_query The term query object.
*/
public static function set_term_query( MBR_Query_Term $term_query ) {
self::$term_query = $term_query;
}

/**
* Set user query.
*
* @param MBR_Query_User $user_query The user query object.
*/
public static function set_user_query( MBR_Query_User $user_query ) {
self::$user_query = $user_query;
}
Expand All @@ -78,7 +44,6 @@ public static function set_user_query( MBR_Query_User $user_query ) {
* Register a relationship.
*
* @param array $settings Relationship parameters.
*
* @return MBR_Relationship
*/
public static function register( $settings ) {
Expand All @@ -91,7 +56,6 @@ public static function register( $settings ) {
* @param int $from From object ID.
* @param int $to To object ID.
* @param string $id Relationship ID.
*
* @return bool
*/
public static function has( $from, $to, $id ) {
Expand All @@ -107,7 +71,6 @@ public static function has( $from, $to, $id ) {
* @param string $id Relationship ID.
* @param int $order_from The order on the "from" side.
* @param int $order_to The order on the "to" side.
*
* @return bool
*/
public static function add( $from, $to, $id, $order_from = 1, $order_to = 1 ) {
Expand All @@ -122,7 +85,6 @@ public static function add( $from, $to, $id, $order_from = 1, $order_to = 1 ) {
* @param int $from From object ID.
* @param int $to To object ID.
* @param string $id Relationship ID.
*
* @return bool
*/
public static function delete( $from, $to, $id ) {
Expand All @@ -137,13 +99,10 @@ public static function delete( $from, $to, $id ) {
* @param array $query_vars Extra query variables.
*/
public static function each_connected( $args, $query_vars = array() ) {
$args = wp_parse_args(
$args,
array(
'id' => '',
'property' => 'connected',
)
);
$args = wp_parse_args( $args, [
'id' => '',
'property' => 'connected',
] );
$relationship = self::$factory->get( $args['id'] );
if ( ! $relationship ) {
return;
Expand All @@ -163,16 +122,12 @@ public static function each_connected( $args, $query_vars = array() ) {
* Get connected items.
*
* @param array $args Relationship arguments.
*
* @return array
*/
public static function get_connected( $args ) {
$args = wp_parse_args(
$args,
array(
'id' => '',
)
);
$args = wp_parse_args( $args, [
'id' => '',
] );
$relationship = self::$factory->get( $args['id'] );
if ( ! $relationship ) {
return array();
Expand All @@ -193,31 +148,27 @@ public static function get_connected( $args ) {
* @param array $connected List of connected objects.
* @param string $property Name of connected array property.
* @param string $id_key ID key of the objects.
*
* @return array
*/
protected static function distribute( &$items, $connected, $property, $id_key ) {
private static function distribute( &$items, $connected, $property, $id_key ) {
foreach ( $items as &$item ) {
$item->$property = self::filter( $connected, $item->$id_key );
}
return $items;
}

/**
* Filter to find the matched items with "mb_origin" value.
*
* @param array $list List of objects.
* @param string $value "mb_origin" value.
* Filter to find the matched items.
* Non-reciprocal relationships: uses mbr_origin key.
* Reciprocal relationships: uses mbr_from and mbr_to keys.
*
* @param array $items Connected items.
* @param string $object_id Connected object ID.
* @return array
*/
protected static function filter( $list, $value ) {
$filtered = array();
foreach ( $list as $item ) {
if ( $value == $item->mb_origin ) {
$filtered[] = $item;
}
}
return $filtered;
private static function filter( $items, $object_id ) {
return array_filter( $items, function( $item ) use ( $object_id ) {
return ( isset( $item->mbr_origin ) && $item->mb_origin == $object_id ) || $item->mbr_from == $object_id || $item->mbr_to == $object_id;
} );
}
}
2 changes: 1 addition & 1 deletion inc/database/storage-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function filter_storage( $storage, $object_type, $meta_box ) {
return $storage;
}
if ( ! $this->storage ) {
$this->storage = new MBR_Storage();
$this->storage = new MBR_Storage( $this->factory );
}

return $this->storage;
Expand Down
Loading