• 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

89.29
/src/Modules/Redirection/Data/Cursor/RedirectionCursor.php
1
<?php
2
/**
3
 * The Redirection cursor.
4
 *
5
 * @package WPGraphQL\RankMath\Modules\Redirection\Data\Cursor;
6
 * @since @todo
7
 */
8

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

11
use WPGraphQL\RankMath\Utils\RMUtils;
12

13
/**
14
 * Class - RedirectionCursor
15
 */
16
class RedirectionCursor {
17
        /**
18
         * Our current cursor offset.
19
         * For example, the term, post, user, or comment ID.
20
         *
21
         * @var int
22
         */
23
        public $cursor_offset;
24

25
        /**
26
         * @var string|null
27
         */
28
        public $cursor;
29

30
        /**
31
         * The WP object instance for the cursor.
32
         *
33
         * @var mixed
34
         */
35
        public $cursor_node;
36

37
        /**
38
         * Copy of query_vars so we can modify them safely
39
         *
40
         * @var array<string,mixed>
41
         */
42
        public $query_vars = [];
43

44
        /**
45
         * Constructor
46
         *
47
         * @param array<string,mixed> $query_args The query args.
48
         * @param string              $cursor The cursor.
49
         */
50
        public function __construct( $query_args, $cursor = 'after' ) {
51
                $this->query_vars = $query_args;
6✔
52
                $this->cursor     = $cursor;
6✔
53

54
                // Get the cursor offset if any.
55
                $offset_key = 'graphql_' . $cursor . '_cursor';
6✔
56
                $offset     = $this->get_query_var( $offset_key );
6✔
57

58
                $this->cursor_offset = absint( $offset );
6✔
59

60
                $this->cursor_node = $this->get_cursor_node();
6✔
61
        }
62

63
        /**
64
         * Get the WP Object instance for the cursor.
65
         *
66
         * This is cached internally so it should not generate additionl queries.
67
         *
68
         * @return ?array<string,mixed>
69
         */
70
        public function get_cursor_node() {
71
                if ( ! $this->cursor_offset ) {
6✔
72
                        return null;
×
73
                }
74

75
                // We don't want to reset the sql clauses.
76
                return RMUtils::get_redirection_from_db( $this->cursor_offset );
6✔
77
        }
78

79
        /**
80
         * Get the direction pagination is going in.
81
         *
82
         * @return string
83
         */
84
        public function get_cursor_compare() {
85
                return 'DESC' === $this->query_vars['order'] ? '<' : '>';
6✔
86
        }
87

88
        /**
89
         * Ensure the cursor_offset is a positive integer and we have a valid object for our cursor node.
90
         *
91
         * @return bool
92
         */
93
        protected function is_valid_offset_and_node() {
94
                if (
95
                        ! is_int( $this->cursor_offset ) ||
6✔
96
                        0 >= $this->cursor_offset ||
6✔
97
                        ! $this->cursor_node
6✔
98
                ) {
99
                        return false;
×
100
                }
101

102
                return true;
6✔
103
        }
104

105
        /**
106
         * Return the additional AND operators for the where statement
107
         *
108
         * @return array<string,mixed>
109
         */
110
        public function get_where() {
111
                // If we have a bad cursor, just return an empty array.
112
                if ( ! $this->is_valid_offset_and_node() ) {
6✔
113
                        return [];
×
114
                }
115

116
                $orderby          = $this->get_query_var( 'orderby' );
6✔
117
                $compare          = $this->get_cursor_compare();
6✔
118
                $comparison_value = $this->cursor_node[ $orderby ];
6✔
119

120
                if ( 'id' === $orderby ) {
6✔
121
                        $comparison_value = (int) $comparison_value;
4✔
122
                }
123

124
                return [
6✔
125
                        'column'   => $orderby,
6✔
126
                        'operator' => $compare,
6✔
127
                        'value'    => $comparison_value,
6✔
128
                ];
6✔
129
        }
130

131
        /**
132
         * Get the query variable for the provided name.
133
         *
134
         * @param string $name .
135
         *
136
         * @return mixed|null
137
         */
138
        public function get_query_var( string $name ) {
139
                return ! empty( $this->query_vars[ $name ] ) ? $this->query_vars[ $name ] : null;
6✔
140
        }
141
}
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