Skip to content

Commit

Permalink
Better Post-Formats support (#990)
Browse files Browse the repository at this point in the history
* Better Post-Formats support

This change uses the Post-Format also for Custom-Post-Type if they support them.

* improve check

* changed based on the feedback of @obenland

* a less restrictive check

* handle all `! Pages` the same way

@obenland that puts a knot in my brain ^^

what about this change?

* Add test for CPT with post format support.

* Combine control structures

---------

Co-authored-by: Konstantin Obenland <obenland@gmx.de>
  • Loading branch information
pfefferle and obenland authored Nov 14, 2024
1 parent 317dc9b commit 6d76880
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
35 changes: 8 additions & 27 deletions includes/transformer/class-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,33 +729,14 @@ protected function get_type() {
return 'Note';
}

// Default to Article.
$object_type = 'Article';
$post_format = 'standard';

if ( \get_theme_support( 'post-formats' ) ) {
$post_format = \get_post_format( $this->wp_object );
}

$post_type = \get_post_type( $this->wp_object );
switch ( $post_type ) {
case 'post':
switch ( $post_format ) {
case 'standard':
case '':
$object_type = 'Article';
break;
default:
$object_type = 'Note';
break;
}
break;
case 'page':
$object_type = 'Page';
break;
default:
$object_type = 'Article';
break;
// Default to Note.
$object_type = 'Note';
$post_type = \get_post_type( $this->wp_object );

if ( 'page' === $post_type ) {
$object_type = 'Page';
} elseif ( ! \get_post_format( $this->wp_object ) ) {
$object_type = 'Article';
}

return $object_type;
Expand Down
34 changes: 34 additions & 0 deletions tests/class-test-transformer-post.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,38 @@ public function test_get_type_respects_post_type_title_support() {
// Clean up.
unregister_post_type( 'no_title_type' );
}

/**
* Test that the get_type method returns article for custom post type with post format support.
*
* @covers ::get_type
*/
public function test_get_type_respects_post_format_support() {

// Create custom post type without title support.
register_post_type(
'no_title_type',
array(
'public' => true,
'supports' => array( 'editor', 'title', 'post-formats' ), // Needs to include 'title'.
)
);

$post_id = $this->factory->post->create(
array(
'post_title' => 'Test Post',
'post_content' => str_repeat( 'Long content. ', 100 ),
'post_type' => 'no_title_type',
)
);
$post = get_post( $post_id );

$transformer = new Post( $post );
$type = $this->reflection_method->invoke( $transformer );

$this->assertSame( 'Article', $type );

// Clean up.
unregister_post_type( 'no_title_type' );
}
}

0 comments on commit 6d76880

Please sign in to comment.