如何更正下面的代码片段

时间:2019-02-19 17:01:11

标签: php code-snippets

我想给我的网站会员评分,当他们评价我们的列表/产品/帖子时,取决于1星,2星,依此类推,他们将获得10分,20分...最终5星获得50分。我使用的是myCRED管理系统,但是没有基于评分的评分方法。我为此编写了一个代码段,下面给出了代码。它无法正常工作。请帮我弄清楚。在此先感谢:)

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;
    }
}

0 个答案:

没有答案
相关问题