|
1
|
|
|
<?php if ( !defined( 'ABSPATH' ) ) exit; |
|
2
|
|
|
class wps_barcode { |
|
3
|
|
|
public function __construct() { |
|
4
|
|
|
global $post_ID; |
|
5
|
|
|
|
|
6
|
|
|
$this->install(); |
|
7
|
|
|
add_action( 'save_post', array($this, 'insert_barcode' ), 10, 3 ); |
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Install default configuration in database if not present |
|
12
|
|
|
*/ |
|
13
|
|
|
public function install() { |
|
14
|
|
|
global $table_prefix, $wpdb; |
|
15
|
|
|
|
|
16
|
|
|
/*Create config informations*/ |
|
17
|
|
|
$conf = get_option('wps_barcode'); |
|
18
|
|
|
|
|
19
|
|
|
if ( empty($conf) ) { |
|
20
|
|
|
$conf['generate_barcode'] = false; |
|
21
|
|
|
$conf['type'] = 'internal'; |
|
22
|
|
|
$conf['internal_client'] = '040'; |
|
23
|
|
|
$conf['internal_provider'] = '041'; |
|
24
|
|
|
$conf['internal_invoice_client'] = '042'; |
|
25
|
|
|
$conf['internal_do_client'] = '043'; |
|
26
|
|
|
$conf['internal_product'] = '044'; |
|
27
|
|
|
$conf['internal_assets_client'] = '050'; |
|
28
|
|
|
$conf['internal_coupons'] = '051'; |
|
29
|
|
|
$conf['internal_invoice_provider'] = '045'; |
|
30
|
|
|
$conf['internal_do_provider'] = '046'; |
|
31
|
|
|
|
|
32
|
|
|
$conf['normal_country_code'] = '320'; |
|
33
|
|
|
$conf['normal_enterprise_code'] = '0001'; |
|
34
|
|
|
|
|
35
|
|
|
add_option('wps_barcode', $conf); |
|
36
|
|
|
|
|
37
|
|
|
/*Adding barcode for existing products*/ |
|
38
|
|
|
$posts = get_posts( array('posts_per_page' => -1 ,'post_type' => 'wpshop_product')); |
|
|
|
|
|
|
39
|
|
|
foreach ( $posts as $post ) { |
|
|
|
|
|
|
40
|
|
|
$meta = get_post_meta($post->ID); |
|
41
|
|
|
|
|
42
|
|
|
$attr = wpshop_attributes_set::getAttributeSetDetails($meta['_wpshop_product_attribute_set_id']); |
|
43
|
|
|
$output_order = array(); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
if ( count($attr) > 0 ) { |
|
46
|
|
|
if (!empty($attr) ) { |
|
47
|
|
|
foreach ( $attr as $product_attr_group_id => |
|
|
|
|
|
|
48
|
|
|
$product_attr_group_detail) { |
|
49
|
|
|
foreach ( $product_attr_group_detail['attribut'] |
|
50
|
|
|
as $position => $attribute_def) { |
|
51
|
|
|
if ( !empty($attribute_def->code) |
|
52
|
|
|
AND $attribute_def->code === 'barcode') { |
|
53
|
|
|
|
|
54
|
|
|
$types_with_barcode = array( |
|
55
|
|
|
WPSHOP_IDENTIFIER_PRODUCT, |
|
56
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, |
|
57
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, |
|
58
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_COUPON, |
|
59
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_ORDER, |
|
60
|
|
|
); |
|
61
|
|
|
|
|
62
|
|
|
if ( !empty( $post->post_type ) && |
|
63
|
|
|
in_array( $post->post_type, |
|
64
|
|
|
$types_with_barcode ) ) { |
|
65
|
|
|
|
|
66
|
|
|
$ref = ''; |
|
67
|
|
|
|
|
68
|
|
View Code Duplication |
if ( strlen($post->ID) < 5 ) { |
|
|
|
|
|
|
69
|
|
|
$length = 5-strlen($post->ID); |
|
70
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
71
|
|
|
$ref .= '0'; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$ref .= strval($post->ID); |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
if ( $conf['type'] === 'normal' ) { |
|
|
|
|
|
|
78
|
|
|
$array['normal'] = array('country' => |
|
|
|
|
|
|
79
|
|
|
$conf['normal_country_code'], |
|
80
|
|
|
'enterprise' => |
|
81
|
|
|
$conf['normal_enterprise_code'],'ID' => $ref); |
|
82
|
|
|
} |
|
83
|
|
|
else if ( $conf['type'] === 'internal' ) { |
|
84
|
|
|
$pDate = new DateTime($post->post_date); |
|
85
|
|
|
$date = $pDate->format('my'); |
|
86
|
|
|
|
|
87
|
|
|
if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT || |
|
88
|
|
|
$post->post_type === WPSHOP_IDENTIFIER_PRODUCT) { |
|
89
|
|
|
$type = $conf['internal_product']; |
|
90
|
|
|
} |
|
91
|
|
|
else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_ORDER) { |
|
92
|
|
|
$type = $conf['internal_invoice_client']; |
|
93
|
|
|
} |
|
94
|
|
|
else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) { |
|
95
|
|
|
$type = $conf['internal_coupons']; |
|
96
|
|
|
} |
|
97
|
|
|
else { |
|
98
|
|
|
$type = '000'; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$array['internal'] = array('type' => $type, |
|
|
|
|
|
|
102
|
|
|
'date' => $date, |
|
103
|
|
|
'ID' => $ref); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$barcode = $this->wps_generate_barcode($array); |
|
|
|
|
|
|
107
|
|
|
$data = array('entity_type_id' => 4, |
|
108
|
|
|
'attribute_id' => '12', |
|
109
|
|
|
'entity_id' => $post->ID, |
|
110
|
|
|
'user_id' => $post->post_author, |
|
111
|
|
|
'value' => $barcode, |
|
112
|
|
|
'creation_date_value' => date( 'Y-m-d H:i:s' )); |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$query = $wpdb->prepare( " |
|
116
|
|
|
SELECT * |
|
117
|
|
|
FROM " . WPSHOP_DBT_ATTRIBUTE_VALUES_VARCHAR . " |
|
118
|
|
|
WHERE entity_id = %d |
|
119
|
|
|
AND attribute_id = ( |
|
120
|
|
|
SELECT id |
|
121
|
|
|
FROM " . WPSHOP_DBT_ATTRIBUTE . " |
|
122
|
|
|
WHERE code = %s |
|
123
|
|
|
)" , $post->ID, 'barcode' ); |
|
124
|
|
|
$result = $wpdb->get_results( $query, ARRAY_A ); |
|
125
|
|
|
|
|
126
|
|
|
if ( empty($result) ) { |
|
127
|
|
|
$wpdb->insert($table_prefix. |
|
128
|
|
|
'wpshop__attribute_value_varchar', $data); |
|
129
|
|
|
} elseif( empty( $result[0]['value'] ) ) { |
|
130
|
|
|
$wpdb->update($table_prefix. |
|
131
|
|
|
'wpshop__attribute_value_varchar', $data, array( 'value_id' => $result[0]['value_id'] ) ); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/*Adding barcode for existing coupon*/ |
|
142
|
|
|
$posts = get_posts( array('posts_per_page' => -1 ,'post_type' => 'wpshop_shop_coupon') ); |
|
|
|
|
|
|
143
|
|
|
foreach ( $posts as $post ) { |
|
|
|
|
|
|
144
|
|
|
$meta = get_post_meta($post->ID); |
|
145
|
|
|
if ( empty($meta['wpshop_coupon_barcode']) ) { |
|
146
|
|
|
if ( $conf['type'] === 'normal' ) { |
|
147
|
|
|
$array['normal'] = array('country' => |
|
148
|
|
|
$conf['normal_country_code'], |
|
149
|
|
|
'enterprise' => |
|
150
|
|
|
$conf['normal_enterprise_code'],'ID' => $ref); |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
else if ( $conf['type'] === 'internal' ) { |
|
153
|
|
|
$pDate = new DateTime($post->post_date); |
|
154
|
|
|
$date = $pDate->format('my'); |
|
155
|
|
|
|
|
156
|
|
|
if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) { |
|
157
|
|
|
$type = $conf['internal_coupons']; |
|
158
|
|
|
} |
|
159
|
|
|
else { |
|
160
|
|
|
$type = '000'; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
$ref = ''; |
|
164
|
|
View Code Duplication |
if ( strlen($post->ID) < 5 ) { |
|
|
|
|
|
|
165
|
|
|
$length = 5-strlen($post->ID); |
|
166
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
167
|
|
|
$ref .= '0'; |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$ref .= strval($post->ID); |
|
172
|
|
|
$array['internal'] = array('type' => $type, |
|
173
|
|
|
'date' => $date, |
|
174
|
|
|
'ID' => $ref); |
|
175
|
|
|
} |
|
176
|
|
|
$barcode = $this->wps_generate_barcode($array); |
|
177
|
|
|
update_post_meta($post->ID, 'wpshop_coupon_barcode', $barcode); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Verifty post identifier and run a barcode generator |
|
185
|
|
|
* @param string $post_ID Ident of post |
|
186
|
|
|
*/ |
|
187
|
|
|
public function insert_barcode( $post_ID, $post, $update ) { |
|
|
|
|
|
|
188
|
|
|
if ( wp_is_post_revision( $post_ID ) ) |
|
189
|
|
|
return; |
|
190
|
|
|
|
|
191
|
|
|
global $wpdb; |
|
192
|
|
|
|
|
193
|
|
|
$types_with_barcode = array( |
|
194
|
|
|
WPSHOP_IDENTIFIER_PRODUCT, |
|
195
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, |
|
196
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, |
|
197
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_COUPON, |
|
198
|
|
|
WPSHOP_NEWTYPE_IDENTIFIER_ORDER, |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
/** Si c'est un type qui est dans le tableau $types_with_barcode */ |
|
202
|
|
|
if ( !empty( $post->post_type ) && in_array( $post->post_type, |
|
203
|
|
|
$types_with_barcode ) ) { |
|
204
|
|
|
|
|
205
|
|
|
$conf = get_option('wps_barcode'); |
|
206
|
|
|
$ref = ''; |
|
207
|
|
|
|
|
208
|
|
View Code Duplication |
if ( strlen($post_ID) < 5 ) { |
|
|
|
|
|
|
209
|
|
|
$length = 5-strlen($post_ID); |
|
210
|
|
|
for ($i = 0; $i < $length; $i++) { |
|
211
|
|
|
$ref .= '0'; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$ref .= strval($post_ID); |
|
216
|
|
|
|
|
217
|
|
View Code Duplication |
if ( $conf['type'] === 'normal' ) { |
|
|
|
|
|
|
218
|
|
|
$array['normal'] = array( |
|
|
|
|
|
|
219
|
|
|
'country' => $conf['normal_country_code'], |
|
220
|
|
|
'enterprise' => $conf['normal_enterprise_code'], |
|
221
|
|
|
'ID' => $ref, |
|
222
|
|
|
); |
|
223
|
|
|
} |
|
224
|
|
|
else if ( $conf['type'] === 'internal' ) { |
|
225
|
|
|
$pDate = new DateTime($post->post_date); |
|
226
|
|
|
$date = $pDate->format('my'); |
|
227
|
|
|
|
|
228
|
|
|
if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT || $post->post_type === WPSHOP_IDENTIFIER_PRODUCT) { |
|
229
|
|
|
$type = $conf['internal_product']; |
|
230
|
|
|
} |
|
231
|
|
|
else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_ORDER) { |
|
232
|
|
|
$type = $conf['internal_invoice_client']; |
|
233
|
|
|
} |
|
234
|
|
|
else if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) { |
|
235
|
|
|
$type = $conf['internal_coupons']; |
|
236
|
|
|
} |
|
237
|
|
|
else { |
|
238
|
|
|
$type = '000'; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
$array['internal'] = array( |
|
|
|
|
|
|
242
|
|
|
'type' => $type, |
|
243
|
|
|
'date' => $date, |
|
244
|
|
|
'ID' => $ref |
|
245
|
|
|
); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
//$barcode = $this->wps_generate_barcode($array); |
|
|
|
|
|
|
249
|
|
|
/** For add a product */ |
|
250
|
|
|
$_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = !empty( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ) ? sanitize_text_field( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ) : null; |
|
251
|
|
|
if( !isset($_REQUEST['wpshop_product_attribute']['varchar']['barcode']) ) { |
|
252
|
|
|
wpeologs_ctr::log_datas_in_files( 'wps_barcode', |
|
253
|
|
|
array( |
|
254
|
|
|
'object_id' => $post_ID, |
|
255
|
|
|
'message' => sprintf( __('Adding barcode: %s for %s object ID', 'wps_barcode'), '<b>'.$_REQUEST['wpshop_product_attribute']['varchar']['barcode'].'</b>', '<b>'.$post_ID.'</b>') ), |
|
256
|
|
|
0); |
|
257
|
|
|
$_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = $this->wps_generate_barcode($array); |
|
|
|
|
|
|
258
|
|
|
} |
|
259
|
|
|
/*if ( isset($barcode) ) { |
|
260
|
|
|
if ($barcode !== '') { |
|
261
|
|
|
wpeologs_ctr::log_datas_in_files( 'wps_barcode', |
|
262
|
|
|
array( |
|
263
|
|
|
'object_id' => $post_ID, |
|
264
|
|
|
'message' => sprintf( __('Change barcode: %s replacing %s for %s object ID', 'wps_barcode'), '<b>'.sanitize_text_field( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ).'</b>', '<b>'.$barcode.'</b>', '<b>'.$post_ID.'</b>') ), 0); |
|
265
|
|
|
|
|
266
|
|
|
$barcode = sanitize_text_field( $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] ); |
|
267
|
|
|
} |
|
268
|
|
|
else { |
|
269
|
|
|
wpeologs_ctr::log_datas_in_files( 'wps_barcode', |
|
270
|
|
|
array( |
|
271
|
|
|
'object_id' => $post_ID, |
|
272
|
|
|
'message' => sprintf( __('Adding barcode: %s for %s object ID', 'wps_barcode'), '<b>'.$barcode.'</b>', '<b>'.$post_ID.'</b>') ), |
|
273
|
|
|
0); |
|
274
|
|
|
|
|
275
|
|
|
// $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = $barcode; |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
else { |
|
279
|
|
|
/** On met à jour l'attribut barcode *//* |
|
280
|
|
|
$products = new wps_product_ctr(); |
|
281
|
|
|
$products->update_the_attribute_for_product($post_ID, 'varchar', 'barcode', $barcode); |
|
282
|
|
|
}*/ |
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* Save post meta of the post |
|
288
|
|
|
* @param string $post_type Constant identifier of post |
|
289
|
|
|
* @param string $post_date Date of creation post |
|
290
|
|
|
* @param string $post_ID Ident of post |
|
291
|
|
|
* @return string Number code of barcode |
|
292
|
|
|
*/ |
|
293
|
|
|
//private function wps_generate_barcode($post_type, $post_date, $post_ID) { |
|
|
|
|
|
|
294
|
|
|
private function wps_generate_barcode($array) { |
|
295
|
|
|
include_once('wps_barcodegen.ctr.php'); |
|
296
|
|
|
|
|
297
|
|
|
$code = ''; |
|
298
|
|
|
$ref = ''; |
|
|
|
|
|
|
299
|
|
|
$barcode = new wps_barcodegen; |
|
300
|
|
|
|
|
301
|
|
|
$conf = get_option('wps_barcode'); |
|
|
|
|
|
|
302
|
|
|
|
|
303
|
|
|
if ( array_key_exists('normal', $array) ) { |
|
304
|
|
|
$code .= $array['normal']['country']; |
|
305
|
|
|
$code .= $array['normal']['enterprise']; |
|
306
|
|
|
$code .= $array['normal']['ID']; |
|
307
|
|
|
|
|
308
|
|
|
$id = $array['normal']['ID']; |
|
309
|
|
|
|
|
310
|
|
|
} |
|
311
|
|
|
else if ( array_key_exists('internal', $array) ) { |
|
312
|
|
|
$code.= $array['internal']['type']; |
|
313
|
|
|
$code .= $array['internal']['date']; |
|
314
|
|
|
$code .= $array['internal']['ID']; |
|
315
|
|
|
|
|
316
|
|
|
$id = $array['internal']['ID']; |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
$gencode = $barcode->checksum($code); |
|
320
|
|
|
|
|
321
|
|
|
$barcode->writeLog( sprintf( __("Checksum generate: %s from %s <br />", |
|
322
|
|
|
'wps_barcode'), '<b>'.$gencode.'</b>', |
|
323
|
|
|
'<b>'.$code.'</b>') ); |
|
324
|
|
|
|
|
325
|
|
|
$message = sprintf( __("Log generated by wps_barcodegen for add/update product: <br /> %s", |
|
326
|
|
|
'wps_barcode'), $barcode->getLog() ); |
|
327
|
|
|
|
|
328
|
|
|
if ( class_exists('wpeologs_ctr') ) { |
|
329
|
|
|
wpeologs_ctr::log_datas_in_files( 'wps_barcode', array('object_id' => $id, 'message' => $message), 0); |
|
|
|
|
|
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
return $gencode; |
|
333
|
|
|
} |
|
334
|
|
|
} |
|
335
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.