Home / Admin / FluentCommunity Auto-Delete Diagnostics
Duplicate Snippet

Embed Snippet on Your Site

FluentCommunity Auto-Delete Diagnostics

This diagnostic snippet monitors and logs how the FluentCommunity auto-delete system behaves, without needing debug.log. It records environment details, tracks when FluentCommunity post creation, deletion, and scheduled cron hooks run, stores those logs in WordPress options, and shows everything in a custom WordPress dashboard widget so you can quickly verify whether the auto-delete snippet is working correctly.

Code Preview
php
<?php
/**
 * PN FluentCommunity Auto-Delete Diagnostics
 * No debug.log required
 */
if (!defined('ABSPATH')) {
    exit;
}
/*
|--------------------------------------------------------------------------
| Enable main snippet internal logs too
|--------------------------------------------------------------------------
*/
add_filter('pn_fcom_enable_debug_log', '__return_true');
/*
|--------------------------------------------------------------------------
| Constants
|--------------------------------------------------------------------------
*/
if (!defined('PN_FCOM_DIAG_OPTION')) {
    define('PN_FCOM_DIAG_OPTION', 'pn_fcom_diag_entries');
}
if (!defined('PN_FCOM_DIAG_MAX')) {
    define('PN_FCOM_DIAG_MAX', 250);
}
/*
|--------------------------------------------------------------------------
| Logger storage
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_diag_add_log')) {
    function pn_fcom_diag_add_log($label, $context = array()) {
        $entries = get_option(PN_FCOM_DIAG_OPTION, array());
        if (!is_array($entries)) {
            $entries = array();
        }
        $entries[] = array(
            'time'    => current_time('mysql'),
            'label'   => (string) $label,
            'context' => is_array($context) ? $context : array('value' => $context),
        );
        if (count($entries) > PN_FCOM_DIAG_MAX) {
            $entries = array_slice($entries, -PN_FCOM_DIAG_MAX);
        }
        update_option(PN_FCOM_DIAG_OPTION, $entries, false);
    }
}
if (!function_exists('pn_fcom_diag_get_logs')) {
    function pn_fcom_diag_get_logs() {
        $entries = get_option(PN_FCOM_DIAG_OPTION, array());
        return is_array($entries) ? $entries : array();
    }
}
if (!function_exists('pn_fcom_diag_clear_logs')) {
    function pn_fcom_diag_clear_logs() {
        delete_option(PN_FCOM_DIAG_OPTION);
    }
}
/*
|--------------------------------------------------------------------------
| Safe export helpers
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_diag_export')) {
    function pn_fcom_diag_export($value) {
        if (is_array($value)) {
            return $value;
        }
        if (is_object($value)) {
            return array(
                'class' => get_class($value),
                'vars'  => get_object_vars($value),
            );
        }
        return array(
            'type'  => gettype($value),
            'value' => $value,
        );
    }
}
if (!function_exists('pn_fcom_diag_feed_value')) {
    function pn_fcom_diag_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_diag_feed_summary')) {
    function pn_fcom_diag_feed_summary($feed) {
        return array(
            'id'        => (int) pn_fcom_diag_feed_value($feed, 'id', 0),
            'space_id'  => (int) pn_fcom_diag_feed_value($feed, 'space_id', 0),
            'feed_type' => (string) pn_fcom_diag_feed_value($feed, 'feed_type', ''),
            'status'    => (string) pn_fcom_diag_feed_value($feed, 'status', ''),
            'parent_id' => (int) pn_fcom_diag_feed_value($feed, 'parent_id', 0),
            'user_id'   => (int) pn_fcom_diag_feed_value($feed, 'user_id', 0),
        );
    }
}
/*
|--------------------------------------------------------------------------
| Environment
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_diag_get_plugin_version')) {
    function pn_fcom_diag_get_plugin_version($plugin_file) {
        if (!function_exists('get_plugin_data')) {
            require_once ABSPATH . 'wp-admin/includes/plugin.php';
        }
        $path = WP_PLUGIN_DIR . '/' . ltrim($plugin_file, '/');
        if (!file_exists($path)) {
            return 'not found';
        }
        $data = get_plugin_data($path, false, false);
        return !empty($data['Version']) ? $data['Version'] : 'unknown';
    }
}
if (!function_exists('pn_fcom_diag_environment')) {
    function pn_fcom_diag_environment() {
        global $wpdb;
        $target_slug = function_exists('pn_fcom_target_space_slug')
            ? pn_fcom_target_space_slug()
            : 'function not available';
        $target_id = function_exists('pn_fcom_get_target_space_id')
            ? pn_fcom_get_target_space_id()
            : 'function not available';
        return array(
            'site_url'                => site_url(),
            'wp_version'              => get_bloginfo('version'),
            'php_version'             => PHP_VERSION,
            'db_prefix'               => $wpdb->prefix,
            'fluentcommunity_version' => pn_fcom_diag_get_plugin_version('fluent-community/fluent-community.php'),
            'code_snippets_version'   => pn_fcom_diag_get_plugin_version('code-snippets/code-snippets.php'),
            'feed_model_exists'       => class_exists('\FluentCommunity\App\Models\Feed') ? 'yes' : 'no',
            'space_model_exists'      => class_exists('\FluentCommunity\App\Models\Space') ? 'yes' : 'no',
            'target_space_slug'       => $target_slug,
            'target_space_id'         => $target_id,
            'delete_hook_defined'     => defined('PN_FCOM_DELETE_HOOK') ? 'yes' : 'no',
            'delete_hook_name'        => defined('PN_FCOM_DELETE_HOOK') ? PN_FCOM_DELETE_HOOK : 'not defined',
            'delete_delay_seconds'    => function_exists('pn_fcom_delete_delay_seconds') ? (int) pn_fcom_delete_delay_seconds() : 'function not available',
            'delete_enabled'          => function_exists('pn_fcom_enable_delete') ? (pn_fcom_enable_delete() ? 'yes' : 'no') : 'function not available',
        );
    }
}
if (!function_exists('pn_fcom_diag_log_environment_once')) {
    function pn_fcom_diag_log_environment_once() {
        static $done = false;
        if ($done) {
            return;
        }
        $done = true;
        pn_fcom_diag_add_log('Environment', pn_fcom_diag_environment());
    }
}
/*
|--------------------------------------------------------------------------
| Log main snippet messages too
|--------------------------------------------------------------------------
*/
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;
        }
        pn_fcom_diag_add_log('[MAIN] ' . $message, $context);
    }
}
/*
|--------------------------------------------------------------------------
| Request boot
|--------------------------------------------------------------------------
*/
add_action('init', function () {
    pn_fcom_diag_log_environment_once();
}, 20);
/*
|--------------------------------------------------------------------------
| Watch FluentCommunity hooks
|--------------------------------------------------------------------------
*/
add_action('fluent_community/feed/created', function ($feed) {
    pn_fcom_diag_log_environment_once();
    $summary = pn_fcom_diag_feed_summary($feed);
    pn_fcom_diag_add_log('Hook: fluent_community/feed/created summary', $summary);
    pn_fcom_diag_add_log('Hook: fluent_community/feed/created payload', pn_fcom_diag_export($feed));
    if (!empty($summary['id']) && defined('PN_FCOM_DELETE_HOOK')) {
        $next = wp_next_scheduled(PN_FCOM_DELETE_HOOK, array((int) $summary['id']));
        pn_fcom_diag_add_log('Schedule lookup after create', array(
            'feed_id'        => (int) $summary['id'],
            'hook'           => PN_FCOM_DELETE_HOOK,
            'next_scheduled' => $next ? (int) $next : 0,
            'datetime_local' => $next ? wp_date('Y-m-d H:i:s', (int) $next) : '',
        ));
    }
}, 1, 1);
add_action('fluent_community/feed/deleted', function ($feed) {
    pn_fcom_diag_log_environment_once();
    if (is_numeric($feed)) {
        pn_fcom_diag_add_log('Hook: fluent_community/feed/deleted', array(
            'type'    => 'numeric_id',
            'feed_id' => (int) $feed,
        ));
        return;
    }
    pn_fcom_diag_add_log('Hook: fluent_community/feed/deleted', pn_fcom_diag_export($feed));
}, 1, 1);
if (defined('PN_FCOM_DELETE_HOOK')) {
    add_action(PN_FCOM_DELETE_HOOK, function ($feed_id) {
        pn_fcom_diag_log_environment_once();
        pn_fcom_diag_add_log('Cron delete hook executed', array(
            'feed_id' => (int) $feed_id,
            'hook'    => PN_FCOM_DELETE_HOOK,
            'time'    => current_time('mysql'),
        ));
    }, 1, 1);
}
/*
|--------------------------------------------------------------------------
| Dashboard widget
|--------------------------------------------------------------------------
*/
if (!function_exists('pn_fcom_diag_render_dashboard_widget')) {
    function pn_fcom_diag_render_dashboard_widget() {
        if (!current_user_can('manage_options')) {
            echo '<p>Δεν έχεις δικαίωμα πρόσβασης.</p>';
            return;
        }
        if (isset($_GET['pn_fcom_clear_logs']) && check_admin_referer('pn_fcom_clear_logs_action')) {
            pn_fcom_diag_clear_logs();
            echo '<div class="notice notice-success"><p>Τα logs καθαρίστηκαν.</p></div>';
        }
        $env  = pn_fcom_diag_environment();
        $logs = pn_fcom_diag_get_logs();
        echo '<div style="display:grid; gap:16px;">';
        echo '<div>';
        echo '<p><strong>PN FCOM Diagnostics</strong></p>';
        echo '<p>Έλεγχος για το FluentCommunity auto-delete snippet χωρίς debug.log.</p>';
        echo '<p>';
        echo '<a class="button button-primary" href="' . esc_url(admin_url()) . '">Refresh Dashboard</a> ';
        echo '<a class="button" href="' . esc_url(wp_nonce_url(admin_url('index.php?pn_fcom_clear_logs=1'), 'pn_fcom_clear_logs_action')) . '">Clear Logs</a>';
        echo '</p>';
        echo '</div>';
        echo '<table class="widefat striped">';
        echo '<tbody>';
        foreach ($env as $key => $value) {
            echo '<tr>';
            echo '<td style="width:260px;"><strong>' . esc_html($key) . '</strong></td>';
            echo '<td><code>' . esc_html(is_scalar($value) ? (string) $value : wp_json_encode($value)) . '</code></td>';
            echo '</tr>';
        }
        echo '</tbody>';
        echo '</table>';
        echo '<div>';
        echo '<p><strong>Latest logs</strong> (' . (int) count($logs) . ')</p>';
        echo '<textarea readonly style="width:100%; min-height:430px; font-family:monospace; white-space:pre; overflow:auto;">';
        foreach ($logs as $entry) {
            $line = '[' . (isset($entry['time']) ? $entry['time'] : '') . '] ' .
                    (isset($entry['label']) ? $entry['label'] : '');
            if (!empty($entry['context'])) {
                $json = wp_json_encode($entry['context'], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
                $line .= ' | ' . $json;
            }
            echo esc_textarea($line . PHP_EOL);
        }
        echo '</textarea>';
        echo '</div>';
        echo '</div>';
    }
}
add_action('wp_dashboard_setup', function () {
    wp_add_dashboard_widget(
        'pn_fcom_diag_dashboard_widget_nologfile',
        'PN FCOM Diagnostics',
        'pn_fcom_diag_render_dashboard_widget'
    );
});
'; } } add_action('wp_dashboard_setup', function () { wp_add_dashboard_widget( 'pn_fcom_diag_dashboard_widget_nologfile', 'PN FCOM Diagnostics', 'pn_fcom_diag_render_dashboard_widget' ); });

Comments

Add a Comment