[wordpress插件] Code Revisions代码修订

wordpress 插件 文章 5 天前 0 6 全屏看文

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.

使用WordPress 3.6中引入的新修订版查看器轻松查看修订。

This plugin is part of my Google Summer of Code 2013 project at WordPress.

此插件是我在WordPress上的Google Summer of Code 2013项目的一部分。

You can find more information on make/core.

您可以在 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.

inc / class-code-revisions.php:该类在所有页面上加载时会执行多项常规操作。

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

它添加了保存代码修订所需的自定义帖子类型( post_type()),并将用户从帖子编辑器( wp-admin / edit.php )重定向到

appropriate code editor (wp-admin/theme-editor.php or wp-admin/plugin-editor.php) when he tries to view those posts directly (

当他尝试直接查看这些帖子时,适当的代码编辑器( wp-admin / theme-editor.php wp-admin / plugin-editor.php



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-adminevisions

此外,该类还与WordPress版本还原过程挂钩,不仅可以还原帖子,还可以还原相关文件( restore()),并处理WordPress版本查看器的样式( wp-admin / revisions

.php) when viewing code revisions so it feels more code-editor-ish (styles()).

.php ),以便在查看代码修订时感觉更像是代码编辑器风格( styles())。

inc/code-revisions-editors.php: This file contains the Code_Revisions_Editors class which, in contrast to the Code_Revisions class in <

inc / code-revisions-editors.php:该文件包含 Code_Revisions_Editors 类,与<

code>class-code-revisions.php, is only loaded on the WordPress code editor pages using the load-plugin-editor.php and load-theme-editor.php

code> class-code-revisions.php ,仅使用 load-plugin-editor.php load-theme-editor.php加载到WordPress代码编辑器页面上



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

使用 POST GET 数据(如果可用),或者使用适当的默认文件,该类将生成一个包含有关当前查看文件的元信息的数组( 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’
    • 软件包:主题插件或“ plugin / plugin.php”

    • file: relative file path from the theme’s folder or the WordPress plugin directory
    • 文件:主题文件夹或WordPress插件目录中的相对文件路径

    • checksum: md5-checksum of the file
    • 校验和:文件的md5-校验和

    Using this data the plugin can check the database for a related post and retrieve it’s id if available (retrieve()).

    使用此数据,插件可以检查数据库中的相关帖子并检索ID(如果有)( 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()).

    如果没有,则该帖子将使用新内容进行更新(这将导致新的修订),并通过管理员通知( 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()).

    此过程保证存在一个带有初始文件内容的修订版本,用户可以将其还原为( handle_file_update())。

    Additionally the plugin tries to do a syntax check for *.php files to prevent breaking the WordPress installation (check_syntax()).

    另外,该插件会尝试对 *。php 文件进行语法检查,以防止破坏WordPress安装( check_syntax())。

    As mostly recommended the plugin utilizes php -l for this by writing the new contents to a temporary file.

    正如大多数推荐的那样,该插件通过将新内容写入临时文件来利用 php -l <​​/ code>。

    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.

    inc / plugged.php:包含可插拔 wp_text_diff()函数的稍微变化的版本。

    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.

    inc / metabox.php:修订版元框的模板。

    js/editors.js: JavaScript for customizing the code editor pages (wp-admin/plugin-editor.php and wp-admin/theme-

    js / editors.js:用于自定义代码编辑器页面的JavaScript( wp-admin / plugin-editor.php 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.

    css / editors.css :代码编辑器的样式。

    Enqueued in inc/class-code-revisions-editors.php -> scripts().

    排入 inc / class-code-revisions-editors.php-> scripts()

    css/viewer.css: Styles for the revision viewer (wp-adminevisions.php).

    css / viewer.css :修订版查看器的样式( wp-admin / revisions.php )。

    Enqueued only when viewing code revisions in inc/class-code-revisions.php -> styles().

    仅当在 inc / class-code-revisions.php-> styles()中查看代码修订时才入队。