• 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

64.36
/src/Model/ContentNodeSeo.php
1
<?php
2
/**
3
 * The SEO model for ContentNode 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 RankMath\Helper as RMHelper;
15
use WPGraphQL;
16

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

28
        /**
29
         * The database id for the current object.
30
         *
31
         * @var int
32
         */
33
        protected int $database_id;
34

35
        /**
36
         * The settings prefix.
37
         *
38
         * @var string
39
         */
40
        protected string $prefix;
41

42
        /**
43
         * Constructor.
44
         *
45
         * @param int $post_id .
46
         * @throws \GraphQL\Error\Error .
47
         */
48
        public function __construct( int $post_id ) {
49
                $object = get_post( $post_id );
3✔
50
                if ( null === $object ) {
3✔
51
                        throw new Error(
×
52
                                sprintf(
×
53
                                        // translators: post id .
54
                                        esc_html__( 'Invalid post id %d passed to ContentNodeSeo model.', 'wp-graphql-rank-math' ),
×
55
                                        absint( $post_id ),
×
56
                                )
×
57
                        );
×
58
                }
59

60
                $this->database_id = $object->ID;
3✔
61

62
                parent::__construct( $object );
3✔
63
        }
64

65
        /**
66
         * {@inheritDoc}
67
         */
68
        public function setup(): void {
69
                global $wp_query, $post;
3✔
70

71
                /**
72
                 * Store the global post before overriding
73
                 */
74
                $this->global_post = $post;
3✔
75

76
                // Bail early if this is not a post.
77
                if ( ! $this->data instanceof \WP_Post ) {
3✔
78
                        return;
×
79
                }
80

81
                /**
82
                 * Set the resolving post to the global $post. That way any filters that
83
                 * might be applied when resolving fields can rely on global post and
84
                 * post data being set up.
85
                 */
86
                $id        = $this->data->ID;
3✔
87
                $post_type = $this->data->post_type;
3✔
88
                $post_name = $this->data->post_name;
3✔
89
                $data      = $this->data;
3✔
90

91
                if ( 'revision' === $this->data->post_type ) {
3✔
92
                        $id     = $this->data->post_parent;
1✔
93
                        $parent = get_post( $this->data->post_parent );
1✔
94
                        if ( empty( $parent ) ) {
1✔
95
                                $this->fields = [];
×
96
                                return;
×
97
                        }
98
                        $post_type = $parent->post_type;
1✔
99
                        $post_name = $parent->post_name;
1✔
100
                        $data      = $parent;
1✔
101
                }
102

103
                /**
104
                 * Clear out existing postdata
105
                 */
106
                $wp_query->reset_postdata();
3✔
107

108
                /**
109
                 * Parse the query to tell WordPress how to
110
                 * setup global state
111
                 */
112
                switch ( $post_type ) {
113
                        case 'post':
3✔
114
                                $wp_query->parse_query(
3✔
115
                                        [
3✔
116
                                                'page' => '',
3✔
117
                                                'p'    => $id,
3✔
118
                                        ]
3✔
119
                                );
3✔
120
                                break;
3✔
121
                        case 'page':
×
122
                                $wp_query->parse_query(
×
123
                                        [
×
124
                                                'page'     => '',
×
125
                                                'pagename' => $post_name,
×
126
                                        ]
×
127
                                );
×
128
                                break;
×
129
                        case 'attachment':
×
130
                                $wp_query->parse_query( [ 'attachment' => $post_name ] );
×
131
                                break;
×
132
                        default:
133
                                $wp_query->parse_query(
×
134
                                        [
×
135
                                                $post_type  => $post_name,
×
136
                                                'post_type' => $post_type,
×
137
                                                'name'      => $post_name,
×
138
                                        ]
×
139
                                );
×
140
                                break;
×
141
                }
142

143
                $wp_query->setup_postdata( $data );
3✔
144
                $GLOBALS['post']             = $data; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
3✔
145
                $wp_query->queried_object    = get_post( $this->data->ID );
3✔
146
                $wp_query->queried_object_id = $this->data->ID;
3✔
147

148
                parent::setup();
3✔
149
        }
150

151
        /**
152
         * {@inheritDoc}
153
         */
154
        protected function init() {
155
                if ( empty( $this->fields ) ) {
3✔
156
                        parent::init();
3✔
157

158
                        $this->fields = array_merge(
3✔
159
                                $this->fields,
3✔
160
                                [
3✔
161
                                        'breadcrumbTitle' => function (): ?string {
3✔
162
                                                $title = $this->get_meta( 'breadcrumb_title', '', get_the_title( $this->database_id ) );
3✔
163

164
                                                return ! empty( $title ) ? html_entity_decode( $title, ENT_QUOTES ) : null;
3✔
165
                                        },
3✔
166
                                        'isPillarContent' => fn (): bool => ! empty( $this->get_meta( 'pillar_content' ) ),
3✔
167
                                        'seoScore'        => fn () => [
3✔
168
                                                'hasFrontendScore' => static fn (): bool => rank_math()->frontend_seo_score->score_enabled(),
3✔
169
                                                'badgeHtml'        => static function (): ?string {
3✔
170
                                                        $output = rank_math_get_seo_score();
2✔
171
                                                        $output = ! empty( $output ) ? str_replace( [ "\n", "\t", "\r" ], '', $output ) : null;
2✔
172

173
                                                        return ! empty( $output ) ? $output : null;
2✔
174
                                                        },
3✔
175
                                                'rating'           => function (): ?string {
3✔
176
                                                        $score = rank_math()->frontend_seo_score->get_score( $this->database_id );
2✔
177

178
                                                        return rank_math()->frontend_seo_score->get_rating( (int) $score ) ?: null;
2✔
179
                                                },
3✔
180
                                                'score'            => fn (): int => (int) rank_math()->frontend_seo_score->get_score( $this->database_id ),
3✔
181
                                        ],
3✔
182
                                ]
3✔
183
                        );
3✔
184
                }
185
        }
186

187
        /**
188
         * {@inheritDoc}
189
         */
190
        protected function get_breadcrumbs(): ?array {
191
                $breadcrumbs = parent::get_breadcrumbs();
2✔
192

193
                if ( empty( $breadcrumbs ) ) {
2✔
194
                        return null;
×
195
                }
196

197
                $remove_title = ( is_single( $this->database_id ) || is_page( $this->database_id ) ) && RMHelper::get_settings( 'general.breadcrumbs_remove_post_title' );
2✔
198

199
                if ( $remove_title ) {
2✔
200
                        array_pop( $breadcrumbs );
×
201
                }
202

203
                return ! empty( $breadcrumbs ) ? $breadcrumbs : null;
2✔
204
        }
205

206
        /**
207
         * {@inheritDoc}
208
         */
209
        public function get_object_type(): string {
UNCOV
210
                $post_types        = WPGraphQL::get_allowed_post_types( 'objects' );
×
UNCOV
211
                $current_post_type = $this->data->post_type;
×
212

213
                // If this is a revision, get the post type of the parent.
UNCOV
214
                if ( 'revision' === $current_post_type ) {
×
UNCOV
215
                        $current_post_type = get_post_type( $this->data->post_parent );
×
216
                }
217

UNCOV
218
                return $post_types[ $current_post_type ]->graphql_single_name;
×
219
        }
220

221
        /**
222
         * {@inheritDoc}
223
         *
224
         * @throws \GraphQL\Error\UserError If no post permalink.
225
         */
226
        protected function get_object_url(): string {
227
                $permalink = get_permalink( $this->database_id );
3✔
228

229
                if ( false === $permalink ) {
3✔
230
                        throw new UserError( esc_html__( 'There is no URI for the provided content node', 'wp-graphql-rank-math' ) );
×
231
                }
232

233
                return $permalink;
3✔
234
        }
235
}
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