'',
'domain' => '', // disambiguates twins: same slug on another domain
'show_availability' => 'no',
'class' => '',
),
$atts,
'affkeeper'
);
$slug = sanitize_text_field( $atts['slug'] );
if ( '' === $slug ) {
return '';
}
$label = '' !== (string) $content ? wp_kses_post( $content ) : esc_html__( 'View product', 'affkeeper' );
// A local link (created in WP, no account needed) always wins — it lives on this site and keeps
// resolving even after an account is connected, so links already placed in posts never break.
$link = null;
$domain = strtolower( sanitize_text_field( $atts['domain'] ) );
$local = AffKeeper_Local::get_by_slug( $slug );
$site = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
if ( $local && ( '' === $domain || $domain === $site ) ) {
$link = AffKeeper_Local::to_row( $local );
} elseif ( AffKeeper_API::is_configured() ) {
$map = AffKeeper_API::link_map();
if ( ! is_wp_error( $map ) ) {
if ( '' !== $domain ) {
// Explicit domain: scan every row (twins are keyed slug~id) for slug + host.
foreach ( $map as $l ) {
$row_slug = isset( $l['slug'] ) ? $l['slug'] : '';
$host = isset( $l['short_display'] ) ? strtolower( strtok( $l['short_display'], '/' ) ) : '';
if ( $row_slug === $slug && $host === $domain ) {
$link = $l;
break;
}
}
} elseif ( isset( $map[ $slug ] ) ) {
$link = $map[ $slug ];
}
}
}
if ( null === $link ) {
// Unknown slug or the panel is unreachable: fail safe — render the label as plain text
// rather than a broken link, and never expose an error to visitors.
return '' . $label . '';
}
wp_enqueue_style( 'affkeeper-front' );
$href = isset( $link['short_url'] ) ? esc_url( $link['short_url'] ) : '';
$status = isset( $link['availability']['status'] ) ? $link['availability']['status'] : '';
$class = 'affkeeper-link';
$extra = sanitize_html_class( $atts['class'] );
$class .= $extra ? ' ' . $extra : '';
$unbuyable = in_array( $status, array( 'gone', 'out_of_stock' ), true );
$low = in_array( $status, array( 'low_stock', 'partial' ), true );
if ( '' === $href ) {
return '' . $label . '';
}
// The affiliate/redirect flag has a real job even with no account: it sets the rel. An affiliate
// link must disclose the paid relationship (rel="sponsored", per Google/FTC); a plain redirect
// gets nofollow but never "sponsored". (Connecting an account adds monitoring on top of this.)
$is_aff = ! empty( $link['is_affiliate'] );
$rel = $is_aff ? 'sponsored nofollow noopener' : 'nofollow noopener';
$html = '' . $label . '';
if ( 'yes' === $atts['show_availability'] ) {
if ( $unbuyable ) {
$html .= ' ' . esc_html__( 'Currently unavailable', 'affkeeper' ) . '';
} elseif ( $low ) {
$html .= ' ' . esc_html__( 'Low stock', 'affkeeper' ) . '';
}
}
return $html;
}
}