-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMslsOutput.php
More file actions
206 lines (173 loc) · 4.81 KB
/
MslsOutput.php
File metadata and controls
206 lines (173 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
<?php
/**
* MslsOutput
* @author Dennis Ploetner <re@lloc.de>
* @since 0.9.8
*/
namespace lloc\Msls;
use lloc\Msls\Map\HrefLang;
/**
* Output in the frontend
* @package Msls
*/
class MslsOutput extends MslsMain {
/**
* Holds the format for the output
* @var array $tags
*/
protected $tags;
/**
* Creates and gets the output as an array
*
* @param int $display
* @param bool $filter
* @param bool $exists
*
* @return array
* @uses MslsLink
* @uses MslsOptions
*/
public function get( $display, $filter = false, $exists = false ) {
$arr = [];
$blogs = $this->collection->get_filtered( $filter );
if ( $blogs ) {
$mydata = MslsOptions::create();
$link = MslsLink::create( $display );
foreach ( $blogs as $blog ) {
$language = $blog->get_language();
$link->src = $this->options->get_flag_url( $language );
$link->alt = $language;
$is_current_blog = $this->collection->is_current_blog( $blog );
if ( $is_current_blog ) {
$url = $mydata->get_current_link();
$link->txt = $blog->get_description();
} else {
switch_to_blog( $blog->userblog_id );
if ( $this->is_requirements_not_fulfilled( $mydata, $exists, $language ) ) {
restore_current_blog();
continue;
} else {
$url = $mydata->get_permalink( $language );
$link->txt = $blog->get_description();
}
restore_current_blog();
}
if ( has_filter( 'msls_output_get' ) ) {
/**
* Returns HTML-link for an item of the output-arr
*
* @param string $url
* @param MslsLink $link
* @param bool $is_current_blog
*
* @since 0.9.8
*
*/
$arr[] = ( string ) apply_filters( 'msls_output_get', $url, $link, $is_current_blog );
} else {
$arr[] = sprintf(
'<a href="%s" title="%s"%s>%s</a>',
$url,
$link->txt,
$is_current_blog ? ' class="current_language"' : '',
$link
);
}
}
}
return $arr;
}
/**
* Get alternate links for the head section
*
* @return string
*/
public function get_alternate_links() {
$blogs = MslsBlogCollection::instance();
$hreflang = new HrefLang( $blogs );
$options = MslsOptions::create();
$arr = [];
$default = '';
$default_locale = apply_filters('mlsl_output_x_default_locale', null);
foreach ( $blogs->get_objects() as $blog ) {
$url = apply_filters( 'mlsl_output_get_alternate_links', $blog->get_url( $options ), $blog );
if ( is_null( $url ) ) {
continue;
}
$description = $blog->get_description();
$format = '<link rel="alternate" hreflang="%s" href="%s" title="%s" />';
if ( '' === $default || $blog->get_language() === $default_locale ) {
$default = sprintf( $format, 'x-default', $url, esc_attr( $description ) );
}
$arr[] = sprintf( $format, $hreflang->get( $blog->get_language() ), $url, esc_attr( $description ) );
}
return 1 === count( $arr ) ? $default : implode( PHP_EOL, $arr ) . (is_null($default_locale) ? '' : $default);
}
/**
* Returns a string when the object will be treated like a string
* @return string
*/
public function __toString() {
$display = (int) $this->options->display;
$filter = false;
$exists = isset( $this->options->only_with_translation );
$arr = $this->get( $display, $filter, $exists );
if ( empty( $arr ) ) {
return apply_filters( 'msls_output_no_translation_found', '' );
}
$tags = $this->get_tags();
return $tags['before_output'] . $tags['before_item'] .
implode( $tags['after_item'] . $tags['before_item'], $arr ) .
$tags['after_item'] . $tags['after_output'];
}
/**
* Gets tags for the output
* @return array
*/
public function get_tags() {
if ( empty( $this->tags ) ) {
$this->tags = [
'before_item' => $this->options->before_item,
'after_item' => $this->options->after_item,
'before_output' => $this->options->before_output,
'after_output' => $this->options->after_output,
];
/**
* Returns tags array for the output
*
* @param array $tags
*
* @since 1.0
*
*/
$this->tags = ( array ) apply_filters( 'msls_output_get_tags', $this->tags );
}
return $this->tags;
}
/**
* Sets tags for the output
*
* @param array $arr
*
* @return MslsOutput
*/
public function set_tags( array $arr = [] ) {
$this->tags = wp_parse_args( $this->get_tags(), $arr );
return $this;
}
/**
* Returns true if the requirements not fulfilled
*
* @param MslsOptions|null $thing
* @param boolean $exists
* @param string $language
*
* @return boolean
*/
public function is_requirements_not_fulfilled( $thing, $exists, $language ) {
if ( is_null( $thing ) ) {
return $exists;
}
return MslsOptions::class != get_class( $thing ) && ! $thing->has_value( $language ) && $exists;
}
}