Skip to content

Commit

Permalink
Craft 4 compatibility, add settings and query parameter support
Browse files Browse the repository at this point in the history
  • Loading branch information
kringkaste committed May 4, 2022
1 parent 9125a4a commit fa91262
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 145 deletions.
22 changes: 20 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
# Assets Autoversioning Changelog

## Unreleased
## 2.0.0 - 2022-05-04

### Added

- Craft CMS 4 compatibility.
- Settings page.
- Switch to activate/deactivate versioning. Can be set via environment variable.
- The file with the build ID to use as version is now configurable.
- Use query parameter instead of file suffix when adding the version to an asset.

### Changed
- Fixed background color of icon

- Requires Craft CMS >= 4.0

## 1.0.3 - 2019-04-06

### Changed

- New icon
- Updated documentation

## 1.0.2 - 2019-01-09

### Removed

- Removed version number

## 1.0.1 - 2018-12-19

### Changed

- Fix version number

## 1.0.0 - 2018-12-19

### Added

- Initial release
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Assets Autoversioning plugin for Craft CMS 3.x
# Assets Autoversioning plugin for Craft CMS

![Icon](resources/autoversioning.png)

Expand All @@ -10,7 +10,7 @@ To force the browser to download the new asset file after a update, the plugin a

## Requirements

* Craft CMS >= 3.0.0
* Craft CMS >= 4.0.0

## Installation
### Project
Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "codemonauts/craft-asset-autoversioning",
"description": "A Twig extension for Craft CMS that helps you cache-bust your assets.",
"version": "1.0.3",
"version": "2.0.0",
"type": "craft-plugin",
"keywords": [
"craft",
Expand All @@ -24,7 +24,7 @@
"issues": "https://github.com/codemonauts/craft-asset-autoversioning/issues"
},
"require": {
"craftcms/cms": "^3.0.0"
"craftcms/cms": "^4.0.0-alpha.1"
},
"autoload": {
"psr-4": {
Expand All @@ -34,9 +34,6 @@
"extra": {
"handle": "craft3-assets-autoversioning",
"class": "codemonauts\\autoversioning\\Autoversioning",
"name": "Assets Autoversioning",
"description": "Twig extension to cache-bust your assets.",
"hasCpSettings": false,
"hasCpSection": false
"name": "Assets Autoversioning"
}
}
Binary file modified resources/autoversioning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
99 changes: 50 additions & 49 deletions src/Autoversioning.php
Original file line number Diff line number Diff line change
@@ -1,83 +1,84 @@
<?php
/**
* Craft3 Assets Autoversioning plugin for Craft CMS 3.x
*
* A Twig extension for CraftCMS (Craft3.x) that helps you cache-bust your assets
*
* @link https://www.codemonauts.com
* @copyright Copyright (c) 2018 Codemonauts
*/

namespace codemonauts\autoversioning;

use codemonauts\autoversioning\models\Settings;
use codemonauts\autoversioning\twigextensions\AutoversioningTwigExtension;

use Craft;
use craft\base\Model;
use craft\base\Plugin;
use craft\services\Plugins;
use craft\events\PluginEvent;

use yii\base\Event;

/**
* Class Craft3AssetsAutoversioning
*
* @author Codemonauts
* @package Craft3AssetsAutoversioning
* @since 0.1
*
*/
use craft\helpers\UrlHelper;

class Autoversioning extends Plugin
{
// Static Properties
// =========================================================================

/**
* @var Autoversioning
*/
public static $plugin;
public static Autoversioning $plugin;

// Public Properties
// =========================================================================
/**
* @var \codemonauts\autoversioning\models\Settings|null
*/
public static ?Settings $settings;

/**
* @var string
* @inheritDoc
*/
public $schemaVersion = '0.1';
public bool $hasCpSettings = true;

// Public Methods
// =========================================================================
/**
* @var string
*/
public string $schemaVersion = '0.1';

/**
* @inheritdoc
*/
public function init()
{
parent::init();

self::$plugin = $this;

self::$settings = self::$plugin->getSettings();

// Register Twig extension
Craft::$app->view->registerTwigExtension(new AutoversioningTwigExtension());
}

Event::on(
Plugins::class,
Plugins::EVENT_AFTER_INSTALL_PLUGIN,
function (PluginEvent $event) {
if ($event->plugin === $this) {
}
}
);
/**
* @inheritDoc
*/
public function afterInstall(): void
{
parent::afterInstall();

Craft::info(
Craft::t(
'craft3-assets-autoversioning',
'{name} plugin loaded',
['name' => $this->name]
),
__METHOD__
);
if (Craft::$app->getRequest()->getIsConsoleRequest()) {
return;
}

Craft::$app->getResponse()->redirect(
UrlHelper::cpUrl('settings/plugins/craft3-assets-autoversioning')
)->send();
}

// Protected Methods
// =========================================================================
/**
* @inheritDoc
*/
protected function createSettingsModel(): ?Model
{
return new Settings();
}

/**
* @inheritDoc
*/
protected function settingsHtml(): ?string
{
return Craft::$app->getView()->renderTemplate('craft3-assets-autoversioning/settings', [
'settings' => $this->getSettings(),
]
);
}
}
10 changes: 10 additions & 0 deletions src/icon-mask.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace codemonauts\autoversioning\models;

use craft\base\Model;

class Settings extends Model
{
/**
* @var string File to check for build ID
*/
public string $ciFile = '@root/build.txt';

/**
* @var bool Switch to activate versioning
*/
public bool $active = true;

/**
* @var bool Use query parameter instead of path
*/
public bool $useQueryParam = false;
}
33 changes: 33 additions & 0 deletions src/templates/settings.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% import "_includes/forms" as forms %}

{{ forms.booleanMenuField({
label: 'Active'|t('craft3-assets-autoversioning'),
id: 'active',
name: 'active',
value: settings.active,
includeEnvVars: true,
required: true,
errors: settings.getErrors('active')
}) }}

{{ forms.autosuggestField({
label: 'CI File'|t('craft3-assets-autoversioning'),
id: 'ciFile',
name: 'ciFile',
instructions: 'The file containing the build ID to use as version number. Leave empty to always use the modification time of the asset.'|t('craft3-assets-autoversioning'),
value: settings.ciFile,
suggestEnvVars: true,
suggestAliases: true,
type: 'text',
errors: settings.getErrors('ciFile')
}) }}

{{ forms.lightswitchField({
label: 'Use query parameter'|t("craft3-assets-autoversioning"),
instructions: 'Use query parameter instead of file suffix for versioning.'|t('craft3-assets-autoversioning'),
id: 'useQueryParam',
name: 'useQueryParam',
on: settings.useQueryParam,
required: true,
errors: settings.getErrors('useQueryParam')
}) }}
18 changes: 0 additions & 18 deletions src/translations/de/craft3-assets-autoversioning.php

This file was deleted.

18 changes: 0 additions & 18 deletions src/translations/en/craft3-assets-autoversioning.php

This file was deleted.

Loading

0 comments on commit fa91262

Please sign in to comment.