-
Notifications
You must be signed in to change notification settings - Fork 0
/
metorik-helper-customer-role.php
89 lines (78 loc) · 2.03 KB
/
metorik-helper-customer-role.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
<?php
/**
* Plugin Name: Metorik Helper - Customer Role
* Plugin URI: https://metorik.com
* Description: Adds customer role to WooCommerce 2.6 Customers API.
* Version: 1.0.0
* Author: Metorik
* Author URI: https://metorik.com
* WC requires at least: 2.6.0
* WC tested up to: 3.4.0.
*/
class Metorik_Helper_Customer_Role
{
/**
* Current version of Metorik.
*/
public $version = '1.0.0';
/**
* The single instance of the class.
*/
protected static $_instance = null;
/**
* Main Metorik Helper Instance.
*
* Ensures only one instance of the Metorik Helper is loaded or can be loaded.
*
* @return Metorik Helper - Main instance.
*/
public static function instance()
{
if (is_null(self::$_instance)) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Constructor.
*/
public function __construct()
{
add_action('plugins_loaded', array($this, 'init'));
}
/**
* Start plugin.
*/
public function init()
{
if (class_exists('WooCommerce')) {
// Filter customer API response
add_filter('woocommerce_rest_prepare_customer', array($this, 'filter_customer_object'), 10, 2);
}
}
public function filter_customer_object($response, $customer)
{
// if customer
if ($customer) {
// response data
$data = $response->get_data();
// roles meta
$user_meta = get_userdata($customer->ID);
$roles = $user_meta->roles;
// if role set and no role in data already
if (! isset($data['role']) && isset($roles[0])) {
$data['role'] = $roles[0];
$response->set_data($data);
}
}
return $response;
}
}
/**
* For plugin-wide access to initial instance.
*/
function Metorik_Helper_Customer_Role()
{
return Metorik_Helper_Customer_Role::instance();
}
Metorik_Helper_Customer_Role();