[admincontent admin='John Doe' default='John Doe']
Created
November 21, 2024 15:10
-
-
Save Sogl/e261c3ff41d6fb8dddbf1e141ef15875 to your computer and use it in GitHub Desktop.
Admincontent shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Grav\Plugin\Shortcodes; | |
use Thunder\Shortcode\Shortcode\ShortcodeInterface; | |
use Parsedown; | |
class AdminContentShortcode extends \Grav\Plugin\Shortcodes\Shortcode | |
{ | |
public function init() | |
{ | |
$this->shortcode->getHandlers()->add('admincontent', function (ShortcodeInterface $sc) { | |
$user = $this->grav['user']; | |
$adminContent = $sc->getParameter('admin'); | |
$defaultContent = $sc->getParameter('default'); | |
if ($user->authorize('admin.login') || $user->authorize('admin.super')) { | |
return $this->processHtml($adminContent); | |
} | |
return $this->processHtml($defaultContent); | |
}); | |
} | |
private function processHtml($text) | |
{ | |
$parsedown = new Parsedown(); | |
$html = $parsedown->text($text); | |
$html = preg_replace('/<p>(.*?)<\/p>/', '$1', $html); | |
return $html; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment