1
0
Fork 0
mirror of https://github.com/yunluo/gdk.git synced 2024-05-17 21:56:40 +02:00
gdk/class/widget_cache.php
云落 2f66c5ddfe 1,优化了后台选项,去除了debug出来的报错
2,增加了微信,支付宝H5支付
3,优化了小工具缓存功能
4,去除暂时无用的文件
5,优化了metabox的代码
6,取消了设置友情链接分类ID功能,取代为自动获取
7,断代码模块去除多余无用代码
8,优化了导航页面的很多代码
2021-10-07 22:26:17 +08:00

71 lines
2.3 KiB
PHP

<?php
class Auto_Widget_cache
{
public $cache_time = 18000;
/*
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
*/
public function __construct()
{
add_filter('widget_display_callback', array($this, '_cache_widget_output'), 10, 3);
add_action('in_widget_form', array($this, 'in_widget_form'), 5, 3);
add_filter('widget_update_callback', array($this, 'widget_update_callback'), 5, 3);
}
public function get_widget_key($i, $a)
{
return 'WC-' . md5(serialize(array($i, $a)));
}
public function cache_widget_output($instance, $widget, $args)
{
if (false === $instance) {
return $instance;
}
if (isset($instance['wc_cache']) && $instance['wc_cache'] == true) {
return $instance;
}
$timer_start = microtime(true);
$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;
echo '<!-- From widget cache in ' . number_format(microtime(true) - $timer_start, 5) . ' seconds -->';
return false;
}
public function in_widget_form($t, $return, $instance)
{
$instance = wp_parse_args((array)$instance, array('title' => '', 'text' => '', 'wc_cache' => null));
if (!isset($instance['wc_cache'])) {
$instance['wc_cache'] = null;
}
?>
<p>
<input id="<?php
echo $t->get_field_id('wc_cache'); ?>" name="<?php
echo $t->get_field_name('wc_cache'); ?>" type="checkbox" <?php checked($instance['wc_cache'] ?? 0); ?> />
<label for="<?php
echo $t->get_field_id('wc_cache'); ?>">禁止缓存本工具?</label>
</p>
<?php
}
public function widget_update_callback($instance, $new_instance, $old_instance)
{
$instance['wc_cache'] = isset($new_instance['wc_cache']);
return $instance;
}
}
if( gdk_option('gdk_sidebar_cache')){
$GLOBALS['Auto_Widget_cache'] = new Auto_Widget_cache();
}