-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDmnVendorFields.php
197 lines (168 loc) · 7.11 KB
/
DmnVendorFields.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
/**
* DmnVendorFields Class
* Adds Custom Fields for Vendor
*/
defined('ABSPATH') || exit();
include_once(DMN_THEME_DIR . '/includes/classes/DmnFieldFactory.php');
class DmnVendorFields extends DmnFieldFactory{
private $fields = [];
public function __construct($fields = [])
{
if($fields) $this->setFields($fields);
$this->installHooks();
}
private function installHooks(){
// add fields when wp-admin is Initialised
add_action( 'admin_init',array($this, 'registerVendorFields'));
// Add Fields to Vendor Registration Form
add_action( 'wcpv_registration_form', array($this, 'vendors_reg_custom_fields' ));
// Validate Fields from Vendor Registration Form
add_filter( 'wcpv_shortcode_registration_form_validation_errors', array($this,'vendors_reg_custom_fields_validation'), 10, 2 );
// Save Fields of Vendor Registration Form
add_action( 'wcpv_shortcode_registration_form_process', array($this, 'vendors_reg_custom_fields_save'), 10, 2 );
}
public function registerVendorFields(){
// Add Fields to Admins when adding a new Vendor Taxonomy (Optionally can be added with PODS or ACF)
add_action( DMN_VENDOR_TAX . '_add_form_fields', array($this, 'add_vendor_custom_fields' ));
// Add Fields to Admins when editing a new Vendor Taxonomy (Optionally can be added with PODS or ACF)
add_action( DMN_VENDOR_TAX . '_edit_form_fields', array($this,'edit_vendor_custom_fields'), 10 );
// Save the Fields for Taxonomy
// add_action( 'edited_' . DMN_VENDOR_TAX, array($this, 'save_vendor_custom_fields') );
// add_action( 'created_' . DMN_VENDOR_TAX, array($this,'save_vendor_custom_fields') );
// Render Fields for Vendor Dashboard Store Settings (Can't be added with PODS or ACF, and we can decide which fields are editable by the Vendor)
add_action( 'wcpv_vendor_settings_render_additional_fields', array($this, 'add_fields_vendor_store_settings' ));
// Research in Progress Hook to save fields on Dashboard Store Settings
}
/**
* @param Array $fields
* @key String label
* @key String type
* @key String name
* @key Boolean vendor_access (Default true)
* @key Boolean reg_form (Default false)
*/
public function setFields($fields){
if(!$fields) return;
foreach($fields as $field){
$vendorAccess = isset($field['vendor_access']) ? $field['vendor_access'] : true;
$showRegFrom = isset($field['reg_form']) ? $field['reg_form'] : false;
$options = isset($field['options']) ? $field['options'] : [];
$css = isset($field['css']) ? $field['css'] : '';
$filter = isset($field['filter']) ? $field['filter'] : '';
$this->setField($field['name'], $field['label'], $field['type'], $field['description'], $vendorAccess, $showRegFrom, $options, $css, $filter);
}
}
private function setField($name, $label, $type, $desc, $vendorAccess = true, $showRegFrom = false, $options = [], $css = '', $filter = ''){
$this->fields[] = array(
'name' => $name,
'data_array' => 'vendor_data',
'label' => $label,
'type' => $type,
'desc' => $desc,
'options' => $options,
'vendorAccess' => $vendorAccess,
'sowInRegform' => $showRegFrom,
'css' => $css,
'filter' => $filter,
);
}
public function add_vendor_custom_fields(){
wp_nonce_field( basename( __FILE__ ), 'vendor_custom_fields_nonce' );
foreach($this->fields as $field){
echo $this->getFieldHTML($field, '', 'add_term');
}
}
public function edit_vendor_custom_fields($term){
$value = get_term_meta( $term->term_id, 'vendor_data', true );
foreach($this->fields as $field){
echo $this->getFieldHTML($field,$value[$field['name']], 'edit_term');
}
}
public function add_fields_vendor_store_settings(){
$vendor_custom_data_array = WC_Product_Vendors_Utils::get_vendor_data_from_user();
foreach($this->fields as $field){
if ($field['vendorAccess']){
echo $this->getFieldHTML($field, $vendor_custom_data_array[$field['name']], 'edit_term');
}
}
?>
<?php
}
public function vendors_reg_custom_fields() {
foreach($this->fields as $field){
if($field['sowInRegform']){
echo $this->getFieldHTML($field, '', 'reg_form');
}
}
}
public function vendors_reg_custom_fields_validation( $errors, $form_items ) {
foreach($this->fields as $field){
if($field['sowInRegform']){
if(empty($form_items[$field['name']])){
$errors[$field['name']] = __( $field['label'].' field cannot be empty.', 'oceanwp' );
}
if(!empty($field['filter']) && filter_var( $form_items[$field['name']], FILTER_VALIDATE_URL ) === false){
$errors[$field['name']] = __( $field['label'].' field format is not correct.', 'oceanwp' );
}
}
}
return $errors;
}
public function vendors_reg_custom_fields_save( $args, $items ) {
$term = get_term_by( 'name', $items['vendor_name'], DMN_VENDOR_TAX );
$data = get_term_meta( $term->term_id, 'vendor_data', true );
foreach($this->fields as $field){
if($field['sowInRegform']){
if ( isset( $items[$field['name']] ) && ! empty( $items[$field['name']] ) ) {
$data[$field['name']] = $items[$field['name']];
}
}
}
update_term_meta( $term->term_id, 'vendor_data', $data );
}
}
$countries_obj = new WC_Countries();
$fields = array(
array(
'name' => 'vendor_contact',
'type' => 'tel',
'label' => 'Contact Number',
'description' => '',
'reg_form' => true,
'css' => 'form-row-wide'
),
array(
'name' => 'vendor_province',
'type' => 'select',
'label' => 'Province',
'description' => '',
'reg_form' => true,
'css' => 'form-row-first',
'options' => $countries_obj->get_states( "ZA" )
),
array(
'name' => 'vendor_suburb',
'type' => 'text',
'label' => 'Suburb',
'reg_form' => true,
'css' => 'form-row-last',
'description' => ''
),
array(
'name' => 'vendor_address',
'type' => 'text',
'label' => 'Address',
'description' => '',
'reg_form' => true,
'css' => 'form-row-wide'
),
array(
'name' => 'vendor_directions',
'type' => 'url',
'label' => 'Google Map Link',
'description' => 'Insert Shareable link, to get it got to: Google Maps, seach for the name of the place, click on share and get the Send a link url.',
'filter' => FILTER_VALIDATE_URL,
),
);
$fieldobj = new DmnVendorFields($fields);