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

AxeWP / wp-graphql-rank-math / 9046069163

11 May 2024 07:57PM UTC coverage: 90.381% (-0.5%) from 90.848%
9046069163

push

github

web-flow
feat!: Narrow `seo` field types to their implementations. (#96)

* feat!: Narrow `seo` field types to their implementations.

* chore: rename to overload_graphql_field_type()

* fix: overload `HierarchicalContentNode.seo` directly

23 of 23 new or added lines in 3 files covered. (100.0%)

15 existing lines in 3 files now uncovered.

2584 of 2859 relevant lines covered (90.38%)

11.03 hits per line

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

75.44
/src/Model/TermNodeSeo.php
1
<?php
2
/**
3
 * The SEO model for TermNode objects.
4
 *
5
 * @package \WPGraphQL\RankMath\Model
6
 */
7

8
declare( strict_types = 1 );
9

10
namespace WPGraphQL\RankMath\Model;
11

12
use GraphQL\Error\Error;
13
use GraphQL\Error\UserError;
14
use WPGraphQL;
15
use WP_Term;
16

17
/**
18
 * Class - TermNodeSeo
19
 */
20
class TermNodeSeo extends Seo {
21
        /**
22
         * Stores the incoming post data
23
         *
24
         * @var \WP_Term $data
25
         */
26
        protected $data;
27

28
        /**
29
         * The settings prefix
30
         *
31
         * @var string
32
         */
33
        protected string $prefix;
34

35
        /**
36
         * Constructor.
37
         *
38
         * @param int $term_id .
39
         * @throws \GraphQL\Error\Error .
40
         */
41
        public function __construct( int $term_id ) {
42
                /** @var ?\WP_Term $object */
43
                $object = get_term( $term_id );
2✔
44
                if ( null === $object ) {
2✔
45
                        throw new Error(
×
46
                                sprintf(
×
47
                                        // translators: post id .
48
                                        esc_html__( 'Invalid term id %d passed to TermNodeSeo model.', 'wp-graphql-rank-math' ),
×
49
                                        absint( $term_id ),
×
50
                                )
×
51
                        );
×
52
                }
53

54
                $this->database_id = $object->term_id;
2✔
55

56
                parent::__construct( $object );
2✔
57
        }
58

59
        /**
60
         * {@inheritDoc}
61
         */
62
        public function setup(): void {
63
                global $wp_query, $post;
2✔
64

65
                // Store the global post before overriding.
66
                $this->global_post = $post;
2✔
67

68
                // Denylist globally-cached replacements.
69
                add_filter( 'rank_math/replacements/non_cacheable', [ $this, 'non_cacheable_replacements' ] );
2✔
70

71
                if ( $this->data instanceof WP_Term ) {
2✔
72
                        /**
73
                         * Reset global post
74
                         */
75
                        $GLOBALS['post'] = get_post( 0 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride
2✔
76

77
                        /**
78
                         * Parse the query to tell WordPress
79
                         * how to setup global state
80
                         */
81
                        if ( 'category' === $this->data->taxonomy ) {
2✔
82
                                $wp_query->parse_query(
1✔
83
                                        [
1✔
84
                                                'category_name' => $this->data->slug,
1✔
85
                                        ]
1✔
86
                                );
1✔
87
                        } elseif ( 'post_tag' === $this->data->taxonomy ) {
1✔
88
                                $wp_query->parse_query(
×
89
                                        [
×
90
                                                'tag' => $this->data->slug,
×
91
                                        ]
×
92
                                );
×
93
                        } else {
94
                                $wp_query->parse_query(
1✔
95
                                        [
1✔
96
                                                $this->data->taxonomy => $this->data->slug,
1✔
97
                                        ]
1✔
98
                                );
1✔
99
                        }
100

101
                        $wp_query->queried_object_id = $this->data->term_id;
2✔
102
                        $wp_query->queried_object    = $this->data;
2✔
103
                }
104

105
                parent::setup();
2✔
106
        }
107

108
        /**
109
         * Reset global state after the model fields
110
         * have been generated
111
         *
112
         * @return void
113
         */
114
        public function tear_down() {
115
                remove_filter( 'rank_math/replacements/non_cacheable', [ $this, 'non_cacheable_replacements' ] );
2✔
116

117
                $GLOBALS['post'] = $this->global_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
2✔
118

119
                wp_reset_postdata();
2✔
120
        }
121

122
        /**
123
         * {@inheritDoc}
124
         */
125
        protected function init() {
126
                if ( empty( $this->fields ) ) {
2✔
127
                        parent::init();
2✔
128

129
                        $this->fields = array_merge(
2✔
130
                                $this->fields,
2✔
131
                                [
2✔
132
                                        'breadcrumbTitle' => function (): ?string {
2✔
133
                                                $title = $this->get_meta( 'breadcrumb_title', '', $this->data->name );
2✔
134

135
                                                return ! empty( $title ) ? html_entity_decode( $title, ENT_QUOTES ) : null;
2✔
136
                                        },
2✔
137
                                ]
2✔
138
                        );
2✔
139
                }
140
        }
141

142
        /**
143
         * {@inheritDoc}
144
         */
145
        public function get_object_type(): string {
UNCOV
146
                $taxonomies = WPGraphQL::get_allowed_taxonomies( 'objects' );
×
147

UNCOV
148
                return $taxonomies[ $this->data->taxonomy ]->graphql_single_name;
×
149
        }
150

151
        /**
152
         * {@inheritDoc}
153
         *
154
         * @throws \GraphQL\Error\UserError If no valid term link.
155
         */
156
        protected function get_object_url(): string {
157
                $term_link = get_term_link( $this->database_id );
2✔
158

159
                if ( is_wp_error( $term_link ) ) {
2✔
160
                        throw new UserError( esc_html( $term_link->get_error_message() ) );
×
161
                }
162
                return $term_link;
2✔
163
        }
164

165
        /**
166
         * Adds SEO keys that should not be cached by the Rank Math replacements cache.
167
         *
168
         * @uses rank_math/replacements/non_cacheable
169
         *
170
         * @param string[] $args The keys that should not be cached.
171
         *
172
         * @return string[]
173
         */
174
        public function non_cacheable_replacements( array $args ): array {
175
                // This is necessary because RM (as of 1.0.117) does not set `term_description` to nocache.
176
                $args[] = 'term_description';
1✔
177

178
                return $args;
1✔
179
        }
180
}
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

© 2026 Coveralls, Inc