MslsPostTagClassic   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 115
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A add_input() 0 13 1
A edit_input() 0 19 1
A print_option() 0 29 4
A the_input() 0 24 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 );
68
69
		$language = $blog->get_language();
70
		$icon     = MslsAdminIcon::create()
71
			->set_language( $language )
72
			->set_icon_type( 'flag' );
73
		$options  = '';
74
		$terms    = get_terms( $type, [ 'hide_empty' => 0 ] );
75
76
		if ( $mydata->has_value( $language ) ) {
77
			$icon->set_href( $mydata->$language );
78
		}
79
80
		if ( ! empty( $terms ) ) {
81
			foreach ( $terms as $term ) {
82
				$options .= sprintf(
83
					'<option value="%s" %s>%s</option>',
84
					$term->term_id,
85
					selected( $term->term_id, $mydata->$language, false ),
86
					$term->name
87
				);
88
			}
89
		}
90
91
		printf( $item_format, $language, $icon, $options );
92
93
		restore_current_blog();
94
	}
95
96
	/**
97
	 * Print the input fields
98
	 * Returns true if the blogcollection is not empty
99
	 * @param \StdClass $tag
100
	 * @param string $title_format
101
	 * @param string $item_format
102
	 * @return boolean
103
	 */
104
	public function the_input( $tag, $title_format, $item_format ) {
105
		$blogs = $this->collection->get();
106
		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...
107
			$term_id = ( is_object( $tag ) ? $tag->term_id : 0 );
108
			$mydata  = MslsOptionsTax::create( $term_id );
109
			$type    = MslsContentTypes::create()->get_request();
110
111
			$this->maybe_set_linked_term( $mydata );
112
113
			printf(
114
				$title_format,
115
				apply_filters(
116
					'msls_term_select_title',
117
					__( 'Multisite Language Switcher', 'multisite-language-switcher' )
118
				)
119
			);
120
121
			foreach ( $blogs as $blog ) {
122
				$this->print_option( $blog, $type, $mydata, $item_format );
123
			}
124
			return true;
125
		}
126
		return false;
127
	}
128
}
129