1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace lloc\Msls; |
4
|
|
|
|
5
|
|
|
use lloc\Msls\Component\Input\Checkbox; |
6
|
|
|
use lloc\Msls\Component\Input\Group; |
7
|
|
|
use lloc\Msls\Component\Input\Label; |
8
|
|
|
use lloc\Msls\Component\Input\Text; |
9
|
|
|
use lloc\Msls\Component\Input\Select; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Administration of the options |
13
|
|
|
* |
14
|
|
|
* @package Msls |
15
|
|
|
*/ |
16
|
|
|
class MslsAdmin extends MslsMain { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Factory |
20
|
|
|
* |
21
|
|
|
* @codeCoverageIgnore |
22
|
|
|
* |
23
|
|
|
* @return MslsAdmin |
24
|
|
|
*/ |
25
|
|
|
public static function init() { |
26
|
|
|
if ( ! ( $obj = MslsRegistry::get_object( __CLASS__ ) ) ) { |
27
|
|
|
$options = MslsOptions::instance(); |
28
|
|
|
$collection = MslsBlogCollection::instance(); |
29
|
|
|
|
30
|
|
|
$obj = new static( $options, $collection ); |
31
|
|
|
|
32
|
|
|
MslsRegistry::set_object( __CLASS__, $obj ); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Override the capabilities needed for the plugin's settings |
36
|
|
|
* |
37
|
|
|
* @param string $capability |
38
|
|
|
* |
39
|
|
|
* @since 2.0 |
40
|
|
|
* |
41
|
|
|
*/ |
42
|
|
|
$caps = apply_filters( 'msls_admin_caps', 'manage_options' ); |
43
|
|
|
if ( current_user_can( $caps ) ) { |
44
|
|
|
$title = __( 'Multisite Language Switcher', 'multisite-language-switcher' ); |
45
|
|
|
add_options_page( $title, $title, 'manage_options', $obj->get_menu_slug(), [ $obj, 'render' ] ); |
46
|
|
|
|
47
|
|
|
add_action( 'admin_init', [ $obj, 'register' ] ); |
48
|
|
|
add_action( 'admin_notices', [ $obj, 'has_problems' ] ); |
49
|
|
|
|
50
|
|
|
add_filter( 'msls_admin_validate', [ $obj, 'set_blog_language' ] ); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $obj; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Let's do this simple |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
|
|
public function get_menu_slug() { |
63
|
|
|
return 'MslsAdmin'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get's the link for the switcher-settings in the wp-admin |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function get_options_page_link() { |
72
|
|
|
return sprintf( '/options-general.php?page=%s', $this->get_menu_slug() ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* You can use every method of the decorated object |
77
|
|
|
* |
78
|
|
|
* @param string $method |
79
|
|
|
* @param mixed $args |
80
|
|
|
* |
81
|
|
|
* @return mixed |
82
|
|
|
*/ |
83
|
|
|
public function __call( $method, $args ) { |
84
|
|
|
$parts = explode( '_', $method, 2 ); |
85
|
|
|
|
86
|
|
|
if ( 'rewrite' === $parts[0] ) { |
87
|
|
|
return $this->render_rewrite( $parts[1] ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$checkboxes = [ |
91
|
|
|
'activate_autocomplete' => __( 'Activate experimental autocomplete inputs', 'multisite-language-switcher' ), |
92
|
|
|
'activate_content_import' => __( 'Activate the content import functionality', 'multisite-language-switcher' ), |
93
|
|
|
'sort_by_description' => __( 'Sort languages by description', 'multisite-language-switcher' ), |
94
|
|
|
'exclude_current_blog' => __( 'Exclude this blog from output', 'multisite-language-switcher' ), |
95
|
|
|
'only_with_translation' => __( 'Show only links with a translation', 'multisite-language-switcher' ), |
96
|
|
|
'output_current_blog' => __( 'Display link to the current language', 'multisite-language-switcher' ), |
97
|
|
|
'content_filter' => __( 'Add hint for available translations', 'multisite-language-switcher' ), |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
if ( isset ( $checkboxes[ $method ] ) ) { |
101
|
|
|
echo ( new Group() )->add( new Checkbox( $method, $this->options->$method ) )->add( new Label( $method, $checkboxes[ $method ] ) )->render(); |
102
|
|
|
} else { |
103
|
|
|
$value = ! empty( $this->options->$method ) ? $this->options->$method : ''; |
104
|
|
|
echo ( new Text( $method, $value ) )->render(); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* There is something wrong? Here comes the message... |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function has_problems() { |
113
|
|
|
$message = ''; |
114
|
|
|
|
115
|
|
|
if ( $this->options->is_empty() ) { |
116
|
|
|
$message = sprintf( |
117
|
|
|
__( 'Multisite Language Switcher is almost ready. You must complete the configuration process</a>.' ), |
118
|
|
|
esc_url( admin_url( $this->get_options_page_link() ) ) |
119
|
|
|
); |
120
|
|
|
} elseif ( 1 == count( $this->options->get_available_languages() ) ) { |
121
|
|
|
$message = sprintf( |
122
|
|
|
__( 'There are no language files installed. You can <a href="%s">manually install some language files</a> or you could use a <a href="%s">plugin</a> to download these files automatically.' ), |
123
|
|
|
esc_url( 'http://codex.wordpress.org/Installing_WordPress_in_Your_Language#Manually_Installing_Language_Files' ), |
124
|
|
|
esc_url( 'http://wordpress.org/plugins/wp-native-dashboard/' ) |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return MslsPlugin::message_handler( $message, 'updated fade' ); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Render the options-page |
133
|
|
|
*/ |
134
|
|
|
public function render() { |
135
|
|
|
printf( |
136
|
|
|
'<div class="wrap"><div class="icon32" id="icon-options-general"><br/></div><h1>%s</h1>%s<br class="clear"/><form action="options.php" method="post"><p>%s</p>', |
137
|
|
|
__( 'Multisite Language Switcher Options', 'multisite-language-switcher' ), |
138
|
|
|
$this->subsubsub(), |
139
|
|
|
__( 'To achieve maximum flexibility, you have to configure each blog separately.', 'multisite-language-switcher' ) |
140
|
|
|
); |
141
|
|
|
|
142
|
|
|
settings_fields( 'msls' ); |
143
|
|
|
do_settings_sections( __CLASS__ ); |
144
|
|
|
|
145
|
|
|
printf( |
146
|
|
|
'<p class="submit"><input name="Submit" type="submit" class="button button-primary" value="%s" /></p></form></div>', |
147
|
|
|
( $this->options->is_empty() ? __( 'Configure', 'multisite-language-switcher' ) : __( 'Update', 'multisite-language-switcher' ) ) |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Create a submenu which contains links to all blogs of the current user |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
|
|
public function subsubsub() { |
157
|
|
|
$arr = []; |
158
|
|
|
|
159
|
|
|
foreach ( $this->collection->get_plugin_active_blogs() as $blog ) { |
160
|
|
|
$admin_url = get_admin_url( $blog->userblog_id, $this->get_options_page_link() ); |
161
|
|
|
$current = $blog->userblog_id == $this->collection->get_current_blog_id() ? ' class="current"' : ''; |
162
|
|
|
|
163
|
|
|
$arr[] = sprintf( '<a href="%1$s"%2$s>%3$s</a>', $admin_url, $current, $blog->get_title() ); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return empty( $arr ) ? '' : sprintf( '<ul class="subsubsub"><li>%s</li></ul>', implode( ' | </li><li>', $arr ) ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Register the form-elements |
171
|
|
|
* |
172
|
|
|
* @codeCoverageIgnore |
173
|
|
|
*/ |
174
|
|
|
public function register() { |
175
|
|
|
register_setting( 'msls', 'msls', [ $this, 'validate' ] ); |
176
|
|
|
|
177
|
|
|
$sections = [ |
178
|
|
|
'language_section' => __( 'Language Settings', 'multisite-language-switcher' ), |
179
|
|
|
'main_section' => __( 'Main Settings', 'multisite-language-switcher' ), |
180
|
|
|
'advanced_section' => __( 'Advanced Settings', 'multisite-language-switcher' ), |
181
|
|
|
]; |
182
|
|
|
|
183
|
|
|
global $wp_rewrite; |
184
|
|
|
if ( $wp_rewrite->using_permalinks() ) { |
185
|
|
|
$sections['rewrites_section'] = __( 'Rewrites Settings', 'multisite-language-switcher' ); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
foreach ( $sections as $id => $title ) { |
189
|
|
|
add_settings_section( $id, $title, [ $this, $id ], __CLASS__ ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Lets you add your own settings section |
194
|
|
|
* |
195
|
|
|
* @param string $page |
196
|
|
|
* |
197
|
|
|
* @since 1.0 |
198
|
|
|
* |
199
|
|
|
*/ |
200
|
|
|
do_action( 'msls_admin_register', __CLASS__ ); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Registers the fields in the language_section |
205
|
|
|
* |
206
|
|
|
* Returns the number of added fields |
207
|
|
|
* |
208
|
|
|
* @return int |
209
|
|
|
*/ |
210
|
|
|
public function language_section(): int { |
211
|
|
|
$map = [ 'blog_language' => __( 'Blog Language', 'multisite-language-switcher' ) ]; |
212
|
|
|
|
213
|
|
|
return $this->add_settings_fields( $map, 'language_section' ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Registers the fields in the main_section |
218
|
|
|
* |
219
|
|
|
* Returns the number of added fields |
220
|
|
|
* |
221
|
|
|
* @return int |
222
|
|
|
*/ |
223
|
|
|
public function main_section(): int { |
224
|
|
|
$map = [ |
225
|
|
|
'display' => __( 'Display', 'multisite-language-switcher' ), |
226
|
|
|
'sort_by_description' => __( 'Sort languages', 'multisite-language-switcher' ), |
227
|
|
|
'output_current_blog' => __( 'Current language link', 'multisite-language-switcher' ), |
228
|
|
|
'only_with_translation' => __( 'Translation links', 'multisite-language-switcher' ), |
229
|
|
|
'description' => __( 'Description', 'multisite-language-switcher' ), |
230
|
|
|
'before_output' => __( 'Text/HTML before the list', 'multisite-language-switcher' ), |
231
|
|
|
'after_output' => __( 'Text/HTML after the list', 'multisite-language-switcher' ), |
232
|
|
|
'before_item' => __( 'Text/HTML before each item', 'multisite-language-switcher' ), |
233
|
|
|
'after_item' => __( 'Text/HTML after each item', 'multisite-language-switcher' ), |
234
|
|
|
'content_filter' => __( 'Available translations hint', 'multisite-language-switcher' ), |
235
|
|
|
'content_priority' => __( 'Hint priority', 'multisite-language-switcher' ), |
236
|
|
|
]; |
237
|
|
|
|
238
|
|
|
return $this->add_settings_fields( $map, 'main_section' ); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* Registers the fields in the advanced_section |
243
|
|
|
* |
244
|
|
|
* Returns the number of added fields |
245
|
|
|
* |
246
|
|
|
* @return int |
247
|
|
|
*/ |
248
|
|
|
public function advanced_section(): int { |
249
|
|
|
$map = [ |
250
|
|
|
'activate_autocomplete' => __( 'Autocomplete', 'multisite-language-switcher' ), |
251
|
|
|
'image_url' => __( 'Custom URL for flag-images', 'multisite-language-switcher' ), |
252
|
|
|
'reference_user' => __( 'Reference user', 'multisite-language-switcher' ), |
253
|
|
|
'exclude_current_blog' => __( 'Exclude blog', 'multisite-language-switcher' ), |
254
|
|
|
'activate_content_import' => __( 'Content import', 'multisite-language-switcher' ), |
255
|
|
|
]; |
256
|
|
|
|
257
|
|
|
return $this->add_settings_fields( $map, 'advanced_section' ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Registers the fields in the rewrites_section |
262
|
|
|
* |
263
|
|
|
* Returns the number of added fields |
264
|
|
|
* |
265
|
|
|
* @return int |
266
|
|
|
*/ |
267
|
|
|
public function rewrites_section(): int { |
268
|
|
|
$map = []; |
269
|
|
|
foreach ( get_post_types( [ 'public' => true ], 'objects' ) as $key => $object ) { |
270
|
|
|
$map["rewrite_{$key}"] = sprintf( __( '%s Slug', 'multisite-language-switcher' ), $object->label ); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
return $this->add_settings_fields( $map, 'rewrites_section' ); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @param array $map |
278
|
|
|
* @param string $section |
279
|
|
|
* |
280
|
|
|
* @return int |
281
|
|
|
*/ |
282
|
|
|
protected function add_settings_fields( array $map, string $section ): int { |
283
|
|
|
foreach ( $map as $id => $title ) { |
284
|
|
|
add_settings_field( $id, $title, [ $this, $id ], __CLASS__, $section, [ 'label_for' => $id ] ); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Lets you add your own field to the section |
289
|
|
|
* |
290
|
|
|
* @param string $page |
291
|
|
|
* @param string $section |
292
|
|
|
* |
293
|
|
|
* @since 2.4.4 |
294
|
|
|
* |
295
|
|
|
*/ |
296
|
|
|
do_action( "msls_admin_{$section}", __CLASS__, $section ); |
297
|
|
|
|
298
|
|
|
return count( $map ); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Shows the select-form-field 'blog_language' |
303
|
|
|
*/ |
304
|
|
|
public function blog_language() { |
305
|
|
|
$languages = $this->options->get_available_languages(); |
306
|
|
|
$selected = get_locale(); |
307
|
|
|
|
308
|
|
|
echo ( new Select( 'blog_language', $languages, $selected ) )->render(); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* Shows the select-form-field 'display' |
313
|
|
|
*/ |
314
|
|
|
public function display() { |
315
|
|
|
echo ( new Select( 'display', MslsLink::get_types_description(), $this->options->display ) )->render(); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Shows the select-form-field 'reference_user' |
320
|
|
|
*/ |
321
|
|
|
public function reference_user() { |
322
|
|
|
$users = []; |
323
|
|
|
|
324
|
|
|
foreach ( $this->collection->get_users() as $user ) { |
325
|
|
|
$users[ $user->ID ] = $user->user_nicename; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
echo ( new Select( 'reference_user', $users, $this->options->reference_user ) )->render(); |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
/** |
332
|
|
|
* The description for the current blog |
333
|
|
|
* |
334
|
|
|
* The language will be used ff there is no description. |
335
|
|
|
*/ |
336
|
|
|
public function description() { |
337
|
|
|
echo ( new Text( 'description', $this->options->description, '40' ) )->render(); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* If the output in the_content is active you can set the priority too |
342
|
|
|
* |
343
|
|
|
* Default is 10. But may be there are other plugins active and you run into |
344
|
|
|
* trouble. So you can decide a higher (from 1) or a lower (to 100) priority |
345
|
|
|
* for the output |
346
|
|
|
*/ |
347
|
|
|
public function content_priority() { |
348
|
|
|
$temp = array_merge( range( 1, 10 ), [ 20, 50, 100 ] ); |
349
|
|
|
$arr = array_combine( $temp, $temp ); |
350
|
|
|
$selected = empty( $this->options->content_priority ) ? 10 : $this->options->content_priority; |
351
|
|
|
|
352
|
|
|
echo ( new Select( 'content_priority', $arr, $selected ) )->render(); |
353
|
|
|
} |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Rewrites slugs for registered post types |
357
|
|
|
* |
358
|
|
|
* @param string $key |
359
|
|
|
*/ |
360
|
|
|
public function render_rewrite( $key ) { |
361
|
|
|
$rewrite = get_post_type_object( $key )->rewrite; |
362
|
|
|
|
363
|
|
|
$value = ''; |
364
|
|
|
if ( true === $rewrite ) { |
365
|
|
|
$value = $key; |
366
|
|
|
} elseif ( ! empty( $rewrite['slug'] ) ) { |
367
|
|
|
$value = $rewrite['slug']; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
echo ( new Text( "rewrite_{$key}", $value, 30, true ) )->render(); |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* Validates input before saving it |
375
|
|
|
* |
376
|
|
|
* @param array $arr Values of the submitted form |
377
|
|
|
* |
378
|
|
|
* @return array Validated input |
379
|
|
|
*/ |
380
|
|
|
public function validate( array $arr ) { |
381
|
|
|
/** |
382
|
|
|
* Returns custom filtered input array |
383
|
|
|
* |
384
|
|
|
* @param array $arr |
385
|
|
|
* |
386
|
|
|
* @since 1.0 |
387
|
|
|
* |
388
|
|
|
*/ |
389
|
|
|
$arr = (array) apply_filters( 'msls_admin_validate', $arr ); |
390
|
|
|
|
391
|
|
|
$arr['display'] = intval( $arr['display'] ?? 0 ); |
392
|
|
|
if ( isset( $arr['image_url'] ) ) { |
393
|
|
|
$arr['image_url'] = rtrim( esc_attr( $arr['image_url'] ), '/' ); |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
return $arr; |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Filter which sets the global blog language |
401
|
|
|
* |
402
|
|
|
* @param array $arr |
403
|
|
|
* |
404
|
|
|
* @return array |
405
|
|
|
*/ |
406
|
|
|
public function set_blog_language( array $arr ) { |
407
|
|
|
if ( isset( $arr['blog_language'] ) ) { |
408
|
|
|
$blog_language = ( 'en_US' === $arr['blog_language'] ) ? '' : $arr['blog_language']; |
409
|
|
|
update_option( 'WPLANG', $blog_language ); |
410
|
|
|
unset( $arr['blog_language'] ); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
return $arr; |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
} |
417
|
|
|
|