Initial commit

This commit is contained in:
isUnknown 2026-02-12 15:22:46 +01:00
commit 65e0da7e11
1397 changed files with 596542 additions and 0 deletions

View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 brocessing
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View file

@ -0,0 +1,119 @@
<h1 align="center">Kirby UpdateID</h1>
<h3 align="center">Automatically update references to a page id</h3>
<div align="center">
<img alt="version" src="https://img.shields.io/badge/version-0.1.1-green.svg?style=flat-square"/>
<img alt="kirby_version" src="https://img.shields.io/badge/kirby-2.3+-red.svg?style=flat-square"/>
<img alt="license" src="https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square"/>
<br>
<br>
</div>
<br>
<br>
## About
- Sometimes you need to reference pages to each other, usually by the page ID
- When you update a page URI, the ID can change and references to this page from other page/fields can be lost.
- This plugin automatically updates selected fields when a referenced ID change.
<br>
## Installation
Use one of the alternatives below.
### 1. Using [`kirby-webpack`](https://github.com/brocessing/kirby-webpack)
Simply use the built-in **Kirby Package Manager** by running:
```sh
$ npm run kirby:add
$ [?] Git URL: https://github.com/brocessing/kirby-updateid
$ [?] Module name: updateid
$ [?] Category: plugins
```
### 2. Kirby CLI
If you are using the [Kirby CLI](https://github.com/getkirby/cli) you can install this plugin by running the following commands in your shell:
```sh
$ cd path/to/kirby
$ kirby plugin:install brocessing/kirby-updateid
```
### 3. Clone or download
1. [Clone](https://github.com/brocessing/kirby-updateid.git) or [download](https://github.com/brocessing/kirby-updateid/archive/master.zip) this repository.
2. Unzip the archive if needed and rename the folder to `updateid`.
**Make sure that the plugin folder structure looks like this:**
```text
site/plugins/updateid/
```
### 4. Git Submodule
If you know your way around Git, you can download this plugin as a submodule:
```sh
$ cd path/to/kirby
$ git submodule add https://github.com/brocessing/kirby-updateid site/plugins/updateid
```
<br>
## Setup & Usage
**Use `c::set('plugin.updateid', array $config)` to specify which fields can be updated.**
##### Basic configuration:
```php
c::set('plugin.updateid', array(
// On the homepage, page ids from the field featured_works will be auto-updated
array(
'pages' => 'home',
'fields' => 'featured_works'
),
// You can add other pages
// And use arrays to specify multiple pages & multiple fields to update
array(
'pages' => ['about', 'contact'],
'fields' => ['emails', 'authors']
)
));
```
##### You can also use a function to select a collection of pages
```php
c::set('plugin.updateid', array(
// Auto-update client ID on each project page
array(
'pages' => function () { return site()->find('work')->children(); },
'fields' => 'client'
)
));
```
<br>
## Requirements
- [**Kirby**](https://getkirby.com/) 2.3+
<br>
## Disclaimer
This field is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please [create a new issue](https://github.com/brocessing/kirby-updateid/issues/new).
<br>
## License
[MIT](https://opensource.org/licenses/MIT)
It is discouraged to use this field in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

View file

@ -0,0 +1,12 @@
{
"name": "updateid",
"description": "Kirby plugin to automatically update references to a page id",
"author": "Brocessing <https://github.com/brocessing>",
"repository": {
"type": "git",
"url": "https://github.com/brocessing/kirby-previewfiles"
},
"version": "0.1.1",
"type": "kirby-plugin",
"license": "MIT"
}

View file

@ -0,0 +1,66 @@
<?php
kirby()->hook('panel.page.move', function($page, $oldPage) {
$config = c::get('plugin.updateid');
if (!is_array($config)) return;
$oldId = $oldPage->id();
$regex = '/([^a-z0-9-]|^)' . preg_quote($oldId, '/') . '([^a-z0-9-]|$)/m';
$newId = $page->id();
if ($oldId === $newId) return;
foreach($config as $e) {
if (
!is_array($e) ||
!isset($e['fields']) ||
!isset($e['pages']) ||
(!is_string($e['fields']) && !is_array($e['fields']))
) continue;
$fields = is_array($e['fields']) ? $e['fields'] : array($e['fields']);
$targetpages = (is_callable($e['pages']) && !is_string($e['pages']))
? $e['pages']()
: $e['pages'];
$targets = is_array($targetpages) ? pages($targetpages) : pages(array($targetpages));
$targets->first();
// Iterate on each page target by the updateid configuration
while ($targets->current()) {
$target = $targets->current();
// If the site is not multilang we still initialize an array for convenience
$isMultilang = site()->multilang();
$languages = $isMultilang
? array_values(array_map(function($lang) { return $lang->code(); }, site()->languages()->toArray()))
: array(NULL);
// Iterate on each language
foreach($languages as $lang) {
$data = array();
foreach($fields as $fieldName) {
if (!$target->{$fieldName}()->exists() || $target->{$fieldName}()->empty()) continue;
$fieldValue = $isMultilang
? $target->content($lang)->{$fieldName}()->value()
: $target->{$fieldName}()->value();
if (!preg_match_all($regex, $fieldValue)) continue;
$data[$fieldName] = preg_replace($regex, "$1".$newId."$2", $fieldValue);
// Used for debug
// $message = "\n" . 'Update:';
// $message .= "\n" . ' Page: ' . $target->id();
// $message .= "\n" . ' Field: ' . $fieldName;
// $message .= "\n" . ' Lang: ' . $lang;
// $message .= "\n" . ' From: ' . $fieldValue;
// $message .= "\n" . ' To: ' . $data[$fieldName];
// error_log(var_export($message, true));
}
if (count($data) > 0) {
if ($isMultilang) $target->update($data, $lang);
else $target->update($data);
}
}
$targets->next();
}
}
});