Skip to content

Instantly share code, notes, and snippets.

@Tyxz
Last active August 18, 2022 17:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Tyxz/09ea3d4b205738be2a8b2b777892b62a to your computer and use it in GitHub Desktop.
Save Tyxz/09ea3d4b205738be2a8b2b777892b62a to your computer and use it in GitHub Desktop.
Sync the ratings of the Wordpress plugin "rate-my-post" with all polylang translated variants of it.
<?php
if ( ! defined( 'ABSPATH' ) ) {
die;
}
function pll_sync_store_rating($post_id, $translation_id, $new_avg_rating, $new_vote_count, $submitted_rating) {
// Update rating sum
$new_ratings_sum = $submitted_rating;
if ( $new_vote_count > 1) {
$new_ratings_sum = get_post_meta($post_id, 'rmp_rating_val_sum', true);
}
// Store new sum in linked page
if ( ! add_post_meta( $translation_id, 'rmp_vote_count', $new_vote_count, true ) ) {
update_post_meta( $translation_id, 'rmp_vote_count', $new_vote_count );
}
if ( ! add_post_meta( $translation_id, 'rmp_rating_val_sum', $new_ratings_sum, true ) ) {
update_post_meta( $translation_id, 'rmp_rating_val_sum', $new_ratings_sum );
}
if ( ! add_post_meta( $translation_id, 'rmp_avg_rating', $new_avg_rating, true ) ) {
update_post_meta( $translation_id, 'rmp_avg_rating', $new_avg_rating );
}
}
function pll_sync_after_vote( $post_id, $new_avg_rating, $new_vote_count, $submitted_rating ) {
// Check if polylang is enabled and working
if (function_exists('pll_languages_list') && function_exists('pll_get_post')) {
// get data from switcher for current page
$raw = pll_the_languages( array('post_id' => $post_id, 'raw' => 1));
// parse trough supported languages
foreach($raw as $key => $value) {
if(!$value['current_lang']) {
$translation_id = pll_get_post( $post_id, $value['slug']);
pll_sync_store_rating($post_id, $translation_id, $new_avg_rating, $new_vote_count, $submitted_rating);
}
}
}
}
add_action('rmp_after_vote', 'pll_sync_after_vote', 10, 4);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment