Created
August 21, 2012 04:33
-
-
Save Viper007Bond/3411587 to your computer and use it in GitHub Desktop.
Bug fixes and coding standards for Sticky Custom Post Types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Sticky Custom Post Types | |
Plugin URI: http://superann.com/sticky-custom-post-types/ | |
Description: Enables support for sticky custom post types. Set options in Settings → Reading. | |
Version: 1.2.2 | |
Author: Ann Oyama | |
Author URI: http://superann.com | |
License: GPL2 | |
Copyright 2011 Ann Oyama (email : wordpress [at] superann.com) | |
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 | |
*/ | |
add_action( 'admin_init', 'super_sticky_add_meta_box' ); | |
add_action( 'admin_init', 'super_sticky_admin_init', 20 ); | |
add_action( 'pre_get_posts', 'super_sticky_posts_filter' ); | |
function super_sticky_description() { | |
echo '<p>' . __( 'Enable support for sticky custom post types.' ) . '</p>'; | |
} | |
function super_sticky_set_post_types() { | |
$post_types = get_post_types( array( '_builtin' => false, 'public' => true ), 'names' ); | |
if ( ! empty( $post_types ) ) { | |
$checked_post_types = super_sticky_post_types(); | |
foreach ( $post_types as $post_type ) { ?> | |
<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 | |
} | |
} else { | |
echo '<p>' . __( 'No public custom post types found.' ) . '</p>'; | |
} | |
} | |
function super_sticky_filter( $query_type ) { | |
$filters = (array) get_option( 'sticky_custom_post_types_filters', array() ); | |
return in_array( $query_type, $filters ); | |
} | |
function super_sticky_set_filters() { ?> | |
<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 | |
} | |
function super_sticky_admin_init() { | |
register_setting( 'reading', 'sticky_custom_post_types' ); | |
register_setting( 'reading', 'sticky_custom_post_types_filters' ); | |
add_settings_section( 'super_sticky_options', __( 'Sticky Custom Post Types' ), 'super_sticky_description', 'reading' ); | |
add_settings_field( 'sticky_custom_post_types', __( 'Show "Stick this..." checkbox on' ), 'super_sticky_set_post_types', 'reading', 'super_sticky_options' ); | |
add_settings_field( 'sticky_custom_post_types_filters', __( 'Display selected post type(s) on' ), 'super_sticky_set_filters', 'reading', 'super_sticky_options' ); | |
} | |
function super_sticky_post_types() { | |
return (array) get_option( 'sticky_custom_post_types', array() ); | |
} | |
function super_sticky_meta() { ?> | |
<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 | |
} | |
function super_sticky_add_meta_box() { | |
if( ! current_user_can( 'edit_others_posts' ) ) | |
return; | |
foreach( super_sticky_post_types() as $post_type ) | |
add_meta_box( 'super_sticky_meta', __( 'Sticky' ), 'super_sticky_meta', $post_type, 'side', 'high' ); | |
} | |
function super_sticky_posts_filter( $query ) { | |
if ( $query->is_main_query() && $query->is_home() && ! $query->get( 'suppress_filters' ) && super_sticky_filter( 'home' ) ) { | |
$super_sticky_post_types = super_sticky_post_types(); | |
if ( ! empty( $super_sticky_post_types ) ) { | |
$post_types = array(); | |
$query_post_type = $query->get( 'post_type' ); | |
if ( empty( $query_post_type ) ) { | |
$post_types[] = 'post'; | |
} elseif ( is_string( $query_post_type ) ) { | |
$post_types[] = $query_post_type; | |
} elseif ( is_array( $query_post_type ) ) { | |
$post_types = $query_post_type; | |
} else { | |
return; // Unexpected value | |
} | |
$post_types = array_merge( $post_types, $super_sticky_post_types ); | |
$query->set( 'post_type', $post_types ); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment