Completed
Push — add/wpcom-posts-package ( 70d0c5 )
by
unknown
258:54 queued 248:44
created

Thumbnail::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Thumbnail enhancements for the "Posts" screens on WordPress.com sites.
4
 *
5
 * @package automattic/jetpack-wpcom-posts
6
 */
7
8
namespace Automattic\Jetpack\WPcom\Posts;
9
10
/**
11
 * Class Thumbnail.
12
 */
13
class Thumbnail {
14
	/**
15
	 * Thumbnail constructor.
16
	 */
17
	public function __construct() {
18
		add_filter( 'manage_posts_columns', array( $this, 'add_posts_column_header' ) );
19
		add_filter( 'manage_pages_columns', array( $this, 'add_posts_column_header' ) );
20
		add_action( 'manage_posts_custom_column', array( $this, 'display_posts_column_content' ), 10, 2 );
21
		add_action( 'manage_pages_custom_column', array( $this, 'display_posts_column_content' ), 10, 2 );
22
		add_action( 'admin_print_styles-edit.php', array( $this, 'load_columns_css' ) );
23
	}
24
25
	/**
26
	 * Adds a new column header for displaying the thumbnail of a post.
27
	 *
28
	 * @param array $columns An array of column names.
29
	 * @return array An array of column names.
30
	 */
31
	public function add_posts_column_header( $columns ) {
32
		// Place if before author.
33
		$pos = array_search( 'author', array_keys( $columns ), true );
34
		if ( ! is_int( $pos ) ) {
35
			return $columns;
36
		}
37
		$chunks                 = array_chunk( $columns, $pos, true );
38
		$chunks[0]['thumbnail'] = ''; // Deliberately empty.
39
40
		return call_user_func_array( 'array_merge', $chunks );
41
	}
42
43
	/**
44
	 * Displays the thumbnail content.
45
	 *
46
	 * @param string $column  The name of the column to display.
47
	 * @param int    $post_id The current post ID.
48
	 */
49
	public function display_posts_column_content( $column, $post_id ) {
50
		if ( 'thumbnail' !== $column ) {
51
			return;
52
		}
53
54
		echo get_the_post_thumbnail( $post_id, array( 50, 50 ), array( 'style' => 'height: auto;' ) );
55
	}
56
57
	/**
58
	 * Load CSS needed for the thumbnail column.
59
	 */
60
	public function load_columns_css() {
61
		?>
62
		<style type="text/css">
63
			.fixed .column-thumbnail {
64
				width: 5em;
65
			}
66
		</style>
67
		<?php
68
	}
69
}
70