-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathMslsContentFilter.php
More file actions
93 lines (75 loc) · 2.05 KB
/
MslsContentFilter.php
File metadata and controls
93 lines (75 loc) · 2.05 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
<?php declare( strict_types=1 );
namespace lloc\Msls;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class MslsContentFilter {
/**
* The options instance.
*
* @var MslsOptions
*/
protected MslsOptions $options;
public function __construct( MslsOptions $options ) {
$this->options = $options;
}
public static function init(): void {
$obj = new self( msls_options() );
add_filter( 'the_content', array( $obj, 'content_filter' ) );
}
/**
* @param string $content
*
* @return string
*/
public function content_filter( string $content ) {
if ( ! is_front_page() && is_singular() && $this->options->is_content_filter() ) {
$content .= $this->filter_string();
}
return $content;
}
/**
* Create filter-string for msls_content_filter()
*
* @param string $pref
* @param string $post
*
* @return string
*/
public function filter_string( $pref = '<p id="msls">', $post = '</p>' ) {
$links_arr = MslsOutput::create()->get( 1, true, true );
$links_str = $this->format_available_languages( $links_arr );
/* translators: %s: list of languages */
$format = __( 'This post is also available in %s.', 'multisite-language-switcher' );
if ( has_filter( 'msls_filter_string' ) ) {
/**
* Overrides the string for the output of the translation hint
*
* @param string $output
* @param array $links
*
* @since 1.0
*/
$output = apply_filters( 'msls_filter_string', $format, $links_arr );
} elseif ( $links_str ) {
$output = sprintf( $format, $links_str );
}
return ! empty( $output ) ? $pref . $output . $post : '';
}
/**
* @param string[] $links
* @return string|null
*/
public function format_available_languages( array $links ): ?string {
if ( empty( $links ) ) {
return null;
}
if ( 1 === count( $links ) ) {
return $links[0];
}
$last = array_pop( $links );
/* translators: %1$s: list of languages separated by a comma, %2$s: last language */
$format = __( '%1$s and %2$s', 'multisite-language-switcher' );
return sprintf( $format, implode( ', ', $links ), $last );
}
}