Completed
Push — master ( ea28d3...1760c1 )
by Dennis
04:45
created

MslsAdminIcon::get_img()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * MslsAdminIcon
4
 * @author Dennis Ploetner <[email protected]>
5
 * @since 0.9.8
6
 */
7
8
namespace lloc\Msls;
9
10
/**
11
 * Handles the icon links in the backend
12
 * @package Msls
13
 */
14
class MslsAdminIcon {
15
16
	/**
17
	 * Language
18
	 * @var string
19
	 */
20
	protected $language;
21
22
	/**
23
	 * Origin Language
24
	 * @var string
25
	 */
26
	public $origin_language;
27
28
	/**
29
	 * Source
30
	 * @var string
31
	 */
32
	protected $src;
33
34
	/**
35
	 * URL
36
	 * @var string
37
	 */
38
	protected $href;
39
40
	/**
41
	 * Blog id
42
	 * @var int
43
	 */
44
	protected $blog_id;
45
46
	/**
47
	 * Type
48
	 * @var string
49
	 */
50
	protected $type;
51
52
	/**
53
	 * Path
54
	 * @var string
55
	 */
56
	protected $path = 'post-new.php';
57
58
	/**
59
	 * The current object ID
60
	 * @var int
61
	 */
62
	protected $id;
63
64
	/**
65
	 * Factory method
66
	 * @return MslsAdminIcon
67
	 */
68
	public static function create() {
69
		$obj  = MslsContentTypes::create();
70
		$type = $obj->get_request();
71
72
		if ( $obj->is_taxonomy() ) {
73
			return new MslsAdminIconTaxonomy( $type );
74
		}
75
76
		return new MslsAdminIcon( $type );
77
	}
78
79
	/**
80
	 * Constructor
81
	 * @param string $type
82
	 */
83
	public function __construct( $type ) {
84
		$this->type = esc_attr( $type );
85
		$this->set_path();
86
	}
87
88
	/**
89
	 * Set the path by type
90
	 *
91
	 * @uses add_query_arg()
92
	 *
93
	 * @return MslsAdminIcon
94
	 */
95
	public function set_path() {
96
		if ( 'post' != $this->type ) {
97
			$this->path = add_query_arg( [ 'post_type' => $this->type ], $this->path );
98
		}
99
100
		return $this;
101
	}
102
103
	/**
104
	 * Set language
105
	 *
106
	 * @param string $language
107
	 *
108
	 * @return MslsAdminIcon
109
	 */
110
	public function set_language( $language ) {
111
		$this->language = $language;
112
113
		return $this;
114
	}
115
116
	/**
117
	 * Set src
118
	 *
119
	 * @param string $src
120
	 *
121
	 * @return MslsAdminIcon
122
	 */
123
	public function set_src( $src ) {
124
		$this->src = $src;
125
126
		return $this;
127
	}
128
129
	/**
130
	 * Set href
131
	 * @uses get_edit_post_link()
132
	 *
133
	 * @param int $id
134
	 *
135
	 * @return MslsAdminIcon
136
	 */
137
	public function set_href( $id ) {
138
		$this->href = get_edit_post_link( $id );
139
140
		return $this;
141
	}
142
143
	/**
144
	 * Handles the output when object is treated like a string
145
	 * @return string
146
	 */
147
	public function __toString() {
148
		return $this->get_a();
149
	}
150
151
	/**
152
	 * Get image as html-tag
153
	 *
154
	 * @return string
155
	 */
156
	public function get_img() {
157
		return sprintf( '<img alt="%s" src="%s" />', $this->language, $this->src );
158
	}
159
160
	/**
161
	 * Get link as html-tag
162
	 * @return string
163
	 */
164
	public function get_a() {
165
		if ( ! empty( $this->href ) ) {
166
			$href  = $this->href;
167
			$title = sprintf(
168
				__( 'Edit the translation in the %s-blog', 'multisite-language-switcher' ),
169
				$this->language
170
			);
171
		}
172
		else {
173
			$href  = $this->get_edit_new();
174
			$title = sprintf(
175
				__( 'Create a new translation in the %s-blog', 'multisite-language-switcher' ),
176
				$this->language
177
			);
178
		}
179
		return sprintf(
180
			'<a title="%s" href="%s">%s</a>&nbsp;',
181
			$title,
182
			$href,
183
			$this->get_img()
184
		);
185
	}
186
187
	/**
188
	 * Creates new admin link
189
	 * @uses get_admin_url()
190
	 * @todo check if we need this method separately
191
	 * @return string
192
	 */
193
	public function get_edit_new() {
194
		$path = $this->path;
195
196
		if ( null !== $this->id && null !== $this->origin_language ) {
197
			$path = add_query_arg(
198
				array( 'msls_id' => $this->id, 'msls_lang' => $this->origin_language ),
199
				$this->path
200
			);
201
		}
202
203
		/**
204
		 * Returns custom url of an admin icon link
205
		 * @since 0.9.9
206
		 * @param string $path
207
		 */
208
		return get_admin_url(
209
			get_current_blog_id(),
210
			(string) apply_filters( 'msls_admin_icon_get_edit_new', $path )
211
		);
212
	}
213
214
	/**
215
	 * Sets the id of the object this icon is for
216
	 *
217
	 * @param int $id
218
	 *
219
	 * @return MslsAdminIcon
220
	 */
221
	public function set_id( $id ) {
222
		$this->id = $id;
223
224
		return $this;
225
	}
226
227
	/**
228
	 * Sets the origin language for this icon
229
	 *
230
	 * @param string $origin_language
231
	 *
232
	 * @return MslsAdminIcon
233
	 */
234
	public function set_origin_language( $origin_language ) {
235
		$this->origin_language = $origin_language;
236
237
		return $this;
238
	}
239
240
}
241