Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsCustomColumn::td()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 21
nc 5
nop 2
dl 0
loc 31
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsCustomColumn
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Handling of existing/not existing translations in the backend listings of
12
 * various post types
13
 * @package Msls
14
 */
15
class MslsCustomColumn extends MslsMain {
16
17
	/**
18
	 * Factory
19
	 *
20
	 * @codeCoverageIgnore
21
	 *
22
	 * @return MslsCustomColumn
23
	 */
24
	public static function init() {
25
		$options    = MslsOptions::instance();
26
		$collection = MslsBlogCollection::instance();
27
		$obj        = new static( $options, $collection );
28
29
		if ( ! $options->is_excluded() ) {
30
			$post_type = MslsPostType::instance()->get_request();
31
32
			if ( ! empty( $post_type ) ) {
33
				add_filter( "manage_{$post_type}_posts_columns", [ $obj, 'th' ] );
34
				add_action( "manage_{$post_type}_posts_custom_column", [ $obj, 'td' ], 10, 2 );
35
				add_action( 'trashed_post', [ $obj, 'delete' ] );
36
			}
37
		}
38
39
		return $obj;
40
	}
41
42
	/**
43
	 * Table header
44
	 * @param array $columns
45
	 * @return array
46
	 */
47
	public function th( $columns ) {
48
		$blogs = $this->collection->get();
49
		if ( $blogs ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $blogs of type lloc\Msls\MslsBlog[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
			$arr = array();
51
			foreach ( $blogs as $blog ) {
52
				$language = $blog->get_language();
53
				$flag_url = $this->options->get_flag_url( $language );
54
55
				$icon = new MslsAdminIcon( null );
56
				$icon->set_language( $language )->set_src( $flag_url );
57
58
				if ( $post_id = get_the_ID() ) {
59
					$icon->set_id( $post_id );
60
					$icon->set_origin_language( 'it_IT' );
61
				}
62
63
				$arr[] = $icon->get_img();
64
			}
65
			$columns['mslscol'] = implode( '&nbsp;', $arr );
66
		}
67
68
		return $columns;
69
	}
70
71
	/**
72
	 * Table body
73
	 *
74
	 * @param string $column_name
75
	 * @param int $item_id
76
	 *
77
	 * @codeCoverageIgnore
78
	 */
79
	public function td( $column_name, $item_id ) {
80
		if ( 'mslscol' == $column_name ) {
81
			$blogs           = $this->collection->get();
82
			$origin_language = MslsBlogCollection::get_blog_language();
83
			if ( $blogs ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $blogs of type lloc\Msls\MslsBlog[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
84
				$mydata = MslsOptions::create( $item_id );
85
				foreach ( $blogs as $blog ) {
86
					switch_to_blog( $blog->userblog_id );
0 ignored issues
show
introduced by
switch_to_blog is not something you should ever need to do in a VIP theme context. Instead use an API (XML-RPC, REST) to interact with other sites if needed.
Loading history...
87
88
					$language = $blog->get_language();
89
90
					$icon = MslsAdminIcon::create();
91
					$icon->set_language( $language );
92
					$icon->set_id( $item_id );
93
					$icon->set_origin_language( $origin_language );
94
95
					if ( $mydata->has_value( $language ) ) {
96
						$flag_url = $this->options->get_url( 'images/link_edit.png' );
97
						$icon->set_href( $mydata->$language )->set_src( $flag_url );
98
					}
99
					else {
100
						$flag_url = $this->options->get_url( 'images/link_add.png' );
101
						$icon->set_src( $flag_url );
102
					}
103
					echo $icon;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$icon'
Loading history...
104
105
					restore_current_blog();
106
				}
107
			}
108
		}
109
	}
110
111
}
112