Skip to content

Instantly share code, notes, and snippets.

@code-flow
Created September 1, 2023 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-flow/3395bf1ccb6392895974065a31d3e3a5 to your computer and use it in GitHub Desktop.
Save code-flow/3395bf1ccb6392895974065a31d3e3a5 to your computer and use it in GitHub Desktop.
SNIP Yotpo Bridge Plugin
<?php
/*
Plugin Name: SNIP/Yotpo Bridge
Description: Allows to use Yotpo ratings on Structured Data generated with SNIP.
Author: Florian Simeth
Version: 0.3.0
Author URI: https://rich-snippets.io/yotpo-bridge
Plugin URI: https://rich-snippets.io
*/
define( 'SNIP_YOTPO_CACHE_TIME', 3 * HOUR_IN_SECONDS );
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
/**
*
* PHP Version check.
*
*/
if ( version_compare( PHP_VERSION, '7.0', '<' ) ) {
add_action( 'admin_notices', 'snip_yotpo_old_php_notice' );
function snip_yotpo_old_php_notice() {
printf(
'<div class="notice error"><p>%s</p></div>',
sprintf(
__( 'Hey mate! Sorry for interrupting you. It seem\'s that you\'re using an old PHP version (your current version is %s). You should upgrade to at least 7.0 or higher in order to use the SNIP/Yotpo Bridge plugin. Thank you!', 'snip-yotpo' ),
esc_html( PHP_VERSION )
)
);
}
# sorry. The plugin will not work with an old PHP version.
return;
}
function snip_yotpo_get_reviews( $product_id ) {
if ( ! function_exists( 'wc_yotpo_get_default_settings' ) ) {
return [];
}
$transient_key = "snip/yotpo/prod/{$product_id}/comments";
$cache = get_transient( $transient_key );
if ( is_array( $cache ) ) {
return $cache;
}
if ( ! class_exists( 'Yotpo' ) ) {
$yotpo_dir = realpath( __DIR__ . '/../yotpo-social-reviews-for-woocommerce/lib/yotpo-api/' );
$yotpo_file = trailingslashit( $yotpo_dir ) . 'Yotpo.php';
if ( ! is_file( $yotpo_file ) ) {
return [];
}
include( $yotpo_file );
if ( ! class_exists( 'Yotpo' ) ) {
return [];
}
}
$yotpo_settings = get_option( 'yotpo_settings', wc_yotpo_get_default_settings() );
if ( ! isset( $yotpo_settings['secret'] ) || ! isset( $yotpo_settings['app_key'] ) ) {
return [];
}
$secret = $yotpo_settings['secret'];
$app_key = $yotpo_settings['app_key'];
try {
$yotpo_api = new Yotpo( $app_key, $secret );
$result = $yotpo_api->get_product_reviews( [
'product_id' => $product_id,
'count' => 5,
] );
} catch ( Exception $e ) {
return [];
}
if ( ! isset( $result['response'] ) ) {
set_transient( $transient_key, [], SNIP_YOTPO_CACHE_TIME );
return false;
}
if ( ! isset( $result['response']['reviews'] ) ) {
set_transient( $transient_key, [], SNIP_YOTPO_CACHE_TIME );
return false;
}
if ( ! is_array( $result['response']['reviews'] ) ) {
set_transient( $transient_key, [], SNIP_YOTPO_CACHE_TIME );
return false;
}
set_transient( $transient_key, $result['response']['reviews'], SNIP_YOTPO_CACHE_TIME );
return $result['response']['reviews'];
}
/**
* Get the 'average_score' and the 'total_reviews' of a product rated on Yotpo.
*
* @param int $product_id The WooCommerce product ID.
*
* @since 0.1.0
*
* @return array|bool
*/
function snip_yotpo_get_bottomline( $product_id ) {
$default = [
'average_score' => '',
'total_reviews' => '',
];
if ( ! function_exists( 'wc_yotpo_get_default_settings' ) ) {
return $default;
}
$transient_key = "snip/yotpo/prod/{$product_id}/bottomline";
$cache = get_transient( $transient_key );
if ( is_array( $cache ) ) {
return $cache;
}
if ( ! class_exists( 'Yotpo' ) ) {
$yotpo_dir = realpath( __DIR__ . '/../yotpo-social-reviews-for-woocommerce/lib/yotpo-api/' );
$yotpo_file = trailingslashit( $yotpo_dir ) . 'Yotpo.php';
if ( ! is_file( $yotpo_file ) ) {
return $default;
}
include( $yotpo_file );
if ( ! class_exists( 'Yotpo' ) ) {
return $default;
}
}
$yotpo_settings = get_option( 'yotpo_settings', wc_yotpo_get_default_settings() );
if ( ! isset( $yotpo_settings['secret'] ) || ! isset( $yotpo_settings['app_key'] ) ) {
return $default;
}
$secret = $yotpo_settings['secret'];
$app_key = $yotpo_settings['app_key'];
try {
$yotpo_api = new Yotpo( $app_key, $secret );
$result = $yotpo_api->get_product_bottom_line( [ 'product_id' => $product_id, ] );
} catch ( Exception $e ) {
return $default;
}
if ( ! isset( $result['response'] ) ) {
set_transient( $transient_key, $default, SNIP_YOTPO_CACHE_TIME );
return false;
}
if ( ! isset( $result['response']['bottomline'] ) ) {
set_transient( $transient_key, $default, SNIP_YOTPO_CACHE_TIME );
return false;
}
if ( ! is_array( $result['response']['bottomline'] ) ) {
set_transient( $transient_key, $default, SNIP_YOTPO_CACHE_TIME );
return false;
}
$reviews = wp_parse_args(
$result['response']['bottomline'],
$default
);
set_transient( $transient_key, $reviews, SNIP_YOTPO_CACHE_TIME );
return $reviews;
}
add_filter( 'wpbuddy/rich_snippets/fields/internal_subselect/values', 'snip_yotpo_subselects' );
/**
* Adds new field to use in Global Snippets in the SNIP plugin.
*
* @param array $values
*
* @since 0.1.0
*
* @return array
*/
function snip_yotpo_subselects( $values ) {
if ( ! function_exists( 'wc_yotpo_get_default_settings' ) ) {
return $values;
}
$values['http://schema.org/Text'][] = array(
'id' => 'yotpo_average_score',
'label' => esc_html_x( 'Average Score (Yotpo)', 'subselect field', 'yotpo-snip' ),
'method' => 'snip_yotpo_average_score',
);
$values['http://schema.org/Integer'][] = array(
'id' => 'yotpo_average_score',
'label' => esc_html_x( 'Average Score (Yotpo)', 'subselect field', 'yotpo-snip' ),
'method' => 'snip_yotpo_average_score',
);
$values['http://schema.org/Text'][] = array(
'id' => 'yotpo_total_reviews',
'label' => esc_html_x( 'Total Reviews (Yotpo)', 'subselect field', 'yotpo-snip' ),
'method' => 'snip_yotpo_total_reviews',
);
$values['http://schema.org/Integer'][] = array(
'id' => 'yotpo_total_reviews',
'label' => esc_html_x( 'Total Reviews (Yotpo)', 'subselect field', 'yotpo-snip' ),
'method' => 'snip_yotpo_total_reviews',
);
$values['http://schema.org/Review'][] = array(
'id' => 'yotpo_reviews',
'label' => esc_html_x( 'Yotpo Review Comments', 'subselect field', 'yotpo-snip' ),
'method' => 'snip_yotpo_reviews',
);
return $values;
}
/**
* Returns the average score of a product rated on Yotpo.
*
* @param $val
* @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
* @param array $meta_info
*
* @return string
*/
function snip_yotpo_average_score( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
$ratings = snip_yotpo_get_bottomline( $meta_info['current_post_id'] );
if ( ! is_array( $ratings ) ) {
return '';
}
return $ratings['average_score'];
}
/**
* Returns the total reviews of a product rated on Yotpo.
*
* @param $val
* @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
* @param array $meta_info
*
* @return string
*/
function snip_yotpo_total_reviews( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
$ratings = snip_yotpo_get_bottomline( $meta_info['current_post_id'] );
if ( ! is_array( $ratings ) ) {
return '';
}
return $ratings['total_reviews'];
}
/**
* Returns a review object.
*
* @param $val
* @param \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet
* @param array $meta_info
*
* @since 0.2.0
*
* @return \stdClass[]
*/
function snip_yotpo_reviews( $val, \wpbuddy\rich_snippets\Rich_Snippet $rich_snippet, array $meta_info ) {
if ( ! function_exists( '\wc_get_product' ) ) {
return [];
}
$comments = snip_yotpo_get_reviews( $meta_info['current_post_id'] );
if ( ! is_array( $comments ) || count( $comments ) <= 0 ) {
return [];
}
$reviews = [];
foreach ( $comments as $comment ) {
$review = new \stdClass();
$review->{'@context'} = 'http://schema.org';
$review->{'@type'} = 'Review';
$review->author = new \stdClass();
$review->author->{'@context'} = 'http://schema.org';
$review->author->{'@type'} = 'Person';
$review->author->name = $comment['user']['display_name'];
$review->reviewRating = new \stdClass();
$review->reviewRating->{'@context'} = 'http://schema.org';
$review->reviewRating->{'@type'} = 'Rating';
$review->reviewRating->bestRating = 5;
$review->reviewRating->worstRating = 1;
$review->reviewRating->ratingValue = max( 1, intval( $comment['score'] ) );
$review->reviewBody = strip_tags( $comment['content'] );
$review->datePublished = date_i18n( 'c', strtotime( $comment['created_at'] ) );
$review->headline = strip_tags( $comment['title'] );
$reviews[] = $review;
}
return $reviews;
}
add_action( 'save_post_wpb-rs-global', function ( $post_id ) {
global $wpdb;
$wpdb->query(
"DELETE FROM {$wpdb->options} WHERE option_name LIKE '%snip/yotpo/prod%'"
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment