Skip to content

Instantly share code, notes, and snippets.

@kish2011
Created March 23, 2017 12:05
Show Gist options
  • Save kish2011/7d78139db58098a1a48af67741e09bfe to your computer and use it in GitHub Desktop.
Save kish2011/7d78139db58098a1a48af67741e09bfe to your computer and use it in GitHub Desktop.
Give user option to select NA/ Leave the rating for a particular category.
/**
* Allow to remove method for an hook when, it's a class method used and class don't have variable, but you know the class name :)
*/
function remove_filters_for_anonymous_class( $hook_name = '', $class_name ='', $method_name = '', $priority = 0 ) {
global $wp_filter;
// Take only filters on right hook name and priority
if ( !isset($wp_filter[$hook_name][$priority]) || !is_array($wp_filter[$hook_name][$priority]) )
return false;
// Loop on filters registered
foreach( (array) $wp_filter[$hook_name][$priority] as $unique_id => $filter_array ) {
// Test if filter is an array ! (always for class/method)
if ( isset($filter_array['function']) && is_array($filter_array['function']) ) {
// Test if object is a class, class and method is equal to param !
if ( is_object($filter_array['function'][0]) && get_class($filter_array['function'][0]) && get_class($filter_array['function'][0]) == $class_name && $filter_array['function'][1] == $method_name ) {
// Test for WordPress >= 4.7 WP_Hook class (https://make.wordpress.org/core/2016/09/08/wp_hook-next-generation-actions-and-filters/)
if( is_a( $wp_filter[$hook_name], 'WP_Hook' ) ) {
unset( $wp_filter[$hook_name]->callbacks[$priority][$unique_id] );
}
else {
unset($wp_filter[$hook_name][$priority][$unique_id]);
}
}
}
}
return false;
}
function remove_actions_for_anonymous_class( $tag, $class_name = '', $method_name = '', $priority = 10 ) {
remove_filters_for_anonymous_class( $tag, $class_name, $method_name, $priority );
}
if (class_exists('WPJMR_Edit_Review')) {
remove_actions_for_anonymous_class('add_meta_boxes','WPJMR_Edit_Review','add_meta_box', 10);
class WPJMR_Edit_Review_Ext extends WPJMR_Edit_Review {
public function __construct() {
parent::__construct();
}
public function output( $comment ) {
wp_nonce_field( 'wpjmr_save_data', 'wpjmr_meta_nonce' );
$review_categories = get_comment_meta( $comment->comment_ID, 'review_categories', true );
foreach ( $review_categories as $category_slug => $category ) {
$current = get_comment_meta( $comment->comment_ID, 'star-rating-' . $category_slug, true );
?>
<p><label for="star-rating-<?php echo esc_attr( $category_slug ); ?>">
<?php echo esc_attr( $category ); ?>:
<select name="star-rating-<?php echo esc_attr( $category_slug ); ?>">
<?php for ( $i = 1; $i <= WPJMR()->wpjmr_get_count_stars(); $i++ ) : ?>
<option value="<?php echo $i; ?>" <?php selected( $current, $i ); ?>><?php echo $i; ?></option>
<?php endfor; ?>
<option value="NA" <?php selected( $current, 'NA' ); ?>>NA</option>
</select>
</label></p>
<?php
}
}
}
new WPJMR_Edit_Review_Ext();
}
if (class_exists('WPJMR_Form')) {
remove_filters_for_anonymous_class( 'comment_form_top', 'WPJMR_Form', 'can_submit_review', 10 );
class WPJMR_Form_Ext extends WPJMR_Form {
public function __construct() {
parent::__construct();
}
/**
* Comment form stars.
*
* Add stars to the comment form based on review categories. Done via action hook.
*
* @since 1.0.0
*/
public function comment_form_stars() {
?><div id='wpjmr-submit-ratings' class='review-form-stars'>
<div class='star-ratings ratings'>
<?php foreach ( WPJMR()->wpjmr_get_review_categories() as $category_slug => $category ) : ?>
<div class='rating-row'>
<label for='<?php echo $category_slug; ?>'><?php echo function_exists( 'pll__' ) ? pll__( $category ) : $category; ?></label>
<div class='stars choose-rating' data-rating-category='<?php echo $category_slug; ?>'>
<?php for ( $i = WPJMR()->wpjmr_get_count_stars(); $i > 0 ; $i-- ) : ?>
<span data-star-rating='<?php echo $i; ?>' class="star dashicons dashicons-star-empty"></span>
<?php endfor; ?>
<input type='hidden' class='required' name='star-rating-<?php echo $category_slug; ?>' value='NA'>
</div>
<?php if ('food' == strtolower($category) ){
?>
<span>&nbsp;&nbsp;<?php _e( 'Leave the ratings no food is there.', 'wp-job-manager-reviews' ); ?></span>
<?php
}
?>
</div>
<?php endforeach; ?>
</div>
</div><?php
}
}
new WPJMR_Form_Ext();
}
add_action( 'wpjmr_after_save_comment_review', 'wpjmr_after_save_comment_review', 10, 2 );
function wpjmr_after_save_comment_review($comment_id, $comment) {
if ( ! $review_categories ) {
$review_categories = WPJMR()->wpjmr_get_review_categories();
}
$review_average = 0;
$$counter = 0;
foreach ( $review_categories as $category_slug => $review_category ) {
if ( isset ( $_POST['star-rating-' . $category_slug ] ) ) {
$value = $_POST['star-rating-' . $category_slug ];
$review_average += $value;
update_comment_meta( $comment_id, 'star-rating-' . $category_slug, $value );
$review_categories_value = get_comment_meta( $comment_id, 'star-rating-' . $category_slug, true );
if ($review_categories_value == 'NA')
$counter++;
}
}
if ( $review_average > 0 ) {
$review_average = $review_average / (count( $review_categories ) - $counter);
$review_average = round( $review_average * 2 ) / 2;
// Save the average
update_comment_meta( $comment_id, 'review_average', $review_average );
// Save review categories in database for this review.
update_comment_meta( $comment_id, 'review_categories', $review_categories );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment