forked from lloc/Multisite-Language-Switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMslsBlog.php
More file actions
218 lines (185 loc) · 4.73 KB
/
MslsBlog.php
File metadata and controls
218 lines (185 loc) · 4.73 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
207
208
209
210
211
212
213
214
215
216
217
218
<?php declare( strict_types=1 );
namespace lloc\Msls;
/**
* Internal representation of a blog
*
* @property int $userblog_id
* @package Msls
*/
class MslsBlog {
const MSLS_GET_PERMALINK_HOOK = 'msls_blog_get_permalink';
const WP_ADMIN_BAR_SHOW_SITE_ICONS_HOOK = 'wp_admin_bar_show_site_icons';
/**
* WordPress generates such an object
*
* @var \StdClass
*/
private $obj;
/**
* Language-code e.g. "de_DE", or "en_US", or "it_IT"
*
* @var string
*/
private string $language;
/**
* Description e.g. "Deutsch", or "English", or "Italiano"
*
* @var string
*/
private string $description;
/**
* Constructor
*
* @param ?\StdClass $obj
* @param string $description
*/
public function __construct( $obj, $description ) {
if ( is_object( $obj ) ) {
$this->obj = $obj;
$this->language = MslsBlogCollection::get_blog_language( $this->obj->userblog_id );
}
$this->description = (string) $description;
}
/**
* Gets a member of the \StdClass-object by name
*
* The method return <em>null</em> if the requested member does not exists.
*
* @param string $key
*
* @return mixed|null
*/
final public function __get( $key ) {
return $this->obj->$key ?? null;
}
/**
* Gets the description stored in this object
*
* The method returns the stored language if the description is empty.
*
* @return string
*/
public function get_description(): string {
return empty( $this->description ) ? $this->get_language() : $this->description;
}
/**
* Gets a customized title for the blog
*
* @param string $icon_type
*
* @return string
*/
public function get_title( string $icon_type = 'flag' ): string {
$icon = ( new MslsAdminIcon( null ) )->set_language( $this->language )->set_icon_type( $icon_type );
return sprintf(
'%1$s %2$s',
$this->obj->blogname,
'<span class="msls-icon-wrapper flag">' . $icon->get_icon() . '</span>'
);
}
/**
* Gets the language stored in this object
*
* @param string $preset
*
* @return string
*/
public function get_language( $preset = 'en_US' ) {
return empty( $this->language ) ? $preset : $this->language;
}
/**
* Gets the alpha2-part of the language-code
*
* @return string
*/
public function get_alpha2() {
$language = $this->get_language();
return substr( $language, 0, 2 );
}
/**
* @param OptionsInterface $options
*
* @return string|null
*/
public function get_url( $options ) {
if ( msls_blog_collection()->get_current_blog_id() === $this->obj->userblog_id ) {
return $options->get_current_link();
}
return $this->get_permalink( $options );
}
/**
* @param OptionsInterface $options
*
* @return ?string
*/
protected function get_permalink( OptionsInterface $options ) {
$url = null;
$is_front_page = is_front_page();
$is_posts_page = ! $is_front_page && is_home();
switch_to_blog( $this->obj->userblog_id );
if ( $is_front_page || $options->has_value( $this->get_language() ) ) {
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, $options->get_permalink( $this->get_language() ), $this );
} elseif ( $is_posts_page ) {
$page_for_posts = (int) get_option( 'page_for_posts' );
if ( $page_for_posts > 0 ) {
$url = apply_filters( self::MSLS_GET_PERMALINK_HOOK, (string) get_permalink( $page_for_posts ), $this );
}
}
restore_current_blog();
return $url;
}
/**
* Sort objects helper
*
* @param string $a
* @param string $b
*
* @return int
*/
public static function internal_cmp( $a, $b ) {
if ( $a === $b ) {
return 0;
}
return ( $a < $b ? ( - 1 ) : 1 );
}
/**
* Sort objects by language
*
* @param MslsBlog $a
* @param MslsBlog $b
*
* @return int
*/
public static function language( MslsBlog $a, MslsBlog $b ) {
return self::internal_cmp( $a->get_language(), $b->get_language() );
}
/**
* Sort objects by description
*
* @param MslsBlog $a
* @param MslsBlog $b
*
* @return int
*/
public static function description( MslsBlog $a, MslsBlog $b ) {
return self::internal_cmp( $a->get_description(), $b->get_description() );
}
/**
* @return string
*/
public function get_blavatar(): string {
$blavatar_html = '<div class="blavatar"></div>';
$show_site_icons = apply_filters( self::WP_ADMIN_BAR_SHOW_SITE_ICONS_HOOK, true );
switch_to_blog( $this->obj->userblog_id );
if ( true === $show_site_icons && has_site_icon( $this->obj->userblog_id ) ) {
$blavatar_html = sprintf(
'<img class="blavatar" src="%s" srcset="%s 2x" alt="" width="16" height="16"%s />',
esc_url( get_site_icon_url( 16 ) ),
esc_url( get_site_icon_url( 32 ) ),
( wp_lazy_loading_enabled( 'img', 'site_icon_in_toolbar' ) ? ' loading="lazy"' : '' )
);
}
restore_current_blog();
return $blavatar_html;
}
}