• 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

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

8
namespace WPGraphQL\RankMath\Model;
9

10
use GraphQL\Error\Error;
11
use GraphQL\Error\UserError;
12
use WPGraphQL;
13
use WP_Term;
14

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

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

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

52
                $this->database_id = $object->term_id;
2✔
53

54
                parent::__construct( $object );
2✔
55
        }
56

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

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

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

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

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

99
                        $wp_query->queried_object_id = $this->data->term_id;
2✔
100
                        $wp_query->queried_object    = get_term( $this->data->term_id, $this->data->taxonomy );
2✔
101
                }
102

103
                parent::setup();
2✔
104
        }
105

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

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

117
                wp_reset_postdata();
2✔
118
        }
119

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

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

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

140
        /**
141
         * {@inheritDoc}
142
         */
143
        public function get_object_type(): string {
144
                $taxonomies = WPGraphQL::get_allowed_taxonomies( 'objects' );
2✔
145

146
                return $taxonomies[ $this->data->taxonomy ]->graphql_single_name;
2✔
147
        }
148

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

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

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

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