/** * Tests getting two rating item for a rating form but three rating items exist in db * * @group func */ public function test_get_rating_items2() { global $wpdb; $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing 1', 'max_option_value' => 5)); $rating_item_id1 = $wpdb->insert_id; $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing 2', 'max_option_value' => 3)); $rating_item_id2 = $wpdb->insert_id; $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing 3', 'max_option_value' => 5)); $rating_item_id3 = $wpdb->insert_id; $rating_items = Multi_Rating_API::get_rating_items(); $this->assertEquals(3, count($rating_items)); }
/** * Edits a rating and redirect back to the entries page if successful */ function mr_edit_rating() { // get the entry id $entry_id = null; if (isset($_GET['entry-id'])) { $entry_id = $_GET['entry-id']; } else { if (isset($_POST['entry-id'])) { $entry_id = $_POST['entry-id']; } } $rating_items = Multi_Rating_API::get_rating_items(array('rating_item_entry_id' => $entry_id)); global $wpdb; // get post id $post_id_query = 'SELECT rie.post_id as post_id' . ' FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME . ' AS rie' . ' WHERE rating_item_entry_id = "' . $entry_id . '"'; $post_id = $wpdb->get_var($post_id_query, 0, 0); if ($post_id == null) { echo '<div class="error"><p>' . __('An error occured', 'multi-rating') . '</p></div>'; return; } foreach ($rating_items as $rating_item) { $rating_item_id = $rating_item['rating_item_id']; $rating_item_value = isset($_POST['rating-item-' . $rating_item_id]) ? $_POST['rating-item-' . $rating_item_id] : null; if ($rating_item_value != null) { $query = 'SELECT COUNT(*) FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME . ' WHERE rating_item_entry_id = "' . $entry_id . '" AND rating_item_id = "' . $rating_item_id . '"'; $rows = $wpdb->get_col($query, 0); if ($rows[0] == 0) { $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $entry_id, 'rating_item_id' => $rating_item_id, 'value' => $rating_item_value), array('%d', '%d', '%d')); } else { $wpdb->update($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('value' => $rating_item_value), array('rating_item_entry_id' => $entry_id, 'rating_item_id' => $rating_item_id)); } } } $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS); $rating_results_cache = $general_settings[Multi_Rating::RATING_RESULTS_CACHE_OPTION]; if ($rating_results_cache == true) { // update rating results cache update_post_meta($post_id, Multi_Rating::RATING_RESULTS_POST_META_KEY, null); } // redirect back to entries page $entries_page = 'admin.php?page=mr_rating_results&tab=mr_entries&entry-id=' . $entry_id . '&post-id=' . $post_id; if (isset($_REQUEST['username'])) { $entries_page .= '&username='******'username']; } if (isset($_REQUEST['to-date'])) { $entries_page .= '&to-date=' . $_REQUEST['to-date']; } if (isset($_REQUEST['from-date'])) { $entries_page .= '&from-date=' . $_REQUEST['from-date']; } if (isset($_REQUEST['comments-only'])) { $entries_page .= '&comments-only=' . $_REQUEST['comments-only']; } if (isset($_REQUEST['paged'])) { $entries_page .= '&paged=' . $_REQUEST['paged']; } wp_redirect($entries_page); exit; }
/** * Filters the_title() * * @param $title * @return filtered title */ function mr_filter_the_title($title) { // get the post id global $post; $post_id = null; if (!isset($post_id) && isset($post)) { $post_id = $post->ID; } else { if (!isset($post) && !isset($post_id)) { return $title; // No post id available to display rating result } } $can_apply_filter = !(!in_the_loop() || is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX)); if (!apply_filters('mr_can_apply_filter', $can_apply_filter, 'the_title', $title, $post_id)) { return $title; } $rating_results_position = get_post_meta($post->ID, Multi_Rating::RATING_RESULTS_POSITION_POST_META, true); if ($rating_results_position == Multi_Rating::DO_NOT_SHOW) { return $title; } $position_settings = (array) get_option(Multi_Rating::POSITION_SETTINGS); // use default rating results position if ($rating_results_position == '') { $rating_results_position = $position_settings[Multi_Rating::RATING_RESULTS_POSITION_OPTION]; } $rating_results_html = null; if ($rating_results_position == 'before_title' || $rating_results_position == 'after_title') { $rating_results_html = Multi_Rating_API::display_rating_result(array('post_id' => $post_id, 'echo' => false, 'show_date' => false, 'show_rich_snippets' => true, 'class' => $rating_results_position . ' mr-filter')); } $filtered_title = ''; if ($rating_results_position == 'before_title' && $rating_results_html != null) { $filtered_title .= $rating_results_html; } $filtered_title .= $title; if ($rating_results_position == 'after_title' && $rating_results_html != null) { $filtered_title .= $rating_results_html; } do_action('mr_after_auto_placement', 'the_title', $post_id); return $filtered_title; }
/** * Clears all rating results from the database */ function mr_clear_database() { if (!current_user_can('manage_options')) { return; } $username = isset($_POST['username']) ? $_POST['username'] : null; $from_date = isset($_POST['from-date2']) ? $_POST['from-date2'] : null; $to_date = isset($_POST['to-date2']) ? $_POST['to-date2'] : null; $post_id = isset($_POST['post-id']) ? $_POST['post-id'] : null; $user_id = null; if ($username) { $user = get_user_by('login', $username); if ($user && $user->ID) { $user_id = $user->ID; } } $entries = Multi_Rating_API::get_rating_item_entries(array('user_id' => $user_id, 'from_date' => $from_date, 'to_date' => $to_date, 'post_id' => $post_id)); if (count($entries) > 0) { $entry_id_array = array(); foreach ($entries as $entry) { array_push($entry_id_array, $entry['rating_item_entry_id']); // rating results cache will be refreshed next time it's needed delete_post_meta($entry['post_id'], Multi_Rating::RATING_RESULTS_POST_META_KEY); } global $wpdb; $entry_id_list = implode(',', $entry_id_array); try { $rows = $wpdb->get_results('DELETE FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME . ' WHERE rating_item_entry_id IN ( ' . $entry_id_list . ')'); $rows = $wpdb->get_results('DELETE FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME . ' WHERE rating_item_entry_id IN ( ' . $entry_id_list . ')'); echo '<div class="updated"><p>' . __('Database cleared successfully.', 'multi-rating') . '</p></div>'; } catch (Exception $e) { echo '<div class="error"><p>' . sprintf(__('An error has occured. %s', 'multi-rating'), $e->getMessage()) . '</p></div>'; } } else { echo '<div class="error"><p>' . __('No entries found', 'multi-rating') . '</p></div>'; } }
/** @deprecated */ public static function display_top_rating_results($params = array()) { return Multi_Rating_API::display_rating_results_list($params); }
/** * Column default * * @param $item * @param $column_name * @return */ function column_default($item, $column_name) { switch ($column_name) { case MR_Rating_Entry_Table::SHORTCODE_COLUMN: echo '[mr_rating_result post_id="' . $item[MR_Rating_Entry_Table::POST_ID_COLUMN] . '"]'; break; case MR_Rating_Entry_Table::ENTRY_DATE_COLUMN: echo date('F j, Y, g:i a', strtotime($item[$column_name])); break; case MR_Rating_Entry_Table::CHECKBOX_COLUMN: return $item[$column_name]; break; case MR_Rating_Results_Table::POST_ID_COLUMN: $post_id = $item[MR_Rating_Entry_Table::POST_ID_COLUMN]; $temp_post_id = $post_id; // WPML get adjusted post id for active language, just for the string translation if (function_exists('icl_object_id')) { $temp_post_id = icl_object_id($post_id, get_post_type($post_id), true, ICL_LANGUAGE_CODE); } $post_link = esc_html(get_the_title($temp_post_id)); if (current_user_can('edit_post', $temp_post_id)) { $post_link = "<a href='" . esc_url(get_edit_post_link($temp_post_id)) . "'>"; $post_link .= esc_html(get_the_title($temp_post_id)) . '</a>'; } echo $post_link . ' (Id=' . $post_id . ')'; break; case MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN: case MR_Rating_Entry_Table::IP_ADDRESS_COLUMN: case MR_Rating_Entry_Table::USER_ID_COLUMN: echo $item[$column_name]; break; case MR_Rating_Entry_Table::RATING_RESULT_COLUMN: $rating_result = Multi_Rating_API::calculate_rating_item_entry_result($item[MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN], null); echo __('Star: ', 'multi-rating') . '<span style="color: #0074a2;">' . round($rating_result['adjusted_star_result'], 2) . '/5</span><br />' . __('Score: ', 'multi-rating') . '<span style="color: #0074a2;">' . round($rating_result['adjusted_score_result'], 2) . '/' . $rating_result['total_max_option_value'] . '</span><br />' . __('Percentage: ', 'multi-rating') . '<span style="color: #0074a2;">' . round($rating_result['adjusted_percentage_result'], 2) . '%</span>'; break; case MR_Rating_Entry_Table::ACTION_COLUMN: // do not need to pass post id and rating form id $url = '?page=mr_edit_rating&entry-id=' . $item[MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN]; if (isset($_REQUEST['username'])) { $url .= '&username='******'username']; } if (isset($_REQUEST['to-date'])) { $url .= '&to-date=' . $_REQUEST['to-date']; } if (isset($_REQUEST['from-date'])) { $url .= '&from-date=' . $_REQUEST['from-date']; } if (isset($_REQUEST['paged'])) { $url .= '&paged=' . $_REQUEST['paged']; } ?> <a class="edit-rating-anchor" href="<?php echo $url; ?> "><?php _e('Edit Rating', 'multi-rating'); ?> </a> <?php break; default: return print_r($item, true); } }
/** * Tests rating result list * * @group func2 */ public function test_rating_result_list1() { global $wpdb; $results = $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_TBL_NAME, array('description' => 'Testing', 'max_option_value' => 5)); $rating_item_id = $wpdb->insert_id; $post_ids = $this->factory->post->create_many(5); $user_id1 = $this->factory->user->create(array('role' => 'administrator')); $user_id2 = $this->factory->user->create(array('role' => 'subscribor')); $post_ratings = array(array(array(5, '2015/01/01 00:00:00', $user_id1, null), array(5, '2015/02/01 00:00:00', $user_id2, null), array(5, '2015/03/01 00:00:00', null, null)), array(array(1, '2015/01/01 00:00:00', $user_id1), array(2, '2015/02/01 00:00:00', $user_id2), array(1, '2015/03/01 00:00:00', null, null), array(2, '2015/04/01 00:00:00', null, null)), array(array(3, '2015/01/01 00:00:00', $user_id1), array(1, '2015/02/01 00:00:00', null), array(1, '2015/03/01 00:00:00', null, null)), array(array(5, '2015/01/01 00:00:00', null, null), array(4, '2015/02/01 00:00:00', null, null)), array(array(5, '2015/01/01 00:00:00', $user_id2, null))); $rating_entry_ids = array(); $index = 0; foreach ($post_ids as $post_id) { $post_ratings_data = $post_ratings[$index]; foreach ($post_ratings_data as $post_ratings_data) { $data = array('post_id' => $post_id, 'entry_date' => $post_ratings_data[1]); $data_format = array('%d', '%s'); if (is_numeric($post_ratings_data[2])) { $data['user_id'] = $post_ratings_data[2]; array_push($data_format, '%d'); } $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME, $data, $data_format); $rating_entry_id = $wpdb->insert_id; array_push($rating_entry_ids, $rating_entry_id); $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $rating_entry_id, 'rating_item_id' => $rating_item_id, 'value' => $post_ratings_data[0]), array('%d', '%d', '%d')); } $index++; } // highest rated $rating_result_list = Multi_Rating_API::get_rating_results(array()); $this->assertEquals(5, count($rating_result_list)); }
/** * Saves a rating form entry. */ public static function save_rating() { $ajax_nonce = $_POST['nonce']; if (wp_verify_nonce($ajax_nonce, Multi_Rating::ID . '-nonce')) { global $wpdb; $rating_items = $_POST['ratingItems']; $post_id = $_POST['postId']; $ip_address = MR_Utils::get_ip_address(); $entry_date_mysql = current_time('mysql'); $sequence = isset($_POST['sequence']) ? $_POST['sequence'] : ''; // WPML get original pst id for default language if (function_exists('icl_object_id')) { global $sitepress; $post_id = icl_object_id($post_id, get_post_type($post_id), true, $sitepress->get_default_language()); } $data = array('sequence' => $sequence, 'post_id' => $post_id); $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS); $custom_text_settings = (array) get_option(Multi_Rating::CUSTOM_TEXT_SETTINGS); // get user id global $wp_roles; $user = wp_get_current_user(); $user_id = $user->ID; // stores any validation results, custom validation results can be added through filters $validation_results = array(); $validation_results = MR_Utils::validate_save_rating_restricton($validation_results, $post_id); $validation_results = MR_Utils::validate_rating_item_required($validation_results, $rating_items); $validation_results = apply_filters('mr_after_rating_form_validation_save', $validation_results, $data); if (MR_Utils::has_validation_error($validation_results)) { echo json_encode(array('status' => 'error', 'data' => $data, 'validation_results' => $validation_results)); die; } // everything is OK so now insert the rating form entry and entry values into the database tables $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME, array('post_id' => $post_id, 'entry_date' => $entry_date_mysql, 'ip_address' => $ip_address, 'user_id' => $user_id), array('%d', '%s', '%s', '%d')); $rating_entry_id = $wpdb->insert_id; foreach ($rating_items as $rating_item) { $rating_item_id = $rating_item['id']; $rating_item_value = $rating_item['value']; $wpdb->insert($wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME, array('rating_item_entry_id' => $rating_entry_id, 'rating_item_id' => $rating_item_id, 'value' => $rating_item_value), array('%d', '%d', '%d')); } // Set cookie if restriction type is used foreach ($general_settings[Multi_Rating::SAVE_RATING_RESTRICTION_TYPES_OPTION] as $save_rating_restriction_type) { if ($save_rating_restriction_type == 'cookie') { if (!headers_sent()) { $save_rating_restriction_hours = $general_settings[Multi_Rating::SAVE_RATING_RESTRICTION_HOURS_OPTION]; setcookie(Multi_Rating::POST_SAVE_RATING_COOKIE . '-' . $post_id, true, time() + 60 * 60 * $save_rating_restriction_hours, COOKIEPATH, COOKIE_DOMAIN, false, true); } break; } } $rating_items = Multi_Rating_API::get_rating_items(array('post_id' => $post_id)); $rating_result = Multi_Rating_API::calculate_rating_result(array('post_id' => $post_id, 'rating_items' => $rating_items)); $rating_results_cache = $general_settings[Multi_Rating::RATING_RESULTS_CACHE_OPTION]; if ($rating_results_cache == true) { // update rating results cache update_post_meta($post_id, Multi_Rating::RATING_RESULTS_POST_META_KEY, $rating_result); } $style_settings = (array) get_option(Multi_Rating::STYLE_SETTINGS); $font_awesome_version = $style_settings[Multi_Rating::FONT_AWESOME_VERSION_OPTION]; $icon_classes = MR_Utils::get_icon_classes($font_awesome_version); $use_custom_star_images = $style_settings[Multi_Rating::USE_CUSTOM_STAR_IMAGES]; $image_width = $style_settings[Multi_Rating::CUSTOM_STAR_IMAGE_WIDTH]; $image_height = $style_settings[Multi_Rating::CUSTOM_STAR_IMAGE_HEIGHT]; $rating_results_position = get_post_meta($post_id, Multi_Rating::RATING_RESULTS_POSITION_POST_META, true); $position_settings = (array) get_option(Multi_Rating::POSITION_SETTINGS); // use default rating results position if ($rating_results_position == '') { $rating_results_position = $position_settings[Multi_Rating::RATING_RESULTS_POSITION_OPTION]; } ob_start(); mr_get_template_part('rating-result', null, true, array('no_rating_results_text' => '', 'show_rich_snippets' => false, 'show_title' => false, 'show_date' => false, 'show_count' => true, 'result_type' => Multi_Rating::STAR_RATING_RESULT_TYPE, 'class' => 'rating-result-' . $post_id . ' ' . $rating_results_position . ' mr-filter', 'rating_result' => $rating_result, 'before_count' => '(', 'after_count' => ')', 'post_id' => $post_id, 'ignore_count' => false, 'preserve_max_option' => false, 'before_date' => '', 'after_date' => '', 'icon_classes' => $icon_classes, 'use_custom_star_images' => $use_custom_star_images, 'image_width' => $image_width, 'image_height' => $image_height)); $html = ob_get_contents(); ob_end_clean(); $data['html'] = $html; // if the custom text does not contain %, then there's no need to substitute the message $message = $custom_text_settings[Multi_Rating::RATING_FORM_SUBMIT_SUCCESS_MESSAGE_OPTION]; if (strpos($message, '%') !== false) { $message = MR_Utils::substitute_message($message, $user, Multi_Rating_API::calculate_rating_item_entry_result($rating_entry_id, $rating_items)); } $data['rating_result'] = $rating_result; $data['hide_rating_form'] = $general_settings[Multi_Rating::HIDE_RATING_FORM_AFTER_SUBMIT_OPTION]; echo json_encode(array('status' => 'success', 'data' => $data, 'message' => $message, 'validation_results' => $validation_results)); } die; }
/** * Handles bulk actions */ function process_bulk_action() { if (!current_user_can('manage_options')) { return; // should not get here } if ($this->current_action() === 'delete') { global $wpdb; $checked = is_array($_REQUEST['delete']) ? $_REQUEST['delete'] : array($_REQUEST['delete']); foreach ($checked as $post_id) { /* * delete rating item entry values as well */ $entries = Multi_Rating_API::get_rating_item_entries(array('post_id' => $post_id)); foreach ($entries as $entry) { $rating_item_entry_id = $entry['rating_item_entry_id']; $entry_values_query = 'DELETE FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_VALUE_TBL_NAME . ' WHERE ' . MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN . ' = "' . $rating_item_entry_id . '"'; $results = $wpdb->query($entry_values_query); $entries_query = 'DELETE FROM ' . $wpdb->prefix . Multi_Rating::RATING_ITEM_ENTRY_TBL_NAME . ' WHERE ' . MR_Rating_Entry_Table::RATING_ITEM_ENTRY_ID_COLUMN . ' = "' . $rating_item_entry_id . '"'; $results = $wpdb->query($entries_query); } /* * delete rating results cache in WordPress postmeta table */ delete_post_meta($post_id, Multi_Rating::RATING_RESULTS_POST_META_KEY); } echo '<div class="updated"><p>' . __('Rating results deleted successfully.', 'multi-rating') . '</p></div>'; } }
/** * (non-PHPdoc) * @see WP_Widget::widget() */ function widget($args, $instance) { // https://codex.wordpress.org/Function_Reference/url_to_postid // FIXME may not work with attachments. See here: https://pippinsplugins.com/retrieve-attachment-id-from-image-url/ $post_id = url_to_postid(MR_Utils::get_current_url()); if ($post_id == 0 || $post_id == null) { return; // Nothing to do. } if (!apply_filters('mr_can_apply_widget', true, $post_id, $args, $instance)) { return; // do nothing } extract($args); $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base); $header = empty($instance['header']) ? 'h3' : $instance['header']; $before_title = '<' . $header . ' class="widget-title">'; $after_title = '</' . $header . '>'; $title = apply_filters('widget_title', $title); echo $before_widget; Multi_Rating_API::display_rating_form(array('class' => 'mr-widget', 'before_title' => $before_title, 'after_title' => $after_title, 'title' => $title, 'post_id' => $post_id)); echo $after_widget; }
/** * Shortcode function for displaying rating results list * * @param $atts * @return string */ function mr_rating_results_list($atts = array(), $content = null, $tag) { $can_do_shortcode = !(is_admin() && (!defined('DOING_AJAX') || !DOING_AJAX)); if (!apply_filters('mr_can_do_shortcode', $can_do_shortcode, 'mr_rating_results_list', $atts)) { return; } $general_settings = (array) get_option(Multi_Rating::GENERAL_SETTINGS); $custom_text_settings = (array) get_option(Multi_Rating::CUSTOM_TEXT_SETTINGS); extract(shortcode_atts(array('title' => $custom_text_settings[Multi_Rating::RATING_RESULTS_LIST_TITLE_TEXT_OPTION], 'before_title' => '<h4>', 'after_title' => '</h4>', 'no_rating_results_text' => $custom_text_settings[Multi_Rating::NO_RATING_RESULTS_TEXT_OPTION], 'show_count' => true, 'show_category_filter' => true, 'limit' => 10, 'result_type' => Multi_Rating::STAR_RATING_RESULT_TYPE, 'show_rank' => true, 'show_title' => true, 'class' => '', 'category_id' => 0, 'taxonomy' => null, 'term_id' => 0, 'filter_button_text' => $custom_text_settings[Multi_Rating::FILTER_BUTTON_TEXT_OPTION], 'category_label_text' => $custom_text_settings[Multi_Rating::FILTER_LABEL_TEXT_OPTION], 'show_featured_img' => true, 'image_size' => 'thumbnail', 'show_filter' => false, 'filter_label_text' => $custom_text_settings[Multi_Rating::FILTER_LABEL_TEXT_OPTION], 'sort_by' => 'highest_rated'), $atts)); // temp if (is_string($show_category_filter)) { $show_category_filter = $show_category_filter == 'true' ? true : false; $show_filter = $show_filter; } if (is_string($show_filter)) { $show_filter = $show_filter == 'true' ? true : false; } if (is_string($show_count)) { $show_count = $show_count == 'true' ? true : false; } if (is_string($show_title)) { $show_title = $show_title == 'true' ? true : false; } if (is_string($show_featured_img)) { $show_featured_img = $show_featured_img == 'true' ? true : false; } if ($category_id != 0) { $term_id = $category_id; $taxonomy = 'category'; } return Multi_Rating_API::display_rating_results_list(array('no_rating_results_text' => $no_rating_results_text, 'show_count' => $show_count, 'echo' => false, 'title' => $title, 'show_filter' => $show_filter, 'limit' => $limit, 'result_type' => $result_type, 'show_rank' => $show_rank, 'show_title' => $show_title, 'class' => $class . ' mr-shortcode', 'before_title' => $before_title, 'after_title' => $after_title, 'taxonomy' => $taxonomy, 'term_id' => $term_id, 'filter_button_text' => $filter_button_text, 'filter_label_text' => $filter_label_text, 'show_featured_img' => $show_featured_img, 'image_size' => $image_size, 'sort_by' => $sort_by)); }