* Plugin Name: Custom Query String * Version: 2.7 * Plugin URI: http://mattread.com/projects/wp-plugins/custom-query-string-plugin/ * Description: Change the number of posts displayed when viewing different archive pages. * Author: Matt Read * Author URI: http://www.mattread.com/ * * License: GNU General Public License * * * Copyright (C) 2005 Matt Read * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * You can contact the author by the following: * Matt Read * 102 Delaney Drive * Ottawa, ON K0A 1L0 CA * mattread@mattread.com */ # define the current verion define('k_CQS_VER', '2.7'); class cqs { var $query = false; var $category = false; var $query_vars = array(); var $query_string; var $options = array(); var $option = array(); var $what_to_shows = array('posts', 'days'); var $conditions = array('is_archive', 'is_author', 'is_category', 'is_date', 'is_year', 'is_month', 'is_day', 'is_time', 'is_search', 'is_home', 'is_paged', 'is_feed'); var $orderbys = array('date','category','title','author'); var $orders = array('DESC', 'ASC'); var $request = array(); /** * Register WordPress plugin actions and gets options. * @since version 2.6 */ function cqs() { add_action('activate_'.$this->plugin_basename(__FILE__), array(&$this, 'activate')); add_action('deactivate_'.$this->plugin_basename(__FILE__), array(&$this, 'deactivate')); add_action('admin_menu', array(&$this, 'admin_menu')); add_filter('query_string', array(&$this, 'custom_query_string')); if (!$this->options = get_option('cqs_options')) $this->options = array(); } /** * Initializes the variables etc.. * @param string the query string to be modified. * @since version 2.0.1 */ function init($query_string) { parse_str($query_string, $qv); $this->query_string = $query_string; $this->query_vars = $qv; if ($this->options) { $this->get_query(); $this->get_category(); } } /** * The main function, merges query string with our new queries * @param string the query string to be modified. * @since version 2.0 * @return string the new customized query string. * @access public */ function custom_query_string($query_string) { $this->init($query_string); /* * Check if have a custom query for this category then check * if have a custom query and only add it if it exsists. */ if ( $this->category ) $this->option = $this->options[$this->category]; elseif ( $this->query ) $this->option = $this->options[$this->query]; if ( $this->option ) { $custom_query_string = array( 'posts_per_page' => $this->option['posts_per_page'], 'what_to_show' => $this->option['what_to_show'], 'orderby' => $this->option['orderby'], 'order' => $this->option['order'] ); /* * Make sure we don't override any queries already set. Merge * the original query vars to the new ones. Then build the string * from the new array. */ $this->query_vars = array_merge($custom_query_string,$this->query_vars); $this->query_string = $this->build_query_string($this->query_vars); } return $this->query_string; } /** * Runs after plugin is activated. Adds default settings to the options table. * @since Version 2.4 */ function activate() { if (!get_option('cqs_options')) add_option('cqs_options', array(), 'The Custom Query String plugin settings.', 'no'); return true; } /** * Runs after plugin is deactivated. Removes all settings from the options table. * @since Version 2.4 */ function deactivate() { delete_option('cqs_options'); return true; } /** * Get the plugin basename. * @param str $file The plugin file path. * @since version 2.7 */ function plugin_basename($file) { $file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file); return $file; } /** * Rebuild query string from assosiative array * @since version 2.0 */ function build_query_string ($query_vars) { foreach ($query_vars as $key => $value) { $query[] = $key.'='.$value; } return join('&', $query); } /** * Determines the type of query to use * the order is very important! * archive includes: author, category and date. * date includes: time, day, month, year. * paged overrides all. Use with caution! * @since version 2.0 */ function get_query() { global $wp_query; $wp_query->parse_query($this->query_string); if ($wp_query->is_feed AND $this->options['is_feed']) $this->query = 'is_feed'; elseif ($wp_query->is_paged AND $this->options['is_paged']) $this->query = 'is_paged'; elseif ($wp_query->is_archive) { if ($wp_query->is_author AND $this->options['is_author']) $this->query = 'is_author'; elseif ($wp_query->is_category AND $this->options['is_category']) $this->query = 'is_category'; elseif ($wp_query->is_date) { if ($wp_query->is_time AND $this->options['is_time']) $this->query = 'is_time'; elseif ($wp_query->is_day AND $this->options['is_day']) $this->query = 'is_day'; elseif ($wp_query->is_month AND $this->options['is_month']) $this->query = 'is_month'; elseif ($wp_query->is_year AND $this->options['is_year']) $this->query = 'is_year'; elseif ($this->options['is_date']) $this->query = 'is_date'; } elseif ($this->options['is_archive']) $this->query = 'is_archive'; } elseif ($wp_query->is_search AND $this->options['is_search']) $this->query = 'is_search'; elseif ($wp_query->is_home AND $this->options['is_home']) $this->query = 'is_home'; } /** * Get the category ID * @since version 2.2 */ function get_category() { global $wp_query; if ( $wp_query->is_category ) { if ( !($category = $wp_query->get('cat')) ) { $category = $wp_query->get('category_name'); $category = $this->get_category_id($category); } if ( $this->options['cat_'.$category] ) { $this->category = 'cat_'.$category; } } } /** * Get the category ID from category nice_name. * @since version 2.2 * @param string The category nice_name. * @return int The category id. */ function get_category_id($category_nicename) { global $wpdb; return (int) $wpdb->get_var("SELECT cat_ID FROM $wpdb->categories WHERE category_nicename = '". $wpdb->escape($category_nicename) ."'"); } /** * Get the category nice_name from category ID. * NOT IN USE. Names change and ID's don't. * @since version 2.7 * @param int The category ID. * @return string The category nice_name. */ function get_category_nicename($category_ID) { global $wpdb; return $wpdb->get_var("SELECT category_nicename FROM $wpdb->categories WHERE cat_ID = '". $wpdb->escape((int) $category_ID) ."'"); } /** * Adds CQS to the admin menu * @since version 2.6 */ function admin_menu() { add_options_page('CQS Options', 'CQS', 'manage_options', $this->plugin_basename(__FILE__), array(&$this, 'options_page')); } /** * initializes the options page * @since version 2.0 */ function options_page() { load_plugin_textdomain('custom-query-string'); $this->request = $_REQUEST['cqs']; if ($this->request['add'] || $this->request['addCategory']) { $this->add_options(); $this->display_options_page(true); } elseif ($this->request['deleteChecked']) { $this->delete_options(); $this->display_options_page(true); } else { $this->display_options_page(); } } /** * adds/updates new options * @since version 2.0 */ function add_options() { if ($this->request['addCategory']) { $cqs_new_options = array( 'cat_'. $_REQUEST['cat'] => array( 'posts_per_page' => intval($this->request['category']['posts_per_page']), 'what_to_show' => $this->request['category']['what_to_show'], 'orderby' => $this->request['category']['orderby'], 'order' => $this->request['category']['order'] )); } else { $cqs_new_options = array( $this->request['condition'] => array( 'posts_per_page' => intval($this->request['posts_per_page']), 'what_to_show' => $this->request['what_to_show'], 'orderby' => $this->request['orderby'], 'order' => $this->request['order'] )); } $this->options = array_merge($this->options, $cqs_new_options); update_option('cqs_options', $this->options); } /** * deletes selected options * @since version 2.0 */ function delete_options() { if ($this->request['delete']) { foreach($this->request['delete'] as $delete) unset($this->options[$delete]); } update_option('cqs_options', $this->options); } /** * Display the footer with Credit. * @since version 2.2 */ function footer() { echo '

Custom Query String '. k_CQS_VER .' by Matt Read.

'; } /** * Display "check all" JS and button. * @since version 2.7 */ function check_all_js() { ob_start(); ?> " />

'. __('Options saved.', 'cqs') .'

'; ob_start(); ?>
options) : ?>

Custom Query String

options as $condition => $cqs_option) : # get the name of cat for display. if (strpos($condition, 'cat_') !== false) { $cat_id = str_replace('cat_', '', $condition); $cat_name = $this->get_category_nicename(intval($cat_id)); $display_condition = 'cat_'. $cat_name; } else $display_condition = $condition; ?> >
%posts_per_page% %what_to_show% per page, ordered by %orderby% %order%', 'cqs'); echo str_replace(array('%posts_per_page%', '%what_to_show%', '%orderby%', '%order%'), array($cqs_option['posts_per_page'], $cqs_option['what_to_show'], $cqs_option['orderby'], $cqs_option['order']), $string); ?>

check_all_js(); ?>

   
footer(); ?>