Overview
  • Namespace
  • Class

Namespaces

  • WPGraphQL
    • Data
    • Type
      • Avatar
      • Comment
        • Connection
      • CommentAuthor
      • EditLock
      • Enum
      • MediaItem
        • Mutation
      • Plugin
        • Connection
      • PostObject
        • Connection
        • Mutation
      • PostType
      • Setting
      • Taxonomy
      • TermObject
        • Connection
        • Mutation
      • Theme
        • Connection
      • Union
      • User
        • Connection
        • Mutation
    • Utils

Classes

  • WPGraphQL\AppContext
  • WPGraphQL\Data\Config
  • WPGraphQL\Data\ConnectionResolver
  • WPGraphQL\Data\DataSource
  • WPGraphQL\Data\Loader
  • WPGraphQL\Router
  • WPGraphQL\Type\Avatar\AvatarType
  • WPGraphQL\Type\Comment\CommentQuery
  • WPGraphQL\Type\Comment\CommentType
  • WPGraphQL\Type\Comment\Connection\CommentConnectionArgs
  • WPGraphQL\Type\Comment\Connection\CommentConnectionDefinition
  • WPGraphQL\Type\Comment\Connection\CommentConnectionResolver
  • WPGraphQL\Type\CommentAuthor\CommentAuthorQuery
  • WPGraphQL\Type\CommentAuthor\CommentAuthorType
  • WPGraphQL\Type\EditLock\EditLockType
  • WPGraphQL\Type\Enum\MediaItemStatusEnumType
  • WPGraphQL\Type\Enum\MimeTypeEnumType
  • WPGraphQL\Type\Enum\PostObjectFieldFormatEnumType
  • WPGraphQL\Type\Enum\PostStatusEnumType
  • WPGraphQL\Type\Enum\PostTypeEnumType
  • WPGraphQL\Type\Enum\RelationEnumType
  • WPGraphQL\Type\Enum\TaxonomyEnumType
  • WPGraphQL\Type\MediaItem\MediaItemType
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemCreate
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemDelete
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemMutation
  • WPGraphQL\Type\MediaItem\Mutation\MediaItemUpdate
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionDefinition
  • WPGraphQL\Type\Plugin\Connection\PluginConnectionResolver
  • WPGraphQL\Type\Plugin\PluginQuery
  • WPGraphQL\Type\Plugin\PluginType
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgs
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionArgsDateQuery
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionDefinition
  • WPGraphQL\Type\PostObject\Connection\PostObjectConnectionResolver
  • WPGraphQL\Type\PostObject\Mutation\PostObjectCreate
  • WPGraphQL\Type\PostObject\Mutation\PostObjectDelete
  • WPGraphQL\Type\PostObject\Mutation\PostObjectMutation
  • WPGraphQL\Type\PostObject\Mutation\PostObjectUpdate
  • WPGraphQL\Type\PostObject\Mutation\TermObjectDelete
  • WPGraphQL\Type\PostObject\PostObjectQuery
  • WPGraphQL\Type\PostObject\PostObjectType
  • WPGraphQL\Type\PostType\PostTypeType
  • WPGraphQL\Type\RootMutationType
  • WPGraphQL\Type\RootQueryType
  • WPGraphQL\Type\Setting\SettingQuery
  • WPGraphQL\Type\Setting\SettingType
  • WPGraphQL\Type\Taxonomy\TaxonomyType
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionArgs
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionDefinition
  • WPGraphQL\Type\TermObject\Connection\TermObjectConnectionResolver
  • WPGraphQL\Type\TermObject\Mutation\TermObjectCreate
  • WPGraphQL\Type\TermObject\Mutation\TermObjectMutation
  • WPGraphQL\Type\TermObject\Mutation\TermObjectUpdate
  • WPGraphQL\Type\TermObject\TermObjectQuery
  • WPGraphQL\Type\TermObject\TermObjectType
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionDefinition
  • WPGraphQL\Type\Theme\Connection\ThemeConnectionResolver
  • WPGraphQL\Type\Theme\ThemeType
  • WPGraphQL\Type\Union\CommentAuthorUnionType
  • WPGraphQL\Type\Union\PostObjectUnionType
  • WPGraphQL\Type\Union\TermObjectUnionType
  • WPGraphQL\Type\User\Connection\UserConnectionArgs
  • WPGraphQL\Type\User\Connection\UserConnectionDefinition
  • WPGraphQL\Type\User\Connection\UserConnectionResolver
  • WPGraphQL\Type\User\Mutation\UserCreate
  • WPGraphQL\Type\User\Mutation\UserDelete
  • WPGraphQL\Type\User\Mutation\UserMutation
  • WPGraphQL\Type\User\Mutation\UserUpdate
  • WPGraphQL\Type\User\UserQuery
  • WPGraphQL\Type\User\UserType
  • WPGraphQL\Type\WPEnumType
  • WPGraphQL\Type\WPInputObjectType
  • WPGraphQL\Type\WPObjectType
  • WPGraphQL\Types
  • WPGraphQL\Utils\InstrumentSchema
  • WPGraphQL\WPSchema

Interfaces

  • WPGraphQL\Data\ConnectionResolverInterface
  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 
<?php

namespace WPGraphQL\Data;

/**
 * Class Config
 *
 * This class contains configurations for various data-related things, such as query filters for cursor pagination.
 *
 * @package WPGraphQL\Data
 */
class Config {

    /**
     * Config constructor.
     */
    public function __construct() {

        /**
         * Filter the term_clauses in the WP_Term_Query to allow for cursor pagination support where a Term ID
         * can be used as a point of comparison when slicing the results to return.
         */
        add_filter( 'comments_clauses', [ $this, 'graphql_wp_comments_query_cursor_pagination_support' ], 10, 2 );


        /**
         * Filter the WP_Query to support cursor based pagination where a post ID can be used
         * as a point of comparison when slicing the results to return.
         */
        add_filter( 'posts_where', [ $this, 'graphql_wp_query_cursor_pagination_support' ], 10, 2 );

        /**
         * Filter the term_clauses in the WP_Term_Query to allow for cursor pagination support where a Term ID
         * can be used as a point of comparison when slicing the results to return.
         */
        add_filter( 'terms_clauses', [ $this, 'graphql_wp_term_query_cursor_pagination_support' ], 10, 3 );

    }

    /**
     * This filters the WPQuery 'where' $args, enforcing the query to return results before or
     * after the referenced cursor
     *
     * @param string    $where The WHERE clause of the query.
     * @param \WP_Query $query The WP_Query instance (passed by reference).
     *
     * @return string
     */
    public function graphql_wp_query_cursor_pagination_support( $where, \WP_Query $query ) {

        /**
         * Access the global $wpdb object
         */
        global $wpdb;

        /**
         * If there's a graphql_cursor_offset in the query, we should check to see if
         * it should be applied to the query
         */
        if ( defined( 'GRAPHQL_REQUEST' ) && GRAPHQL_REQUEST ) {

            $cursor_offset = ! empty( $query->query_vars['graphql_cursor_offset'] ) ? $query->query_vars['graphql_cursor_offset'] : 0;

            /**
             * Ensure the cursor_offset is a positive integer
             */
            if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {

                $compare = ! empty( $query->get( 'graphql_cursor_compare' ) ) ? $query->get( 'graphql_cursor_compare' ) : '>';
                $compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
                $compare_opposite = ( '<' === $compare ) ? '>' : '<';

                // Get the $cursor_post
                $cursor_post = get_post( $cursor_offset );

                /**
                 * If the $cursor_post exists (hasn't been deleted), modify the query to compare based on the ID and post_date values
                 * But if the $cursor_post no longer exists, we're forced to just compare with the ID
                 *
                 */
                if ( ! empty( $cursor_post ) && ! empty( $cursor_post->post_date ) ) {
                    $orderby = $query->get( 'orderby' );
                    if ( ! empty( $orderby ) && is_array( $orderby ) ) {
                        foreach ( $orderby as $by => $order ) {
                            $order_compare = ( 'ASC' === $order ) ? '>' : '<';
                            $value = $cursor_post->{$by};
                            if ( ! empty( $by ) && ! empty( $value ) ) {
                                $where .= $wpdb->prepare( " AND {$wpdb->posts}.{$by} {$order_compare} %s", $value );
                            }
                        }
                    } else {
                        $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date {$compare}= %s AND NOT ( {$wpdb->posts}.post_date {$compare}= %s AND {$wpdb->posts}.ID {$compare_opposite}= %d )", esc_sql( $cursor_post->post_date ), esc_sql( $cursor_post->post_date ), absint( $cursor_offset ) );
                    }
                } else {
                    $where .= $wpdb->prepare( " AND {$wpdb->posts}.ID {$compare} %d", $cursor_offset );
                }
            }
        }


        return $where;

    }

    /**
     * This filters the term_clauses in the WP_Term_Query to support cursor based pagination, where we can
     * move forward or backward from a particular record, instead of typical offset pagination which can be
     * much more expensive and less accurate.
     *
     * @param array $pieces     Terms query SQL clauses.
     * @param array $taxonomies An array of taxonomies.
     * @param array $args       An array of terms query arguments.
     *
     * @return array $pieces
     */
    public function graphql_wp_term_query_cursor_pagination_support( array $pieces, $taxonomies, $args ) {

        /**
         * Access the global $wpdb object
         */
        global $wpdb;

        if ( defined( 'GRAPHQL_REQUEST' ) && GRAPHQL_REQUEST && ! empty( $args['graphql_cursor_offset'] ) ) {

            $cursor_offset = $args['graphql_cursor_offset'];

            /**
             * Ensure the cursor_offset is a positive integer
             */
            if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {

                $compare = ! empty( $args['graphql_cursor_compare'] ) ? $args['graphql_cursor_compare'] : '>';
                $compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';

                $order_by = ! empty( $args['orderby'] ) ? $args['orderby'] : 'comment_date';
                $order = ! empty( $args['order'] ) ? $args['order'] : 'DESC';
                $order_compare = ( 'ASC' === $order ) ? '>' : '<';

                // Get the $cursor_post
                $cursor_term = get_term( $cursor_offset );

                if ( ! empty( $cursor_term ) && ! empty( $cursor_term->name ) ) {
                    $pieces['where'] .= $wpdb->prepare( " AND t.{$order_by} {$order_compare} %s", $cursor_term->{$order_by} );
                } else {
                    $pieces['where'] .= $wpdb->prepare( ' AND t.term_id %1$s %2$d', $compare, $cursor_offset );
                }
            }
        }

        return $pieces;

    }

    /**
     * This returns a modified version of the $pieces of the comment query clauses if the request
     * is a GRAPHQL_REQUEST and the query has a graphql_cursor_offset defined
     *
     * @param array             $pieces A compacted array of comment query clauses.
     * @param \WP_Comment_Query $query  Current instance of WP_Comment_Query, passed by reference.
     *
     * @return array $pieces
     */
    public function graphql_wp_comments_query_cursor_pagination_support( array $pieces, \WP_Comment_Query $query ) {

        /**
         * Access the global $wpdb object
         */
        global $wpdb;

        if (
            defined( 'GRAPHQL_REQUEST' ) && GRAPHQL_REQUEST &&
            ( is_array( $query->query_vars ) && array_key_exists( 'graphql_cursor_offset', $query->query_vars ) )
        ) {

            $cursor_offset = $query->query_vars['graphql_cursor_offset'];

            /**
             * Ensure the cursor_offset is a positive integer
             */
            if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {

                $compare = ! empty( $query->get( 'graphql_cursor_compare' ) ) ? $query->get( 'graphql_cursor_compare' ) : '>';
                $compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';

                $order_by = ! empty( $query->query_vars['order_by'] ) ? $query->query_vars['order_by'] : 'comment_date';
                $order = ! empty( $query->query_vars['order'] ) ? $query->query_vars['order'] : 'DESC';
                $order_compare = ( 'ASC' === $order ) ? '>' : '<';

                // Get the $cursor_post
                $cursor_comment = get_comment( $cursor_offset );
                if ( ! empty( $cursor_comment ) ) {
                    $pieces['where'] .= $wpdb->prepare( " AND {$order_by} {$order_compare} %s", $cursor_comment->{$order_by} );
                } else {
                    $pieces['where'] .= $wpdb->prepare( ' AND comment_ID %1$s %2$d', $compare, $cursor_offset );
                }
            }
        }

        return $pieces;

    }

}
API documentation generated by ApiGen