WordPress.org

Plugin Directory

Changeset 614406


Ignore:
Timestamp:
10/19/12 07:15:07 (18 months ago)
Author:
jeremyfelt
Message:

Sync trunk with changes from https://github.com/jeremyfelt/Custom-Posts-Per-Page for version 1.6

Location:
custom-posts-per-page/trunk
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • custom-posts-per-page/trunk/custom-posts-per-page.php

    r528367 r614406  
    22/* 
    33Plugin Name: Custom Posts Per Page 
    4 Plugin URI: http://www.jeremyfelt.com/wordpress/plugins/custom-posts-per-page/ 
     4Plugin URI: http://jeremyfelt.com/wordpress/plugins/custom-posts-per-page/ 
    55Description: Shows a custom set number of posts depending on the type of page being viewed. 
    6 Version: 1.5 
     6Version: 1.6 
    77Author: Jeremy Felt 
    8 Author URI: http://www.jeremyfelt.com 
     8Author URI: http://jeremyfelt.com 
    99Text Domain: custom-posts-per-page 
    1010Domain Path: /lang 
     
    1212*/ 
    1313 
    14 /*  Copyright 2011 Jeremy Felt (email: jeremy.felt@gmail.com) 
     14/*  Copyright 2011-2012 Jeremy Felt (email: jeremy.felt@gmail.com) 
    1515 
    1616    This program is free software; you can redistribute it and/or modify 
     
    3030class Custom_Posts_Per_Page_Foghlaim { 
    3131 
     32    /** 
     33     * When our first page has a different count than subsequent pages, we need to make 
     34     * sure the offset value is selected in order for the query to be as aware as us. 
     35     * 
     36     * @var int contains the offset to pass to the query 
     37     */ 
    3238    private $page_count_offset = 0; 
     39 
     40    /** 
     41     * We'll want to share some data about the final determinations we've made concerning 
     42     * the page view amongst methods. This is a good a container as any for it. 
     43     * 
     44     * @var array containing option data 
     45     */ 
     46    private $final_options = array(); 
     47 
     48    /** 
     49     * If we're on page 1, this will always be false. But if we do land on a page 2 or more, 
     50     * we'll be rocking true and can use that info. 
     51     * 
     52     * @var bool indication of whether a paged view has been requested 
     53     */ 
     54    private $paged_view = false; 
     55 
     56    /** 
     57     * If we're on page 1 of a big view, WordPress will give us 0. But it will report 2 and 
     58     * above, so we should be aware. 
     59     * 
     60     * @var int containing the currently viewed page number 
     61     */ 
     62    private $page_number = 1; 
    3363 
    3464    /** 
     
    4171        register_activation_hook( __FILE__, array( $this, 'upgrade_check' ) ); 
    4272 
     73        add_filter( 'plugin_action_links', array( $this, 'add_plugin_action_links' ), 10, 2 ); 
     74 
    4375        add_action( 'admin_init', array( $this, 'upgrade_check' ) ); 
    44         add_action( 'admin_menu', array( $this, 'add_settings' ) ); 
    4576        add_action( 'admin_init', array( $this, 'register_settings' ) ); 
    4677        add_action( 'admin_init', array( $this, 'add_languages' ) ); 
    47         add_filter( 'plugin_action_links', array( $this, 'add_plugin_action_links' ), 10, 2 ); 
     78 
     79        add_action( 'admin_menu', array( $this, 'add_settings' ) ); 
    4880 
    4981        if ( ! is_admin() ) 
     
    5284 
    5385    /** 
    54      * Load our language information 
     86     * Load the custom-posts-per-page text domain for internationalization 
    5587     */ 
    5688    public function add_languages() { 
     
    6597     */ 
    6698    public function upgrade_check() { 
    67         if ( '1.3' == get_option( 'cpppc_upgrade', '1.3' ) ) { 
     99        if ( '1.3' === get_option( 'cpppc_upgrade', '1.3' ) ) { 
    68100            $this->activate(); 
    69101 
    70102            $cpppc_options = get_option( 'cpppc_options' ); 
    71103 
    72             if ( isset( $cpppc_options[ 'front_page_count' ] ) ) { 
    73                 $cpppc_options[ 'front_count' ] = $cpppc_options[ 'front_page_count' ]; 
    74                 unset( $cpppc_options[ 'front_page_count' ] ); 
     104            if ( isset( $cpppc_options['front_page_count'] ) ) { 
     105                $cpppc_options['front_count'] = $cpppc_options['front_page_count']; 
     106                unset( $cpppc_options['front_page_count'] ); 
    75107            } 
    76108 
    77             if ( isset( $cpppc_options[ 'index_count' ] ) ) { 
    78                 $cpppc_options[ 'front_count_paged' ] = $cpppc_options[ 'index_count' ]; 
    79                 unset( $cpppc_options[ 'index_count' ] ); 
     109            if ( isset( $cpppc_options['index_count'] ) ) { 
     110                $cpppc_options['front_count_paged'] = $cpppc_options['index_count']; 
     111                unset( $cpppc_options['index_count'] ); 
    80112            } 
    81113 
     
    154186 
    155187    /** 
    156      * Add the settings page for Custom Posts Per Page under the settings menu. 
     188     * Add the settings page for Posts Per Page under the settings menu. 
    157189     */ 
    158190    public function add_settings() { 
     
    175207            <p style="margin-left:12px;max-width:640px;"><?php _e( 'The initial value used on activation was pulled from the setting <em>Blog Pages show at most</em> found in the', 'custom-posts-per-page' ); ?> <a href="<?php echo site_url( '/wp-admin/options-reading.php' ); ?>" title="Reading Settings"><?php _e( 'Reading Settings', 'custom-posts-per-page' ); ?></a></p> 
    176208            <form method="post" action="options.php"> 
    177     <?php 
    178         settings_fields( 'cpppc_options' ); 
    179         do_settings_sections( 'cpppc' ); 
    180         do_settings_sections( 'cpppc_custom' ); 
    181     ?> 
    182         <p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'custom-posts-per-page' ); ?>" /></p></form> 
     209                <?php 
     210                    settings_fields( 'cpppc_options' ); 
     211                    do_settings_sections( 'cpppc' ); 
     212                    do_settings_sections( 'cpppc_custom' ); 
     213                ?> 
     214            <p class="submit"><input type="submit" class="button-primary" value="<?php _e( 'Save Changes', 'custom-posts-per-page' ); ?>" /></p></form> 
    183215        </div> 
    184     <?php 
     216        <?php 
    185217    } 
    186218 
     
    220252     */ 
    221253    public function output_main_section_text() { 
    222     ?> 
     254        ?> 
    223255        <h3><?php _e( 'Main Settings', 'custom-posts-per-page' ); ?></h3> 
    224256        <p style="max-width:640px;margin-left:12px;"><?php _e( 'This section allows you to modify page view types that are 
     
    228260        <em><?php _e( 'For each setting, the box on the <strong>LEFT</strong> controls the the number of posts displayed on the first page of that view while 
    229261        the box on the <strong>RIGHT</strong> controls the number of posts seen on pages 2, 3, 4, etc... of that view.', 'custom-posts-per-page' ); ?></em></p> 
    230     <?php 
     262        <?php 
    231263    } 
    232264 
     
    235267     */ 
    236268    public function output_custom_section_text() { 
    237     ?> 
    238     <h3><?php _e( 'Custom Post Type Specific Settings', 'custom-posts-per-page' ); ?></h3> 
    239     <p style="max-width:640px;margin-left:12px;"><?php _e( 'This section contains a list of all of your registered custom post 
    240     types. In order to not conflict with other plugins or themes, these are set to 0 by default. When an option is 
    241     set to 0, it will not modify any page requests for that custom post type archive. For Custom Posts Per Page to 
    242     control the number of posts to display, these will need to be changed.', 'custom-post-per-page' ); ?></p> 
    243     <?php 
     269        ?> 
     270        <h3><?php _e( 'Custom Post Type Specific Settings', 'custom-posts-per-page' ); ?></h3> 
     271        <p style="max-width:640px;margin-left:12px;"><?php _e( 'This section contains a list of all of your registered custom post 
     272        types. In order to not conflict with other plugins or themes, these are set to 0 by default. When an option is 
     273        set to 0, it will not modify any page requests for that custom post type archive. For Custom Posts Per Page to 
     274        control the number of posts to display, these will need to be changed.', 'custom-post-per-page' ); ?></p> 
     275        <?php 
    244276    } 
    245277 
     
    267299            $this_post_data = get_post_type_object( $p ); 
    268300 
    269         ?> 
     301            ?> 
    270302            <tr> 
    271303                <td><?php echo $this_post_data->labels->name; ?></td> 
     
    274306                </td> 
    275307            </tr> 
    276         <?php 
     308            <?php 
    277309        } 
    278310    } 
     
    283315    public function output_index_count_text() { 
    284316        $cpppc_options = get_option( 'cpppc_options', array( 'front_count' => 0, 'front_count_paged' => 0 ) ); 
    285     ?> 
     317 
     318        ?> 
    286319        <input id="cpppc_index_count[0]" name="cpppc_options[front_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'front_count' ] ); ?>" /> 
    287320        &nbsp;<input id="cpppc_index_count[1]" name="cpppc_options[front_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'front_count_paged' ] ); ?>" /> 
    288     <?php 
     321        <?php 
    289322    } 
    290323 
     
    294327    public function output_category_count_text() { 
    295328        $cpppc_options = get_option( 'cpppc_options', array( 'category_count' => 0, 'category_count_paged' => 0 ) ); 
    296     ?> 
     329 
     330        ?> 
    297331        <input id="cppppc_category_count[0]" name="cpppc_options[category_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'category_count' ] ); ?>" /> 
    298332        &nbsp;<input id="cppppc_category_count[1]" name="cpppc_options[category_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'category_count_paged' ] ); ?>" /> 
    299     <?php 
     333        <?php 
    300334    } 
    301335 
     
    305339    public function output_archive_count_text() { 
    306340        $cpppc_options = get_option( 'cpppc_options', array( 'archive_count' => 0, 'archive_count_paged' => 0 ) ); 
    307     ?> 
     341 
     342        ?> 
    308343        <input id="cppppc_archive_count[0]" name="cpppc_options[archive_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'archive_count' ] ); ?>" /> 
    309344        &nbsp;<input id="cppppc_archive_count[1]" name="cpppc_options[archive_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'archive_count_paged' ] ); ?>" /> 
    310     <?php 
     345        <?php 
    311346    } 
    312347 
     
    316351    public function output_tag_count_text() { 
    317352        $cpppc_options = get_option( 'cpppc_options', array( 'tag_count' => 0, 'tag_count_paged' => 0 ) ); 
    318     ?> 
     353 
     354        ?> 
    319355        <input id="cpppc_tag_count[0]" name="cpppc_options[tag_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'tag_count' ] ); ?>" /> 
    320356        &nbsp;<input id="cpppc_tag_count[1]" name="cpppc_options[tag_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'tag_count_paged' ] ); ?>" /> 
    321     <?php 
     357        <?php 
    322358    } 
    323359 
     
    327363    public function output_author_count_text() { 
    328364        $cpppc_options = get_option( 'cpppc_options', array( 'author_count' => 0, 'author_count_paged' => 0 ) ); 
    329     ?> 
     365 
     366        ?> 
    330367        <input id="cpppc_author_count[0]" name="cpppc_options[author_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'author_count' ] ); ?>" /> 
    331368        &nbsp;<input id="cpppc_author_count[1]" name="cpppc_options[author_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'author_count_paged' ] ); ?>" /> 
    332     <?php 
     369        <?php 
    333370    } 
    334371 
     
    338375    public function output_search_count_text() { 
    339376        $cpppc_options = get_option( 'cpppc_options', array( 'search_count' => 0, 'search_count_paged' => 0 ) ); 
    340     ?> 
     377 
     378        ?> 
    341379        <input id="cppppc_search_count[0]" name="cpppc_options[search_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'search_count' ] ); ?>" /> 
    342380        &nbsp;<input id="cppppc_search_count[1]" name="cpppc_options[search_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'search_count_paged' ] ); ?>" /> 
    343     <?php 
     381        <?php 
    344382    } 
    345383 
     
    349387    public function output_default_count_text() { 
    350388        $cpppc_options = get_option( 'cpppc_options', array( 'default_count' => 0, 'default_count_paged' => 0 ) ); 
    351     ?> 
     389 
     390        ?> 
    352391        <input id="cppppc_default_count[0]" name="cpppc_options[default_count]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'default_count' ] ); ?>" /> 
    353392        &nbsp;<input id="cppppc_default_count[1]" name="cpppc_options[default_count_paged]" size="10" type="text" value="<?php echo esc_attr( $cpppc_options[ 'default_count_paged' ] ); ?>" /> 
    354     <?php 
     393        <?php 
    355394    } 
    356395 
     
    382421 
    383422        /*  If this isn't the main query, we'll avoid altering the results. */ 
    384         if ( ! $this->check_main_query( $query ) ) 
     423        if ( ! $this->check_main_query( $query ) || is_admin() ) 
    385424            return; 
    386425 
     
    392431        } 
    393432 
    394         /*  Set our own page flag for our own sanity. */ 
    395         $cpppc_paged = ( $query->get( 'paged' ) && 2 <= $query->get( 'paged' ) ) ? true : false; 
    396         $page_number = $query->get( 'paged' ); 
     433        $this->paged_view = ( $query->get( 'paged' ) && 2 <= $query->get( 'paged' ) ) ? true : false; 
     434        $this->page_number = $query->get( 'paged' ); 
    397435 
    398436        if ( $query->is_home() ) { 
    399             $final_options = $this->process_options( 'front', $cpppc_paged, $cpppc_options, $page_number ); 
     437            $this->process_options( 'front', $cpppc_options ); 
    400438        } elseif ( $query->is_post_type_archive( $post_type_array ) ) { 
    401             /*  We've just established that the visitor is loading an archive page of a custom post type by matching 
    402              *  it to a general array. Now we'll loop back through until we find exactly what post type is matching 
    403              *  so we can modify the request accordingly. */ 
    404             foreach ( $post_type_array as $my_post_type ) { 
    405                 if ( $query->is_post_type_archive( $my_post_type ) ) { 
    406                     /*  Now we know for sure what custom post type we're on. */ 
    407                     $my_post_type_option = $my_post_type; 
    408                 } 
    409             } 
    410             /*  Now check to see if we've assigned a value to this yet. When our plugin is registered, only the custom 
    411              *  post types available to us at the time are assigned options. If a new custom post type has been 
    412              *  installed, it's possible it does not yet have an option. For now we'll skip the request modification 
    413              *  and let it slide by if there is no match. */ 
    414             $final_options = $this->process_options( $my_post_type_option, $cpppc_paged, $cpppc_options, $page_number ); 
     439            $current_post_type_object = $query->get_queried_object(); 
     440            $this->process_options( $current_post_type_object->name, $cpppc_options ); 
    415441        } elseif ( $query->is_category() ) { 
    416             $final_options = $this->process_options( 'category', $cpppc_paged, $cpppc_options, $page_number ); 
     442            $this->process_options( 'category', $cpppc_options ); 
    417443        } elseif ( $query->is_tag() ) { 
    418             $final_options = $this->process_options( 'tag', $cpppc_paged, $cpppc_options, $page_number ); 
     444            $this->process_options( 'tag', $cpppc_options ); 
    419445        } elseif ( $query->is_author() ) { 
    420             $final_options = $this->process_options( 'author', $cpppc_paged, $cpppc_options, $page_number ); 
     446            $this->process_options( 'author', $cpppc_options ); 
    421447        } elseif ( $query->is_search() ) { 
    422             $final_options = $this->process_options( 'search', $cpppc_paged, $cpppc_options, $page_number ); 
     448            $this->process_options( 'search', $cpppc_options ); 
    423449        } elseif ( $query->is_archive() ) { 
    424450            /*  Note that the check for is_archive needs to be below anything else that WordPress may consider an 
    425451             *  archive. This includes is_tag, is_category, is_author and probably some others. */ 
    426             $final_options = $this->process_options( 'archive', $cpppc_paged, $cpppc_options, $page_number ); 
     452            $this->process_options( 'archive', $cpppc_options ); 
    427453        } else { 
    428             $final_options = $this->process_options( 'default', $cpppc_paged, $cpppc_options, $page_number ); 
    429         } 
    430  
    431         if ( isset( $final_options[ 'posts' ] ) ) { 
    432             $query->set( 'posts_per_page', $final_options[ 'posts' ] ); 
    433             $query->set( 'offset', $final_options[ 'offset' ] ); 
    434         } 
    435  
    436         if ( 0 <> $this->page_count_offset ) 
    437             add_filter( 'found_posts', array( $this, 'correct_found_posts' ) ); 
     454            $this->process_options( 'default', $cpppc_options ); 
     455        } 
     456 
     457        if ( isset( $this->final_options['posts'] ) ) { 
     458            $query->set( 'posts_per_page', $this->final_options['posts'] ); 
     459            $query->set( 'offset', $this->final_options['offset'] ); 
     460        } 
     461 
     462        add_filter( 'found_posts', array( $this, 'correct_found_posts' ) ); 
     463 
    438464    } 
    439465 
    440466    /** 
    441467     * The offset and post count deal gets a bit confused when the first page and subsequent pages stop matching. 
    442      * This function helps realign things once we've screwed with them. 
     468     * This function helps realign things once we've screwed with them by doing some math to determine how many 
     469     * posts we need to return to the query in order for core to calculate the correct number of pages required. 
    443470     * 
    444471     * @param $found_posts int The number of found posts 
     
    446473     */ 
    447474    public function correct_found_posts( $found_posts ) { 
    448         return ( $found_posts + $this->page_count_offset ); 
    449     } 
    450  
    451     /** 
    452      * @param $option_prefix string The prefix of the count and count_paged options in the database 
    453      * @param $cpppc_paged bool True if this is paged, false if it isn't. 
     475 
     476        if ( empty( $this->final_options['set_count'] ) || empty( $this->final_options['set_count_paged'] ) ) 
     477            return $found_posts; 
     478 
     479        // We don't have the same issues if our first page and paged counts are the same as the math is easy then 
     480        if ( $this->final_options['set_count'] == $this->final_options['set_count_paged'] ) 
     481            return $found_posts; 
     482 
     483        // Do the true calculation for pages required based on both 
     484        // values: page 1 posts count and subsequent page post counts 
     485        $pages_required = ( ( ( $found_posts - $this->final_options['set_count'] ) / $this->final_options['set_count_paged'] ) + 1 ); 
     486 
     487        if ( 0 === $this->page_number ) 
     488            return $pages_required * $this->final_options['set_count']; 
     489 
     490        if ( 1 < $this->page_number ) 
     491            return $pages_required * $this->final_options['set_count_paged']; 
     492 
     493        return $found_posts; 
     494    } 
     495 
     496    /** 
     497     * We use this function to abstract the processing of options while we determine what 
     498     * type of view we're working with and what to use for the count on the initial page 
     499     * and subsequent pages. The options are stored in a private property that allows us 
     500     * access throughout the class after this. 
     501     * 
     502     * @param $option_prefix string prefix of the count and count_paged options in the database 
    454503     * @param $cpppc_options array of options from the database for custom posts per page 
    455      * @param null $page_number int Which page is it? 
    456      * @return array Contains the final values for posts_per_page and offset to be passed to the query 
    457      */ 
    458     public function process_options( $option_prefix, $cpppc_paged, $cpppc_options, $page_number = NULL ) { 
    459         $final_options = array(); 
    460  
    461         if ( ! $cpppc_paged && ! empty( $cpppc_options[ $option_prefix . '_count' ] ) ) { 
    462             $final_options[ 'posts' ]  = $cpppc_options[ $option_prefix . '_count' ]; 
    463             $final_options[ 'offset' ] = 0; 
    464         } elseif ( $cpppc_paged & ! empty( $cpppc_options[ $option_prefix . '_count_paged' ] ) ) { 
     504     */ 
     505    public function process_options( $option_prefix, $cpppc_options ) { 
     506        if ( ! $this->paged_view && ! empty( $cpppc_options[ $option_prefix . '_count' ] ) ) { 
     507            $this->final_options[ 'posts' ]  = $cpppc_options[ $option_prefix . '_count' ]; 
     508            $this->final_options[ 'offset' ] = 0; 
     509            $this->final_options['set_count'] = $cpppc_options[ $option_prefix . '_count' ]; 
     510            $this->final_options['set_count_paged'] = $cpppc_options[ $option_prefix . '_count_paged' ]; 
     511        } elseif ( $this->paged_view & ! empty( $cpppc_options[ $option_prefix . '_count_paged' ] ) ) { 
    465512            $this->page_count_offset = ( $cpppc_options[ $option_prefix . '_count_paged' ] - $cpppc_options[ $option_prefix . '_count' ] ); 
    466             $final_options[ 'offset' ]  = ( ( $page_number - 2 ) * $cpppc_options[ $option_prefix . '_count_paged' ] + $cpppc_options[ $option_prefix . '_count' ] ); 
    467             $final_options[ 'posts' ]   = $cpppc_options[ $option_prefix . '_count_paged' ]; 
    468         } 
    469         return $final_options; 
     513            $this->final_options[ 'offset' ]  = ( ( $this->page_number - 2 ) * $cpppc_options[ $option_prefix . '_count_paged' ] + $cpppc_options[ $option_prefix . '_count' ] ); 
     514            $this->final_options[ 'posts' ]   = $cpppc_options[ $option_prefix . '_count_paged' ]; 
     515            $this->final_options['set_count'] = $cpppc_options[ $option_prefix . '_count' ]; 
     516            $this->final_options['set_count_paged'] = $cpppc_options[ $option_prefix . '_count_paged' ]; 
     517        } 
    470518    } 
    471519} 
  • custom-posts-per-page/trunk/readme.txt

    r528369 r614406  
    22 
    33Contributors: jeremyfelt 
    4 Donate link: http://www.jeremyfelt.com/wordpress/plugins/custom-posts-per-page/ 
     4Donate link: http://jeremyfelt.com/wordpress/plugins/custom-posts-per-page/ 
    55Tags: admin, administration, settings, archives, posts-per-page, paged, posts, count, number, custom-post-type 
    66Requires at least: 3.2.1 
    7 Tested up to: 3.4 
    8 Stable tag: 1.5 
     7Tested up to: 3.5 
     8Stable tag: 1.6 
    99 
    1010Custom Posts Per Page provides a settings page in your WordPress admin that allows you to specify how many posts are displayed for different views. 
     
    1212== Description == 
    1313 
    14 Custom Posts Per Page allows you to specify how many posts are displayed per page depending on your current view. Once settings are changed, the *Blog pages show at most* setting in the *Reading* menu will ignored. 
     14Custom Posts Per Page allows you to specify how many posts are displayed per page depending on your current view. Once settings are changed, the *Blog pages show at most* setting in the *Reading* menu will be ignored. 
    1515 
    1616Settings are available for: 
     
    5353 
    5454== Changelog == 
     55= 1.6 = 
     56* General code cleanup, more documentation 
     57* Move final_options into a private property for easy passing around the plugin 
     58* Much improved logic for handling the expected page count based on found posts. Should resolve any issues found when using plugins such as WP-PageNavi to show number of pages in navigation. 
     59 
    5560= 1.5 = 
    5661* A bunch of code cleanup. Move everything to a class. 
     
    109114 
    110115== Upgrade Notice == 
     116= 1.6 = 
     117* Fixes issues of wrongly reported page numbering when using WP-PageNavi or other paged navigation plugins that rely on WordPress's page count. 
     118 
    111119= 1.4 = 
    112120 
Note: See TracChangeset for help on using the changeset viewer.