Skip to content

Commit 0eba30f

Browse files
committed
add UI support for multi-posttype connections
1 parent 0890e81 commit 0eba30f

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

includes/Registry.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function get_relationship_key( $from, $to, $name ) {
3535
sort( $to );
3636
$to = implode( '.', $to );
3737

38-
return "{$from}_{$to}_{$name}";
38+
return "{$from}~{$to}~{$name}";
3939
}
4040

4141
/**
@@ -70,6 +70,20 @@ public function get_post_to_post_relationship_by_key( $key ) {
7070
return $this->post_post_relationships[ $key ];
7171
}
7272

73+
// Fuzzy match the relationship by parsing the key and looping through the relationships
74+
foreach ( $this->post_post_relationships as $relationship_key => $relationship ) {
75+
$relationship_key_parts = explode( '~', $relationship_key );
76+
$key_parts = explode( '~', $key );
77+
78+
if (
79+
$key_parts[0] === $relationship_key_parts[0] &&
80+
strpos( $relationship_key_parts[1], $key_parts[1] ) !== false &&
81+
$key_parts[2] === $relationship_key_parts[2]
82+
) {
83+
return $this->post_post_relationships[ $relationship_key ];
84+
}
85+
}
86+
7387
return false;
7488
}
7589

@@ -91,10 +105,6 @@ public function get_post_to_post_relationship( $cpt1, $cpt2, $name ) {
91105
return $relationship;
92106
}
93107

94-
// Try the inverse, only if "cpt2" isn't an array
95-
if ( is_array( $cpt2 ) ) {
96-
return false;
97-
}
98108
$key = $this->get_relationship_key( $cpt2, $cpt1, $name );
99109

100110
$relationship = $this->get_post_to_post_relationship_by_key( $key );

includes/Relationships/PostToPost.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,9 @@ public function setup() {
6161

6262
// Make sure CPT is not the same as "from" so we don't get a duplicate, then register if enabled
6363
if ( $this->to !== $this->from && $this->enable_to_ui === true ) {
64-
// Currently, only support a default UI when the "to" end is a single post type
65-
if ( count( $this->to ) === 1 ) {
66-
$this->to_ui = new \TenUp\ContentConnect\UI\PostToPost( $this, $this->to[0], $this->to_labels, $this->to_sortable );
67-
$this->to_ui->setup();
68-
}
64+
$this->to_ui = new \TenUp\ContentConnect\UI\PostToPost( $this, $this->to, $this->to_labels, $this->to_sortable );
65+
$this->to_ui->setup();
6966
}
70-
7167
}
7268

7369
/**

includes/UI/MetaBox.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function save_post( $post_id ) {
7272
$post_type = get_post_type( $post_id );
7373
if ( $relationship->from_ui->render_post_type === $post_type ) {
7474
$relationship->from_ui->handle_save( $relationship_data, $post_id );
75-
} else if ( is_object( $relationship->to_ui ) && $relationship->to_ui->render_post_type === $post_type ) {
75+
} else if ( is_object( $relationship->to_ui ) && in_array( $post_type, $relationship->to_ui->render_post_type, true ) ) {
7676
$relationship->to_ui->handle_save( $relationship_data, $post_id );
7777
}
7878

includes/UI/PostToPost.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ public function setup() {
1111
}
1212

1313
public function filter_data( $data, $post ) {
14+
15+
if ( ! is_array( $this->render_post_type ) ) {
16+
$this->render_post_type = array( $this->render_post_type );
17+
}
18+
1419
// Don't add any data if we aren't on the post type we're supposed to render for
15-
if ( $post->post_type !== $this->render_post_type ) {
20+
if ( ! in_array( $post->post_type, $this->render_post_type ) ) {
1621
return $data;
1722
}
1823

1924
// Determine the other post type in the relationship
20-
$other_post_type = $this->relationship->from == $this->render_post_type ? $this->relationship->to : $this->relationship->from;
25+
if ( $post->post_type === $this->relationship->from ) {
26+
$other_post_type = $this->relationship->to;
27+
} else {
28+
$other_post_type = $this->relationship->from;
29+
}
2130

2231
$final_posts = array();
2332

0 commit comments

Comments
 (0)