午夜精品人妻久久久-成年美女很黄的网站-在线看片免费人成视久网app-国产精品美女无遮挡一区二区-91精品国产综合久久久久-国产的免费视频又猛又爽又刺激-在线看片免费人成视久网app-久久香蕉国产精品视频-av一区二区三区高清

用最古老的 WordPress 系統(tǒng),寫最現(xiàn)代的 PHP 代碼!

【用最古老的 WordPress 系統(tǒng),寫最現(xiàn)代的 PHP 代碼!】我們知道 WordPress 的函數(shù)在失敗的時(shí)候是不會拋出異常的,因?yàn)?WordPress 在 PHP 4 的時(shí)候就創(chuàng)建了,那時(shí)候 PHP 語法結(jié)構(gòu)還沒有 try/catch 異常處理機(jī)制 。
WP_Error 錯(cuò)誤處理機(jī)制
取而代之,WordPress 在失敗的時(shí)候,返回返回一個(gè)WP_Error對象,比如插入文章的函數(shù) wp_insert_post,如果文章標(biāo)題,內(nèi)容摘要都為空的時(shí)候,就會插入失敗 , 會返回 WP_Error 對象 。
WP_Error 對象錯(cuò)誤對象和異常很類似,也有一個(gè)錯(cuò)誤代碼和錯(cuò)誤信息 , 比如上面的錯(cuò)誤,返回 WP_Error 對象的錯(cuò)誤代碼就是 empty_content,錯(cuò)誤信息是:內(nèi)容,標(biāo)題和摘要為空 。
WordPress 還提供了 is_wp_error 函數(shù) , 用于判斷接受到數(shù)據(jù)是不是 WP_Error 對象,這樣我們在寫代碼的時(shí)候,就需要自己判斷返回值是不是 WP_Error 對象,然后進(jìn)行額外處理 , 舉個(gè)例子,WPJAM Basic 的快速復(fù)制擴(kuò)展功能的代碼:
function wpjam_duplicate_post($post_id){ // 獲取舊文章信息,并插入新文章 $post_arr = get_post($post_id, ARRAY_A); $new_post_id = wp_inssert_post($post_arr, $wp_error=true); if(is_wp_error($new_post_id)){ // 如果失敗,返回錯(cuò)誤 return $new_post_id; } // 獲取舊文章的分類信息 , 并將同樣的分類信息設(shè)置到新的文章中 foreach(get_object_taxonomies($post_arr[post_type]) as $taxonomy){ $terms = wp_get_object_terms($post_id, $taxonomy, [fields => ids]); $result = wp_set_object_terms($new_post_id, $terms, $taxonomy); if(is_wp_error($result)){ // 如果失敗,返回錯(cuò)誤 return $result; } } // 假如還有其他操作 $result = other_post_function($new_post_id, $args); if(is_wp_error($result)){ // 如果失敗,返回錯(cuò)誤 return $result; } return $new_post_id; // 最后才返回復(fù)制成功的文章 ID }上面的代碼我為了方便演示,做了一些簡化,留下大致的骨架,可以看出快速復(fù)制文章有三個(gè)過程,注釋里面已經(jīng)寫的非常清楚,下面簡單說一下
獲取舊文章信息,并插入新文章,如果 WP_Error 對象,則直接返回 。獲取舊文章的分類信息,并將同樣的分類信息設(shè)置到新的文章中,同樣碰到 WP_Error 對象,則直接返回最后假設(shè)還有其他操作 , 同樣也要處理錯(cuò)誤 。
這樣的代碼給人感覺就是滿屏的錯(cuò)誤處理,非常難受 。
使用 Try / Catch 異常處理機(jī)制
有沒有辦法優(yōu)化我們的代碼呢?可以把 WP_Error 對象轉(zhuǎn)換成 PHP 異常繼承類的對象,然后使用現(xiàn)代 PHP 的 Try / Catch 異常處理機(jī)制來優(yōu)化 。
首先創(chuàng)建用于處理 WP_Error 對象的異常處理類:
class WPJAM_Exception extends Exception{ private $wp_error = null; public function __construct($message, $code=0, Throwable $previous=null){ if(is_wp_error($message)){ $this->wp_error = $message; $message = $this->wp_error->get_error_message(); $code = $this->wp_error->get_error_code(); }else{ $this->wp_error = new WP_Error($code, $message); } parent::__construct($message, 0, $previous); } public function get_wp_error(){ return $this->wp_error; } }創(chuàng)建一個(gè)高階函數(shù) wpjam_try,自動將 WP_Error 對象轉(zhuǎn)換成異常:
function wpjam_try($callback, ...$args){ try{ $result = call_user_func_array($callback, $args); if(is_wp_error($result)){ throw new WPJAM_Exception($result); } return $result; }catch(Exception $e){ throw $e; } }最后我們就可以使用 wpjam_try 對上面復(fù)制文章這段代碼進(jìn)行改造了
function wpjam_duplicate_post($post_id){ try{ // 獲取舊文章信息 , 并插入新文章 $post_arr = get_post($post_id, ARRAY_A); $new_post_id = wpjam_try(wp_inssert_post, $post_arr, $wp_error=true); // 獲取舊文章的分類信息,并將同樣的分類信息設(shè)置到新的文章中 foreach(get_object_taxonomies($post_arr[post_type]) as $taxonomy){ $terms = wp_get_object_terms($post_id, $taxonomy, [fields => ids]); $result = wpjam_try(wp_set_object_terms, $new_post_id, $terms, $taxonomy); } // 假如還有其他操作 $result = wpjam_try(other_post_function, $new_post_id, $args); return $new_post_id; // 最后才返回復(fù)制成功的文章 ID }catch(WPJAM_Exception $e){ if($exception){ throw $e; }else{ return $e->get_wp_error(); } } }改造的過程分成三步:
把會返回 WP_Error 對象的函數(shù),通過 wpjam_try 調(diào)用 。去掉所有 is_wp_error 的判斷,因?yàn)?wpjam_try 會拋出異常 。將所有代碼放到 try/catch 的結(jié)構(gòu)中,最后只需要捕捉異常,再將異常轉(zhuǎn)換成 WP_Error 對象即可 。
這樣就可以在 WordPress 寫代碼的時(shí)候,避免滿屏幕的錯(cuò)誤處理,最后返回還是 WP_Error 對象 , 保證了對原來邏輯的兼容 。