Home / Widgets / Auto delete FluentCommunity drug shortages posts
Duplicate Snippet

Embed Snippet on Your Site

Auto delete FluentCommunity drug shortages posts

This code snippet automatically schedules the deletion of FluentCommunity posts created in a specific space after one month using WP-Cron. It also shows a small notice under each post with the planned deletion date, while keeping the post title unchanged and safely cleaning up scheduled events if the post is deleted manually first.

Code Preview
php
<?php
<?php
/**
 * Scoped auto-delete for FluentCommunity posts in one specific space only.
 *
 * What it does:
 * - Schedules deletion only for posts created in one target FluentCommunity space
 * - Deletes them after 1 month via WP-Cron
 * - Shows a small delete notice only on those targeted posts
 * - Does not modify titles or other feed fields globally
 * - Includes strict safety checks so it does not affect unrelated FluentCommunity behavior
 *
 * Use in WPCode / Code Snippets as a PHP snippet and run everywhere.
 */
if (!defined('ABSPATH')) {
    exit;
}
/*
|--------------------------------------------------------------------------
| Constants
|--------------------------------------------------------------------------
*/
if (!defined('PN_FCOM_DELETE_HOOK')) {
    define('PN_FCOM_DELETE_HOOK', 'pn_fcom_delete_space_feed_after_delay');
}
if (!defined('PN_FCOM_DELETE_META_KEY')) {
    define('PN_FCOM_DELETE_META_KEY', '_pn_fcom_delete_scheduled');
}
/*
|--------------------------------------------------------------------------
| Config
|--------------------------------------------------------------------------
|
| Override via filters if needed:
|
| add_filter('pn_fcom_target_space_slug', function () {
|     return 'drug-shortages';
| });
|
| add_filter('pn_fcom_delete_delay_seconds', function () {
|     return MONTH_IN_SECONDS;
| });
|
| add_filter('pn_fcom_enable_delete', '__return_true');
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_target_space_slug')) {
    function pn_fcom_target_space_slug() {
        return sanitize_title((string) apply_filters('pn_fcom_target_space_slug', 'drug-shortages'));
    }
}
if (!function_exists('pn_fcom_delete_delay_seconds')) {
    function pn_fcom_delete_delay_seconds() {
        $seconds = (int) apply_filters('pn_fcom_delete_delay_seconds', MONTH_IN_SECONDS);
        return max(5, $seconds);
    }
}
if (!function_exists('pn_fcom_enable_delete')) {
    function pn_fcom_enable_delete() {
        return (bool) apply_filters('pn_fcom_enable_delete', true);
    }
}
if (!function_exists('pn_fcom_notice_label_prefix')) {
    function pn_fcom_notice_label_prefix() {
        return (string) apply_filters('pn_fcom_notice_label_prefix', 'Προγραμματισμένη Διαγραφή:');
    }
}
if (!function_exists('pn_fcom_notice_label_text')) {
    function pn_fcom_notice_label_text($delete_at_text, $feed = null) {
        $text = 'Το post έχει προγραμματιστεί να διαγραφεί στις: ' . $delete_at_text;
        return (string) apply_filters('pn_fcom_notice_label_text', $text, $delete_at_text, $feed);
    }
}
/*
|--------------------------------------------------------------------------
| Logging
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_log')) {
    function pn_fcom_log($message, array $context = array()) {
        $enabled = (bool) apply_filters('pn_fcom_enable_debug_log', false);
        if (!$enabled) {
            return;
        }
        $line = '[PN_FCOM] ' . $message;
        if (!empty($context)) {
            $line .= ' | ' . wp_json_encode($context);
        }
        error_log($line);
    }
}
/*
|--------------------------------------------------------------------------
| Plugin availability
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_is_available')) {
    function pn_fcom_is_available() {
        return class_exists('\FluentCommunity\App\Models\Feed')
            && class_exists('\FluentCommunity\App\Models\Space');
    }
}
/*
|--------------------------------------------------------------------------
| Helpers
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_get_feed_value')) {
    function pn_fcom_get_feed_value($feed, $key, $default = null) {
        if (is_object($feed) && isset($feed->{$key})) {
            return $feed->{$key};
        }
        if (is_array($feed) && array_key_exists($key, $feed)) {
            return $feed[$key];
        }
        return $default;
    }
}
if (!function_exists('pn_fcom_is_valid_feed_payload')) {
    function pn_fcom_is_valid_feed_payload($feed) {
        return !empty($feed) && (is_object($feed) || is_array($feed));
    }
}
if (!function_exists('pn_fcom_get_feed_id')) {
    function pn_fcom_get_feed_id($feed) {
        return (int) pn_fcom_get_feed_value($feed, 'id', 0);
    }
}
if (!function_exists('pn_fcom_get_feed_space_id')) {
    function pn_fcom_get_feed_space_id($feed) {
        return (int) pn_fcom_get_feed_value($feed, 'space_id', 0);
    }
}
if (!function_exists('pn_fcom_get_feed_type')) {
    function pn_fcom_get_feed_type($feed) {
        return (string) pn_fcom_get_feed_value($feed, 'feed_type', '');
    }
}
if (!function_exists('pn_fcom_feed_exists')) {
    function pn_fcom_feed_exists($feed_id) {
        $feed_id = (int) $feed_id;
        if ($feed_id <= 0 || !pn_fcom_is_available()) {
            return false;
        }
        try {
            $feed_model_class = '\FluentCommunity\App\Models\Feed';
            $feed = $feed_model_class::find($feed_id);
            return !empty($feed);
        } catch (\Throwable $e) {
            pn_fcom_log('Feed exists check failed.', array(
                'feed_id' => $feed_id,
                'error'   => $e->getMessage(),
            ));
            return false;
        }
    }
}
if (!function_exists('pn_fcom_get_schedule_option_key')) {
    function pn_fcom_get_schedule_option_key($feed_id) {
        return PN_FCOM_DELETE_META_KEY . '_' . (int) $feed_id;
    }
}
if (!function_exists('pn_fcom_mark_feed_scheduled')) {
    function pn_fcom_mark_feed_scheduled($feed_id, $timestamp) {
        update_option(pn_fcom_get_schedule_option_key($feed_id), (int) $timestamp, false);
    }
}
if (!function_exists('pn_fcom_get_feed_scheduled_timestamp')) {
    function pn_fcom_get_feed_scheduled_timestamp($feed_id) {
        return (int) get_option(pn_fcom_get_schedule_option_key($feed_id), 0);
    }
}
if (!function_exists('pn_fcom_unmark_feed_scheduled')) {
    function pn_fcom_unmark_feed_scheduled($feed_id) {
        delete_option(pn_fcom_get_schedule_option_key($feed_id));
    }
}
if (!function_exists('pn_fcom_get_feed_delete_datetime_string')) {
    function pn_fcom_get_feed_delete_datetime_string($feed_id) {
        $timestamp = pn_fcom_get_feed_scheduled_timestamp($feed_id);
        if ($timestamp <= 0) {
            return '';
        }
        $format = (string) apply_filters('pn_fcom_delete_datetime_format', 'd/m/Y H:i');
        return wp_date($format, $timestamp);
    }
}
if (!function_exists('pn_fcom_notice_already_present')) {
    function pn_fcom_notice_already_present($html) {
        return is_string($html) && strpos($html, 'pn-fcom-delete-notice') !== false;
    }
}
if (!function_exists('pn_fcom_build_delete_notice_html')) {
    function pn_fcom_build_delete_notice_html($feed, $delete_at_text) {
        $prefix = pn_fcom_notice_label_prefix();
        $text   = pn_fcom_notice_label_text($delete_at_text, $feed);
        $html  = '<div class="pn-fcom-delete-notice" style="margin-top:10px;padding:8px 10px;border-radius:8px;background:#fff4e5;border:1px solid #ffd8a8;font-size:13px;line-height:1.4;">';
        $html .= '<strong>' . esc_html($prefix) . '</strong> ' . esc_html($text);
        $html .= '</div>';
        return (string) apply_filters('pn_fcom_build_delete_notice_html', $html, $feed, $delete_at_text);
    }
}
/*
|--------------------------------------------------------------------------
| Target space resolver
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_get_target_space_id')) {
    function pn_fcom_get_target_space_id() {
        static $cache = array();
        if (!pn_fcom_is_available()) {
            return 0;
        }
        $slug = pn_fcom_target_space_slug();
        if (!$slug) {
            return 0;
        }
        if (isset($cache[$slug])) {
            return (int) $cache[$slug];
        }
        $space_id = 0;
        try {
            $space_model_class = '\FluentCommunity\App\Models\Space';
            $space_id = (int) $space_model_class::query()
                ->where('slug', $slug)
                ->value('id');
        } catch (\Throwable $e) {
            pn_fcom_log('Space model lookup failed, trying SQL fallback.', array(
                'slug'  => $slug,
                'error' => $e->getMessage(),
            ));
        }
        if ($space_id <= 0) {
            global $wpdb;
            $table = $wpdb->prefix . 'fcom_spaces';
            $table_exists = $wpdb->get_var(
                $wpdb->prepare('SHOW TABLES LIKE %s', $table)
            );
            if ($table_exists === $table) {
                $space_id = (int) $wpdb->get_var(
                    $wpdb->prepare(
                        "SELECT id FROM {$table} WHERE slug = %s LIMIT 1",
                        $slug
                    )
                );
            }
        }
        $cache[$slug] = $space_id > 0 ? $space_id : 0;
        return (int) $cache[$slug];
    }
}
if (!function_exists('pn_fcom_is_target_space_feed')) {
    function pn_fcom_is_target_space_feed($feed) {
        $target_space_id = pn_fcom_get_target_space_id();
        if ($target_space_id <= 0) {
            return false;
        }
        $space_id = pn_fcom_get_feed_space_id($feed);
        return $space_id > 0 && $space_id === $target_space_id;
    }
}
if (!function_exists('pn_fcom_should_schedule_feed_deletion')) {
    function pn_fcom_should_schedule_feed_deletion($feed) {
        if (!pn_fcom_enable_delete() || !pn_fcom_is_available()) {
            return false;
        }
        if (!pn_fcom_is_valid_feed_payload($feed)) {
            pn_fcom_log('Invalid feed payload on create.');
            return false;
        }
        $feed_id   = pn_fcom_get_feed_id($feed);
        $space_id  = pn_fcom_get_feed_space_id($feed);
        $feed_type = pn_fcom_get_feed_type($feed);
        if ($feed_id <= 0 || $space_id <= 0) {
            pn_fcom_log('Feed payload missing id or space_id.', array(
                'feed_id'   => $feed_id,
                'space_id'  => $space_id,
                'feed_type' => $feed_type,
            ));
            return false;
        }
        if (!pn_fcom_is_target_space_feed($feed)) {
            return false;
        }
        if (!pn_fcom_feed_exists($feed_id)) {
            pn_fcom_log('Feed does not exist at schedule time.', array(
                'feed_id' => $feed_id,
            ));
            return false;
        }
        return true;
    }
}
/*
|--------------------------------------------------------------------------
| Cron scheduling
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_schedule_feed_deletion')) {
    function pn_fcom_schedule_feed_deletion($feed_id) {
        $feed_id = (int) $feed_id;
        if ($feed_id <= 0) {
            return false;
        }
        $args = array($feed_id);
        $existing = wp_next_scheduled(PN_FCOM_DELETE_HOOK, $args);
        if ($existing) {
            pn_fcom_log('Delete already scheduled.', array(
                'feed_id'   => $feed_id,
                'timestamp' => (int) $existing,
            ));
            return true;
        }
        $timestamp = current_time('timestamp', true) + pn_fcom_delete_delay_seconds();
        $scheduled = wp_schedule_single_event($timestamp, PN_FCOM_DELETE_HOOK, $args);
        if ($scheduled) {
            pn_fcom_mark_feed_scheduled($feed_id, $timestamp);
            pn_fcom_log('Delete scheduled.', array(
                'feed_id'   => $feed_id,
                'timestamp' => $timestamp,
                'delay'     => pn_fcom_delete_delay_seconds(),
            ));
            return true;
        }
        pn_fcom_log('Failed to schedule delete.', array(
            'feed_id' => $feed_id,
        ));
        return false;
    }
}
if (!function_exists('pn_fcom_unschedule_feed_deletion')) {
    function pn_fcom_unschedule_feed_deletion($feed_id) {
        $feed_id = (int) $feed_id;
        if ($feed_id <= 0) {
            return 0;
        }
        $args  = array($feed_id);
        $count = 0;
        while ($timestamp = wp_next_scheduled(PN_FCOM_DELETE_HOOK, $args)) {
            $unscheduled = wp_unschedule_event($timestamp, PN_FCOM_DELETE_HOOK, $args);
            if (!$unscheduled) {
                break;
            }
            $count++;
        }
        pn_fcom_unmark_feed_scheduled($feed_id);
        return $count;
    }
}
/*
|--------------------------------------------------------------------------
| Main create hook
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_schedule_space_feed_deletion')) {
    function pn_fcom_schedule_space_feed_deletion($feed) {
        if (!pn_fcom_should_schedule_feed_deletion($feed)) {
            return;
        }
        pn_fcom_schedule_feed_deletion(pn_fcom_get_feed_id($feed));
    }
}
/*
|--------------------------------------------------------------------------
| Render notice only for targeted feed
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_append_delete_notice_to_rendering_feed_model')) {
    function pn_fcom_append_delete_notice_to_rendering_feed_model($feed, $config = array()) {
        if (!pn_fcom_enable_delete() || !pn_fcom_is_available()) {
            return $feed;
        }
        if (empty($feed) || !is_object($feed)) {
            return $feed;
        }
        $feed_id  = isset($feed->id) ? (int) $feed->id : 0;
        $space_id = isset($feed->space_id) ? (int) $feed->space_id : 0;
        if ($feed_id <= 0 || $space_id <= 0) {
            return $feed;
        }
        if ($space_id !== pn_fcom_get_target_space_id()) {
            return $feed;
        }
        $delete_at_text = pn_fcom_get_feed_delete_datetime_string($feed_id);
        if (!$delete_at_text) {
            return $feed;
        }
        $notice_html = pn_fcom_build_delete_notice_html($feed, $delete_at_text);
        if (isset($feed->message_rendered) && is_string($feed->message_rendered)) {
            if (!pn_fcom_notice_already_present($feed->message_rendered)) {
                $feed->message_rendered .= $notice_html;
            }
            return $feed;
        }
        if (isset($feed->message) && is_string($feed->message)) {
            if (!pn_fcom_notice_already_present($feed->message)) {
                $feed->message .= $notice_html;
            }
        }
        return $feed;
    }
}
/*
|--------------------------------------------------------------------------
| Delete scheduled feed
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_delete_scheduled_space_feed')) {
    function pn_fcom_delete_scheduled_space_feed($feed_id) {
        $feed_id = (int) $feed_id;
        if ($feed_id <= 0) {
            return;
        }
        $scheduled_at = pn_fcom_get_feed_scheduled_timestamp($feed_id);
        pn_fcom_unmark_feed_scheduled($feed_id);
        if (!pn_fcom_enable_delete() || !pn_fcom_is_available()) {
            return;
        }
        try {
            $feed_model_class = '\FluentCommunity\App\Models\Feed';
            $feed = $feed_model_class::find($feed_id);
            if (!$feed) {
                return;
            }
            $target_space_id = pn_fcom_get_target_space_id();
            if ($target_space_id <= 0) {
                pn_fcom_log('Target space id not found during delete.', array(
                    'feed_id' => $feed_id,
                    'slug'    => pn_fcom_target_space_slug(),
                ));
                return;
            }
            $feed_space_id = isset($feed->space_id) ? (int) $feed->space_id : 0;
            $feed_status   = isset($feed->status) ? (string) $feed->status : '';
            $feed_type     = isset($feed->feed_type) ? (string) $feed->feed_type : '';
            if ($feed_space_id !== $target_space_id) {
                pn_fcom_log('Safety check prevented deletion.', array(
                    'feed_id'       => $feed_id,
                    'feed_space_id' => $feed_space_id,
                    'target_space'  => $target_space_id,
                ));
                return;
            }
            pn_fcom_log('Deleting feed.', array(
                'feed_id'     => $feed_id,
                'space_id'    => $feed_space_id,
                'status'      => $feed_status,
                'feed_type'   => $feed_type,
                'scheduledAt' => $scheduled_at,
            ));
            $feed->delete();
            pn_fcom_log('Feed deleted successfully.', array(
                'feed_id' => $feed_id,
            ));
        } catch (\Throwable $e) {
            pn_fcom_log('Exception while deleting feed.', array(
                'feed_id' => $feed_id,
                'error'   => $e->getMessage(),
            ));
        }
    }
}
/*
|--------------------------------------------------------------------------
| Cleanup if feed deleted manually first
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_cleanup_scheduled_delete')) {
    function pn_fcom_cleanup_scheduled_delete($feed_id) {
        if (is_object($feed_id)) {
            $feed_id = isset($feed_id->id) ? (int) $feed_id->id : 0;
        } elseif (is_array($feed_id)) {
            $feed_id = isset($feed_id['id']) ? (int) $feed_id['id'] : 0;
        } else {
            $feed_id = (int) $feed_id;
        }
        if ($feed_id <= 0) {
            return;
        }
        pn_fcom_unschedule_feed_deletion($feed_id);
    }
}
/*
|--------------------------------------------------------------------------
| Register hooks only when WordPress is ready
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_register_hooks')) {
    function pn_fcom_register_hooks() {
        add_action('fluent_community/space_feed/created', 'pn_fcom_schedule_space_feed_deletion', 10, 1);
        add_action('fluent_community/feed/created', 'pn_fcom_schedule_space_feed_deletion', 10, 1);
        add_filter('fluent_community/rendering_feed_model', 'pn_fcom_append_delete_notice_to_rendering_feed_model', 10, 2);
        add_action(PN_FCOM_DELETE_HOOK, 'pn_fcom_delete_scheduled_space_feed', 10, 1);
        add_action('fluent_community/feed/deleted', 'pn_fcom_cleanup_scheduled_delete', 10, 1);
        add_action('fluent_community/space_feed/deleted', 'pn_fcom_cleanup_scheduled_delete', 10, 1);
    }
}
add_action('init', 'pn_fcom_register_hooks', 20);

Comments

Add a Comment