Skip to content
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
20 changes: 15 additions & 5 deletions includes/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
Copy link
Author

Choose a reason for hiding this comment

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

Switched to a more obscure delimiter to avoid confusion with underscores in post type names.

}

/**
Expand Down Expand Up @@ -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
Copy link
Author

@gthayer gthayer May 16, 2025

Choose a reason for hiding this comment

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

Truthfully, I'm not in love with the fuzzy match. I think this could break down if a post type name contained the name of another post type. ex: "post" and "postcard".

The challenge here is that the relationship name needs to be able to be constructed, however when the post type is one of the "many" set, it does not have knowledge of it's siblings and the key can not be constructed properly.

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;
}

Expand All @@ -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 );
Expand Down
8 changes: 2 additions & 6 deletions includes/Relationships/PostToPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

/**
Expand Down
2 changes: 1 addition & 1 deletion includes/UI/MetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
13 changes: 11 additions & 2 deletions includes/UI/PostToPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down