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

AxeWP / wp-graphql-rank-math / 5243741106

pending completion
5243741106

push

github

web-flow
feat: Add support for Redirections Module (#52)

* feat: add redirection settings

* ...save-progress

* stash changes

* chore: lint

* dev: get single redirection from db directly

* ci: use mariadb 10.x

* dev: fix cursor comparison

* chore: cleanup

* tests: additional pagination tests

* chore: update deps and changelog

454 of 454 new or added lines in 22 files covered. (100.0%)

2490 of 2727 relevant lines covered (91.31%)

10.74 hits per line

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

92.11
/src/Modules/Redirection/Data/Connection/RedirectionConnectionResolver.php
1
<?php
2
/**
3
 * The Rank Math redirection connection resolver
4
 *
5
 * @package WPGraphQL\RankMath\Modules\Redirection\Data\Connection;
6
 * @since @todo
7
 */
8

9
namespace WPGraphQL\RankMath\Modules\Redirection\Data\Connection;
10

11
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
12
use WPGraphQL\RankMath\Modules\Redirection\Data\Loader\RedirectionsLoader;
13
use WPGraphQL\RankMath\Utils\RMUtils;
14

15
/**
16
 * Class RedirectionConnectionResolver
17
 */
18
class RedirectionConnectionResolver extends AbstractConnectionResolver {
19
        /**
20
         * {@inheritDoc}
21
         *
22
         * @var ?array
23
         */
24
        protected $query;
25

26
        /**
27
         * {@inheritDoc}
28
         */
29
        public function get_loader_name() {
30
                return RedirectionsLoader::$name;
6✔
31
        }
32

33
        /**
34
         * {@inheritDoc}
35
         */
36
        public function get_query_args() {
37
                /**
38
                 * Prepare for later use
39
                 */
40
                $last  = ! empty( $this->args['last'] ) ? $this->args['last'] : null;
6✔
41
                $first = ! empty( $this->args['first'] ) ? $this->args['first'] : null;
6✔
42

43
                $query_args = [];
6✔
44

45
                if ( ! empty( $this->args['where']['search'] ) ) {
6✔
46
                        $query_args['search'] = $this->args['where']['search'];
×
47
                }
48

49
                $query_args['status'] = ! empty( $this->args['where']['status'] ) ? $this->args['where']['status'] : 'active';
6✔
50

51
                if ( ! empty( $this->args['where']['orderby']['field'] ) ) {
6✔
52
                        $query_args['orderby'] = $this->args['where']['orderby']['field'];
2✔
53
                }
54

55
                $query_args['order'] = ! empty( $this->args['where']['orderby']['order'] ) ? $this->args['where']['orderby']['order'] : 'DESC';
6✔
56

57
                // If $last is set, we need to reverse the order.
58
                if ( ! empty( $last ) ) {
6✔
59
                        $query_args['order'] = 'DESC' === $query_args['order'] ? 'ASC' : 'DESC';
6✔
60
                }
61

62
                /**
63
                 * Set limit the highest value of $first and $last, with a (filterable) max of 100
64
                 */
65
                $query_args['limit'] = $this->one_to_one ? 1 : min( max( absint( $first ), absint( $last ), 10 ), $this->query_amount ) + 1;
6✔
66

67
                /**
68
                 * Set the before and after cursors. This will modify the query in CoreSchemaFilters::add_redirection_pagination_support()
69
                 */
70
                $query_args['graphql_cursor_compare'] = ! empty( $last ) ? '>' : '<';
6✔
71

72
                if ( ! empty( $this->args['after'] ) ) {
6✔
73
                        $query_args['graphql_after_cursor'] = $this->get_after_offset();
3✔
74
                }
75
                
76
                if ( ! empty( $this->args['before'] ) ) {
6✔
77
                        $query_args['graphql_before_cursor'] = $this->get_before_offset();
3✔
78
                }
79

80
                return $query_args;
6✔
81
        }
82

83
        /**
84
         * {@inheritDoc}
85
         */
86
        public function get_query() {
87
                if ( ! isset( $this->query ) ) {
6✔
88
                        $query = RMUtils::get_redirections( $this->query_args );
6✔
89

90
                        // Prime the cache for each of the queried redirections.
91
                        $loader = $this->getLoader();
6✔
92
                        foreach ( $query['redirections'] as $redirection ) {
6✔
93
                                $loader->prime( $redirection['id'], $redirection );
6✔
94
                        }
95

96
                        $this->query = $query;
6✔
97
                }
98

99
                return $this->query;
6✔
100
        }
101

102
        /**
103
         * {@inheritDoc}
104
         */
105
        public function should_execute() {
106
                if ( 'active' === $this->query_args['status'] ) {
6✔
107
                        return true;
6✔
108
                }
109

110
                return RMUtils::has_cap( 'redirections' );
×
111
        }
112

113
        /**
114
         * {@inheritDoc}
115
         */
116
        public function is_valid_offset( $offset ) {
117
                return ! empty( RMUtils::get_redirection_by_id( $offset ) );
6✔
118
        }
119

120
        /**
121
         * {@inheritDoc}
122
         */
123
        public function get_ids_from_query() {
124
                $ids     = [];
6✔
125
                $queried = $this->query['redirections'] ?? [];
6✔
126

127
                if ( empty( $queried ) ) {
6✔
128
                        return $ids;
×
129
                }
130

131
                $ids = array_column( $queried, 'id' );
6✔
132

133
                // If we're going backwards, we need to reverse the array.
134
                if ( ! empty( $this->args['last'] ) ) {
6✔
135
                        $ids = array_reverse( $ids );
6✔
136
                }
137

138
                return $ids;
6✔
139
        }
140
}
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