Skip to content

Commit

Permalink
feat: add header caching, cleanup (#1)
Browse files Browse the repository at this point in the history
* feat: add header caching, cleanup

* Apply fixes from StyleCI

* Add string length to validator

* Update LICENSE.md

* chore: cleanup handlers

* Apply fixes from StyleCI

* define a shared key

* Apply fixes from StyleCI

* refine header validation a little

* Apply fixes from StyleCI

* Update README

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
imorland and StyleCIBot authored Sep 18, 2023
1 parent b6fc869 commit f4f2b94
Show file tree
Hide file tree
Showing 26 changed files with 1,054 additions and 787 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
MIT License Copyright (c) 2021 Ian Morland
MIT License Copyright (c) 2021 - 2023 Ian Morland

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:

Expand Down
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@

![License](https://img.shields.io/badge/license-MIT-blue.svg) [![Latest Stable Version](https://img.shields.io/packagist/v/ianm/html-head.svg)](https://packagist.org/packages/ianm/html-head)

A [Flarum](http://flarum.org) extension. Add custom items to the &lt;head&gt; items
A [Flarum](http://flarum.org) extension that offers a seamless way to add custom items to the HTML &lt;head&gt; section of your forum.

### Usage
### Features

- **Easy Integration**: With just a few clicks, forum administrators can embed additional items directly into the forum's HTML <head>. This is particularly useful for adding meta tags, styles, or any other custom scripts.

- **Clean UI**: The extension provides a user-friendly interface that makes it simple to manage and view all added items.

- **Cache Management**: Any changes made to the head items are automatically reflected, thanks to the efficient cache management in place. This ensures that your updates are immediately visible without any additional steps.

Forum admins may add additional items into the forum's HTML `<head>`
- **Error Handling**: In the event that an invalid header item is added, the extension intelligently logs the error, ensuring the forum's functionality isn't compromised.

![image](https://user-images.githubusercontent.com/16573496/104105231-a3bb3000-52a4-11eb-97dc-d4c097471ebd.png)

![image](https://user-images.githubusercontent.com/16573496/104105258-cc432a00-52a4-11eb-8ee6-51d75c731b51.png)

### Usage

To use the extension, simply navigate to the admin dashboard. Here you'll find the option to add or modify items that will be included in the forum's HTML `<head>`.

### Installation

Install manually with composer:
Expand All @@ -24,8 +34,12 @@ composer require ianm/html-head:"*"

```sh
composer update ianm/html-head
php flarum cache:clear
```

### Links

- [Packagist](https://packagist.org/packages/ianm/html-head)
- [GitHub](https://github.com/imorland/html-head)
- [Discuss](https://discuss.flarum.org/d/25907)

22 changes: 8 additions & 14 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,35 @@
/*
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead;

use Flarum\Extend;
use Flarum\Frontend\Document;
use IanM\HtmlHead\Api\Controllers;
use Illuminate\Database\Eloquent\Collection;

return [

(new Extend\Frontend('admin'))
->js(__DIR__.'/js/dist/admin.js')
->css(__DIR__.'/resources/less/admin.less'),

new Extend\Locales(__DIR__ . '/resources/locale'),
new Extend\Locales(__DIR__.'/resources/locale'),

(new Extend\Frontend('forum'))
->content(function (Document $document) {
/** @var Collection */
$headCollection = Header::where('active', 1)->get();

/** @var Header $headItem */
foreach ($headCollection as $headItem) {
$document->head[] = $headItem->header;
}
}),
->content(Content\AddHeaders::class),

(new Extend\Routes('api'))
->get('/html-headers', 'ianm.html-headers.index', Controllers\ListHeadersController::class)
->post('/html-headers', 'ianm.html-headers.create', Controllers\CreateHeaderItemController::class)
->patch('/html-headers/{id}', 'ianm.html-headers.update', Controllers\UpdateHeaderItemController::class)
->delete('/html-headers/{id}', 'ianm.html-headers.delete', Controllers\DeleteHeaderItemController::class),

(new Extend\Event())
->subscribe(Listener\ClearCache::class),
];
1,327 changes: 638 additions & 689 deletions js/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
"@flarum/prettier-config": "^1.0.0",
"flarum-tsconfig": "^1.0.2",
"flarum-webpack-config": "^2.0.0",
"webpack": "^5.67.0",
"webpack-cli": "^4.9.2"
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4"
},
"devDependencies": {
"prettier": "^2.5.1"
"prettier": "^3.0.3"
},
"scripts": {
"dev": "webpack --mode development --watch",
Expand Down
2 changes: 0 additions & 2 deletions js/src/admin/components/CreateHeadItemModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ export default class CreateHeadItemModal extends Modal {
onsubmit(e) {
e.preventDefault();

if (!this.header()) return;

this.loading = true;

const attrs = {
Expand Down
2 changes: 1 addition & 1 deletion js/src/admin/components/HeadItemListItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default class HeadItemListItem extends Component {
</td>
<td>
{Switch.component({
state: this.item.data.attributes.active,
state: this.item.active(),
onchange: (value) => {
this.activeLoading = true;

Expand Down
11 changes: 6 additions & 5 deletions migrations/2021_01_01_000000_create_html_headers_table.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

use Illuminate\Database\Schema\Blueprint;
Expand All @@ -18,7 +19,7 @@
return;
}

$schema->create('html_headers', function (Blueprint $table) use ($schema) {
$schema->create('html_headers', function (Blueprint $table) {
$table->increments('id');

$table->text('description', 200);
Expand All @@ -30,4 +31,4 @@
'down' => function (Builder $schema) {
$schema->dropIfExists('html_headers');
},
];
];
19 changes: 15 additions & 4 deletions src/Api/Controllers/CreateHeaderItemController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead\Api\Controllers;
Expand All @@ -15,6 +16,7 @@
use Flarum\Http\RequestUtil;
use IanM\HtmlHead\Api\Serializers\HeaderSerializer;
use IanM\HtmlHead\Command\CreateHeaderItem;
use IanM\HtmlHead\Validator\HeaderItemValidator;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -27,6 +29,11 @@ class CreateHeaderItemController extends AbstractCreateController
*/
public $serializer = HeaderSerializer::class;

/**
* @var HeaderItemValidator
*/
protected $validator;

/**
* @var Dispatcher
*/
Expand All @@ -35,8 +42,9 @@ class CreateHeaderItemController extends AbstractCreateController
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
public function __construct(HeaderItemValidator $validator, Dispatcher $bus)
{
$this->validator = $validator;
$this->bus = $bus;
}

Expand All @@ -46,9 +54,12 @@ public function __construct(Dispatcher $bus)
protected function data(ServerRequestInterface $request, Document $document)
{
$actor = RequestUtil::getActor($request);
$data = Arr::get($request->getParsedBody(), 'data', []);

$this->validator->assertValid($data);

return $this->bus->dispatch(
new CreateHeaderItem($actor, Arr::get($request->getParsedBody(), 'data', []))
new CreateHeaderItem($actor, $data)
);
}
}
8 changes: 4 additions & 4 deletions src/Api/Controllers/DeleteHeaderItemController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead\Api\Controllers;
Expand Down Expand Up @@ -38,11 +39,10 @@ public function __construct(Dispatcher $bus)
*/
protected function delete(ServerRequestInterface $request)
{
/** @var \Flarum\User\User */
$actor = RequestUtil::getActor($request);

$this->bus->dispatch(
new DeleteHeaderItem($actor, Arr::get($request->getQueryParams(), 'id'))
);
}
}
}
5 changes: 3 additions & 2 deletions src/Api/Controllers/ListHeadersController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead\Api\Controllers;
Expand Down
16 changes: 13 additions & 3 deletions src/Api/Controllers/UpdateHeaderItemController.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead\Api\Controllers;
Expand All @@ -15,6 +16,7 @@
use Flarum\Http\RequestUtil;
use IanM\HtmlHead\Api\Serializers\HeaderSerializer;
use IanM\HtmlHead\Command\UpdateHeaderItem;
use IanM\HtmlHead\Validator\HeaderItemValidator;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Support\Arr;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -27,6 +29,11 @@ class UpdateHeaderItemController extends AbstractShowController
*/
public $serializer = HeaderSerializer::class;

/**
* @var HeaderItemValidator
*/
protected $validator;

/**
* @var Dispatcher
*/
Expand All @@ -35,8 +42,9 @@ class UpdateHeaderItemController extends AbstractShowController
/**
* @param Dispatcher $bus
*/
public function __construct(Dispatcher $bus)
public function __construct(HeaderItemValidator $validator, Dispatcher $bus)
{
$this->validator = $validator;
$this->bus = $bus;
}

Expand All @@ -49,6 +57,8 @@ protected function data(ServerRequestInterface $request, Document $document)
$id = Arr::get($request->getQueryParams(), 'id');
$data = $request->getParsedBody();

$this->validator->assertValid($data);

return $this->bus->dispatch(
new UpdateHeaderItem($actor, $id, $data)
);
Expand Down
5 changes: 3 additions & 2 deletions src/Api/Serializers/HeaderSerializer.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead\Api\Serializers;
Expand Down
9 changes: 5 additions & 4 deletions src/Command/CreateHeaderItem.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

/*
* This file is part of ianm/html-head.
* This file is part of ianm/htmlhead.
*
* Copyright (c) 2021 IanM.
* Copyright (c) IanM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*
*/

namespace IanM\HtmlHead\Command;
Expand All @@ -23,14 +24,14 @@ class CreateHeaderItem
public $actor;

/**
* The attributes of the new draft.
* The attributes of the new header.
*
* @var array
*/
public $data;

/**
* CreateDraft constructor.
* CreateHeaderItem constructor.
*
* @param User $actor
* @param array $data
Expand Down
Loading

0 comments on commit f4f2b94

Please sign in to comment.