• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

AxeWP / wp-graphql-rank-math / 6257335822

21 Sep 2023 04:53AM UTC coverage: 92.176%. First build
6257335822

Pull #59

github

web-flow
Merge e6916cad3 into a7f39383c
Pull Request #59: chore: Update dev-deps and lint

24 of 24 new or added lines in 11 files covered. (100.0%)

2533 of 2748 relevant lines covered (92.18%)

11.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/Type/WPInterface/NodeWithSeo.php
1
<?php
2
/**
3
 * Interface for a Node with SEO data.
4
 *
5
 * @package WPGraphQL\RankMath\Type\WPInterface
6
 * @since 0.0.8
7
 */
8

9
namespace WPGraphQL\RankMath\Type\WPInterface;
10

11
use GraphQL\Error\UserError;
12
use WPGraphQL\Model\Model;
13
use WPGraphQL\RankMath\Model\ContentNodeSeo;
14
use WPGraphQL\RankMath\Model\ContentTypeSeo;
15
use WPGraphQL\RankMath\Model\TermNodeSeo;
16
use WPGraphQL\RankMath\Model\UserSeo;
17
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Abstracts\InterfaceType;
18
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\TypeWithInterfaces;
19

20
/**
21
 * Class - NodeWithSeo
22
 */
23
class NodeWithSeo extends InterfaceType implements TypeWithInterfaces {
24
        /**
25
         * {@inheritDoc}
26
         */
27
        public static function register(): void {
28
                register_graphql_interface_type( static::type_name(), static::get_type_config() );
18✔
29

30
                /**
31
                 * Filters the GraphQL types that have SEO data.
32
                 * This is used to register the NodeWithSeo interface to the types.
33
                 *
34
                 * @since 0.0.8
35
                 *
36
                 * @param array $types_with_seo The types that have SEO data.
37
                 */
38
                $types_with_seo = apply_filters(
18✔
39
                        'graphql_seo_types_with_seo',
18✔
40
                        [
18✔
41
                                'User',
18✔
42
                                'TermNode',
18✔
43
                                'ContentType',
18✔
44
                                'ContentNode',
18✔
45
                        ]
18✔
46
                );
18✔
47

48
                // @todo only apply to ContentTypes that have SEO data.
49

50
                register_graphql_interfaces_to_types( self::type_name(), $types_with_seo );
18✔
51
        }
52

53
        /**
54
         * {@inheritDoc}
55
         */
56
        protected static function type_name(): string {
57
                return 'NodeWithRankMathSeo';
18✔
58
        }
59

60
        /**
61
         * {@inheritDoc}
62
         */
63
        public static function get_description(): string {
64
                return __( 'A node with RankMath SEO data.', 'wp-graphql-rank-math' );
18✔
65
        }
66

67
        /**
68
         * {@inheritDoc}
69
         */
70
        public static function get_fields(): array {
71
                return [
18✔
72
                        'seo' => [
18✔
73
                                'type'        => Seo::get_type_name(),
18✔
74
                                'description' => __( 'The RankMath SEO data for the node.', 'wp-graphql-rank-math' ),
18✔
75
                                'resolve'     => static function ( $source ) {
18✔
76
                                        if ( ! $source instanceof Model ) {
6✔
77
                                                return null;
×
78
                                        }
79

80
                                        $model = self::get_model_for_node( $source );
6✔
81

82
                                        if ( empty( $model ) ) {
6✔
83
                                                throw new UserError(
×
84
                                                        sprintf(
×
85
                                                                /* translators: %s: The name of the node type */
86
                                                                esc_html__( 'The %s type does not have a corresponding SEO model class.', 'wp-graphql-rank-math' ),
×
87
                                                                esc_html( get_class( $source ) )
×
88
                                                        )
×
89
                                                );
×
90
                                        }
91

92
                                        return $model;
6✔
93
                                },
18✔
94
                        ],
18✔
95
                ];
18✔
96
        }
97

98
        /**
99
         * {@inheritDoc}
100
         */
101
        public static function get_interfaces(): array {
102
                return [ 'Node' ];
18✔
103
        }
104

105
        /**
106
         * Gets the SEO model class for a given node model.
107
         *
108
         * @param \WPGraphQL\Model\Model $node_model The node model.
109
         */
110
        private static function get_model_for_node( Model $node_model ): ?Model {
111
                // A map of the node models to their corresponding SEO model classes.
112
                switch ( true ) {
113
                        case $node_model instanceof \WPGraphQL\Model\Post:
6✔
114
                                $seo_model = new ContentNodeSeo( $node_model->ID );
2✔
115
                                break;
2✔
116
                        case $node_model instanceof \WPGraphQL\Model\PostType:
4✔
117
                                $seo_model = new ContentTypeSeo( $node_model->name );
1✔
118
                                break;
1✔
119
                        case $node_model instanceof \WPGraphQL\Model\Term:
3✔
120
                                $seo_model = new TermNodeSeo( $node_model->databaseId );
2✔
121
                                break;
2✔
122
                        case $node_model instanceof \WPGraphQL\Model\User:
1✔
123
                                $seo_model = new UserSeo( $node_model->databaseId );
1✔
124
                                break;
1✔
125
                        default:
126
                                $seo_model = null;
×
127
                }
128

129
                /**
130
                 * Filter the SEO model class used for a given node model.
131
                 *
132
                 * @since 0.0.8
133
                 *
134
                 * @param \WPGraphQL\Model\Model|null $seo_model The SEO model class to use.
135
                 * @param \WPGraphQL\Model\Model $node_model The Model for the node.
136
                 */
137
                $seo_model = apply_filters( 'graphql_seo_resolved_model', $seo_model, $node_model );
6✔
138

139
                return $seo_model;
6✔
140
        }
141
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc