103 lines
3.2 KiB
PHP
103 lines
3.2 KiB
PHP
<?php
|
|
|
|
class utterances extends Plugin {
|
|
|
|
public function init()
|
|
{
|
|
$this->dbFields = array(
|
|
'myUtterancesRepo' => '',
|
|
'enablePages' => true,
|
|
'enableStatic' => true,
|
|
'enableSticky' => true,
|
|
'followSiteTheme' => true,
|
|
'utterancesTheme' => 'github-dark'
|
|
);
|
|
}
|
|
|
|
private function isOn($key)
|
|
{
|
|
$v = $this->getValue($key);
|
|
return ($v === true || $v === 'true' || $v === 1 || $v === '1');
|
|
}
|
|
|
|
public function form()
|
|
{
|
|
global $L;
|
|
|
|
$toggle = function ($key, $label) use ($L) {
|
|
$on = $this->isOn($key);
|
|
return '<div><label>'.$label.'</label>'
|
|
. '<select name="'.$key.'">'
|
|
. '<option value="true" '.($on ? 'selected' : '').'>'.$L->get('enabled').'</option>'
|
|
. '<option value="false" '.(!$on ? 'selected' : '').'>'.$L->get('disabled').'</option>'
|
|
. '</select></div>';
|
|
};
|
|
|
|
$html = '<div class="alert alert-primary" role="alert">'.$L->get('utterances-installation').'</div>';
|
|
|
|
$html .= '<div><label>'.$L->get('utterances-repository').'</label>';
|
|
$html .= '<input name="myUtterancesRepo" id="jsmyUtterancesRepo" type="text" value="'.htmlspecialchars($this->getValue('myUtterancesRepo'), ENT_QUOTES, 'UTF-8').'">';
|
|
$html .= '<span class="tip">'.$L->get('utterances-tip').'</span></div>';
|
|
|
|
$html .= $toggle('enablePages', $L->get('enable-utterances-on-pages'));
|
|
$html .= $toggle('enableStatic', $L->get('enable-utterances-on-static-pages'));
|
|
$html .= $toggle('enableSticky', $L->get('enable-utterances-on-sticky-pages'));
|
|
$html .= $toggle('followSiteTheme', 'Follow site theme (recommended)');
|
|
|
|
$t = $this->getValue('utterancesTheme');
|
|
$html .= '<div><label>'.$L->get('utterances-select-theme').'</label><select name="utterancesTheme">';
|
|
foreach (array('github-light','github-dark','github-dark-orange','icy-dark','dark-blue','photon-dark') as $opt) {
|
|
$html .= '<option value="'.$opt.'" '.($t === $opt ? 'selected' : '').'>'.$opt.'</option>';
|
|
}
|
|
$html .= '</select></div>';
|
|
|
|
return $html;
|
|
}
|
|
|
|
public function pageEnd()
|
|
{
|
|
global $url, $WHERE_AM_I, $page;
|
|
|
|
if ($url->notFound()) { return false; }
|
|
if ($WHERE_AM_I !== 'page') { return false; }
|
|
if (!isset($page)) { return false; }
|
|
|
|
$show = ($page->published() && $this->isOn('enablePages'))
|
|
|| ($page->isStatic() && $this->isOn('enableStatic'))
|
|
|| ($page->sticky() && $this->isOn('enableSticky'));
|
|
|
|
return $show ? $this->javascript() : false;
|
|
}
|
|
|
|
private function resolveTheme()
|
|
{
|
|
if ($this->isOn('followSiteTheme')) {
|
|
$cookie = isset($_COOKIE['blowdit-theme']) ? $_COOKIE['blowdit-theme'] : '';
|
|
return ($cookie === 'light') ? 'github-light' : 'github-dark';
|
|
}
|
|
$t = $this->getValue('utterancesTheme');
|
|
return ($t !== '') ? $t : 'github-dark';
|
|
}
|
|
|
|
private function javascript()
|
|
{
|
|
$repo = trim($this->getValue('myUtterancesRepo'));
|
|
if ($repo === '') { return false; }
|
|
|
|
$repo = htmlspecialchars($repo, ENT_QUOTES, 'UTF-8');
|
|
$theme = htmlspecialchars($this->resolveTheme(), ENT_QUOTES, 'UTF-8');
|
|
|
|
return <<<EOF
|
|
<section class="utterances-comments">
|
|
<script src="https://utteranc.es/client.js"
|
|
repo="$repo"
|
|
issue-term="pathname"
|
|
theme="$theme"
|
|
crossorigin="anonymous"
|
|
async>
|
|
</script>
|
|
</section>
|
|
EOF;
|
|
}
|
|
}
|