/**
* Finnes EPD-lenke et eller annet sted på produktet?
* - Sjekker ALLE postmeta-felt (inkl. ACF / custom felter)
* - Sjekker post_content (beskrivelse)
* - Sjekker dokumentlisten (norlux_documents) hvis dere bruker den
* - Sjekker PDF-vedlegg som matcher EPD_*.pdf
* - Arver fra varianter hvis produktet er variabelt
*/
function nlx_product_has_epd( int $product_id ): bool {
// Treffer "EPD_*.pdf" (case-insensitivt). Slapper også av rundt skilletegn.
$epd_url_regex = '/(?:^|[\/_-])EPD[^"\s<>]*\.pdf/i';
// 0) Hvis variabelt produkt: arve fra varianter
if ( 'product' === get_post_type( $product_id ) ) {
$product = wc_get_product( $product_id );
if ( $product && $product->is_type( 'variable' ) ) {
foreach ( $product->get_children() as $vid ) {
if ( nlx_product_has_epd_single( (int) $vid, $epd_url_regex ) ) {
return true;
}
}
}
}
// 1) Sjekk selve produktet
if ( nlx_product_has_epd_single( $product_id, $epd_url_regex ) ) {
return true;
}
return false;
}
/**
* Sjekker én post-ID (produkt/variant).
*/
function nlx_product_has_epd_single( int $post_id, string $epd_url_regex ): bool {
// A) Sjekk dokument-liste hvis brukt
$docs = get_post_meta( $post_id, 'norlux_documents', true );
if ( ! empty( $docs ) ) {
$blob = is_scalar( $docs ) ? (string) $docs : wp_json_encode( $docs );
if ( stripos( $blob, 'EPD' ) !== false || preg_match( $epd_url_regex, $blob ) ) {
return true;
}
}
// B) Sjekk ALLE postmeta-felter for EPD_*.pdf
$all_meta = get_post_meta( $post_id );
foreach ( $all_meta as $key => $values ) {
foreach ( (array) $values as $v ) {
$blob = is_scalar( $v ) ? (string) $v : wp_json_encode( $v );
if ( preg_match( $epd_url_regex, $blob ) ) {
return true;
}
}
}
// C) Sjekk innholdet (beskrivelse / kort beskrivelse ligger ofte i meta, men tar post_content også)
$post = get_post( $post_id );
if ( $post && ! empty( $post->post_content ) ) {
if ( preg_match( $epd_url_regex, $post->post_content ) ) {
return true;
}
}
// D) Sjekk PDF-vedlegg som er "attached" til posten
$attachments = get_attached_media( 'application/pdf', $post_id );
if ( ! empty( $attachments ) ) {
foreach ( $attachments as $att ) {
$url = wp_get_attachment_url( $att->ID );
$file = get_post_meta( $att->ID, '_wp_attached_file', true );
if ( ($url && preg_match( $epd_url_regex, $url )) ||
($file && preg_match( $epd_url_regex, (string) $file )) ) {
return true;
}
}
}
return false;
}
Base 6000lm 840 Opal 45W IP69K L=1480mm 3m kabel PMMA | Norlux
Gå til indhold