'.$content.'
'.$after;
return $content;
}
add_filter('the_content', 'gdk_add_content');
//头像解决方案
function gdk_switch_get_avatar( $avatar ) {
switch (gdk_option('gdk_switch_get_avatar')) {
case 1:
$rand_avatar = 'https://cdn.jsdelivr.net/gh/yunluo/GitCafeApi/avatar/' . mt_rand(1, 1999) . '.jpg';
$avatar = "
";
break;
case 2:
$avatar = preg_replace("/http[s]{0,1}:\/\/(secure|www|\d).gravatar.com\/avatar\//","//cdn.v2ex.com/gravatar/",$avatar);
break;
default:
$avatar = preg_replace("/http[s]{0,1}:\/\/(secure|www|\d).gravatar.com\/avatar\//","//dn-qiniu-avatar.qbox.me/avatar/",$avatar);
}
return $avatar;
}
add_filter('get_avatar', 'gdk_switch_get_avatar');
//懒加载
if(gdk_option('gdk_lazyload')){
function gdk_lazyload($content) {
if (!is_feed() || !is_robots()) {
$content = preg_replace('/
/i', "
\n", $content);
}
return $content;
}
add_filter('the_content', 'gdk_lazyload');
}
//fancybox图片灯箱效果
if(gdk_option('gdk_lazyload')){
function gdk_fancybox($content) {
$pattern = "/]*).(bmp|gif|jpeg|jpg|png|swf)('|\")(.*?)>(.*?)<\\/a>/i";
$replacement = '$7';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'gdk_fancybox');
}
//GO跳转
function gdk_link_go($content) {
if(file_exists(ABSPATH.'go.php')) {
$gourl = home_url().'/go.php';
} else {
$gourl = GDK_BASE_URL.'class/go.php';
}
preg_match_all('//',$content,$matches);
if($matches) {
foreach($matches[2] as $val) {
if(in_string($val,'://') && !in_string($val,home_url()) && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)) {
$content=str_replace("href=\"$val\"", "href=\"".$gourl."?url=$val\" ",$content);
}
}
}
return $content;
}
//邮箱SMTP设置
function gdk_smtp( $phpmailer ) {
$phpmailer->FromName = gdk_option('gdk_smtp_mail'); //邮箱地址
$phpmailer->Host = gdk_option('gdk_smtp_host');//服务器地址
$phpmailer->Port = gdk_option('gdk_smtp_port'); //端口
$phpmailer->Username = gdk_option('gdk_smtp_username'); //昵称
$phpmailer->Password = gdk_option('gdk_smtp_password'); //密码
$phpmailer->From = gdk_option('gdk_smtp_mail'); //邮箱地址
$phpmailer->SMTPAuth = true;
$phpmailer->SMTPSecure = 'ssl';
$phpmailer->IsSMTP();
}
// CDN
function gdk_cdn_start() {
ob_start('gdk_cdn_replace');
}
function gdk_cdn_replace($html) {
$local_host = home_url();//博客域名
$cdn_host = gdk_option('gdk_cdn_host');//cdn域名
$cdn_exts = gdk_option('gdk_cdn_ext');//扩展名(使用|分隔)
$cdn_dirs = gdk_option('gdk_cdn_dir');//目录(使用|分隔)
$cdn_dirs = str_replace('-', '\-', $cdn_dirs);
if ($cdn_dirs) {
$regex = '/' . str_replace('/', '\/', $local_host) . '\/((' . $cdn_dirs . ')\/[^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/';
$html = preg_replace($regex, $cdn_host . '/$1$4', $html);
} else {
$regex = '/' . str_replace('/', '\/', $local_host) . '\/([^\s\?\\\'\"\;\>\<]{1,}.(' . $cdn_exts . '))([\"\\\'\s\?]{1})/';
$html = preg_replace($regex, $cdn_host . '/$1$3', $html);
}
return $html;
}
//CDN水印
function gdk_cdn_water($content) {
if (get_post_type() == 'post') {
$pattern = "/
/i";
$replacement = '
';
$content = preg_replace($pattern, $replacement, $content);
}
return $content;
}
//自动替换媒体库图片的域名
if (is_admin() && gdk_option('gdk_cdn')) {
function attachment_replace($text) {
$replace = array(
home_url() => gdk_option('gdk_cdn')
);
$text = str_replace(array_keys($replace) , $replace, $text);
return $text;
}
add_filter('wp_get_attachment_url', 'attachment_replace');
}
//压缩html代码
if (gdk_option('gdk_compress')) {
function gdk_compress_html(){
function gdk_compress_html_callback($buffer){
if ( substr( ltrim( $buffer ), 0, 5) == '", $buffer);
$count = count($buffer);
$i = '';
for ($i = 0; $i <= $count; $i++) {
if (stristr($buffer[$i], '')) {
$buffer[$i] = str_replace("", " ", $buffer[$i]);
} else {
$buffer[$i] = str_replace("\t", " ", $buffer[$i]);
$buffer[$i] = str_replace("\n\n", "\n", $buffer[$i]);
$buffer[$i] = str_replace("\n", "", $buffer[$i]);
$buffer[$i] = str_replace("\r", "", $buffer[$i]);
while (stristr($buffer[$i], ' ')) {
$buffer[$i] = str_replace(" ", " ", $buffer[$i]);
}
}
$buffer_out .= $buffer[$i];
}
$final = strlen($buffer_out);
if ($initial !== 0) {
$savings = ($initial - $final) / $initial * 100;
} else {
$savings = 0;
}
$savings = round($savings, 2);
$buffer_out .= "\n";
return $buffer_out;
}
ob_start("gdk_compress_html_callback");
}
add_action('get_header', 'gdk_compress_html');
function gdk_unCompress($content)
{
if (preg_match_all('/(crayon-|)/i', $content, $matches)) {
$content = '' . $content;
$content .= '';
}
return $content;
}
add_filter('the_content', 'gdk_unCompress');
}