Hello! 欢迎来到盒子萌!

wordpress免插件代码实现七牛云存储


avatar
嘉木 2014-06-01 128

这几天 博主的站有时候打开很慢,发现时谷歌被墙的原因,目前发现有2个地址:

fonts.googleapis.com  谷歌的字体文件和后台的 themes.googleusercontent.com

有时候打开一直显示正在连接,博客首页就是全空白状态,这搞的我好蛋疼啊。

目前还没有解决的方法,然后博主网上找了七牛免插件CDN缓存的方法,现在就可以打开了。

下面是实现的方法:

把代码放到functions.php文件?php?>之间就可以了。

刷新网站首页,查看源代码看看,是不是都变成七牛的链接了,OK搞定

//免插件七牛CDN加速
define('FocusCDNHost','https://www.isays.cn');//网站地址
define('FocusCDNRemote','http://isays.qiniudn.com');//cdn地址
define('FocusCDNIncludes','wp-content,wp-includes');//cdn加速目录
define('FocusCDNExcludes','.php|.xml');//不缓存文件后缀
define('FocusCDNRelative','');//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog's domain as prefix.

function do_cdnrewrite_ob_start() {
$rewriter = new FocusCDNRewriteWordpress();
$rewriter->register_as_output_buffer();
}
add_action('template_redirect', 'do_cdnrewrite_ob_start');

class FocusCDNRewriteWordpress extends FocusCDNRewrite
{
function __construct() {
$excl_tmp = FocusCDNExcludes;
$excludes = array_map('trim', explode('|', $excl_tmp));

parent::__construct(
FocusCDNHost,
FocusCDNRemote,
FocusCDNIncludes,
$excludes,
!!FocusCDNRelative
);
}
public function register_as_output_buffer() {
if ($this->blog_url != FocusCDNRemote) {
ob_start(array(&$this, 'rewrite'));
}
}

}

class FocusCDNRewrite {
var $blog_url = null;//String: the blog's URL
var $cdn_url = null;//String: URL of a CDN domain
var $include_dirs = null;//String: directories to include in static file matching, comma-delimited list
var $excludes = array();//Array: strings which indicate that a given element should not be rewritten (e.g. ".php")
var $rootrelative = false;//Boolean: if true, modifies root-relative links

function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
$this->blog_url = $blog_url;
$this->cdn_url = $cdn_url;
$this->include_dirs = $include_dirs;
$this->excludes = $excludes;
$this->rootrelative = $root_relative;
}

protected function exclude_single(&$match) {
foreach ($this->excludes as $badword) {
if (stristr($match, $badword) != false) {
return true;
}
}
return false;
}

protected function rewrite_single(&$match) {
if ($this->exclude_single($match[0])) {
return $match[0];
} else {
if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
return str_replace($this->blog_url, $this->cdn_url, $match[0]);
} else {
return $this->cdn_url . $match[0];
}
}
}

protected function include_dirs_to_pattern() {
$input = explode(',', $this->include_dirs);
if ($this->include_dirs == '' || count($input) < 1) {
return 'wp\-content|wp\-includes';
} else {
return implode('|', array_map('quotemeta', array_map('trim', $input)));
}
}

public function rewrite(&$content) {
$dirs = $this->include_dirs_to_pattern();
$regex = '#(?<=[(\"\'])';
$regex .= $this->rootrelative
? ('(?:'.quotemeta($this->blog_url).')?')
: quotemeta($this->blog_url);
$regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
}

}

博主继续研究谷歌字体去- -

暂无评论

发表评论