Passed
Branch master (a6d779)
by ANDRE
04:02 queued 01:46
created

Simple_Featured_Image_Column   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A columns() 0 13 4
A column_data() 0 13 3
A init() 0 17 4
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.7
7
 * Author: Andre Aguiar Villela
8
 * Author URI: https://dedevillela.com/
9
 * License: GPLv2+
10
 **/
11
12
  if (!defined( 'ABSPATH' ) || preg_match('#'.basename( __FILE__ ).'#', $_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
		function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
21
			add_action('admin_init', array($this, 'init'));
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
			/** @scrutinizer ignore-call */ 
22
   add_action('admin_init', array($this, 'init'));
Loading history...
22
		}
23
24
		function init() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
26
			$post_types = apply_filters('Simple_Featured_Image_Column_post_types', get_post_types(array('public' => true)));
0 ignored issues
show
Bug introduced by
The function get_post_types was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
			$post_types = apply_filters('Simple_Featured_Image_Column_post_types', /** @scrutinizer ignore-call */ get_post_types(array('public' => true)));
Loading history...
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
			$post_types = /** @scrutinizer ignore-call */ apply_filters('Simple_Featured_Image_Column_post_types', get_post_types(array('public' => true)));
Loading history...
27
			if (empty($post_types)) {
28
				return;
29
			}
30
31
			add_action('admin_head', function() { 
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
			/** @scrutinizer ignore-call */ 
32
   add_action('admin_head', function() { 
Loading history...
32
				echo '<style>th#featured-image  { width: 100px; }</style>'."\r\n"; 
33
			});
34
			
35
			foreach ($post_types as $post_type) {
36
				if (!post_type_supports($post_type, 'thumbnail')) {
0 ignored issues
show
Bug introduced by
The function post_type_supports was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
				if (!/** @scrutinizer ignore-call */ post_type_supports($post_type, 'thumbnail')) {
Loading history...
37
					continue;
38
				}
39
				add_filter("manage_{$post_type}_posts_columns", array($this, 'columns'));
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
				/** @scrutinizer ignore-call */ 
40
    add_filter("manage_{$post_type}_posts_columns", array($this, 'columns'));
Loading history...
40
				add_action("manage_{$post_type}_posts_custom_column", array($this, 'column_data'), 10, 2);
41
			}
42
		}
43
44
		function columns($columns) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
45
			
46
			if (!is_array($columns)) {
47
				$columns = array();
48
			}
49
			$new = array();
50
			foreach($columns as $key => $title){
51
				if ($key == 'title') {
52
					$new['featured-image'] = __('Image', 'wordpress');
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
					$new['featured-image'] = /** @scrutinizer ignore-call */ __('Image', 'wordpress');
Loading history...
53
				}
54
				$new[$key] = $title;
55
			}
56
			return $new;
57
		}
58
59
		function column_data($column_name, $post_id) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
60
			
61
			if ('featured-image' != $column_name) {
62
				return;
63
			}
64
			$style = 'display: block; max-width: 100px; height: auto; border: 1px solid #e5e5e5;';
65
			$style = apply_filters('Simple_Featured_Image_Column_image_style', $style);
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
			$style = /** @scrutinizer ignore-call */ apply_filters('Simple_Featured_Image_Column_image_style', $style);
Loading history...
66
67
			if (has_post_thumbnail($post_id)) {
0 ignored issues
show
Bug introduced by
The function has_post_thumbnail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

67
			if (/** @scrutinizer ignore-call */ has_post_thumbnail($post_id)) {
Loading history...
68
				$size = 'thumbnail';
69
				echo get_the_post_thumbnail($post_id, $size, 'style='.$style);
0 ignored issues
show
Bug introduced by
The function get_the_post_thumbnail was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
				echo /** @scrutinizer ignore-call */ get_the_post_thumbnail($post_id, $size, 'style='.$style);
Loading history...
70
			} else {
71
				echo '<img style="'. $style .'" src="'. esc_url(plugins_url('images/default.png', __FILE__)) .'" />';
0 ignored issues
show
Bug introduced by
The function plugins_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
				echo '<img style="'. $style .'" src="'. esc_url(/** @scrutinizer ignore-call */ plugins_url('images/default.png', __FILE__)) .'" />';
Loading history...
Bug introduced by
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
				echo '<img style="'. $style .'" src="'. /** @scrutinizer ignore-call */ esc_url(plugins_url('images/default.png', __FILE__)) .'" />';
Loading history...
72
			}	
73
		}
74
	}
75
	$featured_image_column = new Simple_Featured_Image_Column;
76
};
77