Skip to content

Commit

Permalink
fix: phpcs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucy Tomás Cross authored and Lucy Tomás Cross committed Nov 18, 2024
1 parent 1a7ce55 commit e087077
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 24 deletions.
15 changes: 8 additions & 7 deletions wp/headless-wp/includes/classes/Integrations/YoastSEO.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function register() {

// Modify API response to optimise payload by removing the yoast_head and yoast_json_head where not needed.
// Embedded data is not added yet on rest_prepare_{$this->post_type}.
add_filter( 'rest_pre_echo_response' , [ $this, 'optimise_yoast_payload' ], 10, 3 );
add_filter( 'rest_pre_echo_response', [ $this, 'optimise_yoast_payload' ], 10, 3 );
}

/**
Expand Down Expand Up @@ -338,13 +338,14 @@ function ( $presenters ) {
* @param array $result The response data to be served, typically an array.
* @param \WP_REST_Server $server Server instance.
* @param \WP_REST_Request $request Request used to generate the response.
* @param boolean $embed Whether the response should include embedded data.
*
* @return array Modified response data.
*/
public function optimise_yoast_payload( $result, $server, $request, $embed = false ) {

$embed = $embed ?: rest_parse_embed_param( $_GET['_embed'] ?? false );
$embed = $embed ? $embed : filter_var( wp_unslash( $_GET['_embed'] ?? false ), FILTER_VALIDATE_BOOLEAN );

if ( ! $embed || empty( $request->get_param( 'optimizeYoastPayload' ) ) ) {
return $result;
}
Expand Down Expand Up @@ -394,9 +395,9 @@ protected function optimise_yoast_payload_for_taxonomy( &$taxonomy_groups, $requ

if ( $first_post ) {
// Get the queried terms for the taxonomy.
$param = $term_obj['taxonomy'] === 'category' ?
$request->get_param('category') ?? $request->get_param('categories') :
$request->get_param( $term_obj['taxonomy'] );
$param = 'category' === $term_obj['taxonomy'] ?
$request->get_param( 'category' ) ?? $request->get_param( 'categories' ) :
$request->get_param( $term_obj['taxonomy'] );
}

if ( $first_post && ! empty( $param ) ) {
Expand Down Expand Up @@ -438,7 +439,7 @@ protected function optimise_yoast_payload_for_author( &$authors, $request, $firs
$param = is_array( $param ) ? $param : explode( ',', $param );

// If the term slug is not in param array, unset yoast heads.
if ( ! in_array( $author['slug'], $param, true ) && ! in_array( $author['id'], $param, true ) ) {
if ( ! in_array( $author['slug'], $param, true ) && ! in_array( $author['id'], $param, true ) ) {
unset( $author['yoast_head'], $author['yoast_head_json'] );
}
} else {
Expand Down
58 changes: 41 additions & 17 deletions wp/headless-wp/tests/php/tests/TestYoastIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Covers the test for the Yoast integration
*/
class TestYoastIntegration extends WP_Test_REST_TestCase {
/**
/**
* The YoastSEO instance
*
* @var YoastSEO
Expand Down Expand Up @@ -70,8 +70,18 @@ public function set_up() {
* Create posts for testing
*/
protected function create_posts() {
$this->category_id = $this->factory()->term->create( [ 'taxonomy' => 'category', 'slug' => 'test-category' ] );
$this->tag_id = $this->factory()->term->create( [ 'taxonomy' => 'post_tag', 'slug' => 'test-post-tag' ] );
$this->category_id = $this->factory()->term->create(
[
'taxonomy' => 'category',
'slug' => 'test-category',
]
);
$this->tag_id = $this->factory()->term->create(
[
'taxonomy' => 'post_tag',
'slug' => 'test-post-tag',
]
);
$this->author_id = $this->factory()->user->create(
[
'role' => 'editor',
Expand All @@ -82,20 +92,34 @@ protected function create_posts() {
]
);

$random_category_id = $this->factory()->term->create( [ 'taxonomy' => 'category', 'slug' => 'random-category' ] );
$random_tag_id = $this->factory()->term->create( [ 'taxonomy' => 'post_tag', 'slug' => 'random-post-tag' ] );
$random_category_id = $this->factory()->term->create(
[
'taxonomy' => 'category',
'slug' => 'random-category',
]
);
$random_tag_id = $this->factory()->term->create(
[
'taxonomy' => 'post_tag',
'slug' => 'random-post-tag',
]
);

$post_1 = $this->factory()->post->create_and_get( [
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => $this->author_id,
]);
$post_1 = $this->factory()->post->create_and_get(
[
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => $this->author_id,
]
);

$post_2 = $this->factory()->post->create_and_get( [
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => $this->author_id,
]);
$post_2 = $this->factory()->post->create_and_get(
[
'post_type' => 'post',
'post_status' => 'publish',
'post_author' => $this->author_id,
]
);

wp_set_post_terms( $post_1->ID, [ $this->category_id, $random_category_id ], 'category' );
wp_set_post_terms( $post_2->ID, [ $this->category_id, $random_category_id ], 'category' );
Expand All @@ -109,7 +133,7 @@ protected function create_posts() {
* @return void
*/
public function test_optimise_yoast_payload() {

// Perform a REST API request for the posts by category.
$result_category = $this->get_posts_by_with_optimised_response( 'categories', $this->category_id );
$this->assert_yoast_head_in_response( $result_category );
Expand Down Expand Up @@ -179,7 +203,7 @@ protected function assert_yoast_head_in_response( $result ) {
*/
protected function assert_embedded_item( $embedded_obj, $type, $first_post, $id = null ) {

foreach ( $embedded_obj[ $type ] as $group) {
foreach ( $embedded_obj[ $type ] as $group ) {

$items = 'wp:term' !== $type ? [ $group ] : $group;

Expand Down

0 comments on commit e087077

Please sign in to comment.