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

MslsBlog::get_alpha2()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
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
	 * Get 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
	 * Get 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() {
70
		return empty( $this->description ) ? $this->get_language() : $this->description;
71
	}
72
73
	/**
74
	 * Gets the language stored in this object
75
	 *
76
	 * @param string $default
77
	 *
78
	 * @return string
79
	 */
80
	public function get_language( $default = 'en_US' ) {
81
		return empty( $this->language ) ? $default : $this->language;
82
	}
83
84
	/**
85
	 * Gets the alpha2-part of the language-code
86
	 *
87
	 * @return string
88
	 */
89
	public function get_alpha2() {
90
		$language = $this->get_language();
91
92
		return substr( $language, 0, 2 );
93
	}
94
95
	/**
96
	 * @param MslsOptions $options
97
	 *
98
	 * @return string|null
99
	 */
100
	public function get_url( $options ) {
101
		if ( $this->obj->userblog_id == MslsBlogCollection::instance()->get_current_blog_id() ) {
102
			return $options->get_current_link();
103
		}
104
105
		return $this->get_permalink( $options );
106
	}
107
108
	/**
109
	 * @param MslsOptions $options
110
	 *
111
	 * @return string|null
112
	 */
113
	protected function get_permalink( $options ) {
114
		$url = null;
115
116
		switch_to_blog( $this->obj->userblog_id );
117
118
		if ( is_object( $options ) && method_exists( $options, 'has_value' ) && $options->has_value( $this->get_language() ) ) {
119
			$url = $options->get_permalink( $this->get_language() );
120
		}
121
122
		restore_current_blog();
123
124
		return $url;
125
	}
126
127
	/**
128
	 * @return string
129
	 */
130
	public function get_hreflang() {
131
		if ( has_filter( 'msls_head_hreflang' ) ) {
132
			/**
133
			 * Overrides the hreflang value
134
			 *
135
			 * @param string $language
136
			 *
137
			 * @since 0.9.9
138
			 */
139
			return (string) apply_filters( 'msls_head_hreflang', $this->get_language() );
140
		}
141
142
		return $this->get_alpha2();
143
	}
144
145
146
	/**
147
	 * Sort objects helper
148
	 *
149
	 * @param string $a
150
	 * @param string $b
151
	 *
152
	 * @return int
153
	 */
154
	public static function _cmp( $a, $b ) {
155
		if ( $a == $b ) {
156
			return 0;
157
		}
158
159
		return ( $a < $b ? ( - 1 ) : 1 );
160
	}
161
162
	/**
163
	 * Sort objects by language
164
	 *
165
	 * @param MslsBlog $a
166
	 * @param MslsBlog $b
167
	 *
168
	 * @return int
169
	 */
170
	public static function language( MslsBlog $a, MslsBlog $b ) {
171
		return self::_cmp( $a->get_language(), $b->get_language() );
172
	}
173
174
	/**
175
	 * Sort objects by description
176
	 *
177
	 * @param MslsBlog $a
178
	 * @param MslsBlog $b
179
	 *
180
	 * @return int
181
	 */
182
	public static function description( MslsBlog $a, MslsBlog $b ) {
183
		return self::_cmp( $a->get_description(), $b->get_description() );
184
	}
185
186
}
187