Completed
Push — master ( d47b2f...717aca )
by Dennis
01:22
created

MslsBlog::get_title()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsBlog
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Internal representation of a blog
12
 * @property int $userblog_id
13
 * @package Msls
14
 */
15
class MslsBlog {
16
17
	/**
18
	 * WordPress generates such an object
19
	 * @var \StdClass
20
	 */
21
	private $obj;
22
23
	/**
24
	 * Language-code eg. de_DE
25
	 * @var string
26
	 */
27
	private $language;
28
29
	/**
30
	 * Description eg. Deutsch
31
	 * @var string
32
	 */
33
	private $description;
34
35
	/**
36
	 * Constructor
37
	 *
38
	 * @param \StdClass $obj
39
	 * @param string $description
40
	 */
41
	public function __construct( $obj, $description ) {
42
		if ( is_object( $obj ) ) {
43
			$this->obj      = $obj;
44
			$this->language = MslsBlogCollection::get_blog_language( $this->obj->userblog_id );
45
		}
46
47
		$this->description = (string) $description;
48
	}
49
50
	/**
51
	 * Gets a member of the \StdClass-object by name
52
	 *
53
	 * The method return <em>null</em> if the requested member does not exists.
54
	 *
55
	 * @param string $key
56
	 *
57
	 * @return mixed|null
58
	 */
59
	final public function __get( $key ) {
60
		return isset( $this->obj->$key ) ? $this->obj->$key : null;
61
	}
62
63
	/**
64
	 * Gets the description stored in this object
65
	 *
66
	 * The method returns the stored language if the description is empty.
67
	 * @return string
68
	 */
69
	public function get_description(): string {
70
		return empty( $this->description ) ? $this->get_language() : $this->description;
71
	}
72
73
	/**
74
	 * Gets a customized title for the blog
75
	 *
76
	 * @return string
77
	 */
78
	public function get_title(): string {
79
		return sprintf( '%1$s (%2$s)', $this->obj->blogname, $this->get_description() );
80
	}
81
82
	/**
83
	 * Gets the language stored in this object
84
	 *
85
	 * @param string $default
86
	 *
87
	 * @return string
88
	 */
89
	public function get_language( $default = 'en_US' ) {
90
		return empty( $this->language ) ? $default : $this->language;
91
	}
92
93
	/**
94
	 * Gets the alpha2-part of the language-code
95
	 *
96
	 * @return string
97
	 */
98
	public function get_alpha2() {
99
		$language = $this->get_language();
100
101
		return substr( $language, 0, 2 );
102
	}
103
104
	/**
105
	 * @param MslsOptions $options
106
	 *
107
	 * @return string|null
108
	 */
109
	public function get_url( $options ) {
110
		if ( $this->obj->userblog_id == MslsBlogCollection::instance()->get_current_blog_id() ) {
111
			return $options->get_current_link();
112
		}
113
114
		return $this->get_permalink( $options );
115
	}
116
117
	/**
118
	 * @param MslsOptions $options
119
	 *
120
	 * @return string|null
121
	 */
122
	protected function get_permalink( $options ) {
123
		$url = null;
124
125
		$is_home = is_front_page();
126
127
		switch_to_blog( $this->obj->userblog_id );
128
129
		if ( is_object( $options ) && method_exists( $options, 'has_value' ) && ( $is_home || $options->has_value( $this->get_language() ) ) ) {
130
			$url = $options->get_permalink( $this->get_language() );
131
		}
132
133
		restore_current_blog();
134
135
		return $url;
136
	}
137
138
	/**
139
	 * @return string
140
	 */
141
	public function get_hreflang() {
142
		if ( has_filter( 'msls_head_hreflang' ) ) {
143
			/**
144
			 * Overrides the hreflang value
145
			 *
146
			 * @param string $language
147
			 *
148
			 * @since 0.9.9
149
			 */
150
			return (string) apply_filters( 'msls_head_hreflang', $this->get_language() );
151
		}
152
153
		return $this->get_alpha2();
154
	}
155
156
157
	/**
158
	 * Sort objects helper
159
	 *
160
	 * @param string $a
161
	 * @param string $b
162
	 *
163
	 * @return int
164
	 */
165
	public static function _cmp( $a, $b ) {
166
		if ( $a == $b ) {
167
			return 0;
168
		}
169
170
		return ( $a < $b ? ( - 1 ) : 1 );
171
	}
172
173
	/**
174
	 * Sort objects by language
175
	 *
176
	 * @param MslsBlog $a
177
	 * @param MslsBlog $b
178
	 *
179
	 * @return int
180
	 */
181
	public static function language( MslsBlog $a, MslsBlog $b ) {
182
		return self::_cmp( $a->get_language(), $b->get_language() );
183
	}
184
185
	/**
186
	 * Sort objects by description
187
	 *
188
	 * @param MslsBlog $a
189
	 * @param MslsBlog $b
190
	 *
191
	 * @return int
192
	 */
193
	public static function description( MslsBlog $a, MslsBlog $b ) {
194
		return self::_cmp( $a->get_description(), $b->get_description() );
195
	}
196
197
}
198