How to make Popularity Posts Widget compatible with WPML

In table $prefix_PopularityPostsWidget 

The view record store here like this:wpml_get_language_information

There is WPML function wpml_get_language_information can get the post language information by post id,
like this wpml_get_language_information($post->ID)

 

Array
(
    [locale] => fr_FR
    [text_direction] => 
    [display_name] => Français
    [native_name] => Français
    [different_language] => 
)
Array
(
    [locale] => en_US
    [text_direction] => 
    [display_name] => Anglais
    [native_name] => English
    [different_language] => 1
)

If it’s different language, the value will be true (1)

Depand on this, now we can modify public function widget in class_popularity-posts-widget.php

Inside function widget, find loop foreach ($rows as $row)

Replace by the code between:

////////////// Loop ///////////////////
foreach ($rows as $row) {
}
////////////// End of loop //////////////

替换成:

 		////////////// Loop ///////////////////
		if(count($rows) > 0){	//防止$rows 是空的时候报错
			foreach ($rows as $row) {

				if (function_exists('wpml_get_language_information')) {
					$language_information = wpml_get_language_information($row['id']);

					if($language_information[different_language] == ""){

						$title_posts=get_the_title($row['id']);
						$permalink=get_permalink($row['id']);

						if ($instance['range'] === "range_alltime" || $instance['range'] === "range_today" ) {
							$hits=$row['hits'];
						}    else {
							$hits=$row['SUM(hits)'];
						}      

						$hits_to_show = $instance['views_checkbox'] ? 'Views ('.$hits.') ' : "";
						$comments_to_show = $instance['comment_checkbox'] ? 'Comments ('.ppw_get_ComCount($row['id']).')': "";

									//Style file
						require ('style/style-one.php');

					}
				} else {
					$title_posts=get_the_title($row['id']);
					$permalink=get_permalink($row['id']);

					if ($instance['range'] === "range_alltime" || $instance['range'] === "range_today" ) {
						$hits=$row['hits'];
					}    else {
						$hits=$row['SUM(hits)'];
					}      

					$hits_to_show = $instance['views_checkbox'] ? 'Views ('.$hits.') ' : "";
					$comments_to_show = $instance['comment_checkbox'] ? 'Comments ('.ppw_get_ComCount($row['id']).')': "";  

								//Style file
					require ('style/style-one.php');
				}

			}
		}
		////////////// End of loop //////////////

OK now! you can drug the widget it you sidebar.

更新:20130401

在新建widget 的时候,会要求填写显示文章的数量,
问题是,如果你写10,他并不会显示10个,而是被几种语言分掉了,要命的还不是平分,

这儿没有考虑更好的解决方法:

只是在 loop 之前的 get_results 上做改变一下 limit 的数量。

思路是先获取一个有多少种语言被激活了

		if (function_exists('icl_get_languages')) {
			$languages = icl_get_languages('skip_missing=0');
			$times = count($languages);
		}else{
			$times = 1;
		}

然后用 $instance[‘number’]*$times,获取语言数量倍数的结果,这只是个妥协的方法,可以将就着先用着~ 哈哈

		if ($instance['range'] === "all" ) {
			$rows = $wpdb->get_results("SELECT * FROM " . $table_name . " ".$cat_res." ORDER BY hits DESC LIMIT " . $instance['number']*$times, ARRAY_A);
		} elseif ($instance['range'] === "today") {
			$rows = $wpdb->get_results("SELECT * FROM " . $table_name_cache . " WHERE date=CURDATE() ".$cat_res." ORDER BY hits DESC LIMIT " . $instance['number']*$times, ARRAY_A);
		} elseif ($instance['range'] === "weekly" || $instance['range'] === "monthly") {
			if ($instance['range'] === "weekly") $num_days = 7;
			if ($instance['range'] === "monthly") $num_days = 30;
			$rows = $wpdb->get_results("SELECT id, SUM(hits) FROM " . $table_name_cache . " WHERE date > DATE_SUB(CURDATE(), INTERVAL ".$num_days." DAY) ".$cat_res." GROUP BY id ORDER BY SUM(hits) DESC LIMIT " . $instance['number']*$times, ARRAY_A);
		}