[WordPress] 外掛分享: Code Revisions

首頁外掛目錄 › Code Revisions
WordPress 外掛 Code Revisions 的封面圖片
10+
安裝啟用
★★★★
4.2/5 分(6 則評價)
4549 天前
最後更新
問題解決
WordPress 3.6+ v1.0 上架:2013-08-26

內容簡介

此外掛將幫助您跟踪通過 WordPress 代碼編輯器進行的佈景主題和外掛文件變更。您不再需要擔心不良更改可能會破壞某些內容,因為您可以隨時恢復文件的較舊版本。此外,此外掛將幫助您重新執行更改,當這些更改可能已被插件更新覆蓋,將輕鬆顯示更改內容。修訂版以 WordPress 原生方式處理。使用 WordPress 3.6 引入的新修訂檢視器舒適地查看修訂版。

此外掛是我在 WordPress 的 2013 年 Google Summer of Code 專案的一部分。您可以在 make/core 上找到更多訊息。如果您有興趣了解更多背景資訊,它也出現在 wptavern.com 上。

開發人員指南
- code-revisions.php:主要的外掛檔案。它定義常量、載入其他檔案、在適當的情況下實體化類別並包含卸載自動化方法。
- inc/class-code-revisions.php:在所有頁面加載此類別,它執行多個一般性操作。它添加所需的自訂文章類型以保存程式碼修訂版(post_type()),並在用戶嘗試直接查看這些文章(redirect())時將用戶從文章編輯器(wp-admin/edit.php)重定向到適當的代碼編輯器(wp-admin/theme-editor.php 或 wp-admin/plugin-editor.php)。此外,此類別連接到 WordPress 修訂版還原程序,不僅還原文章,還還原相關的檔案(restore()),並處理 WordPress 修訂版檢視器(wp-admin/revisions.php)的樣式,以使其感覺更像程式碼編輯器(styles())。
- inc/code-revisions-editors.php:此檔案包含 Code_Revisions_Editors 類別,它與 class-code-revisions.php 中的 Code_Revisions 類別相比,只會在使用 load-plugin-editor.php 和 load-theme-editor.php 鉤子載入 WordPress 代碼編輯器頁面上載入。使用 POST 和 GET 資料或回退到適當的預設檔案之一,該類別會生成包含當前查看的檔案的元資訊陣列(generate_meta())。爲了具有較少的佈景主題或外掛專有程式碼,此陣列包含 4 個字串:
- 類型:'plugin'/'theme'
- 套件:佈景主題 Slug 或 'plugin/plugin.php'
- 檔案:從佈景主題的資料夾或 WordPress 外掛目錄的相對檔案路徑
- 校驗和:檔案的 md5 校驗和
- 利用此資料,外掛可以檢查數據庫是否有相關的文章並檢索其 ID(如果可用)(retrieve())。元資訊陣列存儲為自訂文章的附加資料,並沿著文章一起儲存檔案。
- 當在編輯器中打開檔案並在數據庫中找到相關的文章時,外掛會檢查文章的內容和檔案的內容是否仍相符。如果它們不相符,則更新文章與新內容(導致新的修訂版),並使用管理員通知通知使用者存在變更(handle_direct_changes())。
- 在透過代碼編輯器進行檔案更新時,外掛會在 WordPress 寫入檔案之前檢查檔案是否實際有更動。僅在發現更改時才需要建立修訂版。如果檔案尚未與文章相關聯,它將創建具有舊內容的新文章。然後更新此文章的新內容。此過程保證有一個具有初始檔案內容的修訂版。

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.0) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Code Revisions」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

This plugin will help you to keep track of changes made to theme and plugin files through the WordPress code editors. You no longer need to worry about possibly breaking something with bad changes because you can always return to an older version of the file. Additionally the plugin helps you to redo your changes when they might have been overwritten by a plugin update by easily showing you what changed. The revisions are handled in a way native to WordPress. Comfortably view revisions using the new revision viewer introduced in WordPress 3.6.
This plugin is part of my Google Summer of Code 2013 project at WordPress. You can find more information on make/core. It was also featured on wptavern.com if you are interested in some background information.
Developer’s Guide
code-revisions.php: The main plugin file. It defines constants, loads the other files, instantiates the classes if appropriate and contains the uninstall automatism.
inc/class-code-revisions.php: Loaded on all pages this class does multiple general things. It adds the custom post type required for saving the code revisions (post_type()) and redirects the user from the post editor (wp-admin/edit.php) to the appropriate code editor (wp-admin/theme-editor.php or wp-admin/plugin-editor.php) when he tries to view those posts directly (redirect()). Further more this class hooks into the WordPress revision restore process to not only restore the post but also the related file (restore()) and handles styling the WordPress revision viewer (wp-admin/revisions.php) when viewing code revisions so it feels more code-editor-ish (styles()).
inc/code-revisions-editors.php: This file contains the Code_Revisions_Editors class which, in contrast to the Code_Revisions class in class-code-revisions.php, is only loaded on the WordPress code editor pages using the load-plugin-editor.php and load-theme-editor.php hooks. Using either, if available, POST and GET data or falling back to the appropriate default file the class generates an array containing meta information on the currently viewed file (generate_meta()). In an attempt to have as less theme or plugin file specific code this array contains 4 strings:

type: ‘plugin’/’theme’
package: theme slug or ‘plugin/plugin.php’
file: relative file path from the theme’s folder or the WordPress plugin directory
checksum: md5-checksum of the file

Using this data the plugin can check the database for a related post and retrieve it’s id if available (retrieve()). The meta information array is stored as custom post meta data alongside a file’s post.
When a file is opened in the editor and a related post is found in the database the plugin checks if the post’s content and the file’s content still match. If they don’t the post is updated with the new content (which results in a new revision) and the user is notified about the change using an admin notice (handle_direct_changes()).
On file updates through the code editor the plugin checks if the file has actually changed before WordPress writes to it. Only when changes are found a revision needs to be created. If no post is associated with the file yet a new post is created with the old contents. This post is then updated with the new content. This process guarantees that there is a revision with the initial file content to which the user can revert to (handle_file_update()). Additionally the plugin tries to do a syntax check for *.php files to prevent breaking the WordPress installation (check_syntax()). As mostly recommended the plugin utilizes php -l for this by writing the new contents to a temporary file. If this feature is not available a more basic check using eval is performed. When a syntax error is found the actual file is not written, but the user is redirected back to the editor with a notification about the error and it’s location with line highlighting.
inc/plugged.php: Contains a slightly changed version of the pluggable wp_text_diff() function. wp_text_diff() is utilized for generating the diffs rendered in the revision viewer. Normally it strips leading, trailing and multiple successive whitespaces. However this behavior is not very helpful when viewing code revisions, wherefore the plugin suppresses it for revisions associated with the custom code revisions post type.
inc/metabox.php: The template for the revision metabox.
js/editors.js: JavaScript for customizing the code editor pages (wp-admin/plugin-editor.php and wp-admin/theme-editor.php). It adds the revisions metabox below the editors, the revisions text with link next to the ‘Update File’ button and handles the text replacement and line highlighting when a syntax error was found.
css/editors.css: Styles for the code editors. Enqueued in inc/class-code-revisions-editors.php -> scripts().
css/viewer.css: Styles for the revision viewer (wp-admin/revisions.php). Enqueued only when viewing code revisions in inc/class-code-revisions.php -> styles().

延伸相關外掛

文章
Filter
Apply Filters
Mastodon