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

feat: map user social meta fields #76

Merged
merged 3 commits into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions src/Model/UserSeo.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ protected function init() {
return ! empty( $title ) ? html_entity_decode( $title, ENT_QUOTES ) : null;
},
'ID' => fn (): int => $this->database_id,
'social' => fn (): array => $this->meta_social_fields(),
]
);
}
Expand All @@ -145,4 +146,21 @@ public function get_object_type(): string {
protected function get_object_url(): string {
return get_author_posts_url( $this->database_id );
}

/**
* Resolve meta social fields.
*
* @return array<string, mixed>
*/
private function meta_social_fields(): array {
$facebook_profile_url = get_user_meta( $this->database_id, 'facebook', true );
$twitter_user_name = get_user_meta( $this->database_id, 'twitter', true );
$additional_profiles = get_user_meta( $this->database_id, 'additional_profile_urls', true );

return [
'facebookProfileUrl' => ! empty( $facebook_profile_url ) ? $facebook_profile_url : null,
'twitterUserName' => ! empty( $twitter_user_name ) ? $twitter_user_name : null,
'additionalProfiles' => ! empty( $additional_profiles ) ? explode( ',', $additional_profiles ) : null,
];
}
}
42 changes: 42 additions & 0 deletions src/Type/WPInterface/SocialSeo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Interface for Social Seo fields.
*
* @package WPGraphQL\RankMath\Type\WPInterface
*/

namespace WPGraphQL\RankMath\Type\WPInterface;

use WPGraphQL\RankMath\Type\WPObject\UserSocialMeta;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Abstracts\InterfaceType;

/**
* Class - SocialSeo
*/
class SocialSeo extends InterfaceType {
/**
* {@inheritDoc}
*/
protected static function type_name(): string {
return 'SocialSeo';
}

/**
* {@inheritDoc}
*/
public static function get_description(): string {
return __( 'The social seo data.', 'wp-graphql-rank-math' );
}

/**
* {@inheritDoc}
*/
public static function get_fields(): array {
return [
'social' => [
'type' => UserSocialMeta::get_type_name(),
'description' => __( 'The social meta properties.', 'wp-graphql-rank-math' ),
],
];
}
}
3 changes: 2 additions & 1 deletion src/Type/WPObject/SeoObjects.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use WPGraphQL;
use WPGraphQL\RankMath\Type\WPInterface\ContentNodeSeo;
use WPGraphQL\RankMath\Type\WPInterface\Seo;
use WPGraphQL\RankMath\Type\WPInterface\SocialSeo;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\Registrable;
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Traits\TypeNameTrait;

Expand Down Expand Up @@ -83,7 +84,7 @@ public static function register(): void {
graphql_format_type_name( 'RankMathUserSeo' ),
[
'description' => __( 'The user object SEO data', 'wp-graphql-rank-math' ),
'interfaces' => [ Seo::get_type_name() ],
'interfaces' => [ Seo::get_type_name(), SocialSeo::get_type_name() ],
'fields' => [],
'eagerlyLoadType' => true,
]
Expand Down
49 changes: 49 additions & 0 deletions src/Type/WPObject/UserSocialMeta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* The Rank Math UserSocialMeta GraphQL Object.
*
* @package WPGraphQL\RankMath\Type\WPObject
*/

namespace WPGraphQL\RankMath\Type\WPObject;

use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Abstracts\ObjectType;

/**
* Class - UserSocialMeta
*/
class UserSocialMeta extends ObjectType {
/**
* {@inheritDoc}
*/
protected static function type_name(): string {
return 'UserSocialMeta';
}

/**
* {@inheritDoc}
*/
public static function get_description(): string {
return __( 'The RankMath SEO User Social data', 'wp-graphql-rank-math' );
}

/**
* {@inheritDoc}
*/
public static function get_fields(): array {
return [
'facebookProfileUrl' => [
'type' => 'String',
'description' => __( 'The complete Facebook profile URL.', 'wp-graphql-rank-math' ),
],
'twitterUserName' => [
'type' => 'String',
'description' => __( 'Twitter Username of the user.', 'wp-graphql-rank-math' ),
],
'additionalProfiles' => [
'type' => [ 'list_of' => 'String' ],
'description' => __( 'A list of additional social profiles.', 'wp-graphql-rank-math' ),
],
];
}
}
2 changes: 2 additions & 0 deletions src/TypeRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public static function interfaces(): array {
WPInterface\Seo::class,
WPInterface\ContentNodeSeo::class,
WPInterface\NodeWithSeo::class,
WPInterface\SocialSeo::class,
];

/**
Expand All @@ -163,6 +164,7 @@ public static function objects(): array {
WPObject\SeoScore::class,
WPObject\JsonLd::class,
WPObject\Breadcrumbs::class,
WPObject\UserSocialMeta::class,

// Open Graph.
WPObject\OpenGraph\Article::class,
Expand Down
11 changes: 10 additions & 1 deletion tests/functional/UserSeoQueryCept.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@
title
}
}
... on RankMathUserSeo {
social {
additionalProfiles
facebookProfileUrl
twitterUserName
}
}
}
}
}
Expand Down Expand Up @@ -94,7 +101,9 @@
$I->assertEquals( 'SUMMARY_LARGE_IMAGE', $response_array['data']['user']['seo']['openGraph']['twitterMeta']['card'] );
$I->assertEmpty( $response_array['data']['user']['seo']['openGraph']['twitterMeta']['description'] );
$I->assertEquals( 'testuser - Test', $response_array['data']['user']['seo']['openGraph']['twitterMeta']['title'] );

$I->assertEmpty( $response_array['data']['user']['seo']['social']['additionalProfiles'] );
$I->assertEmpty( $response_array['data']['user']['seo']['social']['facebookProfileUrl'] );
$I->assertEmpty( $response_array['data']['user']['seo']['social']['twitterUserName'] );



Expand Down
15 changes: 15 additions & 0 deletions tests/wpunit/UserSeoQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ public function testUserSeo() {
}
robots
title
... on RankMathUserSeo {
social {
additionalProfiles
facebookProfileUrl
twitterUserName
}
}
}
}
}
Expand Down Expand Up @@ -103,6 +110,14 @@ public function testUserSeo() {
]
),
$this->expectedField( 'title', 'display - Test' ),
$this->expectedObject(
'social',
[
$this->expectedField( 'additionalProfiles', static::IS_NULL ),
$this->expectedField( 'facebookProfileUrl', static::IS_NULL ),
$this->expectedField( 'twitterUserName', static::IS_NULL ),
]
),
]
),
]
Expand Down
Loading