Kirby3 Janitor - a panel button to run jobs defined in php

I am creating posts for my plugins to make it easier to find them using the forum search and not just the docs search.

Kirby 3 Plugin for running jobs.

  • It is a Panel Button!
  • It has jobs build-in for cleaning the cache, sessions, create zip-backup, pre-generate thumbs, open URLs, refresh the current Panel page, download a file, copy to clipboard and more.
  • You can define your own jobs (call API hooks, play a game, hack a server, …)
  • It can be triggered in your frontend code and with CRON.
  • It can also be used as a CLI with fancy output.
  • It can also create logs of what it did.

register and define a job as callback aka closure

site/config/config.php

'bnomei.janitor.jobs' => [
    'aweSomeItCouldBe' => function (Kirby\Cms\Page $page = null, string $data = null) {
        // $page => page object where the button as pressed
        // $data => 'my custom data'
        return [
            'status' => 200,
            'label' => $page->title() . ' ' . $data,
        ];
    },
    'query' => function (Kirby\Cms\Page $page = null, string $data = null) {
        return [
            'status' => 200,
            'label' => $data, // this is the current panel users email
        ];
    },
]

add a panel button

site/blueprints/pages/default.yml

  janitor:
    type: janitor
    label: Awesome
    progress: Awesome...
    job: aweSomeItCouldBe

  janitor_query:
    type: janitor
    label: Query '{{ user.email }}'
    job: query
    data: '{{ user.email }}'
2 Likes

with new v2.13.0 you can now use janitor in CLI mode to

  • janitor --tinker will start a very simple REPL session.
  • janitor --down will put your frontend in maintenance mode. Panel will still be usable.
  • janitor --up will stop the maintenance mode.

Hi Bnomei,

If I want to add a button in pane which blueprint should I edit?

I tried adding this:

janitor:
  type: janitor
  label: Clean Cache
  progress: Cleaning Cache...
  job: cleanCache

to site/blueprints/default.yml but nothing changes in the panel.

What am I doing wrong?

THanks!

That depends on your setup and on which page type you want to use the button

1 Like

@texnixe thanks for your reply!

I thought cleaning the cache would clean the whole website’s cache – ideally it’d be a button/section in the main panel navigation, like I saw other plugins do. It’s not really tied to a page. But I think the owner of the website wouldn’t mind if it would be a button just when editing a certain page, that’s fine.

I tried adding the YAML above it to all 3 blueprints that are actually used on this website and none of the forms changed when trying to edit the pages.

It would probably make sense to include this button on the Dashboard, i.e. site.yml, not a page blueprint.

Since the Janitor button is provided as a field plugins, it needs to be inside a fields section-

Yes, that is correct.

That’s not possible with a field/section plugin.

1 Like

Perfect!

First I wasn’t putting it under the fields entry, second I didn’t even realise about the site/blueprints/site.yml. While upgrading from Kirby 2 to 3 I put all blueprints into blueprints/pages and site.yml wasn’t at all respected, so I thought it was an old unused blueprint. I added it to site.yml fields and the button is there, thanks so much!

But now that I have added it, the previous dashboard where new pages could be added is gone – how could I allow both site.yml fields and editing pages too?

Thanks!

There are several ways to set this up, but the minimum requirement for this would be a fields section and a pages section.

title: Site

sections:
  pages:
    type: pages
    # other page section props.
  fields:
    type: fields
    fields:
      # your fields here

Of course, you can have multiple sections (e.g. pages separated by templates), and also put them into columns and tabs.

2 Likes

Per-fect! Works like a charm, thanks so much!