| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Featured Image Column |
| 4 |
* Plugin URI: http://austinpassy.com/wordpress-plugins/featured-image-column |
| 5 |
* Description: |
| 6 |
* Version: 0.1.5 |
| 7 |
* Author: Austin Passy |
| 8 |
* Author URI: http://austinpassy.com |
| 9 |
* |
| 10 |
* @copyright 2009 - 2011 |
| 11 |
* @author Austin Passy |
| 12 |
* @link http://frostywebdesigns.com/ |
| 13 |
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 18 |
* |
| 19 |
* @package Featured_Image_Column |
| 20 |
*/ |
| 21 |
|
| 22 |
if ( !class_exists( 'Featured_Image_Column' ) ) { |
| 23 |
class Featured_Image_Column { |
| 24 |
|
| 25 |
const domain = 'featured-image-column'; |
| 26 |
const version = '0.1.5'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Sets up the Featured_Image_Column plugin and loads files at the appropriate time. |
| 30 |
* |
| 31 |
* @since 0.1 |
| 32 |
*/ |
| 33 |
function __construct() { |
| 34 |
$post_type = ( !empty( $_GET['post_type'] ) ) ? $_GET['post_type'] : 'post'; |
| 35 |
|
| 36 |
/* Define constants */ |
| 37 |
add_action( 'plugins_loaded', array( __CLASS__, 'constants' ) ); |
| 38 |
|
| 39 |
add_action( 'admin_init', array( __CLASS__, 'localize' ) ); |
| 40 |
|
| 41 |
add_action( 'init', array( __CLASS__, 'add_theme_support' ) ); |
| 42 |
|
| 43 |
/* Print style */ |
| 44 |
add_action( 'admin_head', array( __CLASS__, 'style' ) ); |
| 45 |
|
| 46 |
/* Column manager */ |
| 47 |
if ( self::has_posts( $post_type ) ) { |
| 48 |
add_filter( 'manage_posts_columns', array( __CLASS__, 'columns' ), 10, 2 ); |
| 49 |
add_filter( 'manage_pages_columns', array( __CLASS__, 'columns' ), 10, 2 ); |
| 50 |
add_filter( "manage_edit-{$post_type}_columns", array( __CLASS__, 'columns' ), 10, 2 ); |
| 51 |
add_action( "manage_{$post_type}_posts_custom_column", array( __CLASS__, 'column_data' ), 10, 2 ); |
| 52 |
} |
| 53 |
|
| 54 |
/* Prints pointer javascripts */ |
| 55 |
add_action( 'admin_enqueue_scripts', array( __CLASS__, 'pointer' ) ); |
| 56 |
|
| 57 |
do_action( 'featured_image_column_loaded' ); |
| 58 |
} |
| 59 |
|
| 60 |
function constants() { |
| 61 |
/* Set constant path to the plugin directory. */ |
| 62 |
define( 'FEATURED_IMAGE_COLUMN_DIR', plugin_dir_path( __FILE__ ) ); |
| 63 |
define( 'FEATURED_IMAGE_COLUMN_ADMIN', trailingslashit( FEATURED_IMAGE_COLUMN_DIR ) . 'admin/' ); |
| 64 |
|
| 65 |
/* Set constant path to the plugin URL. */ |
| 66 |
define( 'FEATURED_IMAGE_COLUMN_URL', plugin_dir_url( __FILE__ ) ); |
| 67 |
define( 'FEATURED_IMAGE_COLUMN_IMAGES', FEATURED_IMAGE_COLUMN_URL . 'images/' ); |
| 68 |
define( 'FEATURED_IMAGE_COLUMN_CSS', FEATURED_IMAGE_COLUMN_URL . 'css/' ); |
| 69 |
define( 'FEATURED_IMAGE_COLUMN_JS', FEATURED_IMAGE_COLUMN_URL . 'js/' ); |
| 70 |
} |
| 71 |
|
| 72 |
function localize() { |
| 73 |
load_plugin_textdomain( self::domain, null, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* WordPress version check |
| 78 |
* |
| 79 |
* @since 0.1.1 |
| 80 |
* @since 09/19/2011 |
| 81 |
*/ |
| 82 |
function is_version( $version = '3.3' ) { |
| 83 |
global $wp_version; |
| 84 |
|
| 85 |
if ( version_compare( $wp_version, $version, '<' ) ) { |
| 86 |
return false; |
| 87 |
} |
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Function to test if has posts to avoid a fatal error |
| 93 |
* |
| 94 |
* @since 0.1.4 10/17/2011 |
| 95 |
*/ |
| 96 |
function has_posts( $post_type = null ) { |
| 97 |
$post_type = ( !empty( $_GET['post_type'] ) ) ? $_GET['post_type'] : 'post'; |
| 98 |
|
| 99 |
$args = array( |
| 100 |
'post_type' => $post_type, |
| 101 |
'post_status' => array( 'publish', 'pending', 'draft', 'future', 'private' ) |
| 102 |
); |
| 103 |
|
| 104 |
$the_query = new WP_Query( $args ); |
| 105 |
|
| 106 |
if ( !$the_query->have_posts() ) { |
| 107 |
return false; |
| 108 |
} |
| 109 |
wp_reset_query(); |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Add stylesheet |
| 115 |
* @since 0.1 |
| 116 |
*/ |
| 117 |
function style() { |
| 118 |
global $pagenow; |
| 119 |
|
| 120 |
if ( $pagenow == 'edit.php' ) { ?> |
| 121 |
<style type="text/css"> |
| 122 |
th#featured-image, |
| 123 |
td.featured-image.column-featured-image { |
| 124 |
max-height: 50px; |
| 125 |
max-width: 50px; |
| 126 |
width: 50px; |
| 127 |
} |
| 128 |
.featured-image.column-featured-image img { |
| 129 |
max-height: 32px; |
| 130 |
max-width: 36px; |
| 131 |
-ms-interpolation-mode: bicubic; |
| 132 |
} |
| 133 |
</style><?php |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Filter the image in before the 'title' |
| 139 |
* |
| 140 |
* @todo fix error if no posts exist. |
| 141 |
* For some reason returning before the 'foreach' |
| 142 |
* still triggers the 'foreach', which is causing |
| 143 |
* a fatal error when WP_DEBUG is true OR there are |
| 144 |
* zero posts. |
| 145 |
*/ |
| 146 |
function columns( $columns, $post_type = 'post' ) { |
| 147 |
$post_type = get_post_type(); |
| 148 |
|
| 149 |
/** |
| 150 |
if ( !post_type_supports( $post_type, 'thumbnail' ) ) |
| 151 |
return; |
| 152 |
*/ |
| 153 |
|
| 154 |
if ( !self::included_post_types( $post_type ) ) |
| 155 |
return; |
| 156 |
|
| 157 |
if ( !isset( $columns ) && ( empty( $columns ) || !$columns ) ) |
| 158 |
return; |
| 159 |
|
| 160 |
$new = array(); |
| 161 |
|
| 162 |
foreach( $columns as $key => $title ) { |
| 163 |
if ( $key == 'title' ) // Put the Thumbnail column before the Title column |
| 164 |
$new['featured-image'] = __( 'Image', self::domain ); |
| 165 |
$new[$key] = $title; |
| 166 |
} |
| 167 |
return $new; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Output the image |
| 172 |
* |
| 173 |
*/ |
| 174 |
function column_data( $column_name, $post_id ) { |
| 175 |
|
| 176 |
$post_id = ( !empty( $post_id ) ) ? $post_id : get_the_ID(); |
| 177 |
$post_type = get_post_type(); |
| 178 |
|
| 179 |
if ( !self::included_post_types( $post_type ) ) |
| 180 |
return; |
| 181 |
|
| 182 |
if ( post_type_supports( $post_type, 'thumbnail' ) && 'featured-image' == $column_name ) { |
| 183 |
echo '<img alt="' . esc_attr( get_the_title() ) . '" src="' . esc_url( self::get_the_image( $post_id ) ) . '" />'; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Add theme support for post-thumbnails |
| 189 |
* on pages if the feature isn't available |
| 190 |
* |
| 191 |
* @since 0.1.1 09/20/2011 |
| 192 |
*/ |
| 193 |
function add_theme_support() { |
| 194 |
if ( !post_type_supports( 'page', 'thumbnail' ) ) |
| 195 |
add_theme_support( 'post-thumbnails', array( 'post', 'page' ) ); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* Add custom image size |
| 200 |
* |
| 201 |
* @since 0.1 |
| 202 |
* @deprecated 0.1.1 |
| 203 |
*/ |
| 204 |
function add_image_size() { |
| 205 |
_deprecated_function( __FUNCTION__, '0.1.1', '' ); |
| 206 |
return; |
| 207 |
|
| 208 |
add_image_size( 'featured-column-thumbnail', 32, 32, true ); |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Function to get the image |
| 213 |
* |
| 214 |
* @since 0.1 |
| 215 |
* @updated 0.1.3 - Added wp_cache_set() |
| 216 |
*/ |
| 217 |
function get_the_image( $post_id ) { |
| 218 |
$post_id = ( !empty( $post_id ) ) ? $post_id : get_the_id(); |
| 219 |
|
| 220 |
$image = ''; |
| 221 |
$image = wp_cache_get( 'featured_column_thumbnail', 'post' ); |
| 222 |
|
| 223 |
if ( !is_array( $image ) ) |
| 224 |
$image = array(); |
| 225 |
|
| 226 |
if ( false == $image ) { |
| 227 |
if ( has_post_thumbnail() ) { |
| 228 |
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), array( 36, 32 ) ); |
| 229 |
$image = esc_url( $image[0] ); |
| 230 |
} else { |
| 231 |
$image = esc_url( plugins_url( 'images/default.png', __FILE__ ) ); |
| 232 |
} |
| 233 |
if ( !is_wp_error( $image ) ) |
| 234 |
wp_cache_set( 'featured_column_thumbnail', $image, 'post', 60*60*24 ); |
| 235 |
} |
| 236 |
return apply_filters( 'featured_image_column_default_image', $image ); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Filterable $post_types |
| 241 |
* |
| 242 |
* @since 0.1.4 10/17/2011 |
| 243 |
* @props Bill Erickson |
| 244 |
* @ref http://wordpress.org/support/topic/plugin-featured-image-column-filter-for-post-types |
| 245 |
*/ |
| 246 |
function included_post_types( $post_type ) { |
| 247 |
$post_types = array(); |
| 248 |
|
| 249 |
if ( post_type_supports( 'post', 'thumbnail' ) ) $post_types[] = 'post'; |
| 250 |
if ( post_type_supports( 'page', 'thumbnail' ) ) $post_types[] = 'page'; |
| 251 |
|
| 252 |
$post_types = apply_filters( 'featured_image_column_post_types', $post_types ); |
| 253 |
|
| 254 |
if ( !in_array( $post_type, $post_types ) ) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* In version 3.3 this will show if the user hasn't |
| 262 |
* set page to support 'thumbnails' which is false |
| 263 |
* by deafult |
| 264 |
* |
| 265 |
* @since 0.1.1 |
| 266 |
*/ |
| 267 |
function pointer() { |
| 268 |
|
| 269 |
if ( self::is_version( '3.3.0' ) && post_type_supports( 'page', 'thumbnail' ) ) |
| 270 |
return; |
| 271 |
|
| 272 |
$page = get_user_setting( 'featured_image_page_pointer', 0 ); |
| 273 |
if ( !$page ) { |
| 274 |
wp_enqueue_style( 'wp-pointer' ); |
| 275 |
wp_enqueue_script( array( 'wp-pointer', 'utils' ) ); |
| 276 |
add_action( 'admin_print_footer_scripts', array( __CLASS__, 'pointer_scripts' ) ); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Returns the pointer content |
| 282 |
*/ |
| 283 |
function pointer_scripts() { |
| 284 |
$pointer_content = __( '<h3>Thanks for installing Featured Image Gallery!</h3>' ); |
| 285 |
$pointer_content .= __( '<p>It looks like you haven\'t set your pages to support <code>thumbnail</code>\'s <br />' ); |
| 286 |
?> |
| 287 |
<script type="text/javascript"> |
| 288 |
//<![CDATA[ |
| 289 |
jQuery(document).ready( function($) { |
| 290 |
$('#menu-pages').pointer({ |
| 291 |
content: '<?php echo $pointer_content; ?>', |
| 292 |
position: { |
| 293 |
my: 'right top', |
| 294 |
at: 'left top', |
| 295 |
offset: '0 -2' |
| 296 |
}, |
| 297 |
arrow: { |
| 298 |
edge: 'left', |
| 299 |
align: 'top', |
| 300 |
offset: 10 |
| 301 |
}, |
| 302 |
close: function() { |
| 303 |
setUserSetting( 'featured_image_page_pointer', '1' ); |
| 304 |
} |
| 305 |
}).pointer('open'); |
| 306 |
}); |
| 307 |
//]]> |
| 308 |
</script><?php |
| 309 |
} |
| 310 |
|
| 311 |
} |
| 312 |
}; |
| 313 |
|
| 314 |
$featured_image_column = new Featured_Image_Column; |
| 315 |
|
| 316 |
?> |