1<?php /** @noinspection SpellCheckingInspection */
2
3use ComboStrap\Event;
4use ComboStrap\MarkupCacheDependencies;
5use ComboStrap\Meta\Field\PageH1;
6use ComboStrap\MetadataMutation;
7use ComboStrap\PageDescription;
8use ComboStrap\PagePath;
9use ComboStrap\PageTitle;
10use ComboStrap\ResourceName;
11
12
13require_once(__DIR__ . '/../ComboStrap/PluginUtility.php');
14
15/**
16 * Delete the cache
17 */
18class action_plugin_combo_pageprimarymetamutation extends DokuWiki_Action_Plugin
19{
20
21
22    public const PRIMARY_META_MUTATION_EVENT_NAME = 'page_primary_meta_mutation';
23
24    public const PRIMARY_METAS = [PageTitle::PROPERTY_NAME, ResourceName::PROPERTY_NAME, PageH1::PROPERTY_NAME, PageDescription::DESCRIPTION_PROPERTY];
25
26
27    public function register(Doku_Event_Handler $controller)
28    {
29
30        /**
31         * create the async event
32         */
33        $controller->register_hook(MetadataMutation::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'createPrimaryMetaMutation', array());
34
35        /**
36         * process the Async event
37         */
38        $controller->register_hook(self::PRIMARY_META_MUTATION_EVENT_NAME, 'AFTER', $this, 'handlePrimaryMetaMutation');
39
40    }
41
42    function createPrimaryMetaMutation($event)
43    {
44
45        $data = $event->data;
46
47        /**
48         * The slot cache are re-rendered only when the
49         * below property are updated
50         */
51        if (!in_array($data["name"], self::PRIMARY_METAS)) return;
52
53        Event::createEvent(
54            self::PRIMARY_META_MUTATION_EVENT_NAME,
55            $data
56        );
57
58
59    }
60
61    function handlePrimaryMetaMutation($event)
62    {
63
64        /**
65         * We need to re-render the slot
66         * that are {@link \ComboStrap\MarkupCacheDependencies::PAGE_PRIMARY_META_DEPENDENCY}
67         * dependent
68         */
69        $data = $event->data;
70
71        /**
72         * Build the context back before getting the slots
73         */
74        $path = $data[PagePath::getPersistentName()];
75        MarkupCacheDependencies::reRenderSideSlotIfNeeded(
76            $path,
77            MarkupCacheDependencies::PAGE_PRIMARY_META_DEPENDENCY,
78            self::PRIMARY_META_MUTATION_EVENT_NAME
79        );
80
81    }
82
83
84}
85
86
87
88