前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存

WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存

作者头像
张戈
发布2018-03-21 15:13:39
8000
发布2018-03-21 15:13:39
举报

上一篇文章分享了WordPress 发布文章评论自动刷新腾讯云 CDN 的教程,而博客现在还用到了 VeryCloud 的 CDN,正好有朋友在文章后面留言说 VC 也有刷新缓存的 API,于是就利用中午的时间折腾了下,成功搞定!

WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存
WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存

下面分享一下部署方法。

/**
* WordPress 发布/更新文章、提交/审核评论自动清理VeryCloud CDN缓存(首页、分类以及当前文章) By 张戈博客
* 文章地址:https://zhangge.net/5090.html
* 转载请保留出处,谢谢合作!
**/

//发布、更新文章刷新缓存
add_action('publish_post', 'refresh_By_Publish', 0);
//提交评论刷新缓存
add_action('comment_post', 'refresh_By_Comments',0);
//审核评论刷新缓存
add_action('comment_unapproved_to_approved', 'refresh_By_Approved',0);

define('CRYPT_SALT', 'verycloud#cryptpass');
$cdn_provider = array(
	"verycdn" => array(
		"push" => "https://api3.verycloud.cn/API/cdn/refresh",
		"token" => "https://api3.verycloud.cn/API/OAuth/authorize",
		"username" => "此处填写verycloud用户名",
		"password" => "此处填写verycloud密码",
	),
);

//发布文章更新文章、分类和首页的缓存函数
function refresh_By_Publish($post_ID)
{
    $pageurl    = get_permalink($post_ID);
    $homeurl    = home_url();
    
    $category   = get_the_category();
    if($category[0]){
        //如果文章属于多个分类,默认只刷新第一个分类地址
        $caturl = get_category_link($category[0]->term_id );
        $url    = $pageurl . ',' . $caturl . ',' . $homeurl;
    } else {
        $url    = $pageurl . ',' . $homeurl;
    }
    
    $data = array(
		'type' => 'file',
		'urls' => $url
	);
    refresh($data);
}

//提交评论刷新当前文章CDN缓存
function refresh_By_Comments($comment_id)
{
    $comment = get_comment($comment_id);
    $url = get_permalink($comment->comment_post_ID);
    
    $data = array(
		'type' => 'file',
		'urls' => $url,
	);
    refresh($data);
}

//审核评论刷新当前文章CDN缓存
function refresh_By_Approved($comment)
{
    $url = get_permalink($comment->comment_post_ID);
    $data = array(
		'type' => 'file',
		'urls' => $url,
	);
    refresh($data);
}

function post_data($url, $data, $type = "POST") {
    $ch = curl_init();
  
    // 使用TLS1.0
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1	

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 1 );
    curl_setopt($ch, CURLOPT_POST, true);

    if ( $type == "POST" ) {
	$parm_string = http_build_query($data, '&');
	curl_setopt($ch, CURLOPT_POSTFIELDS, $parm_string);
    } else {
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

//生成tokens
function get_verycdn_token() {
	global $cdn_provider;
	$post_data["username"] = $cdn_provider["verycdn"]["username"];
	$post_data["password"] = encrypt($cdn_provider["verycdn"]["password"]);

	$res = post_data($cdn_provider["verycdn"]["token"], $post_data);
	
	$result = json_decode($res);
	return $result->access_token;
	//var_dump($post_data);
	//var_dump($result);
}

function encrypt($txtStream) {
        //以下行中的字符串可能需要找客服提供,具体请自行试试。
	$lockstream = 'st=lDEFABCNOPyzghi_jQRST-UwxkVWXYZabcdefIJK6/7nopqr89LMmGH012345uv';
	$lockLen = strlen($lockstream);
	$lockCount = rand(0, $lockLen-1);
	$randomLock = $lockstream[$lockCount];
	$password = md5(CRYPT_SALT . $randomLock);
	$txtStream = base64_encode($txtStream);
	$tmpStream = '';
	$i = 0; $j = 0; $k = 0;
	for($i = 0; $i < strlen($txtStream); $i++) {
		$k = ($k == strlen($password)) ? 0 : $k;
		$j = (strpos($lockstream, $txtStream[$i]) + $lockCount + ord($password[$k])) % ($lockLen);
		$tmpStream .= $lockstream[$j];
		$k++;
	}
	return $tmpStream . $randomLock;
}

/**
* 提交刷新
* @param $data
* @return array
*/
function refresh($data) {
  	global $cdn_provider;
    $token = get_verycdn_token();
    if(!$token) {
      return array(
        'code' => 0,
        'message' => 'unable to get token'
      );
    }
    //刷新类型 file 文件 dir 目录
    $type = isset($data['type']) && !empty($data['type']) ? trim($data['type']) : '';
    //刷新url
    $urls = isset($data['urls']) && !empty($data['urls']) ? $data['urls'] : '';
    //url分隔符,多个url使用该符号分隔,默认,
    $partition = isset($data['partition']) && !empty($data['partition']) ? trim($data['partition']) : ',';
    if(empty($type) || empty($urls)) {
      return array(
        'code' => 0,
        'message' => 'type and urls are required',
      );
    }

    $url = $cdn_provider["verycdn"]["push"];

    $send_data = array(
      'token' => $token,
      'type' => $type,
      'urls' => $urls,
      'partition' => $partition
    );

    $return = post_data($url, $send_data);
    return $return;
}

将以上代码粘贴到 WordPress 主题 functions.php 中,然后将 19,20 行对应的中文改成 VeryCloud 的用户名和密码,保存即可。

Ps:貌似 VC 的缓存刷新 API 暂时还没完全公开,如果需要部署这个功能,需要联系客服,然后告知需要使用这个刷新 CDN 缓存的 API,然后提供以下用户名给他就好了。而且代码中的 lockstream 的值可能需要 VC 客服提供,如果发现上述代码无法成功,请自行咨询 VC 客服。

部署好了之后,可以去更新文章或提交评论,然后登陆 VeryCloud 云分发后台,即可看到提交记录:

WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存
WordPress发布/更新文章、提交/审核评论自动清理VeryCloud缓存

至此,说明你已部署成功。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年03月22日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
内容分发网络 CDN
内容分发网络(Content Delivery Network,CDN)通过将站点内容发布至遍布全球的海量加速节点,使其用户可就近获取所需内容,避免因网络拥堵、跨运营商、跨地域、跨境等因素带来的网络不稳定、访问延迟高等问题,有效提升下载速度、降低响应时间,提供流畅的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档