iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 17
1
自我挑戰組

Wordpress 外掛開發系列 第 17

「Wordpress 外掛開發」版本控制與後續維護

對於開發的過程,你在你的vendor之中,可能會有安裝很多的東西,而在上版控的時候,不要去將他加入進去了,在網路上有很多的autoloading與composer的教學,我有整理一些在下方可以參考一下。

在官網之中是推薦用SVN來用,不過現在主流應該是以git來做版空,也是主流,在.git與.gitignore也有更多的資源去製作,也支援大部分像是gitlab或是github來上傳,也是我比較熟悉的使用方式,而這些在CI/CD都已經有整合好了,只需要寫好production是可以直接產出要上傳的zip來做,可以省掉不少時間。

而PHPUnit做測試,這次鐵人賽有不少大佬做覆蓋率的文章,就是提到了這個,而這個開發的流程又可以提到所謂的Test-driven developmentBehavior-driven development,那前者就不提覆蓋率,以測試為主要開發的需求當然是要求到最高,而後者以使用者邏輯開發則是要求在80以上就是個符合自己coding style的好程式了!而這個套件做測試很容易,與一班的e2e很像,把assertion加入完整,就可以得到覆蓋率了。

public function test_term_query_count() {
  $tags = self::factory()->term->create_many( 3, array(
  'taxonomy' => 'post_tag',
  ) );
 
  $term_query = new WP_Term_Query();
  $actual = $term_query->query( array(
  'taxonomy' => 'post_tag',
  'fields' => 'count',
  ) );
 
  $this->assertSame( 3, $actual );
}

那對於你自己的物件中如果有factory這一些要處理,也是可以藉由呼叫object來取得factory,那我們這邊可以使用create_many來確定物件是否是unique,像是email或是username這類的,不過我對於寫測試並不是專家,test class寫好,至少把基礎的測試給寫完整,給同事一個好的開發環境,造福大千世界。

維護模式

那在上版控之後,就算是加入unit也是沒辦法百分之百的確定,所以我們更需要所謂的維護模式,那以我的定義來說,全部的頁面基本上都會跳轉到我們的維護通知介面,而我們可能需要有一個offlin.php來制定,在我們的使用上,可以使用簡單的方式就轉到維護介面,但是更精準的方式我覺得可以使用.htaccess來製作,那我們在進入到maintance mode就可以寫sh來執行,而我們主要是來將htacess的rewrite重新撰寫。

RewriteEngine On

# Add all the IP addresses of people that are helping in development
# and need to be able to get past the maintenance mode.
# One might call this the 'allow people list'
RewriteCond %{REMOTE_HOST} !^83\.101\.79\.62
RewriteCond %{REMOTE_HOST} !^91\.181\.207\.191

# Make sure the maintenance mode only applies to this domain
# Example: I am hosting different sites on my server
# which could be affected by these rules.
RewriteCond %{HTTP_HOST} ^nocreativity.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.nocreativity.com$

# This is the 'ignore file list'. It allows access to all
# files that are needed to display the maintenance mode page.
# Example: pages, css files, js files, images, anything.
# IMPORTANT: If you don't do this properly, visitors will end up with
# endless redirect loops in their browser.
RewriteCond %{REQUEST_URI} !/offline\.htm$
RewriteCond %{REQUEST_URI} !/css\/style\.css$
RewriteCond %{REQUEST_URI} !/images\/logo\.png$

# Rewrite whatever request is coming in to the maintenance mode page
# The R=302 tells browsers (and search engines) that this
# redirect is only temporarily.
# L stops any other rules below this from executing whenever somebody is redirected.
RewriteRule \.*$ /offline.htm [R=302,L]

而這個方式是比較確實,不過比較麻煩,現在是有plugin可以將整個wordpress進入到維護模式,可以查看WP maintenance mode plugin,進到設定設置維護模式就可以使用了,當然我們也可以使用前面的邏輯,加入action來做最簡單的設置,不過這一個如果是碰到系統有漏洞做到權限提升就吃鱉了。

function wp_maintenance_mode() {
  if (!current_user_can('administrator') || !is_user_logged_in()) {
  wp_die('<h1>Under Maintenance</h1><br />Website under planned maintenance. Please check back later.');
  }
}
add_action('get_header', 'wp_maintenance_mode');

最後,大家都在中秋連假,而我還在繼續工作並且還得加緊寫鐵人賽真是讓人完全開心不起來,不過明天還是得繼續上班,明天就來寫比較簡易的主題,Rest Api,endpoint 讓人覺得很有彈性,不可錯過的好API!

Reference

Setting up Version Control for Wordpress plugin development
codeX - coding standard
PHP Composer 是什么技术?
Using Composer With WordPress
Autoloading
composer basic usage
how to set maintenance mode for entire website with htaccess
How to Put WordPress in Maintenance Mode
Which action is triggered when a theme was modified?


上一篇
「Wordpress 外掛開發更新後的外掛,資料庫也需要 - wpdb
下一篇
「Wordpress 外掛開發」建立REST API以及避免原生的設計造成零時差攻擊 - WP-JSON
系列文
Wordpress 外掛開發30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言