diff --git a/includes/class-woocommerce-filters.php b/includes/class-woocommerce-filters.php new file mode 100644 index 00000000..de916077 --- /dev/null +++ b/includes/class-woocommerce-filters.php @@ -0,0 +1,112 @@ +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;