Plugin Directory

Changeset 605669


Ignore:
Timestamp:
09/29/2012 07:57:20 AM (13 years ago)
Author:
superann
Message:

added filter/formatting contributions by Viper007Bond

Location:
sticky-custom-post-types/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • sticky-custom-post-types/trunk/readme.txt

    r435042 r605669  
    44Tags: custom post types, sticky
    55Requires at least: 3.0
    6 Tested up to: 3.2.1
     6Tested up to: 3.4.2
    77Stable tag: trunk
     8License: GPLv2 or later
     9License URI: http://www.gnu.org/licenses/gpl-2.0.html
    810
    911Enables support for sticky custom post types.
     
    3335== Changelog ==
    3436
     37= 1.2.3 =
     38* Added contributions by [Viper007Bond](http://www.viper007bond.com) including enhanced posts filter (now affects main query only and will respect other post types) and formatting updates to conform with the official WordPress Coding Standards doc in the codex.
     39* Home query no longer modified at all if "Display selected post type(s) on home page" option is left unchecked (note that this means that checked sticky custom posts will show up in the home feed but non-sticky custom posts will be left out of the normal flow).
     40
    3541= 1.2.2 =
    3642* Added custom post types to paged blog home.
     
    4854= 1.0 =
    4955* Initial version.
     56
     57== Upgrade Notice ==
     58
     59= 1.2.3 =
     60Activating the posts filter function on home now affects main query only and respects other post types, which should make it play more nicely with other plugins.
  • sticky-custom-post-types/trunk/sticky-custom-post-types.php

    r435040 r605669  
    44Plugin URI: http://superann.com/sticky-custom-post-types/
    55Description: Enables support for sticky custom post types. Set options in Settings → Reading.
    6 Version: 1.2.2
     6Version: 1.2.3
    77Author: Ann Oyama
    88Author URI: http://superann.com
     
    2626*/
    2727
     28add_action( 'admin_init', 'super_sticky_add_meta_box' );
     29add_action( 'admin_init', 'super_sticky_admin_init', 20 );
     30add_action( 'pre_get_posts', 'super_sticky_posts_filter' );
     31
    2832function super_sticky_description() {
    29     echo '<p>'.__('Enable support for sticky custom post types.').'</p>';
     33    echo '<p>' . __( 'Enable support for sticky custom post types.' ) . '</p>';
    3034}
    3135
    3236function super_sticky_set_post_types() {
    33     $post_types = get_post_types(array('_builtin' => false, 'public' => true), 'names');
    34     if(!empty($post_types)) {
     37    $post_types = get_post_types( array( '_builtin' => false, 'public' => true ), 'names' );
     38    if ( ! empty( $post_types ) ) {
    3539        $checked_post_types = super_sticky_post_types();
    36         foreach($post_types as $post_type) { ?>
    37             <div><input type="checkbox" id="post_type_<?php echo $post_type; ?>" name="sticky_custom_post_types[]" value="<?php echo $post_type; ?>" <?php checked(in_array($post_type, $checked_post_types)); ?> /> <label for="post_type_<?php echo $post_type; ?>"><?php echo $post_type; ?></label></div><?php
     40        foreach ( $post_types as $post_type ) { ?>
     41            <div><input type="checkbox" id="<?php echo esc_attr( 'post_type_' . $post_type ); ?>" name="sticky_custom_post_types[]" value="<?php echo esc_attr( $post_type ); ?>" <?php checked( in_array( $post_type, $checked_post_types ) ); ?> /> <label for="<?php echo esc_attr( 'post_type_' . $post_type ); ?>"><?php echo esc_html( $post_type ); ?></label></div><?php
    3842        }
     43    } else {
     44        echo '<p>' . __( 'No public custom post types found.' ) . '</p>';
    3945    }
    40     else
    41         echo '<p>'.__('No public custom post types found.').'</p>';
    4246}
    4347
    44 function super_sticky_filter($query_type) {
    45     $filters = get_option('sticky_custom_post_types_filters');
    46     if(!is_array($filters)) $filters = array();
    47         return in_array($query_type, $filters);
     48function super_sticky_filter( $query_type ) {
     49    $filters = (array) get_option( 'sticky_custom_post_types_filters', array() );
     50
     51    return in_array( $query_type, $filters );
    4852}
    4953
    5054function super_sticky_set_filters() { ?>
    51     <span><input type="checkbox" id="sticky_custom_post_types_filters_home" name="sticky_custom_post_types_filters[]" value="home" <?php checked(super_sticky_filter('home')); ?> /> <label for="sticky_custom_post_types_filters_home">home</label></span><?php
     55    <span><input type="checkbox" id="sticky_custom_post_types_filters_home" name="sticky_custom_post_types_filters[]" value="home" <?php checked( super_sticky_filter( 'home' ) ); ?> /> <label for="sticky_custom_post_types_filters_home">home</label></span><?php
    5256}
    5357
    5458function super_sticky_admin_init() {
    55     register_setting('reading', 'sticky_custom_post_types');
    56     register_setting('reading', 'sticky_custom_post_types_filters');
    57     add_settings_section('super_sticky_options', 'Sticky Custom Post Types', 'super_sticky_description', 'reading');
    58     add_settings_field('sticky_custom_post_types', 'Show "Stick this..." checkbox on', 'super_sticky_set_post_types', 'reading', 'super_sticky_options');
    59     add_settings_field('sticky_custom_post_types_filters', 'Display selected post type(s) on', 'super_sticky_set_filters', 'reading', 'super_sticky_options');
     59    register_setting( 'reading', 'sticky_custom_post_types' );
     60    register_setting( 'reading', 'sticky_custom_post_types_filters' );
     61    add_settings_section( 'super_sticky_options', __( 'Sticky Custom Post Types' ), 'super_sticky_description', 'reading' );
     62    add_settings_field( 'sticky_custom_post_types', __( 'Show "Stick this..." checkbox on' ), 'super_sticky_set_post_types', 'reading', 'super_sticky_options' );
     63    add_settings_field( 'sticky_custom_post_types_filters', __( 'Display selected post type(s) on' ), 'super_sticky_set_filters', 'reading', 'super_sticky_options' );
    6064}
    6165
    62 add_action('admin_init', 'super_sticky_admin_init', 20);
    63 
    64 function super_sticky_post_types($posts=false) {
    65     $post_types = get_option('sticky_custom_post_types');
    66     if(!is_array($post_types))
    67         $post_types = array();
    68     if($posts)
    69         $post_types[] = 'post';
    70     return $post_types;
     66function super_sticky_post_types() {
     67    return (array) get_option( 'sticky_custom_post_types', array() );
    7168}
    7269
    73 function super_sticky_meta() { global $post; ?>
    74     <input id="super-sticky" name="sticky" type="checkbox" value="sticky" <?php checked(is_sticky($post->ID)); ?> /> <label for="super-sticky" class="selectit"><?php _e('Stick this to the front page') ?></label><?php
     70function super_sticky_meta() { ?>
     71    <input id="super-sticky" name="sticky" type="checkbox" value="sticky" <?php checked( is_sticky() ); ?> /> <label for="super-sticky" class="selectit"><?php _e( 'Stick this to the front page' ) ?></label><?php
    7572}
    7673
    7774function super_sticky_add_meta_box() {
    78     foreach(super_sticky_post_types() as $post_type)
    79         if(current_user_can('edit_others_posts'))
    80             add_meta_box('super_sticky_meta', 'Sticky', 'super_sticky_meta', $post_type, 'side', 'high');
     75    if( ! current_user_can( 'edit_others_posts' ) )
     76        return;
     77
     78    foreach( super_sticky_post_types() as $post_type )
     79        add_meta_box( 'super_sticky_meta', __( 'Sticky' ), 'super_sticky_meta', $post_type, 'side', 'high' );
    8180}
    8281
    83 add_action('admin_init', 'super_sticky_add_meta_box');
     82function super_sticky_posts_filter( $query ) {
     83    if ( $query->is_main_query() && $query->is_home() && ! $query->get( 'suppress_filters' ) && super_sticky_filter( 'home' ) ) {
    8484
    85 function super_sticky_posts_filter($query) {
    86     if($query->is_home && !$query->get('suppress_filters'))
    87         if(super_sticky_filter('home'))
    88             $query->set('post_type', super_sticky_post_types(true));
    89         else
    90             $query->set('post_type', 'post');
    91     return $query;
     85        $super_sticky_post_types = super_sticky_post_types();
     86
     87        if ( ! empty( $super_sticky_post_types ) ) {
     88            $post_types = array();
     89
     90            $query_post_type = $query->get( 'post_type' );
     91
     92            if ( empty( $query_post_type ) ) {
     93                $post_types[] = 'post';
     94            } elseif ( is_string( $query_post_type ) ) {
     95                $post_types[] = $query_post_type;
     96            } elseif ( is_array( $query_post_type ) ) {
     97                $post_types = $query_post_type;
     98            } else {
     99                return; // Unexpected value
     100            }
     101
     102            $post_types = array_merge( $post_types, $super_sticky_post_types );
     103
     104            $query->set( 'post_type', $post_types );
     105        }
     106    }
    92107}
    93108
    94 add_filter('pre_get_posts', 'super_sticky_posts_filter');
    95109?>
Note: See TracChangeset for help on using the changeset viewer.