| Server IP : 202.61.199.114 / Your IP : 216.73.217.139 Web Server : nginx/1.22.1 System : Linux de.arni-solutions.de 6.1.0-49-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.174-1 (2026-05-26) x86_64 User : web20 ( 1018) PHP Version : 8.4.23 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/clients/client2/web20/web/wp-content/plugins/dgt-affiliate/includes/ |
Upload File : |
<?php
/**
* Passende Drogentests automatisch in Blog-Beiträgen einblenden.
*
* - Backend-Meta-Box je Beitrag (aktivieren + optional feste Produkt-IDs)
* - Wenn aktiv: thematisch passende Produkte werden zentral in the_content eingefügt
* - Automatisches Matching anhand von Substanz-Stichwörtern im Beitrag
*
* @package dgt-affiliate
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/* =========================================================================
Matching: passende Produkte zu einem Beitrag finden
========================================================================= */
function dgt_aff_match_products( $post_id, $limit = 3 ) {
// Manuelle Auswahl hat Vorrang.
$manual = trim( (string) get_post_meta( $post_id, '_dgt_product_ids', true ) );
if ( '' !== $manual ) {
$ids = array_filter( array_map( 'absint', preg_split( '/[\s,]+/', $manual ) ) );
if ( $ids ) {
return array_slice( $ids, 0, $limit );
}
}
$text = strtolower( get_the_title( $post_id ) . ' ' . wp_strip_all_tags( strip_shortcodes( get_post_field( 'post_content', $post_id ) ) ) );
// Substanz-Signale im Beitrag erkennen.
$keywords = array(
'thc' => array( 'cannabis', 'marihuana', 'thc', 'gras', 'weed', 'joint', 'kiff', 'hanf', 'haschisch', 'tetrahydro' ),
'coc' => array( 'kokain', 'koks', 'cocaine' ),
'amp' => array( 'amphetamin', 'speed', 'crystal', 'methamphetamin', 'pep' ),
'mdma' => array( 'mdma', 'ecstasy', 'xtc' ),
'opi' => array( 'opiat', 'heroin', 'morphin', 'opioid', 'codein' ),
);
$signals = array();
foreach ( $keywords as $code => $list ) {
foreach ( $list as $k ) {
if ( false !== strpos( $text, $k ) ) {
$signals[ $code ] = true;
break;
}
}
}
$products = get_posts(
array(
'post_type' => DGT_AFF_CPT,
'post_status' => 'publish',
'numberposts' => -1,
)
);
$scored = array();
foreach ( $products as $p ) {
$t = strtolower( $p->post_title );
$is_multi = ( false !== strpos( $t, 'multi' ) || false !== strpos( $t, 'parameter' ) || false !== strpos( $t, 'dip 6' ) );
$score = 0;
if ( ! empty( $signals['thc'] ) && ( strpos( $t, 'thc' ) !== false || strpos( $t, 'cannabis' ) !== false || strpos( $t, 'marihuana' ) !== false || strpos( $t, 'tetrahydro' ) !== false ) ) {
$score += 5;
}
if ( ! empty( $signals['coc'] ) && ( strpos( $t, 'coc' ) !== false || strpos( $t, 'kokain' ) !== false ) ) {
$score += 5;
}
if ( ! empty( $signals['amp'] ) && ( strpos( $t, 'amp' ) !== false || strpos( $t, 'amphetamin' ) !== false ) ) {
$score += 5;
}
if ( ! empty( $signals['mdma'] ) && strpos( $t, 'mdma' ) !== false ) {
$score += 4;
}
if ( ! empty( $signals['opi'] ) && ( strpos( $t, 'mor' ) !== false || strpos( $t, 'opi' ) !== false || $is_multi ) ) {
$score += 3;
}
// Multi-Tests passen breit.
if ( $is_multi ) {
$score += $signals ? 2 : 3;
}
if ( $score > 0 ) {
$scored[ $p->ID ] = $score;
}
}
arsort( $scored );
$ids = array_slice( array_keys( $scored ), 0, $limit );
// Fallback: keine Treffer → zufällige (Multi-)Tests.
if ( empty( $ids ) ) {
$ids = wp_list_pluck(
get_posts(
array(
'post_type' => DGT_AFF_CPT,
'post_status' => 'publish',
'numberposts' => $limit,
'orderby' => 'rand',
)
),
'ID'
);
}
return $ids;
}
/* =========================================================================
Einfügen in the_content (zentral)
========================================================================= */
function dgt_aff_inject_products( $content ) {
if ( is_admin() || ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() || is_feed() ) {
return $content;
}
$post_id = get_the_ID();
if ( ! get_post_meta( $post_id, '_dgt_show_products', true ) ) {
return $content;
}
$ids = dgt_aff_match_products( $post_id, 2 );
if ( empty( $ids ) ) {
return $content;
}
$block = dgt_aff_products(
array(
'ids' => implode( ',', $ids ),
'limit' => count( $ids ),
'columns' => 2,
'layout' => 'list',
'title' => 'Passende Drogentests',
)
);
if ( '' === $block ) {
return $content;
}
$block = '<div class="dgt-aff-incontent">' . $block . '</div>';
// Zentral einfügen: nach dem mittleren Absatz.
$parts = preg_split( '#(</p>)#i', $content, -1, PREG_SPLIT_DELIM_CAPTURE );
$paras = array();
$buf = '';
foreach ( $parts as $seg ) {
$buf .= $seg;
if ( '</p>' === strtolower( $seg ) ) {
$paras[] = $buf;
$buf = '';
}
}
if ( '' !== $buf ) {
$paras[] = $buf;
}
$n = count( $paras );
if ( $n < 3 ) {
return $content . $block;
}
$pos = max( 1, (int) round( $n / 2 ) );
array_splice( $paras, $pos, 0, $block );
return implode( '', $paras );
}
add_filter( 'the_content', 'dgt_aff_inject_products', 20 );
/* =========================================================================
Backend: Meta-Box
========================================================================= */
function dgt_aff_incontent_metabox() {
add_meta_box( 'dgt_aff_incontent', 'Drogentests im Beitrag', 'dgt_aff_incontent_html', 'post', 'side', 'default' );
}
add_action( 'add_meta_boxes', 'dgt_aff_incontent_metabox' );
function dgt_aff_incontent_html( $post ) {
wp_nonce_field( 'dgt_aff_incontent', 'dgt_aff_incontent_nonce' );
$on = get_post_meta( $post->ID, '_dgt_show_products', true );
$ids = get_post_meta( $post->ID, '_dgt_product_ids', true );
?>
<p>
<label>
<input type="checkbox" name="dgt_show_products" value="1" <?php checked( $on, '1' ); ?>>
<strong>Passende Drogentests einblenden</strong>
</label>
</p>
<p class="description" style="margin:0 0 10px;">Wenn aktiv, werden thematisch passende Produkte automatisch zentral im Beitrag angezeigt.</p>
<p>
<label for="dgt_product_ids">Feste Produkt-IDs (optional)</label>
<input type="text" id="dgt_product_ids" name="dgt_product_ids" value="<?php echo esc_attr( $ids ); ?>" class="widefat" placeholder="z. B. 991, 983">
<span class="description">Leer = automatische Auswahl nach Thema.</span>
</p>
<?php
if ( $on ) {
$match = dgt_aff_match_products( $post->ID, 2 );
if ( $match ) {
echo '<p class="description" style="margin-top:8px;"><strong>Aktuelle Auswahl:</strong><br>';
foreach ( $match as $mid ) {
echo esc_html( get_the_title( $mid ) ) . '<br>';
}
echo '</p>';
}
}
}
function dgt_aff_incontent_save( $post_id ) {
if ( ! isset( $_POST['dgt_aff_incontent_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['dgt_aff_incontent_nonce'] ) ), 'dgt_aff_incontent' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
update_post_meta( $post_id, '_dgt_show_products', empty( $_POST['dgt_show_products'] ) ? '' : '1' );
$ids = isset( $_POST['dgt_product_ids'] ) ? sanitize_text_field( wp_unslash( $_POST['dgt_product_ids'] ) ) : '';
if ( '' === $ids ) {
delete_post_meta( $post_id, '_dgt_product_ids' );
} else {
update_post_meta( $post_id, '_dgt_product_ids', $ids );
}
}
add_action( 'save_post_post', 'dgt_aff_incontent_save' );