HEX
Server: nginx/1.18.0
System: Linux test-ipsremont 5.4.0-214-generic #234-Ubuntu SMP Fri Mar 14 23:50:27 UTC 2025 x86_64
User: ips (1000)
PHP: 8.0.30
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/quadcodewordpressapi/public/wp-content/plugins/cache/cache.php
<?php
/*
Plugin Name: Nginx Cache Plugin
Description: A plugin for reseting cached pages on Nginx server.
Version:     1.0.1
*/

add_action('save_post', 'resetCacheAfterPostUpdated', 10, 3);

function cache_plugin_updates()
{
    $pluginVersion = get_site_option('cachePluginVersion');

    switch ($pluginVersion) {
        case '':
            update_option('cachePluginVersion', '1.0.0');
            break;
        default:
            break;
    }
}

add_action('plugins_loaded', 'cache_plugin_updates');

function resetCacheAfterPostUpdated($post_id, WP_Post $post, $update)
{
    if (wp_is_post_revision($post_id)) {
        return;
    }

    if (!in_array($post->post_type, ['post', 'glossary'])) {
        return;
    }

    $meta = get_post_meta($post_id);

    switch ($post->post_type) {
        case 'post':
            $postPrefix = 'blog';
            break;
        case 'glossary':
            $postPrefix = 'glossary';
            break;
        default:
            break;
    }

    // Current post path
    $postPaths = [$postPrefix . '/' . $post->post_name];

    $availableLangs = ['es', 'pt', 'ru', 'vi', 'th', 'ko'];
    foreach ($availableLangs as $availableLang) {
        $postPaths[] = $availableLang . '/' . $postPrefix . '/' . $post->post_name;
    }

    // Revision post paths
    if (!empty($meta['_wp_old_slug'])) {
        $lastRevisionSlug = array_pop($meta['_wp_old_slug']);
        $postPaths[] = $postPrefix . '/' . $lastRevisionSlug;
        foreach ($availableLangs as $availableLang) {
            $postPaths[] = $availableLang . '/' . $postPrefix . '/' . $lastRevisionSlug;
        }
    }

    $settings = get_option('cache_settings');

    foreach ($postPaths as $postPath) {
        $resetUrl = rtrim($settings['reset_url'], '/') . '/purge/' . $postPath;
        try {
            sendRequest($resetUrl);
        } catch (Exception $e) {
            error_log('Cache reset error: ' . $e->getMessage());
        }
    }
}

add_action('admin_menu', 'cache_add_admin_menu');
add_action('admin_init', 'cache_settings_init');

function cache_add_admin_menu()
{
    add_options_page(
        'Cache Settings',
        'Cache',
        'manage_options',
        'cache-settings-form',
        'cache_settings_page'
    );
}

function cache_settings_page()
{
    if (!empty($_POST['clearAll'])) {
        $settings = get_option('cache_settings');
        $resetUrl = rtrim($settings['reset_url'], '/') . '/purge_all';

        if (empty($settings['reset_url'])) {
            add_settings_error(
                'cache_settings',
                'cache_reset_url_empty',
                esc_html__('Cache reset error: Cache Reset Url is empty', 'nginx-cache-plugin'),
                'error'
            );
        } else {
            try {
                sendRequest($resetUrl);
                add_settings_error(
                    'cache_settings',
                    'cache_cleared_success',
                    esc_html__('Cache cleared successfully.', 'nginx-cache-plugin'),
                    'updated'
                );
            } catch (Exception $e) {
                add_settings_error(
                    'cache_settings',
                    'cache_reset_url_empty',
                    esc_html__('Cache cleaned error: ' . $e->getMessage(), 'nginx-cache-plugin'),
                    'error'
                );
            }
        }
    }
    ?>
    <form action="options.php" method="post">
        <h2>Cache Settings</h2>
        <?php
        settings_errors('cache_settings');
        settings_fields('cache_settings');
        do_settings_sections('cache-settings-form');
        submit_button();
        ?>
    </form>
    <?php
    cache_button_render();
}

function cache_settings_init()
{
    register_setting('cache_settings', 'cache_settings');

    add_settings_section(
        'cache_section_main',
        '',
        null,
        'cache-settings-form'
    );

    add_settings_field(
        'cache_reset_url',
        'Cache Reset Url',
        'cache_reset_url_render',
        'cache-settings-form',
        'cache_section_main'
    );
}

function cache_reset_url_render()
{
    $options = get_option('cache_settings');
    ?>
    <input type="text" name="cache_settings[reset_url]" value="<?php echo esc_attr($options['reset_url'] ?? ''); ?>" size="50">
    <p class="description">ex. https://test.quadcode.com/</p>
    <?php
}

function cache_button_render()
{
    ?>
    <form action="/wp-admin/options-general.php?page=cache-settings-form" method="post">
        <input type="hidden" name="clearAll" value="true">
        <button type="submit" class="button button-primary">Reset All Cache</button>
    </form>
    <?php
}

add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'cache_settings_link');

function cache_settings_link($links)
{
    $settings_link = '<a href="options-general.php?page=cache-settings-form">' . __('Settings') . '</a>';
    array_unshift($links, $settings_link);

    return $links;
}


function sendRequest($url, $body = [], $headers = [])
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    if (empty($body)) {
        curl_setopt($ch, CURLOPT_POST, false);
    } else {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
        $headers[] = "Content-Type: application/json";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    $response = curl_exec($ch);

    return $response;
}