Skip to content

Commit 7d084ea

Browse files
committed
API overhaul, phpstan issues
1 parent 2f1bded commit 7d084ea

1 file changed

Lines changed: 216 additions & 0 deletions

File tree

includes/functions.php

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
3+
declare( strict_types=1 );
4+
5+
if ( ! defined( 'ABSPATH' ) ) {
6+
exit;
7+
}
8+
9+
/**
10+
* Get the output for using the links to the translations in your code
11+
*
12+
* @package Msls
13+
* @param mixed $attr
14+
* @return string
15+
*/
16+
function msls_get_switcher( $attr ): string {
17+
$arr = is_array( $attr ) ? $attr : array();
18+
$obj = apply_filters( 'msls_get_output', null );
19+
20+
return ! is_null( $obj ) ? strval( $obj->set_tags( $arr ) ) : '';
21+
}
22+
23+
/**
24+
* Output the links to the translations in your template
25+
*
26+
* You can call this function directly like that
27+
*
28+
* if ( function_exists ( 'msls_the_switcher' ) )
29+
* msls_the_switcher();
30+
*
31+
* or just use it as shortcode [sc_msls]
32+
*
33+
* @package Msls
34+
* @uses get_the_msls
35+
*
36+
* @param string[] $arr
37+
*/
38+
function msls_the_switcher( array $arr = array() ): void {
39+
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
40+
echo msls_get_switcher( $arr );
41+
}
42+
43+
/**
44+
* Gets the URL of the country flag-icon for a specific locale
45+
*
46+
* @param string $locale
47+
*
48+
* @return string
49+
*/
50+
function msls_get_flag_url( string $locale ): string {
51+
return ( new \lloc\Msls\MslsOptions() )->get_flag_url( $locale );
52+
}
53+
54+
/**
55+
* Gets the description for a blog for a specific locale
56+
*
57+
* @param string $locale
58+
* @param string $preset
59+
*
60+
* @return string
61+
*/
62+
function msls_get_blog_description( string $locale, string $preset = '' ): string {
63+
$blog = msls_blog( $locale );
64+
65+
return $blog ? $blog->get_description() : $preset;
66+
}
67+
68+
/**
69+
* Gets the permalink for a translation of the current post in a given language
70+
*
71+
* @param string $locale
72+
* @param string $preset
73+
*
74+
* @return string
75+
*/
76+
function msls_get_permalink( string $locale, string $preset = '' ): string {
77+
$url = null;
78+
$blog = msls_blog( $locale );
79+
80+
if ( $blog ) {
81+
$options = \lloc\Msls\MslsOptions::create();
82+
$url = $blog->get_url( $options );
83+
}
84+
85+
return $url ?? $preset;
86+
}
87+
88+
/**
89+
* Looks for the MslsBlog instance for a specific locale
90+
*
91+
* @param string $locale
92+
*
93+
* @return \lloc\Msls\MslsBlog|null
94+
*/
95+
function msls_blog( string $locale ): ?\lloc\Msls\MslsBlog {
96+
return msls_blog_collection()->get_blog( $locale );
97+
}
98+
99+
/**
100+
* Gets the MslsBlogCollection instance
101+
*
102+
* @return \lloc\Msls\MslsBlogCollection
103+
*/
104+
function msls_blog_collection(): \lloc\Msls\MslsBlogCollection {
105+
return \lloc\Msls\MslsBlogCollection::instance();
106+
}
107+
108+
/**
109+
* Gets the MslsOptions instance
110+
*
111+
* @return \lloc\Msls\MslsOptions
112+
*/
113+
function msls_options(): \lloc\Msls\MslsOptions {
114+
return \lloc\Msls\MslsOptions::instance();
115+
}
116+
117+
/**
118+
* Gets the MslsContentTypes instance
119+
*
120+
* @return \lloc\Msls\MslsContentTypes
121+
*/
122+
function msls_content_types(): \lloc\Msls\MslsContentTypes {
123+
return \lloc\Msls\MslsContentTypes::create();
124+
}
125+
126+
/**
127+
* Gets the MslsPostType instance
128+
*
129+
* @return \lloc\Msls\MslsPostType
130+
*/
131+
function msls_post_type(): \lloc\Msls\MslsPostType {
132+
return \lloc\Msls\MslsPostType::instance();
133+
}
134+
135+
/**
136+
* Gets the MslsTaxonomy instance
137+
*
138+
* @return \lloc\Msls\MslsTaxonomy
139+
*/
140+
function msls_taxonomy(): \lloc\Msls\MslsTaxonomy {
141+
return \lloc\Msls\MslsTaxonomy::instance();
142+
}
143+
144+
/**
145+
* Gets the MslsOutput instance
146+
*
147+
* @return \lloc\Msls\MslsOutput
148+
*/
149+
function msls_output(): \lloc\Msls\MslsOutput {
150+
return \lloc\Msls\MslsOutput::create();
151+
}
152+
153+
/**
154+
* Retrieves the MslsOptionsPost instance.
155+
*
156+
* @param int $id
157+
* @return \lloc\Msls\MslsOptionsPost
158+
*/
159+
function msls_get_post( int $id ): \lloc\Msls\MslsOptionsPost {
160+
return new \lloc\Msls\MslsOptionsPost( $id );
161+
}
162+
163+
/**
164+
* Retrieves the MslsOptionsTax instance.
165+
*
166+
* Determines the current query based on conditional tags:
167+
* - is_category
168+
* - is_tag
169+
* - is_tax
170+
*
171+
* @param int $id
172+
* @return \lloc\Msls\OptionsTaxInterface
173+
*/
174+
function msls_get_tax( int $id ): \lloc\Msls\OptionsTaxInterface {
175+
return \lloc\Msls\MslsOptionsTax::create( $id );
176+
}
177+
178+
/**
179+
* Retrieves the MslsOptionsQuery instance.
180+
*
181+
* Determines the current query based on conditional tags:
182+
* - is_day
183+
* - is_month
184+
* - is_year
185+
* - is_author
186+
* - is_post_type_archive
187+
*
188+
* @return ?\lloc\Msls\MslsOptionsQuery
189+
*/
190+
function msls_get_query(): ?\lloc\Msls\MslsOptionsQuery {
191+
return \lloc\Msls\MslsOptionsQuery::create();
192+
}
193+
194+
/**
195+
* Gets structured language data for all available translations
196+
*
197+
* Returns an array of language entries with locale, alpha2 code, URL,
198+
* label, flag URL, and whether it's the current language. This provides
199+
* programmatic access to the same data used by the switcher output,
200+
* without any HTML rendering.
201+
*
202+
* @param bool $filter When true, only returns languages with existing translations
203+
*
204+
* @return array<int, array{locale: string, alpha2: string, url: string, label: string, flag_url: string, current: bool}>
205+
*/
206+
function msls_get_languages( bool $filter = false ): array {
207+
return msls_output()->get_languages( $filter );
208+
}
209+
210+
/**
211+
* Trivial void function for actions that do not return anything.
212+
*
213+
* @return void
214+
*/
215+
function msls_return_void(): void {
216+
}

0 commit comments

Comments
 (0)