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)
);");
//if (!$result) {
// return false;
//}
}
/* Default 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');
/* 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;
}
/* Plugin uninstallation */
function aixorder_deactivate(){
global $wpdb;
/* Drop table aixorder :'( */
$result = $wpdb->query("DROP TABLE ".$wpdb->prefix."aixorder");
//if (!$result) {return false;}
$result = $wpdb->query("ALTER TABLE ".$wpdb->prefix."posts DROP COLUMN aixorder_score");
//if (!$result) {return false;}
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'");
//if (!$result) {
// return false;
//}
$data = mysql_fetch_array($result);
$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_popularity_max'))*100
);
/* Write value into table posts */
$result = $wpdb->query("UPDATE ".$wpdb->prefix."aixorder SET success = $success WHERE post_id = '$post_id'");
//if (!$result) {
// return false;
//}
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;
return $wpdb->prefix."posts.aixorder_score DESC, ".$wpdb->prefix."posts.post_date DESC";
}
/******************************************************************************
* Admin menus
*******************************************************************************/
function admin_menu(){
add_options_page('aiXorder', 'aiXorder', 8, __FILE__, array(&$this, 'display_admin_menu'));
}
function display_admin_menu(){
global $wpdb;
/* First recalculate all scores */
$this->recalculate_all_scores();
?>
| version version; ?> | | Ek0
aixorder_deactivate();
$plugin = plugin_basename(__FILE__);
deactivate_plugins($plugin);
?>