Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add this variable to this code?

In the code below, how do I insert a custom variable I created called $website_url? I want to insert it for the get_highest_score and get_highest_score_range functions. Right now, the format of the output is like this: <li><a href="post-url">Post Name</a></li> I want to change the post-url to the custom website url.

// Display Widget

function widget($args, $instance) {

    extract($args);

    $title = apply_filters('widget_title', esc_attr($instance['title']));

    $type = esc_attr($instance['type']);

    $mode = esc_attr($instance['mode']);

    $limit = intval($instance['limit']);

    $min_votes = intval($instance['min_votes']);

    $chars = intval($instance['chars']);

    $cat_ids = explode(',', esc_attr($instance['cat_ids']));

    $time_range = esc_attr($instance['time_range']);

    echo $before_widget.$before_title.$title.$after_title;

    echo '<ul>'."\n";

    switch($type) {

        case 'most_rated':

            get_most_rated($mode, $min_votes, $limit, $chars);

            break;

        case 'most_rated_category':

            get_most_rated($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'most_rated_range':

            get_most_rated_range($time_range, $mode, $limit, $chars);

            break;

        case 'most_rated_range_category':

            get_most_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);

            break;

        case 'highest_rated':

            get_highest_rated($mode, $min_votes, $limit, $chars);

            break;

        case 'highest_rated_category':

            get_highest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'highest_rated_range':

            get_highest_rated_range($time_range, $mode, $limit, $chars);

            break;

        case 'highest_rated_range_category':

            get_highest_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);

            break;

        case 'lowest_rated':

            get_lowest_rated($mode, $min_votes, $limit, $chars);

            break;

        case 'lowest_rated_category':

            get_lowest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'lowest_rated_range':

            get_lowest_rated_range($time_range, $mode, $limit, $chars);

            break;

        case 'highest_score':

            get_highest_score($mode, $min_votes, $limit, $chars);

            break;

        case 'highest_score_category':

            get_highest_score_category($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'highest_score_range':

            get_highest_score_range($time_range, $mode, $limit, $chars);

            break;

        case 'highest_score_range_category':

            get_highest_score_range_category($time_range, $cat_ids, $mode, $limit, $chars);

            break;

    }

    echo '</ul>'."\n";

    echo $after_widget;

}

Do I have to add this anywhere first?

$custom = get_post_custom($post->ID);
$website_url = $custom["website_url"][0];

-Edit- Here's how I changed it. I did something wrong though because the website url is not outputting.

// Display Widget

function widget($args, $instance) {

    extract($args);

    $title = apply_filters('widget_title', esc_attr($instance['title']));

    $type = esc_attr($instance['type']);

    $mode = esc_attr($instance['mode']);

    $limit = intval($instance['limit']);

    $min_votes = intval($instance['min_votes']);

    $chars = intval($instance['chars']);

    $cat_ids = explode(',', esc_attr($instance['cat_ids']));

    $time_range = esc_attr($instance['time_range']);

    $custom = get_post_custom($post->ID);

    $website_url = $custom["website_url"][0];

    echo $before_widget.$before_title.$title.$after_title;

    echo '<ul>'."\n";

    switch($type) {

        case 'most_rated':

            get_most_rated($mode, $min_votes, $limit, $chars);

            break;

        case 'most_rated_category':

            get_most_rated($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'most_rated_range':

            get_most_rated_range($time_range, $mode, $limit, $chars);

            break;

        case 'most_rated_range_category':

            get_most_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);

            break;

        case 'highest_rated':

            get_highest_rated($mode, $min_votes, $limit, $chars);

            break;

        case 'highest_rated_category':

            get_highest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'highest_rated_range':

            get_highest_rated_range($time_range, $mode, $limit, $chars);

            break;

        case 'highest_rated_range_category':

            get_highest_rated_range_category($time_range, $cat_ids, $mode, $limit, $chars);

            break;

        case 'lowest_rated':

            get_lowest_rated($mode, $min_votes, $limit, $chars);

            break;

        case 'lowest_rated_category':

            get_lowest_rated_category($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'lowest_rated_range':

            get_lowest_rated_range($time_range, $mode, $limit, $chars);

            break;

        case 'highest_score':

            get_highest_score($mode, $min_votes, $limit, $chars, $website_url);

            break;

        case 'highest_score_category':

            get_highest_score_category($cat_ids, $mode, $min_votes, $limit, $chars);

            break;

        case 'highest_score_range':

            get_highest_score_range($time_range, $mode, $limit, $chars, $website_url);

            break;

        case 'highest_score_range_category':

            get_highest_score_range_category($time_range, $cat_ids, $mode, $limit, $chars);

            break;

    }

    echo '</ul>'."\n";

    echo $after_widget;

}

Here are the functions for get_highest_score and get_highest_score_range.

### Function: Display Highest Score Page/Post

if(!function_exists('get_highest_score')) {

    function get_highest_score($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {

        global $wpdb;

        $output = '';

        if(!empty($mode) && $mode != 'both') {

            $where = "$wpdb->posts.post_type = '$mode'";

        } else {

            $where = '1=1';

        }

        $temp = stripslashes(get_option('postratings_template_mostrated'));

        $highest_score = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");

        if($highest_score) {

            foreach ($highest_score as $post) {

                $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";

            }

        } else {

            $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";

        }

        if($display) {

            echo $output;

        } else {

            return $output;

        }

    }

}

...and get_highest_score_range...

### Function: Display Highest Score Page/Post With Time Range

if(!function_exists('get_highest_score_range')) {

    function get_highest_score_range($time = '1 day', $mode = '', $limit = 10, $chars = 0, $display = true) {

        global $wpdb;

        $min_time = strtotime('-'.$time, current_time('timestamp')); 

        $output = '';

        if(!empty($mode) && $mode != 'both') {

            $where = "$wpdb->posts.post_type = '$mode'";

        } else {

            $where = '1=1';

        }

        $temp = stripslashes(get_option('postratings_template_mostrated'));

        $highest_score = $wpdb->get_results("SELECT COUNT($wpdb->ratings.rating_postid) AS ratings_users, SUM($wpdb->ratings.rating_rating) AS ratings_score, ROUND(((SUM($wpdb->ratings.rating_rating)/COUNT($wpdb->ratings.rating_postid))), 2) AS ratings_average, $wpdb->posts.* FROM $wpdb->posts LEFT JOIN $wpdb->ratings ON $wpdb->ratings.rating_postid = $wpdb->posts.ID WHERE rating_timestamp >= $min_time AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."'  AND $wpdb->posts.post_status = 'publish' AND $where GROUP BY $wpdb->ratings.rating_postid ORDER BY ratings_score DESC, ratings_average DESC LIMIT $limit");

        if($highest_score) {

            foreach ($highest_score as $post) {

                $output .= expand_ratings_template($temp, $post->ID, $post, $chars)."\n";

            }

        } else {

            $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";

        }

        if($display) {

            echo $output;

        } else {

            return $output;

        }

    }

}

This is the expand_ratings_template function.

### Function: Replaces the template's variables with appropriate values

function expand_ratings_template($template, $post_id, $post_ratings_data = null, $max_post_title_chars = 0) {

    global $post;

    $temp_post = $post;

    // Get global variables

    $ratings_image = get_option('postratings_image');

    $ratings_max = intval(get_option('postratings_max'));

    $ratings_custom = intval(get_option('postratings_customrating'));

    // Get post related variables

    if(is_null($post_ratings_data)) {

        $post_ratings_data = get_post_custom($post_id);

        $post_ratings_users = intval($post_ratings_data['ratings_users'][0]);

        $post_ratings_score = intval($post_ratings_data['ratings_score'][0]);

        $post_ratings_average = floatval($post_ratings_data['ratings_average'][0]);

    } else {

        $post_ratings_users = intval($post_ratings_data->ratings_users);

        $post_ratings_score = intval($post_ratings_data->ratings_score);

        $post_ratings_average = floatval($post_ratings_data->ratings_average);

    }

    if($post_ratings_score == 0 || $post_ratings_users == 0) {

        $post_ratings = 0;

        $post_ratings_average = 0;

        $post_ratings_percentage = 0;

    } else {

        $post_ratings = round($post_ratings_average, 1);

        $post_ratings_percentage = round((($post_ratings_score/$post_ratings_users)/$ratings_max) * 100, 2);    

    }

    $post_ratings_text = '<span class="post-ratings-text" id="ratings_'.$post_id.'_text"></span>';

    // Get the image's alt text

    if($ratings_custom && $ratings_max == 2) {

        if($post_ratings_score > 0) {

            $post_ratings_score = '+'.$post_ratings_score;

        }

        $post_ratings_alt_text = sprintf(_n('%s rating', '%s rating', $post_ratings_score, 'wp-postratings'), number_format_i18n($post_ratings_score)).__(',', 'wp-postratings').' '.sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users));

    } else {

        $post_ratings_score = number_format_i18n($post_ratings_score);

        $post_ratings_alt_text = sprintf(_n('%s vote', '%s votes', $post_ratings_users, 'wp-postratings'), number_format_i18n($post_ratings_users)).__(',', 'wp-postratings').' '.__('average', 'wp-postratings').': '.number_format_i18n($post_ratings_average, 2).' '.__('out of', 'wp-postratings').' '.number_format_i18n($ratings_max);

    }

    // Check for half star

    $insert_half = 0;

    $average_diff = abs(floor($post_ratings_average)-$post_ratings);

    if($average_diff >= 0.25 && $average_diff <= 0.75) {

        $insert_half = ceil($post_ratings_average);

    } elseif($average_diff > 0.75) {

        $insert_half = ceil($post_ratings);

    }  

    // Replace the variables

    $value = $template;

    if (strpos($template, '%RATINGS_IMAGES%') !== false) {

        $post_ratings_images = get_ratings_images($ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half);

        $value = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $value);

    }

    if (strpos($template, '%RATINGS_IMAGES_VOTE%') !== false) {

        $ratings_texts = get_option('postratings_ratingstext');

        $post_ratings_images = get_ratings_images_vote($post_id, $ratings_custom, $ratings_max, $post_ratings, $ratings_image, $post_ratings_alt_text, $insert_half, $ratings_texts);

        $value = str_replace("%RATINGS_IMAGES_VOTE%", $post_ratings_images, $value);

    }

    $value = str_replace("%RATINGS_ALT_TEXT%", $post_ratings_alt_text, $value);

    $value = str_replace("%RATINGS_TEXT%", $post_ratings_text, $value);

    $value = str_replace("%RATINGS_MAX%", number_format_i18n($ratings_max), $value);

    $value = str_replace("%RATINGS_SCORE%", $post_ratings_score, $value);

    $value = str_replace("%RATINGS_AVERAGE%", number_format_i18n($post_ratings_average, 2), $value);

    $value = str_replace("%RATINGS_PERCENTAGE%", number_format_i18n($post_ratings_percentage, 2), $value);

    $value = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $value);

    if (strpos($template, '%POST_URL%') !== false) {

        $post_link = get_permalink($post_id);

        $value = str_replace("%POST_URL%", $post_link, $value);

    }

    if (strpos($template, '%POST_TITLE%') !== false) {

        $post_title = get_the_title($post_id);

        if ($max_post_title_chars > 0) {

            $post_title = snippet_text($post_title, $max_post_title_chars);

        }

        $value = str_replace("%POST_TITLE%", $post_title, $value);

    }

    if (strpos($template, '%POST_EXCERPT%') !== false) {

        if ($post->ID != $post_id) {

            $post = &get_post($post_id);

        }

        $post_excerpt = ratings_post_excerpt($post_id, $post->post_excerpt, $post->post_content, $post->post_password);

        $value = str_replace("%POST_EXCERPT%", $post_excerpt, $value);

    }

    if (strpos($template, '%POST_CONTENT%') !== false) {

        if ($post->ID != $post_id) {

            $post = &get_post($post_id);

        }

        $value = str_replace("%POST_CONTENT%", get_the_content(), $value);

    }

    // Return value

    $post = $temp_post;

    return apply_filters('expand_ratings_template', htmlspecialchars_decode($value));

}

Here are the two files (unedited) in their entirety:

wp-postratings.php and postratings-stats.php

like image 387
Terry Avatar asked Nov 13 '22 20:11

Terry


1 Answers

You don't have to use $website_url after all. This is the part of the expand_ratings_template function that you want to edit.

 if (strpos($template, '%POST_URL%') !== false) {

        $post_link = get_permalink($post_id);

        $value = str_replace("%POST_URL%", $post_link, $value);

    }

Shall become:

   if (strpos($template, '%POST_URL%') !== false) {
            $custom = get_post_custom($post_id);
            $post_link = $custom["website_url"][0];    
            $value = str_replace("%POST_URL%", $post_link, $value);    
        } 

Hopefully that works. If not, insert a

print_r($custom); 

after the line

$custom = get_post_custom($post_id);

and post the content here!

like image 70
philVigneault Avatar answered Dec 20 '22 04:12

philVigneault