• 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

69.15
/src/Model/ContentNodeSeo.php
1
<?php
2
/**
3
 * The SEO model for ContentNode 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

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

25
        /**
26
         * The database id for the current object.
27
         *
28
         * @var integer
29
         */
30
        protected int $database_id;
31

32
        /**
33
         * The settings prefix.
34
         *
35
         * @var string
36
         */
37
        protected string $prefix;
38

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

57
                $this->database_id = $object->ID;
2✔
58

59
                parent::__construct( $object );
2✔
60
        }
61

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

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

73
                // Bail early if this is not a post.
74
                if ( ! $this->data instanceof \WP_Post ) {
2✔
75
                        return;
×
76
                }
77

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

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

100
                /**
101
                 * Clear out existing postdata
102
                 */
103
                $wp_query->reset_postdata();
2✔
104

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

140
                $wp_query->setup_postdata( $data );
2✔
141
                $GLOBALS['post']             = $data; // phpcs:ignore WordPress.WP.GlobalVariablesOverride
2✔
142
                $wp_query->queried_object    = get_post( $this->data->ID );
2✔
143
                $wp_query->queried_object_id = $this->data->ID;
2✔
144

145
                parent::setup();
2✔
146
        }
147

148
        /**
149
         * {@inheritDoc}
150
         */
151
        protected function init() {
152
                if ( empty( $this->fields ) ) {
2✔
153
                        parent::init();
2✔
154

155
                        $this->fields = array_merge(
2✔
156
                                $this->fields,
2✔
157
                                [
2✔
158
                                        'breadcrumbTitle' => function (): ?string {
2✔
159
                                                $title = $this->get_meta( 'breadcrumb_title', '', get_the_title( $this->database_id ) );
2✔
160

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

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

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

184
        /**
185
         * {@inheritDoc}
186
         */
187
        public function get_object_type(): string {
188
                $post_types        = WPGraphQL::get_allowed_post_types( 'objects' );
2✔
189
                $current_post_type = $this->data->post_type;
2✔
190

191
                // If this is a revision, get the post type of the parent.
192
                if ( 'revision' === $current_post_type ) {
2✔
193
                        $current_post_type = get_post_type( $this->data->post_parent );
1✔
194
                }
195

196
                return $post_types[ $current_post_type ]->graphql_single_name;
2✔
197
        }
198

199
        /**
200
         * {@inheritDoc}
201
         *
202
         * @throws \GraphQL\Error\UserError If no post permalink.
203
         */
204
        protected function get_object_url(): string {
205
                $permalink = get_permalink( $this->database_id );
2✔
206

207
                if ( false === $permalink ) {
2✔
208
                        throw new UserError( esc_html__( 'There is no URI for the provided content node', 'wp-graphql-rank-math' ) );
×
209
                }
210

211
                return $permalink;
2✔
212
        }
213
}
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