From 2c5218f41883a6c8a398d6ddbe623aea416bc143 Mon Sep 17 00:00:00 2001 From: Mohammad Jaqmaqji Date: Mon, 8 Aug 2022 09:26:48 +0300 Subject: [PATCH 1/3] chore: add composer --- .gitignore | 1 + composer.json | 12 ++++++++++++ 2 files changed, 13 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..57872d0 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..3add347 --- /dev/null +++ b/composer.json @@ -0,0 +1,12 @@ +{ + "name": "builtbycactus/total-counts-for-wp-graphql", + "description": "Adds the ability to fetch total post counts from WP-GraphQL", + "type": "wordpress-plugin", + "license": "GNU General Public License v2.0", + "authors": [ + { + "name": "Built By Cactus", + "homepage": "https://builtbycactus.co.uk/" + } + ] +} From 194c6fb045e78847cddddcb3ffef30180dcb405f Mon Sep 17 00:00:00 2001 From: Mohammad Jaqmaqji Date: Mon, 8 Aug 2022 09:33:55 +0300 Subject: [PATCH 2/3] doc: add release notes in changelog --- CHANGELOG.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..945c5b9 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,12 @@ +# Changelog + +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## v0.0.2 2022-08-08 + +### Added + +- `composer.json` to make the plugin ready to be installed via composer From b181e5e7a33f7392f3f3fbc79393c6a06065be3a Mon Sep 17 00:00:00 2001 From: Mohammad Jaqmaqji Date: Wed, 31 Aug 2022 19:27:03 +0300 Subject: [PATCH 3/3] fix: get query once when resolve total field --- composer.json | 1 + total-counts-for-wp-graphql.php | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/composer.json b/composer.json index 3add347..750da37 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,7 @@ "description": "Adds the ability to fetch total post counts from WP-GraphQL", "type": "wordpress-plugin", "license": "GNU General Public License v2.0", + "version": "0.0.3", "authors": [ { "name": "Built By Cactus", diff --git a/total-counts-for-wp-graphql.php b/total-counts-for-wp-graphql.php index 8c4d4d9..a98fd03 100644 --- a/total-counts-for-wp-graphql.php +++ b/total-counts-for-wp-graphql.php @@ -7,7 +7,7 @@ * Author URI: https://builtbycactus.co.uk/ * Text Domain: cactus-gqltc * Domain Path: /languages - * Version: 0.0.2 + * Version: 0.0.3 */ namespace Cactus\GQLTC; @@ -73,13 +73,20 @@ function () { */ 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; + + // Change the query property to be public then access it. + $reflector = new \ReflectionObject( $connection ); + $property = $reflector->getProperty( 'query' ); + $property->setAccessible( true ); + $connectionQuery = $property->getValue( $connection ); + + if ( $connectionQuery instanceof \WP_Query ) { + if ( isset( $connectionQuery->found_posts ) ) { + $page_info['total'] = (int) $connectionQuery->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; + } elseif ( $connectionQuery instanceof \WP_User_Query ) { + if ( isset( $connectionQuery->total_users ) ) { + $page_info['total'] = (int) $connectionQuery->total_users; } }