Skip to content

Commit

Permalink
Merge branch 'feature/#17-mediaItem-mutations' of https://github.com/…
Browse files Browse the repository at this point in the history
…hughdevore/wp-graphql into feature/#17-mediaItem-mutations
  • Loading branch information
hughdevore committed Aug 9, 2017
2 parents 4a4f57f + ee7ab0e commit dfed277
Show file tree
Hide file tree
Showing 55 changed files with 2,487 additions and 1,179 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

# WPGraphQL

<a href="https://www.wpgraphql.com" target="_blank">Website</a> • <a href="https://www.gitbook.com/book/wp-graphql/wp-graphql/" target="_blank">Docs</a> • <a href="https://wp-graphql.github.io/wp-graphql-api-docs/" target="_blank">ApiGen Code Docs</a>
<a href="https://www.wpgraphql.com" target="_blank">Website</a> • <a href="https://wp-graphql.gitbooks.io/wp-graphql/content/" target="_blank">Docs</a> • <a href="https://wp-graphql.github.io/wp-graphql-api-docs/" target="_blank">ApiGen Code Docs</a>

GraphQL API for WordPress.

[![Build Status](https://travis-ci.org/wp-graphql/wp-graphql.svg?branch=master)](https://travis-ci.org/wp-graphql/wp-graphql) [![Coverage Status](https://coveralls.io/repos/github/wp-graphql/wp-graphql/badge.svg?branch=master)](https://coveralls.io/github/wp-graphql/wp-graphql?branch=master)
[![Build Status](https://travis-ci.org/wp-graphql/wp-graphql.svg?branch=master)](https://travis-ci.org/wp-graphql/wp-graphql)
[![Coverage Status](https://coveralls.io/repos/github/wp-graphql/wp-graphql/badge.svg?branch=master)](https://coveralls.io/github/wp-graphql/wp-graphql?branch=master)
[![WPGraphQL on Slack](https://slackin-wpgraphql.herokuapp.com/badge.svg)](https://slackin-wpgraphql.herokuapp.com/)

------
Expand Down
58 changes: 39 additions & 19 deletions src/Data/Config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace WPGraphQL\Data;

/**
Expand Down Expand Up @@ -56,17 +57,17 @@ public function graphql_wp_query_cursor_pagination_support( $where, \WP_Query $q
* 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 && ! empty( $query->get( 'graphql_cursor_offset' ) ) ) {
if ( defined( 'GRAPHQL_REQUEST' ) && GRAPHQL_REQUEST ) {

$cursor_offset = $query->get( 'graphql_cursor_offset' );
$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 = $query->get( 'graphql_cursor_compare' );
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
$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
Expand All @@ -78,13 +79,25 @@ public function graphql_wp_query_cursor_pagination_support( $where, \WP_Query $q
*
*/
if ( ! empty( $cursor_post ) && ! empty( $cursor_post->post_date ) ) {
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_date {$compare}= %s AND NOT ( {$wpdb->posts}.post_date {$compare_opposite}= %s AND {$wpdb->posts}.ID {$compare_opposite}= %d )", esc_sql( $cursor_post->post_date ), esc_sql( $cursor_post->post_date ), absint( $cursor_offset ) );
$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} '{$value}'", $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;

}
Expand All @@ -111,15 +124,18 @@ public function graphql_wp_term_query_cursor_pagination_support( array $pieces,
*/
if ( is_integer( $cursor_offset ) && 0 < $cursor_offset ) {

$compare = $args['graphql_cursor_compare'];
$compare = ! empty( $args['graphql_cursor_compare'] ) ? $args['graphql_cursor_compare'] : '>';
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
$compare_opposite = ( '<' === $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'] .= sprintf( ' AND t.name %1$s= "%3$s" AND NOT ( t.name %2$s= "%3$s" AND t.term_id %2$s= %4$d )', $compare, $compare_opposite, $cursor_term->name, $cursor_offset );
$pieces['where'] .= sprintf( " AND t.{$order_by} {$order_compare} '{$cursor_term->{$order_by}}'" );
} else {
$pieces['where'] .= sprintf( ' AND t.term_id %1$s %2$d', $compare, $cursor_offset );
}
Expand All @@ -134,37 +150,41 @@ public function graphql_wp_term_query_cursor_pagination_support( array $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 $comment_query Current instance of WP_Comment_Query, passed by reference.
* @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 $comment_query ) {
public function graphql_wp_comments_query_cursor_pagination_support( array $pieces, \WP_Comment_Query $query ) {

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

$cursor_offset = ! empty( $comment_query->query_vars['graphql_cursor_offset'] ) ? $comment_query->query_vars['graphql_cursor_offset'] : null;
$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 = $comment_query->query_vars['graphql_cursor_compare'];
$compare = ! empty( $query->get( 'graphql_cursor_compare' ) ) ? $query->get( 'graphql_cursor_compare' ) : '>';
$compare = in_array( $compare, [ '>', '<' ], true ) ? $compare : '>';
$compare_opposite = ( '<' === $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 ) && ! empty( $cursor_comment->comment_date ) ) {
$pieces['where'] .= sprintf( ' AND comment_date %1$s= "%3$s" AND NOT ( comment_date %2$s= "%3$s" AND comment_ID %2$s= %4$d )', $compare, $compare_opposite, esc_html( $cursor_comment->comment_date ), absint( $cursor_offset ) );
if ( ! empty( $cursor_comment ) ) {
$pieces['where'] .= sprintf( " AND {$order_by} {$order_compare} '{$cursor_comment->{$order_by}}'" );
} else {
$pieces['where'] .= sprintf( ' AND comment_ID %1$s %2$d', $compare, $cursor_offset );
}
}
}

return $pieces;

}
Expand Down
23 changes: 11 additions & 12 deletions src/Data/ConnectionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static function resolve( $source, $args, AppContext $context, ResolveInfo
$query = static::get_query( $query_args );
$array_slice = self::get_array_slice( $query, $args );
$connection = static::get_connection( $query, $array_slice, $source, $args, $context, $info );

/**
* Filter the connection, and provide heaps of info to make it easy to filter very specific cases
*
Expand Down Expand Up @@ -78,7 +77,7 @@ public static function resolve( $source, $args, AppContext $context, ResolveInfo
public static function get_connection( $query, array $array, $source, array $args, AppContext $context, ResolveInfo $info ) {

$meta = self::get_array_meta( $query, $args );
$connection = ArrayConnection::connectionFromArray( $array, $args, $meta );
$connection = ArrayConnection::connectionFromArraySlice( $array, $args, $meta );

return $connection;

Expand All @@ -93,12 +92,12 @@ public static function get_connection( $query, array $array, $source, array $arg
* @return array
*/
public static function get_array_slice( $query, array $args ) {
$array_slice = [];

$info = self::get_query_info( $query );
$items = $info['items'];
$array_slice = [];
if ( ! empty( $items ) && is_array( $items ) ) {
foreach ( $items as $item ) {

if ( true === is_object( $item ) ) {
switch ( true ) {
case $item instanceof \WP_Comment:
Expand All @@ -115,12 +114,11 @@ public static function get_array_slice( $query, array $args ) {
$array_slice[] = $item;
break;
default:
$array_slice[] = $item;
$array_slice = $items;
}
}
}
}

return $array_slice;
}

Expand Down Expand Up @@ -171,7 +169,7 @@ public static function get_amount_requested( array $args ) {
}
}

return max( 0, absint( $amount_requested ) );
return max( 0, $amount_requested );

}

Expand Down Expand Up @@ -281,13 +279,14 @@ public static function get_query_info( $query ) {
if ( true === is_object( $query ) ) {
switch ( true ) {
case $query instanceof \WP_Query:
$query_info['total_items'] = ! empty( $query->found_posts ) ? $query->found_posts : count( $query->posts );
$query_info['items'] = $query->posts;
$query_info['request'] = $query->request;
$found_posts = $query->posts;
$query_info['total_items'] = ! empty( $found_posts ) ? count( $found_posts ) : null;
$query_info['items'] = $found_posts;
break;
case $query instanceof \WP_Comment_Query:
$query_info['total_items'] = $query->found_comments;
$query_info['items'] = $query->get_comments();
$found_comments = $query->get_comments();
$query_info['total_items'] = ! empty( $found_comments ) ? count( $found_comments ) : null;
$query_info['items'] = ! empty( $found_comments ) ? $found_comments : [];
break;
case $query instanceof \WP_Term_Query:
$query_info['total_items'] = ! empty( $query->query_vars['taxonomy'] ) ? wp_count_terms( $query->query_vars['taxonomy'][0], $query->query_vars ) : 0;
Expand Down
28 changes: 14 additions & 14 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,9 @@ public static function resolve_http_request() {
* @param string $key Header key.
* @param string $value Header value.
*/
public function send_header( $key, $value ) {
/*
public static function send_header( $key, $value ) {

/**
* Sanitize as per RFC2616 (Section 4.2):
*
* Any LWS that occurs between field-content MAY be replaced with a
Expand All @@ -169,7 +170,7 @@ public function send_header( $key, $value ) {
*
* @param int $code HTTP status.
*/
protected function set_status( $code ) {
protected static function set_status( $code ) {
status_header( $code );
}

Expand Down Expand Up @@ -262,7 +263,6 @@ public static function process_http_request() {

try {


if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] === 'GET' ) {

$data = [
Expand All @@ -271,6 +271,12 @@ public static function process_http_request() {
'variables' => isset( $_GET['variables'] ) ? $_GET['variables'] : '',
];

/**
* Allow the data to be filtered
*
* @param array $data An array containing the pieces of the data of the GraphQL request
*/
$data = apply_filters( 'graphql_request_data', $data );

/**
* If the variables are already formatted as an array use them.
Expand All @@ -286,16 +292,12 @@ public static function process_http_request() {
}
$decoded_variables = $sanitized_variables;

/**
* If the variables are not an array, let's attempt to decode them and convert them to an array for
* use in the executor.
*/
/**
* If the variables are not an array, let's attempt to decode them and convert them to an array for
* use in the executor.
*/
} else {
$decoded_variables = json_decode( $data['variables'] );
if ( empty( $decoded_variables ) ) {
$variables = preg_replace( '#(?<pre>\{|\[|,)\s*(?<key>(?:\w|_)+)\s*:#im', '$1"$2":', sanitize_text_field( $_GET['variables'] ) );
$decoded_variables = (array) json_decode( $variables );
}
}

$data['variables'] = ! empty( $decoded_variables ) && is_array( $decoded_variables ) ? $decoded_variables : null;
Expand All @@ -318,7 +320,6 @@ public static function process_http_request() {
$data = json_decode( self::get_raw_data(), true );
}


/**
* If the $data is empty, catch an error.
*/
Expand Down Expand Up @@ -347,7 +348,6 @@ public static function process_http_request() {
*/
$graphql_results = do_graphql_request( $request, $operation_name, $variables );


/**
* Ensure the $graphql_request is returned as a proper, populated array,
* otherwise add an error to the result
Expand Down
16 changes: 16 additions & 0 deletions src/Type/Comment/Connection/CommentConnectionArgs.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ private static function fields() {
'type' => self::comments_orderby_enum(),
'description' => __( 'Field to order the comments by.', 'wp-graphql' ),
],
'order' => [
'type' => new WPEnumType([
'name' => 'commentsOrder',
'values' => [
[
'name' => 'ASC',
'value' => 'ASC',
],
[
'name' => 'DESC',
'value' => 'DESC',
],
],
'defaultValue' => 'DESC',
]),
],
'parent' => [
'type' => Types::int(),
'description' => __( 'Parent ID of comment to retrieve children of.', 'wp-graphql' ),
Expand Down

0 comments on commit dfed277

Please sign in to comment.