
在本系列的这一点上,我们已准备好通过记录我们的文件、类、函数、变量等来完善我们的插件。
虽然这是我们实际上必须完成插件的最后一步,但这并不是该系列的最后一篇文章,因为我们将继续研究面向对象编程中的一些高级主题。
但在此之前,让我们将我们在上一篇文章中学到的所有内容付诸实践,将我们的插件升级到 1.0 版本。
当然,与之前的所有文章一样,我建议您及时了解我们迄今为止介绍的所有内容,这样您不仅可以完全了解我们所做的工作在上一篇文章中,但我们实际上是如何达到本文讨论的最后一点的。
- 简介
- 课程
- 类型
- 控制结构:条件语句
- 控制结构:循环
- 函数和属性
- 范围
- 构建插件 I
- 构建插件 II
- 记录插件 I
了解和审查了所有这些内容后,让我们开始记录每个文件。
记录插件
我们可以通过多种不同的方式来记录这个插件:
- 我们可以首先记录所有文件头,然后我们可以回来记录类,然后我们可以回来记录变量,然后我们可以记录函数。
- 我们可以一次记录每个文件,并对每个文件中包含的所有内容进行简短的讨论。
显然,该选项将为每个部分生成更多文档,但应该会导致一篇不那么乏味的文章,并且更容易理解整个插件的控制流程。
为此,我们将逐个文件地研究该插件,为我们拥有的每段代码引入文档,然后我们将讨论代码后面的任何感兴趣的点。
最后,我们将确保在文章末尾引用该插件的最终版本。话虽如此,让我们开始吧。
单个帖子元管理器
回想一下,启动插件的主文件是位于插件目录根目录中的 single-post-meta-manager.php 文件。
以下是该文件的完整文档版本。仔细阅读每条评论,不仅要注意其遵循的格式,还要注意其提供的内容。
run(); } // Call the above function to begin execution of the plugin. run_single_post_meta_manager();
在上面的代码中,请注意,我们已经根据上一篇文章中概述的约定定义了文件头。我们还维护了所需的插件标头标签,以便 WordPress 正确读取它们。
请注意,在本例中,我们已将它们包含在自定义 @wordpress-plugin 标记下。这不是必需的,但有助于将文件头注释与所需的插件注释分开。
最后,请注意,我们已将此插件的版本提升至 1.0,并且我们还为该插件指定了 @package 值="inline">SPMM 缺少Single Post Meta Manager。我们将在整个插件中使用它。
包含目录
接下来,让我们将注意力转向包含目录中的所有文件。
由于在管理目录中的任何内容之前都需要这些文件,因此单独查看这些文件中的每一个是有意义的,然后用管理目录中的其余文件来完善我们的讨论。
单个帖子元管理器
plugin_slug = 'single-post-meta-manager-slug';
$this->version = '1.0.0';
$this->load_dependencies();
$this->define_admin_hooks();
}
/**
* Imports the Single Post Meta administration classes, and the Single Post Meta Loader.
*
* The Single Post Meta Manager administration class defines all unique functionality for
* introducing custom functionality into the WordPress dashboard.
*
* The Single Post Meta Manager Loader is the class that will coordinate the hooks and callbacks
* from WordPress and the plugin. This function instantiates and sets the reference to the
* $loader class property.
*
* @access private
*/
private function load_dependencies() {
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-single-post-meta-manager-admin.php';
require_once plugin_dir_path( __FILE__ ) . 'class-single-post-meta-manager-loader.php';
$this->loader = new Single_Post_Meta_Manager_Loader();
}
/**
* Defines the hooks and callback functions that are used for setting up the plugin stylesheets
* and the plugin's meta box.
*
* This function relies on the Single Post Meta Manager Admin class and the Single Post Meta Manager
* Loader class property.
*
* @access private
*/
private function define_admin_hooks() {
$admin = new Single_Post_Meta_Manager_Admin( $this->get_version() );
$this->loader->add_action( 'admin_enqueue_scripts', $admin, 'enqueue_styles' );
$this->loader->add_action( 'add_meta_boxes', $admin, 'add_meta_box' );
}
/**
* Sets this class into motion.
*
* Executes the plugin by calling the run method of the loader class which will
* register all of the hooks and callback functions used throughout the plugin
* with WordPress.
*/
public function run() {
$this->loader->run();
}
/**
* Returns the current version of the plugin to the caller.
*
* @return string $this->version The current version of the plugin.
*/
public function get_version() {
return $this->version;
}
}
显然,这个特定文件中引入了很多新评论;但是,每个类属性、构造函数和内部函数的作用应该是非常不言自明的。
除了如何通过插件协调信息之外,需要注意的关键是我们如何遵守上一篇文章中定义的标准。
但请注意,如果文档中的某些标签和/或功能在不相关时不使用,我们就冒昧地使用它们。我们将在本文的其余部分中继续这样做。
单个帖子元管理器加载器
actions = array();
$this->filters = array();
}
/**
* Registers the actions with WordPress and the respective objects and
* their methods.
*
* @param string $hook The name of the WordPress hook to which we're registering a callback.
* @param object $component The object that contains the method to be called when the hook is fired.
* @param string $callback The function that resides on the specified component.
*/
public function add_action( $hook, $component, $callback ) {
$this->actions = $this->add( $this->actions, $hook, $component, $callback );
}
/**
* Registers the filters with WordPress and the respective objects and
* their methods.
*
* @param string $hook The name of the WordPress hook to which we're registering a callback.
* @param object $component The object that contains the method to be called when the hook is fired.
* @param string $callback The function that resides on the specified component.
*/
public function add_filter( $hook, $component, $callback ) {
$this->filters = $this->add( $this->filters, $hook, $component, $callback );
}
/**
* Registers the filters with WordPress and the respective objects and
* their methods.
*
* @access private
*
* @param array $hooks The collection of existing hooks to add to the collection of hooks.
* @param string $hook The name of the WordPress hook to which we're registering a callback.
* @param object $component The object that contains the method to be called when the hook is fired.
* @param string $callback The function that resides on the specified component.
*
* @return array The collection of hooks that are registered with WordPress via this class.
*/
private function add( $hooks, $hook, $component, $callback ) {
$hooks[] = array(
'hook' => $hook,
'component' => $component,
'callback' => $callback
);
return $hooks;
}
/**
* Registers all of the defined filters and actions with WordPress.
*/
public function run() {
foreach ( $this->filters as $hook ) {
add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
}
foreach ( $this->actions as $hook ) {
add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ) );
}
}
}
请注意,此类或多或少是插件的核心组件,因为它协调整个插件中使用的所有操作和过滤器。该插件集中了整个插件中使用的钩子的所有注册和协调。
最后,当调用 run 时,所有挂钩都会向 WordPress 注册,因此当插件启动时,它将调用每个注册的操作和过滤器。
admin 目录
此时,我们准备将注意力转向位于插件管理目录中的文件。
虽然该文件由几个 PHP 文件组成,但它还包含一个 CSS 文件。出于本文的目的,我们不会记录 CSS 文件;我们将编写 CSS 文件。但是,WordPress Codex确实为此定义了文档。
现在,让我们继续记录 admin 目录中存在的类和文件。
单个帖子元管理器管理员
Single Post Meta Manager Admin 类具有单一职责:定义为仪表板呈现元框及其样式的功能。
version = $version;
}
/**
* Enqueues the style sheet responsible for styling the contents of this
* meta box.
*/
public function enqueue_styles() {
wp_enqueue_style(
'single-post-meta-manager-admin',
plugin_dir_url( __FILE__ ) . 'css/single-post-meta-manager-admin.css',
array(),
$this->version,
FALSE
);
}
/**
* Registers the meta box that will be used to display all of the post meta data
* associated with the current post.
*/
public function add_meta_box() {
add_meta_box(
'single-post-meta-manager-admin',
'Single Post Meta Manager',
array( $this, 'render_meta_box' ),
'post',
'normal',
'core'
);
}
/**
* Requires the file that is used to display the user interface of the post meta box.
*/
public function render_meta_box() {
require_once plugin_dir_path( __FILE__ ) . 'partials/single-post-meta-manager.php';
}
}
请注意,上面的类只有很少的功能细节。主要是,该类维护对插件版本的引用、用于设置元框样式的样式表以及实际渲染元框所需的函数。
回想一下,所有这些都是在核心插件文件和加载器中设置的。这有助于解耦插件中存在的逻辑,以便每个类都可以专注于其主要目的。
当然,插件的最后一部分依赖于包含显示元框所需标记的实际部分文件。
单个帖子元管理器部分
$post_meta_value ) { ?>
这应该是相对不言自明的;但是,为了完整起见,请注意此文件获取当前帖子 ID(通过使用 get_the_ID() 函数),读取帖子元数据,然后迭代它构建一个表显示键和值。
完成插件
至此,我们已经完成了插件的实现。从实施面向对象的编程实践,到记录代码。
您可以在 GitHub 上获取该插件的最终版本;但是,我们将在更多帖子中继续进行面向对象的讨论,以便我们可以探索一些更高级的主题,例如继承、抽象和其他主题。
同时,如果您对该插件有疑问或意见,请随时在评论中留下!










