Skip to content

Commit 76d165c

Browse files
committed
Documentation added
1 parent 5554786 commit 76d165c

3 files changed

Lines changed: 665 additions & 1 deletion

File tree

docs/api.md

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
# Public API Functions
2+
3+
Multisite Language Switcher ships with a small set of global helper functions
4+
that act as the plugin's public API. They live in `includes/api.php` and are
5+
loaded on the `plugins_loaded` action, so they're available anywhere your
6+
theme or another plugin runs after WordPress has bootstrapped its plugins.
7+
8+
Use these functions in templates, shortcodes, blocks, or other plugins
9+
whenever you need to render the switcher, resolve a translation URL, or
10+
reach into MSLS without instantiating its internal classes directly. They
11+
also wrap MSLS's registry/singleton plumbing, which means calling them
12+
repeatedly is cheap and safe.
13+
14+
A `function_exists()` guard is recommended before any of these calls in theme
15+
templates, so your theme degrades gracefully when MSLS is deactivated:
16+
17+
```php
18+
if ( function_exists( 'msls_the_switcher' ) ) {
19+
msls_the_switcher();
20+
}
21+
```
22+
23+
## Rendering the switcher
24+
25+
### msls_the_switcher
26+
27+
Prints the language switcher in your template. The optional array argument is
28+
forwarded to the underlying `Output` object as tag overrides (`before_output`,
29+
`after_output`, `before_item`, `after_item`), which lets you wrap the
30+
switcher in custom markup for a specific template without changing the
31+
plugin's global settings.
32+
33+
### msls_get_switcher
34+
35+
Returns the language switcher as a string instead of printing it, so you can
36+
embed it in other strings, return it from a shortcode, or pass it through
37+
your own escaping/filtering. The optional argument behaves the same as for
38+
`msls_the_switcher()`.
39+
40+
## Translation lookups
41+
42+
### msls_get_permalink
43+
44+
Returns the URL of the translation of the current post (or term, or archive)
45+
in the language identified by the given locale. If no translation exists, the
46+
optional `$preset` string is returned instead — typically an empty string or
47+
a fallback URL. Useful when you need to render your own switcher markup or
48+
build a single direct link to a specific language.
49+
50+
### msls_get_flag_url
51+
52+
Returns the URL of the flag icon for a given locale. The base directory can
53+
be customized through the `msls_options_get_flag_url` filter and the file
54+
name through `msls_options_get_flag_icon`, so this helper always points at
55+
the configured icon source.
56+
57+
### msls_get_blog_description
58+
59+
Returns the textual description configured for the blog mapped to a given
60+
locale (the same value used by MSLS settings as the language label). The
61+
optional `$preset` is returned when no matching blog is registered.
62+
63+
### msls_blog
64+
65+
Resolves a locale to its `Blog` instance, or `null` when no blog with that
66+
locale is part of the MSLS collection. Use it when you need direct access to
67+
a single blog — for example to call `get_url()` for a custom switcher — and
68+
want to handle the missing-blog case explicitly.
69+
70+
## Service accessors
71+
72+
### msls_blog_collection
73+
74+
Returns the `Blog\Collection` singleton, which represents every blog MSLS
75+
tracks (their locales, descriptions, URLs, and the current blog). Reach for
76+
it when you want to iterate over the full set of languages yourself.
77+
78+
### msls_options
79+
80+
Returns the global `Options` singleton — the merged plugin settings for the
81+
current request. Use it to read configuration values such as the display
82+
mode, image URL overrides, or whether MSLS should include the current blog
83+
in the switcher.
84+
85+
### msls_output
86+
87+
Returns a fresh `Frontend\Output` instance built for the current request.
88+
This is the same object `msls_the_switcher()` and `msls_get_switcher()` use
89+
internally, so call it directly when you need to chain custom tag overrides
90+
or inspect the resolved switcher state before rendering.
91+
92+
### msls_content_types
93+
94+
Returns the `ContentTypes\ContentTypes` instance, a context-aware factory
95+
that exposes both supported post types and taxonomies. Use it when you need
96+
to ask "is this current request a translatable content type?" without
97+
worrying about whether you're on a post or a taxonomy screen.
98+
99+
### msls_post_type
100+
101+
Returns the `ContentTypes\PostType` singleton, which lists the post types
102+
MSLS treats as translatable. Use it to check or iterate the supported post
103+
types from outside the plugin.
104+
105+
### msls_taxonomy
106+
107+
Returns the `ContentTypes\Taxonomy` singleton, which lists the taxonomies
108+
MSLS treats as translatable. Use it to check or iterate the supported
109+
taxonomies from outside the plugin.
110+
111+
## Object-level translation accessors
112+
113+
### msls_get_post
114+
115+
Returns the `Options\Post\Post` instance for a specific post ID. The object
116+
exposes the post's translation map — the IDs of the equivalent posts on
117+
other blogs — and helpers such as `get_permalink()` for individual
118+
languages.
119+
120+
### msls_get_tax
121+
122+
Returns the `Options\Tax\OptionsTaxInterface` instance for a specific term
123+
ID. Depending on the current query (category, tag, or custom taxonomy) the
124+
returned object is the most specific subclass available, so you can read
125+
the term's translations without doing the context detection yourself.
126+
127+
### msls_get_query
128+
129+
Returns the `Options\Query\Query` instance for the current archive request
130+
(day, month, year, author, or post type archive), or `null` when the current
131+
request is not an archive. Use it to get translation URLs for date- or
132+
archive-based pages, which `msls_get_permalink()` already wraps but which
133+
you might need to query in more detail.
134+
135+
## Internal helpers
136+
137+
### msls_return_void
138+
139+
A trivial no-op function. It exists so that callers (typically WordPress
140+
action registrations or fluent setups) can pass a callable that "does
141+
nothing" without inventing a closure. There's no end-user use case here —
142+
mentioned only for completeness.
143+
144+
## Deprecated functions
145+
146+
The following pre-2.10.1 names live in `includes/deprectated.php`. Each one
147+
still works but emits a `_deprecated_function()` notice and simply forwards
148+
to its modern `msls_*` replacement. Update calls in your code at your
149+
earliest convenience.
150+
151+
### get_the_msls
152+
153+
Deprecated since 2.10.1 — use `msls_get_switcher()`.
154+
155+
### the_msls
156+
157+
Deprecated since 2.10.1 — use `msls_the_switcher()`.
158+
159+
### get_msls_flag_url
160+
161+
Deprecated since 2.10.1 — use `msls_get_flag_url()`.
162+
163+
### get_msls_blog_description
164+
165+
Deprecated since 2.10.1 — use `msls_get_blog_description()`.
166+
167+
### get_msls_permalink
168+
169+
Deprecated since 2.10.1 — use `msls_get_permalink()`.

0 commit comments

Comments
 (0)