Skip to content

Commit 3264221

Browse files
committed
initial commit
0 parents  commit 3264221

File tree

12 files changed

+563
-0
lines changed

12 files changed

+563
-0
lines changed

.editorconfig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
indent_style = tab
6+
tab_width = 2
7+
end_of_line = lf
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false
14+
indent_style = space
15+
indent_size = 2
16+
17+
[*.php]
18+
tab_width = 4

.gitattributes

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.gitattributes export-ignore
2+
.gitignore export-ignore
3+
.editorconfig export-ignore
4+
composer.lock export-ignore
5+
.php-cs-fixer.php export-ignore
6+
7+
.cache/ export-ignore
8+
.github/ export-ignore

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.DS_Store
2+
*.cache
3+
composer.lock
4+
/vendor

.php-cs-fixer.php

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
$finder = PhpCsFixer\Finder::create()
4+
->exclude('vendor')
5+
->ignoreDotFiles(true)
6+
->in(__DIR__);
7+
8+
$config = new PhpCsFixer\Config();
9+
return $config
10+
->setUsingCache(true)
11+
->setCacheFile(__DIR__ . '/.cache/.php-cs-fixer.cache')
12+
->setRules([
13+
'@PSR12' => true,
14+
'align_multiline_comment' => ['comment_type' => 'phpdocs_only'],
15+
'array_indentation' => true,
16+
'array_syntax' => ['syntax' => 'short'],
17+
'assign_null_coalescing_to_coalesce_equal' => true,
18+
'blank_line_after_opening_tag' => true,
19+
'cast_spaces' => ['space' => 'none'],
20+
'combine_consecutive_issets' => true,
21+
'combine_consecutive_unsets' => true,
22+
'combine_nested_dirname' => true,
23+
'concat_space' => ['spacing' => 'one'],
24+
'declare_equal_normalize' => ['space' => 'single'],
25+
'dir_constant' => true,
26+
'explicit_string_variable' => true,
27+
'full_opening_tag' => true,
28+
'function_declaration' => ['closure_function_spacing' => 'one', 'closure_fn_spacing' => 'one'],
29+
'function_typehint_space' => true,
30+
'include' => true,
31+
'logical_operators' => true,
32+
'magic_constant_casing' => true,
33+
'magic_method_casing' => true,
34+
'method_chaining_indentation' => true,
35+
'modernize_types_casting' => true,
36+
'multiline_comment_opening_closing' => true,
37+
'native_function_casing' => true,
38+
'native_function_type_declaration_casing' => true,
39+
'new_with_braces' => true,
40+
'no_blank_lines_after_phpdoc' => true,
41+
'no_empty_comment' => true,
42+
'no_empty_phpdoc' => true,
43+
'no_empty_statement' => true,
44+
'no_leading_namespace_whitespace' => true,
45+
'no_mixed_echo_print' => ['use' => 'echo'],
46+
'no_short_bool_cast' => true,
47+
'no_superfluous_elseif' => true,
48+
'no_superfluous_phpdoc_tags' => ['allow_unused_params' => true],
49+
'no_unneeded_braces' => true,
50+
'no_unneeded_control_parentheses' => true,
51+
'no_unneeded_import_alias' => true,
52+
'no_unused_imports' => true,
53+
'no_useless_else' => true,
54+
'no_useless_nullsafe_operator' => true,
55+
'no_useless_return' => true,
56+
'no_whitespace_before_comma_in_array' => true,
57+
'nullable_type_declaration_for_default_null_value' => true,
58+
'nullable_type_declaration' => ['syntax' => 'union'],
59+
'object_operator_without_whitespace' => true,
60+
'operator_linebreak' => ['position' => 'end', 'only_booleans' => true],
61+
'ordered_imports' => ['sort_algorithm' => 'alpha'],
62+
'ordered_types' => ['sort_algorithm' => 'none', 'null_adjustment' => 'always_last'],
63+
'phpdoc_align' => ['align' => 'left'],
64+
'phpdoc_indent' => true,
65+
'phpdoc_param_order' => true,
66+
'phpdoc_scalar' => true,
67+
'phpdoc_trim' => true,
68+
'single_line_comment_style' => true,
69+
'single_quote' => true,
70+
'statement_indentation' => ['stick_comment_to_next_continuous_control_statement' => true],
71+
'ternary_to_null_coalescing' => true,
72+
'trim_array_spaces' => true,
73+
'whitespace_after_comma_in_array' => true
74+
])
75+
->setRiskyAllowed(true)
76+
->setIndent("\t")
77+
->setFinder($finder);

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024-present Tim Narr
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Kirby Helpers
2+
3+
Kirby Helpers is a collection of useful helper functions for Kirby CMS.
4+
5+
## Installation via Composer
6+
To install Kirby Helpers via Composer, run the following command:
7+
8+
```bash
9+
composer require timnarr/kirby-helpers
10+
```
11+
12+
## Options
13+
The following options are available for customization:
14+
15+
| Option | Default | Type | Description |
16+
| ------ | ------- | ---- | ----------- |
17+
| `vite.manifestPath` | `kirby()->root() . '/build/manifest.json'` | string | Path to vites manifest file to determine dev mode. Used by `isViteDevMode()` |
18+
19+
## Translations
20+
Translations are required for the labels returned by the `linkLabel()` function. This plugin provides translations for English and German. The following translation keys are available for customization:
21+
22+
| Key | Default |
23+
| --- | ------- |
24+
| `link_label_internal_home` | `Link to homepage: { title }` |
25+
| `link_label_internal` | `Link to page: { title }` |
26+
| `link_label_document` | `Download file: { filename }` |
27+
| `link_label_external` | `External link: { url } (Opens new tab)` |
28+
| `link_label_mail` | `Send email to: { mail } (Opens new window of your email program)` |
29+
| `link_label_tel` | `Call phone number: { tel } (Opens new window/program)` |
30+
31+
32+
## License
33+
Kirby Helpers is licensed under the [MIT License](./LICENSE). © 2024-present Tim Narr

composer.json

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"name": "timnarr/kirby-helpers",
3+
"description": "This plugin provides a collection of helper functions for Kirby",
4+
"type": "kirby-plugin",
5+
"version": "0.1.0",
6+
"license": "MIT",
7+
"homepage": "https://github.com/timnarr/kirby-helpers",
8+
"authors": [
9+
{
10+
"name": "Tim Narr",
11+
"email": "[email protected]",
12+
"homepage": "https://tim-narr.com"
13+
}
14+
],
15+
"require": {
16+
"php": "^8.2",
17+
"getkirby/composer-installer": "^1.2"
18+
},
19+
"require-dev": {
20+
"friendsofphp/php-cs-fixer": "^3",
21+
"getkirby/cms": "^4.0"
22+
23+
},
24+
"scripts": {
25+
"lint": ["php-cs-fixer fix --dry-run --diff"],
26+
"lint:fix": ["php-cs-fixer fix"]
27+
},
28+
"autoload": {
29+
"files": [
30+
"src/css.php",
31+
"src/ensure.php",
32+
"src/misc.php",
33+
"src/vite.php"
34+
]
35+
},
36+
"config": {
37+
"optimize-autoloader": true,
38+
"allow-plugins": {
39+
"getkirby/composer-installer": true
40+
}
41+
},
42+
"extra": {
43+
"kirby-cms-path": false
44+
}
45+
}

index.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
Kirby::plugin('timnarr/kirby-helpers', [
4+
'options' => [
5+
'vite' => [
6+
'manifestPath' => kirby()->root() . '/build/manifest.json',
7+
]
8+
],
9+
'translations' => [
10+
'en' => [
11+
'link_label_internal_home' => 'Link to homepage: { title }',
12+
'link_label_internal' => 'Link to page: { title }',
13+
'link_label_document' => 'Download file: { filename }',
14+
'link_label_external' => 'External link: { url } (Opens new tab)',
15+
'link_label_mail' => 'Send email to: { mail } (Opens new window of your email program)',
16+
'link_label_tel' => 'Call phone number: { tel } (Opens new window/program)',
17+
],
18+
'de' => [
19+
'link_label_internal_home' => 'Link zur Startseite: { title }',
20+
'link_label_internal' => 'Link zur Seite: { title }',
21+
'link_label_document' => 'Datei herunterladen: { filename }',
22+
'link_label_external' => 'Externer Link: { url } (Öffnet neuen Tab)',
23+
'link_label_mail' => 'E-Mail schreiben an: { mail } (Öffnet neues Fenster Ihres E-Mail Programms)',
24+
'link_label_tel' => 'Telefonnummer anrufen: { tel } (Öffnet neues Fenster/Programm)',
25+
]
26+
]
27+
]);

src/css.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
use Kirby\Cms\Html;
4+
5+
/**
6+
* Load a CSS file lazily.
7+
*
8+
* @param string $file The CSS file path.
9+
* @param bool $omitNoscript Optional. If true, omit the noscript fallback.
10+
*
11+
* @example
12+
* cssLazy('assets/css/carousel.css', true);
13+
*
14+
* @example with vite
15+
* cssLazy(vite()->asset('styles/carousel.scss'), true);
16+
*/
17+
if (!function_exists('cssLazy')) {
18+
function cssLazy(string $file, bool $omitNoscript = false): void
19+
{
20+
echo Html::css($file, [
21+
'as' => 'style',
22+
'rel' => 'preload',
23+
'fetchpriority' => 'low',
24+
'onload' => "this.onload=null;this.rel='stylesheet'",
25+
]);
26+
27+
if (!$omitNoscript) {
28+
echo '<noscript>' . Html::css($file) . '</noscript>';
29+
}
30+
}
31+
}
32+
33+
34+
/**
35+
* Load CSS file only if a defined block is used.
36+
*
37+
* @param string $file The CSS file path.
38+
* @param string $blockType The block type to check.
39+
* @param array $usedBlockTypes The array of used block types.
40+
* @param bool $lazy Optional. If true, load the CSS file lazily.
41+
*
42+
* @example
43+
* cssIfBlock('assets/css/carousel.css', 'carousel', $pageBlocks, true);
44+
*
45+
* @example with vite
46+
* cssIfBlock(vite()->asset('styles/carousel.scss'), 'carousel', $pageBlocks, true);
47+
*/
48+
if (!function_exists('cssIfBlock')) {
49+
function cssIfBlock(string $file, string $blockType, array $usedBlockTypes, bool $lazy = false): void
50+
{
51+
if (in_array($blockType, $usedBlockTypes)) {
52+
if ($lazy) {
53+
cssLazy($file);
54+
} else {
55+
Html::css($file);
56+
}
57+
}
58+
}
59+
}
60+
61+
62+
/**
63+
* Load a CSS file only for a defined page template or an array of templates.
64+
*
65+
* @param string $file The CSS file path.
66+
* @param string|array $template The page template name or an array of template names.
67+
* @param bool $lazy Optional. If true, load the CSS file lazily.
68+
*/
69+
if (!function_exists('cssIfTemplate')) {
70+
function cssIfTemplate(string $file, string|array $template, bool $lazy = false): void
71+
{
72+
$templates = (array)$template;
73+
$currentTemplate = page()->template();
74+
75+
if (in_array($currentTemplate, $templates, true)) {
76+
if ($lazy) {
77+
cssLazy($file);
78+
} else {
79+
Html::css($file);
80+
}
81+
}
82+
}
83+
}

src/ensure.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/**
4+
* Ensure that a string starts with the specified prefix.
5+
*
6+
* @param string $string The input string.
7+
* @param string $prefix The prefix to ensure.
8+
* @return string The string starting with the prefix.
9+
*/
10+
if (!function_exists('ensureLeft')) {
11+
function ensureLeft(string $string, string $prefix): string
12+
{
13+
return str_starts_with($string, $prefix) ? $string : $prefix . $string;
14+
}
15+
}
16+
17+
18+
/**
19+
* Ensure that a string ends with the specified suffix.
20+
*
21+
* @param string $string The input string.
22+
* @param string $suffix The suffix to ensure.
23+
* @return string The string ending with the suffix.
24+
*/
25+
if (!function_exists('ensureRight')) {
26+
function ensureRight(string $string, string $suffix): string
27+
{
28+
return str_ends_with($string, $suffix) ? $string : $string . $suffix;
29+
}
30+
}
31+
32+
33+
/**
34+
* Ensure that a string starts with a hash character (#).
35+
* Mainly used to ensure jump-to ids start with a #.
36+
*
37+
* @param string $string The input string.
38+
* @return string The string starting with a hash.
39+
*/
40+
if (!function_exists('ensureHashed')) {
41+
function ensureHashed(string $string): string
42+
{
43+
return ensureLeft($string, '#');
44+
}
45+
}

0 commit comments

Comments
 (0)