Files
display-featured-image-genesis/includes/class-displayfeaturedimagegenesis-admin.php
T

169 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
2015-01-01 18:55:13 -05:00
* Dependent class to establish/display columns for featured images
*
2015-04-19 16:27:57 -04:00
* @package DisplayFeaturedImageGenesis
2015-01-01 18:55:13 -05:00
* @author Robin Cornett <hello@robincornett.com>
* @license GPL-2.0+
* @link http://robincornett.com
2015-04-19 16:27:57 -04:00
* @copyright 2015 Robin Cornett Creative, LLC
2015-01-10 10:45:16 -05:00
* @since 2.0.0
*/
2015-01-01 18:55:13 -05:00
class Display_Featured_Image_Genesis_Admin {
2015-07-12 12:28:21 -04:00
protected $common;
2015-06-03 14:36:16 -04:00
2015-01-03 15:41:49 -05:00
public function set_up_columns() {
2015-07-26 16:21:14 -04:00
$this->common = new Display_Featured_Image_Genesis_Common();
2015-01-03 15:41:49 -05:00
$this->set_up_taxonomy_columns();
$this->set_up_post_type_columns();
2015-01-19 20:58:30 -05:00
add_action( 'admin_enqueue_scripts', array( $this, 'featured_image_column_width' ) );
2015-01-03 15:41:49 -05:00
}
2015-01-01 18:55:13 -05:00
/**
* set up new column for all public taxonomies
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-01 18:55:13 -05:00
*/
2015-07-26 16:21:14 -04:00
protected function set_up_taxonomy_columns() {
$args = array(
2015-04-24 17:02:47 -04:00
'public' => true,
);
$output = 'names';
$taxonomies = get_taxonomies( $args, $output );
foreach ( $taxonomies as $taxonomy ) {
add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'add_column' ) );
add_action( "manage_{$taxonomy}_custom_column", array( $this, 'manage_taxonomy_column' ), 10, 3 );
}
}
2015-01-01 18:55:13 -05:00
/**
* set up new column for all public post types
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-01 18:55:13 -05:00
*/
2015-07-26 16:21:14 -04:00
protected function set_up_post_type_columns() {
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'names';
$post_types = get_post_types( $args, $output );
$post_types['post'] = 'post';
$post_types['page'] = 'page';
foreach ( $post_types as $post_type ) {
2015-04-20 13:14:08 -04:00
if ( ! post_type_supports( $post_type, 'thumbnail' ) ) {
2015-07-24 08:13:50 -04:00
continue;
2015-01-01 19:18:24 -05:00
}
2015-04-20 13:14:08 -04:00
add_filter( "manage_edit-{$post_type}_columns", array( $this, 'add_column' ) );
add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'custom_post_columns' ), 10, 2 );
}
}
2015-01-01 18:55:13 -05:00
/**
* add featured image column
* @param column $columns set up new column to show featured image for taxonomies/posts/etc.
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-01 18:55:13 -05:00
*/
public function add_column( $columns ) {
$new_columns = $columns;
array_splice( $new_columns, 1 );
$new_columns['featured_image'] = __( 'Featured Image', 'display-featured-image-genesis' );
return array_merge( $new_columns, $columns );
}
2015-01-01 18:55:13 -05:00
/**
* manage new taxonomy column
* @param blank $value blank (because WP)
* @param column id $column column id is featured_image
* @param term id $term_id term_id for taxonomy
* @return featured image display featured image, if it exists, for each term in a public taxonomy
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-01 18:55:13 -05:00
*/
public function manage_taxonomy_column( $value, $column, $term_id ) {
2015-04-20 12:57:03 -04:00
if ( 'featured_image' !== $column ) {
return;
}
$term_meta = get_option( "displayfeaturedimagegenesis_$term_id" );
if ( empty( $term_meta['term_image'] ) ) {
return;
}
$taxonomy = filter_input( INPUT_POST, 'taxonomy', FILTER_SANITIZE_STRING );
$taxonomy = ! is_null( $taxonomy ) ? $taxonomy : get_current_screen()->taxonomy;
$alt = get_term( $term_id, $taxonomy )->name;
2015-06-06 21:30:42 -04:00
$id = is_numeric( $term_meta['term_image'] ) ? $term_meta['term_image'] : Display_Featured_Image_Genesis_Common::get_image_id( $term_meta['term_image'] );
2015-04-20 12:57:03 -04:00
$preview = apply_filters(
'display_featured_image_genesis_admin_term_thumbnail',
wp_get_attachment_image_src( $id, 'thumbnail' ),
$id
);
2015-05-07 20:36:35 -04:00
printf( '<img src="%1$s" alt="%2$s" />',
esc_url( $preview[0] ),
esc_attr( $alt )
);
2015-04-20 12:57:03 -04:00
}
2015-01-01 18:55:13 -05:00
/**
* manage new post_type column
* @param column id $column column id is featured_image
* @param post id $post_id id of each post
* @return featured image display featured image, if it exists, for each post
*
2015-01-10 10:45:16 -05:00
* @since 2.0.0
2015-01-01 18:55:13 -05:00
*/
public function custom_post_columns( $column, $post_id ) {
2015-04-20 13:14:08 -04:00
if ( 'featured_image' !== $column ) {
return;
}
$id = get_post_thumbnail_id( $post_id );
if ( ! $id ) {
return;
}
2015-04-20 13:14:08 -04:00
$preview = apply_filters(
'display_featured_image_genesis_admin_post_thumbnail',
wp_get_attachment_image_src( $id, 'thumbnail' ),
$id
);
2015-05-07 20:36:35 -04:00
printf( '<img src="%1$s" alt="%2$s" />',
esc_url( $preview[0] ),
esc_attr( the_title_attribute( 'echo=0' ) )
);
2015-04-20 13:14:08 -04:00
}
2015-01-19 20:58:30 -05:00
/**
* sets a width for the featured image column
* @return stylesheet inline stylesheet to set featured image column width
*/
public function featured_image_column_width() {
$screen = get_current_screen();
if ( in_array( $screen->base, array( 'edit', 'edit-tags' ) ) ) { ?>
<style type="text/css">
2015-03-02 11:36:01 -05:00
.column-featured_image { width: 105px; }
.column-featured_image img { margin: 0 auto; display: block; height: auto; width: auto; max-width: 60px; max-height: 80px; }
2015-08-06 20:56:56 -04:00
@media screen and (max-width: 782px) { .column-featured_image img { margin: 0; } }
2015-01-19 20:58:30 -05:00
</style> <?php
}
}
2015-01-01 18:55:13 -05:00
}