'',
'star-empty' => '',
'star-half' => '',
'star-full' => '',
'x' => '',
);
return apply_filters( 'affreviews_svg_icons', $icons );
}
}
/**
* Review rating.
*/
if ( ! function_exists( 'affreviews_get_rating' ) ) {
function affreviews_get_rating( $post_id ) {
if ( ! $post_id ) {
return '';
}
$html = '';
$count = 0;
$rating_num = affreviews_get_rating_num( $post_id );
$half_icon = apply_filters( 'affreviews_rating_half_icon', true );
$icons = affreviews_svg_icons();
if ( ! $rating_num ) {
return 'No rating available.';
}
$html .= '
';
$rest = 5 - $rating_num;
while ( $count < floor( $rating_num ) ) {
$html .= $icons['star-full'];
$count++;
}
if ( $rest > 0 ) {
$count = 0;
while ( $count < $rest ) {
$dec = $rating_num - floor( $rating_num );
if ( $half_icon ) {
if ( 0.7 > $dec && 0.3 < $dec && is_float( $rest ) && 0 == $count ) {
$html .= $icons['star-half'];
} elseif ( 0.7 <= $dec && is_float( $rest ) && 0 == $count ) {
$html .= $icons['star-full'];
} else {
$html .= $icons['star-empty'];
}
} else {
if ( $dec >= 0.6 && is_float( $rest ) && 0 === $count ) {
$html .= $icons['star-full'];
} else {
$html .= $icons['star-empty'];
}
}
$count++;
}
}
$html .= '
';
return apply_filters( 'affreviews_rating_html', $html );
}
}
/**
* Return affiliate rating number
*/
if ( ! function_exists( 'affreviews_get_rating_num' ) ) {
function affreviews_get_rating_num( $post_id ) {
if ( ! $post_id ) {
return;
}
$rating_num = (float) get_post_meta( $post_id, 'affreviews_rating', true );
return $rating_num;
}
}
/**
* Extend ruleset of wp_kses for SVG icons
*
* @return void
*/
if ( ! function_exists( 'affreviews_kses_extended_ruleset' ) ) {
function affreviews_kses_extended_ruleset() {
$kses_defaults = wp_kses_allowed_html( 'post' );
$svg_args = array(
'svg' => array(
'class' => true,
'aria-hidden' => true,
'aria-labelledby' => true,
'role' => true,
'xmlns' => true,
'width' => true,
'height' => true,
'viewbox' => true, // <= Must be lower case!
),
'g' => array( 'fill' => true ),
'title' => array( 'title' => true ),
'path' => array(
'd' => true,
'fill' => true,
),
);
return array_merge( $kses_defaults, $svg_args );
}
}
/**
* Review thumbnail element
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_thumb' ) ) {
function affreviews_get_thumb( $post_id, $atts = array() ) {
if ( ! $post_id ) {
return '';
}
if ( ! has_post_thumbnail( $post_id ) ) {
return '';
}
$link = affreviews_get_review_link_url( $post_id );
$classes = '';
$css_vars = '';
$image = '';
$pre_image_html = $link ? '' : '';
$post_image_html = $link ? '' : '';
if ( in_array( 'logo-box', $atts, true ) || in_array( 'logo-circle', $atts, true ) ) {
$background_color = get_post_meta( $post_id, 'affreviews_logo_background_color', true );
if ( in_array( 'logo-circle', $atts, true ) ) {
$classes .= ' affr-thumb--circle';
$pre_image_html .= '';
$post_image_html = '
' . $post_image_html;
} elseif ( in_array( 'logo-box', $atts, true ) ) {
$classes .= ' affr-thumb--box';
}
if ( $background_color && '' !== $background_color ) {
$css_vars = '--affr-logo-background-color: ' . $background_color . ';';
}
$image = get_the_post_thumbnail( $post_id, 'affreviews-logo' );
} else {
$image = get_the_post_thumbnail( $post_id, 'affreviews-product' );
}
$output = '';
$output .= $pre_image_html;
$output .= $image;
$output .= $post_image_html;
$output .= '
';
return $output;
}
}
/**
* Review pros list
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_pros' ) ) {
function affreviews_get_pros( $post_id, $num = 999999 ) {
if ( ! $post_id ) {
return '';
}
$positives = get_post_meta( $post_id, 'affreviews_positives', false );
$output = '';
$icons = affreviews_svg_icons();
if ( ! $positives ) {
return '';
}
$output .= '';
$output .= '
';
$count = 1;
foreach ( $positives[0] as $item ) {
if ( $count <= $num ) {
$output .= "- {$icons['check']}{$item}
";
}
$count++;
}
$output .= '
';
$output .= '
';
return $output;
}
}
/**
* Review cons list
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_cons' ) ) {
function affreviews_get_cons( $post_id, $num = 999999 ) {
if ( ! $post_id ) {
return '';
}
$negatives = get_post_meta( $post_id, 'affreviews_negatives', false );
$output = '';
$icons = affreviews_svg_icons();
if ( ! $negatives ) {
return '';
}
$output .= '';
$output .= '
';
$count = 1;
foreach ( $negatives[0] as $item ) {
if ( $count <= $num ) {
$output .= "- {$icons['x']}{$item}
";
}
$count++;
}
$output .= '
';
$output .= '
';
return $output;
}
}
/**
* Review infolist
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_infolist' ) ) {
function affreviews_get_infolist( $post_id ) {
if ( ! $post_id ) {
return '';
}
$infolist = get_post_meta( $post_id, 'affreviews_group_info_list', false );
$output = '';
$icons = affreviews_svg_icons();
if ( ! isset( $infolist[0] ) ) {
return '';
}
$output .= '';
$output .= '
';
foreach ( $infolist[0] as $item ) {
$output .= "- {$item['title']}:{$item['value']}
";
}
$output .= '
';
$output .= '
';
return $output;
}
}
/**
* Review title
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_title' ) ) {
function affreviews_get_title( $post_id ) {
if ( ! $post_id ) {
return '';
}
$output = '';
$output .= get_the_title( $post_id );
$output .= '
';
return $output;
}
}
/**
* Review bonus
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_bonus' ) ) {
function affreviews_get_bonus( $post_id ) {
if ( ! $post_id ) {
return '';
}
$affbonus = get_post_meta( $post_id, 'affreviews_bonus', true );
if ( empty( $affbonus ) ) {
return '';
}
$output = '';
$output .= '
' . $affbonus . '
';
$output .= '
';
return $output;
}
}
/**
* Review link
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_review_link' ) ) {
function affreviews_get_review_link( $post_id, $review_link_text = null ) {
if ( ! $post_id ) {
return '';
}
$link = affreviews_get_review_link_url( $post_id );
if ( ! $link ) {
return '';
}
$review_link_text = $review_link_text ?? __( 'Read Review', 'affiliate-reviews' );
$output = '';
return $output;
}
}
/**
* Review link URL
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_review_link_url' ) ) {
function affreviews_get_review_link_url( $post_id ) {
if ( ! $post_id ) {
return '';
}
$settings = get_option( 'affreviews_general_settings' );
$affreviews_afflink = get_post_meta( $post_id, 'affreviews_review_link', true );
if ( isset( $settings['reviews_visibility'] ) && 'visible' === $settings['reviews_visibility'] ) {
return get_permalink( $post_id );
} elseif ( $affreviews_afflink ) {
return $affreviews_afflink;
} else {
return '';
}
}
}
/**
* Review counter
*
* @param [int] $count
* @return void
*/
if ( ! function_exists( 'affreviews_get_review_count' ) ) {
function affreviews_get_review_count( $count ) {
if ( ! $count ) {
return '';
}
$output = '';
$output .= $count;
$output .= '
';
return $output;
}
}
/**
* Review tag
*
* @param [int] $count
* @return void
*/
if ( ! function_exists( 'affreviews_get_tag' ) ) {
function affreviews_get_tag( $post_id ) {
if ( ! $post_id ) {
return '';
}
$affreviews_tag_text = get_post_meta( $post_id, 'affreviews_tag_text', true );
$affreviews_tag_background_color = get_post_meta( $post_id, 'affreviews_tag_background_color', true );
$affreviews_text_color = '';
$css = '';
if ( empty( $affreviews_tag_text ) ) {
return '';
}
if ( isset( $affreviews_tag_background_color ) && ! empty( $affreviews_tag_background_color ) ) {
$affreviews_text_color = affreviews_get_contrast_color( $affreviews_tag_background_color );
$css = "--affr-tag-text-color:{$affreviews_text_color};--affr-tag-background-color:{$affreviews_tag_background_color};";
}
$output = '';
$output .= $affreviews_tag_text;
$output .= '
';
return $output;
}
}
/**
* Get contrast color based on background color
*
* @param [int] $hex_color
* @return void
*/
if ( ! function_exists( 'affreviews_get_contrast_color' ) ) {
function affreviews_get_contrast_color( $hex_color ) {
// hex_color RGB
$r1 = hexdec( substr( $hex_color, 1, 2 ) );
$g1 = hexdec( substr( $hex_color, 3, 2 ) );
$b1 = hexdec( substr( $hex_color, 5, 2 ) );
// Black RGB
$blackcolor = '#000000';
$r2blackcolor = hexdec( substr( $blackcolor, 1, 2 ) );
$g2blackcolor = hexdec( substr( $blackcolor, 3, 2 ) );
$b2blackcolor = hexdec( substr( $blackcolor, 5, 2 ) );
// Calc contrast ratio
$l1 = 0.2126 * pow( $r1 / 255, 2.2 ) +
0.7152 * pow( $g1 / 255, 2.2 ) +
0.0722 * pow( $b1 / 255, 2.2 );
$l2 = 0.2126 * pow( $r2blackcolor / 255, 2.2 ) +
0.7152 * pow( $g2blackcolor / 255, 2.2 ) +
0.0722 * pow( $b2blackcolor / 255, 2.2 );
$contrastratio = 0;
if ( $l1 > $l2 ) {
$contrastratio = (int) ( ( $l1 + 0.05 ) / ( $l2 + 0.05 ) );
} else {
$contrastratio = (int) ( ( $l2 + 0.05 ) / ( $l1 + 0.05 ) );
}
// If contrast is more than 5, return black color
if ( $contrastratio > 5 ) {
return '#000000';
} else {
// if not, return white color.
return '#FFFFFF';
}
}
}
/**
* Affiliate link attributes from the General settings.
*
* Used by the legacy blocks and as the default for the Affiliate Button block.
* The fallbacks match the 1.0.x output, for sites that never saved them.
*
* @return array { @type string $rel, @type bool $new_tab }
*/
if ( ! function_exists( 'affreviews_get_aff_link_settings' ) ) {
function affreviews_get_aff_link_settings() {
$settings = get_option( 'affreviews_general_settings' );
$rel = isset( $settings['aff_link_rel'] ) ? (string) $settings['aff_link_rel'] : 'nofollow';
$tokens = preg_split( '/\s+/', strtolower( $rel ), -1, PREG_SPLIT_NO_EMPTY );
return array(
'rel' => implode( ' ', array_intersect( $tokens, array( 'sponsored', 'nofollow', 'noopener' ) ) ),
'new_tab' => isset( $settings['aff_link_new_tab'] ) ? (bool) $settings['aff_link_new_tab'] : true,
);
}
}
/**
* Link cloaking settings from the General settings.
*
* Off for sites that never turned it on.
*
* @return array { @type bool $enabled, @type string $prefix }
*/
if ( ! function_exists( 'affreviews_get_link_cloaking_settings' ) ) {
function affreviews_get_link_cloaking_settings() {
$settings = get_option( 'affreviews_general_settings' );
$prefix = isset( $settings['link_prefix'] ) ? sanitize_title( $settings['link_prefix'] ) : '';
$public = isset( $settings['reviews_visibility'] ) && 'visible' === $settings['reviews_visibility'];
$reviews_slug = $public && isset( $settings['reviews_slug'] ) ? sanitize_title( $settings['reviews_slug'] ) : '';
// Sharing the public reviews slug would send review pages to the redirect.
if ( '' === $prefix || $prefix === $reviews_slug ) {
$prefix = 'go' === $reviews_slug ? 'out' : 'go';
}
return array(
'enabled' => ! empty( $settings['link_cloaking'] ),
'prefix' => $prefix,
);
}
}
/**
* Cloaked URL for a link slug, e.g. https://example.com/go/my-slug/.
*
* @param string $slug Link slug.
* @return string
*/
if ( ! function_exists( 'affreviews_get_cloaked_url' ) ) {
function affreviews_get_cloaked_url( $slug ) {
$cloaking = affreviews_get_link_cloaking_settings();
$slug = sanitize_title( $slug );
if ( ! get_option( 'permalink_structure' ) ) {
return add_query_arg( 'affr_go', $slug, home_url( '/' ) );
}
return home_url( user_trailingslashit( $cloaking['prefix'] . '/' . $slug ) );
}
}
/**
* The URL affiliate buttons link to: the review's cloaked link when cloaking
* is on and the review has a link slug, otherwise its affiliate link as is.
*
* @param int $post_id Review.
* @return string
*/
if ( ! function_exists( 'affreviews_get_aff_link_url' ) ) {
function affreviews_get_aff_link_url( $post_id ) {
$afflink = get_post_meta( $post_id, 'affreviews_afflink', true );
if ( empty( $afflink ) ) {
return $afflink;
}
$cloaking = affreviews_get_link_cloaking_settings();
$slug = $cloaking['enabled'] ? (string) get_post_meta( $post_id, 'affreviews_link_slug', true ) : '';
$url = '' !== $slug ? affreviews_get_cloaked_url( $slug ) : $afflink;
return apply_filters( 'affreviews_aff_link_url', $url, $post_id, $afflink );
}
}
/**
* Review affiliate button
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_aff_button' ) ) {
function affreviews_get_aff_button( $post_id, $button_text = null ) {
if ( ! $post_id ) {
return '';
}
$button_text = $button_text ?? __( 'Check Best Deals', 'affiliate-reviews' );
$afflink = affreviews_get_aff_link_url( $post_id );
if ( empty( $afflink ) ) {
return '';
}
// Defaults give the 1.0.x output: target="_blank" rel="nofollow".
$link = affreviews_get_aff_link_settings();
$button_atts = ( $link['new_tab'] ? 'target="_blank" ' : '' ) . ( $link['rel'] ? 'rel="' . esc_attr( $link['rel'] ) . '"' : '' );
$button_atts = apply_filters( 'affreviews_aff_button_atts', trim( $button_atts ) );
$output = '';
return $output;
}
}
/**
* Review terms
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_terms' ) ) {
function affreviews_get_terms( $post_id ) {
if ( ! $post_id ) {
return '';
}
$terms = get_post_meta( $post_id, 'affreviews_terms', true );
if ( empty( $terms ) ) {
return '';
}
$output = '';
$output .= '
' . nl2br( $terms ) . '
';
$output .= '
';
return $output;
}
}
/**
* Review description
*
* @param [type] $post_id
* @return void
*/
if ( ! function_exists( 'affreviews_get_description' ) ) {
function affreviews_get_description( $post_id ) {
if ( ! $post_id ) {
return '';
}
$descr = get_post_meta( $post_id, 'affreviews_description', true );
if ( empty( $descr ) ) {
return '';
}
$output = '';
$output .= '
' . nl2br( $descr ) . '
';
$output .= '
';
return $output;
}
}
/**
* A CSS value from the visual settings, or '' when it could end the
* declaration or the