-
Notifications
You must be signed in to change notification settings - Fork 5
/
total-counts-for-wp-graphql.php
112 lines (99 loc) · 2.51 KB
/
total-counts-for-wp-graphql.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
<?php
/**
* Plugin Name: Total Counts for WPGraphQL
* Plugin URI: https://github.com/builtbycactus/total-counts-for-wp-graphql
* Description: Adds Total Count to the WPGraphQL Schema
* Author: Built By Cactus
* Author URI: https://builtbycactus.co.uk/
* Text Domain: cactus-gqltc
* Domain Path: /languages
* Version: 0.0.2
*/
namespace Cactus\GQLTC;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Initialize the plugin
*/
function init() {
/**
* If WPGraphQL is not active, show the admin notice
*/
if ( ! class_exists( 'WPGraphQL' ) ) {
add_action( 'admin_init', __NAMESPACE__ . '\show_admin_notice' );
} else {
add_filter( 'graphql_connection_page_info', __NAMESPACE__ . '\resolve_total_field', 10, 2 );
add_filter( 'graphql_connection_query_args', __NAMESPACE__ . '\count_total_rows' );
add_filter( 'graphql_register_types', __NAMESPACE__ . '\register_total_field' );
}
}
add_action( 'init', __NAMESPACE__ . '\init' );
/**
* Show admin notice to admins if this plugin is active but either ACF and/or WPGraphQL
* are not active
*
* @return bool
*/
function show_admin_notice() {
/**
* For users with lower capabilities, don't show the notice
*/
if ( ! current_user_can( 'manage_options' ) ) {
return false;
}
add_action(
'admin_notices',
function () {
?>
<div class="error notice">
<p><?php _e( 'WPGraphQL must be active for "total-counts-for-wpgraphql" to work',
'cactus-gqltc' ); ?></p>
</div>
<?php
}
);
return true;
}
/**
* Resolve the total field.
*
* @param $page_info
* @param $connection
*
* @return mixed
*/
function resolve_total_field( $page_info, $connection ) {
$page_info['total'] = null;
if ( $connection->get_query() instanceof \WP_Query ) {
if ( isset( $connection->get_query()->found_posts ) ) {
$page_info['total'] = (int) $connection->get_query()->found_posts;
}
} elseif ( $connection->get_query() instanceof \WP_User_Query ) {
if ( isset( $connection->get_query()->total_users ) ) {
$page_info['total'] = (int) $connection->get_query()->total_users;
}
}
return $page_info;
}
/**
* Tell the underlying WP_Query to count the total number of rows.
*
* @param $args
*
* @return mixed
*/
function count_total_rows( $args ) {
$args['no_found_rows'] = false;
$args['count_total'] = true;
return $args;
}
/**
* Register a total field for queries.
*/
function register_total_field() {
register_graphql_field( 'WPPageInfo', 'total', [
'type' => 'Int',
] );
}
;