| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Featured Image Column |
| 4 |
* Plugin URI: http://austinpassy.com/wordpress-plugins/featured-image-column |
| 5 |
* Description: Adds a column to the edit screen with the featured image if it exists. |
| 6 |
* Version: 0.1.6 |
| 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.6'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Ensures that the rest of the code only runs on edit.php pages |
| 30 |
* |
| 31 |
* @since 0.1 |
| 32 |
*/ |
| 33 |
function __construct() { |
| 34 |
add_action( 'load-edit.php', array( __CLASS__, 'load' ) ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Since the load-edit.php hook is too early for checking the post type, hook the rest |
| 39 |
* of the code to the wp action to allow the query to be run first |
| 40 |
* |
| 41 |
* @since 0.1.6 |
| 42 |
*/ |
| 43 |
function load() { |
| 44 |
add_action( 'wp', array( __CLASS__, 'init' ) ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Sets up the Featured_Image_Column plugin and loads files at the appropriate time. |
| 49 |
* |
| 50 |
* @since 0.1.6 |
| 51 |
*/ |
| 52 |
function init() { |
| 53 |
$post_type = get_post_type(); |
| 54 |
|
| 55 |
if ( !post_type_supports( $post_type, 'thumbnail' ) ) |
| 56 |
return; |
| 57 |
|
| 58 |
/* Print style */ |
| 59 |
add_action( 'admin_print_styles', array( __CLASS__, 'style' ), 0 ); |
| 60 |
|
| 61 |
/* Column manager */ |
| 62 |
add_filter( "manage_{$post_type}_posts_columns", array( __CLASS__, 'columns' ) ); |
| 63 |
add_action( "manage_{$post_type}_posts_custom_column", array( __CLASS__, 'column_data' ), 10, 2 ); |
| 64 |
|
| 65 |
do_action( 'featured_image_column_loaded' ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Enqueue stylesheaet |
| 70 |
* @since 0.1 |
| 71 |
*/ |
| 72 |
function style() { |
| 73 |
wp_enqueue_style( 'featured-image-column', plugin_dir_url( __FILE__ ) . 'css/column.css', null, self::version ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Filter the image in before the 'title' |
| 78 |
*/ |
| 79 |
function columns( $columns ) { |
| 80 |
if ( !is_array( $columns ) ) |
| 81 |
$columns = array(); |
| 82 |
|
| 83 |
$new = array(); |
| 84 |
|
| 85 |
foreach( $columns as $key => $title ) { |
| 86 |
if ( $key == 'title' ) // Put the Thumbnail column before the Title column |
| 87 |
$new['featured-image'] = __( 'Image', self::domain ); |
| 88 |
|
| 89 |
$new[$key] = $title; |
| 90 |
} |
| 91 |
|
| 92 |
return $new; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Output the image |
| 97 |
*/ |
| 98 |
function column_data( $column_name, $post_id ) { |
| 99 |
if ( 'featured-image' != $column_name ) |
| 100 |
return; |
| 101 |
|
| 102 |
$image_src = self::get_the_image( $post_id ); |
| 103 |
|
| 104 |
if ( empty( $image_src ) ) { |
| 105 |
echo " "; // This helps prevent issues with empty cells |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
echo '<img alt="' . esc_attr( get_the_title() ) . '" src="' . esc_url( $image_src ) . '" />'; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Function to get the image |
| 114 |
* |
| 115 |
* @since 0.1 |
| 116 |
* @updated 0.1.3 - Added wp_cache_set() |
| 117 |
*/ |
| 118 |
function get_the_image( $post_id ) { |
| 119 |
$image = wp_cache_get( 'featured_column_thumbnail', 'post' ); |
| 120 |
|
| 121 |
if ( empty( $image) || !is_string( $image ) ) { |
| 122 |
$image = ''; |
| 123 |
|
| 124 |
if ( has_post_thumbnail( $post_id ) ) { |
| 125 |
$image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), array( 36, 32 ) ); |
| 126 |
|
| 127 |
if ( is_array( $image_array ) && is_string( $image_array[0] ) ) |
| 128 |
$image = $image_array[0]; |
| 129 |
} |
| 130 |
|
| 131 |
if ( empty( $image ) ) { |
| 132 |
$image = plugins_url( 'images/default.png', __FILE__ ); |
| 133 |
$image = apply_filters( 'featured_image_column_default_image', $image ); |
| 134 |
} |
| 135 |
|
| 136 |
$image = esc_url( $image ); |
| 137 |
|
| 138 |
wp_cache_set( 'featured_column_thumbnail', $image, 'post', 60 * 60 * 24 /* 24 hours */ ); |
| 139 |
} |
| 140 |
|
| 141 |
return $image; |
| 142 |
} |
| 143 |
} |
| 144 |
$featured_image_column = new Featured_Image_Column(); |
| 145 |
}; |
| 146 |
|
| 147 |
?> |