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

AxeWP / wp-graphql-rank-math / 10645252121

31 Aug 2024 12:31PM UTC coverage: 88.927% (-0.2%) from 89.142%
10645252121

push

github

web-flow
dev: add GraphQL Debug message to response when attempting to use `Page.seo` for the Posts Archive. (#113)

* dev: add debug notice when querying for the posts archive as `Page`

* chore: props

0 of 7 new or added lines in 1 file covered. (0.0%)

1 existing line in 1 file now uncovered.

2586 of 2908 relevant lines covered (88.93%)

11.4 hits per line

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

72.88
/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
declare( strict_types = 1 );
10

11
namespace WPGraphQL\RankMath\Type\WPInterface;
12

13
use GraphQL\Error\UserError;
14
use WPGraphQL\Model\Model;
15
use WPGraphQL\RankMath\Model\ContentNodeSeo;
16
use WPGraphQL\RankMath\Model\ContentTypeSeo;
17
use WPGraphQL\RankMath\Model\TermNodeSeo;
18
use WPGraphQL\RankMath\Model\UserSeo;
19
use WPGraphQL\RankMath\Type\WPInterface\ContentNodeSeo as WPInterfaceContentNodeSeo;
20
use WPGraphQL\RankMath\Utils\Utils;
21
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Abstracts\InterfaceType;
22
use WPGraphQL\RankMath\Vendor\AxeWP\GraphQL\Interfaces\TypeWithInterfaces;
23

24
/**
25
 * Class - NodeWithSeo
26
 */
27
class NodeWithSeo extends InterfaceType implements TypeWithInterfaces {
28
        /**
29
         * {@inheritDoc}
30
         */
31
        public static function register(): void {
32
                register_graphql_interface_type( static::type_name(), static::get_type_config() );
19✔
33

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

52
                // @todo only apply to ContentTypes that have SEO data.
53

54
                register_graphql_interfaces_to_types( self::type_name(), $types_with_seo );
19✔
55

56
                // Narrow down ContentNode types.
57
                Utils::overload_graphql_field_type( 'ContentNode', 'seo', WPInterfaceContentNodeSeo::get_type_name() );
19✔
58
                // This is necessary because the filter doesn't work for inheritance.
59
                Utils::overload_graphql_field_type( 'HierarchicalContentNode', 'seo', WPInterfaceContentNodeSeo::get_type_name() );
19✔
60
        }
61

62
        /**
63
         * {@inheritDoc}
64
         */
65
        protected static function type_name(): string {
66
                return 'NodeWithRankMathSeo';
19✔
67
        }
68

69
        /**
70
         * {@inheritDoc}
71
         */
72
        public static function get_description(): string {
73
                return __( 'A node with RankMath SEO data.', 'wp-graphql-rank-math' );
19✔
74
        }
75

76
        /**
77
         * {@inheritDoc}
78
         */
79
        public static function get_fields(): array {
80
                return [
19✔
81
                        'seo' => [
19✔
82
                                'type'        => Seo::get_type_name(),
19✔
83
                                'description' => __( 'The RankMath SEO data for the node.', 'wp-graphql-rank-math' ),
19✔
84
                                'resolve'     => static function ( $source ) {
19✔
85
                                        if ( ! $source instanceof Model ) {
7✔
86
                                                return null;
×
87
                                        }
88

89
                                        if ( empty( $source->uri ) ) {
7✔
90
                                                /**
91
                                                 * This can occur when querying the `Posts` page, since the Model "casts" it as a `ContentType` due to the lack of archive support.
92
                                                 *
93
                                                 * @see \WPGraphQL\Model\Post::$uri
94
                                                 */
NEW
95
                                                if ( $source instanceof \WPGraphQL\Model\Post && $source->isPostsPage ) {
×
NEW
96
                                                        graphql_debug(
×
NEW
97
                                                                sprintf(
×
98
                                                                        // translators: %d: The ID of the Post model being queried.
NEW
99
                                                                        esc_html__( 'Post %d is configured as the Posts archive, but is being queried as a `Page`. To get the SEO data, please query the object as a `ContentType` (e.g. via `nodeByUri`).', 'wp-graphql-rank-math' ),
×
NEW
100
                                                                        $source->databaseId,
×
NEW
101
                                                                )
×
NEW
102
                                                        );
×
103
                                                }
UNCOV
104
                                                return null;
×
105
                                        }
106

107
                                        $model = self::get_model_for_node( $source );
7✔
108

109
                                        if ( empty( $model ) ) {
7✔
110
                                                throw new UserError(
×
111
                                                        sprintf(
×
112
                                                                /* translators: %s: The name of the node type */
113
                                                                esc_html__( 'The %s type does not have a corresponding SEO model class.', 'wp-graphql-rank-math' ),
×
114
                                                                esc_html( get_class( $source ) )
×
115
                                                        )
×
116
                                                );
×
117
                                        }
118

119
                                        return $model;
7✔
120
                                },
19✔
121
                        ],
19✔
122
                ];
19✔
123
        }
124

125
        /**
126
         * {@inheritDoc}
127
         */
128
        public static function get_interfaces(): array {
129
                return [ 'Node' ];
19✔
130
        }
131

132
        /**
133
         * Gets the SEO model class for a given node model.
134
         *
135
         * @param \WPGraphQL\Model\Model $node_model The node model.
136
         */
137
        private static function get_model_for_node( Model $node_model ): ?Model {
138
                // A map of the node models to their corresponding SEO model classes.
139
                switch ( true ) {
140
                        case $node_model instanceof \WPGraphQL\Model\Post:
7✔
141
                                $seo_model = new ContentNodeSeo( $node_model->databaseId );
4✔
142
                                break;
4✔
143
                        case $node_model instanceof \WPGraphQL\Model\PostType:
4✔
144
                                $seo_model = new ContentTypeSeo( $node_model->name );
1✔
145
                                break;
1✔
146
                        case $node_model instanceof \WPGraphQL\Model\Term:
3✔
147
                                $seo_model = new TermNodeSeo( $node_model->databaseId );
2✔
148
                                break;
2✔
149
                        case $node_model instanceof \WPGraphQL\Model\User:
1✔
150
                                $seo_model = new UserSeo( $node_model->databaseId );
1✔
151
                                break;
1✔
152
                        default:
153
                                $seo_model = null;
×
154
                }
155

156
                /**
157
                 * Filter the SEO model class used for a given node model.
158
                 *
159
                 * @since 0.0.8
160
                 *
161
                 * @param \WPGraphQL\Model\Model|null $seo_model The SEO model class to use.
162
                 * @param \WPGraphQL\Model\Model $node_model The Model for the node.
163
                 */
164
                $seo_model = apply_filters( 'graphql_seo_resolved_model', $seo_model, $node_model );
7✔
165

166
                return $seo_model;
7✔
167
        }
168
}
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