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

Add conditional for CPT archives and CPT term archives #328

Merged
merged 17 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 15 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
5 changes: 2 additions & 3 deletions src/class-parsely.php
Original file line number Diff line number Diff line change
Expand Up @@ -1018,9 +1018,8 @@ public function construct_parsely_metadata( array $parsely_options, $post ) {
$author = ( get_query_var( 'author_name' ) ) ? get_user_by( 'slug', get_query_var( 'author_name' ) ) : get_userdata( get_query_var( 'author' ) );
$parsely_page['headline'] = $this->get_clean_parsely_page_value( 'Author - ' . $author->data->display_name );
$parsely_page['url'] = $current_url;
} elseif ( is_category() ) {
$category = get_the_category();
$category = $category[0];
} elseif ( is_category() || is_post_type_archive() || is_tax() ) {
$category = get_queried_object();
$parsely_page['headline'] = $this->get_clean_parsely_page_value( $category->name );
$parsely_page['url'] = $current_url;
} elseif ( is_date() ) {
Expand Down
83 changes: 83 additions & 0 deletions tests/StructuredData/CustomPostTypeArchiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Structured Data Tests for the blog page (archive).
*
* @package Parsely\Tests
*/

namespace Parsely\Tests\StructuredData;

/**
* Structured Data Tests for the custom post type (archive).
*
* @see https://www.parse.ly/help/integration/jsonld
* @covers \Parsely::construct_parsely_metadata
*/
final class CustomPostTypeArchiveTest extends NonPostTestCase {
/**
* Check metadata for custom post type archive.
*
* @covers \Parsely::construct_parsely_metadata
* @uses \Parsely::__construct
* @uses \Parsely::get_author_name
* @uses \Parsely::get_author_names
* @uses \Parsely::get_bottom_level_term
* @uses \Parsely::get_category_name
* @uses \Parsely::get_clean_parsely_page_value
* @uses \Parsely::get_coauthor_names
* @uses \Parsely::get_current_url
* @uses \Parsely::get_first_image
* @uses \Parsely::get_options
* @uses \Parsely::get_tags
* @uses \Parsely::post_has_trackable_status
* @uses \Parsely::update_metadata_endpoint
* @group metadata
*/
public function test_metadata_is_correctly_constructed_for_custom_post_type_archive() {
// Set permalinks, as Parsely currently strips ?page_id=... from the URL property.
// See https://github.com/Parsely/wp-parsely/issues/151.
$this->set_permalink_structure( '/%postname%/' );

// Setup Parsley object.
$parsely = new \Parsely();
$parsely_options = get_option( \Parsely::OPTIONS_KEY );

// Register Post Type with specific archive URL.
register_post_type(
'custom_post_type',
array(
'public' => true,
'has_archive' => 'cpt-archive',
)
);

// Add post to custom post type.
self::factory()->post->create(
array(
'title' => 'Post Title',
'post_type' => 'custom_post_type',
)
);

// Flush rewrite rules after creating new post type with archive.
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules
flush_rewrite_rules();

// Go to the custom post type archive page.
$this->go_to( home_url( '/cpt-archive' ) );

// The query should be for a custom post type archive.
self::assertQueryTrue( 'is_archive', 'is_post_type_archive' );

// Create the structured data for that CPT.
// The CPT archive metadata doesn't use the post data, but the construction method requires it for now.
$structured_data = $parsely->construct_parsely_metadata( $parsely_options, get_post() );

// Check the required properties exist.
$this->assert_data_has_required_properties( $structured_data );

// The headline should be the CPT name.
self::assertEquals( 'custom_post_type', $structured_data['headline'] );
self::assertEquals( home_url( '/cpt-archive' ), $structured_data['url'] );
}
}
83 changes: 83 additions & 0 deletions tests/StructuredData/CustomTaxonomyTermArchiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Structured Data Tests for the blog page (archive).
*
* @package Parsely\Tests
*/

namespace Parsely\Tests\StructuredData;

/**
* Structured Data Tests for the custom taxonomy term (archive).
*
* @see https://www.parse.ly/help/integration/jsonld
* @covers \Parsely::construct_parsely_metadata
*/
class CustomTaxonomyTermArchiveTest extends NonPostTestCase {
/**
* Check metadata for custom post type term archive.
*
* @covers \Parsely::construct_parsely_metadata
* @uses \Parsely::__construct
* @uses \Parsely::get_author_name
* @uses \Parsely::get_author_names
* @uses \Parsely::get_bottom_level_term
* @uses \Parsely::get_category_name
* @uses \Parsely::get_clean_parsely_page_value
* @uses \Parsely::get_coauthor_names
* @uses \Parsely::get_current_url
* @uses \Parsely::get_first_image
* @uses \Parsely::get_options
* @uses \Parsely::get_tags
* @uses \Parsely::post_has_trackable_status
* @uses \Parsely::update_metadata_endpoint
* @group metadata
*/
public function test_metadata_is_correctly_constructed_for_custom_taxonomy_term_archive() {
// Set permalinks, as Parsely currently strips ?page_id=... from the URL property.
// See https://github.com/Parsely/wp-parsely/issues/151.
$this->set_permalink_structure( '/%postname%/' );

// Setup Parsley object.
$parsely = new \Parsely();
$parsely_options = get_option( \Parsely::OPTIONS_KEY );

// Register custom taxonomy.
register_taxonomy( 'custom_tax', array( 'post' ) );

// Insert a single term, and a post with the custom term.
$term = self::factory()->term->create(
array(
'taxonomy' => 'custom_tax',
'slug' => 'term',
'name' => 'Custom Taxonomy Term',
)
);
$post_id = self::factory()->post->create();

wp_set_post_terms( $post_id, $term, 'custom_tax' );

$term_link = get_term_link( $term );

// Flush rewrite rules after creating new taxonomy type.
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.flush_rewrite_rules_flush_rewrite_rules
flush_rewrite_rules();

// Go to the term archive page.
$this->go_to( $term_link );

// The query should be for a taxonomy archive.
self::assertQueryTrue( 'is_archive', 'is_tax' );

// Create the structured data for that term archive.
// The term archive metadata doesn't use the post data, but the construction method requires it for now.
$structured_data = $parsely->construct_parsely_metadata( $parsely_options, get_post() );

// Check the required properties exist.
$this->assert_data_has_required_properties( $structured_data );

// The headline should be the term name.
self::assertEquals( 'Custom Taxonomy Term', $structured_data['headline'] );
self::assertEquals( $term_link, $structured_data['url'] );
}
}