diff --git a/includes/Registry.php b/includes/Registry.php index f12807a..93d28e8 100644 --- a/includes/Registry.php +++ b/includes/Registry.php @@ -35,7 +35,7 @@ public function get_relationship_key( $from, $to, $name ) { sort( $to ); $to = implode( '.', $to ); - return "{$from}_{$to}_{$name}"; + return "{$from}~{$to}~{$name}"; } /** @@ -70,6 +70,20 @@ public function get_post_to_post_relationship_by_key( $key ) { return $this->post_post_relationships[ $key ]; } + // Fuzzy match the relationship by parsing the key and looping through the relationships + foreach ( $this->post_post_relationships as $relationship_key => $relationship ) { + $relationship_key_parts = explode( '~', $relationship_key ); + $key_parts = explode( '~', $key ); + + if ( + $key_parts[0] === $relationship_key_parts[0] && + strpos( $relationship_key_parts[1], $key_parts[1] ) !== false && + $key_parts[2] === $relationship_key_parts[2] + ) { + return $this->post_post_relationships[ $relationship_key ]; + } + } + return false; } @@ -91,10 +105,6 @@ public function get_post_to_post_relationship( $cpt1, $cpt2, $name ) { return $relationship; } - // Try the inverse, only if "cpt2" isn't an array - if ( is_array( $cpt2 ) ) { - return false; - } $key = $this->get_relationship_key( $cpt2, $cpt1, $name ); $relationship = $this->get_post_to_post_relationship_by_key( $key ); diff --git a/includes/Relationships/PostToPost.php b/includes/Relationships/PostToPost.php index ce53e29..4d51b9f 100644 --- a/includes/Relationships/PostToPost.php +++ b/includes/Relationships/PostToPost.php @@ -61,13 +61,9 @@ public function setup() { // Make sure CPT is not the same as "from" so we don't get a duplicate, then register if enabled if ( $this->to !== $this->from && $this->enable_to_ui === true ) { - // Currently, only support a default UI when the "to" end is a single post type - if ( count( $this->to ) === 1 ) { - $this->to_ui = new \TenUp\ContentConnect\UI\PostToPost( $this, $this->to[0], $this->to_labels, $this->to_sortable ); - $this->to_ui->setup(); - } + $this->to_ui = new \TenUp\ContentConnect\UI\PostToPost( $this, $this->to, $this->to_labels, $this->to_sortable ); + $this->to_ui->setup(); } - } /** diff --git a/includes/UI/MetaBox.php b/includes/UI/MetaBox.php index cad50c1..24ff162 100644 --- a/includes/UI/MetaBox.php +++ b/includes/UI/MetaBox.php @@ -72,7 +72,7 @@ public function save_post( $post_id ) { $post_type = get_post_type( $post_id ); if ( $relationship->from_ui->render_post_type === $post_type ) { $relationship->from_ui->handle_save( $relationship_data, $post_id ); - } else if ( is_object( $relationship->to_ui ) && $relationship->to_ui->render_post_type === $post_type ) { + } else if ( is_object( $relationship->to_ui ) && in_array( $post_type, $relationship->to_ui->render_post_type, true ) ) { $relationship->to_ui->handle_save( $relationship_data, $post_id ); } diff --git a/includes/UI/PostToPost.php b/includes/UI/PostToPost.php index 67bd134..ea5b38a 100644 --- a/includes/UI/PostToPost.php +++ b/includes/UI/PostToPost.php @@ -11,13 +11,22 @@ public function setup() { } public function filter_data( $data, $post ) { + + if ( ! is_array( $this->render_post_type ) ) { + $this->render_post_type = array( $this->render_post_type ); + } + // Don't add any data if we aren't on the post type we're supposed to render for - if ( $post->post_type !== $this->render_post_type ) { + if ( ! in_array( $post->post_type, $this->render_post_type ) ) { return $data; } // Determine the other post type in the relationship - $other_post_type = $this->relationship->from == $this->render_post_type ? $this->relationship->to : $this->relationship->from; + if ( $post->post_type === $this->relationship->from ) { + $other_post_type = $this->relationship->to; + } else { + $other_post_type = $this->relationship->from; + } $final_posts = array();