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