forked from wp-graphql/wp-graphql-woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 1
/
access-functions.php
91 lines (84 loc) · 2.26 KB
/
access-functions.php
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
<?php
/**
* This file contains access functions for various class methods
*
* @package WPGraphQL\Extensions\WooCommerce
* @since 0.0.1
*/
/**
* Adds an ObjectType to the TypeRegistry and node resolvers to Relay node definitions.
*
* @param string $type_name The name of the Type to register.
* @param array $config The Type config.
*/
function wc_register_graphql_object_type( $type_name, $config ) {
$config['kind'] = 'object';
if ( ! empty( $config['resolve_node'] ) ) {
add_filter( 'graphql_resolve_node', $config['resolve_node'], 10, 4 );
unset( $config['resolve_node'] );
}
if ( ! empty( $config['resolve_node_type'] ) ) {
add_filter( 'graphql_resolve_node_type', $config['resolve_node_type'], 10, 2 );
unset( $config['resolve_node_type'] );
}
register_graphql_type( $type_name, $config );
}
/**
* Checks if source string starts with the target string
*
* @param string $haystack - Source string.
* @param string $needle - Target string.
*
* @return bool
*/
function wc_graphql_starts_with( $haystack, $needle ) {
$length = strlen( $needle );
return ( substr( $haystack, 0, $length ) === $needle );
}
/**
* Checks if source string ends with the target string
*
* @param string $haystack - Source string.
* @param string $needle - Target string.
*
* @return bool
*/
function wc_graphql_ends_with( $haystack, $needle ) {
$length = strlen( $needle );
if ( 0 === $length ) {
return true;
}
return ( substr( $haystack, -$length ) === $needle );
}
/**
* Returns formatted array of tax statement objects.
*
* @param array $raw_taxes - array of raw taxes object from WC_Order_Item crud objects.
*
* @return array
*/
function wc_graphql_map_tax_statements( $raw_taxes ) {
$taxes = array();
foreach ( $raw_taxes as $field => $values ) {
foreach ( $values as $id => $amount ) {
if ( empty( $taxes[ $id ] ) ) {
$taxes[ $id ] = array();
}
$taxes[ $id ]['ID'] = $id;
$taxes[ $id ][ $field ] = $amount;
}
}
return array_values( $taxes );
}
/**
* Get order statuses without prefixes.
*
* @return array
*/
function wc_graphql_get_order_statuses() {
$order_statuses = array();
foreach ( array_keys( wc_get_order_statuses() ) as $status ) {
$order_statuses[] = str_replace( 'wc-', '', $status );
}
return $order_statuses;
}