Skip to content

Commit

Permalink
feat: WooGraphQL settings tab added. (#726)
Browse files Browse the repository at this point in the history
* feat: WooGraphQL settings tab added.

* chore: WPCS compliance met

* bugfix: Order Item Node ID implemented.

* chore: Deprecated "AppContext::getLoader" calls replaced.

* chore: WPCS compliance met.

* fix: DownloadableItem "id" field implemented
  • Loading branch information
kidunot89 authored Apr 4, 2023
1 parent 4fc9b93 commit c068671
Show file tree
Hide file tree
Showing 19 changed files with 1,169 additions and 805 deletions.
13 changes: 7 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions includes/admin/class-general.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Defines WooGraphQL's general settings.
*
* @package WPGraphQL\WooCommerce\Admin
*/

namespace WPGraphQL\WooCommerce\Admin;

/**
* General class
*/
class General extends Section {

/**
* Returns General settings fields.
*
* @return array
*/
public static function get_fields() {
return [
[
'name' => 'disable_ql_session_handler',
'label' => __( 'Disable QL Session Handler', 'wp-graphql-woocommerce' ),
'desc' => __( 'The QL Session Handler takes over management of WooCommerce Session Management on WPGraphQL request replacing the usage of HTTP Cookies with JSON Web Tokens.', 'wp-graphql-woocommerce' ),
'type' => 'checkbox',
'default' => 'off',
],
];
}
}
21 changes: 21 additions & 0 deletions includes/admin/class-section.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* The section defines the root functionality for a settings section
*
* @package WPGraphQL\WooCommerce\Admin
*/

namespace WPGraphQL\WooCommerce\Admin;

/**
* Section class
*/
abstract class Section {

/**
* Returns Section settings fields.
*
* @return array
*/
abstract public static function get_fields();
}
62 changes: 62 additions & 0 deletions includes/admin/class-substitutions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Defines WooGraphQL's substitutions settings.
*
* @package WPGraphQL\WooCommerce\Admin
*/

namespace WPGraphQL\WooCommerce\Admin;

use WPGraphQL\WooCommerce\WP_GraphQL_WooCommerce as WooGraphQL;

/**
* General class
*/
class Substitutions extends Section {

/**
* Return option list of valid GraphQL types for products.
*
* @return array
*/
public static function get_dropdown_list() {
$graphql_types = [];
foreach ( WooGraphQL::get_enabled_product_types() as $type ) {
$graphql_types[ $type ] = $type;
}
return $graphql_types;
}

/**
* Returns General settings fields.
*
* @return array
*/
public static function get_fields() {
$type_labels = wc_get_product_types();
$registered_types = array_keys( WooGraphQL::get_enabled_product_types() );
$all_types = array_keys( $type_labels );

$unregistered_types = array_diff( $registered_types, $all_types );
$fields = [];
foreach ( $unregistered_types as $product_type ) {
$fields[] = [
'name' => "{$product_type}_substitution_type",
'label' => sprintf(
/* translators: product type */
__( 'Substitution type for %s', 'wp-graphql-woocommerce' ),
$type_labels[ $product_type ]
),
'desc' => sprintf(
/* translators: product type */
__( 'Set a replacement GraphQL type for %s because it\'s unregistered', 'wp-graphql-woocommerce' ),
$type_labels[ $product_type ]
),
'type' => 'select',
'options' => self::get_dropdown_list(),
];
}

return $fields;
}
}
49 changes: 49 additions & 0 deletions includes/class-admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Initializes a WooGraphQL Pro admin settings.
*
* @package WPGraphQL\WooCommerce\Pro
* @since 1.0.0
*/

namespace WPGraphQL\WooCommerce;

use WPGraphQL\Admin\Settings\Settings;
use WPGraphQL\WooCommerce\Admin\General;
use WPGraphQL\WooCommerce\Admin\Substitutions;

/**
* Class Admin
*/
class Admin {

/**
* Admin constructor
*/
public function __construct() {
add_action( 'graphql_register_settings', [ $this, 'register_settings' ] );
}

/**
* Registers the WooGraphQL Settings tab.
*
* @param Settings $manager Settings Manager.
* @return void
*/
public function register_settings( Settings $manager ) {
$manager->settings_api->register_section(
'woographql_settings',
[ 'title' => __( 'WooGraphQL', 'wp-graphql-woocommerce' ) ]
);

$manager->settings_api->register_fields(
'woographql_settings',
General::get_fields(),
);

$manager->settings_api->register_fields(
'woographql_settings',
Substitutions::get_fields(),
);
}
}
19 changes: 19 additions & 0 deletions includes/class-core-schema-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,18 @@ public static function add_filters() {
);
}

/**
* Check and returns a GraphQL type from the
* "{$product_type}_substitution_type" in the WooGraphQL settings.
*
* @param string $product_type Product type in need of a GraphQL type.
*
* @return string|null
*/
public static function get_substitution_type( $product_type ) {
return get_graphql_setting( "{$product_type}_substitution_type", null, 'woographql_settings' );
}

/**
* Registers WooCommerce post-types to be used in GraphQL schema
*
Expand All @@ -152,6 +164,13 @@ public static function register_post_types( $args, $post_type ) {
if ( isset( $possible_types[ $value->type ] ) ) {
return $type_registry->get_type( $possible_types[ $value->type ] );
}

// Look for substitution type.
$substitution_type = self::get_substitution_type( $value->type );
if ( ! empty( $substitution_type ) ) {
return $type_registry->get_type( $substitution_type );
}

throw new UserError(
sprintf(
/* translators: %s: Product type */
Expand Down
11 changes: 10 additions & 1 deletion includes/class-woocommerce-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static function setup() {
self::$session_header = apply_filters( 'graphql_woocommerce_cart_session_http_header', 'woocommerce-session' );

// Check if request is a GraphQL POST request.
if ( ! defined( 'NO_QL_SESSION_HANDLER' ) ) {
if ( ! self::is_session_handler_disabled() ) {
add_filter( 'woocommerce_session_handler', [ __CLASS__, 'woocommerce_session_handler' ] );
add_filter( 'graphql_response_headers_to_send', [ __CLASS__, 'add_session_header_to_expose_headers' ] );
add_filter( 'graphql_access_control_allow_headers', [ __CLASS__, 'add_session_header_to_allow_headers' ] );
Expand All @@ -37,6 +37,15 @@ public static function setup() {
add_filter( 'graphql_stripe_process_payment_args', [ __CLASS__, 'woographql_stripe_gateway_args' ], 10, 2 );
}

/**
* Returns true if the "Disable QL Session Handler" option is checked on the settings page.
*
* @return boolean
*/
public static function is_session_handler_disabled() {
return 'on' === get_graphql_setting( 'disable_ql_session_handler', 'off', 'woographql_settings' );
}

/**
* WooCommerce Session Handler callback
*
Expand Down
9 changes: 9 additions & 0 deletions includes/class-wp-graphql-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,13 @@ private function includes() {
require $include_directory_path . 'connection/class-variation-attributes.php';
require $include_directory_path . 'connection/class-wc-terms.php';

// Include admin files.
require $include_directory_path . 'admin/class-section.php';
require $include_directory_path . 'admin/class-general.php';
require $include_directory_path . 'admin/class-substitutions.php';

// Include main plugin class files.
require $include_directory_path . 'class-admin.php';
require $include_directory_path . 'class-core-schema-filters.php';
require $include_directory_path . 'class-jwt-auth-schema-filters.php';
require $include_directory_path . 'class-woocommerce-filters.php';
Expand Down Expand Up @@ -358,6 +364,9 @@ function () {
* Sets up WooGraphQL schema.
*/
private function setup() {
// Initialize WooGraphQL Settings.
new Admin();

// Setup minor integrations.
Functions\setup_minor_integrations();

Expand Down
14 changes: 7 additions & 7 deletions includes/data/class-factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static function resolve_customer( $id, AppContext $context ) {
return null;
}
$customer_id = absint( $id );
$loader = $context->getLoader( 'wc_customer' );
$loader = $context->get_loader( 'wc_customer' );
$loader->buffer( [ $customer_id ] );
return new Deferred(
function () use ( $loader, $customer_id ) {
Expand All @@ -72,10 +72,10 @@ public static function resolve_crud_object( $id, AppContext $context ) {
return null;
}

$context->getLoader( 'wc_post' )->buffer( [ $id ] );
$context->get_loader( 'wc_post' )->buffer( [ $id ] );
return new Deferred(
function () use ( $id, $context ) {
return $context->getLoader( 'wc_post' )->load( $id );
return $context->get_loader( 'wc_post' )->load( $id );
}
);
}
Expand Down Expand Up @@ -114,7 +114,7 @@ public static function resolve_tax_rate( $id, AppContext $context ) {
}

$id = absint( $id );
$loader = $context->getLoader( 'tax_rate' );
$loader = $context->get_loader( 'tax_rate' );
$loader->buffer( [ $id ] );
return new Deferred(
function () use ( $loader, $id ) {
Expand Down Expand Up @@ -169,10 +169,10 @@ public static function resolve_cart_item( $key, AppContext $context ) {
return null;
}

$context->getLoader( 'cart_item' )->buffer( [ $key ] );
$context->get_loader( 'cart_item' )->buffer( [ $key ] );
return new Deferred(
function () use ( $key, $context ) {
return $context->getLoader( 'cart_item' )->load( $key );
return $context->get_loader( 'cart_item' )->load( $key );
}
);
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public static function resolve_downloadable_item( $id, AppContext $context ) {
return null;
}
$object_id = absint( $id );
$loader = $context->getLoader( 'downloadable_item' );
$loader = $context->get_loader( 'downloadable_item' );
$loader->buffer( [ $object_id ] );
return new Deferred(
function () use ( $loader, $object_id ) {
Expand Down
20 changes: 18 additions & 2 deletions includes/data/connection/class-order-item-connection-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,25 @@ public function get_query() {
$type = 'coupon';
break;
default:
$type = 'line_item';
/**
* Filter the $item_type to allow non-core item types.
*
* @param array $query_args The args that will be passed to the WP_Query.
* @param mixed $source The source that's passed down the GraphQL queries.
* @param array $args The inputArgs on the field.
* @param AppContext $context The AppContext passed down the GraphQL tree.
* @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree.
*/
$type = apply_filters(
'graphql_order_item_connection_item_type',
'line_item',
$this->source,
$this->args,
$this->context,
$this->info
);
break;
}
}//end switch

$items = [];
foreach ( $this->source->get_items( $type ) as $id => $item ) {
Expand Down
4 changes: 2 additions & 2 deletions includes/data/loader/class-wc-cpt-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function ( $split, \WP_Query $query ) {
case 'shop_order':
$customer_id = get_post_meta( $key, '_customer_user', true );
if ( ! empty( $customer_id ) ) {
$this->context->getLoader( 'wc_customer' )->buffer( [ $customer_id ] );
$this->context->get_loader( 'wc_customer' )->buffer( [ $customer_id ] );
}
break;
case 'product_variation':
Expand All @@ -180,7 +180,7 @@ function ( $split, \WP_Query $query ) {
$load_dependencies = new Deferred(
function() use ( $key, $post_type, $customer_id, $parent_id, $context ) {
if ( ! empty( $customer_id ) ) {
$context->getLoader( 'wc_customer' )->load( $customer_id );
$context->get_loader( 'wc_customer' )->load( $customer_id );
}
if ( ! empty( $parent_id ) ) {
$this->load( $parent_id );
Expand Down
Loading

0 comments on commit c068671

Please sign in to comment.