xiaowang/kirby/config/helpers.php

685 lines
17 KiB
PHP
Raw Normal View History

2021-10-29 18:05:46 +02:00
<?php
use Kirby\Cms\App;
2023-04-14 16:30:28 +02:00
use Kirby\Cms\File;
2022-08-31 16:08:03 +02:00
use Kirby\Cms\Helpers;
2021-10-29 18:05:46 +02:00
use Kirby\Cms\Html;
2023-04-14 16:30:28 +02:00
use Kirby\Cms\Page;
use Kirby\Cms\Pages;
2022-12-19 16:26:24 +01:00
use Kirby\Cms\Response;
2023-04-14 16:30:28 +02:00
use Kirby\Cms\Site;
2021-10-29 18:05:46 +02:00
use Kirby\Cms\Url;
2021-11-18 17:44:47 +01:00
use Kirby\Filesystem\Asset;
use Kirby\Filesystem\F;
use Kirby\Http\Router;
2023-04-14 16:30:28 +02:00
use Kirby\Template\Slot;
use Kirby\Template\Snippet;
2022-03-22 15:39:39 +01:00
use Kirby\Toolkit\Date;
2021-10-29 18:05:46 +02:00
use Kirby\Toolkit\I18n;
use Kirby\Toolkit\Str;
use Kirby\Toolkit\V;
2022-08-31 16:08:03 +02:00
if (Helpers::hasOverride('asset') === false) { // @codeCoverageIgnore
/**
* Helper to create an asset object
*/
2023-04-14 16:30:28 +02:00
function asset(string $path): Asset
2022-08-31 16:08:03 +02:00
{
return new Asset($path);
}
}
if (Helpers::hasOverride('attr') === false) { // @codeCoverageIgnore
/**
* Generates a list of HTML attributes
*
* @param array|null $attr A list of attributes as key/value array
* @param string|null $before An optional string that will be prepended if the result is not empty
* @param string|null $after An optional string that will be appended if the result is not empty
*/
2023-04-14 16:30:28 +02:00
function attr(
array|null $attr = null,
string|null $before = null,
string|null $after = null
): string|null {
2022-08-31 16:08:03 +02:00
return Html::attr($attr, null, $before, $after);
}
}
if (Helpers::hasOverride('collection') === false) { // @codeCoverageIgnore
/**
* Returns the result of a collection by name
2024-12-20 12:37:35 +01:00
*
* @return \Kirby\Toolkit\Collection|null
* @todo 5.0 Add return type declaration
2022-08-31 16:08:03 +02:00
*/
2024-12-20 12:37:35 +01:00
function collection(string $name, array $options = [])
2022-08-31 16:08:03 +02:00
{
2024-12-20 12:37:35 +01:00
return App::instance()->collection($name, $options);
2022-08-31 16:08:03 +02:00
}
}
if (Helpers::hasOverride('csrf') === false) { // @codeCoverageIgnore
/**
* Checks / returns a CSRF token
*
* @param string|null $check Pass a token here to compare it to the one in the session
* @return string|bool Either the token or a boolean check result
*/
2023-04-14 16:30:28 +02:00
function csrf(string|null $check = null): string|bool
2022-08-31 16:08:03 +02:00
{
// check explicitly if there have been no arguments at all;
// checking for null introduces a security issue because null could come
// from user input or bugs in the calling code!
if (func_num_args() === 0) {
return App::instance()->csrf();
}
return App::instance()->csrf($check);
}
}
if (Helpers::hasOverride('css') === false) { // @codeCoverageIgnore
/**
* Creates one or multiple CSS link tags
*
* @param string|array $url Relative or absolute URLs, an array of URLs or `@auto` for automatic template css loading
2023-04-14 16:30:28 +02:00
* @param string|array|null $options Pass an array of attributes for the link tag or a media attribute string
2022-08-31 16:08:03 +02:00
*/
2023-04-14 16:30:28 +02:00
function css(
string|array $url,
string|array|null $options = null
): string|null {
2022-08-31 16:08:03 +02:00
return Html::css($url, $options);
}
}
if (Helpers::hasOverride('deprecated') === false) { // @codeCoverageIgnore
/**
* Triggers a deprecation warning if debug mode is active
* @since 3.3.0
*
* @return bool Whether the warning was triggered
*/
function deprecated(string $message): bool
{
return Helpers::deprecated($message);
}
}
if (Helpers::hasOverride('dump') === false) { // @codeCoverageIgnore
/**
* Simple object and variable dumper
* to help with debugging.
*/
2023-04-14 16:30:28 +02:00
function dump(mixed $variable, bool $echo = true): string
2022-08-31 16:08:03 +02:00
{
return Helpers::dump($variable, $echo);
}
}
if (Helpers::hasOverride('e') === false) { // @codeCoverageIgnore
/**
* Smart version of echo with an if condition as first argument
*
* @param mixed $value The string to be echoed if the condition is true
* @param mixed $alternative An alternative string which should be echoed when the condition is false
*/
2023-04-14 16:30:28 +02:00
function e(mixed $condition, mixed $value, mixed $alternative = null): void
2022-08-31 16:08:03 +02:00
{
echo $condition ? $value : $alternative;
}
}
2023-04-14 16:30:28 +02:00
if (Helpers::hasOverride('endslot') === false) { // @codeCoverageIgnore
/**
* Ends the last started template slot
*/
function endslot(): void
{
Slot::end();
}
}
if (Helpers::hasOverride('endsnippet') === false) { // @codeCoverageIgnore
/**
* Renders the currently active snippet with slots
*/
function endsnippet(): void
{
Snippet::end();
}
}
2022-08-31 16:08:03 +02:00
if (Helpers::hasOverride('esc') === false) { // @codeCoverageIgnore
/**
* Escape context specific output
*
* @param string $string Untrusted data
* @param string $context Location of output (`html`, `attr`, `js`, `css`, `url` or `xml`)
* @return string Escaped data
*/
function esc(string $string, string $context = 'html'): string
{
return Str::esc($string, $context);
}
}
if (Helpers::hasOverride('get') === false) { // @codeCoverageIgnore
/**
* Shortcut for $kirby->request()->get()
*
* @param mixed $key The key to look for. Pass false or null to return the entire request array.
* @param mixed $default Optional default value, which should be returned if no element has been found
*/
2023-04-14 16:30:28 +02:00
function get(mixed $key = null, mixed $default = null): mixed
2022-08-31 16:08:03 +02:00
{
return App::instance()->request()->get($key, $default);
}
}
if (Helpers::hasOverride('gist') === false) { // @codeCoverageIgnore
/**
* Embeds a Github Gist
*/
2022-12-19 16:26:24 +01:00
function gist(string $url, string|null $file = null): string
2022-08-31 16:08:03 +02:00
{
return App::instance()->kirbytag([
'gist' => $url,
'file' => $file,
]);
}
}
if (Helpers::hasOverride('go') === false) { // @codeCoverageIgnore
/**
* Redirects to the given Urls
* Urls can be relative or absolute.
*/
2025-01-12 19:21:26 +01:00
function go(string $url = '/', int $code = 302): never
2022-08-31 16:08:03 +02:00
{
Response::go($url, $code);
}
}
if (Helpers::hasOverride('h') === false) { // @codeCoverageIgnore
/**
* Shortcut for html()
*
* @param string|null $string unencoded text
*/
2022-12-19 16:26:24 +01:00
function h(string|null $string, bool $keepTags = false): string
2022-08-31 16:08:03 +02:00
{
return Html::encode($string, $keepTags);
}
}
if (Helpers::hasOverride('html') === false) { // @codeCoverageIgnore
/**
* Creates safe html by encoding special characters
*
* @param string|null $string unencoded text
*/
2022-12-19 16:26:24 +01:00
function html(string|null $string, bool $keepTags = false): string
2022-08-31 16:08:03 +02:00
{
return Html::encode($string, $keepTags);
}
}
if (Helpers::hasOverride('image') === false) { // @codeCoverageIgnore
/**
* Return an image from any page
* specified by the path
*
* Example:
* <?= image('some/page/myimage.jpg') ?>
*/
2023-04-14 16:30:28 +02:00
function image(string|null $path = null): File|null
2022-08-31 16:08:03 +02:00
{
return App::instance()->image($path);
}
}
if (Helpers::hasOverride('invalid') === false) { // @codeCoverageIgnore
/**
* Runs a number of validators on a set of data and checks if the data is invalid
*/
2023-04-14 16:30:28 +02:00
function invalid(
array $data = [],
array $rules = [],
array $messages = []
): array {
2022-08-31 16:08:03 +02:00
return V::invalid($data, $rules, $messages);
}
}
if (Helpers::hasOverride('js') === false) { // @codeCoverageIgnore
/**
* Creates a script tag to load a javascript file
*/
2023-04-14 16:30:28 +02:00
function js(
string|array $url,
string|array|bool|null $options = null
): string|null {
2022-08-31 16:08:03 +02:00
return Html::js($url, $options);
}
}
if (Helpers::hasOverride('kirby') === false) { // @codeCoverageIgnore
/**
* Returns the Kirby object in any situation
*/
2023-04-14 16:30:28 +02:00
function kirby(): App
2022-08-31 16:08:03 +02:00
{
return App::instance();
}
}
if (Helpers::hasOverride('kirbytag') === false) { // @codeCoverageIgnore
/**
* Makes it possible to use any defined Kirbytag as standalone function
*/
2023-04-14 16:30:28 +02:00
function kirbytag(
string|array $type,
string|null $value = null,
array $attr = [],
array $data = []
): string {
2022-08-31 16:08:03 +02:00
return App::instance()->kirbytag($type, $value, $attr, $data);
}
}
if (Helpers::hasOverride('kirbytags') === false) { // @codeCoverageIgnore
/**
* Parses KirbyTags in the given string. Shortcut
* for `$kirby->kirbytags($text, $data)`
*/
2022-12-19 16:26:24 +01:00
function kirbytags(string|null $text = null, array $data = []): string
2022-08-31 16:08:03 +02:00
{
return App::instance()->kirbytags($text, $data);
}
}
if (Helpers::hasOverride('kirbytext') === false) { // @codeCoverageIgnore
/**
* Parses KirbyTags and Markdown in the
* given string. Shortcut for `$kirby->kirbytext()`
*/
2022-12-19 16:26:24 +01:00
function kirbytext(string|null $text = null, array $data = []): string
2022-08-31 16:08:03 +02:00
{
return App::instance()->kirbytext($text, $data);
}
}
if (Helpers::hasOverride('kirbytextinline') === false) { // @codeCoverageIgnore
/**
* Parses KirbyTags and inline Markdown in the
* given string.
* @since 3.1.0
*/
2022-12-19 16:26:24 +01:00
function kirbytextinline(string|null $text = null, array $options = []): string
2022-08-31 16:08:03 +02:00
{
$options['markdown']['inline'] = true;
return App::instance()->kirbytext($text, $options);
}
}
if (Helpers::hasOverride('kt') === false) { // @codeCoverageIgnore
/**
* Shortcut for `kirbytext()` helper
*/
2022-12-19 16:26:24 +01:00
function kt(string|null $text = null, array $data = []): string
2022-08-31 16:08:03 +02:00
{
return App::instance()->kirbytext($text, $data);
}
}
if (Helpers::hasOverride('kti') === false) { // @codeCoverageIgnore
/**
* Shortcut for `kirbytextinline()` helper
* @since 3.1.0
*/
2022-12-19 16:26:24 +01:00
function kti(string|null $text = null, array $options = []): string
2022-08-31 16:08:03 +02:00
{
$options['markdown']['inline'] = true;
return App::instance()->kirbytext($text, $options);
}
}
if (Helpers::hasOverride('load') === false) { // @codeCoverageIgnore
/**
* A super simple class autoloader
*/
2022-12-19 16:26:24 +01:00
function load(array $classmap, string|null $base = null): void
2022-08-31 16:08:03 +02:00
{
F::loadClasses($classmap, $base);
}
}
if (Helpers::hasOverride('markdown') === false) { // @codeCoverageIgnore
/**
* Parses markdown in the given string. Shortcut for
* `$kirby->markdown($text)`
*/
2022-12-19 16:26:24 +01:00
function markdown(string|null $text = null, array $options = []): string
2022-08-31 16:08:03 +02:00
{
return App::instance()->markdown($text, $options);
}
}
if (Helpers::hasOverride('option') === false) { // @codeCoverageIgnore
/**
* Shortcut for `$kirby->option($key, $default)`
*/
2023-04-14 16:30:28 +02:00
function option(string $key, mixed $default = null): mixed
2022-08-31 16:08:03 +02:00
{
return App::instance()->option($key, $default);
}
}
if (Helpers::hasOverride('page') === false) { // @codeCoverageIgnore
/**
* Fetches a single page by id or
* the current page when no id is specified
*/
2023-04-14 16:30:28 +02:00
function page(string|null $id = null): Page|null
2022-08-31 16:08:03 +02:00
{
if (empty($id) === true) {
return App::instance()->site()->page();
}
return App::instance()->site()->find($id);
}
}
if (Helpers::hasOverride('pages') === false) { // @codeCoverageIgnore
/**
* Helper to build pages collection
*/
2023-04-14 16:30:28 +02:00
function pages(string|array ...$id): Pages|null
2022-08-31 16:08:03 +02:00
{
// ensure that a list of string arguments and an array
// as the first argument are treated the same
if (count($id) === 1 && is_array($id[0]) === true) {
$id = $id[0];
}
// always passes $id an array; ensures we get a
// collection even if only one ID is passed
return App::instance()->site()->find($id);
}
}
if (Helpers::hasOverride('param') === false) { // @codeCoverageIgnore
/**
* Returns a single param from the URL
*
2022-12-19 16:26:24 +01:00
* @psalm-return ($fallback is string ? string : string|null)
2022-08-31 16:08:03 +02:00
*/
2022-12-19 16:26:24 +01:00
function param(string $key, string|null $fallback = null): string|null
2022-08-31 16:08:03 +02:00
{
return App::instance()->request()->url()->params()->$key ?? $fallback;
}
}
if (Helpers::hasOverride('params') === false) { // @codeCoverageIgnore
/**
* Returns all params from the current Url
*/
function params(): array
{
return App::instance()->request()->url()->params()->toArray();
}
}
if (Helpers::hasOverride('r') === false) { // @codeCoverageIgnore
/**
* Smart version of return with an if condition as first argument
*
* @param mixed $value The string to be returned if the condition is true
* @param mixed $alternative An alternative string which should be returned when the condition is false
*/
2023-04-14 16:30:28 +02:00
function r(mixed $condition, mixed $value, mixed $alternative = null): mixed
2022-08-31 16:08:03 +02:00
{
return $condition ? $value : $alternative;
}
}
if (Helpers::hasOverride('router') === false) { // @codeCoverageIgnore
/**
* Creates a micro-router and executes
* the routing action immediately
* @since 3.6.0
*/
2023-04-14 16:30:28 +02:00
function router(
string|null $path = null,
string $method = 'GET',
array $routes = [],
Closure|null $callback = null
): mixed {
2022-08-31 16:08:03 +02:00
return Router::execute($path, $method, $routes, $callback);
}
}
if (Helpers::hasOverride('site') === false) { // @codeCoverageIgnore
/**
* Returns the current site object
*/
2023-04-14 16:30:28 +02:00
function site(): Site
2022-08-31 16:08:03 +02:00
{
return App::instance()->site();
}
}
if (Helpers::hasOverride('size') === false) { // @codeCoverageIgnore
/**
* Determines the size/length of numbers, strings, arrays and countable objects
*/
2023-04-14 16:30:28 +02:00
function size(mixed $value): int
2022-08-31 16:08:03 +02:00
{
return Helpers::size($value);
}
}
2023-04-14 16:30:28 +02:00
if (Helpers::hasOverride('slot') === false) { // @codeCoverageIgnore
/**
* Starts a new template slot
*/
function slot(string $name = 'default'): void
{
Slot::begin($name);
}
}
2022-08-31 16:08:03 +02:00
if (Helpers::hasOverride('smartypants') === false) { // @codeCoverageIgnore
/**
* Enhances the given string with
* smartypants. Shortcut for `$kirby->smartypants($text)`
*/
2022-12-19 16:26:24 +01:00
function smartypants(string|null $text = null): string
2022-08-31 16:08:03 +02:00
{
return App::instance()->smartypants($text);
}
}
if (Helpers::hasOverride('snippet') === false) { // @codeCoverageIgnore
/**
* Embeds a snippet from the snippet folder
*/
2023-04-14 16:30:28 +02:00
function snippet(
$name,
$data = [],
bool $return = false,
bool $slots = false
): Snippet|string|null {
return App::instance()->snippet($name, $data, $return, $slots);
2022-08-31 16:08:03 +02:00
}
}
if (Helpers::hasOverride('svg') === false) { // @codeCoverageIgnore
/**
* Includes an SVG file by absolute or
* relative file path.
*/
2023-04-14 16:30:28 +02:00
function svg(string|File $file): string|false
2022-08-31 16:08:03 +02:00
{
return Html::svg($file);
}
}
if (Helpers::hasOverride('t') === false) { // @codeCoverageIgnore
/**
* Returns translate string for key from translation file
*/
2023-04-14 16:30:28 +02:00
function t(
string|array $key,
string|null $fallback = null,
string|null $locale = null
): string|array|Closure|null {
2022-08-31 16:08:03 +02:00
return I18n::translate($key, $fallback, $locale);
}
}
if (Helpers::hasOverride('tc') === false) { // @codeCoverageIgnore
/**
* Translates a count
*
* @param bool $formatNumber If set to `false`, the count is not formatted
*/
function tc(
string $key,
int $count,
2023-04-14 16:30:28 +02:00
string|null $locale = null,
2022-08-31 16:08:03 +02:00
bool $formatNumber = true
2023-04-14 16:30:28 +02:00
): mixed {
2022-08-31 16:08:03 +02:00
return I18n::translateCount($key, $count, $locale, $formatNumber);
}
}
if (Helpers::hasOverride('timestamp') === false) { // @codeCoverageIgnore
/**
* Rounds the minutes of the given date
* by the defined step
*
* @param int|array|null $step array of `unit` and `size` to round to nearest
*/
2023-04-14 16:30:28 +02:00
function timestamp(
string|null $date = null,
int|array|null $step = null
): int|null {
2022-08-31 16:08:03 +02:00
return Date::roundedTimestamp($date, $step);
}
}
if (Helpers::hasOverride('tt') === false) { // @codeCoverageIgnore
/**
* Translate by key and then replace
* placeholders in the text
*/
2023-04-14 16:30:28 +02:00
function tt(
string $key,
string|array|null $fallback = null,
array|null $replace = null,
string|null $locale = null
): string {
2022-08-31 16:08:03 +02:00
return I18n::template($key, $fallback, $replace, $locale);
}
}
if (Helpers::hasOverride('twitter') === false) { // @codeCoverageIgnore
/**
* Builds a Twitter link
*/
2023-04-14 16:30:28 +02:00
function twitter(
string $username,
string|null $text = null,
string|null $title = null,
string|null $class = null
): string {
2022-08-31 16:08:03 +02:00
return App::instance()->kirbytag([
'twitter' => $username,
'text' => $text,
'title' => $title,
'class' => $class
]);
}
}
if (Helpers::hasOverride('u') === false) { // @codeCoverageIgnore
/**
* Shortcut for url()
*/
2023-04-14 16:30:28 +02:00
function u(
string|null $path = null,
array|string|null $options = null
): string {
2022-08-31 16:08:03 +02:00
return Url::to($path, $options);
}
}
if (Helpers::hasOverride('url') === false) { // @codeCoverageIgnore
/**
* Builds an absolute URL for a given path
*/
2023-04-14 16:30:28 +02:00
function url(
string|null $path = null,
array|string|null $options = null
): string {
2022-08-31 16:08:03 +02:00
return Url::to($path, $options);
}
}
if (Helpers::hasOverride('uuid') === false) { // @codeCoverageIgnore
/**
* Creates a compliant v4 UUID
*/
function uuid(): string
{
return Str::uuid();
}
}
if (Helpers::hasOverride('video') === false) { // @codeCoverageIgnore
/**
* Creates a video embed via iframe for Youtube or Vimeo
* videos. The embed Urls are automatically detected from
* the given Url.
*/
2022-12-19 16:26:24 +01:00
function video(string $url, array $options = [], array $attr = []): string|null
2022-08-31 16:08:03 +02:00
{
return Html::video($url, $options, $attr);
}
}
if (Helpers::hasOverride('vimeo') === false) { // @codeCoverageIgnore
/**
* Embeds a Vimeo video by URL in an iframe
*/
2022-12-19 16:26:24 +01:00
function vimeo(string $url, array $options = [], array $attr = []): string|null
2022-08-31 16:08:03 +02:00
{
return Html::vimeo($url, $options, $attr);
}
}
if (Helpers::hasOverride('widont') === false) { // @codeCoverageIgnore
/**
* The widont function makes sure that there are no
* typographical widows at the end of a paragraph
* that's a single word in the last line
*/
2023-04-14 16:30:28 +02:00
function widont(string|null $string = null): string
2022-08-31 16:08:03 +02:00
{
return Str::widont($string);
}
}
if (Helpers::hasOverride('youtube') === false) { // @codeCoverageIgnore
/**
* Embeds a Youtube video by URL in an iframe
*/
2022-12-19 16:26:24 +01:00
function youtube(string $url, array $options = [], array $attr = []): string|null
2022-08-31 16:08:03 +02:00
{
return Html::youtube($url, $options, $attr);
}
2021-10-29 18:05:46 +02:00
}