Completed
Pull Request — master (#113)
by
unknown
01:53
created

MslsOutput::is_requirements_not_fulfilled()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 2
nc 5
nop 3
dl 0
loc 3
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsOutput
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Output in the frontend
12
 * @package Msls
13
 */
14
class MslsOutput extends MslsMain {
15
16
	/**
17
	 * Holds the format for the output
18
	 * @var array $tags
19
	 */
20
	protected $tags;
21
22
	/**
23
	 * Creates and gets the output as an array
24
	 *
25
	 * @param int $display
26
	 * @param bool $filter
27
	 * @param bool $exists
28
	 *
29
	 * @uses MslsOptions
30
	 * @uses MslsLink
31
	 * @return array
32
	 */
33
	public function get( $display, $filter = false, $exists = false ) {
34
		$arr = array();
35
36
		$blogs = $this->collection->get_filtered( $filter );
37
		if ( $blogs ) {
38
			$mydata = MslsOptions::create();
39
			$link   = MslsLink::create( $display );
40
41
			foreach ( $blogs as $blog ) {
42
				$language = $blog->get_language();
43
				$url      = $mydata->get_current_link();
44
				$current  = ( $blog->userblog_id == $this->collection->get_current_blog_id() );
45
46
				if ( $current ) {
47
					$link->txt = $blog->get_description();
48
				} else {
49
					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...
50
51
					if ( $this->is_requirements_not_fulfilled( $mydata, $exists, $language ) ) {
52
						restore_current_blog();
53
						continue;
54
					} else {
55
						$url       = $mydata->get_permalink( $language );
56
						$link->txt = $blog->get_description();
57
					}
58
59
					restore_current_blog();
60
				}
61
62
				$link->src = $this->options->get_flag_url( $language );
63
				$link->alt = $language;
64
65
				if ( has_filter( 'msls_output_get' ) ) {
66
					/**
67
					 * Returns HTML-link for an item of the output-arr
68
					 * @since 0.9.8
69
					 *
70
					 * @param string $url
71
					 * @param MslsLink $link
72
					 * @param bool current
73
					 */
74
					$arr[] = ( string ) apply_filters( 'msls_output_get', $url, $link, $current );
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(string)" but found "( string )"
Loading history...
75
				}
76
				else {
77
					$arr[] = sprintf(
78
						'<a href="%s" title="%s"%s>%s</a>',
79
						$url,
80
						$link->txt,
81
						( $current ? ' class="current_language"' : '' ),
82
						$link
83
					);
84
				}
85
			}
86
		}
87
88
		return $arr;
89
	}
90
91
	/**
92
	 * Returns a string when the object will be treated like a string
93
	 * @return string
94
	 */
95
	public function __toString() {
96
		$display = (int) $this->options->display;
97
		$filter  = false;
98
		$exists  = isset( $this->options->only_with_translation );
99
100
		$arr = $this->get( $display, $filter, $exists );
101
		if ( empty( $arr ) ) {
102
			return '';
103
		}
104
105
		$tags = $this->get_tags();
106
107
		return $tags['before_output'] . $tags['before_item'] .
108
		       implode( $tags['after_item'] . $tags['before_item'], $arr ) .
109
		       $tags['after_item'] . $tags['after_output'];
110
	}
111
112
	/**
113
	 * Gets tags for the output
114
	 * @return array
115
	 */
116
	public function get_tags() {
117
		if ( empty( $this->tags ) ) {
118
			$this->tags = array(
119
				'before_item'   => $this->options->before_item,
120
				'after_item'    => $this->options->after_item,
121
				'before_output' => $this->options->before_output,
122
				'after_output'  => $this->options->after_output,
123
			);
124
125
			/**
126
			 * Returns tags array for the output
127
			 * @since 1.0
128
			 *
129
			 * @param array $tags
130
			 */
131
			$this->tags = ( array ) apply_filters( 'msls_output_get_tags', $this->tags );
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(array)" but found "( array )"
Loading history...
132
		}
133
134
		return $this->tags;
135
	}
136
137
	/**
138
	 * Sets tags for the output
139
	 *
140
	 * @param array $arr
141
	 *
142
	 * @return MslsOutput
143
	 */
144
	public function set_tags( array $arr = [] ) {
145
		$this->tags = wp_parse_args( $this->get_tags(), $arr );
146
147
		return $this;
148
	}
149
150
	/**
151
	 * Returns true if the requirements not fulfilled
152
	 *
153
	 * @param MslsOptions|null $mydata
154
	 * @param boolean $exists
155
	 * @param string $language
156
	 *
157
	 * @return boolean
158
	 */
159
	public function is_requirements_not_fulfilled( $mydata, $exists, $language ) {
160
		return $exists && ( is_null( $mydata ) || ( ! $mydata->has_value( $language ) && MslsOptions::class != get_class( $mydata ) );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';'
Loading history...
161
	}
162
}
163