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

MslsPostTagClassic   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 111
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add_input() 0 13 1
A edit_input() 0 19 1
B print_option() 0 28 4
A the_input() 0 21 4
1
<?php
2
/**
3
 * MslsPostTagClassic
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.9
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Post Tag Clasic
12
 * @package Msls
13
 */
14
class MslsPostTagClassic extends MslsPostTag {
15
16
	/**
17
	 * Add the input fields to the add-screen of the taxonomies
18
	 * @param \StdClass $tag
19
	 */
20
	public function add_input( $tag ) {
21
		$title_format = '<h3>%s</h3>';
22
23
		$item_format = '<label for="msls_input_%1$s">%2$s</label>
24
			<select class="msls-translations" name="msls_input_%1$s">
25
			<option value=""></option>
26
			%3$s
27
			</select>';
28
29
		echo '<div class="form-field">';
30
		$this->the_input( $tag, $title_format, $item_format );
31
		echo '</div>';
32
	}
33
34
	/**
35
	 * Add the input fields to the edit-screen of the taxonomies
36
	 * @param \StdClass $tag
37
	 */
38
	public function edit_input( $tag ) {
39
		$title_format = '<tr>
40
			<th colspan="2">
41
			<strong>%s</strong>
42
			</th>
43
			</tr>';
44
45
		$item_format = '<tr class="form-field">
46
			<th scope="row" valign="top">
47
			<label for="msls_input_%1$s">%2$s</label></th>
48
			<td>
49
			<select class="msls-translations" name="msls_input_%1$s">
50
			<option value=""></option>
51
			%3$s
52
			</select></td>
53
			</tr>';
54
55
		$this->the_input( $tag, $title_format, $item_format );
56
	}
57
58
	/**
59
	 * Prints options inputs
60
	 * @uses selected
61
	 * @param MslsBlog $blog
62
	 * @param string $type
63
	 * @param MslsOptionsTax $mydata
64
	 * @param string $item_format
65
	 */
66
	public function print_option( MslsBlog $blog, $type, MslsOptionsTax $mydata, $item_format ) {
67
		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...
68
69
		$language = $blog->get_language();
70
		$flag_url = $this->options->get_flag_url( $language );
71
		$icon     = MslsAdminIcon::create()->set_language( $language )->set_src( $flag_url );
72
		$options  = '';
73
		$terms    = get_terms( $type, [ 'hide_empty' => 0 ] );
74
75
		if ( $mydata->has_value( $language ) ) {
76
			$icon->set_href( $mydata->$language );
77
		}
78
79
		if ( ! empty( $terms ) ) {
80
			foreach ( $terms as $term ) {
81
				$options .= sprintf(
82
					'<option value="%s" %s>%s</option>',
83
					$term->term_id,
84
					selected( $term->term_id, $mydata->$language, false ),
85
					$term->name
86
				);
87
			}
88
		}
89
90
		printf( $item_format, $language, $icon, $options );
91
92
		restore_current_blog();
93
	}
94
95
	/**
96
	 * Print the input fields
97
	 * Returns true if the blogcollection is not empty
98
	 * @param \StdClass $tag
99
	 * @param string $title_format
100
	 * @param string $item_format
101
	 * @return boolean
102
	 */
103
	public function the_input( $tag, $title_format, $item_format ) {
104
		$blogs = $this->collection->get();
105
		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...
106
			$term_id = ( is_object( $tag ) ? $tag->term_id : 0 );
107
			$mydata  = MslsOptionsTax::create( $term_id );
108
			$type    = MslsContentTypes::create()->get_request();
109
110
			$this->maybe_set_linked_term( $mydata );
111
112
			printf(
113
				$title_format,
114
				__( 'Multisite Language Switcher', 'multisite-language-switcher' )
115
			);
116
117
			foreach ( $blogs as $blog ) {
118
				$this->print_option( $blog, $type, $mydata, $item_format );
119
			}
120
			return true;
121
		}
122
		return false;
123
	}
124
}
125