Completed
Push — master ( 196f44...96e9ab )
by Dennis
01:21
created

MslsOutput::get_alternate_links()   A

Complexity

Conditions 5
Paths 8

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 8
nop 0
dl 0
loc 25
rs 9.2088
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
	 * @return array
30
	 * @uses MslsLink
31
	 * @uses MslsOptions
32
	 */
33
	public function get( $display, $filter = false, $exists = false ) {
34
		$arr = [];
35
36
		$blogs = $this->collection->get_filtered( $filter );
37
		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...
38
			$mydata = MslsOptions::create();
39
			$link   = MslsLink::create( $display );
40
41
			foreach ( $blogs as $blog ) {
42
				$language = $blog->get_language();
43
44
				$link->src = $this->options->get_flag_url( $language );
45
				$link->alt = $language;
46
47
				$is_current_blog = $this->collection->is_current_blog( $blog );
48
				if ( $is_current_blog ) {
49
					$url       = $mydata->get_current_link();
50
					$link->txt = $blog->get_description();
51
				} else {
52
					switch_to_blog( $blog->userblog_id );
53
54
					if ( $this->is_requirements_not_fulfilled( $mydata, $exists, $language ) ) {
55
						restore_current_blog();
56
						continue;
57
					} else {
58
						$url       = $mydata->get_permalink( $language );
59
						$link->txt = $blog->get_description();
60
					}
61
62
					restore_current_blog();
63
				}
64
65
				if ( has_filter( 'msls_output_get' ) ) {
66
					/**
67
					 * Returns HTML-link for an item of the output-arr
68
					 *
69
					 * @param string $url
70
					 * @param MslsLink $link
71
					 * @param bool $is_current_blog
72
					 *
73
					 * @since 0.9.8
74
					 *
75
					 */
76
					$arr[] = ( string ) apply_filters( 'msls_output_get', $url, $link, $is_current_blog );
77
				} else {
78
					$arr[] = sprintf(
79
						'<a href="%s" title="%s"%s>%s</a>',
80
						$url,
81
						$link->txt,
82
						$is_current_blog ? ' class="current_language"' : '',
83
						$link
84
					);
85
				}
86
			}
87
		}
88
89
		return $arr;
90
	}
91
92
	/**
93
	 * Get alternate links for the head section
94
	 *
95
	 * @return string
96
	 */
97
	public function get_alternate_links() {
98
		$blogs   = MslsBlogCollection::instance();
99
		$options = MslsOptions::create();
100
		$arr     = [];
101
		$default = '';
102
103
		foreach ( $blogs->get_objects() as $blog ) {
104
			$url = $blog->get_url( $options );
0 ignored issues
show
Bug introduced by
It seems like $options defined by \lloc\Msls\MslsOptions::create() on line 99 can be null; however, lloc\Msls\MslsBlog::get_url() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
105
			if ( is_null( $url ) ) {
106
				continue;
107
			}
108
109
			$description = $blog->get_description();
110
			$hreflang    = $blog->get_hreflang();
111
112
			$format = '<link rel="alternate" hreflang="%s" href="%s" title="%s" />';
113
			if ( '' === $default ) {
114
				$default = sprintf( $format, 'x-default', $url, esc_attr( $description ) );
115
			}
116
117
			$arr[] = sprintf( $format, $hreflang, $url, esc_attr( $description ) );
118
		}
119
120
		return 1 === count( $arr ) ? $default : implode( PHP_EOL, $arr );
121
	}
122
123
	/**
124
	 * Returns a string when the object will be treated like a string
125
	 * @return string
126
	 */
127
	public function __toString() {
128
		$display = (int) $this->options->display;
129
		$filter  = false;
130
		$exists  = isset( $this->options->only_with_translation );
131
132
		$arr = $this->get( $display, $filter, $exists );
133
		if ( empty( $arr ) ) {
134
			return '';
135
		}
136
137
		$tags = $this->get_tags();
138
139
		return $tags['before_output'] . $tags['before_item'] .
140
		       implode( $tags['after_item'] . $tags['before_item'], $arr ) .
141
		       $tags['after_item'] . $tags['after_output'];
142
	}
143
144
	/**
145
	 * Gets tags for the output
146
	 * @return array
147
	 */
148
	public function get_tags() {
149
		if ( empty( $this->tags ) ) {
150
			$this->tags = [
151
				'before_item'   => $this->options->before_item,
152
				'after_item'    => $this->options->after_item,
153
				'before_output' => $this->options->before_output,
154
				'after_output'  => $this->options->after_output,
155
			];
156
157
			/**
158
			 * Returns tags array for the output
159
			 *
160
			 * @param array $tags
161
			 *
162
			 * @since 1.0
163
			 *
164
			 */
165
			$this->tags = ( array ) apply_filters( 'msls_output_get_tags', $this->tags );
166
		}
167
168
		return $this->tags;
169
	}
170
171
	/**
172
	 * Sets tags for the output
173
	 *
174
	 * @param array $arr
175
	 *
176
	 * @return MslsOutput
177
	 */
178
	public function set_tags( array $arr = [] ) {
179
		$this->tags = wp_parse_args( $this->get_tags(), $arr );
180
181
		return $this;
182
	}
183
184
	/**
185
	 * Returns true if the requirements not fulfilled
186
	 *
187
	 * @param MslsOptions|null $thing
188
	 * @param boolean $exists
189
	 * @param string $language
190
	 *
191
	 * @return boolean
192
	 */
193
	public function is_requirements_not_fulfilled( $thing, $exists, $language ) {
194
		if ( is_null( $thing ) ) {
195
			return $exists;
196
		}
197
198
		return MslsOptions::class != get_class( $thing ) && ! $thing->has_value( $language ) && $exists;
199
	}
200
201
}
202