-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathMslsAdmin.php
481 lines (417 loc) · 13.8 KB
/
MslsAdmin.php
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
<?php
namespace lloc\Msls;
use lloc\Msls\Component\Input\Checkbox;
use lloc\Msls\Component\Input\Group;
use lloc\Msls\Component\Input\Label;
use lloc\Msls\Component\Input\Text;
use lloc\Msls\Component\Input\Select;
/**
* Administration of the options
*
* @method activate_autocomplete(): void
* @method sort_by_description(): void
* @method exclude_current_blog(): void
* @method only_with_translation(): void
* @method output_current_blog(): void
* @method before_output(): void
* @method after_output(): void
* @method before_item(): void
* @method after_item(): void
* @method content_filter(): void
*
* @package Msls
*/
class MslsAdmin extends MslsMain {
public const MAX_REFERENCE_USERS = 100;
/**
* Factory
*
* @codeCoverageIgnore
*
* @return MslsAdmin
*/
public static function init() {
if ( ! ( $obj = MslsRegistry::get_object( __CLASS__ ) ) ) {
$collection = msls_blog_collection();
$obj = new static( msls_options(), $collection );
MslsRegistry::set_object( __CLASS__, $obj );
/**
* Override the capabilities needed for the plugin's settings
*
* @param string $capability
*
* @since 2.0
*/
$caps = apply_filters( 'msls_admin_caps', 'manage_options' );
if ( current_user_can( $caps ) ) {
$title = __( 'Multisite Language Switcher', 'multisite-language-switcher' );
add_options_page( $title, $title, 'manage_options', $obj->get_menu_slug(), array( $obj, 'render' ) );
add_action( 'admin_init', array( $obj, 'register' ) );
add_action( 'admin_notices', array( $obj, 'has_problems' ) );
add_filter( 'msls_admin_validate', array( $obj, 'set_blog_language' ) );
}
}
return $obj;
}
/**
* Let's do this simple
*
* @return string
*/
public function get_menu_slug() {
return 'MslsAdmin';
}
/**
* Get's the link for the switcher-settings in the wp-admin
*
* @return string
*/
public function get_options_page_link() {
return sprintf( '/options-general.php?page=%s', $this->get_menu_slug() );
}
/**
* You can use every method of the decorated object
*
* @param string $method
* @param mixed $args
*
* @return mixed
*/
public function __call( $method, $args ) {
$parts = explode( '_', $method, 2 );
if ( is_array( $parts ) && 'rewrite' === $parts[0] ) {
$this->render_rewrite( $parts[1] );
return;
}
$checkboxes = array(
'activate_autocomplete' => __(
'Activate experimental autocomplete inputs',
'multisite-language-switcher'
),
'activate_content_import' => __(
'Activate the content import functionality',
'multisite-language-switcher'
),
'sort_by_description' => __( 'Sort languages by description', 'multisite-language-switcher' ),
'exclude_current_blog' => __( 'Exclude this blog from output', 'multisite-language-switcher' ),
'only_with_translation' => __( 'Show only links with a translation', 'multisite-language-switcher' ),
'output_current_blog' => __( 'Display link to the current language', 'multisite-language-switcher' ),
'content_filter' => __( 'Add hint for available translations', 'multisite-language-switcher' ),
);
if ( isset( $checkboxes[ $method ] ) ) {
echo ( new Group() )->add( new Checkbox( $method, $this->options->$method ) )->add(
new Label(
$method,
$checkboxes[ $method ]
)
)->render();
} else {
$value = ! empty( $this->options->$method ) ? $this->options->$method : '';
echo ( new Text( $method, $value ) )->render();
}
}
/**
* There is something wrong? Here comes the message...
*
* @return bool
*/
public function has_problems(): bool {
$message = '';
if ( $this->options->is_empty() ) {
$message = sprintf(
__(
'Multisite Language Switcher is almost ready. You must <a href="%s">complete the configuration process</a>.',
'multisite-language-switcher'
),
esc_url( admin_url( $this->get_options_page_link() ) )
);
} elseif ( 1 == count( $this->options->get_available_languages() ) ) {
$message = sprintf(
__(
'There are no language files installed. You can <a href="%1$s">manually install some language files</a> or you could use a <a href="%2$s">plugin</a> to download these files automatically.',
'multisite-language-switcher'
),
esc_url( 'http://codex.wordpress.org/Installing_WordPress_in_Your_Language#Manually_Installing_Language_Files' ),
esc_url( 'http://wordpress.org/plugins/wp-native-dashboard/' )
);
}
return MslsPlugin::message_handler( $message, 'updated fade' );
}
/**
* Render the options-page
*/
public function render(): void {
printf(
'<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>',
__( 'Multisite Language Switcher Options', 'multisite-language-switcher' ),
$this->subsubsub(),
__(
'To achieve maximum flexibility, you have to configure each blog separately.',
'multisite-language-switcher'
)
);
settings_fields( 'msls' );
do_settings_sections( __CLASS__ );
printf(
'<p class="submit"><input name="Submit" type="submit" class="button button-primary" value="%s" /></p></form></div>',
( $this->options->is_empty() ? __( 'Configure', 'multisite-language-switcher' ) : __(
'Update',
'multisite-language-switcher'
) )
);
}
/**
* Create a submenu which contains links to all blogs of the current user
*
* @return string
*/
public function subsubsub() {
$icon_type = $this->options->get_icon_type();
$arr = array();
foreach ( $this->collection->get_plugin_active_blogs() as $blog ) {
$admin_url = get_admin_url( $blog->userblog_id, $this->get_options_page_link() );
$current = $blog->userblog_id == $this->collection->get_current_blog_id() ? ' class="current"' : '';
$arr[] = sprintf( '<a href="%1$s"%2$s>%3$s</a>', $admin_url, $current, $blog->get_title( $icon_type ) );
}
return empty( $arr ) ? '' : sprintf(
'<ul class="subsubsub"><li>%s</li></ul>',
implode( ' | </li><li>', $arr )
);
}
/**
* Register the form-elements
*
* @codeCoverageIgnore
*/
public function register() {
register_setting( 'msls', 'msls', array( $this, 'validate' ) );
$sections = array(
'language_section' => __( 'Language Settings', 'multisite-language-switcher' ),
'main_section' => __( 'Main Settings', 'multisite-language-switcher' ),
'advanced_section' => __( 'Advanced Settings', 'multisite-language-switcher' ),
);
global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() ) {
$sections['rewrites_section'] = __( 'Rewrites Settings', 'multisite-language-switcher' );
}
foreach ( $sections as $id => $title ) {
add_settings_section( $id, $title, array( $this, $id ), __CLASS__ );
}
/**
* Lets you add your own settings section
*
* @param string $page
*
* @since 1.0
*/
do_action( 'msls_admin_register', __CLASS__ );
}
/**
* Registers the fields in the language_section
*
* Returns the number of added fields
*
* @return int
*/
public function language_section(): int {
$map = array( 'blog_language' => __( 'Blog Language', 'multisite-language-switcher' ) );
return $this->add_settings_fields( $map, 'language_section' );
}
/**
* Registers the fields in the main_section
*
* Returns the number of added fields
*
* @return int
*/
public function main_section(): int {
$map = array(
'display' => __( 'Display', 'multisite-language-switcher' ),
'admin_display' => __( 'Admin Display', 'multisite-language-switcher' ),
'sort_by_description' => __( 'Sort languages', 'multisite-language-switcher' ),
'output_current_blog' => __( 'Current language link', 'multisite-language-switcher' ),
'only_with_translation' => __( 'Translation links', 'multisite-language-switcher' ),
'description' => __( 'Description', 'multisite-language-switcher' ),
'before_output' => __( 'Text/HTML before the list', 'multisite-language-switcher' ),
'after_output' => __( 'Text/HTML after the list', 'multisite-language-switcher' ),
'before_item' => __( 'Text/HTML before each item', 'multisite-language-switcher' ),
'after_item' => __( 'Text/HTML after each item', 'multisite-language-switcher' ),
'content_filter' => __( 'Available translations hint', 'multisite-language-switcher' ),
'content_priority' => __( 'Hint priority', 'multisite-language-switcher' ),
);
return $this->add_settings_fields( $map, 'main_section' );
}
/**
* Registers the fields in the advanced_section
*
* Returns the number of added fields
*
* @return int
*/
public function advanced_section(): int {
$map = array(
'activate_autocomplete' => __( 'Autocomplete', 'multisite-language-switcher' ),
'image_url' => __( 'Custom URL for flag-images', 'multisite-language-switcher' ),
'reference_user' => __( 'Reference user', 'multisite-language-switcher' ),
'exclude_current_blog' => __( 'Exclude blog', 'multisite-language-switcher' ),
'activate_content_import' => __( 'Content import', 'multisite-language-switcher' ),
);
return $this->add_settings_fields( $map, 'advanced_section' );
}
/**
* Registers the fields in the rewrites_section
*
* Returns the number of added fields
*
* @return int
*/
public function rewrites_section(): int {
$map = array();
foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $key => $object ) {
$map[ "rewrite_{$key}" ] = sprintf( __( '%s Slug', 'multisite-language-switcher' ), $object->label );
}
return $this->add_settings_fields( $map, 'rewrites_section' );
}
/**
* @param array $map
* @param string $section
*
* @return int
*/
protected function add_settings_fields( array $map, string $section ): int {
foreach ( $map as $id => $title ) {
add_settings_field( $id, $title, array( $this, $id ), __CLASS__, $section, array( 'label_for' => $id ) );
}
/**
* Lets you add your own field to the section
*
* @param string $page
* @param string $section
*
* @since 2.4.4
*/
do_action( "msls_admin_{$section}", __CLASS__, $section );
return count( $map );
}
/**
* Shows the select-form-field 'blog_language'
*/
public function blog_language() {
$languages = $this->options->get_available_languages();
$selected = get_locale();
echo ( new Select( 'blog_language', $languages, $selected ) )->render();
}
/**
* Shows the select-form-field 'display'
*/
public function display() {
echo ( new Select( 'display', MslsLink::get_types_description(), $this->options->display ) )->render();
}
/**
* Shows the select-form-field 'admin_display'
*/
public function admin_display() {
echo ( new Select(
'admin_display',
array(
'flag' => __( 'Flag', 'multisite-language-switcher' ),
'label' => __( 'Label', 'multisite-language-switcher' ),
),
$this->options->admin_display
) )->render();
}
/**
* Shows the select-form-field 'reference_user'
*/
public function reference_user() {
$users = array();
foreach ( (array) apply_filters( 'msls_reference_users', $this->collection->get_users() ) as $user ) {
$users[ $user->ID ] = $user->user_nicename;
}
if ( count( $users ) > self::MAX_REFERENCE_USERS ) {
$users = array_slice( $users, 0, self::MAX_REFERENCE_USERS, true );
$message = sprintf(
__(
'Multisite Language Switcher: Collection for reference user has been truncated because it exceeded the maximum of %s users. Please, use the hook "msls_reference_users" to filter the result before!',
'multisite-language-switcher'
),
self::MAX_REFERENCE_USERS
);
trigger_error( $message );
}
echo ( new Select( 'reference_user', $users, $this->options->reference_user ) )->render();
}
/**
* The description for the current blog
*
* The language will be used ff there is no description.
*/
public function description() {
echo ( new Text( 'description', $this->options->description, '40' ) )->render();
}
/**
* If the output in the_content is active you can set the priority too
*
* Default is 10. But may be there are other plugins active and you run into
* trouble. So you can decide a higher (from 1) or a lower (to 100) priority
* for the output
*/
public function content_priority() {
$temp = array_merge( range( 1, 10 ), array( 20, 50, 100 ) );
$arr = array_combine( $temp, $temp );
$selected = empty( $this->options->content_priority ) ? 10 : $this->options->content_priority;
echo ( new Select( 'content_priority', $arr, $selected ) )->render();
}
/**
* Rewrites slugs for registered post types
*
* @param string $key
*/
public function render_rewrite( $key ) {
$rewrite = get_post_type_object( $key )->rewrite;
$value = '';
if ( true === $rewrite ) {
$value = $key;
} elseif ( ! empty( $rewrite['slug'] ) ) {
$value = $rewrite['slug'];
}
echo ( new Text( "rewrite_{$key}", $value, 30, true ) )->render();
}
/**
* Validates input before saving it
*
* @param array $arr Values of the submitted form
*
* @return array Validated input
*/
public function validate( array $arr ) {
/**
* Returns custom filtered input array
*
* @param array $arr
*
* @since 1.0
*/
$arr = (array) apply_filters( 'msls_admin_validate', $arr );
$arr['display'] = intval( $arr['display'] ?? 0 );
if ( isset( $arr['image_url'] ) ) {
$arr['image_url'] = rtrim( esc_attr( $arr['image_url'] ), '/' );
}
return $arr;
}
/**
* Filter which sets the global blog language
*
* @param array $arr
*
* @return array
*/
public function set_blog_language( array $arr ) {
if ( isset( $arr['blog_language'] ) ) {
$blog_language = ( 'en_US' === $arr['blog_language'] ) ? '' : $arr['blog_language'];
update_option( 'WPLANG', $blog_language );
unset( $arr['blog_language'] );
}
return $arr;
}
}