API / Minn\Runtime / Shortcodes

Shortcodes final class

Minn\Runtime\Shortcodes · public/minn/src/Minn/Runtime/Shortcodes.php · 274 lines

The shortcode registry plugin code fills with add_shortcode, and the expansion do_shortcode performs: [tag attrs], [tag attrs/], [tag]…[/tag] (the first closing tag wins; content is not expanded again), [[tag]] as the literal, unregistered tags left as written.

Used by 1

Constants and properties

constATTRIBUTES = '/([\\w-]+)\\s*=\\s*"([^"]*)"(?:\\s|$)|([\\w-]+)\\s*=\\s*\'([^\']*)\'(?:\\s|$)|([\\w-]+)\\s*=\\s*([^\\s\'"]+)(?:\\s|$)|"([^"]*)"(?:\\s|$)|\'([^\']*)\'(?:\\s|$)|(\\S+)(?:\\s|$)/'

The attribute pattern shortcode_parse_atts reads by (get_shortcode_atts_regex).

privateSET_ASIDE = array ( '[' => '[', ']' => ']', )
privateRESTORE = array ( '[' => '[', ']' => ']', )
privatearray $tags

Public

methodtags(): array#

The registry itself, by reference, so the $shortcode_tags global plugin code reads and copies is this array and not a snapshot of it.

@return array<string, callable>
methodadd(string $tag, callable $callback): void#

Registers a shortcode.

methodremove(string $tag): void#

Forgets a shortcode.

methodremoveAll(): void#

Forgets every shortcode.

methodhas(string $tag): bool#

Whether a shortcode is registered.

methodall(): array#

Every shortcode with its callback.

@return array<string, callable> every registered tag and its handler, to restore after a narrowed run
methodrestore(array $tags): void#

Replaces the registry, after a save-and-restore.

$tagsarray<string, callable>
methodnames(): array#

Every shortcode name.

@return list<string>
methodpattern(?array $tags = NULL): ?string#

The regex matching the registered shortcodes, or null for none.

methodapply(string $content, string $tags = 'run'): string#

Content with its shortcodes run (do_shortcode, probe shortcode-run): only the registered names the text holds; inside HTML tags first (inTags: run, or with 'escape' as with ignore_html, left alone), then in the text, each through run(); the brackets set aside as entities come back at the end. Images rendered meanwhile see the do_shortcode context.

methodrun(array $m): string#

One matched shortcode (do_shortcode_tag): [[tag]] gives the tag as written; otherwise pre_do_shortcode_tag may answer, or the callback runs with the attributes, the content ("" when it closes itself) and the name, and do_shortcode_tag sees the output in its brackets.

$marray<int, string>the pattern's match
methodinTags(string $content, string $tags, array $names): string#

Shortcodes inside HTML tags (do_shortcodes_in_html_tags), run before the text: a quoted attribute value's shortcode runs and the attribute is judged again by kses, kept when anything is left; an unquoted one (or one standing as a name or as the tag) runs as written. Comments, CDATA and, with 'escape', every tag are left alone. Brackets left in a tag (and entities already written for them) are set aside so the text pass cannot reach them.

$nameslist<string>
methodstrip(string $content): string#

Content with its shortcodes taken out (strip_shortcodes): the names strip_shortcodes_tagnames leaves, never inside a tag; [[tag]] gives the tag as written.

staticstripped(array $m): string#

One matched shortcode taken out (strip_shortcode_tag): its outer brackets stay, [[tag]] gives the tag. @param array<int, string> $m

$marray<int, string>
staticparse(string $text): array#

Shortcode attribute text as an array (shortcode_parse_atts): no-break and zero-width spaces read as spaces; name=value pairs (names lower case) and bare values in order, each a token followed by whitespace or the end, backslash escapes undone; a value holding "<" that is not whole tags becomes "".

@return array<int|string, string>

Internals 2

Private and protected methods: how the class does its work. Not a contract; they change whenever the code is tidied.

privatepresent(string $content, array $names): array#

The names among those given that the text opens a bracket with.

$nameslist<string>
@return list<string>
privateattributesRun(array $parts, Closure $run): string#

A tag's attributes with their shortcodes run, from elementParts().

$partslist<string>