Skip to content

Commit

Permalink
Merge pull request #1 from pamtbaau/main
Browse files Browse the repository at this point in the history
Discontinue plugin when banner has been dismissed
  • Loading branch information
aricooperdavis authored Apr 29, 2021
2 parents 10d33ab + e83f629 commit ca277da
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions custom-banner.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Grav\Plugin;

use Composer\Autoload\ClassLoader;
Expand All @@ -10,6 +11,8 @@
*/
class CustomBannerPlugin extends Plugin
{
private const CUSTOM_BANNER_DISMISS = 'custom-banner-dismiss';

/**
* @return array
*
Expand All @@ -32,10 +35,10 @@ public static function getSubscribedEvents(): array
}

/**
* Composer autoload.
*is
* @return ClassLoader
*/
* Composer autoload.
*is
* @return ClassLoader
*/
public function autoload(): ClassLoader
{
return require __DIR__ . '/vendor/autoload.php';
Expand All @@ -51,6 +54,11 @@ public function onPluginsInitialized(): void
return;
}

// Do not continue if banner has been dismissed
if (isset($_COOKIE[self::CUSTOM_BANNER_DISMISS])) {
return;
}

// Enable the main events we are interested in
$this->enable([
'onAssetsInitialized' => ['onAssetsInitialized', 0],
Expand All @@ -60,8 +68,8 @@ public function onPluginsInitialized(): void

public function onAssetsInitialized(): void
{
$this->grav['assets']->addDirCss('plugins://custom-banner/css');
$this->grav['assets']->addDirJs('plugins://custom-banner/js');
$this->grav['assets']->addDirCss('plugins://custom-banner/css');
$this->grav['assets']->addDirJs('plugins://custom-banner/js');
}

public function onOutputGenerated(): void
Expand All @@ -71,7 +79,7 @@ public function onOutputGenerated(): void
$config = $this->config();
$config['exclude-pages'] = (array)$config['exclude-pages'];
$defaults = $this->config->getDefaults()['plugins']['custom-banner'];
$config = array_merge($defaults, array_filter($config, function($v){
$config = array_merge($defaults, array_filter($config, function ($v) {
return !(is_null($v));
}));

Expand All @@ -84,7 +92,7 @@ public function onOutputGenerated(): void
$button_text = $config['button-text'];
$button_url = $config['button-url'];
$dismiss_text = $config['dismiss-text'];
$dismiss_button = ( $config['dismiss-button'] ? 'unset' : 'none' );
$dismiss_button = ($config['dismiss-button'] ? 'unset' : 'none');

// Style
$position = $config['position'];
Expand Down

0 comments on commit ca277da

Please sign in to comment.