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