MslsAdminIcon::get_edit_new()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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