├── .gitignore
├── CHANGELOG.md
├── LICENSE.md
├── README.md
├── composer.json
├── screenshot.png
└── src
├── CpClearCache.php
├── CpClearCacheBundle.php
├── icon.svg
└── resources
└── cpclearcache.js
/.gitignore:
--------------------------------------------------------------------------------
1 | # CRAFT ENVIRONMENT
2 | .env.php
3 | .env.sh
4 | .env
5 |
6 | # COMPOSER
7 | /vendor
8 | /composer.lock
9 |
10 | # BUILD FILES
11 | /bower_components/*
12 | /node_modules/*
13 | /build/*
14 | /yarn-error.log
15 |
16 | # MISC FILES
17 | .cache
18 | .DS_Store
19 | .idea
20 | .project
21 | .settings
22 | *.esproj
23 | *.sublime-workspace
24 | *.sublime-project
25 | *.tmproj
26 | *.tmproject
27 | .vscode/*
28 | !.vscode/settings.json
29 | !.vscode/tasks.json
30 | !.vscode/launch.json
31 | !.vscode/extensions.json
32 | config.codekit3
33 | prepros-6.config
34 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # CP Clear Cache Changelog
2 |
3 | All notable changes to this project will be documented in this file.
4 |
5 | The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
6 |
7 | ## 2.0.1 - 2024-03-30
8 | ### Fixed
9 | - Fixed a bug where newly added cache items could appear as unchecked, when the "All" option was checked ([#16](https://github.com/mmikkel/CpClearCache-Craft/issues/16))
10 |
11 | ## 2.0.0 - 2024-03-28
12 | ### Added
13 | - Added Craft 5 compatibility
14 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2018 Mats Mikkel Rummelhoff
4 |
5 | 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:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # CP Clear Cache plugin for Craft CMS 5.x
2 |
3 | Less clickin’ to get clearin’! CP Clear Cache provides easy access to the native _Clear Caches_ utility.
4 |
5 | For users that doesn't have access to the Clear Caches utility, this plugin does absolutely nothing.
6 |
7 | 
8 |
9 | ## Requirements
10 |
11 | This plugin requires Craft CMS 5.0+
12 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mmikkel/cp-clearcache",
3 | "description": "Less clickin’ to get clearin’",
4 | "type": "craft-plugin",
5 | "version": "2.0.1",
6 | "keywords": [
7 | "craft",
8 | "cms",
9 | "craftcms",
10 | "craft-plugin",
11 | "cp clear cache"
12 | ],
13 | "support": {
14 | "docs": "https://github.com/mmikkel/CpClearCache-Craft/blob/master/README.md",
15 | "issues": "https://github.com/mmikkel/CpClearCache-Craft/issues"
16 | },
17 | "license": "MIT",
18 | "authors": [
19 | {
20 | "name": "Mats Mikkel Rummelhoff",
21 | "homepage": "https://vaersaagod.no"
22 | }
23 | ],
24 | "require": {
25 | "craftcms/cms": "^5.0.0",
26 | "php": "^8.2"
27 | },
28 | "minimum-stability": "dev",
29 | "prefer-stable": true,
30 | "config": {
31 | "platform": {
32 | "php": "8.2"
33 | },
34 | "allow-plugins": {
35 | "yiisoft/yii2-composer": true,
36 | "craftcms/plugin-installer": true
37 | }
38 | },
39 | "autoload": {
40 | "psr-4": {
41 | "mmikkel\\cpclearcache\\": "src/"
42 | }
43 | },
44 | "extra": {
45 | "name": "CP Clear Cache",
46 | "handle": "cp-clearcache",
47 | "schemaVersion": "1.0.0",
48 | "hasCpSettings": false,
49 | "hasCpSection": false,
50 | "changelogUrl": "https://raw.githubusercontent.com/mmikkel/CpClearCache-Craft/master/CHANGELOG.md",
51 | "class": "mmikkel\\cpclearcache\\CpClearCache"
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/screenshot.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/mmikkel/CpClearCache-Craft/1ed155059dcdab2de7dc85c427d6299a3e829b6a/screenshot.png
--------------------------------------------------------------------------------
/src/CpClearCache.php:
--------------------------------------------------------------------------------
1 | getRequest();
48 |
49 | if (!$request->getIsCpRequest() || !$request->getIsGet() || $request->getIsLoginRequest()) {
50 | return;
51 | }
52 |
53 | Craft::$app->onInit(function () {
54 | try {
55 | $this->doIt();
56 | } catch (\Throwable $e) {
57 | Craft::error($e, __METHOD__);
58 | };
59 | });
60 |
61 | }
62 |
63 | /**
64 | * @return void
65 | * @throws \Throwable
66 | */
67 | protected function doIt(): void
68 | {
69 |
70 | if (!Craft::$app->getUser()->getIdentity()) {
71 | return;
72 | }
73 |
74 | // Get the Clear Caches utility and check that the current user has access to it
75 | $utilitiesService = Craft::$app->getUtilities();
76 | $clearCachesUtility = $utilitiesService->getUtilityTypeById('clear-caches');
77 | if ($clearCachesUtility === null || $utilitiesService->checkAuthorization($clearCachesUtility) === false) {
78 | return;
79 | }
80 |
81 | // Register asset bundle
82 | Event::on(
83 | View::class,
84 | ViewAlias::EVENT_BEGIN_BODY,
85 | function () {
86 | try {
87 | $html = $this->_getClearCachesUtilityHtml();
88 | $html = "
$html
";
89 | $view = Craft::$app->getView();
90 | $view->registerAssetBundle(UtilitiesAsset::class);
91 | $view->registerAssetBundle(CpClearCacheBundle::class);
92 | $view->registerJs('if (Craft.CpClearCachePlugin) { Craft.CpClearCachePlugin.init(' . json_encode(['html' => $html]) . '); };', View::POS_READY);
93 | } catch (\Throwable $e) {
94 | Craft::error(
95 | 'Error registering AssetBundle - ' . $e->getMessage(),
96 | __METHOD__
97 | );
98 | }
99 | }
100 | );
101 |
102 | // Add CP nav menu item
103 | Event::on(Cp::class, Cp::EVENT_REGISTER_CP_NAV_ITEMS, function (RegisterCpNavItemsEvent $event) {
104 | $event->navItems['mmikkel/cpclearcache'] = [
105 | 'label' => \Craft::t('app', 'Clear Caches'),
106 | 'fontIcon' => 'trash',
107 | 'url' => 'mmikkel/cpclearcache'
108 | ];
109 | });
110 | }
111 |
112 | /**
113 | * @return string
114 | * @throws \Twig\Error\LoaderError
115 | * @throws \Twig\Error\RuntimeError
116 | * @throws \Twig\Error\SyntaxError
117 | * @throws \yii\base\Exception
118 | */
119 | private function _getClearCachesUtilityHtml(): string
120 | {
121 | $cacheOptions = [];
122 | $tagOptions = [];
123 |
124 | foreach (ClearCaches::cacheOptions() as $cacheOption) {
125 | $cacheOptions[] = [
126 | 'label' => $cacheOption['label'],
127 | 'value' => $cacheOption['key'],
128 | 'info' => $cacheOption['info'] ?? null,
129 | ];
130 | }
131 |
132 | foreach (ClearCaches::tagOptions() as $tagOption) {
133 | $tagOptions[] = [
134 | 'label' => $tagOption['label'],
135 | 'value' => $tagOption['tag'],
136 | ];
137 | }
138 |
139 | ArrayHelper::multisort($cacheOptions, 'label');
140 | $view = Craft::$app->getView();
141 |
142 | return $view->renderTemplate('_components/utilities/ClearCaches', [
143 | 'cacheOptions' => $cacheOptions,
144 | 'tagOptions' => $tagOptions,
145 | ]);
146 | }
147 |
148 | }
149 |
--------------------------------------------------------------------------------
/src/CpClearCacheBundle.php:
--------------------------------------------------------------------------------
1 | sourcePath = '@mmikkel/cpclearcache/resources';
22 |
23 | // define the dependencies
24 | $this->depends = [
25 | CpAsset::class,
26 | ];
27 |
28 | // define the relative path to CSS/JS files that should be registered with the page
29 | // when this asset bundle is registered
30 | $this->js = [
31 | 'cpclearcache.js',
32 | ];
33 |
34 | $this->css = [
35 |
36 | ];
37 |
38 | parent::init();
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/src/icon.svg:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/src/resources/cpclearcache.js:
--------------------------------------------------------------------------------
1 | (function (window) {
2 |
3 | if (!window.Craft || !window.jQuery) {
4 | return false;
5 | }
6 |
7 | Craft.CpClearCachesUtility = Garnish.Base.extend(
8 | {
9 | init: function (formId) {
10 | let $form = $('#' + formId);
11 | let $checkboxes = $form.find('input[type=checkbox]');
12 | let $btn = $form.find('.btn');
13 | let checkInputs = function () {
14 | if ($checkboxes.filter(':checked').length) {
15 | $btn.removeClass('disabled');
16 | } else {
17 | $btn.addClass('disabled');
18 | }
19 | };
20 | $checkboxes.on('change', checkInputs);
21 | checkInputs();
22 | this.addListener($form, 'submit', ev => {
23 | ev.preventDefault();
24 | if (!$btn.hasClass('disabled')) {
25 | this.onSubmit(ev);
26 | }
27 | });
28 | },
29 |
30 | onSubmit: function (ev) {
31 | let $form = $(ev.currentTarget);
32 | let $trigger = $form.find('button.submit');
33 | let $status = $form.find('.utility-status');
34 |
35 | if ($trigger.hasClass('disabled')) {
36 | return;
37 | }
38 |
39 | let progressBar, $allDone;
40 | if (!$form.data('progressBar')) {
41 | progressBar = new Craft.ProgressBar($status);
42 | $form.data('progressBar', progressBar);
43 | } else {
44 | progressBar = $form.data('progressBar');
45 | progressBar.resetProgressBar();
46 | $allDone = $form.data('allDone');
47 | }
48 |
49 | progressBar.$progressBar.removeClass('hidden');
50 |
51 | progressBar.$progressBar.velocity('stop').velocity({
52 | opacity: 1
53 | }, {
54 | complete: $.proxy(function () {
55 | let postData = Garnish.getPostData($form);
56 | let params = Craft.expandPostArray(postData);
57 |
58 | Craft.postActionRequest(params.action, params, (response, textStatus) => {
59 | if (response && response.error) {
60 | alert(response.error);
61 | }
62 |
63 | progressBar.setProgressPercentage(100);
64 |
65 | setTimeout(() => {
66 | if (!$allDone) {
67 | $allDone = $('').appendTo($status);
68 | $allDone.css('opacity', 0);
69 | $form.data('allDone', $allDone);
70 | }
71 |
72 | progressBar.$progressBar.velocity({ opacity: 0 }, {
73 | duration: 'fast', complete: () => {
74 | $allDone.velocity({ opacity: 1 }, { duration: 'fast' });
75 | $trigger.removeClass('disabled');
76 | $trigger.trigger('focus');
77 | },
78 | });
79 | }, 300);
80 | }, {
81 | complete: $.noop
82 | });
83 | }, this)
84 | });
85 |
86 | if ($allDone) {
87 | $allDone.css('opacity', 0);
88 | }
89 |
90 | $trigger.addClass('disabled');
91 | $trigger.trigger('blur');
92 | },
93 | });
94 |
95 | Craft.CpClearCachePlugin = {
96 |
97 | hud: null,
98 | $trigger: null,
99 | localStorageKey: 'cpclearcache_selected',
100 |
101 | onChange: function () {
102 | Craft.setLocalStorage(this.localStorageKey, $.map($('#mmikkel-cpclearcache input[type="checkbox"]:checked'), function (input) {
103 | return input.value;
104 | }));
105 | },
106 |
107 | onClick: function (e) {
108 |
109 | e.preventDefault();
110 |
111 | var $trigger = $(e.currentTarget);
112 | var _this = this;
113 |
114 | var $html = $(this.data.html);
115 | $html.attr('id', 'mmikkel-cpclearcache');
116 | $('.info', $html).infoicon();
117 |
118 | $html.find('form').each(function (index) {
119 | this.id = 'mmikkel-cpclearcache-form-' + index;
120 | var $checkboxes = $(this).find('.checkbox-select');
121 | new Garnish.CheckboxSelect($checkboxes);
122 | var checkedBoxes = Craft.getLocalStorage(_this.localStorageKey);
123 | $checkboxes.find('input[type="checkbox"]').each(function () {
124 | var checked = !checkedBoxes || checkedBoxes.indexOf(this.value) > -1;
125 | if (!checked && !!checkedBoxes && checkedBoxes.indexOf('*') > -1 && !!$(this).closest('fieldset.checkbox-select').eq(0).find('input[type="checkbox"][value="*"]').length) {
126 | // Not specifically checked, but "All" is checked
127 | checked = true;
128 | }
129 | $(this).prop('checked', checked).trigger('change');
130 | });
131 | });
132 |
133 | this.hud = new Garnish.HUD($trigger, $html, {
134 | orientations: ['top', 'bottom', 'right', 'left'],
135 | hudClass: 'hud toolhud',
136 | onShow: function () {
137 | Garnish.requestAnimationFrame(function () {
138 | $html.find('form').each(function () {
139 | new Craft.CpClearCachesUtility(this.id);
140 | });
141 | });
142 | },
143 | onHide: function () {
144 | _this.hud.destroy();
145 | _this.hud = null;
146 | }
147 | });
148 |
149 | },
150 |
151 | init: function (data) {
152 |
153 | this.data = data;
154 |
155 | $('body').on('click', '#nav a[href*="mmikkel/cpclearcache"]', $.proxy(this.onClick, this));
156 |
157 | $('body')
158 | .on('change', '#mmikkel-cpclearcache input[type="checkbox"]', $.proxy(this.onChange, this))
159 | .on('click', '#mmikkel-cpclearcache .info', e => {
160 | e.preventDefault();
161 | e.stopPropagation();
162 | });
163 |
164 | }
165 |
166 | };
167 |
168 | }(window));
169 |
--------------------------------------------------------------------------------