intval(get_option('posts_per_rss')), //post limit in category feed
'comments_per_rss' => intval(get_option('posts_per_rss')), //comment limit in comment feed
'posts_per_rss' => 10, //post limit in main feed
'what_to_show' => 'posts', //posts or days
'postsort' => 0, //post sort, 0=>publish date, 1=>modified date, 2=>post ID
'page' => false, //Include page
'pages_per_rss' => 3, //page limit in main feed
'pagesort' => 1, //page sort, 0=>publish date, 1=>modified date, 2=>post ID
'exclude' => '', //exclude ID
'include' => '' //include ID
);
update_option("feedcontrolplus", $options);
}
}
function fcp_deactivate() {
//delete_option("feedcontrolplus");
}
load_plugin_textdomain('fcp');
add_filter('option_posts_per_rss', 'fcp_post_per_rss' );
function fcp_post_per_rss($value) {
global $withcomments;
if ( !is_feed() ) return $value; //admin page
$fc = get_option('feedcontrolplus');
if ( $withcomments == 1 ) return $fc['comments_per_rss']; //main comment feed and post comment feed
if ( is_archive() ) return $fc['cat_feed_posts']; //categories feed
return 999; //for if ($items_count == get_settings('posts_per_rss')) at the end of wp-rss2.php
}
add_filter('posts_request', 'fcp_posts_request');
function fcp_posts_request($request) {
global $withcomments;
if ( !is_feed() || ($withcomments==1) || is_attachment() || is_archive() || is_single() || is_page() || is_search() || is_home() || is_trackback() || is_404() || is_admin() || is_comments_popup() ) {
return $request;
} else {
$fcp_pr = new Fcp_pr();
return $fcp_pr->get_request($request);
}
}
class Fcp_pr {
var $fc;
var $now;
var $sort = array(
0 => 'post_date_gmt',
1 => 'post_modified_gmt',
2 => 'ID'
);
var $where_exclude;
function Fcp_pr() {
global $wpdb;
$this->fc = get_option('feedcontrolplus');
$this->now = current_time('mysql', 1);
//set where_exclude
$where_exclude = '';
if ( !empty($this->fc['exclude']) ) {
$excludes = explode(',', $this->fc['exclude']);
if ( count($excludes) ) {
foreach ( $excludes as $exclude ) {
if (strpos($exclude, '-')==false) {
$where_exclude .= " AND $wpdb->posts.ID <> " . intval($exclude);
} else {
$pos = strpos($exclude, '-');
$first = intval( substr($exclude, 0, $pos) );
$last = intval( substr($exclude, $pos+1) );
$where_exclude .= " AND ($wpdb->posts.ID < $first OR $wpdb->posts.ID > $last)";
}
}
}
}
$this->where_exclude = $where_exclude;
}
function get_request($request) {
$requests = array();
$requests[] = $this->get_post();
if ( $this->fc['page'] )
$requests[] = $this->get_page();
if ( !empty($this->fc['include']) )
$requests[] = $this->get_include();
$request = '(' . implode(') UNION (', $requests) . ')';
return $request;
}
function get_post() {
global $wpdb;
$where = " AND post_date_gmt <= '{$this->now}'";
$where .= " AND post_status = 'publish'";
$where .= $this->where_exclude;
$orderby = $this->sort[$this->fc['postsort']];
if ( $this->fc['what_to_show'] == 'posts' ) {
$limit = ' LIMIT 0, ' . $this->fc['posts_per_rss'];
} else {
$endrow = $this->fc['posts_per_rss'] - 1;
$post_date = ($this->fc['postsort']==1) ? 'post_modified' : 'post_date';
$end_date = $wpdb->get_var("SELECT min($post_date) FROM $wpdb->posts WHERE post_date_gmt <= '{$this->now}' AND post_status = 'publish'{$this->where_exclude} GROUP BY year($post_date), month($post_date), dayofmonth($post_date) ORDER BY $post_date DESC LIMIT $endrow,1");
$where .= " AND $post_date >= '$end_date'";
$limit = ' LIMIT 999'; //If don't add this, the SQL result order will be error. Fix MySQL UNION order error
}
return "SELECT DISTINCT * FROM $wpdb->posts WHERE 1=1$where GROUP BY ID ORDER BY $orderby DESC$limit";
//SELECT DISTINCT * FROM wp_posts WHERE 1=1 AND post_date_gmt <= '2006-11-18 12:03:59' AND (post_status = "publish" OR post_author = 1 AND post_status != 'draft' AND post_status != 'static') AND post_status != "attachment" GROUP BY wp_posts.ID ORDER BY post_date DESC LIMIT 0, 10
//$request = " SELECT * FROM $wpdb->posts WHERE 1=1" . $where . " ORDER BY " . $orderby . " DESC LIMIT 0, " . $limit;
}
function get_page() {
global $wpdb;
$where = " AND post_date_gmt <= '{$this->now}'";
$where .= " AND post_status = 'static'";
$where .= $this->where_exclude;
$orderby = $this->sort[$this->fc['pagesort']];
return "SELECT DISTINCT * FROM $wpdb->posts WHERE 1=1$where GROUP BY ID ORDER BY $orderby DESC LIMIT 0, " . $this->fc['pages_per_rss'];
}
function get_include() {
global $wpdb;
$where = '';
$includes = explode(',', $this->fc['include']);
if ( count($includes) ) {
foreach ( $includes as $include ) {
if (strpos($include, '-')==false) {
$where .= " OR $wpdb->posts.ID = " . intval($include);
} else {
$pos = strpos($include, '-');
$first = intval( substr($include, 0, $pos) );
$last = intval( substr($include, $pos+1) );
$where .= " OR ($wpdb->posts.ID >= $first AND $wpdb->posts.ID <= $last)";
}
}
}
return "SELECT DISTINCT * FROM $wpdb->posts WHERE 1=0$where GROUP BY ID";
}
} //end of class Fcp_pr
add_action('admin_menu', 'fcp_add_option_page');
function fcp_add_option_page() {
if ( function_exists('add_options_page') ) {
add_options_page('Feed Control Plus Settings', 'Feed Control+', 8, __FILE__, 'fcp_option_page');
}
}
function fcp_option_page() {
global $wpdb;
if ( isset($_POST['Submit']) ) {
$options = array();
$options['cat_feed_posts'] = (int) $_POST['cat_feed_posts'];
$options['comments_per_rss'] = (int) $_POST['comments_per_rss'];
$options['posts_per_rss'] = (int) $_POST['posts_per_rss'];
$options['what_to_show'] = (string) $_POST['what_to_show'];
$options['postsort'] = (int) $_POST['postsort'];
$options['page'] = (bool) $_POST['page'];
$options['pages_per_rss'] = (int) $_POST['pages_per_rss'];
$options['pagesort'] = (int) $_POST['pagesort'];
$options['exclude'] = fcp_option_optimize( (string) $_POST['exclude'] );
$options['include'] = fcp_option_optimize( (string) $_POST['include'] );
update_option('feedcontrolplus', $options);
?>