Last active
February 14, 2019 19:03
-
-
Save nczz/d435edd3555bc832d3b455341de95955 to your computer and use it in GitHub Desktop.
放在 WordPress 安裝的根目錄下執行~
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//提升執行環境 | |
set_time_limit(0); | |
ini_set('memory_limit', '512M'); | |
include dirname(__FILE__) . '/wp-load.php'; | |
global $wpdb, $pid; | |
$all_post_ids = $wpdb->get_results("SELECT ID FROM {$wpdb->prefix}posts WHERE post_status='publish'", ARRAY_A); | |
foreach ($all_post_ids as $key => $ID) { | |
global $pid; | |
$pid = $ID['ID']; | |
$post_content = $wpdb->get_results("SELECT post_content FROM {$wpdb->prefix}posts WHERE ID={$pid}", ARRAY_A); | |
$content = $post_content[0]['post_content']; | |
//抽取連結資料處理 | |
$content = preg_replace_callback('/<a\s+.*?href=[\"\'](http[s]{0,1}\:\/\/?[^\"\' >]*)[\"\']?[^>]*>/i', | |
function ($res) { | |
global $pid; | |
$link = $res[1]; | |
//確認是網址 | |
if (!filter_var($link, FILTER_VALIDATE_URL)) { | |
echo 'POST ID: ' . $pid . ' => ERROR LINK' . PHP_EOL; | |
//不是的話把網址拔掉,留個記錄 | |
return '<a class="links" href="#error_link" data-original-href="' . $link . '">'; | |
//或原封不動掛回去 | |
//return '<a class="links" href="' . $link . '">'; | |
} | |
//網址前處理 | |
//判斷是否為縮網址服務 Ref: https://bit.do/list-of-url-shorteners.php | |
$pass_list = array('t.co', 'lnkd.in', 'db.tt', 'qr.ae', 'adf.ly', 'goo.gl', 'bitly.com', 'cur.lv', 'tinyurl.com', 'ow.ly', 'bit.ly', 'ity.im', 'q.gs', 'is.gd', 'po.st', 'bc.vc', 'twitthis.com', 'u.to', 'j.mp', 'buzurl.com', 'cutt.us', 'u.bb', 'yourls.org', 'x.co', 'prettylinkpro.com', 'scrnch.me', 'filoops.info', 'vzturl.com', 'qr.net', '1url.com', 'tweez.me', 'v.gd'); | |
$parse = parse_url($link); | |
if (!in_array($parse['host'], $pass_list)) { | |
//不是就給它輸出吧! | |
echo 'POST ID: ' . $pid . ' => ' . $link . PHP_EOL; | |
return '<a class="links" href="' . $link . '">'; | |
} | |
//是,就開始擷取原位置,僅針對有按照 HTTP 協議做導向的短網址有效 | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $link); | |
curl_setopt($ch, CURLOPT_HEADER, true); | |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36'); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //跟隨轉址 | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_NOBODY, true); //吃皮就好,不要肉 | |
$headers = curl_exec($ch); //全部的標頭在此執行後取得 | |
$original_link = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); //取得最後有效的連結 | |
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); | |
curl_close($ch); | |
if ($httpcode != 200 || $original_link == "") { | |
echo 'POST ID: ' . $pid . ' (HTTP Status Code: ' . $httpcode . ') => ' . $original_link . PHP_EOL; | |
//短網址目標解析壞掉惹,掛回原本的 | |
return '<a class="links" href="' . $link . '#broken_link" data-original-href="' . $original_link . '">'; | |
} | |
/** | |
** 反解回來後的「原網址」要做處理可以在下面這段補上客製化程式 | |
**/ | |
echo 'POST ID: ' . $pid . ' (FINAL) => ' . $original_link . PHP_EOL; | |
return '<a class="links" href="' . $original_link . '" data-original-href="' . $link . '">'; | |
}, | |
$content); | |
//都處理完成後就更新文章 | |
$update_post = array( | |
'ID' => $pid, | |
'post_content' => $content, | |
); | |
wp_update_post($update_post); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment