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

wp-graphql / wp-graphql / 15710056976

17 Jun 2025 02:27PM UTC coverage: 84.17% (-0.1%) from 84.287%
15710056976

push

github

actions-user
release: merge develop into master for v2.3.3

15925 of 18920 relevant lines covered (84.17%)

258.66 hits per line

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

97.96
/src/Model/Term.php
1
<?php
2

3
namespace WPGraphQL\Model;
4

5
use GraphQLRelay\Relay;
6
use WP_Taxonomy;
7
use WP_Term;
8

9
/**
10
 * Class Term - Models data for Terms
11
 *
12
 * @property ?int    $count
13
 * @property ?int    $databaseId
14
 * @property ?string $description
15
 * @property ?string $id
16
 * @property ?string $link
17
 * @property ?string $name
18
 * @property ?string $parentId
19
 * @property ?int    $parentDatabaseId
20
 * @property ?string $slug
21
 * @property ?string $taxonomyName
22
 * @property ?int    $termGroupId
23
 * @property ?int    $termTaxonomyId
24
 *
25
 * Aliases:
26
 * @property ?int     $term_id
27
 *
28
 * @package WPGraphQL\Model
29
 *
30
 * @extends \WPGraphQL\Model\Model<\WP_Term>
31
 */
32
class Term extends Model {
33
        /**
34
         * Stores the taxonomy object for the term being modeled
35
         *
36
         * @var \WP_Taxonomy|null $taxonomy_object
37
         */
38
        protected $taxonomy_object;
39

40
        /**
41
         * The global Post instance
42
         *
43
         * @var \WP_Post
44
         */
45
        protected $global_post;
46

47
        /**
48
         * Term constructor.
49
         *
50
         * @param \WP_Term $term The incoming WP_Term object that needs modeling
51
         *
52
         * @return void
53
         */
54
        public function __construct( WP_Term $term ) {
116✔
55
                $this->data            = $term;
116✔
56
                $taxonomy              = get_taxonomy( $term->taxonomy );
116✔
57
                $this->taxonomy_object = $taxonomy instanceof WP_Taxonomy ? $taxonomy : null;
116✔
58
                parent::__construct();
116✔
59
        }
60

61
        /**
62
         * {@inheritDoc}
63
         */
64
        public function setup() {
116✔
65
                global $wp_query, $post;
116✔
66

67
                /**
68
                 * Store the global post before overriding
69
                 */
70
                $this->global_post = $post;
116✔
71

72
                if ( $this->data instanceof WP_Term ) {
116✔
73

74
                        /**
75
                         * Reset global post
76
                         */
77
                        $GLOBALS['post'] = get_post( 0 ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride
116✔
78

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

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

102
        /**
103
         * {@inheritDoc}
104
         */
105
        public function tear_down() {
116✔
106
                $GLOBALS['post'] = $this->global_post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
116✔
107
                wp_reset_postdata();
116✔
108
        }
109

110
        /**
111
         * {@inheritDoc}
112
         */
113
        protected function init() {
116✔
114
                if ( empty( $this->fields ) ) {
116✔
115
                        $this->fields = [
116✔
116
                                'count'                    => function () {
116✔
117
                                        return ! empty( $this->data->count ) ? absint( $this->data->count ) : null;
2✔
118
                                },
116✔
119
                                'databaseId'               => function () {
116✔
120
                                        return ( ! empty( $this->data->term_id ) ) ? absint( $this->data->term_id ) : null;
116✔
121
                                },
116✔
122
                                'description'              => function () {
116✔
123
                                        return ! empty( $this->data->description ) ? $this->html_entity_decode( $this->data->description, 'description' ) : null;
16✔
124
                                },
116✔
125
                                'enqueuedScriptsQueue'     => static function () {
116✔
126
                                        global $wp_scripts;
16✔
127
                                        $wp_scripts->reset();
16✔
128
                                        do_action( 'wp_enqueue_scripts' );
16✔
129
                                        $queue = $wp_scripts->queue;
16✔
130
                                        $wp_scripts->reset();
16✔
131
                                        $wp_scripts->queue = [];
16✔
132

133
                                        return $queue;
16✔
134
                                },
116✔
135
                                'enqueuedStylesheetsQueue' => static function () {
116✔
136
                                        global $wp_styles;
16✔
137
                                        do_action( 'wp_enqueue_scripts' );
16✔
138
                                        $queue = $wp_styles->queue;
16✔
139
                                        $wp_styles->reset();
16✔
140
                                        $wp_styles->queue = [];
16✔
141

142
                                        return $queue;
16✔
143
                                },
116✔
144
                                'id'                       => function () {
116✔
145
                                        return ( ! empty( $this->data->taxonomy ) && ! empty( $this->databaseId ) ) ? Relay::toGlobalId( 'term', (string) $this->databaseId ) : null;
114✔
146
                                },
116✔
147
                                'link'                     => function () {
116✔
148
                                        $link = get_term_link( $this->data->term_id );
15✔
149

150
                                        return ! is_wp_error( $link ) ? $link : null;
15✔
151
                                },
116✔
152
                                'name'                     => function () {
116✔
153
                                        return ! empty( $this->data->name ) ? $this->html_entity_decode( $this->data->name, 'name', true ) : null;
48✔
154
                                },
116✔
155
                                'parentDatabaseId'         => function () {
116✔
156
                                        return ! empty( $this->data->parent ) ? $this->data->parent : null;
10✔
157
                                },
116✔
158
                                'parentId'                 => function () {
116✔
159
                                        return ! empty( $this->parentDatabaseId ) ? Relay::toGlobalId( 'term', (string) $this->parentDatabaseId ) : null;
×
160
                                },
116✔
161
                                'slug'                     => function () {
116✔
162
                                        return ! empty( $this->data->slug ) ? urldecode( $this->data->slug ) : null;
30✔
163
                                },
116✔
164
                                'taxonomyName'             => function () {
116✔
165
                                        return ! empty( $this->taxonomy_object->name ) ? $this->taxonomy_object->name : null;
34✔
166
                                },
116✔
167
                                'termGroupId'              => function () {
116✔
168
                                        return ! empty( $this->data->term_group ) ? absint( $this->data->term_group ) : null;
1✔
169
                                },
116✔
170
                                'termTaxonomyId'           => function () {
116✔
171
                                        return ! empty( $this->data->term_taxonomy_id ) ? absint( $this->data->term_taxonomy_id ) : null;
1✔
172
                                },
116✔
173
                                'uri'                      => function () {
116✔
174
                                        $link = $this->link;
14✔
175

176
                                        $maybe_url = isset( $link ) ? wp_parse_url( $link ) : null;
14✔
177

178
                                        // If wp_parse_url() returned false, we can assume it's been filtered and just return the link value.
179
                                        if ( false === $maybe_url ) {
14✔
180
                                                return $link;
×
181
                                        }
182

183
                                        // Replace the home_url in the link in order to return a relative uri.
184
                                        // For subdirectory multisites, this replaces the home_url which includes the subdirectory.
185
                                        return ! empty( $link ) ? str_ireplace( home_url(), '', $link ) : null;
14✔
186
                                },
116✔
187

188
                                // Aliases.
189
                                'term_id'                  => function () {
116✔
190
                                        return $this->databaseId;
1✔
191
                                },
116✔
192
                        ];
116✔
193

194
                        // Deprecated.
195
                        if ( isset( $this->taxonomy_object, $this->taxonomy_object->graphql_single_name ) ) {
116✔
196
                                $type_id                  = $this->taxonomy_object->graphql_single_name . 'Id';
116✔
197
                                $this->fields[ $type_id ] = absint( $this->data->term_id );
116✔
198
                        }
199
                }
200
        }
201
}
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