class-metabox.php
9.15 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
class WooCommerce_Role_Based_Price_Product_Metabox {
public function __construct() {
add_action('add_meta_boxes_product', array( $this, 'add_metabox' ));
}
public function add_metabox($post) {
add_meta_box('wc-rbp-product-editor', __('Role Based Price For WooCommerce Editor', WC_RBP_TXT), array(
$this,
'render_price_editor_metabox',
), 'product', 'advanced', 'high');
}
public function render_price_editor_metabox($post) {
if( is_object($post) ) {
$id = $post->ID;
} else {
$id = $post;
}
$prod = NULL;
$prodType = $this->get_post_type($id);
$url = admin_url('admin-ajax.php?action=wc_rbp_save_product_prices');
$render_info = '<div class="wc-rbp-metabox-container" method="POST" action="' . $url . '" > ';
$render_info .= wc_rbp_get_ajax_overlay();
$this->allowed_roles = wc_rbp_allowed_roles();
$this->registered_roles = wc_rbp_get_wp_roles();
$args = array();
$args['render_default'] = TRUE;
$args['html'] = '';
$args['postid'] = $id;
$args['mb'] = $this;
$args['parentID'] = isset($_REQUEST['parentID']) ? sanitize_text_field($_REQUEST['parentID']) : $id;
$args['selectedID'] = isset($_REQUEST['pid']) ? sanitize_text_field($_REQUEST['pid']) : $id;
$args = apply_filters_ref_array('wc_rbp_metabox_render', array( &$args ));
if( $args['render_default'] ) {
$render_info .= $this->render_default_metabox($id, $post, $args);
} else {
$render_info .= $args['html'];
}
$render_info .= '</div>';
echo $render_info;
}
public function get_post_type($id) {
$product_type = '';
if( $terms = wp_get_object_terms($id, 'product_type') ) {
$product_type = sanitize_title(current($terms)->name);
} else {
$product_type = apply_filters('default_product_type', 'simple');
}
return $product_type;
}
public function render_default_metabox($id, $post, $args, $type = 'default') {
$product_type = $this->get_post_type($id);
$render_info = '';
$post_type = get_post_type($id);
if( $product_type == 'variable' || $post_type == 'product_variation' ) {
$render_info .= $this->generate_variation_selectbox($args['parentID'], $id);
} else {
ob_start();
do_action("wc_rbp_metabox_header", $id, $product_type, $post, $this);
$render_info .= ob_get_contents();
ob_end_clean();
}
$prod = wc_get_product($id);
$prodType = 'simple';
if( is_object($prod) ) {
if( wc_rbp_is_wc_v('>=', '3.0.0') ) {
$prodType = $prod->get_type();
} else {
$prodType = $prod->product_type;
}
}
ob_start();
do_action('wc_rbp_before_metabox_content', $prod, $prodType);
$render_info .= ob_get_contents();
ob_end_clean();
$tabs = $this->get_metabox_tabs($id, $prodType);
$content = $this->get_metabox_content($id, $tabs, $prod, $prodType);
$render_info .= wc_rbp_generate_tabs($tabs, $content);
$render_info .= wc_rbp_get_editor_fields($type);
$render_info .= '<input type="hidden" id="wc_rbp_product_id" name="product_id" value="' . $id . '" /> ';
ob_start();
do_action('wc_rbp_after_metabox_content', $prod, $prodType);
$render_info .= ob_get_contents();
ob_end_clean();
$render_info .= $this->render_metabox_footer($id);
return $render_info;
}
public function generate_variation_selectbox($id, $selected = '') {
$header = $this->render_selectbox_header();
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'posts_per_page' => -1,
'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'DESC' ),
'post_parent' => $id,
'fields' => 'ids',
);
$variations = get_children($args);
$return = ' <optgroup label="' . __("Variations", WC_RBP_TXT) . '"> ';
foreach( $variations as $ids ) {
$prod = wc_get_product($ids);
$name = '#' . $ids . ' | ';
if( wc_rbp_is_wc_v('>=', '3.0') ) {
$name .= ' ' . wc_get_formatted_variation($prod, TRUE);
} else {
$name .= ' ' . $prod->get_formatted_variation_attributes(TRUE);
}
$selecteds = '';
if( $selected == $ids ) {
$selecteds = 'selected';
}
$return .= '<option data-type="variation" value="' . $ids . '" ' . $selecteds . '>' . $name . '</option>';
}
$return .= ' </optgroup> ';
$footer = $this->render_selectbox_footer();
$return = apply_filters("role_based_price_metabox_variation_select", $return, $selected, $id);
$return = $header . $return . $footer;
return $return;
}
public function render_selectbox_header($placeholder = '') {
if( empty($placeholder) ) {
$placeholder = __("Select A Variation : ", WC_RBP_TXT);
}
$header = '<select id="wc_rbp_variation_select" style="width: 30%; display: inline-block; vertical-align: middle; margin-left: 10px;" name="wc_rbp_variation_select" class="wcrbpvariationbx"> <option value="">' . $placeholder . '</option>';
return apply_filters("role_based_price_admin_selectbox_header", $header);
}
public function render_selectbox_footer() {
return apply_filters("role_based_price_admin_selectbox_header", '</select>');
}
public function get_metabox_tabs($id, $prodType) {
$tabs = apply_filters('wc_rbp_before_default_product_tabs', array(), $id, $prodType);
$registered_roles = $this->registered_roles;
foreach( $this->allowed_roles as $role ) {
if( isset($registered_roles[$role]) ) {
$icon = 'dashicons dashicons-admin-users';
$tabs[$role] = array(
'title' => $registered_roles[$role]['name'],
'icon' => $icon,
'show_status' => TRUE,
);
}
}
$tabs = apply_filters('wc_rbp_after_default_product_tabs', $tabs, $id, $prodType);
return $tabs;
}
public function get_metabox_content($id, $tabs, $prod, $prodType) {
$content = array();
foreach( $tabs as $tab_id => $val ) {
ob_start();
do_action('wc_rbp_price_edit_tab_' . $tab_id . '_before', $id, $prodType, $prod, $tab_id);
do_action('wc_rbp_price_edit_tab_' . $tab_id, $id, $prodType, $prod, $tab_id);
do_action('wc_rbp_price_edit_tab_' . $tab_id . '_after', $id, $prodType, $prod, $tab_id);
$content[$tab_id] = ob_get_contents();
ob_end_clean();
}
return $content;
}
public function render_metabox_footer($id) {
$base_price = $this->get_base_price($id);
$clbtn = '';
$product_type = wp_get_post_terms($id, 'product_type', array( 'fields' => 'names' ));
if( in_array('variable', $product_type) || get_post_type($id) == 'product_variation' ) {
$clbtn = '<button style="float:left;" type="button" id="wc_rbp_clear_trasient" class="button button-secondary">' . __('Clear Cache', WC_RBP_TXT) . '</button>';
}
return ' <h2 class="" style="margin: 0px -12px -12px; border-top: 1px solid #eee; text-align:right;">
<span class="wc_rbp_base_product_price">' . $base_price . '</span>
' . $clbtn . '
<button type="button" id="wc_rbp_update_price" class="button button-primary">' . __('Save Price', WC_RBP_TXT) . '</button></h2> ';
}
public function get_base_price($id) {
$pro = wc_get_product($id);
$price = '';
if( is_object($pro) ) {
$price = array();
$this->hook_filter(TRUE);
$price['regular_price'] = wc_rbp_price_types('regular_price') . ' : ';
$price['regular_price'] .= wc_price($pro->get_regular_price());
$price['selling_price'] = wc_rbp_price_types('selling_price') . ' : ';
$price['selling_price'] .= wc_price($pro->get_sale_price());
$this->hook_filter(FALSE);
$price = implode(' | ', $price);
}
$head = '<span class="headTxt">' . __("WC Product Price : ") . '</span>' . $price;
return $head;
}
public function hook_filter($hook = TRUE) {
if( $hook == TRUE ) {
add_filter('role_based_price_status', array( $this, 'base_price_return_false' ));
}
if( ! $hook == TRUE ) {
remove_filter('role_based_price_status', array( $this, 'base_price_return_false' ));
}
}
public function base_price_return_false($s) {
return FALSE;
}
}
return new WooCommerce_Role_Based_Price_Product_Metabox;