Simple_Featured_Image_Column::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * Plugin Name: Simple Featured Image Column
4
 * Plugin URI: https://github.com/dedevillela/Simple-Featured-Image-Column/
5
 * Description: A simple plugin that displays the "Featured Image" column in admin post type listing. Supports Post, Pages and Custom Posts.
6
 * Version: 1.0.8
7
 * Author: Andre Aguiar Villela
8
 * Author URI: https://dedevillela.com/
9
 * License: GPLv2+
10
 **/
11
12
if (!defined('ABSPATH') || preg_match('#'.basename(__FILE__).'#', filter_input(INPUT_SERVER, 'PHP_SELF'))) {
13
	die("Hey, dude! What are you doing here?");
14
}
15
16
if (!class_exists('Simple_Featured_Image_Column')) {
17
18
	class Simple_Featured_Image_Column {
19
20
		public function __construct() {
21
			/** @scrutinizer ignore-call */
22
			add_action('admin_init', array($this, 'init'));
23
		}
24
25
		private function init() {
26
27
			$post_types = /** @scrutinizer ignore-call */ apply_filters('Simple_Featured_Image_Column_post_types', /** @scrutinizer ignore-call */ get_post_types(array('public' => true)));
28
			if (empty($post_types)) {
29
				return;
30
			}
31
			/** @scrutinizer ignore-call */
32
			add_action('admin_head', function() {
33
				return $this->/** @scrutinizer ignore-call */ getResponse()->setBody('<style>th#featured-image  { width: 100px; }</style>'."\r\n"); 
34
			});
35
			
36
			foreach ($post_types as $post_type) {
37
				if (!/** @scrutinizer ignore-call */ post_type_supports($post_type, 'thumbnail')) {
38
					continue;
39
				}
40
				/** @scrutinizer ignore-call */
41
				add_filter("manage_{$post_type}_posts_columns", array($this, 'columns'));
42
				/** @scrutinizer ignore-call */
43
				add_action("manage_{$post_type}_posts_custom_column", array($this, 'column_data'), 10, 2);
44
			}
45
		}
46
47
		private function columns($columns) {
48
			
49
			if (!is_array($columns)) {
50
				$columns = array();
51
			}
52
			$new = array();
53
			foreach ($columns as $key => $title) {
54
				if ($key == 'title') {
55
					$new['featured-image'] = /** @scrutinizer ignore-call */ __('Image', 'wordpress');
56
				}
57
				$new[$key] = $title;
58
			}
59
			return $new;
60
		}
61
62
		private function column_data($column_name, $post_id) {
63
			
64
			if ('featured-image' != $column_name) {
65
				return;
66
			}
67
			$style = 'display: block; max-width: 100px; height: auto; border: 1px solid #e5e5e5;';
68
			$style = /** @scrutinizer ignore-call */ apply_filters('Simple_Featured_Image_Column_image_style', $style);
69
70
			if (/** @scrutinizer ignore-call */ has_post_thumbnail($post_id)) {
71
				$size = 'thumbnail';
72
				return $this->/** @scrutinizer ignore-call */ getResponse()->setBody(/** @scrutinizer ignore-call */ get_the_post_thumbnail($post_id, $size, 'style='.$style));
73
			} else {
74
				return $this->/** @scrutinizer ignore-call */ getResponse()->setBody('<img style="'.$style.'" src="'./** @scrutinizer ignore-call */ esc_url(/** @scrutinizer ignore-call */ plugins_url('images/default.png', __FILE__)).'" />');
75
			}	
76
		}
77
	}
78
	$featured_image_column = new Simple_Featured_Image_Column;	  
79
};
80