This repository has been archived on 2024-09-05. You can view files and clone it, but cannot push or open issues or pull requests.
shimmie2/ext/featured/main.php

87 lines
2.9 KiB
PHP
Raw Normal View History

2021-12-14 18:32:47 +00:00
<?php
declare(strict_types=1);
namespace Shimmie2;
class Featured extends Extension
{
2020-02-04 00:46:36 +00:00
/** @var FeaturedTheme */
2023-06-27 14:56:49 +00:00
protected Themelet $theme;
2020-02-04 00:46:36 +00:00
public function onInitExt(InitExtEvent $event): void
{
global $config;
$config->set_default_int('featured_id', 0);
}
public function onPageRequest(PageRequestEvent $event): void
{
global $config, $page, $user;
2024-04-07 09:25:58 +00:00
if ($event->page_matches("featured_image/set/{image_id}", method: "POST", permission: Permissions::EDIT_FEATURE)) {
$id = $event->get_iarg('image_id');
2024-02-11 11:34:09 +00:00
$config->set_int("featured_id", $id);
log_info("featured", "Featured post set to >>$id", "Featured post set");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(make_link("post/view/$id"));
}
if ($event->page_matches("featured_image/download")) {
$image = Image::by_id($config->get_int("featured_id"));
if (!is_null($image)) {
$page->set_mode(PageMode::DATA);
$page->set_mime($image->get_mime());
$page->set_data(\Safe\file_get_contents($image->get_image_filename()));
}
2024-02-11 11:34:09 +00:00
}
if ($event->page_matches("featured_image/view")) {
$image = Image::by_id($config->get_int("featured_id"));
if (!is_null($image)) {
send_event(new DisplayingImageEvent($image));
}
}
}
public function onPostListBuilding(PostListBuildingEvent $event): void
{
global $cache, $config, $page, $user;
$fid = $config->get_int("featured_id");
if ($fid > 0) {
2023-12-14 17:06:54 +00:00
$image = cache_get_or_set(
"featured_image_object:$fid",
function () use ($fid) {
$image = Image::by_id($fid);
if ($image) { // make sure the object is fully populated before saving
$image->get_tag_array();
}
return $image;
},
600
);
if (!is_null($image)) {
if (Extension::is_enabled(RatingsInfo::KEY)) {
if (!in_array($image['rating'], Ratings::get_user_class_privs($user))) {
return;
}
}
$this->theme->display_featured($page, $image);
}
}
}
public function onImageDeletion(ImageDeletionEvent $event): void
{
global $config;
2023-02-02 16:50:09 +00:00
if ($event->image->id == $config->get_int("featured_id")) {
$config->delete("featured_id");
}
}
public function onImageAdminBlockBuilding(ImageAdminBlockBuildingEvent $event): void
{
global $user;
if ($user->can(Permissions::EDIT_FEATURE) && $event->context == "view") {
2024-02-10 18:35:55 +00:00
$event->add_button("Feature This", "featured_image/set/{$event->image->id}");
}
}
}