PluginProbe
Welcart e-Commerce / 2.11.35
Welcart e-Commerce v2.11.35
2.12.4 2.12.3 2.11.35 2.12.2 2.12.1 2.11.34 2.11.33 2.11.32 2.11.31 2.11.30 1.3.16 1.3.17 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.8 1.3.9 1.4.0 1.4.1 1.4.10 1.4.11 1.4.12 All 292 releases
usc-e-shop / includes / product / wel-item-functions.php

wel-item-functions.php in Welcart e-Commerce 2.11.35, at includes/product/wel-item-functions.php

744 lines 20.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Welcart Item Functions
4 *
5 * Functions for product related.
6 *
7 * @package Welcart
8 */
9
10 defined( 'ABSPATH' ) || exit;
11
12 /**
13 * Main function for returning products, uses the Welcart_Item class.
14 *
15 * @since 2.2.2
16 *
17 * @param mixed $the_item Post object or post ID of the item.
18 * @param boolean $cache Switch of cache.
19 * @return ProductData|null|false
20 */
21 function wel_get_product( $the_item = false, $cache = true ) {
22 $WelItem = new Welcart\ItemData( $the_item, $cache );
23 $product = $WelItem->get_product( $cache );
24 $post_id = $WelItem->get_id();
25 $product = apply_filters( 'wel_get_product', $product, $post_id );
26 return $product;
27 }
28
29 /**
30 * Function for returning products by item code.
31 *
32 * @since 2.2.2
33 *
34 * @param string $item_code Item code of the item.
35 * @param boolean $cache Switch of cache.
36 * @return ProductData|null|false
37 */
38 function wel_get_product_by_code( $item_code, $cache = true ) {
39 $WelItem = new Welcart\ItemData( null, $cache );
40 $post_id = $WelItem->get_id_by_item_code( $item_code, $cache );
41 $WelItem->set_id( $post_id );
42 $WelItem->set_data( $post_id, $cache );
43 $product = $WelItem->get_product( $cache );
44 $product = apply_filters( 'usces_filter_get_item', $product, $post_id );
45 return $product;
46 }
47
48 /**
49 * Function for returning Post ID by item code.
50 *
51 * @since 2.2.2
52 *
53 * @param string $item_code Item code of the item.
54 * @param boolean $cache Switch of cache.
55 * @return Post ID|null|false
56 */
57 function wel_get_id_by_item_code( $item_code, $cache = true ) {
58 $WelItem = new Welcart\ItemData( null, $cache );
59 $post_id = $WelItem->get_id_by_item_code( $item_code, $cache );
60 return $post_id;
61 }
62
63 /**
64 * The function for returning item data, uses the Welcart_Item class.
65 *
66 * @since 2.2.2
67 *
68 * @param mixed $the_item Post ID of the item.
69 * @param boolean $cache Switch of cache.
70 * @return itemData|null|false
71 */
72 function wel_get_item( $the_item, $cache = true ) {
73 $WelItem = new Welcart\ItemData( $the_item, $cache );
74 return $WelItem->get_item();
75 }
76
77 /**
78 * The function for returning sku data, uses the Welcart_Item class.
79 *
80 * @since 2.2.2
81 *
82 * @param mixed $the_item Post ID of the item.
83 * @param string $sku_code SKU code of the item.
84 * @param boolean $cache Switch of cache.
85 * @return skuData|null|false
86 */
87 function wel_get_sku( $the_item, $sku_code, $cache = true ) {
88 $WelItem = new Welcart\ItemData( $the_item, $cache );
89 return $WelItem->get_sku_by_code( $sku_code );
90 }
91
92 /**
93 * The function for returning sku data by meta_id, uses the Welcart_Item class.
94 *
95 * @since 2.6
96 *
97 * @param int $meta_id meta_id of the item.
98 * @param boolean $cache Switch of cache.
99 * @return skuData|null|false
100 */
101 function wel_get_sku_by_id( $meta_id, $cache = true ) {
102 global $wpdb;
103 $sku_table = $wpdb->prefix . 'usces_skus';
104
105 $res = $wpdb->get_row(
106 $wpdb->prepare(
107 "SELECT * FROM {$sku_table} WHERE meta_id = %d",
108 $meta_id
109 ),
110 ARRAY_A
111 );
112 if ( ! $res ) {
113 return false;
114 }
115
116 $WelItem = new Welcart\ItemData( $res['post_id'], $cache );
117 return $WelItem->get_sku_by_id( $meta_id );
118 }
119
120 /**
121 * The function for returning all sku data in a product, uses the Welcart_Item class.
122 *
123 * @since 2.3.2
124 *
125 * @param mixed $the_item Post object or post ID of the item.
126 * @param string $sortkey Key for sorting.
127 * @param boolean $cache Switch of cache.
128 * @return skuDatas|null|false
129 */
130 function wel_get_skus( $the_item = false, $sortkey = 'sort', $cache = true ) {
131 $WelItem = new Welcart\ItemData( $the_item, $cache );
132 $skus = $WelItem->get_skus( $sortkey );
133 $post_id = $WelItem->get_id();
134 $skus = apply_filters( 'usces_filter_get_skus', $skus, $post_id, $sortkey );
135 return $skus;
136 }
137
138 /**
139 * The function for returning all option data in a product, uses the Welcart_Item class.
140 *
141 * @since 2.3.2
142 *
143 * @param mixed $the_item Post object or post ID of the item.
144 * @param string $sortkey Key for sorting.
145 * @param boolean $cache Switch of cache.
146 * @return OptionDatas|null|false
147 */
148 function wel_get_opts( $the_item = false, $sortkey = 'sort', $cache = true ) {
149 $WelItem = new Welcart\ItemData( $the_item, $cache );
150 $opts = $WelItem->get_opts( $sortkey, $cache );
151 $post_id = $WelItem->get_id();
152 $opts = apply_filters( 'usces_filter_get_opts', $opts, $post_id, $sortkey );
153
154 return $opts;
155 }
156
157 /**
158 * The function for returning extra data in a product, uses the Welcart_Item class.
159 *
160 * @since 2.3.2
161 *
162 * @param mixed $the_item Post object or post ID of the item.
163 * @param boolean $cache Switch of cache.
164 * @return ExtraDatas|null|false
165 */
166 function wel_get_extra_data( $the_item = false, $cache = true ) {
167 $WelItem = new Welcart\ItemData( $the_item, $cache );
168 $extra = $WelItem->get_ext();
169 $post_id = $WelItem->get_id();
170 $extra = apply_filters( 'usces_filter_get_extra_data', $extra, $extra, $post_id );
171 return $extra;
172 }
173
174 /**
175 * Check if the item sku is out of stock.
176 *
177 * @since 2.2.2
178 *
179 * @param mixed $the_item Post object or post ID of the item.
180 * @param string $sku_code SKU code of the item.
181 * @param boolean $cache Switch of cache.
182 * @return boolean Return true if in stock, false otherwise.
183 */
184 function wel_has_stock( $the_item, $sku_code, $cache = true ) {
185 global $usces;
186
187 $WelItem = new Welcart\ItemData( $the_item, $cache );
188 $product = $WelItem->get_product( $cache );
189 $sku = $WelItem->get_sku_by_code( $sku_code );
190 $status = (int) $sku['stock'];
191 $stock = $sku['stocknum'];
192 $iOAp = $product['itemOrderAcceptable'];
193
194 if ( 1 !== $iOAp ) {
195
196 if ( false !== $stock
197 && ( 0 < (int) $stock || WCUtils::is_blank( $stock ) )
198 && false !== $status
199 && 2 > $status
200 ) {
201 $res = true;
202 } else {
203 $res = false;
204 }
205 } else {
206
207 if ( false !== $status && 2 > $status ) {
208 $res = true;
209 } else {
210 $res = false;
211 }
212 }
213
214 return $res;
215 }
216
217 /**
218 * Sanitize free text fields of the item core data before saving.
219 *
220 * These fields accept arbitrary text from the product edit screen and from CSV import,
221 * and they are rendered in many places (storefront, admin, e-mail, payment forms).
222 * sanitize_text_field() strips markup without converting characters to HTML entities,
223 * so values stay safe to store while the existing esc_html() calls on the output side
224 * keep rendering them exactly as before (no double escaping).
225 *
226 * @since 2.12.2
227 *
228 * @param array $data Core data associative array.
229 * @return array Sanitized core data associative array.
230 */
231 function wel_sanitize_item_data( $data ) {
232 if ( ! is_array( $data ) ) {
233 return $data;
234 }
235
236 $text_keys = array( 'itemName' );
237 foreach ( $text_keys as $key ) {
238 if ( isset( $data[ $key ] ) && is_string( $data[ $key ] ) ) {
239 $data[ $key ] = sanitize_text_field( $data[ $key ] );
240 }
241 }
242
243 return $data;
244 }
245
246 /**
247 * Sanitize free text fields of the SKU data before saving.
248 *
249 * @since 2.12.2
250 *
251 * @param array $sku SKU data associative array.
252 * @return array Sanitized SKU data associative array.
253 */
254 function wel_sanitize_sku_data( $sku ) {
255 if ( ! is_array( $sku ) ) {
256 return $sku;
257 }
258
259 $text_keys = array( 'code', 'name', 'unit' );
260 foreach ( $text_keys as $key ) {
261 if ( isset( $sku[ $key ] ) && is_string( $sku[ $key ] ) ) {
262 $sku[ $key ] = sanitize_text_field( $sku[ $key ] );
263 }
264 }
265
266 return $sku;
267 }
268
269 /**
270 * Sanitize free text fields of the option data before saving.
271 *
272 * The 'value' field holds the selectable choices separated by line breaks, so it must be
273 * sanitized with sanitize_textarea_field(). sanitize_text_field() would collapse every
274 * choice onto a single line and destroy the option.
275 *
276 * @since 2.12.2
277 *
278 * @param array $opt Option data associative array.
279 * @return array Sanitized option data associative array.
280 */
281 function wel_sanitize_opt_data( $opt ) {
282 if ( ! is_array( $opt ) ) {
283 return $opt;
284 }
285
286 $text_keys = array( 'code', 'name' );
287 foreach ( $text_keys as $key ) {
288 if ( isset( $opt[ $key ] ) && is_string( $opt[ $key ] ) ) {
289 $opt[ $key ] = sanitize_text_field( $opt[ $key ] );
290 }
291 }
292
293 if ( isset( $opt['value'] ) && is_string( $opt['value'] ) ) {
294 $opt['value'] = sanitize_textarea_field( $opt['value'] );
295 }
296
297 return $opt;
298 }
299
300 /**
301 * The function for updating core data for the Item object.
302 * The data should consist only of the keys you want to update, not the ones you don't want to update.
303 * For details of data, refer to the member variable ($data) of ItemData class.
304 *
305 * @since 3.0.0
306 *
307 * @param array $data Core data associative array.
308 * @param int $post_id Post ID.
309 * @param bool $delete True if you want to delete before updating.
310 * @return true|false Execution result.
311 */
312 function wel_update_item_data( $data, $post_id, $delete = false ) {
313 $data = wel_sanitize_item_data( $data );
314 $WelItem = new Welcart\ItemData( $post_id, false );
315 return $WelItem->update_item_data( $data, $delete );
316 }
317
318 /**
319 * The function for delete item data.
320 *
321 * @since 2.7
322 *
323 * @param int $post_id Post ID.
324 * @return boolean Result.
325 */
326 function wel_delete_item_data( $post_id ) {
327 $WelItem = new Welcart\ItemData( $post_id, false );
328 return $WelItem->delete_item_data();
329 }
330
331 /**
332 * The function for updating sku data by meta_id.
333 *
334 * @since 3.0.0
335 *
336 * @param array $meta_id Meta ID.
337 * @param int $post_id Post ID.
338 * @param int $sku SKU data associative array.
339 * @return true|false Execution result.
340 */
341 function wel_update_sku_data_by_id( $meta_id, $post_id, $sku ) {
342 $sku = wel_sanitize_sku_data( $sku );
343 $WelItem = new Welcart\ItemData( $post_id, false );
344 $sku['meta_id'] = $meta_id;
345 return $WelItem->update_sku_data( $sku );
346 }
347
348 /**
349 * The function for adding sku data.
350 *
351 * @since 3.0.0
352 *
353 * @param int $post_id Post ID.
354 * @param int $sku SKU data associative array.
355 * @return int New meta_id.
356 */
357 function wel_add_sku_data( $post_id, $sku ) {
358 $sku = wel_sanitize_sku_data( $sku );
359 $WelItem = new Welcart\ItemData( $post_id, false );
360 return $WelItem->add_sku_data( $sku );
361 }
362
363 /**
364 * The function for deleting sku data.
365 *
366 * @since 3.0.0
367 *
368 * @param int $meta_id Meta ID.
369 * @param int $post_id Post ID.
370 * @return boolean Result.
371 */
372 function wel_delete_sku_data_by_id( $meta_id, $post_id ) {
373 $WelItem = new Welcart\ItemData( $post_id, false );
374 return $WelItem->delete_sku_data( $meta_id );
375 }
376
377 /**
378 * The function for deleting all sku data in a product.
379 *
380 * @since 3.0.0
381 *
382 * @param int $post_id Post ID.
383 * @return boolean Result.
384 */
385 function wel_delete_all_sku_data( $post_id ) {
386 $WelItem = new Welcart\ItemData( $post_id, false );
387 return $WelItem->delete_all_sku_data();
388 }
389
390 /**
391 * The function for updating option data by meta_id.
392 *
393 * @since 3.0.0
394 *
395 * @param array $meta_id Meta ID.
396 * @param int $post_id Post ID.
397 * @param int $opt Option data associative array.
398 * @return true|false Execution result.
399 */
400 function wel_update_opt_data_by_id( $meta_id, $post_id, $opt ) {
401 $opt = wel_sanitize_opt_data( $opt );
402 $WelItem = new Welcart\ItemData( $post_id, false );
403 $opt['meta_id'] = $meta_id;
404 return $WelItem->update_opt_data( $opt );
405 }
406
407 /**
408 * The function for adding option data.
409 *
410 * @since 3.0.0
411 *
412 * @param int $post_id Post ID.
413 * @param int $opt Option data associative array.
414 * @return int New meta_id.
415 */
416 function wel_add_opt_data( $post_id, $opt ) {
417 $opt = wel_sanitize_opt_data( $opt );
418 $WelItem = new Welcart\ItemData( $post_id, false );
419 return $WelItem->add_opt_data( $opt );
420 }
421
422 /**
423 * The function for deleting option data.
424 *
425 * @since 3.0.0
426 *
427 * @param int $meta_id Meta ID.
428 * @param int $post_id Post ID.
429 * @return boolean Result.
430 */
431 function wel_delete_opt_data_by_id( $meta_id, $post_id ) {
432 $WelItem = new Welcart\ItemData( $post_id, false );
433 return $WelItem->delete_opt_data( $meta_id );
434 }
435
436 /**
437 * The function for deleting all option data in a product.
438 *
439 * @since 3.0.0
440 *
441 * @param int $post_id Post ID.
442 * @return boolean Result.
443 */
444 function wel_delete_all_opt_data( $post_id ) {
445 $WelItem = new Welcart\ItemData( $post_id, false );
446 return $WelItem->delete_all_opt_data();
447 }
448
449 /**
450 * Main product image id.
451 *
452 * @since 3.0.0
453 *
454 * @param int $item_code Item Code.
455 * @param boolean $cache Switch of cache.
456 * @return int Pict ID.
457 */
458 function wel_get_main_pict_id_by_code( $item_code, $cache = true ) {
459
460 $product = wel_get_product_by_code( $item_code, $cache );
461 $pctid = 0;
462
463 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
464
465 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
466
467 } else {
468
469 global $usces, $wpdb;
470
471 $cache_key = 'wel_main_pictid_by_code_' . $item_code;
472
473 $pctid = wp_cache_get( $cache_key );
474 if ( false === $pctid ) {
475 if ( ! empty( $item_code ) ) {
476 $pctid = $wpdb->get_var(
477 $wpdb->prepare(
478 "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' LIMIT 1",
479 $item_code
480 )
481 );
482 if ( null !== $pctid ) {
483 wp_cache_set( $cache_key, $pctid );
484 } else {
485 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
486 }
487 }
488 }
489 if ( null === $pctid ) {
490 $pctid = false;
491 } else {
492 $pctid = (int) apply_filters( 'usces_filter_get_mainpictid', $pctid, $item_code );
493 }
494 }
495
496 return $pctid;
497 }
498
499 /**
500 * Sub product image ids.
501 *
502 * @since 3.0.0
503 *
504 * @param int $item_code Item Code.
505 * @param boolean $cache Switch of cache.
506 * @return array Pict IDs.
507 */
508 function wel_get_sub_pict_ids_by_code( $item_code, $cache = true ) {
509
510 $product = wel_get_product_by_code( $item_code, $cache );
511 $pctids = array();
512
513 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
514
515 if ( isset( $product['itemPicts'] ) ) {
516 if ( is_array( $product['itemPicts'] ) && 0 < count( $product['itemPicts'] ) ) {
517 array_shift( $product['itemPicts'] );
518 }
519 $pctids = $product['itemPicts'];
520 }
521
522 } else {
523
524 global $usces, $wpdb;
525
526 $cache_key = 'wel_sub_pictids_' . $item_code;
527
528 $pctids = wp_cache_get( $cache_key );
529 if ( false === $pctids ) {
530 if ( ! empty( $item_code ) ) {
531 if ( ! $usces->options['system']['subimage_rule'] ) {
532 $codestr = $wpdb->esc_like( $item_code ) . '-%';
533 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s AND post_title <> %s AND post_type = 'attachment' ORDER BY post_title", $codestr, $item_code );
534 } else {
535 $codestr = $wpdb->esc_like( $item_code ) . '--%';
536 $codestr2 = $wpdb->esc_like( $item_code ) . '\_\_%';
537 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE (post_title LIKE %s OR post_title LIKE %s) AND post_type = 'attachment' ORDER BY post_title", $codestr, $codestr2 );
538 }
539 $pctids = $wpdb->get_col( $query );
540 if ( null !== $pctids ) {
541 wp_cache_set( $cache_key, $pctids );
542 } else {
543 if ( is_array( $product['itemPicts'] ) && 0 < count( $product['itemPicts'] ) ) {
544 array_shift( $product['itemPicts'] );
545 }
546 $pctids = $product['itemPicts'];
547 }
548 }
549 }
550 if ( null === $pctids ) {
551 $pctids = false;
552 } else {
553 $pctids = (array) apply_filters( 'usces_filter_get_pictids', $pctids, $item_code );
554 }
555 }
556
557 return $pctids;
558 }
559
560 /**
561 * Main product image id.
562 *
563 * @since 3.0.0
564 *
565 * @param int $post_id item id.
566 * @param boolean $cache Switch of cache.
567 * @return int Pict ID.
568 */
569 function wel_get_main_pict_id( $post_id, $cache = true ) {
570
571 if ( 0 === $post_id ) {
572 return false;
573 }
574
575 $product = wel_get_product( $post_id, $cache );
576 $item_code = isset( $product['itemCode'] ) ? $product['itemCode'] : '';
577 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
578
579 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
580 $pctid = isset( $product['itemPicts'][0] ) ? $product['itemPicts'][0] : 0;
581
582 } else {
583
584 global $wpdb;
585
586 $cache_key = 'wel_main_pictid_by_code_' . $item_code;
587
588 $pctid = wp_cache_get( $cache_key );
589 if ( false === $pctid ) {
590 if ( ! empty( $item_code ) ) {
591 $pctid = $wpdb->get_var(
592 $wpdb->prepare(
593 "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' LIMIT 1",
594 $item_code
595 )
596 );
597 if ( null !== $pctid && $cache ) {
598 wp_cache_set( $cache_key, $pctid );
599 }
600 }
601 }
602 if ( null === $pctid ) {
603 $pctid = false;
604 } else {
605 $pctid = (int) apply_filters( 'usces_filter_get_mainpictid', $pctid, $item_code );
606 }
607 }
608
609 return $pctid;
610 }
611
612 /**
613 * Sub product image ids.
614 *
615 * @since 3.0.0
616 *
617 * @param int $post_id Item id.
618 * @param boolean $cache Switch of cache.
619 * @return array Pict IDs.
620 */
621 function wel_get_sub_pict_ids( $post_id, $cache = true ) {
622
623 if ( 0 === $post_id ) {
624 return false;
625 }
626 $pctids = array();
627 $product = wel_get_product( $post_id, $cache );
628 $item_code = isset( $product['itemCode'] ) ? $product['itemCode'] : '';
629
630 if ( NEW_PRODUCT_IMAGE_REGISTER::$opts['switch_flag'] ) {
631
632 if ( isset( $product['itemPicts'] ) ) {
633 if ( is_array( $product['itemPicts'] ) && 0 < count( $product['itemPicts'] ) ) {
634 array_shift( $product['itemPicts'] );
635 }
636 $pctids = $product['itemPicts'];
637 }
638
639 } else {
640
641 global $usces, $wpdb;
642
643 $cache_key = 'wel_sub_pictids_' . $item_code;
644
645 $pctids = wp_cache_get( $cache_key );
646 if ( false === $pctids ) {
647 if ( ! empty( $item_code ) ) {
648 if ( ! $usces->options['system']['subimage_rule'] ) {
649 $codestr = $wpdb->esc_like( $item_code ) . '-%';
650 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s AND post_title <> %s AND post_type = 'attachment' ORDER BY post_title", $codestr, $item_code );
651 } else {
652 $codestr = $wpdb->esc_like( $item_code ) . '--%';
653 $codestr2 = $wpdb->esc_like( $item_code ) . '\_\_%';
654 $query = $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE (post_title LIKE %s OR post_title LIKE %s) AND post_type = 'attachment' ORDER BY post_title", $codestr, $codestr2 );
655 }
656 $pctids = $wpdb->get_col( $query );
657 if ( null !== $pctids && $cache ) {
658 wp_cache_set( $cache_key, $pctids );
659 }
660 }
661 }
662 if ( null === $pctids ) {
663 $pctids = false;
664 } else {
665 $pctids = (array) apply_filters( 'usces_filter_get_pictids', $pctids, $item_code );
666 }
667 }
668 return $pctids;
669 }
670
671 /**
672 * List all item pict ID by item post id.
673 *
674 * @param integer $post_id item post id.
675 * @param boolean $cache Switch of cache.
676 * @return array
677 */
678 function wel_get_item_pict_ids( $post_id, $cache = false ) {
679 $arr_pict_ids = array();
680 $main_pict_id = (int) wel_get_main_pict_id( $post_id, $cache );
681
682 if ( false !== $main_pict_id && 0 < $main_pict_id ) {
683 $arr_pict_ids[] = $main_pict_id;
684 }
685
686 $sub_item_pict_ids = wel_get_sub_pict_ids( $post_id, $cache );
687
688 if ( is_array( $sub_item_pict_ids ) && ! empty( $sub_item_pict_ids ) ) {
689 $arr_pict_ids = array_merge( $arr_pict_ids, $sub_item_pict_ids );
690 }
691
692 $result = array();
693 foreach ( $arr_pict_ids as $pict_id ) {
694 if ( 0 < $pict_id ) {
695 $result[] = (int) $pict_id;
696 }
697 }
698 return $result;
699 }
700
701 /**
702 * Get image id by title.
703 *
704 * @since 3.0.0
705 *
706 * @param integer $post_id item post id.
707 * @param string $title Image post_title.
708 * @param boolean $cache Switch of cache.
709 * @return array $pctid Pict ID.
710 */
711 function wel_pict_id_by_title( $post_id, $title, $cache = true ) {
712 if ( empty( $title ) ) {
713 return false;
714 }
715
716 global $usces, $wpdb;
717
718 $cache_key = 'wel_pict_id_by_title_' . $post_id . $title;
719
720 $pctid = wp_cache_get( $cache_key );
721 if ( false === $pctid ) {
722
723 $candidate = $wpdb->get_col(
724 $wpdb->prepare(
725 "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'attachment' ORDER BY post_title",
726 $title
727 )
728 );
729 if ( ! empty( $candidate ) ) {
730 $item_ids = wel_get_item_pict_ids( $post_id, $cache );
731 foreach ( $candidate as $can ) {
732 if ( in_array( $can, $item_ids ) ) {
733 $pctid = $can;
734 break;
735 }
736 }
737 }
738 if ( false !== $pctid && $cache ) {
739 wp_cache_set( $cache_key, $pctid );
740 }
741 }
742 return $pctid;
743 }
744