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

Define Post Model Terms Fluently #277

Merged
merged 14 commits into from
Jun 2, 2022
Merged

Define Post Model Terms Fluently #277

merged 14 commits into from
Jun 2, 2022

Conversation

srtfisher
Copy link
Member

@srtfisher srtfisher commented May 27, 2022

Extends #276
Docs https://github.com/alleyinteractive/mantle-docs/pull/21/files

Allows post models to add/set/delete/update terms using a fluent format.

The Post model support interacting with terms through relationships or through the model directly. The model supports multiple methods to make setting terms on a post simple:

$category = Category::whereName( 'Example Category' )->first();

// Save the category to a post.
$post = new Post( [ 'title' => 'Example Post' ] );

// Also supports an array of IDs or WP_Term objects.
$post->terms->category = [ $category ];

$post->save();

// Read the tags from a post.
$post->terms->post_tag // Term[]

Terms can also be set when creating a post (specifying the taxonomy is optional):

$post = new Post( [
	'title' => 'Example Title',
	'terms' => [ $category ],
] );

$post = new Post( [
	'title' => 'Example Title',
	'terms' => [
		'category' => [ $category ],
		'post_tag' => [ $tag ],
	],
] );

@srtfisher srtfisher changed the title model term helpers Model Term Helpers May 27, 2022
@srtfisher srtfisher changed the title Model Term Helpers Define Post Model Terms Fluently May 27, 2022
Copy link
Contributor

@anubisthejackle anubisthejackle left a comment

Choose a reason for hiding this comment

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

Looks good. I would confirm my suspicions about the logic concern in one of the assertions before merging, but everything else seems good to go.

🎸

src/mantle/testing/concerns/trait-assertions.php Outdated Show resolved Hide resolved
src/mantle/testing/concerns/trait-assertions.php Outdated Show resolved Hide resolved
src/mantle/database/model/term/trait-model-term.php Outdated Show resolved Hide resolved
@srtfisher srtfisher enabled auto-merge June 2, 2022 21:11
@srtfisher srtfisher merged commit 1e41df4 into main Jun 2, 2022
@srtfisher srtfisher deleted the model-term-helpers branch June 2, 2022 21:13
Comment on lines +426 to +436
public function assertPostNotHasTerm( $post, $term ) {
if ( $post instanceof Post ) {
$post = $post->id();
}

$term = $this->get_term_from_argument( $term );

if ( $term ) {
PHPUnit::assertFalse( \has_term( $term->term_id, $term->taxonomy, $post ) );
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I know this has already been merged, but I wanted to call out that without PHPUnit::assertNotInstanceOf we no longer have an assertion if $term is null. That was the intent behind that line in my previous suggestion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants