Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielpowney/3f914c494b273bf70bd7cc7be199fb6a to your computer and use it in GitHub Desktop.
Save danielpowney/3f914c494b273bf70bd7cc7be199fb6a to your computer and use it in GitHub Desktop.
When a comment status or rating entry status is changed, all "calculated" ratings which share the same rating form id or post id are deleted from the database. The ratings are recalculated when needed if using the shortcodes/auto placment. The WP query does not check if any ratings need to be recalculated. If necessary to recalclate the ratings …
<?php
// When a comment status or rating entry status is changed, all "calculated" ratings which share the same rating form
// id or post id are deleted from the database. The ratings are recalculated when needed if using the
// shortcodes/auto placment.
//
// The WP query does not check if any ratings need to be recalculated. If necessary to recalclate the ratings straight
// away, you can use the mrp_comment_status_changed and mrp_after_save_rating_entry_success hooks as follows.
/**
* If a comment status has changed, recalculate all post ratings associated with the rating form id
*
* @param unknown $params
*/
function my_comment_status_changed( $params ) {
my_recalculate_ratings( $params['rating_form_id'] );
}
add_action( 'mrp_comment_status_changed', 'my_comment_status_changed', 10, 1 );
/**
* If an existing rating rentry has been saved and the entry status changed, recalculate all
* post ratings associated with the rating form id
*
* @param unknown $rating_entry
* @param unknown $is_new
* @param unknown $entry_status_changed
*/
function my_after_save_rating_entry_success( $rating_entry, $is_new, $entry_status_changed ) {
if ( ! $is_new && $entry_status_changed ) {
my_recalculate_ratings( $rating_entry['rating_form_id'] );
}
}
add_action( 'mrp_after_save_rating_entry_success', 'my_after_save_rating_entry_success', 10, 3 );
/**
* Recalculate the rating result list for a rating form
*
* @param unknown $params
*/
function my_recalculate_ratings( $rating_form_id ) {
// get all rating entries for rating form id
$rating_entries = MRP_Multi_Rating_API::get_rating_entries( array( 'rating_form_id' => $rating_form_id ) );
$post_ids = array();
foreach ( $rating_entries as $rating_entry ) {
if ( ! in_array( $rating_entry['post_id'], $post_ids, true ) ) { // do not add same post ids
array_push( $post_ids, $rating_entry['post_id'] );
}
}
$temp = MRP_Multi_Rating_API::get_rating_result_list( array(
'rating_form_id' => $rating_form_id,
'post_ids' => implode( ',', $post_ids ),
'limit' => 1 // for performance reasons we don't need to retrieve the default limit of 10
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment