forked from lloc/Multisite-Language-Switcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMslsAdminIcon.php
More file actions
322 lines (271 loc) · 6.04 KB
/
Copy pathMslsAdminIcon.php
File metadata and controls
322 lines (271 loc) · 6.04 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?php declare( strict_types=1 );
namespace lloc\Msls;
use lloc\Msls\Component\Component;
use lloc\Msls\Component\Icon\IconSvg;
use lloc\Msls\Component\Icon\IconLabel;
/**
* Handles the icon links in the backend
*
* @package Msls
*/
class MslsAdminIcon {
/**
* @var string
*/
protected $icon_type = 'action';
/**
* @var string
*/
protected $language;
/**
* @var string
*/
public $origin_language;
/**
* @var string
*/
protected $src;
/**
* @var string
*/
protected $href;
/**
* @var int
*/
protected $blog_id;
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $path = 'post-new.php';
/**
* The current object ID
*
* @var int
*/
protected $id;
const TYPE_FLAG = 'flag';
const TYPE_LABEL = 'label';
/**
* Constructor
*
* @param string $type
*/
public function __construct( ?string $type = null ) {
$this->type = $type;
$this->set_path();
}
/**
* @return string
*/
public function __toString() {
return $this->get_a();
}
/**
* @param ?string $type
*
* @return MslsAdminIcon|MslsAdminIconTaxonomy
*/
public static function create( ?string $type = null ) {
$obj = msls_content_types();
if ( ! $type ) {
$type = $obj->get_request();
}
return $obj->is_taxonomy() ? new MslsAdminIconTaxonomy( $type ) : new MslsAdminIcon( $type );
}
/**
* Set the icon path
*
* @param string $icon_type
*
* @return MslsAdminIcon
*/
public function set_icon_type( string $icon_type ): MslsAdminIcon {
$this->icon_type = $icon_type;
return $this;
}
/**
* Set the path by type
*
* @return MslsAdminIcon
*/
public function set_path(): MslsAdminIcon {
if ( 'post' !== $this->type ) {
$query_vars = array( 'post_type' => $this->type );
$this->path = add_query_arg( $query_vars, $this->path );
}
return $this;
}
/**
* Set language
*
* @param string $language
*
* @return MslsAdminIcon
*/
public function set_language( string $language ): MslsAdminIcon {
$this->language = $language;
return $this;
}
/**
* Set src
*
* @param string $src
*
* @return MslsAdminIcon
*/
public function set_src( string $src ): MslsAdminIcon {
$this->src = $src;
return $this;
}
/**
* Set href
*
* @param int $id
*
* @return MslsAdminIcon
*/
public function set_href( int $id ): MslsAdminIcon {
$this->href = get_edit_post_link( $id );
return $this;
}
/**
* Sets the id of the object this icon is for
*
* @param int $id
*
* @return MslsAdminIcon
*/
public function set_id( int $id ): MslsAdminIcon {
$this->id = $id;
return $this;
}
/**
* Sets the origin language for this icon
*
* @param string $origin_language
*
* @return MslsAdminIcon
*/
public function set_origin_language( string $origin_language ): MslsAdminIcon {
$this->origin_language = $origin_language;
return $this;
}
/**
* Get image as html-tag
*
* @return string
*/
public function get_img(): string {
return sprintf( '<img alt="%s" src="%s" />', $this->language, $this->src );
}
/**
* Get link as html-tag
*
* @return string
*/
public function get_a(): string {
if ( empty( $this->href ) ) {
if ( $this->should_quick_create() ) {
return $this->get_quick_create_a();
}
/* translators: %s: blog name */
$format = __( 'Create a new translation in the %s-blog', 'multisite-language-switcher' );
$href = $this->get_edit_new();
} else {
/* translators: %s: blog name */
$format = __( 'Edit the translation in the %s-blog', 'multisite-language-switcher' );
$href = $this->href;
}
$title = sprintf( $format, $this->language );
return sprintf( '<a title="%1$s" href="%2$s">%3$s</a> ', esc_attr( $title ), esc_url( $href ), $this->get_icon() );
}
/**
* @return bool
*/
protected function should_quick_create(): bool {
return null !== $this->id
&& null !== $this->origin_language
&& msls_options()->activate_quick_create;
}
/**
* @return string
*/
protected function get_quick_create_a(): string {
$collection = msls_blog_collection();
$source_blog_id = $collection->get_blog_id( $this->origin_language );
$target_blog_id = get_current_blog_id();
/* translators: %s: blog name */
$format = __( 'Create a new translation in the %s-blog', 'multisite-language-switcher' );
$title = sprintf( $format, $this->language );
return sprintf(
'<button type="button" class="msls-quick-create" title="%1$s" aria-label="%1$s" data-target-blog-id="%2$d" data-source-post-id="%3$d" data-source-blog-id="%4$d">%5$s</button> ',
esc_attr( $title ),
$target_blog_id,
$this->id,
$source_blog_id,
$this->get_icon()
);
}
/**
* Get icon as html-tag
*
* @return string
*/
public function get_icon(): string {
if ( ! $this->language ) {
return '';
}
switch ( $this->icon_type ) {
case self::TYPE_FLAG:
$class = ( new IconSvg() )->get( $this->language );
$icon = sprintf(
'<span class="flag-icon %s">%s</span>',
esc_attr( $class ),
esc_html( $this->language )
);
break;
case self::TYPE_LABEL:
$text = ( new IconLabel() )->get( $this->language );
$icon = sprintf(
'<span class="language-badge %s">%s</span>',
esc_attr( $this->language ),
$text
);
break;
default:
$class = empty( $this->href ) ? 'dashicons-plus' : 'dashicons-edit';
$icon = sprintf( '<span class="dashicons %s"></span>', esc_attr( $class ) );
}
return wp_kses( $icon, Component::get_allowed_html() );
}
/**
* Creates new admin link
*
* @return string
*/
public function get_edit_new(): string {
$path = $this->path;
if ( null !== $this->id && null !== $this->origin_language ) {
$path = add_query_arg(
array(
'msls_id' => $this->id,
'msls_lang' => $this->origin_language,
),
$this->path
);
}
/**
* Returns custom url of an admin icon link
*
* @param string $path
*
* @since 0.9.9
*/
$path = (string) apply_filters( 'msls_admin_icon_get_edit_new', $path );
return get_admin_url( get_current_blog_id(), $path );
}
}