* * Released under the GPL license * http://www.opensource.org/licenses/gpl-license.php * * This is an add-on for WordPress * http://wordpress.org * * ********************************************************************** * 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 3 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 * ********************************************************************** */ if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. } /** * Initialize. * Init Textdomain & List table columns. * * @return void */ function absp_thumbnail_column_init() { // Load Textdomain. load_plugin_textdomain( 'absp-thumbnail-column', false, basename( dirname( __FILE__ ) ) . '/languages/' ); $supported_types = []; foreach ( get_post_types() as $post_type ) { /** * Filters post thumbnail support status for post type. * * @param bool $supports * @param string $post_type */ $supports = apply_filters( 'absp_post_type_supports_thumbnail', post_type_supports( $post_type, 'thumbnail' ), $post_type ); if ( ! $supports ) { continue; } $supported_types[] = $post_type; // Hook into post-type's list table. add_filter( "manage_{$post_type}_posts_columns", 'add_absp_thumbnail_column', 100 ); add_filter( "manage_{$post_type}_posts_custom_column", 'render_absp_thumbnail_column_content', 10, 2 ); } if ( ! empty( $supported_types ) ) { add_action( 'admin_head', 'absp_thumbnail_column_styles', 99999 ); add_action( 'admin_footer', 'absp_thumbnail_column_scripts', 99999 ); } } /** * Adds the thumbnail column. * * @param array $columns List Table Columns * * @return array */ function add_absp_thumbnail_column( $columns ) { $screen = get_current_screen(); if ( isset( $columns['thumb'] ) || 'product' === $screen->post_type ) { return $columns; } $offset = 2; return array_slice( $columns, 0, $offset, true ) + [ 'absp_thumb_col' => __( 'Thumbnail', 'absp-thumbnail-column' ), ] + array_slice( $columns, $offset, null, true ); } /** * Render post list table column data for thumbnail column. * @param string $column Column name. * @param int $id Post id. */ function render_absp_thumbnail_column_content( $column, $id ) { if ( in_array( $column, [ 'absp_thumb_col', 'thumb' ] ) ) { wp_enqueue_media(); generate_absp_thumbnail( $id ); } } /** * Checks if post type has thumbnail support. * * @return bool */ function absp_current_post_type_supports_thumbnail() { $screen = get_current_screen(); $supports = $screen && ( 'edit' === $screen->base && ! empty( $screen->post_type ) && post_type_supports( $screen->post_type, 'thumbnail') ); /** * Filters post thumbnail support status for current post type. * * @param bool $supports * @param string $post_type */ return apply_filters( 'absp_current_post_type_supports_thumbnail', $supports, $screen ? $screen->post_type : '' ); } /** * Generate the thumbnail html data. * * @param int $id Post id. * @param bool $echo Echo or return the content. * * @return false|string|void */ function generate_absp_thumbnail( $id, $echo = true ) { $thumb_id = get_post_thumbnail_id( $id ); $has_thumb = has_post_thumbnail( $id ); if ( ! $thumb_id ) { $thumb_id = -1; } if ( ! $echo ) { ob_start(); } ?>
get_the_title( $id ) ] ); ?>
', plugins_url( 'assets/images/placeholder.png', plugin_basename( __FILE__ ) ), esc_attr__( 'No Thumb', 'absp-thumbnail-column' ) ); ?>