-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwp-graphql-woocommerce-gold-price.php
157 lines (128 loc) · 4.99 KB
/
wp-graphql-woocommerce-gold-price.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* Plugin Name: WPGraphQL Gold price Extension for woocommerce
* Description: Add WooCommerce Gold Prices support and functionality to your WPGraphQL server
* Author: Vino Crazy
* Author URI: https://vinocrazy.com/
* Version: 1.0.0
* Requires PHP: 7.0
* GitHub Plugin URI: https://github.com/vinocrzy/wp-graphql-woocommerce-gold-price
*
* @package WPGraphQL_WOO_Gold
*/
add_action('graphql_register_types', 'gold_price');
function gold_price()
{
register_graphql_object_type('GoldPriceType', [
'description' => __('Describe the Type and what it represents', 'wp-graphql-gold-price'),
'fields' => [
'gold24k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold24k', 'wp-graphql-gold-price'),
'resolve' => function ($source) {
return (float) $source['24k'];
},
],
'gold22k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold22k', 'wp-graphql-gold-price'),
'resolve' => function ($source) {
return (float) $source['22k'];
},
],
'gold18k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold18k', 'wp-graphql-gold-price'),
'resolve' => function ($source) {
return (float) $source['18k'];
},
],
'gold14k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold14k', 'wp-graphql-gold-price'),
'resolve' => function ($source) {
return (float) $source['14k'];
},
],
],
]);
register_graphql_fields('RootQuery', [
'goldPriceList' => [
'type' => 'GoldPriceType',
'description' => __('Woocommerce Gold Price Options', 'wp-graphql-gold-price'),
'resolve' => function () {
$karats = get_option('woocommerce_gold_price_options');
return $karats;
}
]
]);
register_graphql_mutation('setGoldPrice', [
# inputFields expects an array of Fields to be used for inputting values to the mutation
'inputFields' => [
'gold24k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold24k', 'wp-graphql-gold-price'),
],
'gold22k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold22k', 'wp-graphql-gold-price'),
],
'gold18k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold18k', 'wp-graphql-gold-price'),
],
'gold14k' => [
'type' => 'Float',
'description' => __('Woocommerce Gold Price Options gold14k', 'wp-graphql-gold-price'),
],
],
# outputFields expects an array of fields that can be asked for in response to the mutation
# the resolve function is optional, but can be useful if the mutateAndPayload doesn't return an array
# with the same key(s) as the outputFields
'outputFields' => [
'gold24k' => [
'type' => 'String',
'description' => __('Description of the output field', 'your-textdomain'),
'resolve' => function ($payload, $args, $context, $info) {
return isset($payload['out']) ? json_encode($payload['out']) : null;
}
]
],
# mutateAndGetPayload expects a function, and the function gets passed the $input, $context, and $info
# the function should return enough info for the outputFields to resolve with
'mutateAndGetPayload' => function ($input, $context, $info) {
// Do any logic here to sanitize the input, check user capabilities, etc
$Output = $input;
return [
'out' => $Output,
];
}
]);
};
/**
* Check whether Woocommerce Gold Price and WPGraphQL are active, and whether the minimum version requirement has been
* met
*
* @return bool
* @since 0.3
*/
function can_load_plugin()
{
// Is Woocommerce Gold Price active?
if (!class_exists('woocommerce_gold_price')) {
return false;
}
// Is WPGraphQL active?
if (!class_exists('WPGraphQL')) {
return false;
}
// Do we have a WPGraphQL version to check against?
if (empty(defined('WPGRAPHQL_VERSION'))) {
return false;
}
// Have we met the minimum version requirement?
if (true === version_compare(WPGRAPHQL_VERSION, WPGRAPHQL_REQUIRED_MIN_VERSION, 'lt')) {
return false;
}
return true;
}