ttrss/plugins/nsfw/init.php

125 lines
3.0 KiB
PHP
Raw Normal View History

2013-02-23 13:02:29 +01:00
<?php
class NSFW extends Plugin {
/** @var PluginHost $host */
2013-02-23 13:02:29 +01:00
private $host;
function about() {
return array(null,
2013-02-23 18:28:09 +01:00
"Hide article content based on tags",
2013-02-23 13:02:29 +01:00
"fox",
false);
}
function init($host) {
$this->host = $host;
2021-03-07 11:22:38 +01:00
$host->add_hook(PluginHost::HOOK_RENDER_ARTICLE, $this);
$host->add_hook(PluginHost::HOOK_RENDER_ARTICLE_CDM, $this);
$host->add_hook(PluginHost::HOOK_RENDER_ARTICLE_API, $this);
$host->add_hook(PluginHost::HOOK_ARTICLE_IMAGE, $this);
$host->add_hook(PluginHost::HOOK_PREFS_TAB, $this);
2013-02-23 13:02:29 +01:00
}
2021-03-07 11:22:38 +01:00
function hook_article_image($enclosures, $content, $site_url, $article) {
$tags = explode(",", $this->host->get($this, "tags"));
$article_tags = $article["tags"];
if (count(array_intersect($tags, $article_tags)) > 0) {
return [Config::get_self_url() . "/plugins/nsfw/nsfw.png", "", "nsfw"];
2021-03-07 11:22:38 +01:00
} else {
return ["", "", $content];
}
}
/**
* @param array<string, mixed> $article
* @return array<string,mixed>
* @throws PDOException
*/
private function rewrite_contents(array $article) : array {
2021-03-07 11:22:38 +01:00
$tags = explode(",", $this->host->get($this, "tags"));
$article_tags = $article["tags"];
2013-02-23 13:02:29 +01:00
2021-03-07 11:22:38 +01:00
if (count(array_intersect($tags, $article_tags)) > 0) {
2021-03-15 17:42:48 +01:00
$article["content"] = "<details class='nsfw'><summary>" . __("Not safe for work (click to toggle)") . "</summary>" . $article["content"] . "</details>";
2013-02-23 13:02:29 +01:00
}
return $article;
}
2021-03-15 17:42:48 +01:00
function get_css() {
return
'details.nsfw {
cursor : pointer;
user-select : none;
}';
}
2021-03-07 11:22:38 +01:00
function hook_render_article_api($row) {
$article = isset($row['headline']) ? $row['headline'] : $row['article'];
return $this->rewrite_contents($article);
2021-03-07 11:22:38 +01:00
}
function hook_render_article($article) {
return $this->rewrite_contents($article);
}
2013-02-23 13:02:29 +01:00
function hook_render_article_cdm($article) {
2021-03-07 11:22:38 +01:00
return $this->rewrite_contents($article);
2013-02-23 13:02:29 +01:00
}
2013-02-23 18:28:09 +01:00
function hook_prefs_tab($args) {
if ($args != "prefPrefs") return;
$tags = $this->host->get($this, "tags");
?>
<div dojoType="dijit.layout.AccordionPane"
title="<i class='material-icons'>extension</i> <?= __("NSFW Plugin") ?>">
<form dojoType="dijit.form.Form">
<?= \Controls\pluginhandler_tags($this, "save") ?>
<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault();
if (this.validate()) {
Notify.progress('Saving data...', true);
xhr.post("backend.php", this.getValues(), (reply) => {
Notify.info(reply);
})
2013-02-23 18:28:09 +01:00
}
</script>
2013-02-23 18:28:09 +01:00
<header><?= __("Tags to consider NSFW (comma-separated):") ?></header>
2013-02-23 18:28:09 +01:00
<fieldset>
<textarea dojoType='dijit.form.SimpleTextarea' rows='4'
style='width: 500px; font-size : 12px;'
name='tags'><?= $tags ?></textarea>
</fieldset>
2013-02-23 18:28:09 +01:00
<hr/>
2013-02-23 18:28:09 +01:00
<?= \Controls\submit_tag(__("Save")) ?>
</form>
</div>
<?php
2013-02-23 18:28:09 +01:00
}
function save() : void {
$tags = implode(", ",
FeedItem_Common::normalize_categories(explode(",", $_POST["tags"] ?? "")));
2013-02-23 18:28:09 +01:00
$this->host->set($this, "tags", $tags);
echo __("Configuration saved.");
}
2013-04-19 15:31:56 +02:00
function api_version() {
return 2;
}
}
2013-04-19 15:31:56 +02:00