Skip to content

Commit a852829

Browse files
committed
Move functions to static class.
1 parent 2e24716 commit a852829

File tree

1 file changed

+136
-81
lines changed

1 file changed

+136
-81
lines changed

wc-api-custom-meta.php

Lines changed: 136 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,157 @@
11
<?php
22
/*
3-
Plugin Name: WC API Custom Metafields
4-
Plugin URI: https://github.com/academe/TBC
5-
Description: Allows custom meta fields to be added to products when creating or updating.
6-
Version: 0.5.1
3+
Plugin Name: WC API Custom Meta
4+
Plugin URI: hhttps://github.com/judgej/wc-api-custom-meta
5+
Description: Allows access to custom meta fields on products through the API.
6+
Version: 0.6.0
77
Author: Jason Judge
88
Author URI: http://academe.co.uk
99
*/
1010

11-
// Want to hook into woocommerce_api_process_product_meta_{product_type} for all product types.
12-
13-
add_action('woocommerce_api_process_product_meta_simple', 'academe_wc_api_custom_meta', 10, 2);
14-
add_action('woocommerce_api_process_product_meta_variable', 'academe_wc_api_custom_meta', 10, 2);
15-
add_action('woocommerce_api_process_product_grouped', 'academe_wc_api_custom_meta', 10, 2);
16-
add_action('woocommerce_api_process_product_external', 'academe_wc_api_custom_meta', 10, 2);
11+
/**
12+
* Everything is static at present.
13+
* We may go singletone route in the future if there is some state
14+
* to handle, for example if the list of protected fields can be
15+
* modified - perhaps through a filter hook - at the start.
16+
*/
17+
18+
class Academe_Wc_Api_Custom_Meta
19+
{
20+
// Meta fields we want to protect, due to them being already handled
21+
// by the WC API.
22+
// To view or change these fields, go through the appropriate API.
23+
24+
protected static $protected_fields = array(
25+
// A few WP internal fields should not be exposed.
26+
'_edit_lock',
27+
'_edit_last',
28+
// All these meta fields are already present in the
29+
// product_data in some form.
30+
'_visibility',
31+
'_stock_status',
32+
'total_sales',
33+
'_downloadable',
34+
'_virtual',
35+
'_regular_price',
36+
'_sale_price',
37+
'_purchase_note',
38+
'_featured',
39+
'_weight',
40+
'_length',
41+
'_width',
42+
'_height',
43+
'_sku',
44+
'_product_attributes',
45+
'_price',
46+
'_sold_individually',
47+
'_manage_stock',
48+
'_backorders',
49+
'_stock',
50+
'_upsell_ids',
51+
'_crosssell_ids',
52+
'_product_image_gallery',
53+
'_sale_price_dates_from',
54+
'_sale_price_dates_to',
55+
);
56+
57+
protected static $product_type = array(
58+
'meta_simple',
59+
'meta_variable',
60+
'grouped',
61+
'external',
62+
);
63+
64+
/**
65+
* Initialise the hooks at plugin initialisation.
66+
*/
67+
public static function initialize()
68+
{
69+
// GET product: add in meta field to results.
70+
add_filter(
71+
'woocommerce_api_product_response',
72+
array('Academe_Wc_Api_Custom_Meta', 'fetchCustomMeta'),
73+
10,
74+
4
75+
);
1776

18-
function academe_wc_api_custom_meta($id, $data) {
19-
if (!empty($data['custom_meta']) && is_array($data['custom_meta'])) {
20-
foreach($data['custom_meta'] as $field_name => $field_value) {
21-
update_post_meta($id, $field_name, wc_clean($field_value));
77+
// Want to hook into woocommerce_api_process_product_meta_{product_type} for all product types.
78+
foreach(static::$product_type as $product_type) {
79+
add_action(
80+
'woocommerce_api_process_product_' . $product_type,
81+
array('Academe_Wc_Api_Custom_Meta', 'updateCustomMeta'),
82+
10,
83+
2
84+
);
2285
}
2386
}
2487

25-
if (!empty($data['custom_meta']) && is_array($data['remove_custom_meta'])) {
26-
foreach($data['remove_custom_meta'] as $key => $value) {
27-
// If the key is numeric, then assume $value is the field name
28-
// and all entries need to be deleted. Otherwise is is a specfic value
29-
// of a named meta field that should be removed.
88+
/**
89+
* Fetch a product.
90+
*/
91+
public static function fetchCustomMeta($product_data, $product, $fields, $server) {
92+
// The admin and shop manager will have the capability "manage_woocommerce".
93+
// We only want users with this capability to see additional product meta fields.
3094

31-
if (is_numeric($key)) {
32-
delete_post_meta($id, $value);
33-
} else {
34-
delete_post_meta($id, $key, $value);
35-
}
36-
}
37-
}
38-
}
95+
if (current_user_can('manage_woocommerce')) {
96+
$product_id = $product->id;
3997

98+
$all_meta = get_post_meta($product_id);
4099

41-
// GET product through the API
42-
add_filter('woocommerce_api_product_response', 'academe_wc_api_get_meta', 10, 4);
43-
44-
function academe_wc_api_get_meta($product_data, $product, $fields, $server) {
45-
// The admin and shop manager will have the capability "manage_woocommerce".
46-
// We only want users with this capability to see additional product meta fields.
47-
48-
if (current_user_can('manage_woocommerce')) {
49-
$product_id = $product->id;
50-
51-
// Meta fields we don't want, due to them being already in the data.
52-
$exclude = array(
53-
// A few WP internal fields should not be exposed.
54-
"_edit_lock",
55-
"_edit_last",
56-
// All these meta fields are already present in the product_data in some form.
57-
"_visibility",
58-
"_stock_status",
59-
"total_sales",
60-
"_downloadable",
61-
"_virtual",
62-
"_regular_price",
63-
"_sale_price",
64-
"_purchase_note",
65-
"_featured",
66-
"_weight",
67-
"_length",
68-
"_width",
69-
"_height",
70-
"_sku",
71-
"_product_attributes",
72-
"_price",
73-
"_sold_individually",
74-
"_manage_stock",
75-
"_backorders",
76-
"_stock",
77-
"_upsell_ids",
78-
"_crosssell_ids",
79-
"_product_image_gallery",
80-
// The sale dates are actually not directly in the normal data returned.
81-
"_sale_price_dates_from",
82-
"_sale_price_dates_to",
83-
);
100+
// Filter out meta we don't want.
101+
$all_meta = array_diff_key($all_meta, array_flip(static::$protected_fields));
84102

85-
$all_meta = get_post_meta($product_id);
103+
// Unserialize the meta field data where necessary.
104+
foreach($all_meta as $key => &$value) {
105+
$value = maybe_unserialize(reset($value));
106+
}
107+
unset($value);
86108

87-
// Filter out meta we don't want.
88-
$all_meta = array_diff_key($all_meta, array_flip($exclude));
109+
$meta = array($all_meta);
89110

90-
// Unserialize the meta field data where necessary.
91-
foreach($all_meta as $key => &$value) {
92-
$value = maybe_unserialize(reset($value));
111+
$product_data['meta'] = $meta;
93112
}
94-
unset($value);
95113

96-
$meta = array($all_meta);
97-
98-
$product_data['meta'] = $meta;
114+
return $product_data;
99115
}
100116

101-
return $product_data;
117+
/**
118+
* Update or create a product.
119+
*/
120+
public static function updateCustomMeta($id, $data) {
121+
// Create or update fields.
122+
if (!empty($data['custom_meta']) && is_array($data['custom_meta'])) {
123+
// Filter out protected fields.
124+
$custom_meta = array_diff_key(
125+
$data['custom_meta'],
126+
array_flip(static::$protected_fields)
127+
);
128+
129+
foreach($custom_meta as $field_name => $field_value) {
130+
update_post_meta($id, $field_name, wc_clean($field_value));
131+
}
132+
}
133+
134+
// Remove meta fields.
135+
if (!empty($data['remove_custom_meta']) && is_array($data['remove_custom_meta'])) {
136+
// Filter out protected fields.
137+
$remove_custom_meta = array_diff(
138+
$data['remove_custom_meta'],
139+
static::$protected_fields
140+
);
141+
142+
foreach($remove_custom_meta as $key => $value) {
143+
// If the key is numeric, then assume $value is the field name
144+
// and all entries need to be deleted. Otherwise is is a specfic value
145+
// of a named meta field that should be removed.
146+
147+
if (is_numeric($key)) {
148+
delete_post_meta($id, $value);
149+
} else {
150+
delete_post_meta($id, $key, $value);
151+
}
152+
}
153+
}
154+
}
102155
}
156+
157+
Academe_Wc_Api_Custom_Meta::initialize();

0 commit comments

Comments
 (0)