1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-11 10:46:52 +02:00
gdk/class/widget_cache.php

76 lines
2.3 KiB
PHP
Raw Permalink Normal View History

2020-01-25 13:22:34 +01:00
<?php
2020-03-15 14:29:06 +01:00
class Auto_Widget_cache
{
2020-01-25 13:22:34 +01:00
public $cache_time = 18000;
2021-10-10 15:48:53 +02:00
2020-01-25 13:22:34 +01:00
/*
MINUTE_IN_SECONDS = 60 seconds
HOUR_IN_SECONDS = 3600 seconds
DAY_IN_SECONDS = 86400 seconds
WEEK_IN_SECONDS = 604800 seconds
YEAR_IN_SECONDS = 3153600 seconds
2020-03-15 14:29:06 +01:00
*/
public function __construct()
{
2021-10-10 15:48:53 +02:00
add_filter('widget_display_callback', [$this, '_cache_widget_output'], 10, 3);
add_action('in_widget_form', [$this, 'in_widget_form'], 5, 3);
add_filter('widget_update_callback', [$this, 'widget_update_callback'], 5, 3);
2020-01-25 13:22:34 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
public function get_widget_key($i, $a)
{
2021-10-10 15:48:53 +02:00
return 'WC-'.md5(serialize([$i, $a]));
2020-01-25 13:22:34 +01:00
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
public function cache_widget_output($instance, $widget, $args)
{
if (false === $instance) {
return $instance;
}
2021-10-10 15:48:53 +02:00
if (isset($instance['wc_cache']) && true == $instance['wc_cache']) {
2020-03-15 14:29:06 +01:00
return $instance;
}
2021-10-10 15:48:53 +02:00
$timer_start = microtime(true);
2020-01-25 13:22:34 +01:00
$transient_name = $this->get_widget_key($instance, $args);
if (false === ($cached_widget = get_transient($transient_name))) {
ob_start();
$widget->widget($args, $instance);
$cached_widget = ob_get_clean();
set_transient($transient_name, $cached_widget, $this->cache_time);
}
echo $cached_widget;
2021-10-10 15:48:53 +02:00
echo '<!-- From widget cache in '.number_format(microtime(true) - $timer_start, 5).' seconds -->';
2020-01-25 13:22:34 +01:00
return false;
}
2021-10-10 15:48:53 +02:00
2020-03-15 14:29:06 +01:00
public function in_widget_form($t, $return, $instance)
{
2021-10-10 15:48:53 +02:00
$instance = wp_parse_args((array) $instance, ['title' => '', 'text' => '', 'wc_cache' => null]);
2020-03-15 14:29:06 +01:00
if (!isset($instance['wc_cache'])) {
$instance['wc_cache'] = null;
2021-10-10 15:48:53 +02:00
} ?>
<p>
<input id="<?php
2020-03-15 14:29:06 +01:00
echo $t->get_field_id('wc_cache'); ?>" name="<?php
echo $t->get_field_name('wc_cache'); ?>" type="checkbox" <?php checked($instance['wc_cache'] ?? 0); ?> />
2021-10-10 15:48:53 +02:00
<label for="<?php
2020-03-15 14:29:06 +01:00
echo $t->get_field_id('wc_cache'); ?>">禁止缓存本工具?</label>
2021-10-10 15:48:53 +02:00
</p>
<?php
}
2020-03-15 14:29:06 +01:00
public function widget_update_callback($instance, $new_instance, $old_instance)
{
2020-01-25 13:22:34 +01:00
$instance['wc_cache'] = isset($new_instance['wc_cache']);
2021-10-10 15:48:53 +02:00
2020-01-25 13:22:34 +01:00
return $instance;
}
2020-03-15 14:29:06 +01:00
}
2021-10-10 15:48:53 +02:00
if (gdk_option('gdk_sidebar_cache')) {
$GLOBALS['Auto_Widget_cache'] = new Auto_Widget_cache();
}