From 68c31b84bdd654febf24ebd5fd63e4ee1dd001d6 Mon Sep 17 00:00:00 2001 From: Carmine Colicino Date: Tue, 19 Mar 2024 19:00:15 +0100 Subject: [PATCH] feat: map user social meta fields --- src/Model/UserSeo.php | 18 ++++++++++ src/Type/WPInterface/SocialSeo.php | 42 +++++++++++++++++++++++ src/Type/WPObject/SeoObjects.php | 3 +- src/Type/WPObject/UserSocialMeta.php | 49 +++++++++++++++++++++++++++ src/TypeRegistry.php | 2 ++ tests/functional/UserSeoQueryCept.php | 11 +++++- tests/wpunit/UserSeoQueryTest.php | 15 ++++++++ 7 files changed, 138 insertions(+), 2 deletions(-) create mode 100644 src/Type/WPInterface/SocialSeo.php create mode 100644 src/Type/WPObject/UserSocialMeta.php diff --git a/src/Model/UserSeo.php b/src/Model/UserSeo.php index 87de0b1..673b9b7 100644 --- a/src/Model/UserSeo.php +++ b/src/Model/UserSeo.php @@ -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(), ] ); } @@ -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 + */ + 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, + ]; + } } diff --git a/src/Type/WPInterface/SocialSeo.php b/src/Type/WPInterface/SocialSeo.php new file mode 100644 index 0000000..90e1649 --- /dev/null +++ b/src/Type/WPInterface/SocialSeo.php @@ -0,0 +1,42 @@ + [ + 'type' => UserSocialMeta::get_type_name(), + 'description' => __( 'The social meta properties.', 'wp-graphql-rank-math' ), + ], + ]; + } +} diff --git a/src/Type/WPObject/SeoObjects.php b/src/Type/WPObject/SeoObjects.php index a8f8476..82631b3 100644 --- a/src/Type/WPObject/SeoObjects.php +++ b/src/Type/WPObject/SeoObjects.php @@ -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; @@ -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, ] diff --git a/src/Type/WPObject/UserSocialMeta.php b/src/Type/WPObject/UserSocialMeta.php new file mode 100644 index 0000000..8ee0816 --- /dev/null +++ b/src/Type/WPObject/UserSocialMeta.php @@ -0,0 +1,49 @@ + [ + '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' ), + ], + ]; + } +} diff --git a/src/TypeRegistry.php b/src/TypeRegistry.php index 697ead3..a9acd18 100644 --- a/src/TypeRegistry.php +++ b/src/TypeRegistry.php @@ -140,6 +140,7 @@ public static function interfaces(): array { WPInterface\Seo::class, WPInterface\ContentNodeSeo::class, WPInterface\NodeWithSeo::class, + WPInterface\SocialSeo::class, ]; /** @@ -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, diff --git a/tests/functional/UserSeoQueryCept.php b/tests/functional/UserSeoQueryCept.php index 54de178..30b3fad 100644 --- a/tests/functional/UserSeoQueryCept.php +++ b/tests/functional/UserSeoQueryCept.php @@ -56,6 +56,13 @@ title } } + ... on RankMathUserSeo { + social { + additionalProfiles + facebookProfileUrl + twitterUserName + } + } } } } @@ -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'] ); diff --git a/tests/wpunit/UserSeoQueryTest.php b/tests/wpunit/UserSeoQueryTest.php index 9e6aa3e..d68ee6d 100644 --- a/tests/wpunit/UserSeoQueryTest.php +++ b/tests/wpunit/UserSeoQueryTest.php @@ -65,6 +65,13 @@ public function testUserSeo() { } robots title + ... on RankMathUserSeo { + social { + additionalProfiles + facebookProfileUrl + twitterUserName + } + } } } } @@ -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 ), + ] + ), ] ), ]