Advertisement
Guest User

Untitled

a guest
Mar 11th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.46 KB | None | 0 0
  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.2.0
  7.  * Author: Austin Passy
  8.  * Author URI: http://austinpassy.com
  9.  *
  10.  * @copyright 2009 - 2012
  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.2.0';
  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.             global $pagenow;
  35.             $is_on_edit_screen = is_admin() && $pagenow === 'edit.php';
  36.             $is_doing_ajax = $pagenow === "admin-ajax.php";
  37.            
  38.             if($is_on_edit_screen || $is_doing_ajax) :
  39.                 add_action('init', array( $this, 'init' ) );
  40.         endif;
  41.    
  42.         } // end constructor
  43.    
  44.         /**
  45.          * Sets up the Featured_Image_Column plugin and loads files at the appropriate time.
  46.          *
  47.          * @since 0.1.6
  48.          */
  49.         function init() {
  50.             if(isset($_GET['post_type'])) {
  51.                 $post_type = $_GET['post_type'];
  52.             } else {
  53.                 $post_type = $_POST['post_type'];
  54.             }
  55.            
  56.             if ( !post_type_supports( $post_type, 'thumbnail' ) )
  57.                 return;        
  58.            
  59.             /* Print style */
  60.             add_action( 'admin_enqueue_scripts',                    array( $this, 'style' ), 0 );
  61.            
  62.             /* Column manager */
  63.             add_filter( "manage_{$post_type}_posts_columns",        array( $this, 'columns' ) );
  64.             add_action( "manage_{$post_type}_posts_custom_column",  array( $this, 'column_data' ), 10, 2 );        
  65.            
  66.             do_action( 'featured_image_column_loaded' );
  67.         }
  68.        
  69.         /**
  70.          * Enqueue stylesheaet
  71.          * @since 0.1
  72.          */
  73.         function style() {
  74.             wp_register_style( 'featured-image-column', apply_filters( 'featured_image_column_css', plugin_dir_url( __FILE__ ) . 'css/column.css' ), null, self::version );
  75.             wp_enqueue_style( 'featured-image-column' );
  76.         }
  77.        
  78.         /**
  79.          * Filter the image in before the 'title'
  80.          */
  81.         function columns( $columns ) {
  82.             if ( !is_array( $columns ) )
  83.                 $columns = array();
  84.            
  85.             $new = array();
  86.            
  87.             foreach( $columns as $key => $title ) {
  88.                 if ( $key == 'title' ) // Put the Thumbnail column before the Title column
  89.                     $new['featured-image'] = __( 'Image', self::domain );
  90.                
  91.                 $new[$key] = $title;
  92.             }
  93.            
  94.             return $new;
  95.         }
  96.        
  97.         /**
  98.          * Output the image
  99.          */
  100.         function column_data( $column_name, $post_id ) {
  101.             global $post;
  102.            
  103.             if ( 'featured-image' != $column_name )
  104.                 return;        
  105.            
  106.             $image_src = self::get_the_image( $post->ID );
  107.            
  108.             if ( empty( $image_src ) ) {
  109.                 echo "&nbsp;"; // This helps prevent issues with empty cells
  110.                 return;
  111.             }
  112.            
  113.             echo '<img alt="' . esc_attr( get_the_title() ) . '" src="' . esc_url( $image_src ) . '" />';
  114.         }
  115.        
  116.         /**
  117.          * Function to get the image
  118.          *
  119.          * @since   0.1
  120.          * @updated 0.1.3 - Added wp_cache_set()
  121.          * @updated 0.1.9 - fixed persistent cache per post_id
  122.          *      @ref    http://www.ethitter.com/slides/wcmia-caching-scaling-2012-02-18/#slide-11
  123.          */
  124.         function get_the_image( $post_id ) {           
  125.             $post_id    = (int)$post_id;
  126.             $cache_key  = 'featured_column_thumbnail';
  127.             $image      = wp_cache_get( $cache_key, null );
  128.            
  129.             if ( !is_array( $image ) )
  130.                 $image = array();
  131.        
  132.             if ( !array_key_exists( $cache_key, $image ) ) {
  133.                 if ( empty( $image) || !is_string( $image ) ) {
  134.                     $image = '';
  135.                        
  136.                     if ( has_post_thumbnail( $post_id ) ) {
  137.                         $image_array = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), array( 36, 32 ) );
  138.                        
  139.                         if ( is_array( $image_array ) && is_string( $image_array[0] ) )
  140.                             $image = $image_array[0];
  141.                     }
  142.                    
  143.                     if ( empty( $image ) ) {
  144.                         $image = plugins_url( 'images/default.png', __FILE__ );
  145.                         $image = apply_filters( 'featured_image_column_default_image', $image );
  146.                     }
  147.                    
  148.                     $image = esc_url( $image );
  149.                     $image[$cache_key] = $image;
  150.                    
  151.                     wp_cache_set( $cache_key, $image, null, 60 * 60 * 24 /* 24 hours */ );
  152.                 }
  153.             }
  154.            
  155.             return $image;
  156.         }
  157.     }  
  158.     $featured_image_column = new Featured_Image_Column();
  159. };
  160.  
  161. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement