-
Notifications
You must be signed in to change notification settings - Fork 6
/
wp-graphql-offset-pagination.php
343 lines (289 loc) · 10.8 KB
/
wp-graphql-offset-pagination.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
<?php
/**
* Plugin Name: WP GraphQL Offset Pagination
* Description: Offset Pagination support for the WPGraphQL plugin.
* Author: Daryll Doyle
* Author URI: https://www.enshrined.co.uk
* Version: 0.0.1
* Text Domain: wp-graphql-offset-pagination
* Requires at least: 4.7.0
* Tested up to: 4.7.1
*/
namespace Enshrined;
use Enshrined\OffsetPagination\OffsetPaginationType;
use WPGraphQL\Data\Connection\AbstractConnectionResolver;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class OffsetPagination {
/**
* This holds the OffsetPaginationType array
* @var $offset_pagination
* @since 0.0.1
*/
private static $offset_pagination;
/**
* OffsetPagination constructor.
*
* This hooks the plugin into the WPGraphQL Plugin
*
* @since 0.0.1
*/
public function __construct() {
/**
* Setup plugin constants
* @since 0.0.1
*/
$this->setup_constants();
/**
* Included required files
* @since 0.0.1
*/
$this->includes();
/**
* Filter the query_args for the PostObjectQueryArgsType
* @since 0.0.1
*/
add_filter( 'graphql_input_fields', array( $this, 'add_input_fields' ), 10, 3 );
/**
* Filter the $allowed_custom_args for the PostObjectsConnectionResolver to map the
* offsetPagination input to WP_Query terms
* @since 0.0.1
*/
add_filter( 'graphql_map_input_fields_to_wp_query', [ $this, 'map_input_fields' ], 10, 2 );
/**
* Filter the $allowed_custom_args for the UserConnectionResolver to map the
* offsetPagination input to WP_User_Query terms
* @since 0.0.1
*/
add_filter( 'graphql_map_input_fields_to_wp_user_query', [ $this, 'map_input_fields' ], 10, 2 );
/**
* Filter the nodes so that we can return the correct number of items.
* @since 0.0.1
*/
add_filter( 'graphql_connection_nodes', [ $this, 'get_nodes' ], 10, 2 );
/**
* Register the extra pageInfo fields that will be used.
* @since 0.0.1
*/
add_filter( 'graphql_register_types', [ $this, 'register_page_info_fields' ], 10, 1 );
/**
* Filter the pageInfo that is returned to the connection.
* @since 0.0.1
*/
add_filter( 'graphql_connection_page_info', [ $this, 'get_page_info' ], 10, 2 );
}
/**
* Setup plugin constants.
*
* @access private
* @return void
* @since 0.0.1
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'WPGRAPHQL_OFFSET_PAGINATION_VERSION' ) ) {
define( 'WPGRAPHQL_OFFSET_PAGINATION_VERSION', '0.0.1' );
}
// Plugin Folder Path.
if ( ! defined( 'WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_DIR' ) ) {
define( 'WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_URL' ) ) {
define( 'WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_FILE' ) ) {
define( 'WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_FILE', __FILE__ );
}
}
/**
* Include required files.
*
* Uses composer's autoload
*
* @access private
* @return void
* @since 0.0.1
*/
private function includes() {
// Autoload Required Classes
require_once( WPGRAPHQL_OFFSET_PAGINATION_PLUGIN_DIR . 'vendor/autoload.php' );
}
/**
* add_input_fields
*
* This adds the OffsetPagination input fields
*
* @param array $fields
* @param string $type_name
* @param array $config
*
* @return mixed
* @since 0.0.1
*/
public function add_input_fields( $fields, $type_name, $config ) {
// Hook into Root User queries.
if ( 'RootQueryToUserConnectionWhereArgs' === $type_name ) {
$fields['offsetPagination'] = self::offset_pagination( $type_name );
}
// Hook into post/page queries.
if ( isset( $config['queryClass'] ) && 'WP_Query' === $config['queryClass'] ) {
$fields['offsetPagination'] = self::offset_pagination( $type_name );
}
return $fields;
}
/**
* This returns the definition for the OffsetPagination
*
* @param string $type_name
*
* @return array
* @since 0.0.1
*/
public static function offset_pagination( $type_name ) {
if ( empty( self::$offset_pagination[ $type_name ] ) ) {
self::$offset_pagination[ $type_name ] = new OffsetPaginationType( $type_name );
}
return ! empty( self::$offset_pagination[ $type_name ] ) ? self::$offset_pagination[ $type_name ] : null;
}
/**
* map_input_fields
*
* This maps the offsetPagination input fields to the WP_Query
*
* @param $query_args
* @param $input_args
*
* @return mixed
* @since 0.0.1
*/
public function map_input_fields( $query_args, $input_args ) {
/**
* OffsetPaginationQuery $args
* This maps the GraphQL offsetPagination input to the WP_Query to handle pagination
* @since 0.0.1
*/
$offset_pagination = null;
if ( ! empty( $input_args['offsetPagination'] ) ) {
// Get the offsetPagination input
$offset_pagination = $input_args['offsetPagination'];
// If per_page is set and is numeric, we can use that. Otherwise default to 10.
if ( isset( $offset_pagination['per_page'] ) && is_numeric( $offset_pagination['per_page'] ) ) {
$query_args['number'] = $query_args['posts_per_page'] = intval( $offset_pagination['per_page'] );
} else {
$query_args['number'] = $query_args['posts_per_page'] = 10;
}
if ( isset( $offset_pagination['page'] ) && is_numeric( $offset_pagination['page'] ) ) {
$query_args['offset'] = ( intval( $offset_pagination['page'] ) - 1 ) * intval( $query_args['number'] );
} else {
$query_args['offset'] = 0;
}
$query_args['number'] = $query_args['posts_per_page'] = $query_args['number'] + 1;
}
unset( $query_args['offsetPagination'] );
/**
* Retrun the $query_args
* @since 0.0.1
*/
return $query_args;
}
/**
* Get the nodes from the query.
*
* We slice the array to match the amount of items that was asked for, as we over-fetched
* by 1 item to calculate pageInfo.
*
* @param $nodes
* @param AbstractConnectionResolver $resolver
*
* @return array
* @since 0.0.1
*/
public function get_nodes( $nodes, AbstractConnectionResolver $resolver ) {
/**
* We must use $resolver->get_items() rather than $nodes as $nodes has already
* been sliced by the AbstractConnectionResolver
*/
return array_slice( $resolver->get_items(), 0, $resolver->get_query_args()['posts_per_page'] - 1 );
}
/**
* Register the new fields that will be used for pagination within WPPageInfo.
*
* @return void
* @since 0.0.1
*/
public function register_page_info_fields() {
register_graphql_field( 'WPPageInfo', 'previousPage', [
'type' => 'Int',
] );
register_graphql_field( 'WPPageInfo', 'nextPage', [
'type' => 'Int',
] );
register_graphql_field( 'WPPageInfo', 'totalPages', [
'type' => 'Int',
] );
}
/**
* Filter the pageInfo that is returned to the connection.
*
* This allows us to interject with our own logic when using offsetPagination.
*
* @param array $page_info
* @param AbstractConnectionResolver $resolver
*
* @return array
* @since 0.0.1
*/
function get_page_info( $page_info, AbstractConnectionResolver $resolver ) {
// Get the resolver args.
$args = $resolver->getArgs();
$page_info['previousPage'] = null;
$page_info['nextPage'] = null;
$page_info['totalPages'] = null;
// If we're querying with an offset pagination query then use this logic.
if ( isset( $args['where'] ) && isset( $args['where']['offsetPagination'] ) ) {
$query_args = $resolver->get_query_args();
$items = $resolver->get_items();
// The number of pages the user actually requested.
$requested_pages = $query_args['posts_per_page'] - 1;
// If we're requesting any offset, then there must be a prev page.
$page_info['hasPreviousPage'] = $query_args['offset'] > 0 ? true : false;
// If we've retrieved more items that we requested (because of the +1) there must be a next page.
$page_info['hasNextPage'] = count( $items ) > $requested_pages ? true : false;
// Get the current page that we're on. (Need to +1 otherwise page 1 would show as 0)
$current_page = $query_args['offset'] / $requested_pages + 1;
// If we have a next page, add it to pageInfo.
$page_info['nextPage'] = $page_info['hasNextPage'] ? $current_page + 1 : null;
// If we have a prev page, add it to pageInfo.
$page_info['previousPage'] = $page_info['hasPreviousPage'] ? $current_page - 1 : null;
/**
* Calculate the total number of pages available on this query.
*
* The calculation is ceil( number_of_posts / items_per_page ).
*/
if ( $resolver->get_query() instanceof \WP_Query ) {
if ( isset( $resolver->get_query()->found_posts ) ) {
$page_info['totalPages'] = ceil( intval( $resolver->get_query()->found_posts ) / $requested_pages );
}
} elseif ( $resolver->get_query() instanceof \WP_User_Query ) {
if ( isset( $resolver->get_query()->total_users ) ) {
$page_info['totalPages'] = ceil( intval( $resolver->get_query()->total_users ) / $requested_pages );
}
}
// Kill off the start and end cursor as they make no sense now we're not using cursors.
$page_info['startCursor'] = $page_info['endCursor'] = null;
}
return $page_info;
}
}
/**
* Instantiate the OffsetPagination class on graphql_init
* @return OffsetPagination
*/
function graphql_init_offset_pagination() {
return new \Enshrined\OffsetPagination();
}
add_action( 'graphql_init', '\Enshrined\graphql_init_offset_pagination' );