I wanna give points to members of my website when they rate our listings/products/posts, depending on 1star, 2star and so on they will get 10points, 20points...ultimately 5star get 50points. I'm using myCRED management system, but there are no such ways to give points on the basis of ratings. I write a snippet for doing this, the code is given below. It does not working properly. Please help me with figuring out. Thanks in advance :)
add_filter( 'mycred_setup_hooks', 'register_my_custom_hook' );
function register_my_custom_hook( $installed )
{
$installed['review'] = array(
'title' => __( '%plural% for Star Review', 'textdomain' ),
'description' => __( 'This hook awards points to users who receive star reviews for service.', 'textdomain' ),
'callback' => array( 'my_custom_hook_class' )
);
return $installed;
}
//myCRED Custom Hook Class
class my_custom_hook_class extends myCRED_Hook {
/**
* Construct
*/
function __construct( $hook_prefs, $type = 'mycred_default' ) {
parent::__construct( array(
'id' => 'star_review',
'defaults' => array(
'creds' => 50,
'log' => '%plural% for receiving a star review'
)
), $hook_prefs, $type );
}
/**
* Hook into WordPress
*/
// Get rating stars value on saved review
function gd_snippet_190212_after_save_review( $request, $text ) {
$rating = (float) $request['geodir_overallrating']; // rating
$post_id = (int) $request['comment_post_ID']; // post id
}
// Do stuff here
public function run() {
// after rating is left on geodirectory listing
add_action( 'geodir_after_save_comment', array( $this, 'star_review' ) );
}
/**
* Assign and check if the user qualifies for points
*/
public function star_review( $user_id ) {
switch ($rating) {
case 5: //5 star rating
$this->prefs['creds'] = 50;
break;
case 4: //4 star rating
$this->prefs['creds'] = 40;
break;
case 3: //3 star rating
$this->prefs['creds'] = 30;
break;
case 2: //2 star rating
$this->prefs['creds'] = 20;
break;
case 1: //1 star rating
$this->prefs['creds'] = 10;
break;
default: //0 star rating
$this->prefs['creds'] = 0;
}
// Check if user is excluded (required)
if ( $this->core->exclude_user( $user_id ) ) return;
// Make sure this is a unique event
if ( $this->has_entry( 'star_review', '', $post_id ) ) return;
// Execute
$this->core->add_creds(
'star_review',
$user_id,
$this->prefs['creds'],
$this->prefs['log'],
'',
'',
$this->mycred_type
);
}
/**
* Add Settings
*/
public function preferences() {
// Our settings are available under $this->prefs
$prefs = $this->prefs; ?>
<!-- First we set the amount -->
<label class="subheader"><?php echo $this->core->plural(); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'creds' ); ?>" id="<?php echo $this->field_id( 'creds' ); ?>" value="<?php echo esc_attr( $prefs['creds'] ); ?>" size="8" /></div>
</li>
</ol>
<!-- Then the log template -->
<label class="subheader"><?php _e( 'Log template', 'mycred' ); ?></label>
<ol>
<li>
<div class="h2"><input type="text" name="<?php echo $this->field_name( 'log' ); ?>" id="<?php echo $this->field_id( 'log' ); ?>" value="<?php echo esc_attr( $prefs['log'] ); ?>" class="long" /></div>
</li>
</ol>
<?php
}
/**
* Sanitize Preferences
*/
public function sanitise_preferences( $data ) {
$new_data = $data;
// Apply defaults if any field is left empty
$new_data['creds'] = ( !empty( $data['creds'] ) ) ? $data['creds'] : $this->defaults['creds'];
$new_data['log'] = ( !empty( $data['log'] ) ) ? sanitize_text_field( $data['log'] ) : $this->defaults['log'];
return $new_data;
}
}