some key files restored.

This commit is contained in:
Geoff Taylor
2019-11-13 17:35:44 -05:00
parent 5d6cab84c6
commit 8efaa910cc
2 changed files with 289 additions and 0 deletions
+112
View File
@@ -0,0 +1,112 @@
<?php
/**
* Adds filters that modify woocommerce functionality on GraphQL requests.
*
* @package \WPGraphQL\WooCommerce
* @since 0.2.0
*/
namespace WPGraphQL\WooCommerce;
use WPGraphQL\WooCommerce\Utils\QL_Session_Handler;
/**
* Class WooCommerce_Filters
*/
class WooCommerce_Filters {
/**
* Stores instance session header name.
*
* @var string
*/
private static $session_header;
/**
* Register filters
*/
public static function add_filters() {
// Setup QL session handler.
if ( ! defined( 'NO_QL_SESSION_HANDLER' ) ) {
// Check if request is a GraphQL POST request.
self::$session_header = apply_filters( 'woocommerce_graphql_session_header_name', 'woocommerce-session' );
if ( self::is_graphql_post_request() ) {
add_filter( 'woocommerce_cookie', array( __CLASS__, 'woocommerce_cookie' ) );
add_filter( 'woocommerce_session_handler', array( __CLASS__, 'init_ql_session_handler' ) );
}
add_filter( 'graphql_response_headers_to_send', array( __CLASS__, 'add_session_header_to_expose_headers' ) );
add_filter( 'graphql_access_control_allow_headers', array( __CLASS__, 'add_session_header_to_allow_headers' ) );
}
}
/**
* Filters WooCommerce cookie key to be used as a HTTP Header on GraphQL HTTP requests
*
* @param string $cookie WooCommerce cookie key.
*
* @return string
*/
public static function woocommerce_cookie( $cookie ) {
return self::$session_header;
}
/**
* Filters WooCommerce session handler class on GraphQL HTTP requests
*
* @param string $session_class Classname of the current session handler class.
*
* @return string
*/
public static function init_ql_session_handler( $session_class ) {
return QL_Session_Handler::class;
}
/**
* Append session header to the exposed headers in GraphQL responses
*
* @param array $headers GraphQL responser headers.
*
* @return array
*/
public static function add_session_header_to_expose_headers( $headers ) {
if ( empty( $headers['Access-Control-Expose-Headers'] ) ) {
$headers['Access-Control-Expose-Headers'] = apply_filters( 'woocommerce_cookie', self::$session_header );
} else {
$headers['Access-Control-Expose-Headers'] .= ', ' . apply_filters( 'woocommerce_cookie', self::$session_header );
}
return $headers;
}
/**
* Append the session header to the allowed headers in GraphQL responses
*
* @param array $allowed_headers The existing allowed headers.
*
* @return array
*/
public static function add_session_header_to_allow_headers( array $allowed_headers ) {
$allowed_headers[] = self::$session_header;
return $allowed_headers;
}
/**
* Confirm that the current uri is the GraphQL endpoint.
*
* @return bool
*/
private static function is_graphql_post_request() {
global $wp;
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$haystack = esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) );
$needle = apply_filters( 'graphql_endpoint', 'graphql' );
$length = strlen( $needle );
if ( 0 === $length ) {
return true;
}
return ( substr( $haystack, -$length ) === $needle );
}
return false;
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
/**
* WP_GraphQL_WooCommerce
*
* Initializes a singleton instance of WP_GraphQL_WooCommerce
*
* @package WPGraphQL\WooCommerce
* @since 0.0.1
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'WP_GraphQL_WooCommerce' ) ) :
/**
* Class WP_GraphQL_WooCommerce
*/
final class WP_GraphQL_WooCommerce {
/**
* Stores the instance of the WPGraphQL\Extensions\WPGraphQLWooCommerce class
*
* @var WP_GraphQL_WooCommerce The one true WPGraphQL\Extensions\WP_GraphQL_WooCommerce
* @access private
*/
private static $instance;
/**
* WP_GraphQL_WooCommerce Constructor
*/
public static function instance() {
if ( ! isset( self::$instance ) && ! ( is_a( self::$instance, __CLASS__ ) ) ) {
self::$instance = new self();
self::$instance->includes();
self::$instance->setup();
}
/**
* Fire off init action
*
* @param WP_GraphQL_WooCommerce $instance The instance of the WPGraphQLWooCommerce class
*/
do_action( 'graphql_woocommerce_init', self::$instance );
/**
* Return the WPGraphQLWooCommerce Instance
*/
return self::$instance;
}
/**
* Returns WooCommerce post-types registered to the WC_Post_Crud_Loader
*
* @return array
*/
public static function get_post_types() {
return apply_filters(
'register_graphql_wc_post_types',
array(
'product',
'product_variation',
'shop_coupon',
'shop_order',
'shop_order_refund',
)
);
}
/**
* Returns WooCommerce product types to be exposed to the GraphQL schema.
*/
public static function get_enabled_product_types() {
return apply_filters(
'graphql_enabled_wc_product_types',
array(
'simple' => 'SimpleProduct',
'variable' => 'VariableProduct',
'external' => 'ExternalProduct',
'grouped' => 'GroupProduct',
)
);
}
/**
* Returns WooCommerce product attribute taxonomies to be registered as
* "TermObject" types in the schema.
*
* @return array
*/
public static function get_product_attribute_taxonomies() {
$attribute_taxonomies = \wc_get_attribute_taxonomies();
// Get taxonomy names.
$attributes = array();
foreach ( $attribute_taxonomies as $tax ) {
$attributes[] = 'pa_' . $tax->attribute_name;
}
/**
* Filter the $attributes to allow the removal or addition of product attribute taxonomies
*
* @param array $attributes Product attributes being passed.
*/
return apply_filters(
'register_graphql_wc_product_attributes_taxonomies',
$attributes
);
}
/**
* Throw error on object clone.
* The whole idea of the singleton design pattern is that there is a single object
* therefore, we don't want the object to be cloned.
*
* @since 0.0.1
* @access public
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'WP_GraphQL_WooCommerce class should not be cloned.', 'wp-graphql-woocommerce' ), '0.0.1' );
}
/**
* Disable unserializing of the class.
*
* @since 0.0.1
* @access protected
* @return void
*/
public function __wakeup() {
// De-serializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, esc_html__( 'De-serializing instances of the WP_GraphQL_WooCommerce class is not allowed', 'wp-graphql-woocommerce' ), '0.0.1' );
}
/**
* Include required files.
* Uses composer's autoload
*
* @access private
* @since 0.0.1
* @return void
*/
private function includes() {
/**
* Autoload Required Classes
*/
if ( defined( 'WPGRAPHQL_WOOCOMMERCE_AUTOLOAD' ) && false !== WPGRAPHQL_WOOCOMMERCE_AUTOLOAD ) {
require_once WPGRAPHQL_WOOCOMMERCE_PLUGIN_DIR . 'vendor/autoload.php';
}
// Required non-autoloaded classes.
require_once WPGRAPHQL_WOOCOMMERCE_PLUGIN_DIR . 'access-functions.php';
require_once WPGRAPHQL_WOOCOMMERCE_PLUGIN_DIR . 'class-inflect.php';
}
/**
* Sets up WooGraphQL schema.
*/
private function setup() {
// Register WooCommerce filters.
\WPGraphQL\WooCommerce\WooCommerce_Filters::add_filters();
// Register WPGraphQL core filters.
\WPGraphQL\WooCommerce\Core_Schema_Filters::add_filters();
// Register WPGraphQL ACF filters.
\WPGraphQL\WooCommerce\ACF_Schema_Filters::add_filters();
// Register WPGraphQL JWT Authentication filters.
\WPGraphQL\WooCommerce\JWT_Auth_Schema_Filters::add_filters();
$registry = new \WPGraphQL\WooCommerce\Type_Registry();
add_action( 'graphql_register_types', array( $registry, 'init' ), 10, 1 );
}
}
endif;