logged = 0; /* Database modification */ require_once(ABSPATH.'wp-admin/install-helper.php'); maybe_add_column($wpdb->prefix."posts", 'aixorder_score', "ALTER TABLE ".$wpdb->prefix."posts ADD aixorder_score INT(11) NOT NULL DEFAULT 0;"); $result = mysql_list_tables(DB_NAME); $tables = array(); while ($row = mysql_fetch_row($result)) { $tables[] = $row[0]; } if (!in_array($wpdb->prefix.'aixorder', $tables)) { $result = $wpdb->query(" CREATE TABLE ".$wpdb->prefix."aixorder( post_id INT(11) NOT NULL , novelty INT(11) NOT NULL , success INT(11) NOT NULL , sticky INT( 11) NOT NULL , feed_views INT(11) NOT NULL , home_views INT(11) NOT NULL , archive_views INT(11) NOT NULL , category_views INT(11) NOT NULL , single_views INT(11) NOT NULL , comments INT(11) NOT NULL , pingbacks INT(11) NOT NULL , trackbacks INT(11) NOT NULL , KEY post_id (post_id) );"); } /* Reset to default settings */ $this->reset_settings(); /* First calcul of scores */ $result = $this->recalculate_all_scores(); /* Schedule score calcul */ if (!wp_next_scheduled('recalculate_all_scores_hook')) { wp_schedule_event(time(), 'hourly', 'recalculate_all_scores_hook' ); } return $result; } /* Reset to default settings */ function reset_settings(){ update_option('aixorder_popularity_max','100'); update_option('aixorder_trackback_value', '100'); update_option('aixorder_pingback_value', '80'); update_option('aixorder_comment_value','50'); update_option('aixorder_single_value','10'); update_option('aixorder_archive_value','4'); update_option('aixorder_category_value','5'); update_option('aixorder_feed_value','1'); update_option('aixorder_home_value','2'); update_option('aixorder_novelty_period','30'); update_option('aixorder_novelty_coeff','50'); update_option('aixorder_period_sticky','5'); update_option('aixorder_nb_sticky_posts','2'); update_option('aixorder_wp-postratings','0'); update_option('aixorder_order_home','on'); update_option('aixorder_order_category','on'); update_option('aixorder_order_feed','on'); update_option('aixorder_order_search','on'); update_option('aixorder_order_archive','on'); } /****************************************************************************** * Maintenance *******************************************************************************/ /* Plugin uninstallation */ function aixorder_deactivate(){ global $wpdb; /* Drop table aixorder :'( */ $result = $wpdb->query("DROP TABLE ".$wpdb->prefix."aixorder"); $result = $wpdb->query("ALTER TABLE ".$wpdb->prefix."posts DROP COLUMN aixorder_score"); return true; } /* Stats reset */ function reset_stats(){ global $wpdb; /* Drop data */ $result = $wpdb->query("TRUNCATE TABLE ".$wpdb->prefix."aixorder"); $result = $wpdb->query("UPDATE TABLE ".$wpdb->prefix."posts SET aixorder_score = NULL"); return true; } /* Post delete */ function post_delete($post_ID){ global $wpdb; /* Drop data */ $result = $wpdb->query("DELETE FROM ".$wpdb->prefix."posts WHERE post_id = '$post_ID'"); return true; } /****************************************************************************** * Scores count *******************************************************************************/ /* Calculates all scores and writes it in the database. */ function recalculate_all_scores(){ //echo "calculate_all_scores
"; global $wpdb; $success = 0; /* Calculate score parameters for each post*/ $posts = mysql_query("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_status='publish'"); if ($posts && mysql_num_rows($posts) > 0) { while ($post = mysql_fetch_object($posts)) { $this->create_post_record($post->ID); $this->count_post_comments($post->ID); $this->calculate_post_novelty($post->ID); $this->calculate_post_sticky($post->ID); $success_tmp = $this->calculate_post_success($post->ID); /* Adjust maximum succes score if needed */ if($success_tmp > $success){ $success = $success_tmp; } } } /* Adjust maximum succes score if needed */ if($success > get_option('aixorder_popularity_max')){ update_option('aixorder_popularity_max',$success); } /* Calculate score for each post*/ $posts = mysql_query("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_status='publish'"); if ($posts && mysql_num_rows($posts) > 0) { while ($post = mysql_fetch_object($posts)) { $this->calculate_post_success($post->ID); $this->calculate_post_score($post->ID); } } /* Now maximum success score is back to 100. */ update_option('aixorder_popularity_max','100'); return true; } /* Initialisation of a post in the table if needed*/ function create_post_record($post_ID) { //echo "create_post_record
"; global $wpdb; $query = "SELECT post_id FROM ".$wpdb->prefix."aixorder WHERE post_id = '$post_ID'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) { $result = $wpdb->query(" INSERT INTO ".$wpdb->prefix."aixorder VALUES ( '$post_ID' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' , '0' ) "); } } /* Inserts number of comments, pings and trackbacks into table*/ function count_post_comments($post_id) { //echo "populate_post_comments
"; global $wpdb; /* Count existing comments */ $result = mysql_query(" SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_type = '' AND comment_approved = '1' "); $nb_comments = mysql_num_rows($result); /* Count existing trackbacks */ $result = mysql_query(" SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$post_id' AND comment_type = 'trackback' AND comment_approved = '1' "); $nb_trackbacks = mysql_num_rows($result); /* Count existing pingbacks */ $result = mysql_query(" SELECT comment_ID FROM ".$wpdb->prefix."comments WHERE comment_post_ID = '$post_id' AND comment_type = 'pingback' AND comment_approved = '1' "); $nb_pingbacks = mysql_num_rows($result); /* Write values in the table */ if ($nb_comments > 0 || $nb_trackbacks > 0 || $nb_pingbacks > 0 ) { $result = mysql_query(" UPDATE ".$wpdb->prefix."aixorder SET comments = $nb_comments, trackbacks = $nb_trackbacks, pingbacks = $nb_pingbacks WHERE post_id = '$post_id' "); if (!$result) { return false; } } } /* Inserts post novelty value into table*/ function calculate_post_novelty($post_id) { //echo "populate_post_novelty
"; global $wpdb; /* Retrieving days passed since the post was published*/ $result = mysql_query("SELECT DATEDIFF(NOW(),post_date) FROM ".$wpdb->prefix."posts WHERE ID = '$post_id'"); if (!$result) { return false; } $date_diff = mysql_fetch_array($result); /* Calculting novelty points */ $novelty = 0; $novelty_period = get_option('aixorder_novelty_period'); if($date_diff[0] <= $novelty_period){ $novelty = (int)floor((float)((1-($date_diff[0]/$novelty_period))*100)); } /* Write value into table */ $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET novelty = $novelty WHERE post_id = '$post_id'"); if (!$result) { return false; } } /* Inserts post sticky value into table*/ function calculate_post_sticky($post_id) { // echo "calculate_post_sticky
"; global $wpdb; $sticky = 0; /* Retrieving days passed since the post was published*/ $result = mysql_query("SELECT DATEDIFF(NOW(),post_date) FROM ".$wpdb->prefix."posts WHERE ID = '$post_id'"); if (!$result) { return false; } $date_diff = mysql_fetch_array($result); /* Determining if sticky or not */ $sticky_period = get_option('aixorder_period_sticky'); if($date_diff[0] <= $sticky_period){ $sticky =1; } $posts = mysql_query("SELECT ID FROM ".$wpdb->prefix."posts WHERE post_status='publish' AND post_type='post' ORDER BY post_date DESC LIMIT 0,".get_option('aixorder_nb_sticky_posts')); if ($posts && mysql_num_rows($posts) > 0) { while ($post = mysql_fetch_object($posts)) { if($post->ID == $post_id){ $sticky = 1; } } } /* Write value into table */ $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET sticky = $sticky WHERE post_id = '$post_id'"); if (!$result) { return false; } return true; } /* Inserts post success value into table aixorder and posts*/ function calculate_post_success($post_id) { //echo "calculate_post_score
"; global $wpdb; $success = 0; /* Retrieving data of post*/ $result = mysql_query("SELECT feed_views as feed_views, archive_views as archive_views, home_views as home_views, category_views as category_views, single_views as single_views, comments as comments, pingbacks as pingbacks, trackbacks as trackbacks FROM ".$wpdb->prefix."aixorder WHERE post_id = '$post_id'"); $data = mysql_fetch_array($result); /* Retrieving rating of post if WP-PostsRatings installed*/ $rating = 0; if(function_exists('the_ratings')) { $result = mysql_query("SELECT p.meta_value as rating FROM ".$wpdb->prefix."aixorder a, ".$wpdb->prefix."postmeta p WHERE p.post_id = '$post_id' AND p.meta_key = 'ratings_average'"); $average_rating = mysql_fetch_array($result); if($average_rating['rating']){ $rating = $average_rating['rating']; } } /* Popularity calcul */ $success = floor(((get_option('aixorder_single_value') * $data['single'] + get_option('aixorder_feed_value') * $data['feed'] + get_option('aixorder_category_value') * $data['category'] + get_option('aixorder_archive_value') * $data['archive'] + get_option('aixorder_comment_value') * $data['comments'] + get_option('aixorder_pingback_value') * $data['pingbacks'] + get_option('aixorder_trackback_value') * $data['trackbacks'] + get_option('aixorder_wp-postratings') * $rating )/get_option('aixorder_popularity_max'))*100 ); /* Write value into table posts */ $result = $wpdb->query("UPDATE ".$wpdb->prefix."aixorder SET success = $success WHERE post_id = '$post_id'"); return $success; } /* Inserts post score value into table aixorder and posts*/ function calculate_post_score($post_id) { //echo "calculate_post_score
"; global $wpdb; $score = 0; /* Retrieving data of post*/ $result = mysql_query("SELECT novelty as novelty, sticky as sticky, success as success FROM ".$wpdb->prefix."aixorder WHERE post_id = '$post_id'"); //if (!$result) { // return false; //} $data = mysql_fetch_array($result); //echo get_option('aixorder_popularity_max')."
"; if($data['sticky'] == 1){ $score = 1000000000; }else{ $score = floor( (get_option('aixorder_novelty_coeff')/100)* $data['novelty'] + ((1 - (get_option('aixorder_novelty_coeff')/100)) * $data['success']) ); } /* Write value into table posts */ $result = $wpdb->query("UPDATE ".$wpdb->prefix."posts SET aixorder_score = $score WHERE ID = '$post_id'"); //if (!$result) { // return false; //} return true; } /****************************************************************************** * Scores updates *******************************************************************************/ /* Update score on post view */ function record_view($content) { if ($this->logged > 0) { return $content; } global $wpdb, $posts; if (!isset($posts) || !is_array($posts) || count($posts) == 0) /* || is_admin_page())*/ { return; } $ids = array(); $aixorder_posts = $posts; foreach ($aixorder_posts as $post) { $ids[] = $post->ID; } if (is_feed()) { $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET feed_views = feed_views + 1 WHERE post_id IN (".implode(',', $ids).")"); } else if (is_archive() && !is_category()) { $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET archive_views = archive_views + 1 WHERE post_id IN (".implode(',', $ids).")"); } else if (is_category()) { $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET category_views = category_views + 1 WHERE post_id IN (".implode(',', $ids).")"); } else if (is_single()) { $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET single_views = single_views + 1 WHERE post_id = '".$ids[0]."'"); } else { $result = mysql_query("UPDATE ".$wpdb->prefix."aixorder SET home_views = home_views + 1 WHERE post_id IN (".implode(',', $ids).")"); } $this->logged++; return $content; } /* Initialize score on post publication */ function publish_post($post_ID) { $result = $this->recalculate_all_scores(); return $result; } /****************************************************************************** * Posts customized display order *******************************************************************************/ /* Order posts by score */ function reorder_listings($orderby){ global $wpdb; if(is_home() && get_option('aixorder_order_home') == 'on' || is_category() && get_option('aixorder_order_category') == 'on' || is_feed() && get_option('aixorder_order_feed') == 'on' || is_search() && get_option('aixorder_order_search') == 'on' || is_archive() && get_option('aixorder_order_archive') == 'on' ){ return $wpdb->prefix."posts.aixorder_score DESC, ".$wpdb->prefix."posts.post_date DESC"; }else{ return $orderby; } } /****************************************************************************** * Shortcode inclusions *******************************************************************************/ function top_chart($atts = null, $content = null){ return '

'; } /****************************************************************************** * Admin menus *******************************************************************************/ function admin_menu(){ add_options_page('aiXorder', 'aiXorder', 8, __FILE__, array(&$this, 'display_admin_menu')); } function display_admin_menu(){ global $wpdb; ?>

aiXorder

| version version; ?> | | Ek0
aixorder_deactivate(); $plugin = plugin_basename(__FILE__); deactivate_plugins($plugin); ?>



reset_settings(); } if(!empty($_POST['aixorder_reset'])){ $this->reset_stats(); } /* Display page */ ?> view your posts points, update the plugin settings or find help here.','aixorder'); ?>






[?] />
/>
/>
/>
/>

[?]
[?]

[?] />0% />10% />20% />30% />40% />50% />60% />70% />80% />90% />100%

[?]

[?]
[?]
[?]
[?]
[?]
[?]
[?]
[?]

[?]
WP-PostsRatings does not seem to be installed and activated on this blog. WP-PostsRating is a very good Wordpress plugin that allows visitors to rate your posts. aiXorder can integrate this rating in the popularity score calcul ! Find more information about this plugin on the official plugin repository.','aixorder'); ?>











[aixorder-chart]

'; ?>

In the next update, you will be able to customize this chart ;)','aixorder'); ?>



official plugin page.','aixorder'); ?>





recalculate_all_scores(); /* Retrieving data for the graphic */ $query = "SELECT ".$wpdb->prefix."posts.post_title as title, ".$wpdb->prefix."aixorder.success as success, ".$wpdb->prefix."aixorder.sticky as sticky, ".$wpdb->prefix."aixorder.novelty as novelty FROM ".$wpdb->prefix."aixorder INNER JOIN ".$wpdb->prefix."posts ON ".$wpdb->prefix."aixorder.post_id = ".$wpdb->prefix."posts.ID WHERE ".$wpdb->prefix."posts.post_type='post' ORDER BY ".$wpdb->prefix."posts.aixorder_score DESC, ".$wpdb->prefix."posts.post_date DESC LIMIT 0,17"; $result = mysql_query($query); $title=""; $success=""; $novelty=""; $color_success=""; $color_novelty=""; if ($result && mysql_num_rows($result) > 0) { while ($post = mysql_fetch_array($result)) { $new_title = substr($post['title'],0,60); if($new_title!=$post['title']){$new_title = $new_title."...";} $title = "|".ereg_replace("[ |,\"]","+",$new_title)."+".$title; if($success!=""){$success = $success.",";} $success = $success.floor((1 - (get_option('aixorder_novelty_coeff')/100))*(($post['success']/get_option('aixorder_popularity_max'))*100)); if($novelty!=""){$novelty = $novelty.",";} $novelty = $novelty.floor((get_option('aixorder_novelty_coeff')/100)* $post['novelty']); if($color_success!=""){$color_success = $color_success."|";} if($post['sticky']=="1"){ $color_success = $color_success."333333"; }else{ $color_success = $color_success."4d89f9"; } if($color_novelty!=""){$color_novelty = $color_novelty."|";} if($post['sticky']=="1"){ $color_novelty = $color_novelty."8a8a8a"; }else{ $color_novelty = $color_novelty."c6d9fd"; } } } $graph="http://chart.apis.google.com/chart?cht=bhs&chco=".$color_success.",".$color_novelty."&chtt=".__('in+grey,+posts+forced+on+first+positions','aixorder')."&chxt=x,y&chxl=1:".$title."&chds=0,100&chdl=".__('Popularity|Novelty','aixorder')."&chs=600x500&chd=t:".$success."|".$novelty; return $graph; } /****************************************************************************** * Translation *******************************************************************************/ function aixorder_translate(){ load_plugin_textdomain('aixorder','wp-content/plugins/aixorder/locales'); } } /****************************************************************************** * Main *******************************************************************************/ /* User callable functions */ function aixorder_top_chart(){ global $aixorder; echo $aixorder->top_chart(); } /* Plugin construction */ $aixorder = new aixorder; ?>